%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% PURPOSE: Plot raw CTD data from dual CTDs
% AUTHOR:  Initial; rsm; nst
% DATE:    99/12/14
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Units and Fundamental Constants:
  units

  if( (exist('ctdlog') ~= 1) | (exist('ctdlog2') ~= 1) ) return; end
  % set variable names
  if (exist('ctdlog') == 1)
      time1 = ctdlog(:,1);
      temp1 = ctdlog(:,ctdTempCol1);
      cond1 = ctdlog(:,ctdCondCol1);
      flowsensor1 = ctdlog(:,flow1Col);
      humidity1 = ctdlog(:,humidityCol);
      obs = ctdlog(:,OBSCol);
      oxygen = ctdlog(:,dissolvedO2Col);
  end
  if (exist('ctdlog2') == 1)
      time2 = ctdlog2(:,1);
      temp2 = ctdlog2(:,ctdTempCol2);
      cond2 = ctdlog2(:,ctdCondCol2);
      flowsensor2 = ctdlog2(:,flow2);
  end
  
  str = [' ',titleStr, ' ',myDate];
  PaperPosn = [.5 .5 3.75 2.5];
  FigurePosn = [5 30 815 760];
  i = 0;
  if exist( 'ctdFig1' ) ~= 1
      ctdFig1 = figure;
  else 
      figure( ctdFig1 ); 
  end
  
  
  if (exist('cond2') == 1)
     if (min(cond1) > min(cond2))
        condmin = (min(cond2))-0.2;
     else
        condmin = (min(cond1))-0.2;
     end
     if (max(cond1) > max(cond2))
        condmax = (max(cond1))+0.2;
     else
        condmax = (max(cond2))+0.2;
     end
     condax = [condmin condmax];
  else
     condax = [(min(cond1)-0.2) (max(cond1)+0.2)];
  end
  if condax(2)>6
      condax(2) = 6;
  end
  
  salax = [32 36];
  flowax = [0 10];
  oxyax = [0 10];
  
  if (exist('temp1') == 1 & norm(temp1) ~= 0)
   tempax = [floor(min(temp1)) ceil(max(temp1))];
   if max(tempax) > 20
       tempax = [floor(min(temp1)) 20];
   end
  end

  
  set( ctdFig1, 'name', 'CTD1 data', ...
       'DeleteFcn', 'clear ctdFig1' );
  set(gcf,'position',position)
  clf

    subplot(4,1,1)
    [xx,yy] = stairs(time1,cond1);
    plot(xx,yy);
    ylabel('S/m')
    title(['Conductivity 1',str]);
    set(gca,'Ylim',condax);
    grid on
    zoom on

    subplot(4,1,2)
    [xx,yy] = stairs(time1,temp1);
    plot(xx,yy);
    ylabel('degrees Celsius')
    title(['Temperature 1',str]);
    if( exist('tempax') == 1 ), set(gca,'Ylim',tempax); end
    grid on
    zoom on
    
 % Calculate approximate saltwater density (in order to calculate delta pressure)
 %   sw_c3515 in mS/cm
   cratio = cond1*10/sw_c3515;
 % Estimate pressure from depth at nominal latitude of MBARI and interpolate to CTD time
   pres1a = sw_pres(dataNav(:,mDepth),44.5);
   pres1 = interp1(dataNav(:,m_present_time),pres1a,time1);
 % Calculate estimate of salinity - NOTE!! DOES NOT TAKE INTO ACCOUNT TIME LAGS OR PRESSURE
 %                                  DIFFERENCES BETWEEN THE LOCATION OF THE PRESSURE SENSOR
 %                                  AND THE SCIENCE SENSORS, ETC ETC ETC
   salin1 = sw_salt(cratio,temp1,pres1); % (psu)

    
    subplot(4,1,3)
    plot(time1,salin1)
    ylabel('psu')
    title(['Salinity 1 ***ESTIMATE***',str])
    set(gca,'Ylim',salax)
    grid on
    zoom on
    

    subplot(4,1,4)
 if ( exist('dataNav') == 1 )
    [xx,yy] = stairs( dataNav(:,m_present_time), dataNav(:,mDepth));
    plot(xx,yy);
 end
    set( gca, 'ydir', 'reverse' );
    ylabel('meters')
    xlabel('Time (s)')
    title(['Depth',str]);
    grid on
    zoom on
   
if (exist('cond2') == 1)
  if exist( 'ctdFig2' ) ~= 1
      ctdFig2 = figure;
  else 
      figure( ctdFig2 ); 
  end
    
  set( ctdFig2, 'name', 'CTD2 data', ...
       'DeleteFcn', 'clear ctdFig2' );
  set(gcf,'position',position)
  clf

    subplot(4,1,1)
    [xx,yy] = stairs(time2,cond2);
    plot(xx,yy);
    ylabel('S/m')
    title(['Conductivity 2',str]);
    set(gca,'Ylim',condax);
    grid on
    zoom on

    subplot(4,1,2)
    [xx,yy] = stairs(time2,temp2);
    plot(xx,yy);
    ylabel('degrees Celsius')
    title(['Temperature 2',str]);
    if( exist('tempax') == 1 ) set(gca,'Ylim',tempax); end
    grid on
    zoom on

% Calculate approximate saltwater density (in order to calculate delta pressure)
 %   sw_c3515 in mS/cm
   cratio = cond2*10/sw_c3515;
 % Estimate pressure from depth at nominal latitude of MBARI and interpolate to CTD time
   pres2a = sw_pres(dataNav(:,mDepth),44.5);
   pres2 = interp1(dataNav(:,m_present_time),pres2a,time2);
 % Calculate estimate of salinity - NOTE!! DOES NOT TAKE INTO ACCOUNT TIME LAGS OR PRESSURE
 %                                  DIFFERENCES BETWEEN THE LOCATION OF THE PRESSURE SENSOR
 %                                  AND THE SCIENCE SENSORS, ETC ETC ETC
   salin2 = sw_salt(cratio,temp2,pres2); % (psu)

    
    subplot(4,1,3)
    plot(time2,salin2)
    ylabel('psu')
    title(['Salinity 1 ***ESTIMATE***',str])
    set(gca,'Ylim',salax)
    grid on
    zoom on
    
    subplot(4,1,4)
 if ( exist('dataNav') == 1 )
    [xx,yy] = stairs( dataNav(:,m_present_time), dataNav(:,mDepth));
    plot(xx,yy);
 end
    set( gca, 'ydir', 'reverse' );
    ylabel('meters')
    xlabel('Time (s)')
    title(['Depth',str]);
    grid on
    zoom on
end
    
    
  if exist( 'ctdFig3' ) ~= 1
      ctdFig3 = figure;
  else 
      figure( ctdFig3 ); 
  end
    
  set( ctdFig3, 'name', 'O2 & OBS data', ...
       'DeleteFcn', 'clear ctdFig3' );
  set(gcf,'position',position)
  clf

  if (exist('oxygen') == 1 )
    subplot(3,1,1)
    [xx,yy] = stairs(time1,oxygen);
    plot(xx,yy);
    ylabel('volts')
    title(['Dissolved Oxygen',str]);
    grid on
    zoom on
  end
    
  if (exist('obs') == 1 )
    subplot(3,1,2)
    [xx,yy] = stairs(time1,obs);
    plot(xx,yy);
    ylabel('volts')
    title(['Optical Backscatter',str]);
    set(gca,'Ylim',oxyax)
    grid on
    zoom on
  end
    
    subplot(3,1,3)
 if ( exist('dataNav') == 1 )
    [xx,yy] = stairs( dataNav(:,m_present_time), dataNav(:,mDepth));
    plot(xx,yy);
 end
    set( gca, 'ydir', 'reverse' );
    ylabel('meters')
    xlabel('Time (s)')
    title(['Depth',str]);
    grid on
    zoom on
    

  if ((exist( 'flowsensor1' ) == 1) | (exist('humidity1') == 1))
    if exist( 'ctdFig4' ) ~= 1
       ctdFig4 = figure;
    else 
       figure( ctdFig4 ); 
    end
    
    set( ctdFig4, 'name', 'Flow Meters & Humidity', ...
       'DeleteFcn', 'clear ctdFig4' );
    set(gcf,'position',position)
    clf

  if (exist('flowsensor1') == 1 )
    subplot(4,1,1)
    [xx,yy] = stairs(time1, flowsensor1);
    plot(xx,yy);
    ylabel('volts')
    title(['Flow 1',str]);
    set(gca,'Ylim',flowax)
    grid on
    zoom on
  end

  if (exist('flowsensor2') == 1 )
    subplot(4,1,2)
    [xx,yy] = stairs(time2, flowsensor2);
    plot(xx,yy);
    ylabel('volts')
    title(['Flow 2',str]);
    set(gca,'Ylim',flowax)
    grid on
    zoom on
  end
  
  if (exist('humidity1') == 1 )
    subplot(4,1,3)
    [xx,yy] = stairs(time1,humidity1);
    plot(xx,yy);
    ylabel('volts')
    title(['Humidity',str]);
    grid on
    zoom on
  end

    subplot(4,1,4)
    if ( exist('dataNav') == 1 )
       [xx,yy] = stairs( dataNav(:,m_present_time), dataNav(:,mDepth));
       plot(xx,yy);
    end
    set( gca, 'ydir', 'reverse' );
    ylabel('meters')
    xlabel('Time (s)')
    title(['Depth',str]);
    grid on
    zoom on
  end
  
  
  
