function hopsgui_menu_command ( action )

%  USAGE hopsgui_menu_command (action)
%  In this function there are some command used in hopsGui for general 
%  purposes and managing of the interfaces objects (slider,edit, pushbutton,...) 
%
% The comands (action) are basically:
%
% openfile,.......Open the input file
% print_eps, print_lp
% print_ps_tall, print_ps,.......  Print in different ways
% exit
% new_variable,.......set the current variable
% settime, timeincrement,....... timedecrement set the current time
% rbvectoroverlay,....... Radio button to set or not overlay vetors
% rbmaskoverlay,.......Radio button to set or not overlay mask     
% rblabelcontours,....... Radio button to set or not label at contours
% rbautomaticontours,....... Radio button to set or not automatic contouring
% getmaxauto,getminauto,
% getintauto,.......Get information in case of manual contours
% velocity_settingsmenu,setarrowsampling,
% setarrowscale,setarrowcolor,
% setarrowreference,....... Set different values for drawing vectors
% fc_edit_depth,fc_slider_depth,
% fc_edit_iindex, fc_edit_jindex, 
% fc_slider_iindex, fc_slider_jindex, 
% fc_edit_kindex, 
% fc_slider_kindex,....... Manage the sliders for kindex,iindex and jindex
% setarrowrvariable,.......Set the vector field to be plotted
% getabsyear,getabsmonth,getabsday,getabshour,.......Get absolute time
%
%  Pedro Velez (UIB), July 1999

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

switch ( action )


case 'openfile'
errortrap on


     %When the file is open some values must be initialed

    	[filename,path] = uigetfile ( '*.nc', 'Select MexCdf Filename' );
	hopsgui_obj{1}.filein = sprintf ( '%s%s', path, filename);	
	hopsgui_obj{1}.ncid =ncmex ( 'open', hopsgui_obj{1}.filein, 'nowrite' );
	
	set(findobj(gcf,'type','uicontrol'),'visible','on');	
	set(findobj(gcf,'type','axes'),'visible','on');
	
	%look which variables are present in the file
	hopsgui_varmenu
	

	
	%Change the maximun and minimun values of the Slider in function of the	data
	set(hopsgui_obj{1}.hslider_depth,'Min',min(min(getnc(hopsgui_obj{1}.filein,'tbath'))));
	set(hopsgui_obj{1}.hslider_iindex,'Max',getnc(hopsgui_obj{1}.filein,'imt'));
	set(hopsgui_obj{1}.hslider_jindex,'Max',getnc(hopsgui_obj{1}.filein,'jmt'));


	set(hopsgui_obj{1}.hslider_kindex,'Max',getnc(hopsgui_obj{1}.filein,'km'));
	temp1=getnc(hopsgui_obj{1}.filein,'outlev');
	if ~exist('temp1') == 0  
	set(hopsgui_obj{1}.hslider_kindex,'Max',length(getnc(hopsgui_obj{1}.filein,'outlev')));
	end
	set(hopsgui_obj{1}.hslider_depth,'Value',fix((min(min(getnc(hopsgui_obj{1}.filein,'tbath')))+max(max(getnc(hopsgui_obj{1}.filein,'tbath'))))/2))
	set(hopsgui_obj{1}.hedit_depth,'String',num2str(fix((min(min(getnc(hopsgui_obj{1}.filein,'tbath')))+max(max(getnc(hopsgui_obj{1}.filein,'tbath'))))/2)))	
	

	%set title in the figure
	titlrun=deblank(getnc(hopsgui_obj{1}.filein,'titlrun')');
	if exist('titlrun') == 0  
	hopsgui_obj{1}.titlrun='';
	else
	hopsgui_obj{1}.titlrun=deblank(getnc(hopsgui_obj{1}.filein,'titlrun')');
	end
	set(hopsgui_obj{1}.hfigure,'Name', hopsgui_obj{1}.titlrun);	

	
	%Time interval and maximun value for time
	ntlast=getnc(hopsgui_obj{1}.filein,'nlast');
	ntsout=getnc(hopsgui_obj{1}.filein,'ntsout');
	dtuv=getnc(hopsgui_obj{1}.filein,'dtuv');
	if exist('ntlast') == 0  | exist('ntsout') == 0 | exist('dtuv') == 0
	hopsgui_obj{1}.timemax=1;
	hopsgui_obj{1}.timeinterval=0. ;
      else
	hopsgui_obj{1}.timemax=ntlast/ntsout+1;
	hopsgui_obj{1}.timeinterval=(dtuv*ntsout)/3600. ;
	end
	
	hopsgui_obj{1}.nframes=hopsgui_obj{1}.timemax;
	
	%Reading topography
	[hopsgui_obj{1}.topofield,hopsgui_obj{1}.topox,hopsgui_obj{1}.topoy]=hopsgui_2d(hopsgui_obj{1}.filein,'tbath');


case 'print_eps'
	full_path_name='salida.ps';
        [filename,path] = ...
            uiputfile ( '*.eps', 'Select Filename For Encapsulated Postscript File' );
        full_path_name = sprintf ( '%s%s', path, filename );
        comm_str = sprintf ( 'print -dpsc2 -noui %s;\n',full_path_name );
        eval ( comm_str );
    


case 'print_lp'
	
	answer = inputdlg({'Print style','Printer Name', 'Other Options'},...
			   'Print Options',1,{'psc2','', '-noui'});
	if (size(answer,1)>0)
	    printstyle = char(answer(1));
	    printername = char(answer(2));
	    others = char(answer(3));
	    comm_str = sprintf( 'print -d%s -P%s %s -f%i ;\n',...
				printstyle, printername, others);
	    eval (comm_str );
	end


case 'print_ps_tall'
	orient tall
	full_path_name='salidatall.ps';
      [filename,path] = ...
            uiputfile ( '*.ps', 'Select Filename For Postscript File' );
        full_path_name = sprintf ( '%s%s', path, filename );
        comm_str = sprintf ( 'print -dpsc2 -noui %s;\n',full_path_name );
        eval ( comm_str );


case 'print_ps'
	full_path_name='salida.ps';
      [filename,path] = ...
            uiputfile ( '*.ps', 'Select Filename For Postscript File' );
        full_path_name = sprintf ( '%s%s', path, filename );
        comm_str = sprintf ( 'print -dpsc2 -noui %s;\n',full_path_name );
        eval ( comm_str );

case 'exit'
	  close all hidden
case 'new_variable'
        hopsgui_obj{1}.variable = get ( gcbo, 'label' );
case 'settime'	  

      hopsgui_obj{1}.time=str2num(get(hopsgui_obj{1}.edit_timestep,'String'));
	if ( hopsgui_obj{1}.time > hopsgui_obj{1}.timemax )
	    hopsgui_obj{1}.time = hopsgui_obj{1}.timemax;
	    
	  disp(sprintf ( '>>>>>There are just %d  time steps \n',hopsgui_obj{1}.timemax));	    
	  set(hopsgui_obj{1}.edit_timestep,'String',num2str(hopsgui_obj{1}.time))
	return;  
      end
      if ( hopsgui_obj{1}.time < 1 )
	    hopsgui_obj{1}.time = 1;
          set(hopsgui_obj{1}.edit_timestep,'String',num2str(hopsgui_obj{1}.time))	    
	return;  	    
      end
      if  ~isempty(hopsgui_obj{1}.action) 	  
	eval(sprintf ( 'hopsgui_contour_command %s\n', hopsgui_obj{1}.action)) ;
	end

	
case 'timeincrement'

	  hopsgui_obj{1}.time=round(str2num(get(hopsgui_obj{1}.edit_timestep,'String')))+1;
	  %Check if the time is smaller than 1
	  if ( hopsgui_obj{1}.time > hopsgui_obj{1}.timemax )
		    hopsgui_obj{1}.time = hopsgui_obj{1}.timemax;
	  disp(sprintf ( '>>>>>There are just %d  time steps \n',hopsgui_obj{1}.timemax));	    
	  return;  	  
  	  end
	  set(hopsgui_obj{1}.edit_timestep,'String',num2str(hopsgui_obj{1}.time));
	  if  ~isempty(hopsgui_obj{1}.action) 	  
	  eval(sprintf ( 'hopsgui_contour_command %s\n', hopsgui_obj{1}.action)) ;
	  end


case 'timedecrement'

	  hopsgui_obj{1}.time=round(str2num(get(hopsgui_obj{1}.edit_timestep,'String')))-1;
	  %Check if the time is smaller than 1
	  if ( hopsgui_obj{1}.time == 0 )
	    hopsgui_obj{1}.time = 1;
	  return;  
	  end
	  set(hopsgui_obj{1}.edit_timestep,'String',num2str(hopsgui_obj{1}.time));
	  if  ~isempty(hopsgui_obj{1}.action) 	  
	  eval(sprintf ( 'hopsgui_contour_command %s\n', hopsgui_obj{1}.action)) ;
	  end

		
case 'rbvectoroverlay'	  

	hopsgui_obj{1}.drawvectors=get(gcbo,'Value');
	if hopsgui_obj{1}.drawvectors == 1
        if strcmp('depth_contour',strrep(hopsgui_obj{1}.action,' ',''))	
	    eval(sprintf ( 'hopsgui_vector_command %s\n','depth_vector'));
	  elseif strcmp('kindex_contour',strrep(hopsgui_obj{1}.action,' ',''))	
	    eval(sprintf ( 'hopsgui_vector_command %s\n','k_vector'));
      end
	elseif strcmp('depth_contour',strrep(hopsgui_obj{1}.action,' ','')) | strcmp('kindex_contour',strrep(hopsgui_obj{1}.action,' ',''))
	delete(hopsgui_obj{1}.hvectors1)
	delete(hopsgui_obj{1}.hvectors2)
	delete(hopsgui_obj{1}.hvectors3)
	end


case 'rbmaskoverlay'	  

	hopsgui_obj{1}.drawmask=get(gcbo,'Value');
	if hopsgui_obj{1}.drawmask==1
	if  strcmp('depth_contour',strrep(hopsgui_obj{1}.action,' ','')) | strcmp('kindex_contour',strrep(hopsgui_obj{1}.action,' ',''))
	eval(sprintf ( 'hopsgui_contour_command %s\n','maskoverlay'));
	end
	end


case 'rbtopooverlay'	  

	hopsgui_obj{1}.drawtopo=get(gcbo,'Value');
	if hopsgui_obj{1}.drawtopo==1
	if  strcmp('depth_contour',strrep(hopsgui_obj{1}.action,' ','')) | strcmp('kindex_contour',strrep(hopsgui_obj{1}.action,' ',''))
	eval(sprintf ( 'hopsgui_contour_command %s\n','topooverlay'));
	end
	elseif strcmp('depth_contour',strrep(hopsgui_obj{1}.action,' ','')) | strcmp('kindex_contour',strrep(hopsgui_obj{1}.action,' ',''))
	delete(hopsgui_obj{1}.hcotopo)		
	end



case 'rbgrid'	  
	hopsgui_obj{1}.drawgrid=get(gcbo,'Value');
	if hopsgui_obj{1}.drawgrid==1
	  grid on
	end

	
case 'rblabelcontours'	  

	hopsgui_obj{1}.labelcontours=get(gcbo,'Value');
	if  ~isempty(hopsgui_obj{1}.action) 
	eval(sprintf ( 'hopsgui_contour_command %s\n',hopsgui_obj{1}.action));
	end

	
	
case 'rblinecontours'	  

	hopsgui_obj{1}.linecontours=get(gcbo,'Value');
	if  ~isempty(hopsgui_obj{1}.action) 
	eval(sprintf ( 'hopsgui_contour_command %s\n',hopsgui_obj{1}.action));
	end

case 'rbzoom'
	hopsgui_obj{1}.czoom=get(gcbo,'Value');
	if   hopsgui_obj{1}.czoom ==1 
	zoom on
	else 
	zoom off
	end

	
case 'rbautomaticontours'

	hopsgui_obj{1}.cautomatic=get(gcbo,'Value');
	if  ~isempty(hopsgui_obj{1}.action) 
	eval(sprintf ( 'hopsgui_contour_command %s\n',hopsgui_obj{1}.action));
	end



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

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

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


case 'velocity_settingsmenu'
	eval(sprintf ( 'hopsgui_velocity_settingsmenu \n'));	

case 'setarrowsampling'

	hopsgui_obj{1}.isub=round(str2num(get(gcbo,'String')));
	delete(hopsgui_obj{1}.hvectors1)
	delete(hopsgui_obj{1}.hvectors2)
	delete(hopsgui_obj{1}.hvectors3)
	figure(hopsgui_obj{1}.hfigure)
	hold on		
	eval(sprintf ( 'hopsgui_vector_command %s\n',hopsgui_obj{1}.vectoraction));
	hold off

	
case 'setarrowscale'

	hopsgui_obj{1}.fac=str2num(get(gcbo,'String'));
	delete(hopsgui_obj{1}.hvectors1)
	delete(hopsgui_obj{1}.hvectors2)
	delete(hopsgui_obj{1}.hvectors3)
	figure(hopsgui_obj{1}.hfigure)
	hold on		
	eval(sprintf ( 'hopsgui_vector_command %s\n',hopsgui_obj{1}.vectoraction));
	hold off

	

case 'setarrowcolor'

	hopsgui_obj{1}.vectorcolor=get(gcbo,'String');
	delete(hopsgui_obj{1}.hvectors1)
	delete(hopsgui_obj{1}.hvectors2)
	delete(hopsgui_obj{1}.hvectors3)
	figure(hopsgui_obj{1}.hfigure)
	hold on		
	eval(sprintf ( 'hopsgui_vector_command %s\n',hopsgui_obj{1}.vectoraction));
	hold off

	
	
case 'setarrowreference'

	hopsgui_obj{1}.refvector=str2num(get(gcbo,'String'));
	delete(hopsgui_obj{1}.hvectors1)
	delete(hopsgui_obj{1}.hvectors2)
	delete(hopsgui_obj{1}.hvectors3)
	figure(hopsgui_obj{1}.hfigure)
	hold on		
	eval(sprintf ( 'hopsgui_vector_command %s\n',hopsgui_obj{1}.vectoraction));
	hold off


	
case 'fc_edit_depth'

	hopsgui_obj{1}.depth=round(str2num(get(hopsgui_obj{1}.hedit_depth,'String')));
	set(hopsgui_obj{1}.hslider_depth,'Value',hopsgui_obj{1}.depth);
	if ~strcmp('kindex_contour',strrep(hopsgui_obj{1}.action,' ','')) & ~strcmp('depth_contour',strrep(hopsgui_obj{1}.action,' ',''))
	zoom off;set(findobj('String','Zoom'),'Value',0);
	hopsgui_obj{1}.czoom=0;
	end

	
case 'fc_slider_depth'

	hopsgui_obj{1}.depth=round(get(hopsgui_obj{1}.hslider_depth,'Value'));
	set(hopsgui_obj{1}.hedit_depth,'String',num2str(hopsgui_obj{1}.depth));
	if ~strcmp('kindex_contour',strrep(hopsgui_obj{1}.action,' ','')) & ~strcmp('depth_contour',strrep(hopsgui_obj{1}.action,' ',''))
	zoom off;set(findobj('String','Zoom'),'Value',0);
	hopsgui_obj{1}.czoom=0;
	end


case 'fc_edit_iindex'

	hopsgui_obj{1}.iindex=fix(str2num(get(hopsgui_obj{1}.hedit_iindex,'String')));
	set(hopsgui_obj{1}.hslider_iindex,'Value',hopsgui_obj{1}.iindex);
	if ~strcmp('iindex_contour',strrep(hopsgui_obj{1}.action,' ',''))
	zoom off;set(findobj('String','Zoom'),'Value',0);
	hopsgui_obj{1}.czoom=0;
	end
	
	
case 'fc_edit_jindex'

	hopsgui_obj{1}.jindex=fix(str2num(get(hopsgui_obj{1}.hedit_jindex,'String')));
	set(hopsgui_obj{1}.hslider_jindex,'Value',hopsgui_obj{1}.jindex);
	if ~strcmp('jindex_contour',strrep(hopsgui_obj{1}.action,' ',''))
	zoom off;set(findobj('String','Zoom'),'Value',0);
	hopsgui_obj{1}.czoom=0;
	end

	
case 'fc_slider_iindex'

	hopsgui_obj{1}.iindex=fix(get(hopsgui_obj{1}.hslider_iindex,'Value'));
	set(hopsgui_obj{1}.hedit_iindex,'String',num2str(hopsgui_obj{1}.iindex));	
	if ~strcmp('iindex_contour',strrep(hopsgui_obj{1}.action,' ',''))
	zoom off;set(findobj('String','Zoom'),'Value',0);
	hopsgui_obj{1}.czoom=0;
	end
	

	
case 'fc_slider_jindex'

	hopsgui_obj{1}.jindex=fix(get(hopsgui_obj{1}.hslider_jindex,'Value'));
	set(hopsgui_obj{1}.hedit_jindex,'String',num2str(hopsgui_obj{1}.jindex));
	if ~strcmp('jindex_contour',strrep(hopsgui_obj{1}.action,' ',''))
	zoom off;set(findobj('String','Zoom'),'Value',0);
	hopsgui_obj{1}.czoom=0;
	end


case 'fc_edit_kindex'

	hopsgui_obj{1}.kindex=fix(str2num(get(hopsgui_obj{1}.hedit_kindex,'String')));
	set(hopsgui_obj{1}.hslider_kindex,'Value',hopsgui_obj{1}.kindex);
	if ~strcmp('kindex_contour',strrep(hopsgui_obj{1}.action,' ','')) & ~strcmp('depth_contour',strrep(hopsgui_obj{1}.action,' ',''))
	zoom off;set(findobj('String','Zoom'),'Value',0);
	hopsgui_obj{1}.czoom=0;
	end



case 'fc_slider_kindex'

	hopsgui_obj{1}.kindex=fix(get(hopsgui_obj{1}.hslider_kindex,'Value'));
	set(hopsgui_obj{1}.hedit_kindex,'String',num2str(hopsgui_obj{1}.kindex));
	if ~strcmp('kindex_contour',strrep(hopsgui_obj{1}.action,' ','')) & ~strcmp('depth_contour',strrep(hopsgui_obj{1}.action,' ',''))
	zoom off;set(findobj('String','Zoom'),'Value',0);
	hopsgui_obj{1}.czoom=0;
	end


case 'setarrowrvariable'
	choice=get(gcbo,'Value');
	if choice==1, hopsgui_obj{1}.vectorvar='vtot';
	elseif choice==2, hopsgui_obj{1}.vectorvar='vclin';
	elseif choice==3, hopsgui_obj{1}.vectorvar='vgeo';	
	end


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


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

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

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

end

if ~strcmp('exit',strrep(action,' ',''))
set(hopsgui_obj{1}.hfigure,'pointer','arrow');
end

return;

