% syslog_eliminateRedundant.m
% Lats modified Sep 16, 2014
% Ben Raanan

% This script eliminates redundent critical faults and counts critical 
% errors by log, mission and component

% 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']);
    
    for v = 1:length(fieldnames(syslog))
        
        clear comp
        comp = syslog.(vh{v}).fault.data.comp;
        
        % reduce missions and logs to unique
        clear logCount missionCount
        logCount     = unique(logName.(vh{v})(:,1));
        missionCount = unique(logName.(vh{v})(:,2));
        
        % eliminate redundant critical messages per log
        neo=1; clear compFilt
        for c=1:size(logCount,1)
            
            % index log entries
            ind = find(ismember(comp(:,3),['D' logCount{c}]));
            
            if ~isempty(ind)
            % index unique componant faults [stable: keep order of origin (chrono)]
            [u, ui]   = unique(comp(ind,1),'stable');
            
            % concatenate
            compFilt(neo:neo+size(u,1)-1,:) = [u comp(ind(ui),2:end)];
            neo = neo+size(u,1);
            end
        end; clear c neo ind u ui;
        
        
        % eliminate redundant critical messages per mission
        perMiss = {'DropWeight','Default:Iridium:Read_Iridium:A_Timeout:B',...
            'Default:Iridium:A_Timeout:B','Environmental'};
        
        for c=1:numel(missionCount)
            
            % index unique componant faults [stable: keep order of origin (chrono)]
            for j=1:numel(perMiss)
                
                % index mission entries
                ind = find(ismember(compFilt(:,2),['M' missionCount{c}]));
                
                f = find(strcmp(perMiss(j),compFilt(ind,1)));
                
                if length(f)>1
                    compFilt(ind(f(2:end)),:)=[]; % keep only the first entry
                end
            end; clear j ind f;
        
            % insert time constraints for recurring malfunction!
        end; clear c
        
        
       
        % count critical fault per mission
        for c=1:numel(missionCount)
            
            missionCount{c,2} = sum(strcmp(['M' missionCount{c,1}],compFilt(:,2)));
            
        end; clear c;
        
       
        
        % count critical fault per log
        for c=1:numel(logCount)
            
            logCount{c,2} = sum(strcmp(['D' logCount{c,1}],compFilt(:,3)));
            
        end; clear c;
        
               
         
        % count critical fault per componant
        compCount = unique(compFilt(:,1));
        for c=1:numel(compCount)
            
            compCount{c,2} = sum(strcmp(compCount(c,1),compFilt(:,1)));
            
        end;
        
        [~,si] = sort(cell2mat(compCount(:,2)),'descend');
        compCount = compCount(si,:);
        clear c si;
        
        % flag critical faults asociated w/ vertical plane flight
        vpc = {'MassServo','ElevatorServo','BuoyancyServo','VerticalControl','ThrusterServo','DropWeight'};
        j=1; compVp = cell(j,12);
        for c=1:size(compFilt,1)
            
            if any(strcmp(compFilt{c,1},vpc))
                compVp(j,:) = compFilt(c,:);
                j=j+1;
            end
        end; clear c j;
      
    syslog.(vh{v}).fault.data.compFilt = compFilt;
    syslog.(vh{v}).fault.compFiltTable = array2table(compFilt,...
        'VariableNames', compHeader);
    
    syslog.(vh{v}).fault.data.compVp = compVp;
    syslog.(vh{v}).fault.compVpTable = array2table(compVp,...
        'VariableNames', compHeader);
    
    syslog.(vh{v}).fault.data.compCount = compCount;
    syslog.(vh{v}).fault.compCount = array2table(compCount,...
        'VariableNames', {'Componant','Fault_count'});
    
    syslog.(vh{v}).fault.data.missionCount = missionCount;
    syslog.(vh{v}).fault.data.logCount = logCount;
    
    
    
    
    clearvars -except workd yr vh q v syslog filePath mission logName compHeader
    end
    
    
    % save
    %
    save(['~/Documents/MATLAB/MBARI/mat/syslog/syslog_' num2str(yr(q)) '_comp.mat'],...
        'filePath','mission','logName','syslog','compHeader')
    %}
end
