function hopsgui_animation(action)
%  USAGE animation(action)
%  In this function there are some command used in hopsGui 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), July 1999


global hopsgui_obj
set(hopsgui_obj{1}.hfigure,'pointer','watch');
drawnow;


switch (action)

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

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

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

case('sanimation')

film=hopsgui_obj{1}.M;

figura1.position=get(hopsgui_obj{1}.hfigure,'position');

ejes1.DataAspectRatio=get(hopsgui_obj{1}.haxes1,'DataAspectRatio');
ejes1.Ylim=get(hopsgui_obj{1}.haxes1,'YLim');
ejes1.Position=get(hopsgui_obj{1}.haxes1,'Position');
ejes1.Xlim=get(hopsgui_obj{1}.haxes1,'XLim');
ejes1.DataAspectRatiomode=get(hopsgui_obj{1}.haxes1,'DataAspectRatiomode');
ejes1.Clim=get(hopsgui_obj{1}.haxes1,'Clim');
ejes1.PlotBoxAspectRatio=get(hopsgui_obj{1}.haxes1,'PlotBoxAspectRatio');
ejes1.PlotBoxAspectRatioMode=get(hopsgui_obj{1}.haxes1,'PlotBoxAspectRatioMode');
ejes1.CameraPosition=get(hopsgui_obj{1}.haxes1,'CameraPosition');
ejes1.CameraPositionMode=get(hopsgui_obj{1}.haxes1,'CameraPositionMode');


text1.string=get(hopsgui_obj{1}.htextaxes,'string');
text1.units=get(hopsgui_obj{1}.htextaxes,'units');
text1.position=get(hopsgui_obj{1}.htextaxes,'position');
text1.horizontalalignment=get(hopsgui_obj{1}.htextaxes,'horizontalalignment');
text1.interpreter=get(hopsgui_obj{1}.htextaxes,'interpreter');

levels=hopsgui_obj{1}.levels;

[filename,path] = ...
  uiputfile ( '*.mat', 'Select Filename with Movie' );
eval(sprintf( 'save %s ejes1 figura1 text1 levels film \n',sprintf ( '%s%s', path, filename)))



case('loadanimation')

[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);
t1 = text('Parent',a1, ...
	'Color',[0 0 0], ...
	'FontName','times', ...
	'HandleVisibility','callback', ...
	'HorizontalAlignment','left', ...
	'Position',[0.08 1.01812 0], ...
	'Tag','UpperTitleAxis', ...
	'VerticalAlignment','bottom');
set(t1,text1);
a2  = axes('Parent',f1, ...
	'Units','normalized', ...
	'Box','on', ...
	'CameraUpVector',[0 1 0], ...
	'FontName','times', ...
	'Layer','top', ...
	'Color',[1 1 1], ...		
	'Position',[0.89 0.25 .05 0.65], ...
	'Tag','Axes1', ...
	'XColor',[0 0 0], ...
	'YColor',[0 0 0], ...
	'ZColor',[0 0 0], ...
        'xtick',[], 'ytick',[]);

caxis(extrem(levels));hopscolorbar(levels,a2,a1)
axes(a1)
	
hopsgui_obj{1}.hanimationsaved=f1;
hopsgui_obj{1}.M=film;
movie(hopsgui_obj{1}.M,hopsgui_obj{1}.nta,hopsgui_obj{1}.fps)


case('playloadanimation')


figure(hopsgui_obj{1}.hanimationsaved)
movie(hopsgui_obj{1}.M,hopsgui_obj{1}.nta,hopsgui_obj{1}.fps)



case ('panimation')

figure(hopsgui_obj{1}.hfigure)
axes(hopsgui_obj{1}.haxes1)
movie(hopsgui_obj{1}.haxes1,hopsgui_obj{1}.M,hopsgui_obj{1}.nta,hopsgui_obj{1}.fps)



%Making the animations
case ('manimation')
figure(hopsgui_obj{1}.hfigure)


switch(hopsgui_obj{1}.action)


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

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

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


  %Contouring
	caxis([minmax]);
	axes(hopsgui_obj{1}.haxes1)
	%checking if we want labels or lines
	if hopsgui_obj{1}.labelcontours==1 
	 extcontour(x,y,field,levels,'fill','label','fontsize',10,'fontname','times');
	elseif  hopsgui_obj{1}.linecontours==1 
	extcontour(x,y,field,levels,'fill'); hold on;
	contour(x,y,field,levels,'k'); hold off
	else
	hopsgui_obj{1}.hcont1=extcontour(x,y,field,levels,'fill');
	end
	axes(hopsgui_obj{1}.haxes1)
	%caxis([minmax]);colorbar(hopsgui_obj{1}.haxes2)
	caxis([minmax]);hopscolorbar(levels,hopsgui_obj{1}.haxes2,hopsgui_obj{1}.haxes1)	
	axes(hopsgui_obj{1}.haxes1)
	set(hopsgui_obj{1}.haxes1,'xlim',xlim,'ylim',ylim);

  %Check if we need to add the land mask
	if hopsgui_obj{1}.drawmask == 1
	   hopsgui_contour_command('maskoverlay')	
	end

  %check if we add grid 
	if hopsgui_obj{1}.drawgrid==1
	  grid on
	end

  %check if we add the current vectors.
  if hopsgui_obj{1}.drawvectors == 1	
   %Drawing vectors overimposed in the contour-depth plots        
    	[u,x,y]=hopsgui_depthsliceu(hopsgui_obj{1}.filein, ...
        hopsgui_obj{1}.depth, ....
        time,hopsgui_obj{1}.vectorvar);

    	[v,x,y]=hopsgui_depthslicev(hopsgui_obj{1}.filein, ...
        hopsgui_obj{1}.depth, ....
        time,hopsgui_obj{1}.vectorvar);
   %Resampling the data 
	[m,n]=size(u);
	u=u([1:hopsgui_obj{1}.isub:m],[1:hopsgui_obj{1}.isub:n]);
	v=v([1:hopsgui_obj{1}.isub:m],[1:hopsgui_obj{1}.isub:n]);
	x=x([1:hopsgui_obj{1}.isub:m],[1:hopsgui_obj{1}.isub:n]);
	y=y([1:hopsgui_obj{1}.isub:m],[1:hopsgui_obj{1}.isub:n]);
	ig=find(finite(u));
	hold on;
	hopsgui_obj{1}.hvectors1=quiver(x(ig),y(ig),u(ig)*hopsgui_obj{1}.fac, ...
		v(ig)*hopsgui_obj{1}.fac,0,hopsgui_obj{1}.vectorcolor);
	set(hopsgui_obj{1}.hvectors1,'linewidth',.1);
	hold off;	
		
	%Reference vector
	hold on;
  	xlim=get(hopsgui_obj{1}.haxes1,'xlim');
	ylim=get(hopsgui_obj{1}.haxes1,'ylim');
	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;
	hopsgui_obj{1}.hvectors2=patch([posx posx+hopsgui_obj{1}.refvector*hopsgui_obj{1}.fac posx+hopsgui_obj{1}.refvector*hopsgui_obj{1}.fac posx], ...
	      [posy-dy posy-dy posy+dy posy+dy],'w','edgecolor',[1 1 1]);
	hopsgui_obj{1}.hvectors3=quiver(posx,posy,...
		hopsgui_obj{1}.refvector*hopsgui_obj{1}.fac,0.,0, ...
		hopsgui_obj{1}.vectorcolor);
	set(hopsgui_obj{1}.hvectors3,'linewidth',.1);		
	hold off ;
  end

  %setting the correct aspectratio
   set(hopsgui_obj{1}.haxes1,'dataaspectratio', ...
	[1 cos(getnc(hopsgui_obj{1}.filein,'rlatd')*pi/180.) 1]);
	
   %Check if we need to add the land mask
	if hopsgui_obj{1}.drawmask == 1
	   hopsgui_contour_command('maskoverlay')	
	end

   %check if we add topography
      if hopsgui_obj{1}.drawtopo == 1 
	hopsgui_contour_command ('topooverlay')
	end


  %set title in the axes
  axes(hopsgui_obj{1}.haxes1)
  
 hopsgui_obj{1}.htextaxes=title(sprintf ('%s for %s at %6.2f metres, \n %s', ....
	hopsgui_obj{1}.variable,...
	hopsgui_obj{1}.titlrun, hopsgui_obj{1}.depth, ...
	hopsgui_obj{1}.filein),...
	'Parent',hopsgui_obj{1}.haxes1,...
	'Units','normalized','position',[0 1.0], ...
	'horizontalalignment','left','interpreter','none');
  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 days',(time-1)*hopsgui_obj{1}.timeinterval/24.),...
	'verticalalignment','bottom')

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

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





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

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


  %Contouring
  	caxis([minmax]);
  	axes(hopsgui_obj{1}.haxes1)
	if hopsgui_obj{1}.labelcontours==1 
	extcontour(y,z,field,levels,'fill','label','k','fontsize',10,'fontname','times');
	elseif  hopsgui_obj{1}.linecontours==1 
	extcontour(y,z,field,levels,'fill'); hold on;
	contour(y,z,field,levels,'k'); hold off
	else
	extcontour(y,z,field,levels,'fill');
	end
	axes(hopsgui_obj{1}.haxes1)
	%caxis([minmax]);colorbar(hopsgui_obj{1}.haxes2)
	caxis([minmax]);hopscolorbar(levels,hopsgui_obj{1}.haxes2,hopsgui_obj{1}.haxes1)	
	axes(hopsgui_obj{1}.haxes1)	
  set(hopsgui_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(hopsgui_obj{1}.haxes1)
   hopsgui_obj{1}.htextaxes=title(sprintf ('%s for %s at %6.2f \n %s', ....
	hopsgui_obj{1}.variable,...
	hopsgui_obj{1}.titlrun,x(hopsgui_obj{1}.iindex),...
	 hopsgui_obj{1}.filein), ...
	'Parent',hopsgui_obj{1}.haxes1,...
	'Units','normalized','position',[0 1], ...
	'horizontalalignment','left','interpreter','none');
  ylabel('Depth') 
	  
  %check if we add grid 
	if hopsgui_obj{1}.drawgrid==1
	  grid on
	end

  %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 days',(time-1)*hopsgui_obj{1}.timeinterval/24.),...
	'verticalalignment','bottom')

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

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



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

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


  %Contouring
  	caxis([minmax]);
	axes(hopsgui_obj{1}.haxes1)
	if hopsgui_obj{1}.labelcontours==1 
	extcontour(x,z,field,levels,'fill','label','k','fontsize',10,'fontname','times');
	elseif hopsgui_obj{1}.linecontours==1 
	extcontour(x,z,field,levels,'fill'); hold on;
	contour(x,z,field,levels,'k'); hold off
	else
	extcontour(x,z,field,levels,'fill');
	end
  set(hopsgui_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(hopsgui_obj{1}.haxes1)
  
   hopsgui_obj{1}.htextaxes=title(sprintf ('%s for %s at %6.2f \n %s', ....
	hopsgui_obj{1}.variable,...
	hopsgui_obj{1}.titlrun,y(hopsgui_obj{1}.jindex),...
	hopsgui_obj{1}.filein),...
	'Parent',hopsgui_obj{1}.haxes1,...
	'Units','normalized','position',[0 1], ...
	'horizontalalignment','left','interpreter','none');
   
  ylabel('Depth') 
  
  %check if we add grid 
	if hopsgui_obj{1}.drawgrid==1
	  grid on
	end

  %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 days',(time-1)*hopsgui_obj{1}.timeinterval/24.),...
	'verticalalignment','bottom')

	  

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

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


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

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

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


  %Contouring
	caxis([minmax]);
	axes(hopsgui_obj{1}.haxes1)
	if hopsgui_obj{1}.labelcontours==1 
	extcontour(x,y,field,levels,'fill','label','k','fontsize',10,'fontname','times');
	elseif  hopsgui_obj{1}.linecontours==1 
	extcontour(x,y,field,levels,'fill'); hold on;
	contour(x,y,field,levels,'k'); hold off	
	else
	extcontour(x,y,field,levels,'fill');
	end
	axes(hopsgui_obj{1}.haxes1)
	%caxis([minmax]);colorbar(hopsgui_obj{1}.haxes2);
	caxis([minmax]);hopscolorbar(levels,hopsgui_obj{1}.haxes2,hopsgui_obj{1}.haxes1);
	axes(hopsgui_obj{1}.haxes1)	

  set(hopsgui_obj{1}.haxes1,'xlim',xlim,'ylim',ylim);

  %check if we add the current vectors.
  if hopsgui_obj{1}.drawvectors == 1	
  %Drawing vectors overimposed in the contour-depth plots        
 	[u,x,y,z]=hopsgui_kslice(hopsgui_obj{1}.filein, ...
        hopsgui_obj{1}.kindex, ....
        time,'ut');

    	[v,x,y,z]=hopsgui_kslice(hopsgui_obj{1}.filein, ...
        hopsgui_obj{1}.kindex, ....
        time,'vt');
  %Resampling the data 
	[m,n]=size(u);
	u=u([1:hopsgui_obj{1}.isub:m],[1:hopsgui_obj{1}.isub:n]);
	v=v([1:hopsgui_obj{1}.isub:m],[1:hopsgui_obj{1}.isub:n]);
	x=x([1:hopsgui_obj{1}.isub:m],[1:hopsgui_obj{1}.isub:n]);
	y=y([1:hopsgui_obj{1}.isub:m],[1:hopsgui_obj{1}.isub:n]);
	ig=find(finite(u));
	hold on;
	hopsgui_obj{1}.hvectors1=quiver(x(ig),y(ig),u(ig)*hopsgui_obj{1}.fac, ...
		v(ig)*hopsgui_obj{1}.fac,0,hopsgui_obj{1}.vectorcolor);
	set(hopsgui_obj{1}.hvectors1,'linewidth',.1);				
  %Reference vector
	hold on;
  	xlim=get(hopsgui_obj{1}.haxes1,'xlim');
	ylim=get(hopsgui_obj{1}.haxes1,'ylim');
	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;
	hopsgui_obj{1}.hvectors2=patch([posx posx+hopsgui_obj{1}.refvector*hopsgui_obj{1}.fac posx+hopsgui_obj{1}.refvector*hopsgui_obj{1}.fac posx], ...
	      [posy-dy posy-dy posy+dy posy+dy],'w','edgecolor',[1 1 1]);
	hopsgui_obj{1}.hvectors3=quiver(posx,posy,...
		hopsgui_obj{1}.refvector*hopsgui_obj{1}.fac,0.,0, ...
		hopsgui_obj{1}.vectorcolor);
	set(hopsgui_obj{1}.hvectors3,'linewidth',.1);				
	hold off;
  end

  %setting the correct aspectratio
   set(hopsgui_obj{1}.haxes1,'dataaspectratio', ...
	[1 cos(getnc(hopsgui_obj{1}.filein,'rlatd')*pi/180.) 1]);
	
  %Adding land mask
  if hopsgui_obj{1}.drawmask == 1
     land=getnc(hopsgui_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(hopsgui_obj{1}.haxes1)
  
   hopsgui_obj{1}.htextaxes=title(sprintf ('%s for %s at level %4d \n %s', ....
	hopsgui_obj{1}.variable,...
	hopsgui_obj{1}.titlrun,hopsgui_obj{1}.kindex, ...
	hopsgui_obj{1}.filein), ...
	'Parent',hopsgui_obj{1}.haxes1,...
	'Units','normalized','position',[0 1], ...
	'horizontalalignment','left','interpreter','none');
  
  hopsgui_obj{1}.htextaxesx=xlabel('Longitude');
  hopsgui_obj{1}.htextaxesy=ylabel('Latitude');
  
  %check if we add grid 
	if hopsgui_obj{1}.drawgrid==1
	  grid on
	end

  %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 days',(time-1)*hopsgui_obj{1}.timeinterval/24.),...
	'verticalalignment','bottom')
  drawnow	

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


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




end %Del swithch of hopsgui_obj{1}.actions


set(hopsgui_obj{1}.hfigure,'pointer','arrow');
drawnow;
end
