% syslog_CRITICAL_readin.m
% Lats modified June 15, 2014 
% Ben Raanan

clear all;

yr=2010:2014;
% components of interest
cName = {'MassServo','ElevatorServo','BouyancyServo','VerticalControl','ThrusterServo','DropWeight'};
compHeader={'Component','Path','Year','Month','Day','HH','MM','SS','Datenum'};

for n=1:length(yr)
    % read-in .syslog file
    filename=['~/Documents/MBARI/project/syslog/' num2str(yr(n)) '-int-CRITICAL.syslog'];
    impLog=importdata(filename); %,delimiterIn,headerlinesIn);
    
    X = impLog;
    
    % initialize loop
    timeLog=NaN(size(impLog));
    path=num2cell(zeros(size(impLog)));
    comp=num2cell(zeros(length(impLog),9));
    for k = 1:size(X,1);
        
        % reduce to line
        dataS = X{k,:};
        
        % flag ':'
        indx = strfind(dataS,':');
        % extract path
        path(k,:)=cellstr(dataS(1:indx(1)-7));
        
        % focus on date/time
        datas=dataS(indx(1)+1:end);
        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));
        % convert to date number
        timeLog(k,:) = datenum(year,month,day,hh,mm,ss);
        
        % categorize by component
        cn=[];c=1;
        while isempty(cn)==1 && c<=length(cName)
            cn = strfind(dataS,char(cName(c)));
            c=c+1;
        end
        % log data
        comp(k,1) = cName(c-1);
        comp(k,2) = path(k,1);
        comp(k,3) = num2cell(year);
        comp(k,4) = num2cell(month);
        comp(k,5) = num2cell(day);
        comp(k,6) = num2cell(hh);
        comp(k,7) = num2cell(mm);
        comp(k,8) = num2cell(ss);
        comp(k,9) = num2cell(timeLog(k));
    end
    
    % save
    %
    save(['~/Documents/MATLAB/MBARI/mat/' num2str(yr(n)) '-int-CRITICAL.mat'],...
        'cName','impLog','timeLog','path','comp','compHeader');
    %}
end