function peigui_contour_command ( action )
%  USAGE peigui_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-IEO), July 1999

global peigui_obj

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


	    	[field,x,y]=peigui_depthslice(peigui_obj{1}.filein, ...
      	  peigui_obj{1}.depth, ....
	        peigui_obj{1}.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);
	  %I Change the windows of the max, min, interval
		set(peigui_obj{1}.hautominedit,'String',deblank(sprintf('%8.5g',peigui_obj{1}.cmin)));
		set(peigui_obj{1}.hautomaxedit,'String',deblank(sprintf('%8.5g',peigui_obj{1}.cmax)));
		set(peigui_obj{1}.hautointedit,'String',deblank(sprintf('%8.5g',peigui_obj{1}.cint)));
		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)
	%checking if we want labels
	if peigui_obj{1}.labelcontours==1 
	 extcontour(x,y,field,levels,'fill','label','k','fontsize',10,'fontname','times');
	elseif   peigui_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(peigui_obj{1}.haxes1)
	%caxis([minmax]);colorbar(peigui_obj{1}.haxes2)
	caxis([minmax]);hopscolorbar(levels,peigui_obj{1}.haxes2,peigui_obj{1}.haxes1)	
	axes(peigui_obj{1}.haxes1)
	
	
	%setting the correct aspectratio
	set(peigui_obj{1}.haxes1,'dataaspectratio', ...
	[1 cos(getnc(peigui_obj{1}.filein,'rlatd')*pi/180.) 1]);


  	%Cehck if we need to add the land mask
	if peigui_obj{1}.drawmask == 1
	   peigui_contour_command('maskoverlay')	
	end
	
	%set title in the axes
	if datenum(peigui_obj{1}.absyear,peigui_obj{1}.absmonth,peigui_obj{1}.absday)==0
      text=title(sprintf ('%s for %s at %6.2f metres, %6.2f hours\n %s', ....
	peigui_obj{1}.variable,...
	peigui_obj{1}.titlrun, peigui_obj{1}.depth, ...
	(peigui_obj{1}.time-1)*peigui_obj{1}.timeinterval, ...
	peigui_obj{1}.filein),...
	'Parent',peigui_obj{1}.haxes1,...
	'Units','normalized','position',[0 1.0], ...
	'horizontalalignment','left','interpreter','none');
	else 
	hora=((peigui_obj{1}.time-1)*peigui_obj{1}.timeinterval)+peigui_obj{1}.abshour;
      title(sprintf ('%s for %s at %6.2f metres, %s \n %s', ....
	  peigui_obj{1}.variable,...
	  peigui_obj{1}.titlrun, peigui_obj{1}.depth, ...
	  datestr(datenum(peigui_obj{1}.absyear,...
	  peigui_obj{1}.absmonth, ...
	  peigui_obj{1}.absday,hora,0,0),0), ...
	  peigui_obj{1}.filein), ...
	  'Parent',peigui_obj{1}.haxes1,...
	  'Units','normalized','position',[0 1.0], ...
	  'horizontalalignment','left','interpreter','none');
	end

	  
	xlabel('Longitude')
	ylabel('Latitude')
	

	

	%check if we add the current vectors.
      if peigui_obj{1}.drawvectors == 1 
	peigui_vector_command ('depth_vector')
	end


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


	[field,x,y,z]=peigui_jslice(peigui_obj{1}.filein, ...
	peigui_obj{1}.jindex,....
      peigui_obj{1}.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);
		
		%I Change the windows of the max, min, interval
		set(peigui_obj{1}.hautominedit,'String',deblank(sprintf('%8.5g',peigui_obj{1}.cmin)));
		set(peigui_obj{1}.hautomaxedit,'String',deblank(sprintf('%8.5g',peigui_obj{1}.cmax)));
		set(peigui_obj{1}.hautointedit,'String',deblank(sprintf('%8.5g',peigui_obj{1}.cint)));
		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)
	if peigui_obj{1}.labelcontours==1 
	extcontour(x,z,field,levels,'fill','label','k','fontsize',10,'fontname','times');
	elseif peigui_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(peigui_obj{1}.haxes1)
	%caxis([minmax]);colorbar(peigui_obj{1}.haxes2)
	caxis([minmax]);hopscolorbar(levels,peigui_obj{1}.haxes2,peigui_obj{1}.haxes1)		
	axes(peigui_obj{1}.haxes1)

  %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
	
	if datenum(peigui_obj{1}.absyear,peigui_obj{1}.absmonth,peigui_obj{1}.absday)==0
      title(sprintf ('%s for %s at j=%3d ,  %6.2f hours\n %s', ....
	peigui_obj{1}.variable,...
	peigui_obj{1}.titlrun,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');
	else 
	hora=((peigui_obj{1}.time-1)*peigui_obj{1}.timeinterval)+peigui_obj{1}.abshour;
      title(sprintf ('%s for %s at j=%3d, %s \n %s', ....
	  peigui_obj{1}.variable,...
	  peigui_obj{1}.titlrun,peigui_obj{1}.jindex, ...
	  datestr(datenum(peigui_obj{1}.absyear,...
	  peigui_obj{1}.absmonth, ...
	  peigui_obj{1}.absday,hora,0,0),0), ...
	  peigui_obj{1}.filein), ...
	  'Parent',peigui_obj{1}.haxes1,...
	  'Units','normalized','position',[0 1.0], ...
	  'horizontalalignment','left','interpreter','none');
	end


	
	
	ylabel('Depth')

	

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

      [field,x,y,z]=peigui_islice(peigui_obj{1}.filein,...
	peigui_obj{1}.iindex,...
	peigui_obj{1}.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);
		
		%I Change the windows of the max, min, interval
		set(peigui_obj{1}.hautominedit,'String',deblank(sprintf('%8.5g',peigui_obj{1}.cmin)));
		set(peigui_obj{1}.hautomaxedit,'String',deblank(sprintf('%8.5g',peigui_obj{1}.cmax)));
		set(peigui_obj{1}.hautointedit,'String',deblank(sprintf('%8.5g',peigui_obj{1}.cint)));
		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)
	if peigui_obj{1}.labelcontours==1 
	extcontour(y,z,field,levels,'fill','label','k','fontsize',10,'fontname','times');
	elseif  peigui_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(peigui_obj{1}.haxes1)
	%caxis([minmax]);colorbar(peigui_obj{1}.haxes2)
	caxis([minmax]);hopscolorbar(levels,peigui_obj{1}.haxes2,peigui_obj{1}.haxes1)		
	axes(peigui_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
	
 
   %set title in the axes
   
   	if datenum(peigui_obj{1}.absyear,peigui_obj{1}.absmonth,peigui_obj{1}.absday)==0
      title(sprintf ('%s for %s at i=%3d , %6.2f hours\n %s', ....
	peigui_obj{1}.variable,...
	peigui_obj{1}.titlrun,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');
	else 
	hora=((peigui_obj{1}.time-1)*peigui_obj{1}.timeinterval)+peigui_obj{1}.abshour;
      title(sprintf ('%s for %s at i=%3d, %s \n %s', ....
	  peigui_obj{1}.variable,...
	  peigui_obj{1}.titlrun,peigui_obj{1}.iindex, ...
	  datestr(datenum(peigui_obj{1}.absyear,...
	  peigui_obj{1}.absmonth, ...
	  peigui_obj{1}.absday,hora,0,0),0), ...
	  peigui_obj{1}.filein), ...
	  'Parent',peigui_obj{1}.haxes1,...
	  'Units','normalized','position',[0 1.0], ...
	  'horizontalalignment','left','interpreter','none');
	end

   	ylabel('Depth') 



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


	%Reading data
      [field,x,y,z]=peigui_kslice(peigui_obj{1}.filein,...
	peigui_obj{1}.kindex,...
	peigui_obj{1}.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);
		
		%I Change the windows of the max, min, interval
		set(peigui_obj{1}.hautominedit,'String',deblank(sprintf('%8.5g',peigui_obj{1}.cmin)));
		set(peigui_obj{1}.hautomaxedit,'String',deblank(sprintf('%8.5g',peigui_obj{1}.cmax)));
		set(peigui_obj{1}.hautointedit,'String',deblank(sprintf('%8.5g',peigui_obj{1}.cint)));
		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)
	if peigui_obj{1}.labelcontours==1 
	extcontour(x,y,field,levels,'fill','label','k','fontsize',10,'fontname','times');
	elseif  peigui_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(peigui_obj{1}.haxes1)
	%caxis([minmax]);colorbar(peigui_obj{1}.haxes2)
	caxis([minmax]);hopscolorbar(levels,peigui_obj{1}.haxes2,peigui_obj{1}.haxes1)		
	axes(peigui_obj{1}.haxes1)	

	if peigui_obj{1}.drawmask == 1
	   peigui_contour_command('maskoverlay')
	end

	%setting the correct aspectratio
	set(peigui_obj{1}.haxes1,'dataaspectratio', ...
	[1 cos(getnc(peigui_obj{1}.filein,'rlatd')*pi/180.) 1]);
  
	%set title in the axes
   	if datenum(peigui_obj{1}.absyear,peigui_obj{1}.absmonth,peigui_obj{1}.absday)==0
      title(sprintf ('%s for %s at k=%3d ,  %6.2f hours\n %s', ....
	peigui_obj{1}.variable,...
	peigui_obj{1}.titlrun,peigui_obj{1}.kindex, ...
	(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');
	else 
	hora=((peigui_obj{1}.time-1)*peigui_obj{1}.timeinterval)+peigui_obj{1}.abshour;
      title(sprintf ('%s for %s at k=%3d, %s \n %s', ....
	  peigui_obj{1}.variable,...
	  peigui_obj{1}.titlrun, peigui_obj{1}.kindex, ...
	  datestr(datenum(peigui_obj{1}.absyear,...
	  peigui_obj{1}.absmonth, ...
	  peigui_obj{1}.absday,hora,0,0),0), ...
	  peigui_obj{1}.filein), ...
	  'Parent',peigui_obj{1}.haxes1,...
	  'Units','normalized','position',[0 1.0], ...
	  'horizontalalignment','left','interpreter','none');
	end


	xlabel('Longitude')
	ylabel('Latitude')

	%check if we add the current vectors.
      if peigui_obj{1}.drawvectors == 1 
	peigui_vector_command ('k_vector')
	end


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

	%Reading data
	
	x=getnc(peigui_obj{1}.filein,'vgrid3',[-1 -1 1 1],[-1 -1 1 1],1,1,2);
	y=getnc(peigui_obj{1}.filein,'vgrid3',[-1 -1 1 2],[-1 -1 1 2],1,1,2);
      land=getnc(peigui_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



end


return;

