% plotdat_chl.m - to plot cast data - Chlorophyll and Temperature
% this one for EDDIES 07/05 OC415-2

printit=1;

set(0,'defaultaxesfontname','timesroman')
set(0,'defaultaxesfontsize',12)
% keep list of starting points for CTD downcast
% cast   1    2     3    4    5   6    7  8    9    10   11    12
%       13   14   15 16  17 18  19 20 21 22
st_ind=[1060 1683 4483 3062 2589 817 1238 862 1184 1205 2415 2758 ...
	1702 2459 1 1195 1 554 2242 1 526 1];


mat_path = '/mnt/oc415-2a/oc415_2/ctd/process/';

%cast = input('Enter cast number: ');

mint=18;maxth=30;
minchl=0;maxchl=1.28;
%minsig=23;maxsig=26.5;

if cast <10
  fn = ['oc41520',int2str(cast)];
elseif cast <100
  fn = ['oc4152',int2str(cast)];
end
%fn = ['cast0',int2str(cast)];
%dirstr = ['Down  ';'Up    '];

%for ii = 1:size(dirstr,1),
%  eval(['load ',mat_path,fn,'_',lower(dirstr(ii,:))])
  eval(['load ',mat_path,fn])

  readhdr

  % compute quantities
  theta0=sw_ptmp(s0,t0,press,0);
  sigma0=sw_pden(s0,t0,press,0);
    
  % initial plot of pressure vs time
  if 0
    figure
    plot(scan,press)                    
    set(gca,'ydir','reverse')
    orient landscape
    wysiwyg
    xlabel('Scan')
    ylabel('Pressure (db)')
    %title(['Cast: ',fn,' - ',dirstr(ii,:),' cast'])
    title(['Cast: ',fn])
    ax = axis;
    axis([scan(1) scan(length(scan)) -1 1010])
  end
  % this plots all 3 quantites on one plot
  figure(cast)
  clf
  %hold off
  bb = find(max(press) == press);
  bb = st_ind(cast):bb(1);

  cc=find(press(bb)>0 & s0(bb)>20 & theta0(bb)>5);
  bb=bb(cc);
  
  if 1
    lim600 = 0;
    if lim600
      aa = find(press(bb) >200 & press(bb) <= 600);
    else
      aa = find(press(bb) >0 & press(bb) <= 200);
    end
    myax = 1;
    %miny = 200;
    miny = 0;
    if lim600
      maxy = 600;
    else
      maxy = 200;
    end
  else
    myax=0;
    aa=1:length(bb);
  end
  
  ntics = 10;
  ax1 = axes('Position',[.13 .11 .7750 .775]);
  h1 = line(t0(bb(aa)),press(bb(aa)));
  set(ax1,'ydir','reverse')
  if myax
    axlim = axis;
    axis([axlim(1:2) miny maxy])
  end
  %axis([mint maxt miny maxy])
  grid
  xlabel(['Temperature (',setstr(176),' C) (-)'])
  ylabel('Pressure (db)')
  %title(['Cast: ',fn,' - ',dirstr(ii,:),' cast'])
  %keyboard
  hold on
  ax2 = axes('Position',get(ax1,'Position'),'xaxislocation','top', ...
	     'color','none','ydir','reverse');
%  h2 = line(chl(bb(aa))*.43,press(bb(aa)),'linestyle','--','parent',ax2);  
  h2 = line(chl(bb(aa)),press(bb(aa)),'linestyle','--','parent',ax2);  
  if myax
    axlim = axis;
    axis([axlim(1:2) miny maxy])
  end
  %axis([minchl maxchl miny maxy])
  xlabel('Chlorophyll (uncorrected) ug/l (- - -)')
  %title(['Cast: ',fn,' - ',dirstr(ii,:),' cast ',Lat,' ',Lon,' ', ...
	% date,' ',time])
  title(['Cast: ',fn,'  ',Lat,' ',Lon,' ', ...
	 date,' ',time])
  box on
  xlimits = get(ax1,'XLim');
  xinc = (xlimits(2)-xlimits(1))/ntics;
  set(ax1,'XTick',[xlimits(1):xinc:xlimits(2)])
  x2limits = get(ax2,'XLim');
  xinc = (x2limits(2)-x2limits(1))/ntics;
  set(ax2,'XTick',[x2limits(1):xinc:x2limits(2)])

  if 0
    ax3 = axes('Position',get(ax1,'Position'),'visible','off', ...
	     'color','none','ydir','reverse');
    h3 = line(sigma0(bb(aa))-1000,press(bb(aa)), ...
	    'linestyle','-.','parent',ax3);
    if myax
      axlim = axis;
      axis([axlim(1:2) miny maxy])
    end
    axis([minsig maxsig miny maxy])
    ax3lim = get(ax3,'XLim');
    xinc = (ax3lim(2)-ax3lim(1))/ntics;
    set(ax3,'XTick',[ax3lim(1):xinc:ax3lim(2)])
    axpos = get(ax1,'Position');
    ax4 = axes('position',[axpos(1) .05 axpos(3) .005]);
    set(ax4,'xlim',ax3lim)
    set(ax4,'XTick',[ax3lim(1):xinc:ax3lim(2)],'yticklabel',[])
    txl=xlabel('Sigma (kg/m^3) ( _ . _ . _ )');
    set(txl,'interpreter','none')
  end
  orient tall
  wysiwyg
%end

if printit
  print('-dpng',['oc41520',int2str(cast),'_chl']);
  %print -Poceanpr2
end
