% syslog_Thethys_relaibility_process.m
% Lats modified Sep 14, 2014
% Ben Raanan

% Wrote this script to filter syslog critical faults by component

clear all;

yr=2012;

workd = ['~/Documents/MATLAB/MBARI/mat/syslog/' num2str(yr) '/Tethys/'];
load([workd 'syslog_Thethys_relaibility_' num2str(yr) '.mat']);

compHeader={'Component','Mission','Log','Year','Month','Day','HH','MM','SS','Datenum','Msg','Path'};
clear comp

comp    = cell(1,12);
for j=1:length(fields)
    
    X = syslog.(fields{j}).CRITICAL;
    
    if ~isempty(X)
        com = cell(length(X),12);
        in = size(comp,1)+1;
        ind = length(X)+in-1;
        
        for k = 1:size(X,1);
            
            % reduce to line
            dataS = X{k,:};
            
            % categorize by component and extract message
            
            cInd1 = strfind(dataS,'[')+1;
            cInd2 = strfind(dataS,'](CRITICAL)')-1;
            cInd  = strfind(dataS,'component: ')+11; % comp name case CBIT
            wInd  = strfind(dataS,'WATCHDOG');       % flag WATCHDOG reset
            eInd  = strfind(dataS,'Environmental');
            mInd = strfind(dataS,'):')+3;
            
            % case CBIT/WATCHDOG
            if strcmp(dataS(cInd1:cInd2),'CBIT')
                if ~isempty(cInd)
                    com{k,1} = dataS(cInd:end);
                elseif ~isempty(wInd)
                    com{k,1} = 'WatchDog'; 
                elseif ~isempty(eInd)
                    com{k,1} = 'Environmental';   
                else
                    com{k,1} = dataS(cInd1:cInd2);
                end
                com{k,11} = ['[CBIT]: ' dataS(mInd:end)];
            else
                com{k,1} = dataS(cInd1:cInd2);
                com{k,11} = dataS(mInd:end);
            end; clear cInd cInd1 cInd2 wInd
            
            % extract mission, log and path
            com(k,3)  = fields(j);
            com(k,2) = mission(j);
            com(k,12) = fileName(j);
            
            
            % extract date/time
            indT = strfind(dataS,'T'); % flag 'T'
            indZ = strfind(dataS,'Z'); % flag 'Z'
            
            % extract date/time
            year = str2double(dataS(1:4));
            month = str2double(dataS(6:7));
            day = str2double(dataS(9:10));
            hh = str2double(dataS(12:13));
            mm = str2double(dataS(15:16));
            ss = str2double(dataS(18:indZ-1));
            clear indT indZ
            
            % log time data
            com(k,4) = num2cell(year);
            com(k,5) = num2cell(month);
            com(k,6) = num2cell(day);
            com(k,7) = num2cell(hh);
            com(k,8) = num2cell(mm);
            com(k,9) = num2cell(ss);
            com(k,10)= num2cell(datenum(year,month,day,hh,mm,ss));
        end; clear dataS year month day hh mm ss k;
        
        % log data
        comp(in:ind,:) = com;
        clear X com in ind;
    end;
end; clear j;
comp(1,:)=[];

% kickout 'SBIT'
comp(strcmp(comp(:,1),'SBIT'),:)=[];

% sort chronologically
[~,si] = sort(cell2mat(comp(:,10)));
comp = comp(si,:);

% save
%
    save([workd 'syslog_Thethys_relaibility_' num2str(yr) '.mat'],...
        'fields','fileName','mission','syslog','comp','compHeader');
%}