%{
check_timestamps_CAPRICORN_2016
Scans son, mot and lic files for data gaps or interruptions in
the 10Hz timeseries

%}

% Initialize run parameters
close all;
fclose all;
clear;

% Set cruise parameters
% cruise ID and paths
cruise = 'CAPRICORN_2016';   % Cruise name
ship = 'Investigator';                % Research vessel
yyyy = '2016';

% cruise data root directory - edit as required for your file system and OS
% note: windows uses the '\' separator and mac-unix uses '/'
data_drive = 'D:\Data\';
path_raw_data_flux = fullfile(data_drive,cruise,ship,'flux','Raw');
path_save_data = fullfile(data_drive,cruise,ship,'flux','Check_Timestamps');
mkdir(path_save_data);

% start and end julian dates for the main flux loop, 16:41 for entire cruise
stjd = 74;
endjd = 74;

% arrays to keep track of bad timestamps
mot_bad = [];
son_bad = [];
lic_bad = [];
mot_Npnts = [];
son_Npnts = [];
lic_Npnts = [];

for ddd=stjd:endjd  % iterate over days
    jd = sprintf('%03i',ddd);
    disp(['Running Day ',jd]);
    for hhh=00:23;   % iterate over hours
        hr = sprintf('%02i',hhh);

        % read mot and interp to 10 Hz
        dfl = fullfile(path_raw_data_flux,[yyyy(3:4),jd],['mot0',yyyy(3:4),jd,hr,'_raw.txt']);
        if exist(dfl,'file')==0
            continue  % if none continue to next hour
        end;
        [badmot,Nmot] = scan_motion_CAPRICORN_2016(dfl,ddd,hhh);
        mot_Npnts = [mot_Npnts;[ddd,hhh,Nmot]];
        if ~isempty(badmot)
            mot_bad = [mot_bad;badmot];
        end;

        % read son and interp to 10 Hz
        dfl = fullfile(path_raw_data_flux,[yyyy(3:4),jd],['son0',yyyy(3:4),jd,hr,'_raw.txt']);
        if exist(dfl,'file')==0
            continue  % if none continue to next hour
        end;
        [badson,Nson] = scan_sonic_CAPRICORN_2016(dfl,ddd,hhh);
        son_Npnts = [son_Npnts;[ddd,hhh,Nson]];
        if ~isempty(badson)
            son_bad = [son_bad;badson];
        end;

        % read licor
        dfl = fullfile(path_raw_data_flux,[yyyy(3:4),jd],['lic0',yyyy(3:4),jd,hr,'_raw.txt']);
        [badlic,Nlic] = scan_licor_CAPRICORN_2016(dfl,ddd,hhh);
        lic_Npnts = [lic_Npnts;[ddd,hhh,Nlic]];
        if ~isempty(badlic)
            lic_bad = [lic_bad;badlic];
        end;
    end;
end;

% plot timeseries of data gaps
figure; plot(mot_bad(:,1),mot_bad(:,3),'ro',son_bad(:,1),son_bad(:,3),'bo',lic_bad(:,1),lic_bad(:,3),'go');
grid; ylabel('Time Gap (s)'); xlabel('DOY 2015'); title('10 Hz Data Gaps');
legend('motion','sonic','licor');
savefig(fullfile(path_save_data,'Data_Gaps_plot.fig'));

% plot timeseries of file lengths
jdmot = mot_Npnts(:,1) + mot_Npnts(:,2)/24;
jdson = son_Npnts(:,1) + son_Npnts(:,2)/24;
jdlic = lic_Npnts(:,1) + lic_Npnts(:,2)/24;
figure; plot(jdmot,36000-mot_Npnts(:,3),'r.',jdson,36000-son_Npnts(:,3),'b.',jdlic,36000-lic_Npnts(:,3),'g.');
grid; ylabel('Missing Points per Hr'); xlabel('DOY 2015'); title('File Length Variability');
legend('motion','sonic','licor');
savefig(fullfile(path_save_data,'File_Length_plot.fig'));

% compute numnber of hours of files affected by gaps
jdhr_son = floor(son_bad(:,1)*24)/24;
jdhr_lic = floor(lic_bad(:,1)*24)/24;
jdhr_mot = floor(mot_bad(:,1)*24)/24;
hrs_bad_mot = length(unique(jdhr_mot));
hrs_bad_lic = length(unique(jdhr_lic));
hrs_bad_son = length(unique(jdhr_son));

% save mat files
save(fullfile(path_save_data,'mot_Npnts'),'mot_Npnts');
save(fullfile(path_save_data,'son_Npnts'),'son_Npnts');
save(fullfile(path_save_data,'lic_Npnts'),'lic_Npnts');
save(fullfile(path_save_data,'Scan_For_Bad_Timestamps.mat'));


