%assemble_for_katie.m
clear;
SN = 'BOG3'
raw = '2022FEB23';

path =    sprintf('../%s/%s',SN,raw);
eng_search = sprintf('%s/raw/*.eng',path);
outfile = sprintf('%s/parsed/CPF%s_summary.txt',path,raw);
sedfile = sprintf('%s/parsed/CPF%s_SED.txt',path,raw);
fp_out = fopen(outfile,"w");
fprintf(fp_out,'%s %s\n\nGPS DATA:\n',SN,raw);

fileList = dir(eng_search);
fileList = fileList(~[fileList.isdir]); %remove directories
[junk, sortorder] = sort([fileList.datenum]);
fileList = fileList(sortorder); %list is now in ascending date order
numfiles = numel(fileList);
%In = cell(numfiles,1);
i=1;

for ii = 1:numfiles
   fp = sprintf('%s/raw/%s',path,fileList(ii).name);
   fprintf('Processing %s\n',fp);
   inFID = fopen(fp);
   
    while ~feof(inFID)
        inString = fgets(inFID);
        if (strfind(inString, 'MO Stat') > 0);
            disp(inString);
            inFields = strsplit(inString, '\t', 'CollapseDelimiters', false);
            fprintf(fp_out,'!%s\n',inFields{39});
            inFields2 = strsplit(inFields{39},',','CollapseDelimiters', false);
            gps_t{i}=(inFields2{18});
            latmin(i)=str2double(inFields2{2})-3600;
            lonmin(i)=str2double(inFields2{4})-12100;
            prof(i) = sscanf(inFields2{9},'%d');
            i=i+1;
        end
    end

    fclose(inFID);
end


fprintf(fp_out,'\n\nSED TRAP DATA:\n');
inFID = fopen(sedfile);
    while ~feof(inFID)
        inString = fgets(inFID);
        fprintf(fp_out,'%s',inString);
    end
 fclose(inFID);


fprintf(fp_out,'\n\nCTD DATA:\n'); 
for ii = 1:numfiles
   fp = sprintf('%s/raw/%s',path,fileList(ii).name);
   fprintf('Processing %s\n',fp);
   inFID = fopen(fp);
   
    while ~feof(inFID)
        inString = fgets(inFID);
        if ( (strfind(inString, 'Dump CP Data') > 0) )
            if  (strfind(inString, 'Unparsed') > 0)  
                disp(inString);
                fprintf(fp_out,'%s',inString);
            end
        end
    end

    fclose(inFID);
end




fclose(fp_out);