% ctd_mld.m
% programme calculant la profondeur de la thermocline
clear
ici=pwd;
dT=0.1;

path_mat='E:\Gatti2\golts\Survey\Survey081203\CTD\Data_extraites\asc';
[f_mat,p_mat]=uigetfile([path_mat,'\*.asc']);
path_mat_file=[p_mat,f_mat];
ctd=load(path_mat_file);

% on enlève les bad Flags
[m,n]=size(ctd);
if n==13|n==14
    ind=find(ctd(:,n)==0);
    ctd=ctd(ind,:);
elseif n==12
    Flag=0*ones(m,1);
    ctd=[ctd Flag];
    n=13;
end

for i=1:length(ctd)
    if (ctd(1,n-4)-ctd(i,n-4))>dT
        eval(['disp(''dt: ',num2str(i),''')'])
        p1=ctd(i,n-7);
        eval(['disp(''',num2str(p1),'m'')'])
        
        figure
        plot(ctd(:,n-4),-ctd(:,n-7))
        hold on
        y=-p1*ones(1,13);
        plot((13:25),y,'r')
        hold off
        break
    else
        p1=NaN;
    end
end

for i=1:length(ctd)
    if abs((ctd(1,n-3)-ctd(i,n-3)))>0.005
        eval(['disp(''drho: ',num2str(i),''')'])
        p2=ctd(i,n-7);
        eval(['disp(''',num2str(p2),'m'')'])
        
        figure
        plot(ctd(:,n-3),-ctd(:,n-7))
        hold on
        y=-p2*ones(1,4);
        plot((27:30),y,'r')
        hold off      
        break
    else
        p2=NaN;
    end
end

mld(1,1)=p1;
mld(1,2)=p2;
cd (ici)
%save  mld.txt mld -ascii


