% syslog_CRITICAL_eliminateRedundant.m
% Lats modified June 16, 2014
% Ben Raanan

% This script eliminates redundency (mostly) of relevant CRITICAL messeges
% from syslogs and sorts data chronologically

clear all;

workd='~/Documents/MATLAB/MBARI/mat/';

% year of interest
yr=2010:2014;

for n=1:length(yr)
    % load
    load([workd num2str(yr(n)) '-int-CRITICAL.mat']);
    cName = {'MassServo','ElevatorServo','BuoyancyServo','VerticalControl','ThrusterServo','DropWeight'};
    
    % initialize loop
    compFilt=num2cell(NaN(length(impLog),9));
    pathFilt=num2cell(NaN(size(impLog)));
    timeFilt=NaN(size(impLog));
    
    iFilt=NaN(size(impLog));
    for j=1:length(cName)
        
        % reduce to single component (cName(j))
        f=strncmp(char(cName(j)),comp(:,1),4);
        cComp=comp(f,:);
        timeComp=timeLog(f);
        pathComp=path(f);
        
        % eliminate messages from 'latest|Lab_Test|Tank_Test|Tow|BatteryDischarge' folders (redundant|irrelevant)
        latest=NaN(size(cComp,1));
        for k=1:size(cComp,1)
            
            % locate undesired keywords in path
            sf1=strfind(char(cComp(k,2)),'latest');
            sf2=strfind(char(cComp(k,2)),'Lab');
            sf3=strfind(char(cComp(k,2)),'Tank');
            sf4=strfind(char(cComp(k,2)),'Tow');
            sf5=strfind(char(cComp(k,2)),'Battery');
            
            % flag redundant/irrelevant paths
            if isempty(sf1)==1 && isempty(sf2)==1 &&...
                    isempty(sf3)==1 && isempty(sf4)==1 && isempty(sf5)==1
                latest(k)=1;
            else
                latest(k)=0;
                warning(['Folder rejected: ' char(cComp(k,2))])
            end
        end; clear k sf*;
        cComp=cComp(latest==1,:);
        timeComp=timeComp(latest==1);
        pathComp=pathComp(latest==1);
        
        % eliminate redundant messages from same folder
        redun=NaN(size(cComp,1)); c=1;
        while c<=size(cComp,1)
            % extract folder path
            s=char(cComp(c,2));
            % flag messages with same path
            ind=find(strcmp(s,cComp(:,2))==1);
            
            % flag recurring messages (less than 16 min between message)
            tsp=timeComp(ind);
            dti(1)=1;
            dti(2:length(ind))=diff(tsp)>=(45/(60*24));
            
            % log data
            redun(ind)=dti;
            
            % skip to next folder
            c=max(ind)+1;
            clear ind dti s tsp;
        end; clear c;
        cComp=cComp(redun==1,:);
        timeComp=timeComp(redun==1);
        pathComp=pathComp(redun==1);
        
        % compile component data
        if isempty(cComp)~=1
            % update index
            a=find(isnan(iFilt)==1,1);
            % log data
            compFilt(a:a+size(cComp,1)-1,:)=cComp;
            timeFilt(a:a+size(cComp,1)-1,:)=timeComp;
            pathFilt(a:a+size(cComp,1)-1,:)=pathComp;
            % update index vector
            iFilt(a:a+size(cComp,1)-1)=1;
        end
        
    end; clear j f latest redun a cComp timeComp pathComp;
    
    % sort chronologically
    [timeFilt,si]=sort(timeFilt(iFilt==1,:));
    compFilt=compFilt(si,:);
    pathFilt=pathFilt(si,:);
    clear si iFilt;
    
    % save
    %
    save([workd num2str(yr(n)) '-int-CRITICAL.mat'],...
        'cName','impLog','timeLog','path','comp','compHeader',...
        'timeFilt','compFilt','pathFilt');
    %}
end; clear n;