%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% PURPOSE: Plot Science Data
% AUTHOR:  Initial; rsm
% DATE:    01/10/14
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Units and Fundamental Constants:
  units
  hardflag=0;

%  str = [' ',titleStr, ' ',myDate];
  str = [' ',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('Meters')
    title(['Depth',str]);
    set(gca,'Ydir','reverse')
    grid on
    zoom on

    subplot(nstrips,1,2)
    plot(mtime, [T1 T2])
    ylabel('Celsius')
    title(['Temperature',str]);
    grid on
    zoom on

    subplot(nstrips,1,3)
    plot(mtime, [S1 S2])
    ylabel('psu')
    xlabel('Time, Seconds');
    title(['Salinity',str]);
    grid on
    zoom on
    set(gca,'Ylim',[33 35])

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

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

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

    subplot(nstrips,1,2)
    plot(c1time, flw1, c2time, flw2)
    ylabel('Flow, L/min')
    title(['Flow',str]);
    grid on
    zoom on

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


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

    deriv = [0;diff(depthnav)];
    idx = find(deriv > 0);
    subplot(1,2,1)
    plot([T1 T2], depthnav,'.');
    line( T2, depthnav,'linestyle','.','color','cyan');
    line([T1(idx) T2(idx)], depthnav(idx),'linestyle','.','color','red');
    line(T2(idx), depthnav(idx),'linestyle','.','color','magenta');
    set(gca,'Ydir','reverse')
    ylabel('Depth, m, 1: blue up, red dn; 2: cyan up, magnt dn')
    xlabel('Celsius')
    title(['Temp v.s. Depth',str]);
    grid on
    zoom on

    subplot(1,2,2)
    plot([S1 S2], depthnav,'.')
    line( S2, depthnav,'linestyle','.','color','cyan');
    line([S1(idx) S2(idx)], depthnav(idx),'linestyle','.','color','red');
    line(S2(idx), depthnav(idx),'linestyle','.','color','magenta');
    set(gca,'Ydir','reverse')
    xlabel('psu')
    ylabel('Depth, Meters');
    title(['Salinity v.s. Depth',str]);
    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)

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

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

    subplot(nstrips,1,2)
    plot(mtime, O2)
    title(['Oxygen',str]);
    ylabel('Oxygen, ml/l')
    set(gca,'Ylim',[6 10])
    grid on
    zoom on

    subplot(nstrips,1,3)
    plot(mtime, OBSbin)
    title(['Flow',str]);
    ylabel('Backscatter Volts')
    xlabel('Time, Seconds');
%    set(gca,'Ylim',[2 5])
    grid on
    zoom on


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

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

   plot(n2,n2depth,'.');
   line(n2(n2idx),n2depth(n2idx), 'linestyle','.','color','red')
   set(gca,'tickdir','out','Ydir','reverse');
   ylabel('Depth (m), Blue down; red up')
   xlabel('Nitrate (uM)')
   title(['Nitrate vs. Depth : ',str])
   grid


  if( exist('ips4log')~=1 | length(ips4log) <= 20 )
    return;
  end

  if exist( 'sciFig5' ) ~= 1
      sciFig5 = figure;
  else
      figure( sciFig5 );
  end
  set( sciFig5, 'name', 'Ice Draft', ...
       'DeleteFcn', 'clear sciFig5' );
  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)=0;
  set(gca,'Ylim',ylimits)
  ylabel('Depth (m)')
  xlabel('Time, Seconds')
  title(['Ice Draft and Depth v.s. Time ',str])
  line(ipstime(indx),ipsdraftraw(indx),'color','green')
  line(mtime,depthnav,'color','blue')
  %area(ipstime,ipsdraftraw,'facecolor', [0 .75 .85]);
  grid on
  zoom on
  legend('IPS Depth','IPS Raw Draft','Parosci Depth')

  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
  

  return

