% Interpolates LR data to common time base so it can be plotted against
% each other.
%
% Assumes data has been concatenated by the standards set in
% Concat_LRAUVDataServer
%
% Driver variable:  tlim

% Select time portion:
if ~exist('tlim')
    t0 = tdep(1);
    t1 = max(tdep);
else
    t0 = tlim(1);
    t1 = tlim(2);
end;
% use index...

% Make sure time monotonic, create common time base, exclude data when
% vehicle is on surface.
ct = tdep;
ct = monotonic(ct);
ok = find((~isnan(ct) & (ct > t0) & (ct < t1)) & dep > 1.5);
ct = ct(ok);

% Do the interp thing...first smooth data if downsampling...
if exist('tchl')
    nn = ceil(1.5* length(t470)/length(ct));
    % t650 = monotonic(t650); 
    ok = find(~isnan(t650));
    c650 = interp1(t650(ok), smooth(eco650(ok),nn), ct);
    
    % t470 = monotonic(t470); 
    ok = find(~isnan(t470));
    c470 = interp1(t470(ok), smooth(eco470(ok),nn), ct);
    
    % tchl = monotonic(tchl); 
    ok = find(~isnan(tchl));
    cchl = interp1(tchl(ok), smooth(ecochl(ok),nn), ct);
elseif exist('teco')
    nn = ceil(1.5* length(teco)/length(ct));
    ok = find(~isnan(teco));
    c650 = interp1(teco(ok), smooth(eco650(ok),nn), ct);
    c470 = interp1(teco(ok), smooth(eco470(ok),nn), ct);
    cchl = interp1(teco(ok), smooth(ecochl(ok),nn), ct);
end;

if exist('tdep')
    nn = ceil(1.5* length(tdep)/length(ct));
    % tdep = monotonic(tdep); 
    ok = find(~isnan(tdep));
    cdep = interp1(tdep(ok), smooth(dep(ok), nn), ct);
end;

if exist('tpar')
    nn = ceil(1.5* length(tpar)/length(ct));
    % tpar = monotonic(tpar); 
    ok = find(~isnan(tpar));
    cpar = interp1(tpar(ok), smooth(par(ok), nn), ct);
end;

if exist('tlat')
    nn = ceil(1.5* length(tlat)/length(ct));
    % tlat = monotonic(tlat); 
    ok = find(~isnan(tlat));
    clat = interp1(tlat(ok), smooth(lat(ok),nn), ct);
    
    tlong = monotonic(tlong); ok = find(~isnan(tlong));
    clong = interp1(tlong(ok), smooth(long(ok),nn), ct);
elseif exist('tpos')
    nn = ceil(1.5* length(tpos)/length(ct));
    % tpos = monotonic(tpos); 
    ok = find(~isnan(tpos));
    clat = interp1(tpos(ok), smooth(lat(ok),nn), ct);
    clong = interp1(tpos(ok), smooth(long(ok),nn), ct);
end;

if exist('tsw')
    nn = ceil(1.5* length(tsw)/length(ct));
    % tsw = monotonic(tsw); 
    ok = find(~isnan(tsw));
    
    cdens = interp1(tsw(ok), smooth(swdens(ok),nn), ct);
    csalt = interp1(tsw(ok), smooth(swsalt(ok),nn), ct);
    ctemp = interp1(tsw(ok), smooth(swtemp(ok),nn), ct);
end;

if exist('talt')
    nn = ceil(1.5* length(talt)/length(ct));
    % talt = monotonic(talt); 
    ok = find(~isnan(talt));
    calt = interp1(talt(ok), smooth(alt(ok),nn), ct);
end;

if exist('tnit')
    nn = ceil(1.5* length(tnit)/length(ct));
    % tnit = monotonic(tnit); 
    ok = find(~isnan(tnit));
    cnit = interp1(tnit(ok), smooth(nit(ok),nn), ct);
end;

if exist('o2')
    nn = ceil(1.5* length(to2)/length(ct));
    % to2 = monotonic(to2); 
    ok = find(~isnan(to2));
    co2 = interp1(to2(ok), smooth(o2(ok),nn), ct);
end;

% Compute buoyancy frequency...
nn = length(ct);
depth_derivative = (cdep(1:nn-2) - cdep(3:nn));  % Compute running difference on depth
depth_derivative(abs(depth_derivative) < 0.02) = NaN;  % Make sure we do not divide by zero
derivative = [0 (cdens(1:nn-2) - cdens(3:nn))./(depth_derivative) 0]; % Compute derivative, make sure we have vector same length
derivative(derivative < 0) = 0; % Make sure there are no negative numbers.
cn = (9.8 * derivative ./ cdens).^0.5; % Compute buoyancy frequency (radian/sec)

% Creat a new time variable that is days from mission start
ctjd = ct;
ct = ct - ct(1);

% common = whos('c*'); save('CommonTime.mat', common(1:length(common)).name);