function cmgtoolfunc(action);
% This is the callback function for the cmgtool GUI
global xfile xf xhand xnum xstr xvalue whichnew whereat jtime binasked whichbin
global xcount xslide xfnew ncf xsave whichfile timebase initialplot initialsave ratioxy ratiox ratioy
global NC_WHATS_NEW_DISPLAYED tdomain fdomain liststore xfnewstore whereatstore xcountstore goodindex
global anachoice tflag

NC_WHATS_NEW_DISPLAYED=1;
examined=0;
plotted=0;
set(xhand.newvarlist,'enable','on');
	
switch action

case 'ModuleTSD'
	tsd=1; adcp=0;
	set(xhand.ToolMenuT,'enable','on');
	set(xhand.ToolMenuF,'enable','on');
	set(xhand.ModuleTSD,'checked','on');
	set(xhand.ModuleADCP,'checked','off');
	
case 'ModuleADCP'
	tsd=0; adcp=1;
	set(xhand.ToolMenuT,'enable','off');
	set(xhand.ToolMenuF,'enable','off');
	set(xhand.ModuleTSD,'checked','off');
	set(xhand.ModuleADCP,'checked','on');
	
case 'ToolMenuT'
	tdomain=1;fdomain=0;
	set(xhand.ToolMenuT,'checked','on');
	set(xhand.ToolMenuF,'checked','off');
	set(xhand.graphit,'string',{'GraphIt','1 Panel','2 Panels','3 Panels','4 Panels','Scatter Plot'});
	set(xhand.analyzeit,'String',anachoice.tsd);
	set(xhand.graphit,'FontWeight','normal','value',1);
	set(xhand.analyzeit,'FontWeight','normal','value',1);
	set(xhand.saveit,'FontWeight','normal','value',1);
	set(xhand.panpush,'visible','off');
	set(xhand.varpanedit,'visible','off');
	set(xhand.titpanedit,'visible','off');
	set(xhand.ypanedit,'visible','off');
	set(xhand.ylimedit,'visible','off');
	set(xhand.plotheader,'visible','off');
	set(xhand.ylimheader,'visible','off');
	set(xhand.plot,'visible','off');
	set(xhand.anatext,'visible','off');
	set(xhand.run,'visible','off');
	set(xhand.options,'visible','off')
	set(xhand.ana,'visible','off');
	set(xhand.anapush,'visible','off');
	set(xhand.oprand,'visible','off');
	set(xhand.checkbox,'visible','off');
	set(xhand.main,'name','CMGTooL - TimeSeries');
	xf.newvarlist=liststore{1};
	xfnew=xfnewstore{1};
	whereat=whereatstore{1};
	xcount.new=xcountstore{1};
	set(xhand.newvarlist,'string',xf.newvarlist,'value',1);
	if xcount.file>0
		fdisplay(xnum.file,0);
	end;
	xcount.save=0;
	xf.savevarlist=[];
	xf.nicknames=[];
	set(xhand.savevarlist,'visible','off','string','','value',1);
	set(xhand.savevarname,'visible','off');
	set(xhand.save,'visible','off');
	set(xhand.addvar,'visible','off');
	set(xhand.addnewvar,'visible','off');
	set(xhand.remvar,'visible','off');
	set(xhand.rename,'visible','off');
	
case 'ToolMenuF'
	tdomain=0;fdomain=1;
	set(xhand.ToolMenuT,'checked','off');
	set(xhand.ToolMenuF,'checked','on');
	set(xhand.graphit,'string',{'GraphIt','LogLog','SemiLogY','SemiLogX','Linear'});
	set(xhand.analyzeit,'string',anachoice.freq);
	set(xhand.graphit,'FontWeight','normal','value',1);
	set(xhand.analyzeit,'FontWeight','normal','value',1);
	set(xhand.saveit,'FontWeight','normal','value',1);
	set(xhand.panpush,'visible','off');
	set(xhand.varpanedit,'visible','off');
	set(xhand.titpanedit,'visible','off');
	set(xhand.ypanedit,'visible','off');
	set(xhand.ylimedit,'visible','off');
	set(xhand.plotheader,'visible','off');
	set(xhand.ylimheader,'visible','off');
	set(xhand.plot,'visible','off');
	set(xhand.anatext,'visible','off');
	set(xhand.run,'visible','off');
	set(xhand.options,'visible','off')
	set(xhand.ana,'visible','off');
	set(xhand.anapush,'visible','off');
	set(xhand.oprand,'visible','off');
	set(xhand.checkbox,'visible','off');
	set(xhand.main,'name','CMGTooL - Spectral');
	xf.newvarlist=liststore{2};
	xfnew=xfnewstore{2};
	whereat=whereatstore{2};
	xcount.new=xcountstore{2};
	set(xhand.newvarlist,'string',xf.newvarlist,'value',1);
	if xcount.file>0
		fdisplay(xnum.file,0);
	end;
	xcount.save=0;
	xf.savevarlist=[];
	xf.nicknames=[];
	set(xhand.savevarlist,'visible','off','string','','value',1);
	set(xhand.savevarname,'visible','off');
	set(xhand.save,'visible','off');
	set(xhand.addvar,'visible','off');
	set(xhand.addnewvar,'visible','off');
	set(xhand.remvar,'visible','off');
	set(xhand.rename,'visible','off');
		
case 'load'
	if xcount.file>9
		errordlg('CMGTooL can only take upto 10 files per session!','Error');
		return;
	end;
	set(xhand.graphit,'FontWeight','normal');
	set(xhand.analyzeit,'FontWeight','normal');
	set(xhand.saveit,'FontWeight','normal');
	[xncf,xncpath]=uigetfile('*','Choose a NetCDF file');
	if (xncf==0),
		return;
	else
		cd(xncpath);
	end;
	fnstr=get(xhand.filename,'string');
	if ~iscell(fnstr)
		fnstr=cellstr(fnstr);
	end;
	for i=2:length(fnstr)
		if strcmpi(fnstr{i}(5:end),xncf)
			errordlg(['File ' xncf ' is already loaded!'],'Error');
			return;
		end;
	end;
			
% 	if any(strcmpi([num2str(xcount.file) '.  ' xncf],get(xhand.filename,'string')))
% 		errordlg(['File ' xncf ' is already loaded!'],'Error');
% 		return;
% 	end;
	set(xhand.notes,'string','Loading a file.....');
	xcount.file=xcount.file+1;
	ncf(xcount.file).file=netcdf([xncpath xncf],'nowrite');
	if isempty(ncf(xcount.file).file),
		xcount.file=xcount.file-1;
		errordlg('File is not a NetCDF format, try again...','Error');
		return;
		set(xhand.notes,'string','Ready - Waiting for command...');	
	end;
	if xcount.file<2
		xf.newvarlist=[];
		xf.savevarlist=[];
		xf.nicknames=[];
		xnum.newvarlist=1;
		xnum.savevarlist=1;
	end;

	xncf=[num2str(xcount.file) '.  ' xncf];
    set(xhand.filename,'visible','on','string',...
		cellstr(strvcat(char(get(xhand.filename,'string')),xncf)));
	set(xhand.filename,'value',xcount.file+1);
    ncf(xcount.file).var=var(ncf(xcount.file).file);
    ncf(xcount.file).topatt=att(ncf(xcount.file).file);
    ncf(xcount.file).dim=dim(ncf(xcount.file).file);
    ncf(xcount.file).varlist=ncnames(ncf(xcount.file).var);
    ncf(xcount.file).topattlist=ncnames(ncf(xcount.file).topatt);
    ncf(xcount.file).dimlist=ncnames(ncf(xcount.file).dim);
	ncf(xcount.file).att=ncf(xcount.file).topatt;
	ncf(xcount.file).attlist=ncf(xcount.file).topattlist;
	set(xhand.author,'visible','off');
	xnum.file=xcount.file;
	fdisplay(xcount.file,1);
	
	clear xnc*;
	set(xhand.notes,'string','Ready - Waiting for command...');	
	
case 'reset'
	set(xhand.graphit,'FontWeight','normal');
	set(xhand.analyzeit,'FontWeight','normal');
	set(xhand.saveit,'FontWeight','normal');
	xcount.file=0;
	xcount.save=0;
	xcount.new=0;
	whereat=[];
	whichfile=[];
	whichnew.save=[];
	whichnew.graph=[];
	whichnew.ana=[];
	xf=[];
	xfile=[];
	xsave=[];
	set(xhand.checkbox,'visible','off');
	set(xhand.oprand,'visible','off');
	set(xhand.panpush,'visible','off');
	set(xhand.varpanedit,'visible','off');
	set(xhand.titpanedit,'visible','off');
	set(xhand.ypanedit,'visible','off');
	set(xhand.ylimedit,'visible','off');
	set(xhand.plotheader,'visible','off');
	set(xhand.ylimheader,'visible','off');
	set(xhand.plot,'visible','off');
	set(xhand.anatext,'visible','off');
	set(xhand.dof,'visible','off');
	set(xhand.run,'visible','off');
	set(xhand.options,'visible','off')
	set(xhand.ana,'visible','off');
	set(xhand.anapush,'visible','off');
	set(xhand.filename,'string','LOADED FILES','value',1);
	set(xhand.varlist,'string','','value',1);
	set(xhand.newvarlist,'string','','value',1);
	set(xhand.savevarlist,'visible','off','string','','value',1);
	set(xhand.savevarname,'visible','off');
	set(xhand.save,'visible','off');
	set(xhand.addvar,'visible','off');
	set(xhand.addnewvar,'visible','off');
	set(xhand.remvar,'visible','off');
	set(xhand.rename,'visible','off');
	set(xhand.length,'string','');
	set(xhand.dt,'string','');
	set(xhand.radio,'value',1);
	set(xhand.att,'string','ATTRIBUTES','value',1);
	set(xhand.attvalue,'string','Click PopupMenu to see file attributes...');
	set(xhand.singlevalue,'string','');
	set(xhand.graphit,'enable','off','value',1);
	set(xhand.saveit,'enable','off','value',1);
	set(xhand.analyzeit,'enable','off','value',1);
	set(xhand.zoomframe,'enable','off');
	set(xhand.zoomymdfrom,'string','');
	set(xhand.zoomhmsfrom,'string','');
	set(xhand.zoomymdto,'string','');
	set(xhand.zoomhmsto,'string','');
	set(xhand.zoomslidefrom,'enable','off','value',0);
	set(xhand.zoomslideto,'enable','off','value',1);
	set(xhand.closefile,'visible','off');
	for i=1:2
		liststore{i}=[];
		xfnewstore{i}=[];
		whereatstore{i}=[];
		xcountstore{i}=0;
	end;
	cmgtoolfunc('ToolMenuT');
	
case 'pickfile'
	ii=get(xhand.filename,'value')-1;
	if ii<1
% 		set(xhand.closefile,'visible','on','enable','off');
		set(xhand.filename,'value',xnum.file+1);
		return;
	end;
	xnum.file=ii;
	fdisplay(ii,0);
	
	if tdomain & (get(xhand.analyzeit,'value')-1)~=10
		set(xhand.ana,'string','');
	end;
	set(xhand.closefile,'visible','on','enable','on');
	
% 	set(xhand.options,'visible','off')
% 	set(xhand.ana,'visible','off');
% 	set(xhand.anapush,'visible','off');
% 	set(xhand.anatext,'visible','off');
% 	set(xhand.run,'visible','off');
% 	set(xhand.analyzeit,'value',1);
	
	initialplot=1;
	
case 'closefile'

	filestr=get(xhand.filename,'string');
	ncf(xnum.file)=[];
	jtime(xnum.file)=[];
	filestr(xnum.file+1)=[];
	xcount.file=xcount.file-1;
	if xcount.file>0
		if xnum.file>xcount.file
			xnum.file=xcount.file;
		end;
		for i=xnum.file+1:length(filestr);
			filestr{i}=strrep(filestr{i},[num2str(i) '.'],[num2str(i-1) '.']);
		end;
		set(xhand.filename,'string',filestr,'value',xnum.file+1);
		fdisplay(xnum.file,0);
		
		set(xhand.graphit,'value',1);
		set(xhand.analyzeit,'value',1);
		set(xhand.saveit,'value',1);
		set(xhand.checkbox,'visible','off');
		set(xhand.oprand,'visible','off');
		set(xhand.panpush,'visible','off');
		set(xhand.varpanedit,'visible','off');
		set(xhand.titpanedit,'visible','off');
		set(xhand.ypanedit,'visible','off');
		set(xhand.ylimedit,'visible','off');
		set(xhand.plotheader,'visible','off');
		set(xhand.ylimheader,'visible','off');
		set(xhand.plot,'visible','off');
		set(xhand.anatext,'visible','off');
		set(xhand.dof,'visible','off');
		set(xhand.run,'visible','off');
		set(xhand.options,'visible','off')
		set(xhand.ana,'visible','off');
		set(xhand.anapush,'visible','off');
		set(xhand.savevarlist,'visible','off','string','','value',1);
		set(xhand.savevarname,'visible','off');
		set(xhand.save,'visible','off');
		set(xhand.addvar,'visible','off');
		set(xhand.addnewvar,'visible','off');
		set(xhand.remvar,'visible','off');
		set(xhand.rename,'visible','off');
		xf.savevarlist=[];
		xf.nicknames=[];
		xcount.save=0;
	else
		cmgtoolfunc('reset');
% 		set(xhand.closefile,'enable','off');
		
	end;
	
case 'radio'
	if get(xhand.radio,'value')==1,
		xf.att=xf.topatt;
		xf.attlist=xf.topattlist;
	    set(xhand.att,'visible','on','string',{'ATTRIBUTES',xf.topattlist{:}},'value',1);
	    set(xhand.attvalue,'visible','on','string','Click PopupMenu to see file attributes...');
	    set(xhand.singlevalue,'string','');
	    set(xhand.length,'string','');
	end;
case 'varlist'
	set(xhand.remvarnew,'enable','off');
	set(xhand.newvarlist,'Backgroundcolor',[1 1 1]);
	set(xhand.varlist,'Backgroundcolor',[1 0.9 .8]);
	xnum.varlist=get(gcbo,'value');
	temp=size(xfile{xnum.varlist}(:,:));
	if temp(1)>length(goodindex{xnum.file})
		temp(1)=length(goodindex{xnum.file});
	end;
	if sum(temp)>length(temp)
		  set(xhand.singlevalue,'string','AN ARRAY','Foregroundcolor',[0 0 0]);
	else
		xvalue.single=num2str(xf.var{xnum.varlist}(:,:));
		set(xhand.singlevalue,'string',xvalue.single,'Foregroundcolor',[0 0 0]);
	end;
	if length(temp)==2
		xstr.length=['[' num2str(temp(1)) 'x' num2str(temp(2)) ']'];
	elseif length(temp)==3
		xstr.length=['[' num2str(temp(1)) 'x' num2str(temp(2)) 'x' num2str(temp(3)) ']'];
	else 
		xstr.length=['[4-D]'];
	end;
	
	xf.att=att(xfile{xnum.varlist});
	xstr.att=ncnames(xf.att);
	set(xhand.length,'string',xstr.length,'Foregroundcolor',[0 0 0]);
	set(xhand.att,'string',{'ATTRIBUTES', xstr.att{:}},'value',1);
	set(xhand.attvalue,'string','Click PopupMenu to view properties...');
	set(xhand.addvar,'enable','on');
	set(xhand.remvar,'enable','off');
	set(xhand.rename,'enable','off');
	set(xhand.addnewvar,'enable','off');
	set(xhand.radio,'value',0);
% 	set(xhand.att,'enable','on');
	
% 	jtime{xnum.file}=jtime{xnum.file}(:,:);
% 	if length(jtime{xnum.file})<6
		delt=round(mean(diff(jtime{xnum.file}))*24*3600);
% 	else
% 		delt=round(mean([diff(jtime{xnum.file}(1:5)); diff(jtime{xnum.file}(end-5 : end))])*24*3600);
% 	end;
	set(xhand.dt,'string',num2str(delt),'Foregroundcolor',[0 0 0]);

	ii=get(xhand.filename,'value')-1;
	if ii<1
		return;
	end;
	xnum.file=ii;

	[ymd,hms]=getgregorian(min(jtime{ii}));
	set(xhand.zoomymdfrom,'visible','on','string',ymd,'Foregroundcolor',[0 0 0]);
	set(xhand.zoomhmsfrom,'visible','on','string',hms,'Foregroundcolor',[0 0 0]);
	[ymd,hms]=getgregorian(max(jtime{ii}));
	set(xhand.zoomymdto,'visible','on','string',ymd,'Foregroundcolor',[0 0 0]);
	set(xhand.zoomhmsto,'visible','on','string',hms,'Foregroundcolor',[0 0 0]);
	set(xhand.zoomslidefrom,'visible','on','enable','off','value',0);
	set(xhand.zoomslideto,'visible','on','enable','off','value',1);
	initialplot=1;
	
case 'popupmenu'
	xnum.att=get(gcbo,'value');
	if xnum.att>1,
		xstr.attvalue=xf.att{xnum.att-1}(:,:);
		if ~ischar(xstr.attvalue),
		  xstr.attvalue=num2str(xstr.attvalue);
		end;
		set(xhand.attvalue,'string',xstr.attvalue);
	else
		set(xhand.attvalue,'string','Click PopupMenu to view properties...');	
	end;

case 'graphit'
	set(xhand.graphit,'FontWeight','bold');
	set(xhand.analyzeit,'FontWeight','normal');
	set(xhand.saveit,'FontWeight','normal');
	set(xhand.savevarlist,'visible','off');
	set(xhand.savevarname,'visible','off');
	set(xhand.addvar,'visible','off');
	set(xhand.addnewvar,'visible','off');
	set(xhand.remvar,'visible','off');
	set(xhand.rename,'visible','off');
	set(xhand.save,'visible','off');
	set(xhand.options,'visible','off')
	set(xhand.ana,'visible','off','string','');
	set(xhand.anapush,'visible','off');
	set(xhand.anatext,'visible','off');
	set(xhand.dof,'visible','off');
	set(xhand.run,'visible','off');
	set(xhand.oprand,'visible','off');
	set(xhand.checkbox,'visible','off');
	set(xhand.analyzeit,'value',1);
	set(xhand.saveit,'value',1);
	set(xhand.panpush,'visible','off');
	set(xhand.varpanedit,'visible','off','string','');
	set(xhand.titpanedit,'visible','off','string','');
	set(xhand.ypanedit,'visible','off','string','');
	set(xhand.ylimedit,'visible','off','string','');
	set(xhand.plotheader,'visible','off');
	set(xhand.ylimheader,'visible','off');
	set(xhand.plot,'visible','off');
	whichfile=zeros(6,6);
	whichnew.graph=[];
	pan=get(xhand.graphit,'value') - 1;
	if pan<1
		return;
	end;
	if tdomain & pan==5
		set(xhand.plot,'visible','off');
		len=100;
		set(xhand.anapush(1),'visible','on',...
			'Position',[20 200 70 20].*ratioxy,...
			'string','Var 1 >>');
		set(xhand.anapush(2),'visible','on',...
			'Position',[20 175 70 20].*ratioxy,...
			'string','Var 2 >>');
		set(xhand.ana(1),'visible','on', ...
			'Position',[95 200 100 20].*ratioxy);
		set(xhand.ana(2),'visible','on', ...
			'Position',[95 175 len 20].*ratioxy);
		set(xhand.run,'visible','on','position',[100+len 175 30 20].*ratioxy,...
			'string','PLOT','callback','analyses(''plot_scatter'')');
		set(xhand.checkbox,'visible','on','string','Plot ellipse');
		return;
	end;
	if tdomain & pan==6
		set(xhand.plot,'visible','off');
		len=100;
		set(xhand.anapush(1),'visible','on',...
			'Position',[20 200 70 20].*ratioxy,...
			'string','East Vel. >>');
		set(xhand.anapush(2),'visible','on',...
			'Position',[20 175 70 20].*ratioxy,...
			'string','North Vel. >>');
		set(xhand.ana(1),'visible','on', ...
			'Position',[95 200 100 20].*ratioxy);
		set(xhand.ana(2),'visible','on', ...
			'Position',[95 175 len 20].*ratioxy);
		set(xhand.run,'visible','on','position',[100+len 175 30 20].*ratioxy,...
			'string','VIEW','callback','analyses(''adcpview'')');
% 		set(xhand.checkbox,'visible','on','string','Plot ellipse');
		return;
	end;
	if fdomain
		set(xhand.panpush(1),'visible','on','position',[235 170 55 20].*ratioxy);
		set(xhand.varpanedit(1),'visible','on');
		set(xhand.plot,'callback','analyses(''plot_fdomain'')');
		pos=get(xhand.panpush(1),'position');
		pos=pos(2);
	  	temp1={'Variables'};
	else
		set(xhand.plot,'callback','analyses(''plot'')');
		set(xhand.panpush(1),'position',[371 170 55 20].*ratioxy);
		for i=1:pan,
			set(xhand.panpush(i),'visible','on');
			set(xhand.varpanedit(i),'visible','on');
			set(xhand.titpanedit(i),'visible','on');
			set(xhand.ypanedit(i),'visible','on');
		end;
		pos=get(xhand.panpush(pan),'position');
		pos=pos(2);
	  	temp1={'Variables','Title','Ylabel'};
	end;
	set(xhand.plot,'visible','on');
  	temp2=[100 235 310];
  	for j=1:length(temp1),
		set(xhand.plotheader(j),'visible','on','position',[temp2(j) 25+(pos)/ratioy 50 15/ratioy].*ratioxy,...
			'string',temp1{j});
	end;
	initialplot=1;
	binasked=0;
	whichbin=1;

case 'saveit'
	set(xhand.saveit,'FontWeight','bold');
	set(xhand.graphit,'FontWeight','normal','value',1);
	set(xhand.analyzeit,'FontWeight','normal','value',1);
	set(xhand.panpush,'visible','off');
	set(xhand.varpanedit,'visible','off');
	set(xhand.titpanedit,'visible','off');
	set(xhand.ypanedit,'visible','off');
	set(xhand.ylimedit,'visible','off');
	set(xhand.plotheader,'visible','off');
	set(xhand.ylimheader,'visible','off');
	set(xhand.plot,'visible','off');
	set(xhand.options,'visible','off')
	set(xhand.ana,'visible','off');
	set(xhand.anapush,'visible','off');
	set(xhand.anatext,'visible','off');
	set(xhand.dof,'visible','off');
	set(xhand.run,'visible','off');
	set(xhand.oprand,'visible','off');
	set(xhand.checkbox,'visible','off');

	set(xhand.savevarlist,'visible','on');
	set(xhand.savevarname,'visible','on');
	set(xhand.addvar,'visible','on','enable','off');
	set(xhand.addnewvar,'visible','on','enable','off');
	set(xhand.remvar,'visible','on');
	set(xhand.rename,'visible','on');
	if isempty(xf.savevarlist)
		set(xhand.save,'visible','on','enable','off');
	else
		set(xhand.save,'visible','on','enable','on');
	end;
	if fdomain
		set(xhand.save,'callback','analyses(''save_fdomain'')');
	else
		set(xhand.save,'callback','analyses(''save'')');
	end;	
	
case 'varlistnew'
	set(xhand.newvarlist,'Backgroundcolor',[1 .9 .8]);
	set(xhand.varlist,'Backgroundcolor',[1 1 1]);
	set(xhand.addvar,'enable','off');
	set(xhand.remvar,'enable','off');
	set(xhand.rename,'enable','off');
	thisvar=get(gco,'string');
	xnum.newvarlist=get(gco,'value');
	if isempty(thisvar)
		set(xhand.remvarnew,'enable','off');
	else	
		set(xhand.addnewvar,'enable','on');
		set(xhand.remvarnew,'visible','on','enable','on');
		xnum.newvarlist=get(gcbo,'value');
		temp=size(xfnew(xnum.newvarlist).data);
		if sum(temp)>length(temp)
			  set(xhand.singlevalue,'string','AN ARRAY','Foregroundcolor',[0 0.4 0]);
		else
			xvalue.single=xfnew(xnum.newvarlist).data;
			xvalue.single=num2str(xvalue.single);
			set(xhand.singlevalue,'string',xvalue.single,'Foregroundcolor',[0 0.4 0]);
		end;
		if length(temp)==2
			xstr.length=['[' num2str(temp(1)) 'x' num2str(temp(2)) ']'];
		elseif length(temp)==3
			xstr.length=['[' num2str(temp(1)) 'x' num2str(temp(2)) 'x' num2str(temp(3)) ']'];
		else 
			xstr.length=['[4-D]'];
		end;
		set(xhand.length,'string',xstr.length,'Foregroundcolor',[0 .4 0]);
		thistime=[];
% 		suffixstr=[];
% 		
% 		dum=findstr('_',fliplr(thisvar{xnum.newvarlist}));
% 		if ~isempty(dum)
% 			thisindex=find(strcmpi(thisvar{xnum.newvarlist}(end-dum+1 : end),tflag)==1);
% 			if ~isempty(thisindex)
% 				suffixstr=tflag{thisindex};
% 			end;
% 		end;
		
% 		for i=1:length(tflag)
% 			dum=length(tflag{i})-1;
% 			if length(thisvar{xnum.newvarlist})>dum
% 				if strcmpi(thisvar{xnum.newvarlist}(end-dum : end),tflag{i})
% 					suffixstr=tflag{i};
% 					break;
% 				end;
% 			end;
% 		end;
% 		if ~isempty(suffixstr)
% 			thistime=xfnew(xnum.newvarlist).time;
% 		end;
% 		if isempty(thistime)
% 			thistime=jtime{whereat(xnum.newvarlist)};
% 		end;
		if tdomain
			if isempty(xfnew(xnum.newvarlist).time)
				thistime=jtime{whereat(xnum.newvarlist)};
			else
				thistime=xfnew(xnum.newvarlist).time;
			end;
			thistime=thistime(:,:);
	% 		if length(thistime)<6
				delt=round(mean(diff(thistime))*24*3600);
	% 		else
	% 			delt=round(mean([diff(thistime(1:5)); diff(thistime(end-5 : end))])*24*3600);
	% 		end;
			set(xhand.dt,'string',num2str(delt),'Foregroundcolor',[0 .4 0]);
			
			[ymd,hms]=getgregorian(min(thistime));
			set(xhand.zoomymdfrom,'visible','on','string',ymd,'Foregroundcolor',[0 .4 0]);
			set(xhand.zoomhmsfrom,'visible','on','string',hms,'Foregroundcolor',[0 .4 0]);
			[ymd,hms]=getgregorian(max(thistime));
			set(xhand.zoomymdto,'visible','on','string',ymd,'Foregroundcolor',[0 .4 0]);
			set(xhand.zoomhmsto,'visible','on','string',hms,'Foregroundcolor',[0 .4 0]);
			set(xhand.zoomslidefrom,'visible','on','enable','off','value',0);
			set(xhand.zoomslideto,'visible','on','enable','off','value',1);
		else
			set(xhand.zoomymdfrom,'Foregroundcolor',[0 0 0]);
			set(xhand.zoomhmsfrom,'Foregroundcolor',[0 0 0]);
			set(xhand.zoomymdto,'Foregroundcolor',[0 0 0]);
			set(xhand.zoomhmsto,'Foregroundcolor',[0 0 0]);
			
		end;
		initialplot=1;
		xf.att=xfnew(xnum.newvarlist).att;
		xstr.att=xfnew(xnum.newvarlist).attname;
		set(xhand.att,'string',{'ATTRIBUTES', xstr.att{:}},'value',1);
		set(xhand.attvalue,'string','Click PopupMenu to view properties...');
	end;
% 	set(xhand.att,'enable','off');
%     set(xhand.attvalue,'string','');
	set(xhand.radio,'value',0);
	
case 'varlistsave'
	set(xhand.addvar,'enable','off');
	set(xhand.addnewvar,'enable','off');
	set(xhand.remvarnew,'enable','off');
	if ~isempty(get(gco,'string')),
		set(xhand.remvar,'enable','on');
		set(xhand.rename,'enable','on');
		xnum.savevarlist=get(gcbo,'value');
	end;

case 'addvar'
	msg_add={'Lat, Lon, Depth, time, time2, and TIM will be saved automatically.'};
	msg_save1={'Variables of different dimensions can''t be save in one file.',...
		'Remove files of different length and save them separately.'};
	msg_save2={'Variables from different source netCDF files can''t be saved in one new netCDF file.'};
	if any(strcmpi(xf.varlist{xnum.varlist},{'lat','lon','depth','time','time2','TIM'})),
		errordlg(msg_add,'Error');
		return;
	end;
	temp1=xfile{xnum.varlist}(:,:);
	if size(temp1,1)>length(goodindex{xnum.file})
		temp1=temp1(goodindex{xnum.file},:);
	end;
	temp2=length(temp1);
	if xcount.save>0
		if (temp2~=xsave(1).length)
			errordlg(msg_save1,'Error');
			return;
		end;
		if xsave(xcount.save).file ~= xnum.file
			errordlg(msg_save2,'Error');
			return;
		end;
	end;		
	xcount.save=xcount.save+1;
	xf.savevarlist{xcount.save}=xf.varlist{xnum.varlist};
	temp1=dataclean(temp1);
	xsave(xcount.save).data=temp1;
	xsave(xcount.save).length=length(temp1);
	xsave(xcount.save).file=xnum.file;
	xsave(xcount.save).type=datatype(xfile{xnum.varlist});
	whichnew.save(1,xcount.save)=0;
	
	xf.nicknames{xcount.save}=xf.savevarlist{xcount.save};	
	set(xhand.savevarlist,'string',xf.nicknames);
% 	set(xhand.savevarlist,'string',xf.savevarlist);
	set(xhand.save,'enable','on');
	set(xhand.addvar,'enable','off');
	
case 'addnewvar'
	msg_save={'Variables of different dimensions can''t be save in one file.',...
		'Remove files of different length and save them separately.'};
	temp1=xfnew(xnum.newvarlist).data;
	temp2=length(temp1);
	if xcount.save>0
		if (temp2~=xsave(1).length) | (xsave(xcount.save).file ~= whereat(xnum.newvarlist))
			errordlg(msg_save,'Error');
			return;
		end;
	end;		
	xcount.save=xcount.save+1;
	xf.savevarlist{xcount.save}=xf.newvarlist{xnum.newvarlist};
	whichnew.save(1,xcount.save)=xnum.newvarlist;
	temp1=dataclean(temp1);
	xsave(xcount.save).data=temp1;
	xsave(xcount.save).length=temp2;
	xsave(xcount.save).file=whereat(xnum.newvarlist);
	xsave(xcount.save).type='float';
		
	xf.nicknames{xcount.save}=xf.savevarlist{xcount.save};	
	set(xhand.savevarlist,'string',xf.nicknames);
% 	set(xhand.savevarlist,'string',xf.savevarlist);
	set(xhand.save,'enable','on');
	set(xhand.addnewvar,'enable','off');
	
case 'removevar'
	switch gco
	case xhand.remvar
		xf.savevarlist(xnum.savevarlist)=[];
		xf.nicknames(xnum.savevarlist)=[];
		temp=struct2cell(xsave);
		ff=fieldnames(xsave);
		temp(:,:,xnum.savevarlist)=[];
		xsave=cell2struct(temp,ff,1);
		xcount.save=xcount.save-1;
		if ~isempty(whichnew.save)
			whichnew.save(xnum.savevarlist)=[];
		end;
		if xcount.save>0,
% 			set(xhand.savevarlist,'string',xf.savevarlist,'value',xcount.save);
			set(xhand.savevarlist,'string',xf.nicknames,'value',xcount.save);
		else
% 			set(xhand.savevarlist,'string',xf.savevarlist,'value',1);
			set(xhand.savevarlist,'string',xf.nicknames,'value',1);
		end;
		
		xnum.savevarlist=xcount.save;
		if xcount.save<1,
			set(xhand.remvar,'enable','off');
			set(xhand.rename,'enable','off');
			set(xhand.save,'enable','off');
		end;
	
	case xhand.remvarnew
		xfnew(xnum.newvarlist)=[];
		xf.newvarlist(xnum.newvarlist)=[];
		xcount.new=xcount.new-1;
		if xcount.new>0,
			set(xhand.newvarlist,'string',xf.newvarlist,'value',xcount.new);
		else
			set(xhand.newvarlist,'string',xf.newvarlist,'value',1);
		end;
		xnum.newvarlist=xcount.new;
		if xcount.new<1,
			set(xhand.remvarnew,'enable','off');
		end;
		if tdomain
			id=1;
		elseif fdomain
			id=2;
		end;
		
		liststore{id}=xf.newvarlist;
		xfnewstore{id}=xfnew;
		whereatstore{id}=whereat;
		xcountstore{id}=xcount.new;
		

	end;

case 'rename'
	newname=inputdlg('Type in the new name:','Rename');
	if ~isempty(newname) & length(newname{1})>0
		xf.nicknames{xnum.savevarlist}=newname{1};
		set(xhand.savevarlist,'string',xf.nicknames,'value',xnum.savevarlist);
		
	end;
	
case 'analyzeit'
	set(xhand.analyzeit,'FontWeight','bold');
	set(xhand.graphit,'FontWeight','normal');
	set(xhand.saveit,'FontWeight','normal');
	set(xhand.panpush,'visible','off');
	set(xhand.varpanedit,'visible','off');
	set(xhand.titpanedit,'visible','off');
	set(xhand.ypanedit,'visible','off');
	set(xhand.ylimedit,'visible','off');
	set(xhand.plotheader,'visible','off');
	set(xhand.ylimheader,'visible','off');
	set(xhand.plot,'visible','off');
	set(xhand.oprand,'visible','off');
	set(xhand.checkbox,'visible','off');
	
	set(xhand.savevarlist,'visible','off');
	set(xhand.savevarname,'visible','off');
	set(xhand.addvar,'visible','off');
	set(xhand.addnewvar,'visible','off');
	set(xhand.remvar,'visible','off');
	set(xhand.rename,'visible','off');
	set(xhand.save,'visible','off');
	set(xhand.options,'visible','off')
	set(xhand.ana,'visible','off','string','');
	set(xhand.anapush,'visible','off');
	set(xhand.anatext,'visible','off');
	set(xhand.dof,'visible','off');
	set(xhand.run,'visible','off','string','RUN');
	set(xhand.graphit,'value',1);
	set(xhand.saveit,'value',1);

	[ymd,hms]=getgregorian(min(jtime{xnum.file}));
	set(xhand.zoomymdfrom,'visible','on','string',ymd);
	set(xhand.zoomhmsfrom,'visible','on','string',hms);
	[ymd,hms]=getgregorian(max(jtime{xnum.file}));
	set(xhand.zoomymdto,'visible','on','string',ymd);
	set(xhand.zoomhmsto,'visible','on','string',hms);
	set(xhand.zoomslidefrom,'visible','on','enable','off','value',0);
	set(xhand.zoomslideto,'visible','on','enable','off','value',1);
	initialplot=1;

	if fdomain
		set(xhand.run,'callback','analyses(''run_fdomain'')');
	else
		set(xhand.run,'callback','analyses(''run'')');
	end;
	method=get(xhand.analyzeit,'value') - 1;
	
	if fdomain
		opstr={'biased','unbiased','coeff','none'};
		switch method
		
		case 1
			set(xhand.anatext,'visible','on',...
				'Position',[10 172 100 95].*ratioxy, ...
				'String',{'NFFT ',' ','NOVERLAP ',' ','Fs ',' ','Pr '});
			set(xhand.anapush(1),'visible','on',...
				'position',[40 275 70 20].*ratioxy,'string','Variables >>');
			set(xhand.ana(1),'visible','on',...
				'Position',[115 275 120 20].*ratioxy);
			for i=2:5
				set(xhand.ana(i),'visible','on',...
					'Position',[115 275-(i-1)*25 50 20].*ratioxy);
			end;
			set(xhand.ana(4),'string',...
				['1/' get(xhand.dt,'string')]);
			set(xhand.run,'visible','on','position',[170 175 30 20].*ratioxy);

		case 2
			set(xhand.anapush(1),'visible','on',...
				'Position',[90 225 70 20].*ratioxy,...
				'string','Variables >>');
			for i=1:2
				set(xhand.ana(i),'visible','on',...
					'Position',[165 225-(i-1)*25 100 20].*ratioxy);
			end;
			callthis=['if(get(gco,''value'')==1),',...
						'set(get(gco,''UserData''),''value'',0),',...
						'else,',...
						'set(gco,''value'',1),',...
						'end'];
			set(xhand.anatext,'visible','on',...
				'Position',[10 195 150 20].*ratioxy, ...
				'String','MaxLags (sec) ');
			for i=1:4
				set(xhand.options(i),'visible','on','value',0,...
					'string',opstr{i},...
					'position',[10+(i-1)*65 175 60 20].*ratioxy);
			end;
			set(xhand.options,'callback',callthis);	
			set(xhand.run,'visible','on','position',[270 175 30 20].*ratioxy);
			
		case 3
			for i=1:2
				set(xhand.ana(i),'visible','on',...
					'Position',[165 225-(i-1)*25 100 20].*ratioxy);
			end;
			set(xhand.anapush(1),'visible','on','Position',[90 225 70 20].*ratioxy);
			callthis=['if(get(gco,''value'')==1),',...
						'set(get(gco,''UserData''),''value'',0),',...
						'else,',...
						'set(gco,''value'',1),',...
						'end'];
			set(xhand.anapush(1),'string','Variables >>');
			set(xhand.anatext,'visible','on',...
				'Position',[10 195 150 20].*ratioxy, ...
				'String','MaxLags (sec) ');
			for i=1:4
				set(xhand.options(i),'visible','on','value',0,...
					'string',opstr{i},...
					'position',[10+(i-1)*65 175 60 20].*ratioxy);
			end;
			set(xhand.options,'callback',callthis);	
			set(xhand.run,'visible','on','position',[270 175 30 20].*ratioxy);
			
		case 4
			set(xhand.anapush(1),'visible','on',...
				'Position',[20 175 70 20].*ratioxy,...
				'string','Variables >>');
			set(xhand.ana(1),'visible','on', ...
				'Position',[95 175 100 20].*ratioxy);
			set(xhand.run,'visible','on','position',[200 175 30 20].*ratioxy);
			
		end; %switch
	end; % if fdomain

	if tdomain
		switch method
		
		case 1
			set(xhand.anatext,'visible','on',...
				'Position',[40 195 100 12].*ratioxy, ...
				'string','Enter formula here:');
			set(xhand.ana(1),'visible','on',...
				'Position',[40 175 200 20].*ratioxy);
			set(xhand.run,'visible','on','position',[245 175 30 20].*ratioxy);
				
		case {2, 13}
			set(xhand.anapush(1),'visible','on',...
				'Position',[90 250 70 20].*ratioxy,...
				'string','Variables >>');
			for i=1:4
				if i>1 len=50; else len=100;end;
				set(xhand.ana(i),'visible','on',...
					'Position',[165 250-(i-1)*25 len 20].*ratioxy);
			end;
			if method==2	
				labstr={'Sampling Freq (cycle/day): ',' ',...
					'Cut-off Freq (cycle/day): ',' ','Subsample Interval: '};
			elseif method==13
				defaultstr={'','2','48','100'};
				for i=2:4
					set(xhand.ana(i),'string',defaultstr{i});	
				end;
				labstr={'Max gap width (Linear fill) ',' ',...
					'Max gap width (Spectral fill) ',' ','Max # of gaps '};
			end;		
			set(xhand.anatext,'visible','on',...
				'Position',[10 170 150 70].*ratioxy, ...
				'String',labstr);
			set(xhand.run,'visible','on','position',[220 175 30 20].*ratioxy);
			
		case 3
			set(xhand.anapush(1),'visible','on',...
				'Position',[90 200 70 20].*ratioxy,...
				'string','Variables >>');
			for i=1:2
				if i>1 len=50; else len=100;end;
				set(xhand.ana(i),'visible','on',...
					'Position',[165 200-(i-1)*25 len 20].*ratioxy);
			end;
			set(xhand.anatext,'visible','on',...
				'Position',[10 170 150 20].*ratioxy, ...
				'String','Subsample Interval: ');
			set(xhand.run,'visible','on','position',[220 175 30 20].*ratioxy);
			
		case {4, 12}
			for i=1:3
				if i>2 & method==4 
					len=50; 
				else 
					len=100;
				end;
				set(xhand.ana(i),'visible','on',...
					'Position',[95 225-(i-1)*25 len 20].*ratioxy);
			end;
			if method==4	
				str1='E-W(U) >>';
				str2='N-S(V) >>';
				set(xhand.anatext,'visible','on',...
					'Position',[20 175 70 20].*ratioxy, ...
					'String',{'ANGLE(deg): '});
				set(xhand.dof,'visible','on',...
					'string','**NOTE** Positive angle = counterclockwise',...
					'Position',[10 165 380 10].*ratioxy);
			elseif method==12
				str1='Wind speed >>';
				str2='Wind dir. >> ';
				set(xhand.anapush(3),'visible','on',...
					'Position',[20 175 70 20].*ratioxy, ...
					'String','Sensor ht(M)');
			end;
		
			
			set(xhand.anapush(1),'visible','on',...
				'Position',[20 225 70 20].*ratioxy,...
				'string',str1);
			set(xhand.anapush(2),'visible','on',...
				'Position',[20 200 70 20].*ratioxy,...
				'string',str2);
			set(xhand.run,'visible','on','position',[100+len 175 30 20].*ratioxy);
			
		case {5, 6, 7, 8, 11, 14}	
			if method==5 | method==11
				str1='E-W(U) >>';
				str2='N-S(V) >>';
				len=100;
			elseif method==6
				str1='SPEED >>';
				str2='DIRECTION >>';
				len=100;
			elseif method==7
				str1='VARIABLES >>';
				str2='AVG LENGTH: ';
				len=50;
			elseif method==8
				str1='VARIABLES >>';
				str2='HOP LENGTH: ';
				len=50;
			elseif method==14
				str1='VARIABLES >>';
				str2='Deg. of Freedom';
				set(xhand.ana(2),'string','Unknown');
				set(xhand.dof,'visible','on',...
					'string','If DOF unknown, click RUN and select an autocorr. scale with the mouse!',...
					'Position',[10 165 380 10].*ratioxy);
				len=50;
			end;
			set(xhand.anapush(1),'visible','on',...
				'Position',[20 200 70 20].*ratioxy,...
				'string',str1);
			set(xhand.ana(1),'visible','on', ...
				'Position',[95 200 100 20].*ratioxy);
			set(xhand.ana(2),'visible','on', ...
				'Position',[95 175 len 20].*ratioxy);
			set(xhand.run,'visible','on','position',[100+len 175 30 20].*ratioxy);
			if method==11
				set(xhand.checkbox,'visible','on','string','Plot (u,v)');
			end;
			if method<=6 | method==11
				set(xhand.anapush(2),'visible','on',...
					'Position',[20 175 70 20].*ratioxy,...
					'string',str2);
			else
				set(xhand.anatext,'visible','on',...
					'Position',[10 170 80 20].*ratioxy, ...
					'String',str2);
			end;
				
		case {9, 15} % Merge and tidal analysis	
			if method==9
				set(xhand.varlist,'Backgroundcolor',[1 .9 .8]);
				set(xhand.newvarlist,'enable','off','Backgroundcolor',[1 1 1]);
				anastr={'File numbers ',' ','Tstart [y m d h m s] ',' ',...
					'Tstop [y m d h m s] ',' ','Time interval (sec) '};
				set(xhand.ana(2),'callback','analyses(''startstop'')');
			elseif method==15
				anastr={'dT (hr) ',' ','Tstart [y m d h] ',' ',...
					'Latitude(deg) ',' ','Rayleigh criterion '};
				set(xhand.dof,'visible','on',...
					'string','The error ''OUT OF MEMORY'' may occur if time-series is too long!',...
					'Position',[40 165 380 10].*ratioxy);
			end;		
			set(xhand.anatext,'visible','on',...
				'Position',[10 175 100 95].*ratioxy, ...
				'String',anastr);
			set(xhand.anapush(1),'visible','on',...
				'position',[40 275 70 20].*ratioxy,'string','Variables >>');
			for i=[1 3 4]
				set(xhand.ana(i),'visible','on',...
					'Position',[115 275-(i-1)*25 120 20].*ratioxy);
			end;
			for i=[2 5]
				set(xhand.ana(i),'visible','on',...
					'Position',[115 275-(i-1)*25 50 20].*ratioxy);
			end;
			set(xhand.run,'visible','on','position',[170 175 30 20].*ratioxy);
			
		case 10 % inter-file calculation
			set(xhand.varlist,'Backgroundcolor',[1 .9 .8]);
			set(xhand.newvarlist,'enable','off','Backgroundcolor',[1 1 1]);
			set(xhand.anatext,'visible','on','position',[50 220 300 20].*ratioxy,...
			'string','NewVar         Coef              Var1          Coef              Var2      ');
			for i=[1 3 5]
				set(xhand.ana(i),'visible','on',...
					'Position',[50+(i-1)*60 200 60 20].*ratioxy);
			end;
			for i=[2 4]
				set(xhand.ana(i),'visible','on',...
					'Position',[10+i*60 200 20 20].*ratioxy,'string','1');
			end;
			set(xhand.anapush(3),'visible','on',...
				'position',[170 178 60 20].*ratioxy,'string','^ Var1 ^');
			set(xhand.anapush(5),'visible','on',...
				'position',[290 178 60 20].*ratioxy,'string','^ Var2 ^');
			for i=1:4
				dum=[115 155 235 275];
				dumstr={'=','x','+','x'};
				set(xhand.oprand(i),'visible','on',...
					'position',[dum(i) 200 10 20].*ratioxy,'string',dumstr{i});
			end;
			set(xhand.run,'visible','on','position',[355 200 30 20].*ratioxy);
		end;%switch method
	end; % if tdomain
	
	whichfile=zeros(6,6);
	whichnew.ana=[];

		
case 'sliderfrom'
	slider('from');

case 'sliderto'
	slider('to');
	
case 'ymdhmsfrom'
	ymdhms('from');
	
case 'ymdhmsto'
	ymdhms('to');
	
case 'click'
	onoff={'addvar','addnewvar','remvar'};
	for i=1:length(onoff),
		eval(['set(xhand.' onoff{i} ',''enable'',''off'');']);
	end;

case 'figClose'
	if gcf==xhand.figT
		initialplot=1;
	end;
	clf;
	
case 'quit'
	selection = questdlg('Close CMG Tool?',...
	                   'Precaution',...
	                   'Yes','No','Yes');
	switch selection,
	case 'No'
		return
	case 'Yes',
		delete(xhand.figT);
		delete(xhand.figF);
		delete(get(0,'CurrentFigure'));
		ncclose;
% 		close all;
%  		clear all;
	end;
end;

return

function slider(sufix)
%Dealing with slider movements

global xnum xsave timebase whichfile xslide xhand jtime

if ~any(whichfile)
	xdata=jtime{xnum.file};
else
% 	test=max(whichfile);
% 	indx=find(test>0);
% 	test=test(indx);
% 	if any(diff(test))
% 		xdata=timebase;
% 	else
% 		xdata=jtime{test(1)};
% 	end;
	xdata=timebase;
end;
eval(['xslide.' sufix '=get(xhand.zoomslide' sufix ',''value'');']);
eval(['dummy=min(xdata)+xslide.' sufix ' * (max(xdata) - min(xdata));']);
eval(['xslide.' sufix '=dummy;']);
[ymdstr,hmsstr]=getgregorian(dummy);
eval(['set(xhand.zoomymd' sufix ',''string'',ymdstr);']);
eval(['set(xhand.zoomhms' sufix ',''string'',hmsstr);']);
for i=1:4
	set(xhand.ylimedit(i),'string', '' );
end;

return;


function ymdhms(sufix)
%Dealing with direct input of times

global xnum xsave timebase whichfile xslide xhand jtime

msg_ymd={'When time is entered manually, use the correct format:',...
	'Year-Month-Day','       and','Hour:Minute:Second'};
if ~any(whichfile)
	xdata=jtime{xnum.file};
else
% 	test=max(whichfile);
% 	indx=find(test>0);
% 	test=test(indx);
% 	if any(diff(test))
% 		xdata=timebase;
% 	else
% 		xdata=jtime{test(1)};
% 	end;
	xdata=timebase;
end;
eval(['ymdstr=get(xhand.zoomymd' sufix ',''string'');']);
if isempty(ymdstr)
	errordlg(msg_ymd,'Error');
	return;
end;
eval(['hmsstr=get(xhand.zoomhms' sufix ',''string'');']);
try
	for i=1:3
		[tt,ymdstr]=strtok(ymdstr,' -');
		ymd(i)=str2num(tt);
	end;
	if isempty(hmsstr)
		hms(1:3)=0;
	else
		for i=1:3
			[tt,hmsstr]=strtok(hmsstr,' :');
			hms(i)=str2num(tt);
		end;
	end;
catch
	errordlg(msg_ymd,'Error');
	return;
end;		
eval(['xslide.' sufix '=julian([ymd hms]);']);
eval(['testy=xslide.' sufix ';']);
if testy>max(xdata) | testy<min(xdata)
	errordlg('Time input out of range.','Error');
	return;
end;
temp=(testy-min(xdata))/(max(xdata)-min(xdata));
eval(['set(xhand.zoomslide' sufix ',''value'',temp);']);
[ymdstr,hmsstr]=getgregorian(testy);
eval(['set(xhand.zoomymd' sufix ',''string'',ymdstr);']);
eval(['set(xhand.zoomhms' sufix ',''string'',hmsstr);']);
for i=1:4
	set(xhand.ylimedit(i),'string', '' );
end;


function mydata=dataclean(mydata);
mydata(mydata>1e34)=nan;
mydata(mydata==-999999)=nan; %for ADCP file (_FillValue)
mydata(mydata==999999)=nan;
return;
