%make a movie of sst 

%define input
 idir='../data/processed/ref/';
 fils = dir([idir 'sst*.nc']);

%plotting details
 %start lims
  xlimss=[10 30];
  ylimss=[53 66];
 %end lims
  xlimse=[13.7 17.5];
  ylimse=[54.8 56.7];

 %number of frames till halfway
  del=80;

 %contour interval
  in=[0.5:0.25:18];

%get grid once
 y=ncread([idir fils(1).name],'yt_ocean');
 x=ncread([idir fils(1).name],'xt_ocean');
 [X,Y]=meshgrid(x,y); 

%some ini
 n=0;
 
for i=3:length(fils)
 temp=ncread([idir fils(i).name],'temp'); temp=squee(temp); 
 t=ncread([idir fils(i).name],'time');
 t=get_calendar_date((t./24)  + get_julian_day(1987,1,1,0));
 
 I=find(temp<-1e10); temp(I)=NaN;
 for k=1:1:length(t)
    n=n+1;
    figure; drawnow
         m=n-1+del;
         xlims=[(del*xlimss(1)+(n-1)*xlimse(1))./m  (del*xlimss(2)+(n-1)*xlimse(2))./m];
         ylims=[(del*ylimss(1)+(n-1)*ylimse(1))./m  (del*ylimss(2)+(n-1)*ylimse(2))./m];
         m_proj('Mercator','long',xlims,'lat', ylims);
        %  m_proj('stereographic','lat',-90,'long',0,'radius',28);
          hold on
          [xxtt,yytt]=m_ll2xy(X,Y);
           ttemp = double(squee(temp(:,:,k)))';
          flat_pcolor(xxtt,yytt,ttemp), shading flat;
          amcolormap(jet,in); amcaxis(in);

          [cc]=amcolorbar('horiz',in);
          set(cc,'linewidth',2,'tickdir','out','fontweight','bold', ...
                  'xtick',in(1:4:end),'xticklabel',in(1:4:end),'position', ...
                  [0.130+0.15 0.1100+0.1 0.7750-0.3 0.0611/4], ...
                  'fontsize',10)

          m_coast('patch',[0.7 0.7 0.7])
          %m_gshhs_f('color','k'])
          m_grid('xtick',[],'ytick',[],'color',[0.99 0.99 0.99])

  ye=num2str(t(k,1)); mo=t(k,2); da=t(k,3); 
   if mo<10; mo=['0' num2str(mo)]; else; mo=num2str(mo); end
   if da<10; da=['0' num2str(da)]; else; da=num2str(da); end
  title([da '.' mo '.' ye],'fontsize',14)
  eval(['print -dtiff ../ps/tmp/sst_' num2str(n+1000) '.tiff'])
  close
 end
end

!rm -f filelist.asc
!ls ../ps/tmp/*.tiff > filelist.asc
!rm -f ../mov/sst.flc
!ppm2fli -ftifftopnm -s50 -N -g1200x900 filelist.asc ../mov/sst.flc
!rm -f filelist.asc
!rm ../ps/tmp/*.tiff
