% load_ctd.m
% load CTD data from the SeaHorse SBE 19plus
%
% 

% setup 2D arrays for CTD parameters
Cond = NaN*zeros(250,137);
Temp = NaN*zeros(250,137);
Pres = NaN*zeros(250,137);
for ii = 1:137
    Pres(:,ii) = [1:250]';
end
Fluor = NaN*zeros(250,137);
Sal = NaN*zeros(250,137);
DesRate = NaN*zeros(250,137);
SigTheta = NaN*zeros(250,137);
Depth = NaN*zeros(250,137);
PotTemp = NaN*zeros(250,137);
Nsqrd = NaN*zeros(250,137);
N = NaN*zeros(250,137);
Flag = NaN*zeros(250,137);
Time = NaN*zeros(250,137);

for ii = 2:136
    ii
    if (ii < 10)
        eval(['[A,B,C,D,E,F,G,H,I,J,K,L] = textread(''sh__000', num2str(ii), '_bf.cnv'', ''%f%f%f%f%f%f%f%f%f%f%f%f'',''headerlines'',144);'])
        for jj = 1:length(A)
            findA = find(A == -9.990e-29);
            A(findA) = NaN;
            findB = find(B == -9.990e-29);
            B(findB) = NaN;
            findC = find(C == -9.990e-29);
            C(findC) = NaN;
            findD = find(D == -9.990e-29);
            D(findD) = NaN;
            findE = find(E == -9.990e-29);
            E(findE) = NaN;
            findF = find(F == -9.990e-29);
            F(findF) = NaN;
            findG = find(G == -9.990e-29);
            G(findG) = NaN;
            findH = find(H == -9.990e-29);
            H(findH) = NaN;
            findI = find(I == -9.990e-29);
            I(findI) = NaN;
            findJ = find(J == -9.990e-29);
            J(findJ) = NaN;
            findK = find(K == -9.990e-29);
            K(findK) = NaN;
            findL = find(L == -9.990e-29);
            L(findL) = NaN;
        end
        Cond(C,ii) = A;
        Temp(C,ii) = B;
        Pres(C,ii) = C;
        Fluor(C,ii) = D;
        Sal(C,ii) = E;
        DesRate(C,ii) = F;
        SigTheta(C,ii) = G;
        Depth(C,ii) = H;
        PotTemp(C,ii) = I;
        Nsqrd(C,ii) = J;
        N(C,ii) = K;
        Flag(C,ii) = L;
        eval(['fid = fopen(''sh__000', num2str(ii), '.cnv'',''r'');'])
        while(~feof(fid))
            gl = fgetl(fid);
            [pt1,pt2] = strtok(gl,':');
            if strcmpi('** First Sample Time',pt1)
                dt = datenum(pt2(2:end));
                Time(:,ii) = dt;
            end
        end
        fclose(fid);
    elseif (ii < 100)
        eval(['[A,B,C,D,E,F,G,H,I,J,K,L] = textread(''sh__00', num2str(ii), '_bf.cnv'', ''%f%f%f%f%f%f%f%f%f%f%f%f'',''headerlines'',144);'])
        for jj = 1:length(A)
            findA = find(A == -9.990e-29);
            A(findA) = NaN;
            findB = find(B == -9.990e-29);
            B(findB) = NaN;
            findC = find(C == -9.990e-29);
            C(findC) = NaN;
            findD = find(D == -9.990e-29);
            D(findD) = NaN;
            findE = find(E == -9.990e-29);
            E(findE) = NaN;
            findF = find(F == -9.990e-29);
            F(findF) = NaN;
            findG = find(G == -9.990e-29);
            G(findG) = NaN;
            findH = find(H == -9.990e-29);
            H(findH) = NaN;
            findI = find(I == -9.990e-29);
            I(findI) = NaN;
            findJ = find(J == -9.990e-29);
            J(findJ) = NaN;
            findK = find(K == -9.990e-29);
            K(findK) = NaN;
            findL = find(L == -9.990e-29);
            L(findL) = NaN;
        end
        Cond(C,ii) = A;
        Temp(C,ii) = B;
        Pres(C,ii) = C;
        Fluor(C,ii) = D;
        Sal(C,ii) = E;
        DesRate(C,ii) = F;
        SigTheta(C,ii) = G;
        Depth(C,ii) = H;
        PotTemp(C,ii) = I;
        Nsqrd(C,ii) = J;
        N(C,ii) = K;
        Flag(C,ii) = L;
        eval(['fid = fopen(''sh__00', num2str(ii), '.cnv'',''r'');'])
        while(~feof(fid))
            gl = fgetl(fid);
            [pt1,pt2] = strtok(gl,':');
            if strcmpi('** First Sample Time',pt1)
                dt = datenum(pt2(2:end));
                Time(:,ii) = dt;
            end
        end
        fclose(fid);
    else
        eval(['[A,B,C,D,E,F,G,H,I,J,K,L] = textread(''sh__0', num2str(ii), '_bf.cnv'', ''%f%f%f%f%f%f%f%f%f%f%f%f'',''headerlines'',144);'])
        for jj = 1:length(A)
            findA = find(A == -9.990e-29);
            A(findA) = NaN;
            findB = find(B == -9.990e-29);
            B(findB) = NaN;
            findC = find(C == -9.990e-29);
            C(findC) = NaN;
            findD = find(D == -9.990e-29);
            D(findD) = NaN;
            findE = find(E == -9.990e-29);
            E(findE) = NaN;
            findF = find(F == -9.990e-29);
            F(findF) = NaN;
            findG = find(G == -9.990e-29);
            G(findG) = NaN;
            findH = find(H == -9.990e-29);
            H(findH) = NaN;
            findI = find(I == -9.990e-29);
            I(findI) = NaN;
            findJ = find(J == -9.990e-29);
            J(findJ) = NaN;
            findK = find(K == -9.990e-29);
            K(findK) = NaN;
            findL = find(L == -9.990e-29);
            L(findL) = NaN;
        end
        Cond(C,ii) = A;
        Temp(C,ii) = B;
        Pres(C,ii) = C;
        Fluor(C,ii) = D;
        Sal(C,ii) = E;
        DesRate(C,ii) = F;
        SigTheta(C,ii) = G;
        Depth(C,ii) = H;
        PotTemp(C,ii) = I;
        Nsqrd(C,ii) = J;
        N(C,ii) = K;
        Flag(C,ii) = L;
        eval(['fid = fopen(''sh__0', num2str(ii), '.cnv'',''r'');'])
        while(~feof(fid))
            gl = fgetl(fid);
            [pt1,pt2] = strtok(gl,':');
            if strcmpi('** First Sample Time',pt1)
                dt = datenum(pt2(2:end));
                Time(:,ii) = dt;
            end
        end
        fclose(fid);
    end
end

clear A B C D E F G H I J K L findA findB findC findD findE findF findG findH findI findJ findK findL
clear dt ii jj gl ans fid pt1 pt2
save sh_ctd_bf.mat


