function [speed, dir, lat, lon, land, speeds, dirs, choices, cstart, astart]=loadBYUhrwind(name);
%
% [speed,dir,lat,lon,land,speeds,dirs,choices]=loadBYUhr(name);
% matlab script for reading BYU high resolution wind fields
% BYU Aug 2003
%
% speed and dir are always output.  If present in output list, lat and
% lon, followed by land, and speeds, dirs, and choices are output.

if nargout < 2
  error('*** insufficient number of output arguments to loadBYUhrwind');
end

fid1=fopen(name,'r','ieee-be'); % note: binary high res files are stored in
                                % big endian order
if fid1 <= 0
  error(['*** could not open file ', name]);
end

% read headers

dumb=fread(fid1,1,'int');
a=fread(fid1,[5],'int')';
ngrid=a(1);
wctgrid=a(2);
watgrid=a(3);
ctgrid=a(4);
atgrid=a(5);
dumb=fread(fid1,1,'int');

dumb=fread(fid1,1,'int');
a=fread(fid1,[4],'int')';
natstart=a(1);
natend=a(2);
nctstart=a(3);
nctend=a(4);
dumb=fread(fid1,1,'int');

dumb=fread(fid1,1,'int');
a=fread(fid1,[4],'int')';
iastart=a(1);
iaend=a(2);
icstart=a(3);
icend=a(4);
dumb=fread(fid1,1,'int');

dumb=fread(fid1,1,'int');
ascnode=fread(fid1,1,'float');
a=fread(fid1,[3],'int')';
irev=a(1);
year=a(2);
day=a(3);
dumb=fread(fid1,1,'int');

fa=iastart-iastart+1;
la=iaend-iastart+1;
fc=icstart-icstart+1;
lc=icend-icstart+1;
wid=min(lc-fc+1,la);
ind=[1:lc];

% allocate output arrays

nchoice=zeros([icend-icstart+1,iaend-iastart+1]);
speed=zeros([icend-icstart+1,iaend-iastart+1]);
dir=zeros([icend-icstart+1,iaend-iastart+1]);

if nargout > 3
  lat=zeros([icend-icstart+1,iaend-iastart+1]);
  lon=zeros([icend-icstart+1,iaend-iastart+1]);
  if nargout > 4
    land=zeros([icend-icstart+1,iaend-iastart+1]);
    if nargout > 7
      speeds=zeros([icend-icstart+1,iaend-iastart+1,4]);
      dirs=zeros([icend-icstart+1,iaend-iastart+1,4]);
      choices=zeros([icend-icstart+1,iaend-iastart+1]);
      if nargout > 8 % optionally output swath location value
	cstart=icstart;
	if nargout > 9 % optionally output swath location value
	  astart=iastart;
	end
      end
    end
  end
end

for i=iastart:iaend
  dumb=fread(fid1,1,'int');
  b=fread(fid1,9*lc,'int16');
  n=b(9*(ind-1)+1);
  if nargout > 7
    choices(ind,i-iastart+1)=n;   % output ambiguity selection choice
    for nn=1:4
      ind3=2*(nn-1)+1+9*(ind'-1)+1;
      speeds(ind,i-iastart+1,nn)=b(ind3)/200.0;
      dirs(ind,i-iastart+1,nn)=b(ind3+1)/180.0;
    end
  end
  n(find(n<=0 | n>4))=1;
  ind2=2*(n-1)+1+9*(ind'-1)+1;
  speed(ind,i-iastart+1)=b(ind2)/200.0; 
  dirt=b(ind2+1)/180;
  dirt(find(dirt<0))=dirt(find(dirt<0))+360.0;
  dir(ind,i-iastart+1)=dirt;
  
  a=fread(fid1,[2*lc],'int16');  % read lat/lon array info
  land1=a(2*ind-1);
  land1(find(land1>0))=1;        % decode land mask
  land1(find(land1<0))=0;
  if nargout > 3                 % output lat/lon location arrays
    lat(ind,i-iastart+1)=(abs(a(2*ind-1))-16201)/180.0;
    lon(ind,i-iastart+1)=a(2*ind)/180.0;
    if nargout > 4               % optionally output land mask
      land(ind,i-iastart+1)=land1;
    end
  end

  % apply land mask
%  speed(ind,i-iastart+1)=speed(ind,i-iastart+1).*land+(1-land)*200;
    
  dumb=fread(fid1,1,'int');

end
fclose(fid1);


















