function [] = get_soak(crsid,verb,profnum)
% function [] = get_soak(crsid,verb,profnum)
%
% determine surface soak of all profiles and store the results in
% a mat file
%
% input :       crsid                - cruise identifier
%               verb        []       - set to 'verbose' to get more info
%
% version 0.8   last change 11.01.2009

% G.Krahmann, IFM-GEOMAR, June 2007

% change in input/output path setup              AF, Mar 2008    0.1-->0.2  
% converted to function and use of
%   settings file                                GK, Mar 2008    0.2-->0.3
% implemented startscan not be before
%    pump start + 5 seconds                      GK, Mar 2008    0.3-->0.4
% better plot for pressure offset                GK, Apr 2008    0.4-->0.5 
% implemented 'verbose' argument                 GK, Jul 2008    0.5-->0.6
% new settings scheme                            GK, 03.01.2009  0.6-->0.7
% catch deep starting profiles (yoyo)            GK, 11.01.2009  0.7-->0.8

%
% parse input arguments
%
if nargin<2
  verb = '';
end
if nargin<3
  profnum = [];
end


% 
% load settings
%
eval([crsid,'_settings'])


%
% get CTD file numbers
%
if isempty(profnum)
  load(['mat/',crsid,'_sensors'])
  prof = sensor.prof(find(~isnan(sensor.prof)));
else
  load(['mat/',crsid,'_sensors'])
  prof = profnum;
end


%
% load soak mat file, if it already exists
%
if exist(['mat/',crsid,'_startscan.mat'])
  load(['mat/',crsid,'_startscan.mat'])
else
  start.scan = nan*ones(1,999);
  start.press = nan*ones(1,999);
  start.prof = nan*ones(1,999);
end


%
% 'delete' old result for profnum, if we are analyzing a single profile
%
if ~isempty(profnum)
  start.prof(profnum) = nan;
  start.press(profnum) = nan;
  start.scan(profnum) = nan;
end


%
% loop over casts
%
for n=1:length(prof)


  %
  % figure whether it has already been calculated
  %
  do_calc = 1;
  if start.prof(prof(n))==prof(n)
    do_calc = 0;
  end


  %
  % if not, determine the start of the profile
  %
  if do_calc==1
    fname = ['mat/',crsid,'_',int2str0(prof(n),3),allsettings.addname,'.mat'];
    [hdr,data] = read_sbe_cnv2mat(fname);  % only one argument used
                                           % here because startscan
                                           % still has to be determined
    if isfield(data,'p_no_offset')
      p = data.p_no_offset;
    else
      p = data.p;
    end
    pump = data.pumps;
    pumpstart = find(pump==1);
    if ~isempty(pumpstart)
      pumpstart = pumpstart(1)+120;	   % add 5 seconds (120 scans) 
                                           % minimum for flushing
    else
      warning('CTD Pump never started according to data !');
      pumpstart = 1;
    end
    disp(' ')
    disp(fname)
    [start.scan(prof(n)),params] = fitsoak(p,prof(n),verb);
    if pumpstart>start.scan(end)
      disp('Pump was started after determined startscan. Replacing with pump start+5sec.')
      start.scan(prof(n)) = pumpstart;
    end
    start.press(prof(n)) = params(1);
    start.prof(prof(n)) = prof(n);
  end


  %
  % override badly determined startscans with hand entered values
  %
  for m=1:size(allsettings.start_scan,1)
    ind = find( [start.prof]==allsettings.start_scan(m,1));
    if length(ind)==1
      start.scan(ind) = allsettings.start_scan(m,2);
      start.press(ind) = nan;
    end
  end

  %
  % store result
  %
  save6(['mat/',crsid,'_startscan'],'start')

end


%
% plot start pressure
%
uni_ctd_setup = unique([sensor.ctd_setup]);
uni_ctd_setup = uni_ctd_setup( find( ~isnan(uni_ctd_setup) ) );
for n=1:length(uni_ctd_setup)
  figure(2)
  clf
  ind = find([sensor.ctd_setup]==uni_ctd_setup(n));
  v = start.press(ind);
  v = round(v*100)/100;
  vm = nmedian(v);
  ind2 = find( start.press(ind)<10 );
  plot(start.prof(ind(ind2)),start.press(ind(ind2)),'r.','markersize',15)
  hold on
  ax = axis;
  plot(ax(1:2),vm*[1,1],'b')
  xlabel('Profile number')
  ylabel('Pressure on deck [dbar]')
  title(['CTD Setup #',int2str(n),...
	'   Enter ''average'' in settings file as p\_offset'])
  disp(' ')
  disp(['Suggested value for all settings using CTD setup #',...
    int2str(n)])
  disp(['  ctd_settings(',int2str(n),').p_offset = ',num2str(vm)])
  disp(' ')
  disp('   Enter/Check value in settings file ')
  if n<length(uni_ctd_setup)
    pause
  end
end
