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

  time = 1;

%  str = [' ',titleStr, ' ',myDate];
  str = [' ',myDate];
  PaperPosn = [.5 .5 3.75 2.5];
  FigurePosn = [5 30 815 760];
  position = [5 30 815 760];

  if exist( 'sciFig1' ) ~= 1
      sciFig1 = figure;
  else
      figure( sciFig1 );
  end
%
%
  dataCTD;
  dataCTD2;
  flow2 = 9;
  
  nstrips = 3;

  set( sciFig1, 'name', 'C T D', ...
       'DeleteFcn', 'clear sciFig1' );
  set(gcf,'position',position)
  clf
  time1=ctdDriverctdlog(:,1)-ctdDriverctdlog(1,1);
  time2=seabirdctdlog(:,1)  -seabirdctdlog(1,1);
  subplot(nstrips,1,1)
    plot(time1,  ctdDriverctdlog(:,conductivity))
    line( time2,   seabirdctdlog(:,conductivity),'color','r')
    ylabel('Conductitity')
    grid on
    zoom on
%    set(gca,'Ylim',[2.62 2.66])

    subplot(nstrips,1,2)
    plot(time1,  ctdDriverctdlog(:,temperature))
    line(  time2,  seabirdctdlog(:,temperature),'color','r')
    ylabel('Celcius')
    title(['Temperature',str]);
    grid on
    zoom on

    subplot(nstrips,1,3)
    plot(time1,  ctdDriverctdlog(:,flow2))
    line( time2,   seabirdctdlog(:,flow2),'color','r')
    ylabel('Flow')
    xlabel('Time, Seconds');
    title(['Salinity',str]);
    grid on
    zoom on

    return

  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(mtime, [FLOW1 FLOW2])
    ylabel('Flow')
    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,Meters')
    xlabel('Centigrade')
    title(['Temp v.s. Depth, Red is Descending',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, Red is Descending',str]);
    grid on
    zoom on
    set(gca,'Xlim',[33 35])



  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',[-2 2])
    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

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


  if( exist('ips4log')~=1 ) 
    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 ),
    time = dataNav(:,1);
    indx = find(dataDvl(:,dvlBotVelStat) == 0);
    dvlRangeInterp = interp1(dataDvl(indx,1),dataDvl(indx,dvlRange),time);
    dvlAlt = dvlRangeInterp .* cos(dataNav(:,mPhi)) .* cos(dataNav(:,mTheta));
    [xx,yy]  = stairs( time, dataNav(:,mDepth)+dvlAlt );
    line( xx, yy, 'color','c')
    legend('IPS Depth','IPS Raw Draft','Parosci Depth','DVL Range')
  end
  

  return

