%% clean_mission: to clean AUV mission data.

clear all; 

%% First compute T and S with correct coefficients
disp(['Loading data for salinity computation']);
load dataN; load dataCTD; CTD_recompute
disp(['Calculating T and S with corrected coefficients']);
clear all; load TScorrected;

%%------------------------
%% Define path for review
%%------------------------
% mission = input('Enter mission: ','s'); eval(['load ' mission '/dataCTD']);
load dataCTD;
mtime = ctdlog(:,time); 

%%------------------------
%% Temperature 
%%------------------------
% t = ctdlog(:,temperature);
%%------------------------
%% Fluorometer and OBS 
%%------------------------
obs=ctdlog(:,OBS); f=ctdlog(:,flour);

%%------------------------
%% Spatial 
%%------------------------
%% Get spatial data and vehicle orientation.    Note:
%%  yaw is heading
%%  separate time stamps for [ctd], [lat lon], [roll pitch yaw], [depth]
[lat,lon,gpstime,depth,roll,pitch,yaw,navtime] = get_spatial;
%% Lat and lon fixed at last recorded position before mission submergence
startlat = lat(length(lat)); startlon = lon(length(lon));
%% Interpolate depth to ctdtime
cdepth = interp1(navtime,depth,mtime); clear depth
croll = interp1(navtime,roll,mtime); clear roll;
cpitch = interp1(navtime,pitch,mtime); clear pitch;
cyaw = interp1(navtime,yaw,mtime); clear yaw;
%% Create lat vector for pressure calculation
clat = repmat(mean(lat),length(ctdlog),1);
%% Compute pressure (db) from depth
pres = sw_pres(cdepth,clat);		% [db]

%%------------------------
%% Salinity
%%------------------------
% disp(['Computing Salinity']); [s] = salsp(ctdlog(:,conductivity),t,pres);
%% median filter salinity to remove spikes
% s=medfilt1(s,5);

%%------------------------
%% Fluorometer and OBS 
%%------------------------
obs=ctdlog(:,OBS); f=ctdlog(:,flour);

%%------------------------
%% Biolume
%%------------------------
load biolume; biotime=biolume(:,time);
%% Interpolate to CTD time
biolum=interp1(biotime,biolume(:,biolum),mtime); clear depth
bioflow=interp1(biotime,biolume(:,flow),mtime); 
biotemp=interp1(biotime,biolume(:,temp),mtime); 

%%------------------------
%% Average 
%%------------------------
%% For February AUV missions, ctd horizontal resolution was ~ 3.5 measurements 
%%  per along-track meter, with a temporal resolution of .25 seconds.
%%  This translates to a vehicle speed of ~ 1 m/s
avgint = 2;	% averaging interval, seconds
[T,S,F,OBS,Biolum,Bioflow,time,depth,epochsec,pitch,roll,yaw] = averCTD(t,s,f,obs,biolum,bioflow,mtime,cdepth,cpitch,croll,cyaw,avgint);

%%------------------------
%% Plot 
%%------------------------
% idx=find(finite(s)); sax = [mean(s(idx))-1 mean(s(idx))+1];
idx=find(S>29 & S<35); sax = [min(S(idx)) max(S(idx))];
fax = [min(F) max(F)]; 
XL = 'mission time';

figure(1); clf; giffig

subplot(411); plot(time,depth,'k'); ylabel('depth'); set(gca,'Ydir','reverse');
xl=get(gca,'xlim'); set(gca,'box','off','YaxisLocation','left','tickdir','out'); 
hold on
P = get(gca,'position'); axes('position',P);
plot(time,pitch,'c'); hold on; plot(xl,[0 0],'k--');
set(gca,'YaxisLocation','right','Ycolor','c','box','off','tickdir','out');
ylabel('Pitch'); set(gca,'color','none','Ylim',[min(pitch) max(pitch)]);
set(gca,'xlim',xl,'XTickLabel',[]);

subplot(412); 
plot(time,T,'r'); set(gca,'ylim',[min(T) max(T)]);
ylabel('temperature');
set(gca,'Ycolor','r','box','off','YaxisLocation','left','tickdir','out');
set(gca,'xlim',xl,'XTickLabel',[]);
P = get(gca,'position'); axes('position',P);
plot(time,S,'b'); set(gca,'YaxisLocation','right','Ycolor','b','box','off');
set(gca,'Ylim',sax); 
ylabel('salinity'); set(gca,'color','none','tickdir','out');
set(gca,'xlim',xl,'XTickLabel',[]);

subplot(413); 
plot(time,Biolum,'m'); set(gca,'ylim',[min(biolum) max(biolum)]);
ylabel('biolum');
set(gca,'Ycolor','r','box','off','YaxisLocation','left','tickdir','out');
set(gca,'xlim',xl,'XTickLabel',[]);
P = get(gca,'position'); axes('position',P);
plot(time,Bioflow,'k'); set(gca,'YaxisLocation','right','box','off');
ylabel('biolum flow'); set(gca,'color','none','tickdir','out');
set(gca,'xlim',xl,'XTickLabel',[]);

subplot(414), plot(time,F,'g'); 
set(gca,'YaxisLocation','left','Ycolor','g','box','off','tickdir','out');
set(gca,'Ylim',fax); ylabel('fluorometer (volts)'); 
set(gca,'xlim',xl); hold on;
P = get(gca,'position'); axes('position',P)
plot(time,OBS,'r'); ylabel('OBS');
xlabel(XL); 
set(gca,'box','off','xlim',xl,'color','none','Yaxislocation','right','Ycolor','r','tickdir','out');

%%------------------------
%% Subset 
%%------------------------
 disp(['Select time subset']); [x y]=ginput(2);
 idx = find(time>=x(1) & time<=x(2));
 T=T(idx); S=S(idx); depth=depth(idx); time=time(idx);
 F=F(idx); OBS=OBS(idx); Biolum=Biolum(idx); Bioflow=Bioflow(idx);
 epochsec=epochsec(idx); roll=roll(idx); pitch=pitch(idx); yaw=yaw(idx);
 disp(['Saving to file mission.mat']);
 save mission T S F Biolum Bioflow OBS epochsec depth startlat startlon roll pitch yaw
