%%------------------------
%% Plot 
%%------------------------
% Set axes limits
depthax = [floor(min(depth1(find(depth1>-999)))) ceil(max(depth1))];

if ((exist('S1') == 1) & (max(S1) < 38))
   sax = [(min(S1(find(S1>=0)))-0.1) (max(S1)+0.1)];
elseif ((exist('S2') == 1) & (max(S2) < 38))
   sax = [(min(S2(find(S2>=0)))-0.1) (max(S2)+0.1)];
else
   sax = [32 36];
end

if ((exist('O2') == 1) & (max(O2) < 20) & (min(O2) >= 0) )
   O2ax = [floor(min(O2(find(O2>=0)))) ceil(max(O2))];
else
   O2ax = [0 8];
end

if ((exist('n2') == 1) & (length(n2time) >= 2) )
   n2ax = [floor(min(n2(find(n2>=0)))) ceil(max(n2))];
end

if ((exist('OBSbin') == 1) & (max(OBSbin) > 0.1) & (max(OBSbin) < 5.0) )
   OBSax = [floor(min(OBSbin(find(OBSbin>=0)))) ceil(max(OBSbin))];
else
   OBSax = [0 5];
end

if ((exist('FLOW1') == 1) & (~isnan(FLOW1)) & (ceil(max(FLOW1)) > min(FLOW1)))
   FLOWax = [floor(min(FLOW1(find(FLOW1>=0)))) (ceil(max(FLOW1))+0.2)];
elseif ((exist('FLOW2') == 1) & (~isnan(FLOW2)) & (ceil(max(FLOW2)) > min(FLOW2)))
   FLOWax = [floor(min(FLOW2(find(FLOW2>=0)))) (ceil(max(FLOW2))+0.2)];
else
   FLOWax = [-0.1 1.5];
end

if ((exist('T1') == 1) & (max(T1) < 25) )
   TEMPax = [(min(T1(find(T1>=0)))-0.2) (max(T1)+0.2)];
elseif ((exist('T2') == 1) & (max(T2) < 25) )
   TEMPax = [(min(T2(find(T2>=0)))-0.2) (max(T2)+0.2)];
else
   TEMPax = [2 20];
end

if (exist('ipsrange') == 1)
   ipsRANGEax = [floor(min(ipsrange(find(ipsrange>0)))-10) ceil(max(ipsrange))+10];
   draftax = [(min(ipsdraft)-0.5) (max(ipsdraft)+0.5)];
   draftrawax = [(mean(ipsdraftraw)-20) (mean(ipsdraft)+20)];
end

titlestr = [' Data file: ',missionID,'  Plotted on ',dateS];
XL = 'Mission time (s)';

% Plot navigation depth and vehicle pitch
% Plot temperature and salinity (from both sensors if the data exists)
if (exist('T1') == 1)
   figure; %giffig_wide
  subplot(211);
   plot(mtime,depthnav,'k');
   ylabel('Depth (m)'); set(gca,'Ydir','reverse');
   xl=get(gca,'xlim'); set(gca,'box','off','YaxisLocation','left','tickdir','out'); 
   title([titlestr]);
   grid
   hold on
   P = get(gca,'position'); axes('position',P);
   plot(mtime,pitch,'c');
   hold on;
   plot(xl,[0 0],'k--');
   set(gca,'YaxisLocation','right','Ycolor','c','box','off','tickdir','out');
   ylabel('Pitch (deg)'); set(gca,'color','none','Ylim',[min(pitch) max(pitch)]);
   set(gca,'xlim',xl);
  subplot(212); 
   plot(mtime,T1,'r');
   set(gca,'ylim',TEMPax);
   ylabel('Temperature 1 (deg C)');
   set(gca,'Ycolor','r','box','off','YaxisLocation','left','tickdir','out');
   grid
   P = get(gca,'position'); axes('position',P);
   plot(mtime,S1,'b');
   set(gca,'YaxisLocation','right','Ycolor','b','box','off');
   set(gca,'Ylim',sax); ylabel('Salinity 1 (psu)'); set(gca,'color','none','tickdir','out');
   set(gca,'xlim',xl);  
end
    
if (exist('T2') == 1)
   figure; %giffig_wide
  subplot(211);
   plot(mtime,depthnav,'k');
   ylabel('Depth (m)'); set(gca,'Ydir','reverse');
   xl=get(gca,'xlim'); set(gca,'box','off','YaxisLocation','left','tickdir','out'); 
   title([titlestr]);
   grid
   hold on
   P = get(gca,'position'); axes('position',P);
   plot(mtime,pitch,'c');
   hold on;
   plot(xl,[0 0],'k--');
   set(gca,'YaxisLocation','right','Ycolor','c','box','off','tickdir','out');
   ylabel('Pitch (deg)'); set(gca,'color','none','Ylim',[min(pitch) max(pitch)]);
   set(gca,'xlim',xl);
  subplot(212); 
   plot(mtime,T2,'r');
   set(gca,'ylim',TEMPax);
   ylabel('Temperature 2 (deg C)');
   set(gca,'Ycolor','r','box','off','YaxisLocation','left','tickdir','out');
   grid
   P = get(gca,'position'); axes('position',P);
   plot(mtime,S2,'g');
   set(gca,'YaxisLocation','right','Ycolor','g','box','off');
   set(gca,'Ylim',sax); ylabel('Salinity 2 (psu)'); set(gca,'color','none','tickdir','out');
   set(gca,'xlim',xl);
end

figure; %giffig_wide
% Plot oxygen and optical backscatter (if data exists)
if ((exist('O2') == 1) & (exist('OBSbin') == 1))
   subplot(311);
   plot(mtime,O2,'b'); set(gca,'ylim',O2ax);
   ylabel('Oxygen (ml/l)'); 
   set(gca,'Ycolor','b','box','off','YaxisLocation','left','tickdir','out');
   title([titlestr]);
   grid
   P = get(gca,'position'); axes('position',P);
   plot(mtime,OBSbin,'g');
   set(gca,'YaxisLocation','right','Ycolor','g','box','off');
   set(gca,'Ylim',OBSax); set(gca,'color','none','tickdir','out');
   ylabel('OBS (volts)'); 
   set(gca,'xlim',xl);
end

% Plot flow from both flow rate sensors (if data exists)
%if ((exist('FLOW1') == 1) & (max(FLOW1)>0))
if ((exist('FLOW1') == 1))
   subplot(312);
   plot(mtime,depth1,'k');
   ylabel('Depth (m)'); set(gca,'Ydir','reverse','Ylim',depthax);
   xl=get(gca,'xlim'); set(gca,'box','off','YaxisLocation','left','tickdir','out'); 
   grid
   P = get(gca,'position'); axes('position',P);
   plot(mtime,FLOW1,'r');
   set(gca,'YaxisLocation','right','Ycolor','r','box','off','tickdir','out');
   set(gca,'Ylim',FLOWax,'color','none'); 
   ylabel('Flow (L/min)'); 
   set(gca,'xlim',xl);
   if (exist('FLOW2') == 1)
      hold on
      plot(mtime,FLOW2,'b');
      legend('1', '2'); 
      hold off
   end
elseif ((exist('FLOW2') == 1) & (max(FLOW2)>0))
   subplot(312);
   plot(mtime,depth2,'k'); ylabel('Depth (m)'); set(gca,'Ydir','reverse','Ylim',depthax);
   xl=get(gca,'xlim'); set(gca,'box','off','YaxisLocation','left','tickdir','out'); 
   grid
   P = get(gca,'position'); axes('position',P);
   plot(mtime,FLOW2,'b');
   set(gca,'YaxisLocation','right','Ycolor','b','box','off','tickdir','out');
   set(gca,'Ylim',FLOWax,'color','none'); 
   ylabel('Flow 2 (L/min)'); 
   set(gca,'xlim',xl);
end    
    

% Plot nitrogen data (if data exists)
if ( exist('n2') == 1 )
   subplot(313);
   plot(n2time,n2,'b'); set(gca,'ylim',n2ax);
   ylabel('Nitrate (uM)')
   set(gca,'Ycolor','b','box','off','YaxisLocation','left','tickdir','out');
   set(gca,'xlim',xl);
   xlabel(XL); 
   grid

   figure;
   plot(n2,n2depth,'+');
   set(gca,'tickdir','out','Ydir','reverse');
   ylabel('Depth (m)')
   xlabel('Nitrate (uM)')
   title(['Nitrate vs. Depth : ',titlestr])
   grid
end

% Plot salinity and temperature vs. depth (from both sensors if data exists) 
figure; clf
P = get(gca,'position'); clf; axes('position',[P(1) P(2) P(3)/2 P(4)]);
if ((exist('T1') == 1) & (exist('T2') == 1))
   plot(T1,depth1,'r');
   hold on
   plot(T2,depth2,'b');
   hold off
   legend('Primary','Secondary',4)
elseif (exist('T2') == 1)
   plot(T2,depth2,'b');
else
   plot(T1,depth1,'r');
end    
ylabel('Depth (m)');
xlabel('Temperature (deg C)');
title([titlestr]);
set(gca,'YaxisLocation','left','tickdir','out','Ydir','reverse','box','off');
set(gca,'xlim',TEMPax);
set(gca,'ylim',depthax);
grid
P = get(gca,'position'); axes('position',P+[P(3) 0 0 0]);
if ((exist('S1') == 1) & (exist('S2') == 1))
   plot(S1,depth1,'r');
   hold on
   plot(S2,depth2,'b');
   hold off
elseif (exist('S2') == 1)
   plot(S2,depth2,'b');
else
   plot(S1,depth1,'r');
end    
set(gca,'YaxisLocation','right','tickdir','out','Ydir','reverse','box','off');
set(gca,'xlim',sax);
set(gca,'ylim',depthax);
xlabel('Salinity (o/oo)');
hold on
plot([sax(1) sax(1)],depthax,'k');
hold off
grid


% Plot Ice Profiler (IPS) data (if data exists)
if ( exist('ipsrange') == 1 )
   figure; clf;
   subplot(211);
   plot(ipstime,ipsdepth,'b');
   ylabel('Ice Profiler Depth (m)');
   set(gca,'Ydir','reverse','Ylim',ipsRANGEax,'Ycolor','b');
   xl=get(gca,'xlim'); set(gca,'box','off','YaxisLocation','left','tickdir','out');
   title([titlestr]);
   grid
   P = get(gca,'position'); axes('position',P);
   plot(ipstime,ipsrange,'r');
   set(gca,'YaxisLocation','right','Ycolor','r','box','off','tickdir','out');
   set(gca,'Ydir','reverse','Ylim',ipsRANGEax,'color','none'); 
   ylabel('IPS Range (m)'); 
   set(gca,'xlim',xl);

   subplot(212);
   plot(ipstime,ipsdraft,'b.'); set(gca,'ylim',draftax);
   ylabel('IPS Draft (m)'); 
   set(gca,'Ycolor','b','box','off','YaxisLocation','left','tickdir','out');
   P = get(gca,'position'); axes('position',P);
   plot(ipstime,ipsdraftraw,'r');
   set(gca,'YaxisLocation','right','Ycolor','r','box','off','Ylim',draftrawax);
   set(gca,'color','none','tickdir','out');
   ylabel('IPS Draft (raw) (m)'); 
   set(gca,'xlim',xl);
   grid
end 


% figure; clf
% i=find(depth1==max(depth1));
% P = get(gca,'position'); clf; axes('position',[P(1) P(2) P(3)/2 P(4)]);
% plot(T1(1:i),depth1(1:i),'r');
% ylabel('Depth (m)');
% xlabel('Temperature (deg C)');
% set(gca,'YaxisLocation','left','tickdir','out','Ydir','reverse','box','off');
% set(gca,'xlim',TEMPax);
% set(gca,'ylim',depthax);
% hold on
% plot(T1(i+1:length(depth1)),depth1(i+1:length(depth1)),'b');
% grid
% legend('Down','Up',4)
% P = get(gca,'position'); axes('position',P+[P(3) 0 0 0]);
% plot(S1(1:i),depth1(1:i),'r');
% set(gca,'YaxisLocation','right','tickdir','out','Ydir','reverse','box','off');
% set(gca,'xlim',sax);
% set(gca,'ylim',depthax);
% xlabel('Salinity (o/oo)');
% hold on
% plot(S1(i+1:length(S1)),depth1(i+1:length(depth1)),'b');
% plot([sax(1) sax(1)],depthax,'k');
% grid
% hold off
