% syslog_RunTime.m
% Lats modified Sep 19, 2014
% Ben Raanan

% This script calculates run time and MTBCF for each log and mission

% 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))
        
        % get mission names
        ms = fieldnames(syslog.(vh{v}));
        ms(strcmp(ms,'fault'))=[];
        
        for c = 1:length(ms)
            
            % get log names
            log = fieldnames(syslog.(vh{v}).(ms{c}));
            
            for j=1:length(log)
                
                chek = fieldnames(syslog.(vh{v}).(ms{c}).(log{j}));
                if any(strcmp(chek,'runTime'))
                    clear X
                    X = syslog.(vh{v}).(ms{c}).(log{j}).runTime;
                else
                    warning([vh{v} '/' ms{c} '/' log{j} ': NO runTime'])
                end
                
                if length(X)==2 && sum(cellfun(@isempty,X))==0
                    t   = zeros(length(X),1);
                    for k = 1:size(X,1);
                        
                        % reduce to line
                        dataS = X{k,:};
                        
                        % 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
                        
                        t(k,1)= datenum(year,month,day,hh,mm,ss);
                        
                    end; clear dataS year month day hh mm ss k;
                    
                    % log runtime in log struct
                    syslog.(vh{v}).(ms{c}).(log{j}).RUNTIME = [t(1),t(2),24*(t(2)-t(1))];
                    
                    mrt(j) = 24*(t(2)-t(1));
                    
                    % calc run-time in logCount struct
                    fl = log{j};
                    f = strcmp(syslog.(vh{v}).fault.data.logCount(:,1),fl(2:end));
                    if any(f)
                        syslog.(vh{v}).fault.data.logCount{f,3} = 24*(t(2)-t(1));
                    end; clear f fl mtbcf
                else
                    warning([vh{v} '/' ms{c} '/' log{j} ': NO runTime'])
                    
                end
            end; clear j f fl t X chek
            % calc mission run-time in missionCount struct
            fms = ms{c};
            fm = strcmp(syslog.(vh{v}).fault.data.missionCount(:,1),fms(2:end));
            if any(fm)
                syslog.(vh{v}).fault.data.missionCount{fm,3} = sum(mrt);
            end; clear fm fms mrt 
        end; clear c ms
        syslog.(vh{v}).fault.missionCountTable = array2table(syslog.(vh{v}).fault.data.missionCount,...
            'VariableNames',{'Mission','Fault_count','Run_time'});
    end; clear v
    % save
    %
    save(['~/Documents/MATLAB/MBARI/mat/syslog/syslog_' num2str(yr(q)) '_comp.mat'],...
        'filePath','mission','logName','syslog','compHeader')
    %}
end; clear q


