% this is to plot the oxygen data from the titrator for the Eddy cruise

load eddy2005_1chla.txt

chla=eddy2005_1chla(:,5);   % in ug/l
phaeo=eddy2005_1chla(:,6);
chlacast=eddy2005_1chla(:,1);
chlaniskin=eddy2005_1chla(:,2);
chladepth=eddy2005_1chla(:,4);
chlaid=chlacast*100+chlaniskin;

load eddy2005_1oxygen.txt
%load position.txt

oxy=eddy2005_1oxygen(:,9);   % in ml/l
oxycast=eddy2005_1oxygen(:,2);
oxyniskin=eddy2005_1oxygen(:,3);
oxyid=oxycast*100+oxyniskin;

%castpos=position(:,1);
%lat=position(:,2)+1/60*position(:,3);
%long=position(:,4)+1/60*position(:,5);

nomdepth=[1:24; 0 0 20 20 40 40 60 60 70 70 80 80 90 100 100 110 120 120 140 140 200 300 500 700];
nomdepth2=[1:24;0 0 0 40 40 40 60 60 60 80 80 80 100 100 100 106 106 106 120 120 120 140 140 140];
nomdepth3=[1:24;0 50:50:800 900 1000 1100 1200 1300 1400 1500];
nomdepth4=[1:24;0 20 40 50 60 70 75 80 85 90 95 100 105 110 115 120 130 140 150 160 170 180 190 200];

for ii=1:length(oxy)
%    oxylong(ii)=long(find(castpos==oxycast(ii)));
%   oxylat(ii)=lat(find(castpos==oxycast(ii)));
   	  if oxycast(ii)==16
     			oxydepth(ii)=nomdepth2(2,find(nomdepth2(1,:)==oxyniskin(ii)));
     	  elseif oxycast(ii)==27
             oxydepth(ii)=nomdepth3(2,find(nomdepth3(1,:)==oxyniskin(ii)));
        elseif oxycast(ii)==80
     			oxydepth(ii)=nomdepth4(2,find(nomdepth4(1,:)==oxyniskin(ii)));
        else 
     			oxydepth(ii)=nomdepth(2,find(nomdepth(1,:)==oxyniskin(ii)));
        end
end

for ii=1:length(chla)
%    chlalong(ii)=long(find(castpos==chlacast(ii)));
%    chlalat(ii)=lat(find(castpos==chlacast(ii)));
   	  if chlacast(ii)==16
     chladepth(ii)=nomdepth2(2,find(nomdepth2(1,:)==chlaniskin(ii)));
        elseif oxycast(ii)==27
     chladepth(ii)=nomdepth3(2,find(nomdepth3(1,:)==chlaniskin(ii)));
         elseif oxycast(ii)==80
     chladepth(ii)=nomdepth4(2,find(nomdepth4(1,:)==chlaniskin(ii)));
else 
     chladepth(ii)=nomdepth(2,find(nomdepth(1,:)==chlaniskin(ii)));
        end
end

[c,ia,ib]=union(oxyid,chlaid);

mm=[c floor(c/100) c-floor(c/100)]; 
for ii=1:length(c)
   	  if floor(c(ii)/100)==16
     depth(ii)=nomdepth2(2,find(nomdepth2(1,:)==c(ii)-floor(c(ii)/100)*100));
        elseif floor(c(ii)/100)==27
     depth(ii)=nomdepth3(2,find(nomdepth3(1,:)==c(ii)-floor(c(ii)/100)*100));
        elseif floor(c(ii)/100)==80
     depth(ii)=nomdepth4(2,find(nomdepth4(1,:)==c(ii)-floor(c(ii)/100)*100));
else 
     depth(ii)=nomdepth(2,find(nomdepth(1,:)==c(ii)-floor(c(ii)/100)*100));
        end
end


M=[];
for ii=1:length(mm)
   fo=find(oxyid==c(ii));
   fc=find(chlaid==c(ii));
   if (isempty(fo) & isempty(fc))
      M=[M; floor(c(ii)/100) c(ii)-floor(c(ii)/100)*100 depth(ii) nan nan nan nan];
   elseif    isempty(fo)
        M=[M; floor(c(ii)/100) c(ii)-floor(c(ii)/100)*100 depth(ii) nan chla(fc) phaeo(fc) chla(fc)+phaeo(fc)];
   elseif isempty(fc)
        M=[M; floor(c(ii)/100) c(ii)-floor(c(ii)/100)*100 depth(ii) oxy(fo) nan nan nan]; 
   else
        M=[M; floor(c(ii)/100) c(ii)-floor(c(ii)/100)*100 depth(ii) oxy(fo) chla(fc) phaeo(fc) chla(fc)+phaeo(fc)]; 
   end
      
end


%save the data
% cast niskin nom_depth oxygen chla phaeo chla+phaeo
forma=['%3i\t %3i\t %3i\t %5.3f\t %5.3f\t %5.3f\t %5.3f\r\n'];
header=['%cast niskin nom_depth oxygen(ml/l) chla(ug/l) phaeo(ug/l) chla+phaeo(ug/l)'];
fid=fopen('oce415_1hydro.dat','w');
fprintf(fid,'%s\r\n',header)
fprintf(fid,forma,M')
fclose(fid)

