% routine to make movies out of Jim B's 5-day ssts
%

tree='/home3/ocn/jmanning/gbs95/sat/grd/oi_sst_95';

% prompt for yearday of openning scene
%sd=input('Enter start yearday: ');
sd=162
nd=input('Enter number of frames: ');

% options for zoom
%wo=menu('Which zoom option?',...
%                            'just georges',...
%                            'include GS-rings',...
%                            'just GS-rings');
wo=1;

% define min & max
if wo==1;
  minx=-70;maxx=-65;miny=40;maxy=43;
elseif wo==2;
  minx=-69;maxx=-65.;miny=39.5;maxy=42.5;
elseif wo==3;
  minx=-69;maxx=-66;miny=39.5;maxy=41;
end;

wm=menu('What mode?','just view','print & view','movie');

if wm==3;  fid2=fopen('list.dat','w'); end;

wtp=menu('What type plot?','pcolor',...
                           'econtour w/labels',...
                           'econtour w/out labels',...
                           'line contour',...
                           'pcolor w/econtour lines');


% start big loop of images
for k=1:nd;

  clf

% get next yearday
  kk=sd+k*5-5;

% open file 
  filen=[tree num2str(kk) '.grd'];
  disp(['Working on ',filen])
  fid=fopen(filen);
  fscanf(fid,'%f',1);
  d=fscanf(fid,'%f',inf);
  dd=reshape(d,6,length(d)/6);
  dd=dd';

% find the box of interest
  idx=find(dd(:,1)>=minx&dd(:,1)<=maxx);
  idy=find(dd(idx,2)>=miny&dd(idx,2)<=maxy);

% define the new subsampled matrix
% need to find first incidence of "x" decreasing
  idxd=find(diff(dd(idx(idy),1))<0);
  numx=idxd(1);
  numy=length(idx(idy))/numx;
  x= dd(idx(idy(1:numx),1));
  y= dd(idx(idy(1:numx:length(idy))),2);
  z=reshape(dd(idx(idy),3),numx,numy);

% contour data depending on what user wants for shade/contours
  if wtp==1;
    pcolor(x,flipud(y),rot90(z))
    shading interp
  elseif wtp==2;
    econtour(x,flipud(y),rot90(z),'fill','labels');
  elseif wtp==3;
    econtour(x,flipud(y),rot90(z),'fill');
    % label min & max temps
    idmi=find(dd(idx(idy),3)==min(dd(idx(idy),3)));
    hi=text(dd(idx(idy(idmi)),1),dd(idx(idy(idmi)),2),num2str(round(min(dd(idx(idy(idmi)),3)))));
    idma=find(dd(idx(idy),3)==max(dd(idx(idy),3)));
    ha=text(dd(idx(idy(idma)),1),dd(idx(idy(idma)),2),num2str(round(max(dd(idx(idy(idma)),3)))));
    
  elseif wtp==5;
    pcolor(x,flipud(y),rot90(z))
    shading interp
    hold on;
    v=[4:2:20];
    econtour(x,flipud(y),rot90(z),'labels',200);
    disp('This option is best looking BUT slow and generates large files');
  end;
  colormap(jet)
  hold on;

% fixup
  georbat
  axis([minx maxx miny maxy]);
  lllabel(60,60,miny,maxy,minx,maxx);
  set(gcf,'paperposition',[.25 1 7 9.5]);

% add text of the date for this image in upper right
%  date=calday(num2str(tree(length(tree)-1:length(tree))),kk);
  date=calday(95,kk);
  h=text(-67.5,maxy-.15,[num2str(date(2)),' / ',num2str(date(3)),' / 19',num2str(date(1))]);
  set(h,'fontsize',18,'color','k');

% action depends on "mode"
  if wm==1;
    drawnow;
  elseif wm==2;
    disp('Print as you wish ... and return');
    !beep
    keyboard;
  elseif wm==3;
    disp('Making gif file...');
    eval(['print -dgif8 ',num2str(kk)]);
%   add to list file
    fprintf(fid2,'%s\n',[num2str(kk),'.gif']);
  end;

% go get another frame
end;

  
if wm==3;
  !~/flic/xanim2703/fbm2fli list.dat this.flc
  !xanim this.flc
end;