%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% PURPOSE: Plot Science Data
% AUTHOR:  Initial; rsm
% DATE:    01/10/14
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Units and Fundamental Constants:
  hardflag=0;
  % set ploterrors = 1 to plot depth error plots 
  ploterrors=1;

%  str = [' ',titleStr, ' ',myDate];
  str = ['; Data Set: ', DirName, '  ', myDate];
  PaperPosn = [.5 .5 3.75 2.5];
  FigurePosn = [5 30 815 760];
  if( exist('position') ~= 1 )  position = [4 30 815 658]; end

  if exist( 'sciFig1' ) ~= 1
      sciFig1 = figure;
  else
      figure( sciFig1 );
  end
  nstrips = 3;

  set( sciFig1, 'name', 'Depth, Temp, Salinity', ...
       'DeleteFcn', 'clear sciFig1' );
  set(gcf,'position',position)
  clf

    subplot(nstrips,1,1)
    plot(mtime, depthnav);
    ylabel('Depth (m)')
    title(['Depth',str]);
    set(gca,'Ydir','reverse')
    xlimits = get(gca,'Xlim');
    grid on
    zoom on

 if exist('T2')==1
    subplot(nstrips,1,2)
    plot(mtime, [T1 T2])
    ylabel('Temperature (deg. C [blue - 1, green - 2])')
    title(['Temperature',str]);
    legend('T1','T2')
    grid on
    zoom on
%    set(gca,'Ylim',[-3 5]);
    set(gca,'Xlim',xlimits);
 else
    subplot(nstrips,1,2)
    plot(mtime, [T1])
    ylabel('Temperature (deg. C)')
    title(['Temperature',str]);
    grid on
    zoom on
%    set(gca,'Ylim',[-3 5]);
    set(gca,'Xlim',xlimits);
 end

 if exist('S2')==1
    subplot(nstrips,1,3)
    plot(mtime, [S1 S2])
    ylabel('Salinity (psu [blue - 1, green - 2])')
    xlabel('Time (s)');
    title(['Salinity',str]);
    grid on
    zoom on
%    set(gca,'Ylim',[33 35]);
    set(gca,'Xlim',xlimits);
else
    subplot(nstrips,1,3)
    plot(mtime, [S1])
    ylabel('Salinity (psu)')
    xlabel('Time (s)');
    title(['Salinity',str]);
    grid on
    zoom on
%    set(gca,'Ylim',[33 35]);
    set(gca,'Xlim',xlimits);
end    

    
    
    
  if exist( 'sciFig2' ) ~= 1
      sciFig2 = figure;
  else
      figure( sciFig2 );
  end

  set( sciFig2, 'name', 'Depth, Flow, Salinity Error', ...
       'DeleteFcn', 'clear sciFig2' );
  set(gcf,'position',position)
  clf

    subplot(nstrips,1,1)
    plot(mtime, depthnav);
    ylabel('Depth (m)')
    title(['Depth,',str]);
    set(gca,'Ydir','reverse')
    grid on
    zoom on

if exist('filtflw2')==1
    subplot(nstrips,1,2)
    plot(newt1time, filtflw1, newt2time, filtflw2)
    ylabel('Flow (mL/s [blue - 1, green - 2])')
    title(['Flow',str]);
    set(gca,'Xlim',xlimits);
    grid on
    zoom on
else
    subplot(nstrips,1,2)
    plot(newt1time, filtflw1)
    ylabel('Flow (mL/s)')
    title(['Flow',str]);
    set(gca,'Xlim',xlimits);
    grid on
    zoom on
end

if exist('S2')==1
    subplot(nstrips,1,3)
    plot(mtime, S1-S2)
    title(['Salinity Difference',str]);
    ylabel('Salinity Diff (psu)')
    set(gca,'Ylim',[-.1 .1])
    xlabel('Time (s)');
    set(gca,'Xlim',xlimits);
    grid on
    zoom on
end
    

if exist('T2')==1
  if exist( 'sciFig3' ) ~= 1
      sciFig3 = figure;
  else
      figure( sciFig3 );
  end

  set( sciFig3, 'name', 'Temp, Cond, and Salinity Difference', ...
       'DeleteFcn', 'clear sciFig3' );
  set(gcf,'position',position)
  clf

    subplot(nstrips,1,1)
    plot(mtime, T1-T2)
    title(['Temperature Difference',str]);
    ylabel('Temperature Diff (deg. C)')
    set(gca,'Ylim',[-.01 .01])
    set(gca,'Xlim',xlimits);
    grid on
    zoom on

    subplot(nstrips,1,2)
    plot(mtime, C1-C2)
    title(['Conductivity Difference',str]);
    ylabel('Conductivity Diff (S/m)')
    set(gca,'Ylim',[-.01 .01])
    set(gca,'Xlim',xlimits);
    grid on
    zoom on

    subplot(nstrips,1,3)
    plot(mtime, S1-S2)
    title(['Salinity Difference',str]);
    ylabel('Salinity Diff (psu)')
    set(gca,'Ylim',[-.1 .1])
    xlabel('Time, Seconds');
    set(gca,'Xlim',xlimits);
    grid on
    zoom on
end


  if exist( 'sciFig1a' ) ~= 1
      sciFig1a = figure;
  else
      figure( sciFig1a );
  end
%
%
  set( sciFig1a, 'name', 'Temp and Salinity vs. Depth', ...
       'DeleteFcn', 'clear sciFig1a' );
  set(gcf,'position',position)
  clf

    deriv = [0;diff(depthnav)];
    idx = find(deriv > 0);
    subplot(1,2,1)
    set(gca,'FontSize',12)
    % plot([T1 T2], depthnav,'.');
    plot(T1,depthnav,'.');
% remove from 'hold on' to 'hold off' if you don't want the up and down casts separately colored
    hold on
    % line( T2, depthnav,'Marker','.','color','cyan');
    %line([T1(idx) T2(idx)], depthnav(idx),'Marker','.','color','red'); 
    plot(T1(idx), depthnav(idx),'r.');
    hold off
    % line(T2(idx), depthnav(idx),'Marker','.','color','magenta');
    set(gca,'Ydir','reverse')
    % ylabel('Depth, m, 1: blue up, red dn; 2: cyan up, magnt dn')
    ylabel('Depth (m [blue up, red down])')
    xlabel('Temperature (deg. C)')
    %title(['Temp v.s. Depth',str]);
    title('Temp vs. Depth')
    grid on
    zoom on
%    set(gca,'Xlim',[0 5])

    
    
    
    subplot(1,2,2)
    set(gca,'FontSize',12)
%    plot([S1 S2], depthnav,'.')
    plot(S1,depthnav,'.')
%    line( S2, depthnav,'Marker','.','color','cyan');
% remove from 'hold on' to 'hold off' if you don't want the up and down casts separately colored
     hold on
     plot(S1(idx),depthnav(idx),'r.')
     hold off
%    line([S1(idx) S2(idx)], depthnav(idx),'Marker','.','color','red');
%    line(S2(idx), depthnav(idx),'Marker','.','color','magenta');
    set(gca,'Ydir','reverse')
    xlabel('Salinity (psu)')
    ylabel('Depth (m [blue up, red down])');
    title(['Salinity v.s. Depth',str]);
    %title('Salinity vs. Depth')
    grid on
    zoom on
    psulimits = get(gca,'Xlim');
    % if( psulimits(1) < 33 ) psulimits(1) = 33; end;
    % if( psulimits(2) > 35 ) psulimits(2) = 35; end;
    % if( psulimits(2)-psulimits(1) < 2 ) psulimits(2) = psulimits(1) + 2;end	  
    % set(gca,'Xlim', psulimits)
%    set(gca,'Xlim',[34 36]);

    
    
  if exist( 'sciFig4' ) ~= 1
      sciFig4 = figure;
  else
      figure( sciFig4 );
  end
  set( sciFig4, 'name', 'Depth, Oxygen, OBS', ...
       'DeleteFcn', 'clear sciFig4' );
  set(gcf,'position',position)
  clf

    subplot(nstrips,1,1)
    plot(mtime, depthnav);
    ylabel('Depth (m)')
    title(['Depth',str]);
    set(gca,'Ydir','reverse')
    grid on
    zoom on

    subplot(nstrips,1,2)
    plot(mtime, O2)
    title(['Dissolved Oxygen',str]);
    ylabel('Dissolved Oxygen (mL/L)')
%    set(gca,'Ylim',[6 10])
    set(gca,'Xlim',xlimits);
    grid on
    zoom on

    subplot(nstrips,1,3)
    plot(mtime, OBSbin)
    title(['OBS',str]);
    ylabel('OBS (volts)')
    xlabel('Time (s)');
%    set(gca,'Ylim',[0 0.5]);
    set(gca,'Xlim',xlimits);
    grid on
    zoom on


if exist('n2')==1    
  if exist( 'sciFig5' ) ~= 1
      sciFig5 = figure;
  else
      figure( sciFig5 );
  end
  set( sciFig5, 'name', 'Nitrate v.s. Depth', ...
       'DeleteFcn', 'clear sciFig5' );
  set(gcf,'position',position)
  clf

  n2deriv = [0;diff(n2depth)];
  n2idx   = find(n2deriv > 0);

   plot(n2,n2depth,'b.');
   hold
   plot(n2(n2idx),n2depth(n2idx),'r.');
   hold off
%   line(n2(n2idx),n2depth(n2idx), 'Marker','.','color','red')
   set(gca,'tickdir','out','Ydir','reverse');
   ylabel('Depth (m [blue up; red down]')
   xlabel('Nitrate (uM)')
   title(['Nitrate vs. Depth : ',str])
   grid
end
   
if exist('ipstime')==1
  if exist( 'sciFig6' ) ~= 1
      sciFig6 = figure;
  else
      figure( sciFig6 );
  end
  set( sciFig6, 'name', 'Ice Draft', ...
       'DeleteFcn', 'clear sciFig6' );
  set(gcf,'position',position)
  clf
  
  indx = find(ipsdraftraw < 5 & ipsdraftraw > 0);
  
  plot(ipstime,ipsdepth,'r');
  set(gca,'tickdir','out','Ydir','reverse');
  ylimits=get(gca,'Ylim');
  ylimits(1)=-5;
  set(gca,'Ylim',ylimits)
  ylabel('Depth (m)')
  xlabel('Time (s)')
  title(['Ice Draft and Depth v.s. Time ',str])
%  line(ipstime(indx),ipsdraftraw(indx),'color','green')
  line(mtime,depthnav,'color','blue')
  line(ipstime,ipsdepthauv,'color','cyan')
  line(ipstime,(ipsdepthauv-auvdepthp),'color','magenta')
  %area(ipstime,ipsdraftraw,'facecolor', [0 .75 .85]);
  grid on
  zoom on
%  legend('IPS Depth','IPS Raw Draft','Parosci AUV Depth','IPS AUV Depth','IPS-Paros Depth',4)
  legend('IPS Depth','Parosci AUV Depth','IPS AUV Depth','IPS - Postcalc Paros Depth',4)
end
  
  if( exist('dvlRange')==1 ),
    indx = find(dataDvl(:,dvlBotVelStat) == 0);
    if( length(indx) < 4 ) 
      disp(' Dvl Range Data Invalid ')
    else
      dvlRangeInterp = interp1(dataDvl(indx,1),dataDvl(indx,dvlRange),navtime);
      dvlAlt = dvlRangeInterp .* cos(dataNav(:,mPhi)) .* cos(dataNav(:,mTheta));
      [xx,yy]  = stairs( navtime, dataNav(:,mDepth)-dvlAlt );
      line( xx, yy, 'color','c')
      legend('IPS Depth','IPS Raw Draft','Parosci Depth','DVL Range')
    end
  end
 
  
 % plot depth error issues
  if ploterrors == 1
  
  if exist( 'sciFig7' ) ~= 1
      sciFig7 = figure;
  else
      figure( sciFig7 );
  end
  set( sciFig7, 'name', 'Depth Comparisons', ...
       'DeleteFcn', 'clear sciFig7' );
  set(gcf,'position',position)
  clf
  
  subplot(3,1,1)
  plot(ptime,parosdpth)
  set(gca,'tickdir','out','Ydir','reverse');
  ylabel('Depth (m)')
  title(['AUV depth from Paros',str])
  grid on
  zoom on

  subplot(3,1,2)
  plot(ptime, (parosdpth-navdepthp),'r');
  line(ptime, (parosdpth-parosdpthcalc), 'color','b')
  set(gca,'tickdir','out');
  ylabel('Depth (m)');
  title(['Depth Errors',str])
  legend('Paros - Nav depth','Paros - Post-processed depth')
  grid on
  zoom on
  
if exist('ipstime')==1
  subplot(3,1,3)
  plot(ipstime,(ipsdepthauv-auvdepthp))
  ylabel('Depth (m)');
  xlabel('Time (s)')
  title(['IPS depth - Paros AUV depth',str])
  grid on
  zoom on
end
  
  
 
 
end
  return

