% b18_concat_OOT_results.m
% concatenates OOT jpg plots for entire cruise into single directories
% so plots can be merged onto movie files for easier review

%% initialize
clear
close all
warning('off','MATLAB:MKDIR:DirectoryExists');

%% paths
root_data_path = '/Users/byronb/Raw_Data_Files.noindex/MOSAIC_2019';
path_prog = fullfile(root_data_path,'_matlab');
path_oot = fullfile(root_data_path,'_processed','metcity_flux_v3','flux_merged_OOT_v2','data_out');
path_hs = fullfile(root_data_path,'_processed','metcity_flux_v3','flux_merged_OOT_v2','plots_hs');
path_mom = fullfile(root_data_path,'_processed','metcity_flux_v3','flux_merged_OOT_v2','plots_mom');
path_co2 = fullfile(root_data_path,'_processed','metcity_flux_v3','flux_merged_OOT_v2','plots_co2');
path_ch4 = fullfile(root_data_path,'_processed','metcity_flux_v3','flux_merged_OOT_v2','plots_ch4');
mkdir(path_hs); mkdir(path_mom); mkdir(path_co2); mkdir(path_ch4);

%% set matlab script path
cd(path_prog);
addpath(genpath(path_prog));
cd(fullfile(path_prog,'co2_ch4_o3_flux'));

%% loop through daily oot output folders and copy jpg files

fldrs = dir(fullfile(path_oot,'20*')); % list of all output folders 'YYYY_MM_DD'

for yyyy = 2019:2020
    YYYY = sprintf('%4i',yyyy);
    for mm = 1:12
        MM = sprintf('%02i',mm);
        for dd = 1:31
            DD = sprintf('%02i',dd);
            DATE = [YYYY,'_',MM,'_',DD];
            % look for a folder with this date
            for ii = 1:length(fldrs)
                jj = strcmp(fldrs(ii).name,DATE);
                if jj; break; end
            end
            % go to next day if no folder is found
            if ~jj; continue; end
            disp(['Copying files for ',DATE])
            % copy jpgs to dest folders
            sub_fldrs = dir(fullfile(fldrs(ii).folder,fldrs(ii).name,'**','FIGURES',...
                          '05 VISUAL INSPECTION'));
            try
                copyfile(fullfile(sub_fldrs(1).folder,'1_Hs','*.jpg'), path_hs);
            catch
                disp(['no hs plots for ',DATE])
            end
            try
                copyfile(fullfile(sub_fldrs(1).folder,'2_Fco2','*.jpg'), path_co2);
            catch
                disp(['no co2 plots for ',DATE])
            end
            try
                copyfile(fullfile(sub_fldrs(1).folder,'3_Fch4','*.jpg'), path_ch4);
            catch
                disp(['no ch4 plots for ',DATE])
            end
            try
                copyfile(fullfile(sub_fldrs(1).folder,'4_MomFlux','*.jpg'), path_mom);
            catch
                disp(['no momentum plots for ',DATE])
            end
        end
    end
end

disp('DONE')