Calspec.m

From Pnb
Revision as of 12:24, 17 March 2011 by Eriks (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
% Title:        CALSPEC.M
% Date:         02/2000
% Author:       EB/SRW
% Input:        list_file:string
% Output:       [returns:dimension]
% Files:        list_file(qcal.txt), calfile.txt
% Assumes:      list_file in local dir with file names and intervals,
%               calibration files are in the local directory,
%               header of ac9.dat files end with 'aquisition binsize',
% Purpose:      calculate spectral average of calibration file
% Calls:        
% Uses:         ac9 calibration
% Example:      calsec('qcal.txt')
% NOTES:        
% 02/2000
% Wetlabs recommends calibrating a,c separately therefore this needs
% to be modified to accomodate ac9.dat files that don't have a or c.
% Need a channel flag and then place error flags in the unused signal.

%function fig=calspec(list_file,autorun,filetype,prop)
function fig=calspec(varargin)
  [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('calspec: command line pairs not correctly formed');
    return
  end
  if isempty(ac9.par.prop),
    msgbox('Select an IOP, rerun calspec');
    return
  end
  if isempty(ac9.par.fluid),
    msgbox('Select a fluid, rerun calspec');
    return
  end
  if ac9.par.fluid, % 1 for water
    if ~isfield(ac9.par,'instr_num'),
      msgbox('Select an AC9, rerun calspec');
      return
    end
    if ~isfield(ac9.par,'Tref'),
      msgbox('Select a factory cal, rerun calspec');
      return
    end
    Tref=ac9.par.Tref;
    if isempty(Tref),
      msgbox('Tref is empty, check factory cal, rerun calspec');
      return
    end
  end
  qcal=qcaled_cb('list_load','list_file',ac9.par.list_file)

  fig=calspecplot(0);
  set(fig,'Userdata',ac9);
  calspecplot(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
for ifile=1:length(fileval), %size(udata.list.fname,1),
  if(fileval(ifile)>0),
    ival=fileval(ifile);
    file=deblank(qcal.list.fname(ifile,:))
    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=read_ac9([file '.dat']);
      %temp=read_ac9(file);
      %eval(['temp=read_ac9('''   shrink(file)  '.dat'');']);
      temp=exciseit(temp,20);
    else
      temp=read_strip([file '.da2']);
      %eval(['temp=readstr('''  shrink(file)  '.da2'');']);
    end;
    minmin=str2num(qcal.list.t1(ifile,:)); % in decimal minutes
    maxmin=str2num(qcal.list.t2(ifile,:));
    if maxmin==0,
      maxmin=temp(end,1);
    end
    time=temp(:,1)/60000;
    range=find(time>minmin & time<maxmin);
    temp2=medfilt1(temp,15);
    if ac9.par.filetype==1
      chan2=temp2(range,2:19);
      ref2=temp2(range,24:41);
      itemp2=temp2(range,20);
    else
      chan2=temp2(range,3:20);
      ref2=temp2(range,24:41);
      itemp2=temp2(range,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
    chan3=mean(chan2);
    specdata=mean(chan2);
    stddata=std(chan2);
    ndata=size(chan2,1);
    udata=get(fig,'Userdata');
    udata.data(ifile).file=file;
    udata.data(ifile).specdata=specdata(I(ichannels));
    udata.data(ifile).specdata;
    udata.data(ifile).nspecdata=udata.data(ifile).specdata - ...
	udata.data(ifile).specdata(6);
    udata.data(ifile).stddata=stddata(I(ichannels));
    udata.data(ifile).inttemp=mean(exciseit(itemp2,0,-1000,1000));
    udata.data(ifile).ndata=ndata*ones(1,9);
    set(fig,'Userdata',udata);
    udata.han=calspecplot(2)
    set(fig,'Userdata',udata);
    calspecplot(3)
    calspecplot(4)
    leg_names =('');
    leg_names=strvcat(leg_names,strrep(udata.data(ifile).file,'_','\_'))
   
    if ~ac9.par.autorun
      disp('press return key or go button for next plot');
      uiwait(fig);
      if isempty(get(findall(0,'Tag','goBut'),'Userdata')),
	break
      end
    end;
  end
end %ifile
figure(fig);
udata.leg_names=leg_names;
a=legend(udata.han,udata.leg_names,-1);
udata.legend=a;
set(fig,'Userdata',udata);
%Query list_file save and write mean value files
ButtonName=questdlg('Save list_file and write mean value files?');
switch ButtonName,
 case 'Yes',
  qcaled_cb('save');
  for ifile=1:size(udata.data,2),
    ispec=[udata.par.wave;
	   udata.data(ifile).specdata;
	   udata.data(ifile).stddata;
 	   udata.data(ifile).ndata];
    save([udata.data(ifile).file '.txt'],'ispec','-ascii')
  end
end

%TODO

%add legends 

calspec