%  This routine makes sure that all the appropriate computations have been
%  done to ensure that CTD data is valid for AUV observations made during
%  the MUSE experiment on the Pt Sur.  This includes computing derivative values
%  and making corrections to the data.
%
%  Derivative values computed include:
%  
%  ctd_depth:  depth interpolated to the ctd time base
%  salt:  salinity
%  dBar:  observed pressure in deciBars
%  dens:  water density in kg/cu m
%  ctd_up:  indices to up-cast data
%  ctd_down:  indices to down-cast data
%
%  Corrections (not made here, but which need to be made) include:
%
%  Night AUV:
%   START - 26 AUG 1200 PDT    (START - JD 239 1200 PDT)
%  	incorrect calibration coeff for temperature & conductivity 
%   START - 27 AUG 1200 PDT    (START - JD 240 1200 PDT)
%     OBS and fluorometer reading switched
%   START - 27 AUG 1200 PDT    (START - JD 240 1200 PDT)
%     magnetic variation set at 0 (should be 15.2)
%   27 AUG 0800 PDT - END      (START - JD 240 1200 PDT)
%     magnetic variation correct
%   START - 28 AUG 1200 PDT:   (START - JD 241 1200 PDT)
%     gain on fluorometer = 30x (1 V/(microg/l))
%   28 AUG 1200 PDT - END      (START - JD 241 1200 PDT)
%     gain on fluorometer = 10x (0.33 V/(microg/l))
%   variable flour renamed fluor
%
%  Day AUV:
%   START - 27 AUG 0800 PDT
%     magnetic variation set at 0 (should be 15.2)
%   27 AUG 0800 PDT - 27 AUG 2000 PDT
%     magnetic variation set at -15.2 (should be 15.2)
%   27 AUG 2000 PDT - END
%     magnetic variation correct
%  
%  J. G. Bellingham
%  August 30, 2000

if (~exist('fluor'))
   fluor = flour;
end;

if (~exist('ctd_mtime'))
   ctd_mtime = ctdlog(:, 1) - ctdlog(1,1);
end;

if (~exist('ctd_depth'))
   % Get depth in correct time-base and compute pressure
   ctd_depth = ctd_depth_prep(dataNav(:, 1), dataNav(:, mDepth), ctdlog(:,1));
   if ~exist('latitude')
      dBar = sw_pres(ctd_depth, 36.8);  % Use 36.8 as latitude....
   else
      dBar = sw_pres(ctd_depth, gps(1, latitude));
   end;
   
   % Compute salinity & density
   salt = sw_salt(10*ctdlog(:,conductivity)/sw_c3515,ctdlog(:,temperature),dBar);
   dens = sw_dens(salt, ctdlog(:,temperature), dBar);
end;

if (~exist('going_up'))
   yoyo_process;
end;

if (~exist('ctd_up'))
   dummy1 = 0 * dataNav(:, 1);
   dummy1(going_up) = 1;
   dummy1(going_down) = -1;
   dummy2 = interp1(dataNav(:, 1),dummy1, ctdlog(:,1), 'nearest');
   ctd_up = find(dummy2 == 1);
   ctd_down = find(dummy2 == -1);
   ctd_yoyo = interp1(dataNav(:, 1),yoyo, ctdlog(:,1), 'nearest');
end;

% Get rid of initial pumped system transients and near-surface stuff
mind = min(ctd_depth(ctd_up(1):ctd_down(length(ctd_down))));
gtd = find(ctd_depth > 10);
ng1 = 1:gtd(1);
ng2 = gtd(length(gtd)):length(ctd_depth);

ctdlog(ng1,temperature) = NaN;
ctdlog(ng1,conductivity) = NaN;
ctdlog(ng1,fluor) = NaN;   
ctdlog(ng1,o2current) = NaN;   
ctdlog(ng1,o2temp) = NaN; 
salt(ng1) = NaN;
dens(ng1) = NaN;
ctdlog(ng1,OBS) = NaN;   

ctdlog(ng2,temperature) = NaN;
ctdlog(ng2,conductivity) = NaN;
ctdlog(ng2,fluor) = NaN;   
ctdlog(ng2,o2current) = NaN;   
ctdlog(ng2,o2temp) = NaN;   
salt(ng2) = NaN;
dens(ng2) = NaN;
ctdlog(ng2,OBS) = NaN;   

clear dummy1, clear dummy2; clear mind; clear gtd; clear ng1; clear ng2;
