%overlayEngData2.m

clear;

raw = '2016Sep23';

MAXi=50000;
MAXSED = 120;
path =    sprintf('../BOG2/%s/raw',raw);
eng_search = sprintf('../%s/raw/*.eng',raw);
outfile = sprintf('../%s/parsed/CPF%s_ENG.mat',raw,raw);
sedfile = sprintf('../%s/parsed/CPF%s_SED.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);
i=1;

P = NaN(numfiles,MAXi);
BP = NaN(numfiles,MAXi);
BATTV = NaN(numfiles,MAXi);
BATTI = NaN(numfiles,MAXi);
DATETIMENUM = NaN(numfiles,MAXi);
format longg, DATETIMENUM;
PIDU = NaN(numfiles,MAXi);
STATE = NaN(numfiles,MAXi);

SEDTRAP(numfiles) = struct('advp',NaN(1,MAXSED),...
    'homep',NaN(1,MAXSED),...
    'sedhome',NaN(1,MAXSED),...
    'sedadv',NaN(1,MAXSED)...
    );



%SEDHOME = NaN(numfiles,MAXSed);
format longg,SEDTRAP.sedhome;
%SEDNEXT = NaN(numfiles,MAXSed);
format longg,SEDTRAP.sedadv;
%CPDATE = NaN(numfiles,MAXi);
%CPTIME = NaN(numfiles,MAXi);

for ii = 1:numfiles
   fp = sprintf('%s/%s',path,fileList(ii).name);
   fprintf('Processing %s\n',fp);
   [p,bp,battv,batti,cpDateTimeNum,PIDu,state,sedtrap]=parseEngData(fp,sedfile);
   P(i,:)=p;
   BP(i,:)=bp;
   BATTV(i,:)=battv;
   BATTI(i,:)=batti;
   DATETIMENUM(i,:)=cpDateTimeNum(1,:);
   PIDU(i,:)=PIDu;
   STATE(i,:)=state;
   %SEDHOME(i,:) = sedhome;
   %SEDNEXT(i,:) = sednext;
   SEDTRAP(i)=sedtrap;
   i=i+1;
end


% figure(1);title('CPF');
% 
% subplot(2,2,1);
% plot(cpT(:),-cpP(:)); hold on
% xlabel('degC'),ylabel('dBar');
% grid on;
% 
% 
% subplot(2,2,2);
% plot(cpS(:),-cpP(:)); hold on
% xlabel('PSU'),ylabel('dBar');
% grid on;
% 
% subplot(2,2,3);
% plot(flbbFL(:),flbbDepth(:)); hold on
% 
% xlabel('flbb'),ylabel('dBar');
% grid on;
% 
% subplot(2,2,4);
% plot(optodeO2Conc(:),optodeDepth(:)); hold on
% 
% xlabel('O2'),ylabel('dBar');
% grid on;


save (outfile);
