function [shf,sha,svf,sva,lat,lon,land,ahf,aha,avf,ava,ihf,iha,ivf,iva]=loadBYUhrsig(name)
%
% [shf,sha,svf,sva,<lat,lon,land,<ahf,aha,avf,ava,ihf,iha,ivf,iva>>]=loadBYUhrsig(name,option)
% matlab script for reading BYU high resolution sigma files
% DGL BYU 21 Aug 2003
% optional arguments: lat,lon,land and ahf,aha,avf,ava,ihf,iha,ivf,iva
%
% note: high resolution sigma-0 files are VERY large and produce very
% large (typically 760x16240) output arrays.  matlab on some machines 
% cannot handle producing all output arrays.

if nargin < 1
  error('*** insufficient number of input arguments to loadBYUhrsig');
end
if nargout < 3
  error('*** insufficient number of output arguments to loadBYUhrsig');
end
if (nargout > 4 & narout < 7) | (nargout > 7 & nargout < 15) 
  error('*** error in number of output arguments to loadBYUhrsig');
end

% open input file for reading

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

% read and decode header

NB=76*10;
buff=fread(fid1,NB,'int16');

nrec=buff(1);           % high res wvc's per record (=NB)
version=buff(2)/10.0;   % data version number
wind_params=buff(3);    % flag for wind retrieval parameters
ngrid=buff(4);          % number of high res pixels per low res wvc
wctgrid=buff(5);        % low resolution wvc cross track grid dimension
watgrid=buff(6);        % low resolution wvc along track grid dimension
ctgrid=buff(7);         % high resolution wvc cross track grid dimension
atgrid=buff(8);         % high resolution wvc along track grid dimension
natstart=buff(9);       % start hr wvc along track index
natend=buff(10);        % end hr wvc along track index
nctstart=buff(11);      % start hr wvc cross track index
nctend=buff(12);        % end hr wvc cross track index
irev=buff(13);          % orbit rev
year=buff(14);          % data year
day=buff(15);           % data day
ascnode=buff(16)/180.0; % orbit ascending node
cnt_arr=buff(17);       % flag for cnt array

disp(['Reading file ',name]);
disp(['Grid enhancement factor ',num2str(ngrid),' AT range: ',...
  num2str(natstart),' to ',num2str(natend)],' CT range: ',...
  num2str(nctstart),' to ',num2str(nctend)])
disp(['Year ', num2str(year), '  Day ',num2str(day),'  Rev ', ...
      num2str(irev),'  Ascnode ',num2str(ascnode)])

% compute size of output array

nctgrid=nctend-nctstart+1;
natgrid=natend-natstart+1;

ahf=zeros([nctgrid natgrid]);
sha=zeros([nctgrid natgrid]);
svf=zeros([nctgrid natgrid]);
sva=zeros([nctgrid natgrid]);

if nargout > 6
  lat=zeros([nctgrid natgrid]);
  lon=zeros([nctgrid natgrid]);
  land=zeros([nctgrid natgrid]);

  if nargout > 14
    ahf=zeros([nctgrid natgrid]);
    aha=zeros([nctgrid natgrid]);
    avf=zeros([nctgrid natgrid]);
    ava=zeros([nctgrid natgrid]);
    ihf=zeros([nctgrid natgrid]);
    iha=zeros([nctgrid natgrid]);
    ivf=zeros([nctgrid natgrid]);
    iva=zeros([nctgrid natgrid]);
  end
end

% begin reading file

for i=1:natgrid

  % read latitude record
  lat1=fread(fid1,[nctgrid 1],'int16');

  [m n]=size(lat1);
  if (m == 0 | n == 0) 
    warning(['*** file read error *** ',num2str(m),' ',num2str(n),' at ',num2str(i)]);
  else
    
    if nargout > 6    
      land(:,i)=lat1;
      lat(:,i)=(abs(lat1)-16201)/180;
    end

    % read longitude record
    lon1=fread(fid1,[nctgrid 1],'int16'); 
    if nargout > 6    
      lon(:,i)=lon1/180;
    end

    for ib=1:4  % for each beam/look direction

      % read sigma-0
      temp=fread(fid1,[nctgrid 1],'int16');  % sigma-0*300 (dB)
      switch ib
	case 1
	  shf(:,i)=temp/300;
	case 2
	  sha(:,i)=temp/300;
	case 3
	  svf(:,i)=temp/300;
	case 4
	  sva(:,i)=temp/300;
      end

      % read optional parameters
      if (wind_params == 1)
	if (cnt_arr == 1)
	  temp=fread(fid1,[nctgrid 1],'int16');  % cnts (discard)
	end

	% azimuth angles
	temp=fread(fid1,[nctgrid 1],'int16');  % azangs * 180
	if nargout > 14
	  switch ib
	    case 1
	      ahf(:,i)=temp/180;
	    case 2
	      aha(:,i)=temp/180;
	    case 3
	      avf(:,i)=temp/180;
	    case 4
	      ava(:,i)=temp/180;
	  end
	end

	% incidene angles
	temp=fread(fid1,[nctgrid 1],'int16');  % incangs * 200
	if nargout > 14
	  switch ib
	    case 1
	      ihf(:,i)=temp/200;
	    case 2
	      iha(:,i)=temp/200;
	    case 3
	      ivf(:,i)=temp/200;
	    case 4
	      iva(:,i)=temp/200;
	  end
	end
      
      end

    end   
  end
end

% close input file
fclose(fid1);

% set land flag

if nargout > 6    
  land(find(land > 0))=0;  % ocean
  land(find(land < 0))=1;  % land
end

return


