% syslog_RunTime_LRAUV.m
% Lats modified Dec 31, 2014
% Ben Raanan

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

clear

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

global vh workd

for v=1:numel(vh)
    
    vhc = vh{v};
    % clearvars -except workd yr vh q v vhc syslog filePath logName compHeader
    load([workd 'syslog_' vhc '_comp.mat']);
    fname = fieldnames(syslog);
    fname(strcmp(fname,'nosyslog'))=[];
    
    for q=1:numel(fname)
        
        yfield = fname{q};
        
        
        % get mission names
        ms = fieldnames(syslog.(yfield));
        ms(strcmp(ms,'Fault'))=[];
        
        for c = 1:length(ms)
            
            % get log names
            log = fieldnames(syslog.(yfield).(ms{c}));
            log(strcmp(log,'dlist'))=[];
            
            for j=1:length(log)
                
                chek = fieldnames(syslog.(yfield).(ms{c}).(log{j}));
                if any(strcmp(chek,'runTime'))
                    clear X
                    X = syslog.(yfield).(ms{c}).(log{j}).runTime;
                else
                    warning([vhc '/' yfield '/' 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.(yfield).(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.(yfield).Fault.data.logCount(:,logCi(q)),fl(2:end));
                    if any(f)
                        syslog.(yfield).Fault.data.logCount{f,logCi(q)+2} = 24*(t(2)-t(1));
                    end; clear f fl mtbcf
                else
                    warning([vhc '/' yfield '/' ms{c} '/' log{j} ': NO runTime'])
                    
                end
            end; clear j f fl t X chek mrt
            
            
            % calc mission run-time in missionCount struct
            %{
            fms = ms{c};
            fm = strcmp(syslog.(yfeild).Fault.data.missionCount(:,1),fms(2:end));
            if any(fm)
                syslog.(yfeild).Fault.data.missionCount{fm,3} = sum(mrt);
            end; clear fm fms mrt
            %}
            
            if isfield(syslog.(yfield).Fault.data,'missionCount')
                syslog.(yfield).Fault.missionCountTable = array2table(syslog.(yfield).Fault.data.missionCount,...
                    'VariableNames',{'Mission','Fault_count'});
            end
        end; clear c ms
        
    end; clear q
    % save
    %
    save([workd 'syslog_' vhc '_comp.mat'],...
        'filePath','logName','syslog','compHeader','logCi')
    %}
end; clear v


