function [xyres,slon,slat,dx,dy,dlon,dlat,u10,v10] = read_gridded_hwind_data(hwindfile)

% Usage: [xyres,slon,slat,dx,dy,dlon,dlat,u10,v10] = read_gridded_hwind_data(hwindfile)

% This script processes the raw HWIND gridded data and extract all the
% contained information


fid   = fopen(hwindfile, 'r');

% Read header 1
[c,fpos1] = textscan(fid, 'SURFACE WIND COMPONENTS FOR HURRICANE %s');
% Read header 2
[c,fpos1] = textscan(fid, 'DX=DY= %f KILOMETERS.');
xyres     = c{1};
% Read header 3
[c,fpos1] = textscan(fid, 'STORM CENTER LOCALE IS %f EAST LONGITUDE and %f NORTH LATITUDE ... STORM CENTER IS AT (X,Y)=(0,0)');
slon      = c{1};
slat      = c{2};
% Read x coordinates
[c,fpos1] = textscan(fid,'MERCATOR X COORDINATES ... KILOMETERS');
[c,fpos1] = textscan(fid, '%n',1);
numx      = c{1};
[c,fpos1] = textscan(fid, '%n',numx);
dx        = c{1};
% Read y coordinates
[c,fpos1] = textscan(fid,'MERCATOR Y COORDINATES ... KILOMETERS');
[c,fpos1] = textscan(fid, '%n',1);
numy      = c{1};
[c,fpos1] = textscan(fid, '%n',numy);
dy        = c{1};
% Read longitudes
[c,fpos1] = textscan(fid,'EAST LONGITUDE COORDINATES ... DEGREES');
[c,fpos1] = textscan(fid, '%n',1);
numxl     = c{1};
if ~(numx == numxl)
    warning(['Error reading HWIND file ',hwindfile]);
    disp('X coordinate size does not match longitude coordinate size.');
    return
end
[c,fpos1] = textscan(fid, '%n',numxl);
dlon      = c{1};
% Read latitudes
[c,fpos1] = textscan(fid,'NORTH LATITUDE COORDINATES ... DEGREES');
[c,fpos1] = textscan(fid, '%n',1);
numyl     = c{1};
if ~(numy == numyl)
    warning(['Error reading HWIND file ',hwindfile]);
    disp('Y coordinate size does not match latitude coordinate size.');
    return
end
[c,fpos1] = textscan(fid, '%n',numyl);
dlat      = c{1};
% Read data header
[c,fpos1] = textscan(fid,'SURFACE WIND COMPONENTS ... M/S ... COMPLEX ARRAY W=(U,V)');
% Read data size
[c,fpos1] = textscan(fid, '%n%n',1);
numxd     = c{1};
numyd     = c{2};
if ~(numx == numxd)
    warning(['Error reading HWIND file ',hwindfile]);
    disp('X coordinate size does not match data X size.');
    return
end
if ~(numy == numyd)
    warning(['Error reading HWIND file ',hwindfile]);
    disp('Y coordinate size does not match data Y size.');
    return
end
% Read data
[c,fpos1] = textscan(fid, '(%n,%n)');
u10       = reshape(c{1},numxd,numyd)';
v10       = reshape(c{2},numxd,numyd)';

