%
% matlat script to display high resolution sigma-0 values
%
file='QS_S1B02071.20001450935.avewr';

fid=fopen(file,'r');
recl=76*10;

buff=fread(fid,[recl],'int16');
if (buff(1) ~= recl)
  disp(['*** File size mismatch: ',num2str(recl),' ',num2str(buff(1))]);
  return
end

version=buff(2)/10;
wind_params=buff(3);
ngrid=buff(4);
wctgrid=buff(5);
watgrid=buff(6);
ctgrid=buff(7);
atgrid=buff(8);
natstart=buff(9);
natend=buff(10);
nctstart=buff(11);
nctend=buff(12);
irev=buff(13);
year=buff(14);
day=buff(15);
ascnode=buff(16)/180;
cnt_arr=buff(17);

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

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

%avehf=zeros([nctgrid natgrid]);
%aveha=zeros([nctgrid natgrid]);
avevf=zeros([nctgrid natgrid]);
aveva=zeros([nctgrid natgrid]);
land=zeros([nctgrid natgrid]);
%latitude=zeros([nctgrid natgrid]);
%longitude=zeros([nctgrid natgrid]);


for i=1:natgrid

  lat=fread(fid,[nctgrid 1],'int16');


  [m n]=size(lat);
  if (m == 0 | n == 0) 
    warning(['*** file read error *** ',num2str(m),' ',num2str(n),' at ',num2str(i)]);
  else
    
    land(:,i)=lat;
%    latitude(:,i)=(lat-18001)/200;
    lon=fread(fid,[nctgrid 1],'int16'); 
%    longitude(:,i)=lon/180;
    for ib=1:4

      temp=fread(fid,[nctgrid 1],'int16');  
      
      if ib == 1
%	avehf(:,i)=temp/300;
      end
      if ib == 2
%	aveha(:,i)=temp/300;
      end
      if ib == 3
	avevf(:,i)=temp/300;
      end
      if ib == 4
	aveva(:,i)=temp/300;
      end
      
      if (wind_params == 1)
	if (cnt_arr == 1)
	  temp=fread(fid,[nctgrid 1],'int16');  % cnts
	end
	temp=fread(fid,[nctgrid 1],'int16');  % azangs * 180 	
	temp=fread(fid,[nctgrid 1],'int16');  % incangs * 200
      end

    end   
  end
end

fclose(fid);

%[min(min(avehf)) max(max(avehf))]
%[min(min(aveha)) max(max(aveha))]

land(find(land > 0))=1;
land(find(land < 0))=0;

if 1==0

myfigure(1)
%image(avehf+60);
image((avehf+60).*land);
colormap('gray')
txt = sprintf('title(''Fore h Sigma-0   Alongtrack [%d to %d]  Crosstrack [%d to %d]'')',natstart/ngrid,natend/ngrid,nctstart/ngrid,nctend/ngrid);
eval(txt)

myfigure(2)
%image(aveha+60);
image((aveha+60).*land);
colormap('gray')
txt = sprintf('title(''Aft h Sigma-0   Alongtrack [%d to %d]  Crosstrack [%d to %d]'')',natstart/ngrid,natend/ngrid,nctstart/ngrid,nctend/ngrid);
eval(txt)

else

myfigure(3)
%image(avevf+60);
image((avevf+60).*land);
colormap('gray')
txt = sprintf('title(''Fore v Sigma-0   Alongtrack [%d to %d]  Crosstrack [%d to %d]'')',natstart/ngrid,natend/ngrid,nctstart/ngrid,nctend/ngrid);
eval(txt)

myfigure(4)
%image(aveva+60);
image((aveva+60).*land);
colormap('gray')
txt = sprintf('title(''Aft v Sigma-0   Alongtrack [%d to %d]  Crosstrack [%d to %d]'')',natstart/ngrid,natend/ngrid,nctstart/ngrid,nctend/ngrid);
eval(txt)

end





