% requires that clean_missionP has already been run, generating the mission file.
clear all
load mission;
time=epochsec - min(epochsec);
dist = (max(epochsec)-min(epochsec))/1000;

%%------------------------
%% Line Plot 
%%------------------------
sax = [min(S) max(S)];
fax = [min(F) max(F)]; Biolumax = [min(Biolum) max(Biolum)]; Bioflowax = [min(Bioflow) max(Bioflow)];
XL = 'mission time';
close all;
figure(7); 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);

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);
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);

subplot(413); plot(time,Biolum,'k'); ylabel('Biolum'); hold on; set(gca,'Ylim',Biolumax); 
set(gca,'box','off','YaxisLocation','left','tickdir','out'); set(gca,'xlim',xl);
P = get(gca,'position'); axes('position',P);
plot(time,Bioflow,'m'); ylabel('Flow)'); set(gca,'Ylim',Bioflowax); 
set(gca,'xlim',xl,'color','none','Yaxislocation','right','Ycolor','m','box','off','tickdir','out');

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');

%%-----------------------------------
%% Gridded plots
%%-----------------------------------
tdist = linspace(0,dist,length(F));
idist = linspace(0,dist,50);
minZ=3; iZ = [minZ:1:max(depth)]';

disp('Gridding...'); meth='linear';
gT = griddata(tdist,depth,T,idist,iZ,meth);
gS = griddata(tdist,depth,S,idist,iZ,meth);
gO = griddata(tdist,depth,Bioflow,idist,iZ,meth);
gP = griddata(tdist,depth,Biolum,idist,iZ,meth);
gF = griddata(tdist,depth,F,idist,iZ,meth);
gB = griddata(tdist,depth,OBS,idist,iZ,meth);

gvars = ['gT';'gS';'gO';'gP';'gF';'gB'];
vars = ['   T   ';'   S   ';'Bioflow';' Biolum';'   F   ';'  OBS  '];
%vars = [' T ';' S ';'Bioflow ';' Biolum';' F ';'OBS'];
vartitl=['    T (' setstr(176) 'C)'; '    Sal   '; '  Bioflow ';'  Biolum  ';'  Fluor   ';'    OBS   '];

figure(2); clf; giffig_wide

ph=.2;
y1=1-.23-ph; y2=y1-ph-.05; y3=y2-ph-.05;
x1=.08; pw=.4; x2=x1+pw+.08;
P1=[x1 y1 pw ph]; P2=[x1 y2 pw ph]; P3=[x1 y3 pw ph];
P4=[x2 y1 pw ph]; P5=[x2 y2 pw ph]; P6=[x2 y3 pw ph];

AX=[0 max(tdist) 0 max(depth)];
[mgv ngv]=size(gvars);
for V=1:mgv;
   eval(['cvar = ' gvars(V,:) ';']) 
   pstr=['axes(' '''' 'position' '''' ',P' int2str(V) ');']; eval(pstr);
	pcolor(idist,iZ,cvar); shading interp; axis(AX);
   cb=colorbar; p=get(cb,'position'); set(cb,'position',p.*[1 1 .2 1]);
   CAX(V,:)=caxis;
   set(gca,'ydir','reverse'); ylabel('Depth (m)');
      if V==3 | V==6
      xlabel('Distance (km)');
   else
      set(gca,'XTicklabel',[])
   end
   title(vartitl(V,:));
   hold on; p=plot(tdist,depth,'k.'); set(p,'markersize',4);
end
   
   frame
   if (exist('missiondata')==2)
   missiondata;
		txt=text(.3,.98,['Mission ID: ' missionID '   ' surveydate ]); 
	end%if
set(txt,'fontweight','bold','fontsize',18);
txt=text(.3,.96,['(uncalibrated)']); set(txt,'fontsize',9)

%%--------------------------------------------------
%% Color Dot plots
%%--------------------------------------------------
figure(3); giffig_wide; cmp=jet(256);
[mv nv]=size(vars);
for V=1:mv;
   eval(['cvar = ' vars(V,:) ';']); 
   cax=CAX(V,:); rng=linspace(cax(1),cax(2),256); 
   pstr=['axes(' '''' 'position' '''' ',P' int2str(V) ');']; eval(pstr);
   for p=1:length(cvar);
      [mink k]=min(abs(rng-cvar(p)));
      g=plot(tdist(p),depth(p),'ko'); hold on;
      set(g,'markerfacecolor',cmp(k,:),'markeredgecolor',cmp(k,:),'markersize',3);
	end   
   set(gca,'ydir','reverse'); ylabel('Depth (m)'); 
   if V==3 | V==6
      xlabel('Distance (km)');
   else
      set(gca,'XTicklabel',[])
   end
   axis(AX);
   title(vartitl(V,:));
end

%%--------------------------------------------------
%% Up versus Down Cast
%%--------------------------------------------------
% Depth is positive
dZdt = [NaN; diff(depth)./diff(time)]; 
yoyo_discriminator = 0.1;	% m/s
going_up=find(dZdt<-1*yoyo_discriminator); 
going_down=find(dZdt>yoyo_discriminator); 

figure(4); giffig_wide; cmp=jet(256);
[mv nv]=size(vars);
for V=1:mv;
   eval(['cvar = ' vars(V,:) ';']); 
   pstr=['subplot(2,3,' int2str(V) ')']; eval(pstr);
   plot(cvar(going_up),depth(going_up),'r.'); hold on;
   plot(cvar(going_down),depth(going_down),'b.');
   set(gca,'ydir','reverse'); ylabel('Depth (m)');
   title(vartitl(V,:));
end

%Track plot
figure(5);
comet3(Xpos,Ypos,-depth);

return
