clear linesToKeep

lineNumber = 1;
ltkIndex = 1;

firstProfile = false;

if(firstProfile)
    %Find the CTD dump setup command response
    temp = char(CTDSal(lineNumber,:));
    while( ~strcmp(temp,'S>ds  '));
        lineNumber = lineNumber + 1;
        temp = char(CTDSal(lineNumber,:));
    end
    linesToKeep(ltkIndex) = lineNumber;
    ltkIndex = ltkIndex + 1;
    lineNumber = lineNumber + 1;
    
    %Store the line numbers for the ds responses
    temp = char(CTDSal(lineNumber,:));
    while( ~strcmp(temp, 'real-time output is P only  '));
        temp = char(Comment(lineNumber,:));
        if(strcmp(temp, 'Unparsed SBE41 Message'));
            linesToKeep(ltkIndex) = lineNumber;
            ltkIndex = ltkIndex + 1;
        end
        lineNumber = lineNumber + 1;
        temp = char(CTDSal(lineNumber,:));
    end
    linesToKeep(ltkIndex) = lineNumber;
    lineNumber = lineNumber + 1;
    ltkIndex = ltkIndex + 1;
    
    %Find the Optode setup data
    temp = char(Topt(lineNumber,:));
    while( ~strcmp(temp, '!Product Name,4330,1532,Optode Sensor'));
        lineNumber = lineNumber + 1;
        temp = char(Topt(lineNumber,:));
    end
    linesToKeep(ltkIndex) = lineNumber;
    ltkIndex = ltkIndex + 1;
    lineNumber = lineNumber + 1;
    
    %Store the line numbers for the Optode setup data
    temp = char(Topt(lineNumber,:));
    while( ~strcmp(temp, '#'));
        temp = char(Comment(lineNumber,:));
        if(strcmp(temp, 'Optode Message'));
            linesToKeep(ltkIndex) = lineNumber;
            ltkIndex = ltkIndex + 1;
        end
        lineNumber = lineNumber + 1;
        temp = char(Topt(lineNumber,:));
    end
    linesToKeep(ltkIndex) = lineNumber;
    lineNumber = lineNumber + 1;
    ltkIndex = ltkIndex + 1;
end

%Find the first Optode measurement
temp = char(Topt(lineNumber,:));
while( length(strfind(temp, 'MEASUREMENT')) < 1);
   lineNumber = lineNumber + 1;
   temp = char(Topt(lineNumber,:));
end
%The Optode measurement doesn't have depth, get it from the line before
linesToKeep(ltkIndex) = lineNumber-1;
ltkIndex = ltkIndex + 1;
linesToKeep(ltkIndex) = lineNumber;
lineNumber = lineNumber + 1;

%Find the first AB Set Fast state
temp = char(State(lineNumber,:));
while( length(strfind(temp, 'AB Set Fast')) < 1);
   lineNumber = lineNumber + 1;
   temp = char(State(lineNumber,:));
end

%Store all the depths and Optode measurements until SOSetBellows state
temp = char(State(lineNumber,:));
while( length(strfind(temp, 'SOSetBellows')) < 1);
    temp = char(Comment(lineNumber,:));
    if( strcmp(temp, 'SBE41 Message'));
        linesToKeep(ltkIndex) = lineNumber;
        ltkIndex = ltkIndex + 1;
    end
    
    temp = char(Topt(lineNumber,:));
    if( length(strfind(temp, 'MEASUREMENT')) > 0);
           linesToKeep(ltkIndex) = lineNumber;
           ltkIndex = ltkIndex + 1;
    end
    
    lineNumber = lineNumber + 1;
    temp = char(State(lineNumber,:));
end
lineNumber = lineNumber + 1;

%Find the CTD dump data command response
temp = char(CTDSal(lineNumber,:));
while( ~strcmp(temp,'S>dd  '));
   lineNumber = lineNumber + 1;
   temp = char(CTDSal(lineNumber,:));
end
linesToKeep(ltkIndex) = lineNumber;
ltkIndex = ltkIndex + 1;
lineNumber = lineNumber + 1;

%Store the line numbers for the dd responses
ddStartIndex = 0;
temp = char(CTDSal(lineNumber,:));
while( ~strcmp(temp, 'upload complete  '));
    temp = char(Comment(lineNumber,:));
    if(strcmp(temp, 'Unparsed SBE41 Message'));
        if(ddStartIndex == 0)
            ddStartIndex = ltkIndex;
        end
        linesToKeep(ltkIndex) = lineNumber;
        ltkIndex = ltkIndex + 1;
    end
    lineNumber = lineNumber + 1;
    temp = char(CTDSal(lineNumber,:));
end
linesToKeep(ltkIndex) = lineNumber;
lineNumber = lineNumber + 1;
ltkIndex = ltkIndex + 1;

fid = fopen('2014-11-26T17-44-7ScienceData.txt', 'w');
%!MEASUREMENT,4330,1532,O2Concentration[uM],3.009799E+02,AirSaturation[%],9.651146E+01,Temperature[Deg.C],1.545611E+01,CalPhase[Deg],3.061698E+01,TCPhase[Deg],3.061698E+01,C1RPh[Deg],3.914038E+01,C2RPh[Deg],8.523399E+00,C1Amp[mV],7.824652E+02,C2Amp[mV],9.305553E+02,RawTemp[mV],3.446682E+02

fprintf(fid, 'Date \tTime \tState \tComment \tPressure \tCTD Message \tOptode Message \tDepth \tTemperature \tSalinity \tO2Concentration[uM] \tAirSaturation[percent] \tTemperature[Deg.C] \tCalPhase[Deg] \tTCPhase[Deg] \tC1RPh[Deg] \tC2RPh[Deg] \tC1Amp[mV] \tC2Amp[mV] \tRawTemp[mV]\n');
for i=1:(length(linesToKeep)-1)
    if(i < ddStartIndex)
        temp = strsplit(datestr(Time(linesToKeep(i))));
         fprintf(fid, '%s\t%s\t%s\t%s\t%f\t%s\t%s', datestr(Date(linesToKeep(i))), temp{2}, ...
            State{linesToKeep(i)}, Comment{linesToKeep(i)}, ...
            Pressure(linesToKeep(i)), ...
            CTDSal{linesToKeep(i)}, ...
            Topt{linesToKeep(i)});
        temp = char(Topt(linesToKeep(i),:));
        if(  length(strfind(temp, 'MEASUREMENT')) > 0)
            optodeFields = strsplit(Topt{linesToKeep(i)}, ',');
            fprintf(fid, '\t \t \t \t%s \t%s  \t%s \t%s \t%s \t%s \t%s \t%s \t%s \t%s \n', optodeFields{5}, optodeFields{7}, optodeFields{9}, optodeFields{11}, ...
                optodeFields{13}, optodeFields{15}, optodeFields{17}, optodeFields{19}, ...
                optodeFields{21}, optodeFields{23});
        else
            fprintf(fid, '\n');
        end
    else
        temp = strsplit(datestr(Time(linesToKeep(i))));
        dts = strsplit(CTDSal{linesToKeep(i)}, ',');
        fprintf(fid, '%s\t%s\t%s\t%s\t%f\t%s\t%s\t%f\t%f\t%f\n', datestr(Date(linesToKeep(i))), temp{2}, ...
            State{linesToKeep(i)}, Comment{linesToKeep(i)}, ...
            Pressure(linesToKeep(i)), ...
            CTDSal{linesToKeep(i)}, ...
            Topt{linesToKeep(i)}, ...
            str2double(dts{1}), str2double(dts{2}), str2double(dts{3}));
    end
end

temp = strsplit(datestr(Time(linesToKeep(length(linesToKeep)))));
dts = strsplit(CTDSal{linesToKeep(length(linesToKeep))}, ',');
fprintf(fid, '%s\t%s\t%s\t%s\t%f\t%s\t%s\n', datestr(Date(linesToKeep(length(linesToKeep)))), temp{2}, ...
    State{linesToKeep(length(linesToKeep))}, Comment{linesToKeep(length(linesToKeep))}, ...
    Pressure(linesToKeep(length(linesToKeep))), ...
    CTDSal{linesToKeep(length(linesToKeep))}, ...
    Topt{linesToKeep(length(linesToKeep))});
fclose('all');


