% routine to plot vertical haul count vs. depth-averaged
% physical variable like temperature
%
% input: ctd data in ".data" files per cruise
%        bio data with station # and counts
%
% output: bar chart of phys vs bio
%
%  9 Feb 1998 @AGU
% 27 May 1998 added phase 2: fish vs hydroids figure(4)

%ph=menu('What?','vs phys variables?','hydroids vs fish','geographic dist');
ph=3;

if ph==1;
%
hmv=1;% number of variables
hmc=5;% number of cruises
nsp=0;% initalize number of variables
for ic=1:hmc;
%ic=2

% load ctd file for one cruise
% ".data" files were was renamed for win95 compatibility to:
% ".ctd"
  direc='/data5/jmanning/ctd/data/'
  fn=input('enter ctd .data filename w/out path or extension: ','s');
  eval(['load ',direc,fn,'.data']);
  eval(['d=',fn,';']);

% define the column of bio count dependednt on file
  if fn(length(fn)-4:length(fn))=='j9507';bfn='hyd9507.mtl';
    col=4;yr='May 1995';year=95;
  elseif fn(length(fn)-4:length(fn))=='oc303';bfn='hydoc303.dat';
    col=2;eval(['load ',biodir,'bongo97.txt']);bt=bongo97;yr='May 1997';year=97;
  elseif fn(length(fn)-4:length(fn))=='j9505';bfn='b9505.mtl';
    col=4;yr='Apr 1995';
  elseif fn(length(fn)-4:length(fn))=='b9403';bfn='b9403.mtl';
    col=4;yr='May 1994';
  elseif fn(length(fn)-4:length(fn))=='l9306';bfn='b9306.mtl';
    col=4;yr='May 1993';
  end;

  biodir='/home3/ocn/jmanning/gbs95/barb/';
% load bio counts
  eval(['load ',biodir,bfn]);
  eval(['b=',bfn(1:length(bfn)-4),';']);


% for each station find the vertical average phys variable
  for i=1:length(b)-1;

%   for oc303 we need to find the "station #'
    if fn(length(fn)-4:length(fn))=='oc303';
      id=find(bt(:,3)==b(i,1));
      id=find(d(:,11)==bt(id(1),4));
    elseif fn(length(fn)-4:length(fn))=='j9505'|fn(length(fn)-4:length(fn))=='b9403';
      id=find(d(:,12)==b(i,1));
    else;
      id=find(d(:,11)==b(i,1));
    end;

    for k=1:3;
      pomean(i,k)=mean(d(id,4+k));
      postd(i,k)=std(d(id,4+k));
%      disp(['Mean ',num2str(k),'= ',num2str(pomean(i,k))]);
    end;
    if id~=[];
      la(i)=d(id(1),9);
      lo(i)=d(id(1),10);
    else;
      lo(i)=NaN;
      la(i)=NaN;
    end;
  end;

% rid of NaN values
  id=find(isnan(pomean(:,1))~=1);
  b=b(id,:);pomean=pomean(id,:);postd=postd(id,:);

% plot bio vs phys
  var=['temp';'salt';'dens'];
  inc=[.5 .2 .07];
  miny=[4 32 24];
  maxy=[12 35 27];
  m=0;
  figure(1);
  for m=1:hmv;
    nsp=nsp+1;
    subplot(hmc,hmv,nsp);
      [y,i]=sort(pomean(:,m));
%     interpolate both to equally spaced
%     eqsp=floor(min(y)):inc(m):ceil(max(y));
      eqsp=min(y):inc(m):max(y);
%      eqsp=miny(m):inc(m):maxy(m);
      bio=interp1(y,b(i,col),eqsp); 
      bar(eqsp,bio,['c',num2str(ic)]);
      axis([miny(m) maxy(m) 0 max(bio)]);
      title([fn,' ',var(m,:)],'fontsize',12);
  end;

  figure(2)
% tsplot
  sizesym([pomean(:,2); 31],[pomean(:,1); 3],[-b(:,col); 7000],1,.7,['c',num2str(ic)]);
  xlabel('Salt')
  ylabel('Temp')
  hold on;
%  text(min(pomean(:,2)),max(pomean(:,1)),yr,'color',['c',num2str(ic)],...
  text(34,9-ic,yr,'color',['c',num2str(ic)],...
     'verticalalignment','bottom','horizontalalignment','left');
  axis([32 35 4 10])
  pause(2);

  

  
% get another cruise
  clear b d pomean postd eqsp bio lo la
end;

% end phase 1
end;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%  Hydroids vs fish (phase 2) %%%%%%%%%

if ph==2;
  % load files for figure 4
  load /home3/ocn/jmanning/gbs95/barb/hydall.mtl
  load /home3/ocn/jmanning/gbs95/bongo/92_95/bongall.dat

  % fish vs hydroids
  figure(3);
  m=0;
  for i=[9306 9403 9505 9507 9704];
    id=find(hydall(:,1)==i);
    idf=find(bongall(:,1)==floor(i/100));
    % now, for each hydroid of this cruise find the fish
    for k=1:length(id);
      id2=find(bongall(idf,2)==hydall(id(k),2));
      if id2~=[];
        m=m+1;
        hydfsh(m,:)=[hydall(id(k),:) bongall(idf(id2),5:6)];
      end;
    end;
  end;
  save /home3/ocn/jmanning/gbs95/barb/hydfsh.dat hydfsh -ascii

  % plot hyd vs fish
  for m=1:length(hydfsh);
    semilogx((hydfsh(m,5)),hydfsh(m,7),'.','markersize',20,...
     'color',['c',num2str(98-floor(hydfsh(m,1)/100))]);
    hold on;
  end; 
  ylabel('COD No./100m3');
  xlabel('Hydroids'); 
end;
    
path(path,'/home3/ocn/jmanning/m/restored/stats'); 
%[B,BINT,R,RINT,STATS] =regress(hydfsh(:,7),hydfsh(:,5),80);
%STATS 

%%%%%%%%%%%%%%%%%%%   PHASE 3 GEOGRAPHIC PLOTS  %%%%%%%%%%%%%%

if ph==3;
  load /home3/ocn/jmanning/gbs95/barb/hydfsh.dat
  b=hydfsh;
  load /home3/ocn/jmanning/gbs95/barb/hydpms.dat
  b2=hydpms;
  col=5;
  mhc=5;
  kk=0;
  figure(4);
  for i=[9306 9403 9505 9507 9704];
    kk=kk+1;
%    subplot(3,2,kk);
    georbat;
    hold on;
    id=find(b(:,1)==i);

%   get more accurate numbers from "hydpms"
    for k=1:length(id);
      id2=find(b2(:,1)==i&b2(:,2)==b(id(k),2));
      b(id(k),5)=b2(id2,3)/w_depth(b(id(k),4),b(id(k),3));
    end;

    sizesym([b(id,4);-70],[b(id,3); 39],[-b(id,col); -7000]',-3,.9,['c',num2str(kk)]);
    axis([-69 -65 40 42.5]);
    title(num2str(i));
    if kk<4;ht=.5;else;ht=.05;end;
    if kk==1|kk==4;lr=.05;elseif kk==2|kk==5;lr=.3;elseif kk==3|kk==6;lr=.7;end;
    set(gca,'position',[lr ht .25 .4]);
    if kk~=hmc;set(gca,'xticklabels',' ');xlabel(' ');end;
    if kk~=1&kk~=4;set(gca,'yticklabels',' ');ylabel(' ');end;
  end;
end;

      