function hopsgui_contour_command ( action )
%  USAGE hopsgui_contour_command(action)
%  In this function there are some command used in hopsGui for contouring
%
% The comands (action) are basically:
% depth_contour,......Do depth contours
% jindex_contour,......Do jindex contours
% iindex_contour,......Do iindex contours
% kindex_contour,......Do kindex contours
% maskoverlay,......Overlay land mask
%
%  Pedro Velez (UIB), July 1999


global hopsgui_obj

switch ( action )
        
%Depth contouring
case 'depth_contour'
hopsgui_obj{1}.action = action;


	    	[field,x,y]=hopsgui_depthslice(hopsgui_obj{1}.filein, ...
      	  hopsgui_obj{1}.depth, ....
	        hopsgui_obj{1}.time, ....
	        hopsgui_obj{1}.variable);
		  
	hopsgui_obj{1}.xlim=get(hopsgui_obj{1}.haxes1,'xlim');
	hopsgui_obj{1}.ylim=get(hopsgui_obj{1}.haxes1,'ylim');
  
  %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);
		
		%I Change the windows of the max, min, interval
		set(hopsgui_obj{1}.hautominedit,'String',deblank(sprintf('%8.5g',hopsgui_obj{1}.cmin)));
		set(hopsgui_obj{1}.hautomaxedit,'String',deblank(sprintf('%8.5g',hopsgui_obj{1}.cmax)));
		set(hopsgui_obj{1}.hautointedit,'String',deblank(sprintf('%8.5g',hopsgui_obj{1}.cint)));
		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	
	hopsgui_obj{1}.levels=levels;

  %Contouring
	axes(hopsgui_obj{1}.haxes1)
	caxis([minmax]);
	%checking if we want labels or lines
	if hopsgui_obj{1}.labelcontours==1 
	 extcontour(x,y,field,levels,'k','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
	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)

	
  %setting the correct aspectratio
	set(hopsgui_obj{1}.haxes1,'dataaspectratio', ...
	[1 cos(getnc(hopsgui_obj{1}.filein,'rlatd')*pi/180.) 1]);

  %Setting the new axis limits is the zoom was activated
	if hopsgui_obj{1}.czoom==1 
	axis([hopsgui_obj{1}.xlim hopsgui_obj{1}.ylim])
	end
	
  %Check if we need to add the land mask
	if hopsgui_obj{1}.drawmask == 1
	   hopsgui_contour_command('maskoverlay')	
	end
	
  %set title in the axes
	if datenum(hopsgui_obj{1}.absyear,hopsgui_obj{1}.absmonth,hopsgui_obj{1}.absday)==0
      text=title(sprintf ('%s for %s at %6.2f metres, %6.2f days\n %s', ....
	hopsgui_obj{1}.variable,...
	hopsgui_obj{1}.titlrun, hopsgui_obj{1}.depth, ...
	((hopsgui_obj{1}.time-1)*hopsgui_obj{1}.timeinterval)/24., ...
	hopsgui_obj{1}.filein),...
	'Parent',hopsgui_obj{1}.haxes1,...
	'Units','normalized','position',[0 1.0], ...
	'horizontalalignment','left','interpreter','none');
	else 
	hora=((hopsgui_obj{1}.time-1)*hopsgui_obj{1}.timeinterval)+hopsgui_obj{1}.abshour;
      title(sprintf ('%s for %s at %6.2f metres, %s \n %s', ....
	  hopsgui_obj{1}.variable,...
	  hopsgui_obj{1}.titlrun, hopsgui_obj{1}.depth, ...
	  datestr(datenum(hopsgui_obj{1}.absyear,...
	  hopsgui_obj{1}.absmonth, ...
	  hopsgui_obj{1}.absday,hora,0,0),0), ...
	  hopsgui_obj{1}.filein), ...
	  'Parent',hopsgui_obj{1}.haxes1,...
	  'Units','normalized','position',[0 1.0], ...
	  'horizontalalignment','left','interpreter','none');
	end
	xlabel('Longitude')
	ylabel('Latitude')

  %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 
	hopsgui_vector_command ('depth_vector')
	end
	
  %check if we add topography
      if hopsgui_obj{1}.drawtopo == 1 
	hopsgui_contour_command ('topooverlay')
	end
	


%%%%Jindex contouring%%%%%
case 'jindex_contour'
hopsgui_obj{1}.action = action;

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

	[field,x,y,z]=hopsgui_jslice(hopsgui_obj{1}.filein, ...
	hopsgui_obj{1}.jindex,....
      hopsgui_obj{1}.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);
		
		%I Change the windows of the max, min, interval
		set(hopsgui_obj{1}.hautominedit,'String',deblank(sprintf('%8.5g',hopsgui_obj{1}.cmin)));
		set(hopsgui_obj{1}.hautomaxedit,'String',deblank(sprintf('%8.5g',hopsgui_obj{1}.cmax)));
		set(hopsgui_obj{1}.hautointedit,'String',deblank(sprintf('%8.5g',hopsgui_obj{1}.cint)));
		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	
	hopsgui_obj{1}.levels=levels;

  %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
	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)

  %this is to include the bathymetry	
	hold on;
	nz=size(z,2);
	plot(x(:,nz),z(:,nz),'k'); hold off  
	hold on;plot(hopsgui_obj{1}.topox(hopsgui_obj{1}.jindex,:),hopsgui_obj{1}.topofield(hopsgui_obj{1}.jindex,:),'k');hold off
	
  %Setting the new axis limits is the zoom was activated
	if hopsgui_obj{1}.czoom==1 
	axis([hopsgui_obj{1}.xlim hopsgui_obj{1}.ylim])
	end
	
  %set title in the axes
	if datenum(hopsgui_obj{1}.absyear,hopsgui_obj{1}.absmonth,hopsgui_obj{1}.absday)==0
      title(sprintf ('%s for %s at j=%3d ,  %6.2f days \n %s', ....
	hopsgui_obj{1}.variable,...
	hopsgui_obj{1}.titlrun,hopsgui_obj{1}.jindex,...
	((hopsgui_obj{1}.time-1)*hopsgui_obj{1}.timeinterval)/24., ...
	hopsgui_obj{1}.filein),...
	'Parent',hopsgui_obj{1}.haxes1,...
	'Units','normalized','position',[0 1], ...
	'horizontalalignment','left','interpreter','none');
	else 
	hora=((hopsgui_obj{1}.time-1)*hopsgui_obj{1}.timeinterval)+hopsgui_obj{1}.abshour;
      title(sprintf ('%s for %s at j=%3d, %s \n %s', ....
	  hopsgui_obj{1}.variable,...
	  hopsgui_obj{1}.titlrun,hopsgui_obj{1}.jindex, ...
	  datestr(datenum(hopsgui_obj{1}.absyear,...
	  hopsgui_obj{1}.absmonth, ...
	  hopsgui_obj{1}.absday,hora,0,0),0), ...
	  hopsgui_obj{1}.filein), ...
	  'Parent',hopsgui_obj{1}.haxes1,...
	  'Units','normalized','position',[0 1.0], ...
	  'horizontalalignment','left','interpreter','none');
	end
	ylabel('Depth')

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

%%%%Iindex contouring%%%%
case 'iindex_contour'
hopsgui_obj{1}.action = action;

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

      [field,x,y,z]=hopsgui_islice(hopsgui_obj{1}.filein,...
	hopsgui_obj{1}.iindex,...
	hopsgui_obj{1}.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);
		
		%I Change the windows of the max, min, interval
		set(hopsgui_obj{1}.hautominedit,'String',deblank(sprintf('%8.5g',hopsgui_obj{1}.cmin)));
		set(hopsgui_obj{1}.hautomaxedit,'String',deblank(sprintf('%8.5g',hopsgui_obj{1}.cmax)));
		set(hopsgui_obj{1}.hautointedit,'String',deblank(sprintf('%8.5g',hopsgui_obj{1}.cint)));
		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	
	hopsgui_obj{1}.levels=levels;
	
  %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)	
  
  %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
	hold on;plot(hopsgui_obj{1}.topoy(:,hopsgui_obj{1}.iindex),hopsgui_obj{1}.topofield(:,hopsgui_obj{1}.iindex),'k');hold off
	
  %Setting the new axis limits is the zoom was activated
	if hopsgui_obj{1}.czoom==1 
	axis([hopsgui_obj{1}.xlim hopsgui_obj{1}.ylim])
	end
 
   %set title in the axes
     	if datenum(hopsgui_obj{1}.absyear,hopsgui_obj{1}.absmonth,hopsgui_obj{1}.absday)==0
      title(sprintf ('%s for %s at i=%3d , %6.2f days\n %s', ....
	hopsgui_obj{1}.variable,...
	hopsgui_obj{1}.titlrun,hopsgui_obj{1}.iindex,...
	((hopsgui_obj{1}.time-1)*hopsgui_obj{1}.timeinterval)/24.,...
	 hopsgui_obj{1}.filein), ...
	'Parent',hopsgui_obj{1}.haxes1,...
	'Units','normalized','position',[0 1], ...
	'horizontalalignment','left','interpreter','none');
	else 
	hora=((hopsgui_obj{1}.time-1)*hopsgui_obj{1}.timeinterval)+hopsgui_obj{1}.abshour;
      title(sprintf ('%s for %s at i=%3d, %s \n %s', ....
	  hopsgui_obj{1}.variable,...
	  hopsgui_obj{1}.titlrun,hopsgui_obj{1}.iindex, ...
	  datestr(datenum(hopsgui_obj{1}.absyear,...
	  hopsgui_obj{1}.absmonth, ...
	  hopsgui_obj{1}.absday,hora,0,0),0), ...
	  hopsgui_obj{1}.filein), ...
	  'Parent',hopsgui_obj{1}.haxes1,...
	  'Units','normalized','position',[0 1.0], ...
	  'horizontalalignment','left','interpreter','none');
	end
   	ylabel('Depth') 

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


%%%%kindex contouring%%%%
case 'kindex_contour'
hopsgui_obj{1}.action = action;

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

  %Reading data
      [field,x,y,z]=hopsgui_kslice(hopsgui_obj{1}.filein,...
	hopsgui_obj{1}.kindex,...
	hopsgui_obj{1}.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);
		
		%I Change the windows of the max, min, interval
		set(hopsgui_obj{1}.hautominedit,'String',deblank(sprintf('%8.5g',hopsgui_obj{1}.cmin)));
		set(hopsgui_obj{1}.hautomaxedit,'String',deblank(sprintf('%8.5g',hopsgui_obj{1}.cmax)));
		set(hopsgui_obj{1}.hautointedit,'String',deblank(sprintf('%8.5g',hopsgui_obj{1}.cint)));
		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	
	hopsgui_obj{1}.levels=levels;
	  
  %Checking if we need labels or lines
	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)


	if hopsgui_obj{1}.drawmask == 1
	   hopsgui_contour_command('maskoverlay')
	end

  %setting the correct aspectratio
	set(hopsgui_obj{1}.haxes1,'dataaspectratio', ...
	[1 cos(getnc(hopsgui_obj{1}.filein,'rlatd')*pi/180.) 1]);
	
  %Setting the new axis limits is the zoom was activated
	if hopsgui_obj{1}.czoom==1 
	axis([hopsgui_obj{1}.xlim hopsgui_obj{1}.ylim])
	end
	
  %set title in the axes
   	if datenum(hopsgui_obj{1}.absyear,hopsgui_obj{1}.absmonth,hopsgui_obj{1}.absday)==0
      title(sprintf ('%s for %s at  k=%3d ,  %6.2f days\n %s', ....
	hopsgui_obj{1}.variable,...
	hopsgui_obj{1}.titlrun,hopsgui_obj{1}.kindex, ...
	((hopsgui_obj{1}.time-1)*hopsgui_obj{1}.timeinterval)/24., ...
	hopsgui_obj{1}.filein), ...
	'Parent',hopsgui_obj{1}.haxes1,...
	'Units','normalized','position',[0 1], ...
	'horizontalalignment','left','interpreter','none');
	else 
	hora=((hopsgui_obj{1}.time-1)*hopsgui_obj{1}.timeinterval)+hopsgui_obj{1}.abshour;
      title(sprintf ('%s for %s at k=%3d, %s \n %s', ....
	  hopsgui_obj{1}.variable,...
	  hopsgui_obj{1}.titlrun, hopsgui_obj{1}.kindex, ...
	  datestr(datenum(hopsgui_obj{1}.absyear,...
	  hopsgui_obj{1}.absmonth, ...
	  hopsgui_obj{1}.absday,hora,0,0),0), ...
	  hopsgui_obj{1}.filein), ...
	  'Parent',hopsgui_obj{1}.haxes1,...
	  'Units','normalized','position',[0 1.0], ...
	  'horizontalalignment','left','interpreter','none');
	end
	xlabel('Longitude')
	ylabel('Latitude')

  %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 
	hopsgui_vector_command ('k_vector')
	end


%%%%Mask Overlay%%%%
case 'maskoverlay'

  %Reading data
	x=getnc(hopsgui_obj{1}.filein,'vgrid3',[-1 -1 1 1],[-1 -1 1 1],1,1,2);
	y=getnc(hopsgui_obj{1}.filein,'vgrid3',[-1 -1 1 2],[-1 -1 1 2],1,1,2);
      land=getnc(hopsgui_obj{1}.filein,'landt',[-1 -1],[-1 -1],1,1,1);
	  [m,n]=size(x);
	  for i=2:m
        for j=2:n
	    if land(i,j) == 0
	      patch([x(i,j) x(i-1,j) x(i-1,j-1) x(i,j-1)],...
             [y(i,j) y(i-1,j) y(i-1,j-1) y(i,j-1)],'k','linestyle','none');
          end
	  end
	  end
	  
%%%%topography Overlay%%%%
case 'topooverlay'

	hold on;
	[c,hopsgui_obj{1}.hcotopo]=contour(hopsgui_obj{1}.topox,hopsgui_obj{1}.topoy,hopsgui_obj{1}.topofield,'--k');
	hold off; 


end  %Del switch
return;

