%gps_correct
clear;


SN = 'BOG3'
raw = '2021NOV02';
fp_root = '2036-1-13T0-24-0'

if(1==0) %calc flbb offset
    %$02-Nov-2021 16:34:03.563 FLBB Message 11/02/21,21:26:07,1303,246,1751,2275,394,561
    prev_date = '02-Nov-2021';
    prev_time = '16:34:03.563';
    CPU_t = datenum(strcat(prev_date, 'T', prev_time));
    FLBB_date = '02-Nov-2021';
    FLBB_time = '21:26:07';
    FLBB_t = datenum(strcat(FLBB_date, 'T', FLBB_time));
    diff_t = CPU_t-FLBB_t;
else
    diff_t = -0.2028;
    FLBB_date = '03-Nov-2021';
    FLBB_time = '22:25:34';
    FLBB_t = datenum(strcat(FLBB_date, 'T', FLBB_time));
end

FLBB_t = FLBB_t+diff_t;
CPU_t = datenum('13-Jan-2036T00:27:16.840');
t_offset = CPU_t-FLBB_t;

%dateF = strsplit(datestr(FLBB_t),' ');
%prev_date = dateF{1};
%prev_time = dateF{2};

path =    sprintf('../%s/%s',SN,raw);
inFile = sprintf('%s/raw_rebuild/%s.eng',path,fp_root);
outfile = sprintf('%s/corrected/%s.eng',path,fp_root);

inFID = fopen(inFile);
outFID = fopen(outfile,'w');

%get the header line
inString = fgets(inFID);
fprintf(outFID,'%s',inString);

%get second line and load up time
%inString = fgets(inFID);


%JUST USE THE LAST FUCKING TIME FROM THE FLBB UNTIL YOU GET A NEW FLBB!!!!!!!!!!!!!!

while ~feof(inFID)
    inString = fgets(inFID);
    %fprintf('IN: %s',inString);
    inFields = strsplit(inString, '\t', 'CollapseDelimiters', false);
    %disp(inString);
    %get date, time, and state
%     if( strfind(inString,'FLBB Message') )
%         %inFields = strsplit(inString, '\t', 'CollapseDelimiters', false);
%         fstr = strsplit(inFields{32},',');
%         tstr = sprintf('%s %s',fstr{1},fstr{2});
%         formatIn='mm/dd/yy HH:MM:SS';
%         FLBB_t = datenum(tstr,formatIn) + diff_t;
%         dateF = strsplit(datestr(FLBB_t),' ');
%         prev_date = dateF{1};
%         prev_time = dateF{2};
%         inFields{2} = prev_date;
%         inFields{3} = prev_time;
%     else
      t = datenum(strcat(inFields{2}, 'T', inFields{3})) - t_offset;
      dateF = strsplit(datestr(t),' ');
      inFields{2} = dateF{1};
      inFields{3} = dateF{2};

    
    
    %rebuild string
    outstr = sprintf('%s',inFields{1});
    for i=2:size(inFields,2)
        outstr = sprintf('%s\t%s',outstr,inFields{i});
    end
    
    
    
    %fprintf('OT: %s',outstr);
    fprintf(outFID,'%s',outstr);
    
    %pause
    
end
fclose(outFID);
fclose(inFID);

