% syslog_TBCF_hist.m
% Last modified Sep 22, 2014
% Ben Raanan

clear all;
workd = '~/Documents/MATLAB/MBARI/mat/syslog/';
fname = which('syslog_TBCF_hist.m');

y = {'y2010','y2011','y2012','y2013','y2014'};
vh={'Tethys','Daphne'};

load([workd 'syslog_All_comp.mat']);


% Compute time between critical failures 
%--------------------------------------------------------------------------
tbcf.Tethys = 0;
tbcf.Daphne = 0;
for q=1:length(y)
    
    for v=1:length(fieldnames(syslogs.(y{q})))
        
        tmp = cell2mat(syslogs.(y{q}).(vh{v}).fault.logCount(:,5));
        tmp = tmp(~isnan(tmp));
        
        if ~isempty(tmp)
            in = length(tbcf.(vh{v}))+1;
            out = length(tmp);
            ind = (in:in+out-1)';
            
            tbcf.(vh{v})(ind,1) = tmp;
            
            
        end
    end
end

tbcf.Tethys(1)=[];
tbcf.Daphne(1)=[];


% plot
figure;
set(gcf,'Units','normalized','Position',[0 0.4 1 0.7],...
    'PaperPositionMode','auto');

s1=subplot(1,2,1);
hist(tbcf.Tethys(tbcf.Tethys<100),100)
title('Tethys','fontweight','bold','fontsize',18)
ylabel('Count','fontweight','bold','fontsize',14)
xlabel('MTBCF (Hours)',...
    'fontweight','bold','fontsize',14)
grid on;
ps1=get(s1, 'Position');



s2=subplot(1,2,2);
hist(tbcf.Daphne(tbcf.Daphne<100),100)
title('Daphne','fontweight','bold','fontsize',18)
ylabel('Count','fontweight','bold','fontsize',14)
xlabel('MTBCF (Hours)',...
    'fontweight','bold','fontsize',14)
grid on;
ps2=get(s2, 'Position');


mtit('LRAUV reliability bathtub curve','fontweight','bold','fontsize',28)
set(s1,'position',[ps1(1) ps1(2)-0.015 ps1(3) ps1(4)-0.02]) % [left bottom width height]
set(s2,'position',[ps2(1) ps2(2)-0.015 ps2(3) ps2(4)-0.02]) % [left bottom width height]
uicontrol('Style', 'text','String', [fname ' - ' datestr(clock)],...
    'Units','normalized',...
    'Position', [0 0 1 0.025]);
% save
%{
saveas(gcf,['~/Desktop/LRAUV_MTBCF_bathCurve'] ,'png');
%}




% Compute time between log start to first CRITICAL
%--------------------------------------------------------------------------
tbcf.Tethys = 0;
tbcf.Daphne = 0;
for q=1:length(y)
    
    for v=1:length(fieldnames(syslogs.(y{q})))
        [m,~,mi] = unique( syslogs.(y{q}).(vh{v}).fault.compFilt(:,2) );
        u = unique(mi);
        
        for j=1:length(u)
            
            f = find(mi==u(j));
            
            [l,li,~] = unique( syslogs.(y{q}).(vh{v}).fault.compFilt(f,3),'stable' );
            
            fl = f(li);
            
            for c=1:length(fl)
                
                chek = fieldnames(syslogs.(y{q}).(vh{v}).(m{j}).(l{c}));
                
                if sum(ismember(chek,'RUNTIME'))~=0
                    start = syslogs.(y{q}).(vh{v}).(m{j}).(l{c}).RUNTIME(1);
                    ftime = cell2mat(syslogs.(y{q}).(vh{v}).fault.compFilt(fl(c),10));
                    
                    tmp = (ftime-start)*24;
                    
                    if ~isempty(tmp)
                        in = length(tbcf.(vh{v}))+1;
                        tbcf.(vh{v})(in,1) = tmp;
                    end
                end
            end
        end
    end
end
tbcf.Tethys(1)=[];
tbcf.Daphne(1)=[];


% plot
figure;
set(gcf,'Units','normalized','Position',[0 0.4 1 0.7],...
    'PaperPositionMode','auto');

s1=subplot(1,2,1);
hist(tbcf.Tethys(tbcf.Tethys<20),200)
title('Tethys','fontweight','bold','fontsize',18)
ylabel('Count','fontweight','bold','fontsize',14)
xlabel('Hours from log start to first CRITICAL',...
    'fontweight','bold','fontsize',14)
grid on;
ps1=get(s1, 'Position');



s2=subplot(1,2,2);
hist(tbcf.Daphne(tbcf.Daphne<20),200)
title('Daphne','fontweight','bold','fontsize',18)
ylabel('Count','fontweight','bold','fontsize',14)
xlabel('Hours from log start to first CRITICAL',...
    'fontweight','bold','fontsize',14)
grid on;
ps2=get(s2, 'Position');


mtit('LRAUV reliability bathtub curve','fontweight','bold','fontsize',28)
set(s1,'position',[ps1(1) ps1(2)-0.015 ps1(3) ps1(4)-0.02]) % [left bottom width height]
set(s2,'position',[ps2(1) ps2(2)-0.015 ps2(3) ps2(4)-0.02]) % [left bottom width height]
uicontrol('Style', 'text','String', [fname ' - ' datestr(clock)],...
    'Units','normalized',...
    'Position', [0 0 1 0.025]);
% save
%{
saveas(gcf,['~/Desktop/LRAUV_startTOcrit_bathCurve'] ,'png');
%}