%make a movie of absolute velocity in the southern ocean

%define input
 idir='../dat/mod/daily/';
 fils = dir([idir '*.nc']);

%plotting details
 xlims=[0 360];
 ylims=[-78.15 -40];
 in=[0:0.01:0.2];

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

%some ini
 n=0;
 L=1; %weekly running mean
 U=zeros(length(x),length(y),L)+NaN;
 
for i=1:length(fils)
 u=ncread([idir fils(i).name],'u_surf'); 
 v=ncread([idir fils(i).name],'v_surf');
 t=ncread([idir fils(i).name],'time');
 I=find(u<-1e10); u(I)=NaN; v(I)=NaN;
 u=double((u.^2+v.^2).^0.5); clear v; 
 for k=1:length(t)
    n=n+1;
    figure; drawnow
        % m_proj('Mercator','long',xlims,'lat', ylims);
          m_proj('stereographic','lat',-90,'long',0,'radius',28);
          hold on
          [xxtt,yytt]=m_ll2xy(X,Y);
          for l=1:L-1
           U(:,:,l)=U(:,:,l+1);
          end
           U(:,:,L)=squee(u(:,:,k));
          flat_pcolor(xxtt,yytt,mean(U,3)'), shading flat;
          amcolormap(jet,in); amcaxis(in);

          [cc]=amcolorbar('horiz',in);
          set(cc,'linewidth',2,'tickdir','out','fontweight','bold', ...
                  'xtick',in(1:2:end),'xticklabel',in(1:2:end),'position', ...
                  [0.130+0.15 0.1100+0.1 0.7750-0.3 0.0611/4], ...
                  'fontsize',10)
          m_grid('xtick',12,'tickdir','out','ytick',[-70:20:20],'linest','-', ...
                 'color','k','xticklabels',[],'yticklabels',[]);

          m_coast('patch',[0.7 0.7 0.7])
  title(['Day ' num2str(n-L)],'fontsize',14)
% keyboard
  if n>L
   eval(['print -dtiff ../tmp/uabs_' num2str(n+1000) '.tiff'])
%   if n==180
%   eval(['print -dtiff -r400 ../tmp/uabs_' num2str(n+1000) '.tiff'])
%   end
  end
  close
 end
end

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