%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% 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)
    tempax = [floor(min(temp1)) ceil(max(temp1))];
    if max(tempax) > 20
      tempax = [floor(min(temp1)) 20];
    end
    if( norm(temp1) == 0 ) tempax = [ 0 1];end
  end
  i = 0;

  
  if exist( 'ctdFig100' ) ~= 1
      ctdFig100 = figure;
  else 
      figure( ctdFig100 ); 
  end
 
  
  set( ctdFig100, 'name', 'CTD1 vs depth data', ...
       'DeleteFcn', 'clear ctdFig100' );
  set(gcf,'position',position)
  clf

   
 % 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)

   depthctd1 = interp1(dataNav(:,m_present_time),dataNav(:,mDepth),time1);
   hl1 = line(salin1,depthctd1,'Color','b','Marker','.');
   set(gca,'XColor','b','YColor','b','YDir','reverse','Xlim',salax)
   xlabel('Salinity 1 **ESTIMATE** (psu)')
   ylabel('Depth (meters)')
   
   ax2 = axes('Position',get(gca,'Position'),'XAxisLocation','top', ...
              'YAxisLocation','right','Color','none','XColor','r','YColor','r', ...
              'XLim',tempax,'YDir','reverse');
   hl2 = line(temp1,depthctd1,'Color','r','LineStyle','.','Parent',ax2);
   xlabel('Temperature 1 (degrees C)')
   ylabel('Depth (meters)')
   grid on
   zoom on
 
   

if (exist('cond2') == 1)
  if exist( 'ctdFig101' ) ~= 1
      ctdFig101 = figure;
  else 
      figure( ctdFig101 ); 
  end
    
  set( ctdFig101, 'name', 'CTD2 vs depth data', ...
       'DeleteFcn', 'clear ctdFig101' );
  set(gcf,'position',position)
  clf

% 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)

   depthctd2 = interp1(dataNav(:,m_present_time),dataNav(:,mDepth),time2);
   hl1a = line(salin2,depthctd2,'Color','b','LineStyle','.');
   set(gca,'XColor','b','YColor','b','YDir','reverse','Xlim',salax);
   xlabel('Salinity 2 **ESTIMATE** (psu)')
   ylabel('Depth (meters)')
   
   ax2a = axes('Position',get(gca,'Position'),'XAxisLocation','top', ...
              'YAxisLocation','right','Color','none','XColor','r','YColor','r', ...
              'Xlim',tempax,'YDir','reverse');
   hl2a = line(temp2,depthctd2,'Color','r','LineStyle','.','Parent',ax2a);
   xlabel('Temperature 2 (degrees C)')
   ylabel('Depth (meters)')
   grid on
   zoom on
    
end
    


  if exist( 'ctdFig102' ) ~= 1
      ctdFig102 = figure;
  else 
      figure( ctdFig102 ); 
  end
    
  set( ctdFig102, 'name', 'O2 vs depth data', ...
       'DeleteFcn', 'clear ctdFig102' );
  set(gcf,'position',position)
  clf
 
   % Get coefficients
   o2serial = 103; 	% oxygen sensor serial number
   [O2_offset, Tcor, Pcor, SOc, BOc] = O2_cal_SBE43(o2serial);
   % if strcmp(option,'control')==1; ounits='mll'; else ounits='umolkg';end
   ounits='mll';
   o2est = ...
       calc_O2_SBE43(oxygen,temp1,salin1,pres1,O2_offset,Tcor,Pcor,SOc,BOc,time1,[0. 0.],ounits);
 
  
  if (exist('oxygen') == 1 )
    plot(o2est,depthctd1,'b.');
    set(gca,'YDir','reverse');
    ylabel('Depth (m)')
    xlabel('Oxygen (mL/L)')
    set(gca,'Xlim',oxyax);
    title(['Dissolved Oxygen **ESTIMATE**',str]);
    grid on
    zoom on
  end
    
    
    

  if ((exist( 'flowsensor1' ) == 1) | (exist('humidityCol') == 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('humidityCol') == 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
  
  
  
