Calview.m

From Pnb
Revision as of 12:30, 17 March 2011 by Eriks (talk | contribs) (Created page with '<pre> %cl: calview() | calview('list_file','foo.txt') % running from cl will always read list_file, gui interaction even if % open %check for list_file in command line …')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
%cl:  calview() | calview('list_file','foo.txt')
% running from cl will always read list_file, gui interaction even if
% open

%check for list_file in command line
         %yes: qcaled(list_file), ac9init, calview(list_file,etc.)
	 %no: "select file dialog, restart", qcaled
%gui: CB: list_file=ac9cal.filename.udata.name (must be current)
       %calview(list_file)
       %check for list_file in command line
	 %yes: qcaled(list_file), ac9init, calview(list_file,etc.)
	 %no: "select file dialog, restart", qcaled

%qcaled has to update filename with any changes to filetext
%ac9init gets qcal filename from qcaled, if ok then call calview

%  list~=qcaled.filename.udata.name, open list_file
%include in filename.udata the handle of the corresponding qcaled
%check to see that filename.udata.name are the same in ac9cal and
%  qcaled to make sure that qcaled hasn't been closed or modified
%lock out qcaled when cv,cs,ts are running 
%closing qcaled should clear ac9cal.filename.udata.name

%has to be an active list_file to run calview and saved copy must
%be up to date, make ac9calinit check for this

%what if qcal.txt is modified outside of qcaled?
% $$$ function calview(list_file,autorun,filetype)
% $$$   
% $$$   ac9calinit %get the contents of the list_file
% $$$   
% $$$   calviewinit, set up the figures
% $$$   
% $$$   for ifile,
% $$$     calview_main, read the data and plot; modify; save
% $$$   end
% $$$   
% $$$   qcalsave, query save of new qcal.txt
% $$$   
% $$$   return

  
  % Title:        CALVIEW.M
% Date:         02/2000
% Author:       EB/SRW
% Input:        parfile:string
% Output:       [returns:dimension]
% Files:        list_file(qcal.txt)
% Assumes:      list_file in local dir with file names and intervals,
%               Order of wavelengths in *.dat file
%               a650 a676 a715 c510 c555 c630 a412 a440 a488 
%               c650 c676 c715 a510 a555 a630 c412 c440 c488
% Purpose:      what do I do?
% Calls:        function names
% Uses:         ac9cal_cb
% Example:      calview() | calview('list_file','foo.txt')
% NOTES:        autorun []=all,man;>0=ran,man;0=all,auto
%               Now plots all wavelengths, zoom to see details.
%		Does not modify list_file if run from the command line.
%               User asked to save the modified filetext to qcal.txt
%               at the end program if run from ac9cal_gui.
%		Varargin passed to calview only update ac9.par in calview,
%		  can't pass them on to ac9cal as function arguments,
%		  not in correct form.
%function fig=calview(list_file,autorun,filetype,prop,mode)
function fig=calview(varargin)
% $$$   h_ac9=findall(0,'Tag','ac9calFig');
% $$$   if isempty(h_ac9),
% $$$     % No ac9cal gui, start and read defaults
% $$$   end
% $$$   ac9=ac9cal_cb('par_update');
  [ac9calfig,ac9]=ac9cal;
  %assign command line parameters to local variables
  narg=length(varargin);
  nrow=narg/2;
  if nrow==round(nrow),
    for iarg=1:2:length(varargin),
      eval(['ac9.par.' varargin{iarg} '= varargin{iarg+1};']);
    end
  else
    display('calview: command line pairs not correctly formed');
    return
  end
  %check that all parameters have been defined
  if isempty(ac9.par.prop),
    msgbox('Select an IOP, rerun calview');
    return
  end
  if isempty(ac9.par.fluid),
    msgbox('Select a fluid, rerun calview');
    return
  end
  if ac9.par.fluid, % 1 for water
    if ~isfield(ac9.par,'instr_num'),
      msgbox('Select an AC9, rerun calview');
      return
    end
    if ~isfield(ac9.par,'Tref'),
      msgbox('Select a factory cal, rerun calview');
      return
    end
    Tref=ac9.par.Tref;
    if isempty(Tref),
      msgbox('Tref is empty, check factory cal, rerun calview');
      return
    end
  end

  qcal=qcaled_cb('list_load','list_file',ac9.par.list_file);
  fig=calviewplot(0);
  set(fig,'Userdata',ac9);
  calviewplot(1);
   
  filetext=qcal.FileText.str;
  fileval=qcal.FileText.val;
  channels=ac9.sort_channels;
  I=ac9.I; %index of channels from .dat file is sorted alphabetically
  ichannels=ac9.isort_channels; %index of channels in sorted array
   
  specdata=[];
  stddata=[];
  ndata=[];
  leg_names='';
 for ifile=1:length(fileval), %size(udata(1).list.fname,1),
  if(fileval(ifile)>0),
    ival=fileval(ifile);
    %file=deblank(udata(1).list.fname(ifile,:))
    file=deblank(qcal.list.fname(ifile,:));
    %add some check to make sure a commented line hasn't been chosen
    if filetext(ival,1)=='#',
      display('uncomment this file, save and rerun');
      break;
    end
    set(findobj('Tag','FileText'),'Value',ival);
    if ac9.par.filetype==1
      [temp,head]=read_ac9([file '.dat']);
      %[temp,head]=read_ac9(file);
      %eval(['temp=read_ac9('''   file  '.dat'');']);
      temp=exciseit(temp,20);
      time=temp(:,1)/60000;
      chan=temp(:,2:19);
      itemp=temp(:,20);
      temp2=medfilt1(temp,15);
      time2=temp2(:,1)/60000;
      chan2=temp2(:,2:19);
      itemp2=temp2(:,20);
    else
      temp=read_strip([file '.da2']);      
      %eval(['temp=read_strip('''   file  '.da2'');']);
      time=temp(:,1)/60000;
      chan=temp(:,3:20);
      itemp=temp(:,21);
      temp2=medfilt1(temp,15);
      time2=temp2(:,1)/60000;
      depth2=temp2(:,2);
      chan2=temp2(:,3:20);
      itemp2=temp2(:,21);
    end;
    if ac9.par.fluid,
      %add wetlabs temperature correction
      %get Tcal from a list file
      Tcaldate=qcal.list.date(ifile,:);
      %Tcalfile=['watertemp_' ac9.par.instr_num '.txt'];
      Tcalfile=[ac9.par.instr_num ac9.par.Tcalfile];
      Tcalfile=fullfile(ac9.par.calroot_dir, ac9.par.instr_num, ...
			ac9.par.Tcaldir, Tcalfile);
      qcal.list.tcal=read_text(Tcalfile);
      find( datenum(Tcaldate)==datenum(parse(qcal.list.tcal,1)) );
      Tcal=0;
      if ~isempty(ans),
	Tcal=parse(qcal.list.tcal(ans(1),:),2);
	%add cal check
	if ~strcmp(Tcal,'Reserved');
	  Tcal=str2num(Tcal);
	else
	  msgbox('Enter Tcal into line 5 of the qcal.txt and rerun');
	  close(fig);
	  return
	end
      else
	qcal.list.tcal;
	disp(['Tcaldate is not in this list, check ' Tcalfile]);
	return
      end
      if Tcal>0,
	del_ts=ac9.par.sit*(Tcal-ac9.par.Tref);
	del_ts=[del_ts del_ts];
	for irow=1:size(chan2,1),
	  chan2(irow,:)=chan2(irow,:)-del_ts(I);
	end
      end
    end
    
    %put this data into the figure
    caldata.file=file;
    caldata.par=ac9.par;
    caldata.I=I;
    caldata.channels=channels;
    caldata.ichannels=ichannels;
    caldata.time2=time2;
    caldata.chan2=chan2;
    caldata.itemp2=itemp2;
% $$$     t_lim=[str2num(udata(1).list.t1(ifile,:)) 
% $$$ 	   str2num(udata(1).list.t2(ifile,:)) ];
    t_lim=[str2num(qcal.list.t1(ifile,:)) 
	   str2num(qcal.list.t2(ifile,:)) ];
    caldata.t_lim=t_lim;
    %put the data into the calplot user data
    set(findobj(fig,'Tag','calplot'),'Userdata',caldata);
    
    %put a spectral plot for entire range on same page 
    %fill edit boxes on qcaled
    %get the time fields to pass as t_lim

    %put into calviewplot and get t_lim from fig.udata.t_lim
    calviewplot(2);
    
% $$$     specdata=get(findobj('Tag','specplot'),'Userdata');
% $$$     stddata=get(findobj('Tag','stdplot'),'Userdata');
    newdata=get(findobj(fig,'Tag','specplot'),'Userdata');
    %save to calplot figure userdata
    udata=get(fig,'Userdata');
    %wavelength should be global, get from udata.par.wave 
    %udata(ifile).wave=par.wave;
    %udata.data should be a structure array of the data
    %udata(ifile).file=file;
    %udata(ifile).specdata=newdata.specdata(end,:);
    %udata(ifile).nspecdata=udata(ifile).specdata - ...
    %	udata(ifile).specdata(6);
    %udata(ifile).stddata=newdata.stddata(end,:);
    %udata(ifile).ndata=newdata.ndata(end,:);
    udata.data(ifile).file=file;
    udata.data(ifile).specdata=newdata.specdata(end,:);
    udata.data(ifile).nspecdata=udata.data(ifile).specdata - ...
	udata.data(ifile).specdata(6);
    udata.data(ifile).stddata=newdata.stddata(end,:);
    udata.data(ifile).ndata=newdata.ndata(end,:);
    set(fig,'Userdata',udata);
    %leg_names=strvcat(leg_names,strrep(udata(ifile).file,'_','\_'));
    leg_names=strvcat(leg_names,strrep(udata.data(ifile).file,'_','\_'));
    if ~ac9.par.autorun
        x1_han(1)=findobj(fig,'Tag','calplot');
        ax1 = axis(x1_han)
        ax1 = ax1(1)
      disp('press left mouse and hold to zoom in on an area');
      disp('press left mouse to zoom out');
      %disp('write down beginning and ending minute to use');
      %disp('press return key to continue to next file ');
      disp('press return key to confirm zoom then press "go" to go to next file')
      disp('');
      disp('');
      uiwait(fig)
      % pause
       x2_han(1)=findobj(fig,'Tag','calplot');
        ax2 = axis(x1_han)
        ax2 = ax2(1)
      if ax2 ~= ax1
        figure(1)  
      calviewplot(8)
      
      end
      %uiwait(fig);
      
      if isempty(get(findall(0,'Tag','goBut'),'Userdata')),
	break
      end
    end %ac9.par.autorun
  end
end %ifile
set(fig,'Close','delete(gcf)')

%save list?
%backup old list file

%Query list_file save 

get(findobj('Tag','SaveBut'),'Value')
if get(findobj('Tag','SaveBut'),'Value'),
  ButtonName=questdlg('Save list_file ?');
  switch ButtonName,
   case 'Yes',
    qcaled_cb('save');
  end
end

pooh=1;
while(pooh)

%Problem: file commented out of filetext during calview
%Following: save original list of files, check that files are subset
%of that list.
%Better: ac9cal_update will parse the qcaled_gui into a structure and
%put it into the figure userdata. This becomes locked while program
%is running. Querry save at end if filetext has been changed, save
%flag is up.
% $$$ if ~get(findobj('Tag','SaveBut'),'Value'),
  udata=get(fig,'Userdata');
  %Exclude files that have been commented out of filetext
% $$$   tdata=get(findobj('Tag','FileText'),'Userdata');
% $$$   fval0=tdata.fval;
  %%%%%fval0=qcal.FileText.val
% $$$   ac9cal_init
  %get new fval, not in qcaled.udata
% $$$   tdata=qcaled_cb('parse');
  %%%%%qcal=qcaled_cb('parse');
% $$$   udata=get(findobj('Tag','FileText'),'Userdata');
  %tdata.fval must be a subset of fval0
% $$$   if all(ismember(qcal.FileText.val,fval0)),
% $$$     for i=1:length(fval0),
% $$$       find(fval0(i)==qcal.FileText.val)
% $$$       if isempty(ans),
% $$$ 	udata.data(i)=[];
% $$$ 	leg_names(i,:)=[]
% $$$       end
% $$$     end
% $$$   else
% $$$     %else won't be needed if the uncomment is blocked during program run
% $$$     msgbox(['files added which are not included in calview, ' ...
% $$$ 	    ' rerun calview with new files or run calspec']);
% $$$     break
% $$$   end
  
  %put a replot in to show overlay again after commenting out file
  
  %Move this up to show the spectra first
  
% $$$   %Query to run a modified version of calspec using averages in data
% $$$   ButtonName=questdlg('Show overlay of spectra, aka calspec?');
 % $$$   switch ButtonName,
% $$$    case 'Yes',
% $$$     fig=calspecplot(0);
% $$$     set(fig,'Userdata',udata);
% $$$     calspecplot(1);
% $$$     udata.han=calspecplot(2)
% $$$     set(fig,'Userdata',udata);
% $$$     calspecplot(3)
% $$$     calspecplot(4)
% $$$     udata.leg_names=leg_names;
% $$$     a=legend(udata.han,udata.leg_names,-1);
% $$$     udata.legend=a;
% $$$     set(fig,'Userdata',udata);
% $$$   end

% $$$ end
  %save means last
  
  %Query  write mean value files
  %Only wrote the first one, not using the file to get fval
  if ~ac9.par.spectra,
    meanBut=questdlg('What now?','', ...
		      'Save means and quit', ...
		      'Quit w/o saving means',...
		      'View spectra', ...
		      'View spectra');
  else
    meanBut='View';
  end
  switch meanBut(1:4),
   case 'Save',
    for ifile=1:length(udata.data),
      ispec=[udata.par.wave;
	     udata.data(ifile).specdata;
	     udata.data(ifile).stddata;
	     udata.data(ifile).ndata];
      save([udata.data(ifile).file '.txt'],'ispec','-ascii');
      pooh=0;
    end
   case 'Quit'
    pooh=0;
   otherwise
    fig=calspecplot(0);
    set(fig,'Userdata',udata);
    calspecplot(1);
    udata.han=calspecplot(2)
    set(fig,'Userdata',udata);
    calspecplot(3)
    calspecplot(4)
    udata.leg_names=leg_names;
    a=legend(udata.han,udata.leg_names,-1);
    udata.legend=a;
    set(fig,'Userdata',udata);
    if ~ac9.par.spectra,
% $$$       listBut=questdlg('Edit list?')
% $$$       switch listBut(1:2)
% $$$        case 'Ye'
% $$$ 	h=msgbox('Comment and save list, then press OK');
% $$$ 	set(h,'Delete',' uiresume( findobj(''Tag'',''calspecFig'')) ' );
% $$$ 	uiwait(fig);
% $$$       end
    else
      ac9.par.spectra=0;
    end
  end
end % while pooh
return

%OUTLINE
%Select range of data(finalize),
%   Calc new stats and plot,
%   Makes new entries in filetext via qcaled,
%Push go,
%   Save filetext to tempfile
%   Calc ave from time in list, save to calviewfig.udata(ifile).ave
%      and calviewfig.udata(ifile).name for writing later
%Query filetext save
%   YES:
%      Writes qcal.txt file
%      Writes ave files
%      Calls calspec for comparison
%   NO: 
%      Bails, no further action

%TODO
%Add Pegau temperature correction,
%   needs Tref from wetlabs cal
%   needs T of water during field calibration
%qcaled('ac9 factory cal')
%   parse ac9cal.dat and put into factory.user
%   instrument and date list boxes
%   add inst and date to ac9cal_def like a,c for batching
%change to qcaled to make updated put ac9calinit into qcaled, real
%time update to a structure made available via qcaled.userdata


%calviewplot('ac9_data')
%serial number list box
%cal location box
%cal number, date list box, parse ac9cal.dat
%tref list box
%caltype button A | Q
%
%
%
%in calviewplot




%calview from ac9cal_cb doesn't pick up the iop

%Add calspec plotting to end of calview

%Starting calview, calspec, ts-cal should disables add/remove buttons

%Add tool tips to buttons

%What about putting buttons into menus

%Add zoom/unzoom in menu for each plot

%Std/linreg plot doesn't come up with color symbols

%Dump results and averages, old/new to dialog and querry save

%Editting list should turn on save flag, check this flag before recalculating

%Add reset option to copy filetext.udata -> filetext.string

%Add ndata to data kept, have to change ts_cal input format, need to
%   calculate weighted average

%Command line launch

%uncommenting a file in qcaled after starting the program is bad

%add Tref to calplot somewhere and note of correction

calview