fid1=fopen('/cers/long/cers33/QS_S1B01201.20001321038_WRave2','r');

% matlab script to plot retrieved hi resolution QSCAT winds

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];

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

for i=iastart:iaend
  dumb=fread(fid1,1,'int');
%  a=fread(fid1,[9,lc],'int16');
  
%  nchoice(ind-icstart+1,i-iastart+1)=a(1,:);
%  speed(ind-icstart+1,i-iastart+1)=mean(a(1,lc))/200.0; 
%  dir(k-icstart+1,i-iastart+1)=a(2,n)/180.0;

  
  % average speed of ambigs
  for k=icstart:icend
    n=fread(fid1,1,'int16');
    nchoice(k-icstart+1,i-iastart+1)=n;

    a=fread(fid1,[2,4],'int16');
    if (n > 0)
      %    speed(k-icstart+1,i-iastart+1)=a(1,n);  % selected ambig speed
      speed(k-icstart+1,i-iastart+1)=mean(a(1,1:n))/200.0;  % average speed of ambigs
      dir(k-icstart+1,i-iastart+1)=a(2,n)/180.0;
    end
  end

  a=fread(fid1,[2*lc],'int16');
  lat(ind,i-iastart+1)=a(2*ind-1)/200.0;
  lon(ind,i-iastart+1)=a(2*ind)/180.0;
  
  dumb=fread(fid1,1,'int');

end
fclose(fid1);


myfigure(1)
image(nchoice*10);
txt = sprintf('title(''Choice*10   Alongtrack [%d to %d]  Crosstrack [%d to %d]'')',(i+iastart-1)/ngrid, (i+wid-1+iastart-1)/ngrid,(fc+icstart-1)/ngrid,(lc+icstart-1)/ngrid);
eval(txt)
%colormap('gray')
colorbar;


myfigure(2)
image(dir*64/360);
cm=hsv;
cm2=cm;
[ncm k]=size(cm);
for k=1:ncm/2
  cm2(k)=cm(2*k-1);
  cm2(ncm-k+1)=cm(2*k-1);
end
colormap(cm2);
colorbar;
txt = sprintf('title(''Direction (deg)   Alongtrack [%d to %d]  Crosstrack [%d to %d]'')',(i+iastart-1)/ngrid, (i+wid-1+iastart-1)/ngrid,(fc+icstart-1)/ngrid,(lc+icstart-1)/ngrid);
eval(txt)

title('Direction (deg)');

%colormap('gray')

myfigure(3)
image(speed*2);
colormap('gray');
colorbar;
txt = sprintf('title(''Speed*2 (m/s)   Alongtrack [%d to %d]  Crosstrack [%d to %d]'')',(i+iastart-1)/ngrid, (i+wid-1+iastart-1)/ngrid,(fc+icstart-1)/ngrid,(lc+icstart-1)/ngrid);
eval(txt)










