function peigui_animation(action)
%  USAGE animation(action)
%  In this function there are some command used in peiGui animation 
%
% The comands (action) are basically:
%gnframes,getfps,gettimes, .....Get setting for doing and playing the animation
%sanimation, .....Save an animation
%loadanimation, .....Load a previoulsy done animation
%playloadanimation, .....Play a previoulsy done animation
%panimation, ..... Play an animation just done
%manimation, ..... make the animations
%
%    Pedro Velez (UIB-IEO), July 1999 


global peigui_obj

switch (action)

case('gnframes')
peigui_obj{1}.nframes=str2num(get(gcbo,'String'));

case('getfps')
peigui_obj{1}.fps=str2num(get(gcbo,'String'));

case('gettimes')
peigui_obj{1}.nta=str2num(get(gcbo,'String'));

case('sanimation')
set(peigui_obj{1}.hfigure,'pointer','watch');
film=peigui_obj{1}.M;
ejes1.DataAspectRatio=get(peigui_obj{1}.haxes1,'DataAspectRatio');
ejes1.Ylim=get(peigui_obj{1}.haxes1,'YLim');
ejes1.Position=get(peigui_obj{1}.haxes1,'Position');
ejes1.Xlim=get(peigui_obj{1}.haxes1,'XLim');
figura1.position=get(peigui_obj{1}.hfigure,'position');
[filename,path] = ...
  uiputfile ( '*.mat', 'Select Filename with Movie' );
eval(sprintf( 'save %s ejes1 figura1 film \n',sprintf ( '%s%s', path, filename,'.mat')))
set(peigui_obj{1}.hfigure,'pointer','arrow');


case('loadanimation')
set(peigui_obj{1}.hfigure,'pointer','watch');
[filename,path] = ...
  uigetfile ( '*.mat', 'Select Filename with Movie' );
eval (sprintf( 'load %s\n',sprintf ( '%s%s', path, filename)));
f1=figure('Units','normalized','DefaultUicontrolUnits','Normalized', .... 
	'Color',[1 1 1], ...
	'InvertHardcopy','off', ...
	'NumberTitle','off',....
	'PaperType','a4letter', ...
	'MenuBar','none', ...
	'Name','Animation');
set(f1,figura1);
a1= axes('Parent',f1, ...
	'Units','normalized', ...
	'Box','on', ...
	'CameraUpVector',[0 1 0], ...
	'FontName','times', ...
	'Layer','top', ...
	'Color',[1 1 1], ...	
	'XColor',[0 0 0], ...
	'YColor',[0 0 0], ...
	'ZColor',[0 0 0]);
set(a1,ejes1);
peigui_obj{1}.hanimationsaved=f1;
peigui_obj{1}.M=film;
set(peigui_obj{1}.hanimationsaved,'pointer','watch');
movie(peigui_obj{1}.M,peigui_obj{1}.nta,peigui_obj{1}.fps)
set(peigui_obj{1}.hfigure,'pointer','arrow');
set(peigui_obj{1}.hanimationsaved,'pointer','arrow');

case('playloadanimation')
set(peigui_obj{1}.hfigure,'pointer','watch');
set(peigui_obj{1}.hanimationsaved,'pointer','watch');
figure(peigui_obj{1}.hanimationsaved)
movie(peigui_obj{1}.M,peigui_obj{1}.nta,peigui_obj{1}.fps)
set(peigui_obj{1}.hfigure,'pointer','arrow');
set(peigui_obj{1}.hanimationsaved,'pointer','arrow');

case ('panimation')
set(peigui_obj{1}.hfigure,'pointer','watch');
figure(peigui_obj{1}.hfigure)
axes(peigui_obj{1}.haxes1)
movie(peigui_obj{1}.haxes1,peigui_obj{1}.M,peigui_obj{1}.nta,peigui_obj{1}.fps)
set(peigui_obj{1}.hfigure,'pointer','arrow');


%Making the animations
case ('manimation')
set(peigui_obj{1}.hfigure,'pointer','watch');

switch(peigui_obj{1}.action)


case('depth_contour')
for time=1:peigui_obj{1}.nframes
disp(sprintf('Working in the animation, frame %d \n',time));

if time == 1
  xlim=get(peigui_obj{1}.haxes1,'xlim');
  ylim=get(peigui_obj{1}.haxes1,'ylim');
end	

  [field,x,y]=peigui_depthslice(peigui_obj{1}.filein, ...
      	  peigui_obj{1}.depth, ....
	        time, ....
	        peigui_obj{1}.variable);
		  
  %Checking automatic limits or not
  if peigui_obj{1}.cautomatic == 1
	minmax=extrem(field);
	vector=nice([minmax],2)';
	levels=min(vector):(vector(2)-vector(1)):max(vector);
	peigui_obj{1}.cmin=minmax(1);
	peigui_obj{1}.cmax=minmax(2);
	peigui_obj{1}.cint=vector(2)-vector(1);
	vector=nice([minmax],2)';
	levels=min(vector):(vector(2)-vector(1)):max(vector);
  else
	minmax(1)=peigui_obj{1}.cmin;
	minmax(2)=peigui_obj{1}.cmax;
	levels=peigui_obj{1}.cmin:peigui_obj{1}.cint:peigui_obj{1}.cmax;
  end	


  %Contouring
  	caxis([minmax]);
  axes(peigui_obj{1}.haxes1)
  set(peigui_obj{1}.haxes1,'xlim',xlim,'ylim',ylim);
  %checking if we want labels
  if peigui_obj{1}.labelcontours==1 
    	extcontour(x,y,field,levels,'fill','label','k');
  else 
	extcontour(x,y,field,levels,'fill');
  end
  axes(peigui_obj{1}.haxes1)
  set(peigui_obj{1}.haxes1,'xlim',xlim,'ylim',ylim);
  %caxis([minmax]);colorbar(peigui_obj{1}.haxes2)
  caxis([minmax]);hopscolorbar(levels,peigui_obj{1}.haxes2,peigui_obj{1}.haxes1)			
  axes(peigui_obj{1}.haxes1)
  set(peigui_obj{1}.haxes1,'xlim',xlim,'ylim',ylim);

  %check if we add the current vectors.
  if peigui_obj{1}.drawvectors == 1	
	axes(peigui_obj{1}.haxes1)
    	[u,x,y]=peigui_depthslicev(peigui_obj{1}.filein, ...
        peigui_obj{1}.depth, ....
        time,'vtot');
    	[v,x,y]=peigui_depthsliceu(peigui_obj{1}.filein, ...
        peigui_obj{1}.depth, ....
        time,'vtot');
	%Resampling the data 
	[m,n]=size(u);
	u=u([1:peigui_obj{1}.isub:m],[1:peigui_obj{1}.isub:n]);
	v=v([1:peigui_obj{1}.isub:m],[1:peigui_obj{1}.isub:n]);
	x=x([1:peigui_obj{1}.isub:m],[1:peigui_obj{1}.isub:n]);
	y=y([1:peigui_obj{1}.isub:m],[1:peigui_obj{1}.isub:n]);
	ig=find(finite(u));
	hold on;
	quiver(x(ig),y(ig),u(ig)*peigui_obj{1}.fac, ...
		v(ig)*peigui_obj{1}.fac,0,peigui_obj{1}.vectorcolor)
	%Reference vector
	posx=xlim(1)+(xlim(2)-xlim(1))*0.02;
	dx=(xlim(2)-xlim(1))*0.3;
	posy=ylim(2)-(ylim(2)-ylim(1))*0.02;
	dy=(ylim(2)-ylim(1))*0.015;
	patch([posx posx+peigui_obj{1}.refvector*peigui_obj{1}.fac posx+peigui_obj{1}.refvector*peigui_obj{1}.fac posx], ...
	      [posy-dy posy-dy posy+dy posy+dy],'w','edgecolor',[1 1 1]);
	quiver(posx,posy,...
		peigui_obj{1}.refvector*peigui_obj{1}.fac,0.,0, ...
		peigui_obj{1}.vectorcolor);
	hold off ;
  end

  %setting the correct aspectratio
   set(peigui_obj{1}.haxes1,'dataaspectratio', ...
	[1 cos(getnc(peigui_obj{1}.filein,'rlatd')*pi/180.) 1]);
	
  %Adding land mask
  if peigui_obj{1}.drawmask == 1
     land=getnc(peigui_obj{1}.filein,'landt',[-1 -1],[-1 -1],1,1,1);
     [m,n]=size(x);
	for i=2:m-1
	for j=2:n-1
		if land(i,j) == 0 	
	      patch([x(i-1,j-1) x(i+1,j-1) x(i+1,j+1) x(i-1,j+1)],...
             [y(i-1,j-1) y(i+1,j-1) y(i+1,j+1) y(i-1,j+1)],'k');
            end
	end
	end
  end


  %set title in the axes
  axes(peigui_obj{1}.haxes1)
  title(sprintf ('%s for %s at %6.2f metres, %6.2f hours', ....
	peigui_obj{1}.variable,...
	peigui_obj{1}.titlrun, peigui_obj{1}.depth, (time-1)*peigui_obj{1}.timeinterval));
  xlabel('Longitude')
  ylabel('Latitude')
  
  %set time clock
  posx=xlim(1)+(xlim(2)-xlim(1))*0.02;
  dx=(xlim(2)-xlim(1))*0.3;
  posy=ylim(1)+(ylim(2)-ylim(1))*0.02;
  dy=(ylim(2)-ylim(1))*0.05;
  patch([posx posx+dx posx+dx posx], ...
	[posy posy posy+dy posy+dy],'w','edgecolor',[1 1 1]);
	text(posx+0.05*dx ,posy+0.05*dy,sprintf('%6.2f hours',(time-1)*peigui_obj{1}.timeinterval),...
	'verticalalignment','bottom')

  if time == 1
	peigui_obj{1}.M=moviein(peigui_obj{1}.nframes+1);
  end	
  
peigui_obj{1}.M(:,time)=getframe;
end

disp(sprintf('Finalized the animation \n'));
figure(peigui_obj{1}.hfigure)
axes(peigui_obj{1}.haxes1)
movie(peigui_obj{1}.M,1)





case('iindex_contour')
for time=1:peigui_obj{1}.nframes
disp(sprintf('Working in the animation, frame %d \n',time));

if time == 1
  xlim=get(peigui_obj{1}.haxes1,'xlim');
  ylim=get(peigui_obj{1}.haxes1,'ylim');
end	
  %Reading data
  [field,x,y,z]=peigui_islice(peigui_obj{1}.filein,...
	peigui_obj{1}.iindex,...
      time,...
	peigui_obj{1}.variable);
		  
  %Checking automatic limits or not
  if peigui_obj{1}.cautomatic == 1
	minmax=extrem(field);
	vector=nice([minmax],2)';
	levels=min(vector):(vector(2)-vector(1)):max(vector);
	peigui_obj{1}.cmin=minmax(1);
	peigui_obj{1}.cmax=minmax(2);
	peigui_obj{1}.cint=vector(2)-vector(1);
	vector=nice([minmax],2)';
	levels=min(vector):(vector(2)-vector(1)):max(vector);
  else
	minmax(1)=peigui_obj{1}.cmin;
	minmax(2)=peigui_obj{1}.cmax;
	levels=peigui_obj{1}.cmin:peigui_obj{1}.cint:peigui_obj{1}.cmax;
  end	


  %Contouring
  	caxis([minmax]);
  axes(peigui_obj{1}.haxes1)
  set(peigui_obj{1}.haxes1,'xlim',xlim,'ylim',ylim);
  %checking if we want labels
  if peigui_obj{1}.labelcontours==1 
	extcontour(y,z,field,levels,'fill','label','k');
  else
	extcontour(y,z,field,levels,'fill');
  end
  axes(peigui_obj{1}.haxes1)
  set(peigui_obj{1}.haxes1,'xlim',xlim,'ylim',ylim);
  %caxis([minmax]);colorbar(peigui_obj{1}.haxes2)
  caxis([minmax]);hopscolorbar(levels,peigui_obj{1}.haxes2,peigui_obj{1}.haxes1)		
  axes(peigui_obj{1}.haxes1)
  set(peigui_obj{1}.haxes1,'xlim',xlim,'ylim',ylim);

  %this is to include the bathymetry	
  hold on;
  nz=size(z,2);
  plot(y(:,nz),z(:,nz),'k'); hold off  %this is to include the bathymetry

	


  %set title in the axes
  axes(peigui_obj{1}.haxes1)
      title(sprintf ('%s for %s at %6.2f , %6.2f hours\n %s', ....
	peigui_obj{1}.variable,...
	peigui_obj{1}.titlrun,x(peigui_obj{1}.iindex),...
	 (peigui_obj{1}.time-1)*peigui_obj{1}.timeinterval,...
	 peigui_obj{1}.filein), ...
	'Parent',peigui_obj{1}.haxes1,...
	'Units','normalized','position',[0 1], ...
	'horizontalalignment','left','interpreter','none');
  ylabel('Depth') 
	  
  %set time clock
  posx=xlim(1)+(xlim(2)-xlim(1))*0.02;
  dx=(xlim(2)-xlim(1))*0.3;
  posy=ylim(1)+(ylim(2)-ylim(1))*0.02;
  dy=(ylim(2)-ylim(1))*0.05;
  patch([posx posx+dx posx+dx posx], ...
	[posy posy posy+dy posy+dy],'w','edgecolor',[1 1 1]);
	text(posx+0.05*dx ,posy+0.05*dy,sprintf('%6.2f hours',(time-1)*peigui_obj{1}.timeinterval),...
	'verticalalignment','bottom')

  if time == 1
	peigui_obj{1}.M=moviein(peigui_obj{1}.nframes+1);
  end	
  
peigui_obj{1}.M(:,time)=getframe;
end

disp(sprintf('Finalized the animation \n'));
figure(peigui_obj{1}.hfigure)
axes(peigui_obj{1}.haxes1)
movie(peigui_obj{1}.M,1)



case('jindex_contour')
for time=1:peigui_obj{1}.nframes
disp(sprintf('Working in the animation, frame %d \n',time));

if time == 1
  xlim=get(peigui_obj{1}.haxes1,'xlim');
  ylim=get(peigui_obj{1}.haxes1,'ylim');
end	
  %Reading data
  [field,x,y,z]=peigui_jslice(peigui_obj{1}.filein, ...
	peigui_obj{1}.jindex,....
      time, ....
	peigui_obj{1}.variable);
		  
  %Checking automatic limits or not
  if peigui_obj{1}.cautomatic == 1
	minmax=extrem(field);
	vector=nice([minmax],2)';
	levels=min(vector):(vector(2)-vector(1)):max(vector);
	peigui_obj{1}.cmin=minmax(1);
	peigui_obj{1}.cmax=minmax(2);
	peigui_obj{1}.cint=vector(2)-vector(1);
	vector=nice([minmax],2)';
	levels=min(vector):(vector(2)-vector(1)):max(vector);
  else
	minmax(1)=peigui_obj{1}.cmin;
	minmax(2)=peigui_obj{1}.cmax;
	levels=peigui_obj{1}.cmin:peigui_obj{1}.cint:peigui_obj{1}.cmax;
  end	


  %Contouring
  caxis([minmax]);
  axes(peigui_obj{1}.haxes1)
  set(peigui_obj{1}.haxes1,'xlim',xlim,'ylim',ylim);
  %checking if we want labels
  if peigui_obj{1}.labelcontours==1 
	extcontour(x,z,field,levels,'fill','label','k');
  else
	extcontour(x,z,field,levels,'fill');
  end
  
  axes(peigui_obj{1}.haxes1)
  set(peigui_obj{1}.haxes1,'xlim',xlim,'ylim',ylim);
  %caxis([minmax]);colorbar(peigui_obj{1}.haxes2)
  caxis([minmax]);hopscolorbar(levels,peigui_obj{1}.haxes2,peigui_obj{1}.haxes1)			
  axes(peigui_obj{1}.haxes1)
  set(peigui_obj{1}.haxes1,'xlim',xlim,'ylim',ylim);

  %this is to include the bathymetry	
  hold on;
  nz=size(z,2);
  plot(x(:,nz),z(:,nz),'k'); hold off  

	


  %set title in the axes
  axes(peigui_obj{1}.haxes1)
      title(sprintf ('%s for %s at %6.2f , %6.2f hours\n %s', ....
	peigui_obj{1}.variable,...
	peigui_obj{1}.titlrun,y(peigui_obj{1}.jindex),...
	 (peigui_obj{1}.time-1)*peigui_obj{1}.timeinterval,...
	 peigui_obj{1}.filein), ...
	'Parent',peigui_obj{1}.haxes1,...
	'Units','normalized','position',[0 1], ...
	'horizontalalignment','left','interpreter','none');
  ylabel('Depth') 

  %set time clock
  posx=xlim(1)+(xlim(2)-xlim(1))*0.02;
  dx=(xlim(2)-xlim(1))*0.3;
  posy=ylim(1)+(ylim(2)-ylim(1))*0.02;
  dy=(ylim(2)-ylim(1))*0.05;
  patch([posx posx+dx posx+dx posx], ...
	[posy posy posy+dy posy+dy],'w','edgecolor',[1 1 1]);
	text(posx+0.05*dx ,posy+0.05*dy,sprintf('%6.2f hours',(time-1)*peigui_obj{1}.timeinterval),...
	'verticalalignment','bottom')

	  

  if time == 1
	peigui_obj{1}.M=moviein(peigui_obj{1}.nframes+1);
  end	
  
peigui_obj{1}.M(:,time)=getframe;
end

disp(sprintf('Finalized the animation \n'));
figure(peigui_obj{1}.hfigure)
axes(peigui_obj{1}.haxes1)
movie(peigui_obj{1}.M,1)


case('kindex_contour')
for time=1:peigui_obj{1}.nframes
disp(sprintf('Working in the animation, frame %d \n',time));

if time == 1
  xlim=get(peigui_obj{1}.haxes1,'xlim');
  ylim=get(peigui_obj{1}.haxes1,'ylim');
end	

  [field,x,y,z]=peigui_kslice(peigui_obj{1}.filein,...
	peigui_obj{1}.kindex,...
	time,...
	peigui_obj{1}.variable);
		  
  %Checking automatic limits or not
  if peigui_obj{1}.cautomatic == 1
	minmax=extrem(field);
	vector=nice([minmax],2)';
	levels=min(vector):(vector(2)-vector(1)):max(vector);
	peigui_obj{1}.cmin=minmax(1);
	peigui_obj{1}.cmax=minmax(2);
	peigui_obj{1}.cint=vector(2)-vector(1);
	vector=nice([minmax],2)';
	levels=min(vector):(vector(2)-vector(1)):max(vector);
  else
	minmax(1)=peigui_obj{1}.cmin;
	minmax(2)=peigui_obj{1}.cmax;
	levels=peigui_obj{1}.cmin:peigui_obj{1}.cint:peigui_obj{1}.cmax;
  end	


  %Contouring
  	caxis([minmax]);
  axes(peigui_obj{1}.haxes1)
  set(peigui_obj{1}.haxes1,'xlim',xlim,'ylim',ylim);
  %checking if we want labels
  if peigui_obj{1}.labelcontours==1 
    	extcontour(x,y,field,levels,'fill','label','k');
  else 
	extcontour(x,y,field,levels,'fill');
  end
  axes(peigui_obj{1}.haxes1)
  set(peigui_obj{1}.haxes1,'xlim',xlim,'ylim',ylim);
  %caxis([minmax]);colorbar(peigui_obj{1}.haxes2)
  caxis([minmax]);hopscolorbar(levels,peigui_obj{1}.haxes2,peigui_obj{1}.haxes1)			
  axes(peigui_obj{1}.haxes1)
  set(peigui_obj{1}.haxes1,'xlim',xlim,'ylim',ylim);

  %check if we add the current vectors.
  if peigui_obj{1}.drawvectors == 1	
	axes(peigui_obj{1}.haxes1)
    	[u,x,y,z]=peigui_kslice(peigui_obj{1}.filein, ...
        peigui_obj{1}.kindex, ....
	  time,'ut');

    	[v,x,y,z]=peigui_kslice(peigui_obj{1}.filein, ...
        peigui_obj{1}.kindex, ....
        time,'vt');
	%Resampling the data 
	[m,n]=size(u);
	u=u([1:peigui_obj{1}.isub:m],[1:peigui_obj{1}.isub:n]);
	v=v([1:peigui_obj{1}.isub:m],[1:peigui_obj{1}.isub:n]);
	x=x([1:peigui_obj{1}.isub:m],[1:peigui_obj{1}.isub:n]);
	y=y([1:peigui_obj{1}.isub:m],[1:peigui_obj{1}.isub:n]);
	ig=find(finite(u));
	hold on;
	quiver(x(ig),y(ig),u(ig)*peigui_obj{1}.fac, ...
		v(ig)*peigui_obj{1}.fac,0,peigui_obj{1}.vectorcolor)
	%Reference vector
	posx=xlim(1)+(xlim(2)-xlim(1))*0.02;
	dx=(xlim(2)-xlim(1))*0.3;
	posy=ylim(2)-(ylim(2)-ylim(1))*0.02;
	dy=(ylim(2)-ylim(1))*0.015;
	patch([posx posx+peigui_obj{1}.refvector*peigui_obj{1}.fac posx+peigui_obj{1}.refvector*peigui_obj{1}.fac posx], ...
	      [posy-dy posy-dy posy+dy posy+dy],'w','edgecolor',[1 1 1]);
	quiver(posx,posy,...
		peigui_obj{1}.refvector*peigui_obj{1}.fac,0.,0, ...
		peigui_obj{1}.vectorcolor);
	hold off ;
  end

  %setting the correct aspectratio
   set(peigui_obj{1}.haxes1,'dataaspectratio', ...
	[1 cos(getnc(peigui_obj{1}.filein,'rlatd')*pi/180.) 1]);
	
  %Adding land mask
  if peigui_obj{1}.drawmask == 1
     land=getnc(peigui_obj{1}.filein,'landt',[-1 -1],[-1 -1],1,1,1);
     [m,n]=size(x);
	for i=2:m-1
	for j=2:n-1
		if land(i,j) == 0 	
	      patch([x(i-1,j-1) x(i+1,j-1) x(i+1,j+1) x(i-1,j+1)],...
             [y(i-1,j-1) y(i+1,j-1) y(i+1,j+1) y(i-1,j+1)],'k');
            end
	end
	end
  end


  %set title in the axes
  axes(peigui_obj{1}.haxes1)
  title(sprintf ('%s for %s at %6.2f, %6.2f hours', ....
	peigui_obj{1}.variable,...
	peigui_obj{1}.titlrun, peigui_obj{1}.kindex, (time-1)*peigui_obj{1}.timeinterval));
  xlabel('Longitude')
  ylabel('Latitude')
  
  %set time clock
  posx=xlim(1)+(xlim(2)-xlim(1))*0.02;
  dx=(xlim(2)-xlim(1))*0.3;
  posy=ylim(1)+(ylim(2)-ylim(1))*0.02;
  dy=(ylim(2)-ylim(1))*0.05;
  patch([posx posx+dx posx+dx posx], ...
	[posy posy posy+dy posy+dy],'w','edgecolor',[1 1 1]);
	text(posx+0.05*dx ,posy+0.05*dy,sprintf('%6.2f hours',(time-1)*peigui_obj{1}.timeinterval),...
	'verticalalignment','bottom')
  drawnow	

  if time == 1
	peigui_obj{1}.M=moviein(peigui_obj{1}.nframes+1);
  end	
  
peigui_obj{1}.M(:,time)=getframe;
end

set(peigui_obj{1}.hfigure,'pointer','arrow');
disp(sprintf('Finalized the animation \n'));
figure(peigui_obj{1}.hfigure)
axes(peigui_obj{1}.haxes1)
movie(peigui_obj{1}.M,1)




end %Del swithch of peigui_obj{1}.actions
end
