% syslog_stats.m
% Lats modified Sep 20, 2014
% Ben Raanan

% This script calculates annual TBCF and MTBCF in logs and also computes  
% stats for vertical plain critical errors 

% clear 

% workd = '~/Documents/MATLAB/MBARI/mat/syslog/';
% yr=2010:2014;
% vh={'Tethys','Daphne','Makai'};

global yr vh workd 


for q=1:length(yr);
    
    load([workd 'syslog_' num2str(yr(q)) '_comp.mat']);
    %{
    if yr(q)==2012 & v==1
        syslog.Tethys.fault.missionCount{11,2} = 1;
        syslog.Tethys.fault.missionCount{11,3} = 63.318;
        syslog.Tethys.fault.missionCount{11,5} = '* Deployment 63 saw a lost vehicle for 2 weeks. Data was modified to reflect deployment up until the catastrophic failure';
    end
    %}
    for v=1:length(fieldnames(syslog))
        
        % snip totals if already existent 
        if strcmp(syslog.(vh{v}).fault.data.logCount(end,1),'Total:')==1
            syslog.(vh{v}).fault.data.logCount(end,:)=[];
        end
        
        % replace empty cells w/ 0
        in = cellfun(@isempty,syslog.(vh{v}).fault.data.logCount);
        syslog.(vh{v}).fault.data.logCount(in) = {0};
        clear in
        
        % get log record length
        fl = length(syslog.(vh{v}).fault.data.logCount(:,1));
        % convert cell to mat 
        lMat = cell2mat(syslog.(vh{v}).fault.data.logCount(:,2:3));
        
        
        % calc TBCF and MTBCF in logs
        %------------------------------------------------------------------
        tbcf    = NaN(length(lMat),1);
        tbcf(1) = lMat(1,2);
        
        % calc TBCF
        for c=2:length(lMat)
            if lMat(c-1,1)==0
                tbcf(c) = tbcf(c-1)+lMat(c,2);
            else
                tbcf(c) = lMat(c,2);
            end;
        end; clear c; 
        lMat(:,3)=tbcf;
        
        % calc MTBCF
        mtbcf = zeros(length(lMat),1);
        mtbcf(lMat(:,1)~=0,:) = tbcf(lMat(:,1)~=0)./lMat(lMat(:,1)~=0,1); 
        mtbcf(mtbcf==0) = NaN;
        lMat(:,4)=mtbcf;
        
        % calc anual stats
        anlogCritical = nansum(lMat(:,1));
        anlogRunTime  = nansum(lMat(:,2));
        anlogMTBCF    = nanmean(lMat(:,4));
        
        % concat
        clMat(:,1) = syslog.(vh{v}).fault.data.logCount(:,1);
        clMat(:,2:5) = num2cell(lMat);
        
        % log in struct
        syslog.(vh{v}).fault.data.logCount = clMat;
        syslog.(vh{v}).fault.data.logCount(fl+1,:) = {'Total:',anlogCritical, anlogRunTime,'' ,anlogMTBCF};
        syslog.(vh{v}).fault.data.logCountHeader   = {'Log_name','Critical_error_count','Log_runtime', 'TBCF','MTBCF'};
        syslog.(vh{v}).fault.logCountTable = array2table(syslog.(vh{v}).fault.data.logCount,...
            'VariableNames',syslog.(vh{v}).fault.data.logCountHeader);
        clear fl lMat tbcf mtbcf anlogCritical anlogRunTime anlogMTBCF clMat
        %------------------------------------------------------------------
        
        
        % Vertical Plane
        %------------------------------------------------------------------
        if strcmp(syslog.(vh{v}).fault.data.logCount(end,1),'Total:')==1
            in = length(syslog.(vh{v}).fault.data.logCount)-1;
        end
        
        logs = syslog.(vh{v}).fault.data.logCount(1:in,1);
        
        vpl = unique(syslog.(vh{v}).fault.data.compVp(:,3));
        for c=1:length(logs)
        vpli(c,1) = ismember(['D' logs{c}],vpl);
        end; clear c

        logs(:,2) = num2cell(zeros(size(logs)));
        logs(:,2) = num2cell(double(vpli));
        logs(:,3) = syslog.(vh{v}).fault.data.logCount(1:end-1,3);
        
        lg = cell2mat(logs(:,2:3));
        
        % calc TBCF and MTBCF in logs
        %------------------------------------------------------------------
        tbcf    = NaN(length(lg),1);
        tbcf(1) = lg(1,2);
        
        % calc TBCF
        for c=2:length(lg)
            if lg(c-1,1)==0
                tbcf(c) = tbcf(c-1)+lg(c,2);
            else
                tbcf(c) = lg(c,2);
            end;
        end; clear c; 
        lg(:,3)=tbcf;
        
        % calc MTBCF
        mtbcf = zeros(length(lg),1);
        mtbcf(lg(:,1)~=0,:) = tbcf(lg(:,1)~=0)./lg(lg(:,1)~=0,1); 
        mtbcf(mtbcf==0) = NaN;
        lg(:,4)=mtbcf;
        
        % calc anual stats
        anlogCritical = nansum(lg(:,1));
        anlogRunTime  = nansum(lg(:,2));
        anlogMTBCF    = nanmean(lg(:,4));
        
        % concat
        clg = logs;
        clg(:,4:5) = num2cell(lg(:,3:4));
        
        % log in struct
        syslog.(vh{v}).fault.data.logCountVerticalPlane = clg;
        syslog.(vh{v}).fault.data.logCountVerticalPlane(end+1,:) = {'Total:',anlogCritical, anlogRunTime,'' ,anlogMTBCF};
        syslog.(vh{v}).fault.logCountVerticalPlaneTable = array2table(syslog.(vh{v}).fault.data.logCountVerticalPlane,...
            'VariableNames',syslog.(vh{v}).fault.data.logCountHeader);
        clear logs vpl vpli lg clg tbcf mtbcf anlogCritical anlogRunTime anlogMTBCF
        
        % gather anual stats per mission
        %{
        %------------------------------------------------------------------
        fm = length(syslog.(vh{v}).fault.missionCount(:,1));
        
        mMat = cell2mat(syslog.(vh{v}).fault.missionCount(:,2:3));
        
        
        anualCritical = nansum(mMat(:,1));
        anualRunTime  = nansum(mMat(:,2));
        anualMTBCF    = nanmean(mMat(:,2));
        
        syslog.(vh{v}).fault.missionCount(fm+1,:) = {'Total:',anualCritical, anualRunTime, anualMTBCF};
        clear fm mMat ind anualCritical anualRunTime anualMTBCF
        %------------------------------------------------------------------
        
        %         anualVp(v,:) = sum(cell2mat(syslog.(vh{v}).fault.compVp(:,2)));
        %}
        % save
        %
        save(['~/Documents/MATLAB/MBARI/mat/syslog/syslog_' num2str(yr(q)) '_comp.mat'],...
            'filePath','mission','logName','syslog','compHeader')
        %}
    end
end


