% CPF O2 T vs CTD T
new_data = 1;
dirs.data    = '\\atlas\ProjectLibrary\CPF\CPF003\Data\ParagonTests\';
dirs.mission = '2022-3-14T16-30-13';
dirs.save    = 'C:\Users\jplant\Documents\MATLAB\CPF\data\';

fd = fullfile(dirs.data, dirs.mission,'\');

if new_data == 1
    list = dir([fd,'*eng']);
    rlist = size(list,1);
    
    
    out       = ones(1e6, 7) * NaN; % [sdn,state,p,CTDT,S,O2T, cycle#] state = 1,2,3,4 = anch, DeAnch, Ascend, air
    dfill     = out(1,:);
    error_log = cell(1e6,4); % file name, time,state, data field
    ct        = 0;
    log_ct    = 0;
    for fct = 1:rlist
        %for fct = 3
        fp  = fullfile(list(fct).folder, list(fct).name);
        prof_ct_str = regexp(list(fct).name,'\d+(?=\.eng)','match','once');
        prof_ct = str2double(prof_ct_str);
        
        fid = fopen(fp);
        
        tline = ' ';
        while ischar(tline)
            state_filt = '(?<=\t)Anchor|DeAnchor|Ascend|Surface Ops(?=\t)';
            CPF_State = regexp(tline,state_filt,'once','match');
            
            if ~isempty(CPF_State)
                if strcmp(CPF_State,'Anchor')
                    State = 1;
                elseif strcmp(CPF_State,'DeAnchor')
                    State = 2;
                elseif strcmp(CPF_State,'Ascend')
                    State = 3;
                elseif strcmp(CPF_State,'Surface Ops')
                    State = 4;
                end
            end
            
            if regexp(tline,'Optode post:\s*', 'once')
                tmp   = regexp(tline,'\t','split');
                dstr  = [tmp{2},' ',tmp{3}];
                sdn   = datenum(dstr,'dd-mmm-yyyy HH:MM:SS.FFF');
                ind   = regexp(tmp{5},'(?<=Optode post:\s*)\d','once');
                O2tmp = regexp(tmp{5}(ind:end),',','split');
                if size(O2tmp,2) == 11 % ALL GOOD
                    ct = ct+1;
                    out(ct,[1,2,6,7]) = [sdn, State, str2double(O2tmp{4}),prof_ct];
                else
                    disp('Warning: O2 post is non standard')
                    log_ct = log_ct+1;
                    error_log(log_ct,:) = {list(fct).name, dstr,CPF_State,  tmp{5}};
                end
            end
            
            if regexp(tline,'(?<=\t)SBE41 Message(?=\t)', 'once')
                tmp   = regexp(tline,'\t','split');
                dstr  = [tmp{2},' ',tmp{3}];
                sdn   = datenum(dstr,'dd-mmm-yyyy HH:MM:SS.FFF');
                CTDtmp = regexp(tmp{18},',','split');
                if size(CTDtmp,2) == 3 % ALL GOOD (pts)
                    ct = ct+1;
                    out(ct,[1:5,7]) = [sdn, State, str2double(CTDtmp),prof_ct];
                else
                    disp('Warning: CTD post is non standard')
                    log_ct = log_ct+1;
                    error_log(log_ct,:) = {list(fct).name, dstr, CPF_State, tmp{18}};
                end
            end
            tline = fgetl(fid);
        end
        fclose(fid);
    end
    
    out = out(1:ct,:);
    error_log = error_log(1:log_ct,:);
    save([dirs.save,dirs.mission,'.mat'],'out', 'error_log', 'dirs','list');
else
    load([dirs.save,dirs.mission,'.mat'])
end

% PLOT SOME TEMPERATURE DATA
F1 = figure(1);
F1.Units = 'Normalized';
F1.Position = [0.1458 0.5028 0.3709 0.4114];

%remove surf obs
% tsurf = out(:,2) == 4;
% out = out(~tsurf,:);

% state_diff = diff(out(:,2));
% inds = find(state_diff ~= 0); % state breaks
% for i = 1:size(inds,1) -1
% 
% end



TT    = datetime(out(:,1),'ConvertFrom','datenum');
tgO2  = ~isnan(out(:,6)) & out(:,6) >2;
tgCTD = ~isnan(out(:,4)) & out(:,4) >2;


ax(1) = subplot(2,1,1);
%plot(TT(tgCTD),out(tgCTD,4),'b-',TT(tgO2),out(tgO2,6), 'r-','LineWidth',1);
plot(TT(tgCTD),out(tgCTD,4),'bo-',TT(tgO2),out(tgO2,6), 'ro-','MarkerSize',3);
ylabel('Temperature');
title(dirs.mission);

hold on
uprofs = unique(out(:,7));
for i = 1:size(uprofs,1)
    ind = find(out(:,7) == uprofs(i),1,'first');
    plot([TT(ind),TT(ind)],ylim,'--','Color',[128 128 128]/256);
    y_lim = ylim;
    text(TT(ind),y_lim(2) - 0.05*diff(y_lim),num2str(uprofs(i)));
end
hold off


ax(1).FontSize = 12;
L1 = legend('CTD','Optode','Location','NorthOutSide','Orientation','Horizontal');

ax(2) = subplot(2,1,2);
O2Ti = interp1(TT(tgO2),out(tgO2,6),TT(tgCTD));
O2diff = out(tgCTD,4) - O2Ti;
plot(TT(tgCTD),O2diff ,'ko-','MarkerSize',3);
ax(2).FontSize = 12;

hold on
uprofs = unique(out(:,7));
for i = 1:size(uprofs,1)
    ind = find(out(:,7) == uprofs(i),1,'first');
    plot([TT(ind),TT(ind)],ylim,'--','Color',[128 128 128]/256);
    y_lim = ylim;
    text(TT(ind),y_lim(2) - 0.05*diff(y_lim),num2str(uprofs(i)));
end
hold off

linkaxes(ax,'x');

