clear all;
threeD=1;
load ctd;
time=epochsec-min(epochsec);

%%------------------------
%% Line Plot 
%%------------------------
sax = [min(S) max(S)];
fax = [min(F) max(F)]; pHax = [min(pH) max(pH)]; O2ax = [min(O2) max(O2)];
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);

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,pH,'k'); ylabel('pH'); hold on; set(gca,'Ylim',pHax); 
set(gca,'box','off','YaxisLocation','left','tickdir','out'); set(gca,'xlim',xl);
P = get(gca,'position'); axes('position',P);
plot(time,O2,'m'); ylabel('Oxygen (ml/l)'); set(gca,'Ylim',O2ax); 
set(gca,'xlim',xl,'color','none','Yaxislocation','right','Ycolor','m','box','off','tickdir','out');
% disp([' ']); disp(['Correct for oxygen time offset !!!']);
% disp(['How much difference does use of ambient temperature make?']); disp([' ']);

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=4; 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,O2,idist,iZ,meth);
gP = griddata(tdist,depth,pH,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 ';'O2 ';' pH';' F ';'OBS'];
vartitl=['T (' setstr(176) 'C)'; '  Sal '; '  O_2 ';'  pH  ';' Fluor';'  OBS '];
x2=1;
%% Plot locations
if x2
 % ph=.2; y1=1-.23-ph; y2=y1-ph-.05; y3=y2-ph-.05;
 ph=.24; y1=1-.1-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];
else
 ph=.13; bufr=.02; y1=1-.08-ph; y2=y1-ph-bufr; y3=y2-ph-bufr;
 y4=y3-ph-bufr; y5=y4-ph-bufr; y6=y5-ph-bufr;
 x1=.08; pw=.89;  
 P1=[x1 y1 pw ph]; P2=[x1 y2 pw ph]; P3=[x1 y3 pw ph];
 P4=[x1 y4 pw ph]; P5=[x1 y5 pw ph]; P6=[x1 y6 pw ph];
end

figure(2); clf; giffig_wide
AX=[0 max(tdist) 0 max(depth)];
for V=1:length(gvars);
   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
txt=text(.3,.98,['Mission ID: ' missionID '   ' surveydate ]); 
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);
for V=1:length(vars);
   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);
      [min k]=min(abs(rng-cvar(p)));
      g=plot(tdist(p),depth(p),'ko'); hold on;
      set(g,'markerfacecolor',cmp(k,:),'markeredgecolor',cmp(k,:),'markersize',5);
	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);
for V=1:length(vars);
   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

return
det=input('Detrend pH? ');
if det==1;
   figure(1); clf
   plot(pH); hold on;
   disp(['Input breakpoint']); [x y]=ginput(1);
   pHd=detrend(pH,'linear',round(x));
   plot(pHd,'r');
end