%
% TO PLOT A SET of CTD "VERTICAL PROFILEs" on the same page 
%
% Jan 1996
%
% Modifications:
%
% Jan 1997: added option for reading jgofs ".dat" files
%           added option for reading 911 ".cnv" files
% Jun 1997: modifications to correct fiddling 
% Jul 1997: added jgofout option to data input formats
% Apr 1999: cleaned up the code


% Set some parameters by hardcoding

%cru=input('Enter cruise name: ');
cru='el04';

%data directory & prefix "d" for downcast
datdir='c:\gbs99\ctd\el9904\d';

% set bio=1 if you have biology
bio=0;

% here's where we set the cast number vector:
castvec=input('Enter the cast number vector: ');

% calculate # of cast
numc=length(castvec);

%  nvar=input('Number of variables including depth?'); 
nvar=3;
disp(['Hardcoded number of variables = ',num2str(nvar)]);

% assign acceptable values
okmin(1)=-1;
okmax(1)=-100;
okmin(2)=1;
okmax(2)=29;
okmin(3)=31;
okmax(3)=37;
okmin(4)=0;
okmax(4)=5;
okmin(5)=24;
okmax(5)=27;
okmin(6)=0;
okmax(6)=5;
okmin(7)=0;
okmax(7)=5;


% set phase of processing to start 
ph=4;
%ph=menu('start where','stripheaders from asc files',...
%                     'stripheaders from prs files',...
%                     'read jgofs files',...
%                     'read 911 cnv files',...
%                     'read jgofout .mat files',...
%                     'read .data file',...
%                     'plot');


% First run a routine to strip the headers
% (Note: stripping headers is no longer necessary
%        in the case of some CTD's like the 911)

if ph==1;
  !asc2mtl.exe
end;

if ph==2;
  !prs2mtl.exe
end;


if ph<=6;
   clf;

% load all cast and keep track of maximums
  for ivar=1:nvar;
   maxv(ivar)=-10000;
   minv(ivar)=10000;
  end;

% read all the data to find limits for each variable
  for i=castvec;

% case of no header files:
    if ph~=4&ph~=3&ph~=5&ph~=6;
      if i<10;zeros='00';elseif i>=1&i<100;zeros='0';else;zeros='';end;
      eval(['load /data1/jgofs/al9306/al06d',zeros,num2str(i),'.mtl;']);
      eval(['d',num2str(i),'=al06d',zeros,num2str(i),';']);
   
% case of jgofs files w/headers
    elseif ph==3;
      if i<10;zeros='00';elseif i>=10&i<100;zeros='0';else;zeros='';end;
      fid=fopen(['/data1/jgofs/',cru,'/sj07d',zeros,num2str(i),'.dat'],'r');
      for kk=1:9;
        fgetl(fid);
      end;
      d=fscanf(fid,'%f',inf);
      [ndr,ndc]=size(d);
      eval(['d',num2str(i),'=(reshape(d,7,ndr/7))'';']);
%     rid of scan number
      eval(['d',num2str(i),'=d',num2str(i),'(:,1:4);']);
      clear d;

% case of 911 cnv files w/headers
    elseif ph==4;
      if i<10;sta='00';else;sta='0';end;
      fid=fopen([datdir,cru,sta,num2str(i),'.cnv'],'r');
      str1='*END*';
      mark=fgetl(fid);
      [mark1,rem]=strtok(mark);
      while strcmp(str1,mark1)~=1
         mark=fgetl(fid);
         [mark1,rem]=strtok(mark);
      end
      mark=fgetl(fid);
      mark=fgetl(fid);
      outfid=fopen('txtfile','w');
      while mark~=-1;
        if length(mark)>0;
           fprintf(outfid,'%s\n',mark);
        end;
        mark=fgetl(fid);        
        mark=fgetl(fid);
      end
      fclose(outfid);
      fclose(fid);
      load txtfile -ascii;
      dd=txtfile;
      %make salt col 3
      % ff=dd(:,10);ff2=dd(:,3);dd(:,3)=ff;dd(:,10)=ff2;
      eval(['d',num2str(i),'=dd;']);
  
% case of reading jgofout .mat file
    elseif ph==5;
      load /data5/jmanning/ctd/jgofout/oc303/nmfs_ctdsb
       %     make a "dx" variable for each cast
      id=find(cast==i);
      eval(['d',num2str(i),'=[press(id) temp_ctd(id) sal_ctd(id) flvolt(id) light_tran_v(id)];']);
      
% case of reading .data file
    elseif ph==6;
      load /data5/jmanning/ctd/data/de8206.data
      dd=de8206;
      % make a "dx" variable for each cast
      id=find(dd(:,11)==i);
      eval(['d',num2str(i),'=[dd(id,3) dd(id,5) dd(id,6)];']);
    end;

%   make depth negative
    eval(['d',num2str(i),'(:,1)=(-1)*d',num2str(i),'(:,1);']); 


% decide what axis boundary is good for all
    eval(['dd=d',num2str(i),';']);
    for ivar=1:nvar;
      if min(dd(:,ivar))<minv(ivar)
        minv(ivar)=min(dd(:,ivar));
%       and warn user if bogus values
        if minv(ivar)<okmin(ivar);
          disp(['Problem with cast ',num2str(i),' variable ',num2str(ivar)]);
        end;
      end;
      if max(dd(:,ivar))>maxv(ivar)
        maxv(ivar)=max(dd(:,ivar));
%       and warn user if bogus values
        if maxv(ivar)>okmax(ivar);
          disp(['Problem with cast ',num2str(i),' variable ',num2str(ivar)]);
        end;
      end;
    end;

  end;

  if nvar==3|nvar==5;
%%%%%%%%%%%%%%%%%%%%%%%% WARNING: THIS MAY BE changed
    offset=.6/numc;
  else;
    offset=.325/numc;
  end;

% determine how many series to be put on this page
% ns=input('How many series on this page? ');
ns=2;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  Temperature (or var) %%%%%%%%%%%%%%%

% start with temperature in upper left
% position each cast appropriately with a subplot 

  wv=menu('Which?','temp&salt','fluor&tran');
  if wv==2;
    %    wv=input('column number of this variable in file: ');
    wv=2;
    var=input('variable name to label top plot: ','s');
    var2=input('variable name to label bottom plot: ','s');
%    var='Transmission (volts)';
%    var2='Density (sigma-t)';
    ivar=wv;
  else;
    wv=2;
    var='Temperature (degC)';
    var2='Salinity (PSU)';
%    var='Fluorescence (Volts)';
%    var2='Transmission (Volts)';
%    var='Density (sigma-t)'; 
    ivar=wv;
  end;
  
  
  isp=0;
  for i=castvec;
    isp=isp+1;
    subplot(ns,numc,isp)
    eval(['h=plot(d',num2str(i),'(:,wv),d',num2str(i),'(:,1));']);
  
%   color the profile
    set(h,'linewidth',2,'color','g') 

%   set axis and position
%    xoffset=offset*(isp)+offset;
       xoffset=0.05+offset*(isp);
    minv(1)=-80;
    disp(['hardcoded to ',num2str(minv(1)),'m depth limit'])
    axis([minv(ivar) maxv(ivar) minv(1) maxv(1)]);
    set(gca,'position',[xoffset .1+ns*.5-.5 offset .7/ns])

%   tick only the min and max xticks
    xt=get(gca,'xtick');
    set(gca,'xtick',[min(xt) max(xt)]);

%   label ticks only one first panel
    if isp~=1;
      set(gca,'yticklabels',[' ']);
      set(gca,'xticklabels',[' ']);
    else;

%     label these ticks with text command
      if numc>5;
        set(gca,'fontsize',10)
      end;
      h=text(40,maxv(1)+20,cru,'fontsize',18);
      set(h,'fontsize',12);
       h=text(minv(ivar)+offset*(.6),maxv(1)+10,var,'color','g');
       ex=get(h,'extent');
       set(h,'fontsize',12);
      h=text(minv(ivar)+offset*(.4),minv(1)+(minv(1)/(-5)),'Cast#');
      set(h,'fontsize',12);
%      set(gca,'xticklabels',[[' ',num2str(min(xt))];'  ';'  ';num2str(max(xt));'  ']);
      ylabel('Depth (m)');
    end;

%   label cast number
    rg=maxv(ivar)-minv(ivar);
    h=text(minv(ivar)+rg/2,minv(1)+(minv(1)/(-10)),num2str(i));
    set(h,'fontsize',10,'horizontalalignment','center');

% Add biology?
%  coh=menu('What?','Cod','Haddock','both');
    if bio==1;
      hold on;
      h=text(ex(4),maxv(1)+10,'Haddock/100m3','color','m');
      h=text(minv(ivar)+offset*(.4),minv(1)+(minv(1)/(-5)),'Haul#','color','m');
      load /home3/ocn/jmanning/gbs95/moc/s197.dat
      id=find(s197(:,3)==i);
      if id~=[];
        xv=(minv(ivar)+rg/2)*ones(length(id),1);
        if coh==1;
          sizesym(xv,s197(id,4),s197(id,5),1,.2,'m');
        elseif coh==2;
          sizesym(xv,s197(id,4),s197(id,6),1,.2,'m');
        elseif coh==3;
          sizesym(xv,s197(id,4),s197(id,5)+s197(id,6),2,.2,'m');
        end;

%       label mocness number
        h=text(minv(ivar)+rg/2,minv(1)+(minv(1)/(-5)),num2str(s197(id(1),2)));
        set(h,'fontsize',10,'color','m');
      end;
    end;

  end;

if ns>1;
%%%%%%%%%%%%%%%%%%%%   Salinity  (or "var2")%%%%%%%%%%%%%%%%%%%%%%%

%  ivar=ivar+2;
% wv=input('column number of this 2nd variable in file: ');
  wv=3;
  ivar=wv;
  ivar=3;
  if nvar==3|nvar==5; nisp=0; end;
  for i=castvec;
    isp=isp+1;
    nisp=nisp+1;
    subplot(2,numc,isp)
    %eval(['h=plot(d',num2str(i),'(:,wv),d',num2str(i),'(:,1));']);
    eval(['h=plot(d',num2str(i),'(:,wv),d',num2str(i),'(:,1));']);

    set(h,'linewidth',2,'color','r')    
%    xoffset=0.05+offset*(nisp)+offset;
    xoffset=0.05+offset*(nisp);
    axis([minv(ivar) maxv(ivar) minv(1) maxv(1)]);
    set(gca,'position',[xoffset .15 offset .35])
    if isp~=numc+1
      set(gca,'xticklabels',[' ']);
      set(gca,'yticklabels',[' ']);
    else;
      if numc>5;
        set(gca,'fontsize',10)
      end;
       h=text(minv(ivar)+offset*(.6),maxv(1)+10,var2,'color','r');
%       h=text(30+offset*(.6),maxv(1)+10,var2);
       set(h,'fontsize',12);
    end;
    xt=get(gca,'xtick');
    set(gca,'xtick',[min(xt) max(xt)]);
  end;


%we=menu('Fluorescence?','y','n');
we=2;
if we==1;  
  nisp=0;
  ivar=ivar+1;
  for i=castvec;
    isp=isp+1;
    nisp=nisp+1;
    subplot(2,numc,isp)
    eval(['plot(d',num2str(i),'(:,4),d',num2str(i),'(:,1))']);
    xoffset=0.05+offset*(nisp);
    if nisp~=1;
      set(gca,'yticklabels',[' ']);
      set(gca,'xticklabels',[' ']);
    else;
      if numc>5;
        set(gca,'fontsize',8)
      end;
      title('fluorescence (volts)')
    end;
    axis([minv(ivar) maxv(ivar) minv(1) maxv(1)]);
    set(gca,'position',[xoffset+.1 .1 offset .35])
    xt=get(gca,'xtick');
    set(gca,'xtick',[min(xt) max(xt)]);
  end;
end;

%we=menu('Light Levels?','y','n');
we=2;
if we==1;  
  ivar=ivar+1;
  for i=castvec;
    isp=isp+1;
    nisp=nisp+1;
    subplot(2,numc,isp)
    eval(['plot(d',num2str(i),'(:,5),d',num2str(i),'(:,1))']);
    xoffset=0.05+offset*(nisp+1);
    set(gca,'yticklabels',[' ']);
    if nisp~=numc+1
      set(gca,'xticklabels',[' ']);
    else;
      if numc>5;
        set(gca,'fontsize',8)
      end;
      title('light ');
    end;
    axis([minv(ivar) maxv(ivar) minv(1) maxv(1)]);
    set(gca,'position',[xoffset+.1 .1 offset .35]);
    xt=get(gca,'xtick');
    set(gca,'xtick',[min(xt) max(xt)]);
  end;
end;
end;
end;
set(gcf,'paperposition',[.1 .1 7.5 9.5])
