%flour.m

clear;
raw = '2016Jul12';
path =    sprintf('../%s/raw',raw);
outfile = sprintf('../%s/parsed/CPF%sFLOUR.mat',raw,raw);
flour_raw_file = sprintf('../%s/parsed/CPF%sFLOUR_RAW.txt',raw,raw);

fileList = dir(path);
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);
flbbIndex=1;
flourFID = fopen(flour_raw_file,'w');


for fileno = 1:numfiles
   fp = sprintf('%s/%s',path,fileList(fileno).name);
   fprintf('Processing %s\n',fp);
   inFID = fopen(fp);
   %get the header line
   inString = fgets(inFID);
    
    while ~feof(inFID)
        inString = fgets(inFID);
        inFields = strsplit(inString, '\t', 'CollapseDelimiters', false);
        
        if strcmp(strtrim(inFields{5}), 'FLBB Message')
            numCommas = length(strfind(inFields{15}, ','));
            if(numCommas == 7)
                flbbFields = strsplit(inFields{15}, ',', 'CollapseDelimiters', false);
                flbbDate(flbbIndex, :) = inFields{2};
                flbbTime(flbbIndex, :) = inFields{3};
                flbbDateTimeNum(flbbIndex) = datenum(strcat(inFields{2}, 'T', inFields{3}));
                %flbbState{flbbIndex} = inFields{4};
                %flbbComment{flbbIndex} = inFields{5};
                flbbDepth(flbbIndex) = -str2double(inFields{6});
                flbbFL(flbbIndex) = str2double(flbbFields{7});
                flbbBB700(flbbIndex) = str2double(flbbFields{6});
                flbbBB470(flbbIndex) = str2double(flbbFields{4});
                
                fprintf(flourFID,'%s,%f\n',inFields{15},flbbDepth(flbbIndex));
%               fprintf(outFID, '%s\t%s\t%s\t%s\t%f\t\t\t\t%f\t%f\r\n', ...
%                 flbbDate(flbbIndex,:), flbbTime(flbbIndex,:), flbbState{flbbIndex}, flbbComment{flbbIndex}, ...
%                 flbbDepth(flbbIndex), flbbFL(flbbIndex), flbbBB(flbbIndex));
            
                flbbIndex = flbbIndex + 1;
            end %end if numCommas
        end %end FLBB Message
         
    end %end inFID parse
    fclose(inFID);
end %for fileno

save(outfile);
fclose(flourFID);