% syslog_Thethys_relaibility_eliminateRedundant.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'};

% eliminate redundant messages from same mission path


% reduce to unique
logCount     = unique(fields);
missionCount = unique(mission);

% eliminate redundant critical messages per log
neo=1;
for c=1:size(logCount,1)
    
    % index log entries
    ind = find(ismember(comp(:,3),logCount{c}));
    
    % 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; clear c neo ind u ui;


% eliminate redundant critical messages per mission
perMiss = {'DropWeight','Default:Iridium:Read_Iridium:A_Timeout:B',...
    'Environmental'};
for c=1:size(missionCount,1)
    
    % index mission entries
    ind = find(ismember(compFilt(:,2),missionCount{c}));
    
    % index unique componant faults [stable: keep order of origin (chrono)]
    for j=1:length(perMiss)
        f = find(strcmp(perMiss(j),compFilt(ind,1)));
        
        if length(f)>1
            compFilt(ind(f(2:end)),:)=[];
        end
    end; clear j ind f;
    
end; clear c


% count critical fault per mission
for c=1:length(missionCount)
    
    missionCount{c,2} = sum(strcmp(missionCount(c,1),compFilt(:,2)));
    
end; clear c;

% count critical fault per mission
for c=1:length(logCount)
    
    logCount{c,2} = sum(strcmp(logCount(c,1),compFilt(:,3)));
    
end; clear c;

% count critical fault per componant
compCount = unique(compFilt(:,1));
for c=1:length(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;
for c=1:length(compFilt)
    
    if sum(strcmp(compFilt{c,1},vpc))~=0
        compVp(j,:) = compFilt(c,:);
        j=j+1;
    end
end; clear c j;

% save
%
    save([workd 'syslog_Thethys_relaibility_' num2str(yr) '_nonRedundant.mat'],...
        'fields','fileName','mission','syslog','comp','compHeader',...
        'compCount','compFilt','compVp','logCount','missionCount','vpc',...
        'perMiss');
%}
