% syslog_Thethys_relaibility_readin.m
% Lats modified Sep 13, 2014
% Ben Raanan

% Wrote this script to read-in txt files from grep search results to MATLAB
% and filter results by component

clear all;

yr=2013;

outf = ['~/Documents/MATLAB/MBARI/mat/syslog/' num2str(yr) '/Tethys/'];
filename=[outf 'Thethys_relaibility_' num2str(yr) '.txt'];

% read-in .txt
% impLog=importdata(filename);

% edit folder path
impLog.textdata(1,:)=[];
fName = cell(length(impLog.textdata(2:end,1)),1);
fields   = cell(length(impLog.textdata(2:end,1)),1);
mission  = cell(length(impLog.textdata(2:end,1)),1);

formatSpec = '%s';
for j=1:length(impLog.textdata(:,1))
    
    
    tmp = char(impLog.textdata(j,1));
    slash = strfind(tmp,'/');
    pl = strfind(tmp,'/900739');
    fName{j,1} = tmp(pl:end);
    fields{j,1}   = ['D' tmp(end-21:end-7)];
    mission{j,1} = tmp(slash(6)+1:slash(7)-1);
    
    syslog.(fields{j}).path      = fName{j};
    syslog.(fields{j}).failCount = impLog.data(j,1);
    syslog.(fields{j}).runTime   = impLog.data(j,2);
    syslog.(fields{j}).MTBCF     = impLog.data(j,3);
    
    
    % readin syslog to struct
    fileID = fopen(['/Volumes' fName{j,1}]);
    txt = textscan(fileID,formatSpec,'Delimiter', '\n');
    txt = txt{1,1};
    
    % index CRITICAL messages and extract them to struct
    ci = (not(cellfun('isempty', strfind(txt,'CRITICAL'))));
    syslog.(fields{j}).CRITICAL = txt(ci,1);
    fclose(fileID); clear txt ci fileID
    
    %     copyfile(['/Volumes' fileName{j,1}],[outf mission{j,1} '_' fields{j,1} '.syslog']);
    
end; clear j tmp;

% save
%
save([outf 'syslog_Thethys_relaibility_readin_' num2str(yr) '.mat'],...
    'fields','fileName','mission','syslog');
%}