% MTBCF_BARPLOTS.m
clear
close all

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

load('syslog_AllVehicles_comp.mat')
d = load([workd 'syslog_Daphne_comp.mat']);
t = load([workd 'syslog_Tethys_comp.mat']);




for v=1:length(vh);
    
    multiY=[]; yrtmp = [];
    fname = fieldnames(syslogs.(vh{v}));
    fname(strcmp(fname,'nosyslog'))=[];
    
    for q=1:length(fname)
        
        yr = fname{q}; yr = str2double(yr(2:end));
        if yr>2011 && yr<2015
            tmp = syslogs.(vh{v}).(fname{q}).Fault.data.logCount(:,1:4);
            multiY = vertcat(multiY,tmp(1:end-1,:));
            yrtmp  = vertcat(yrtmp,ones(size(tmp(1:end-1,1)))*yr);
            yr=[];
        end
    end
    
    [u, ui] = unique(multiY(:,1),'stable');
    depName = u; depYr = yrtmp(ui);
    multiY_c = cell2mat(multiY(:,3));
    multiY_rt = cell2mat(multiY(:,4));
    
    for k = 1:length(u)
        f = find(strcmp(multiY(:,1),u(k)));
        depM(k,:) = sum(multiY_rt(f))/sum(multiY_c(f));
        if isinf(depM(k,:))
            depM(k,:) = sum(multiY_rt(f));
        end
    end
    depM(depM==inf)=NaN;
    
    u = unique(yrtmp,'stable');
    for k = 1:length(u)
        f = find(yrtmp==u(k));
        yrM(k,:) = sum(multiY_rt(f))/sum(multiY_c(f));
    end
    depM(depM==inf)=NaN;
    
    globM = sum(multiY_rt)/sum(multiY_c);
    
    syslogs.(vh{v}).Fault.logCount = multiY; multiY = [];
    syslogs.(vh{v}).Fault.logCountYr = yrtmp; yrtmp = [];
    syslogs.(vh{v}).Fault.depName = depName; depName= [];
    syslogs.(vh{v}).Fault.depYr = depYr; depYr =[];
    syslogs.(vh{v}).Fault.depMTBCF = depM; depM = [];
    syslogs.(vh{v}).Fault.yrMTBCF = yrM; yrM = [];
    syslogs.(vh{v}).Fault.yr = u; u = [];
    syslogs.(vh{v}).Fault.globalMTBCF = globM; globM=[];
    
    
    
    u=unique(syslogs.(vh{v}).Fault.logCount(:,1),'stable');
    
    for k=1:length(u)
        
        l = u{k};
        if any(strfind(l,'201'))
            l=l(12:end);
        end
        l(strfind(l,'_'))=' ';
        l(strfind(l,vh{v}):strfind(l,vh{v})+length(vh{v})-1)=[];
        xlab{k,1} = l;
    end
    
    figure;
    set(gcf,'Units','normalized','Position',[0 0.2 1 0.7],...
        'PaperPositionMode','auto') %,'visible','off');
    b = bar(syslogs.(vh{v}).Fault.depMTBCF);
    hold on;
    
    uyr = unique(syslogs.(vh{v}).Fault.depYr);
    for k=1:length(uyr)
        f = find(syslogs.(vh{v}).Fault.depYr==uyr(k));
        
        annual = mean(syslogs.(vh{v}).Fault.depMTBCF(f));
        
        p(k) = plot(f,ones(size(f))*annual,...
            'linewidth',4);
        lgnd{k} = [' ' num2str(uyr(k)) ' Annual MTBCF ('...
            num2str(annual,3) ' hr)'];
    end
    lg = legend(p,lgnd,'location','nw');
    
    set(gca,'xtick',1:length(syslogs.(vh{v}).Fault.depMTBCF))
    set(gca,'XtickLabel',xlab)
    rotateticklabel(gca,45);
    title([vh{v} ' - Deployment Mean Time Between Critical Failures (MTBCF)'],'fontweight','bold','fontsize',24)
    ylabel('MTBCF (hour)','fontweight','bold','fontsize',20);
    % xlabel('Deployment','fontweight','bold','fontsize',16);
    set(gca,'layer','top','fontWeight','bold','fontsize',16);
    set(lg, 'fontsize',18)
    grid on; box on; axis tight; ylim([0 120]);
    pb = get(gca, 'position');
    set(gca,'Position',[pb(1) pb(2)+0.1 pb(3) pb(4)-0.1])
    
    % print(gcf,[figd 'MTBCF_' vh{v}],'-r300','-dpng');
    % close
    clear xlab lgnd p lgnd lg
end


%% VP vs ALL

for v=1:length(vh);
    
    fname = fieldnames(syslogs.(vh{v}));
    fname(strcmp(fname,'nosyslog'))=[];
    fname(strcmp(fname,'Fault'))=[];
    runTime(v) = sum(cell2mat(syslogs.(vh{v}).Fault.logCount(:,4)));
    for q=1:length(fname)
        
        yr = fname{q}; yr = str2double(yr(2:end));
        if yr>2011 && yr<2015
            x(q) = yr;    
            stmp(v,q) = length(syslogs.(vh{v}).(fname{q}).Fault.data.compFilt(:,1));
            sVPtmp(v,q) = length(syslogs.(vh{v}).(fname{q}).Fault.data.compVp(:,1));
            sDWtmp(v,q) = length(syslogs.(vh{v}).(fname{q}).Fault.data.drop_weight(:,1));

        end
    end
    stmp(stmp==0)=[];   sVPtmp(sVPtmp==0)=[]; 
    sDWtmp(sDWtmp==0)=[]; x(x==0)=[];
    
end

stmp = sum(stmp); sVPtmp = sum(sVPtmp); sDWtmp = sum(sDWtmp); 

figure;
bar(x,stmp,0.5,'FaceColor',[0.2,0.2,0.5]); hold on;
bar(x,sVPtmp,0.5,'FaceColor',[  0    0.4470    0.7410],...
    'EdgeColor',[  0    0.4470    0.7410]);
title(['Tethys and Daphne (' num2str(sum(runTime),4) ' hours of operation)'],...
    'fontweight','bold','fontsize',20);
ylabel('Critical Failure Count','fontweight','bold','fontsize',20);
lg = legend(['Total ' num2str(sum(stmp))],['Vertical plane ' num2str(sum(sVPtmp)) ' ('...
    num2str(100*sum(sVPtmp)/sum(stmp),3) '%)'],'location','northeast');
ylim([0 280])
for j=1:length(x)
    text(x(j),stmp(j),[num2str(100*sVPtmp(j)/stmp(j),3) '%'],...
        'HorizontalAlignment','center',...
        'VerticalAlignment','bottom',...
        'fontweight','bold','fontsize',14)
end
grid on; box on;
set(gca,'layer','top','fontWeight','bold','fontsize',16);

% print(gcf,[figd 'FailCount_DT'],'-r300','-dpng');
% close
    
clear *tmp x runTime

%% comp count plot (per year per vehicle)


% for v=1:length(vh);
%     
%     fname = fieldnames(syslogs.(vh{v}));
%     
%     
%     for q=1:length(fname)
%         
%         yr = fname{q}; yr = str2double(yr(2:end));
%         if yr>2011
%             
%             mname = syslogs.(vh{v}).(fname{q}).Fault.data.logCount(1:end-1,1:2);
%             umname = unique(mname(:,1)); umcount = zeros(size(umname));
%             comp = syslogs.(vh{v}).(fname{q}).Fault.data.compFilt(:,[1,3]);
%             cflist = syslogs.(vh{v}).(fname{q}).Fault.data.compCount;
%             cflist(cell2mat(cflist(:,2))<5,:)=[];
%             
%             figure; hold on;
%             set(gcf,'Units','normalized','Position',[0 0.2 1 0.7],...
%                 'PaperPositionMode','auto')
%             set(gca,'layer','top','fontWeight','bold','fontsize',16);
%             for k = 1:length(cflist)
%                 
%                 f = find(strcmp(comp(:,1),cflist{k,1}));
%                 
%                 logsf = comp(f,2);
%                 umcount = zeros(size(umname));
%                 for c=1:numel(logsf)
%                     logtmp = logsf{c};
%                     
%                     
%                     mistmp = mname(strcmp(logtmp(2:end),mname(:,2)),1);
%                     mistmpi = find(strcmp(umname,mistmp));
%                     umcount(mistmpi) = umcount(mistmpi)+1;
%                 end
%                 
%                 
%                 p(k) = plot(1:numel(umcount),cumsum(umcount),'linewidth',2);
%                 lgnd{k} = [cflist{k,1} ' ('...
%                     num2str(cflist{k,2}) ')'];
%             end
%             
%             for k=1:length(umname)
%                 l = umname{k};
%                 if any(strfind(l,'201'))
%                     l=l(12:end);
%                 end
%                 l(strfind(l,'_'))=' ';
%                 l(strfind(l,vh{v}):strfind(l,vh{v})+length(vh{v})-1)=[];
%                 xlab{k,1} = l;
%             end
%             
%             set(gca,'xtick',1:length(umname))
%             set(gca,'XtickLabel',xlab)
%             rotateticklabel(gca,45);
%             lg = legend(p,lgnd,'location','nw');
%             
%             
%             title([vh{v} ' - Cumulative Component Failure ' num2str(yr) ],'fontweight','bold','fontsize',24)
%             ylabel('Failure Count','fontweight','bold','fontsize',20);
%             set(lg, 'fontsize',18,'Interpreter', 'none');
%             grid on; box on;
%             pb = get(gca, 'position');
%             set(gca,'Position',[pb(1) pb(2)+0.1 pb(3) pb(4)-0.1])
%             
%             clear xlab lgnd p lgnd
%         end
%     end
% end







