% a1_process_daily_FATIMA_2022.m

% run daily scripts to check raw data and plot diagnostics
% For entire FATIMA cruise: 7/4/2022 to 8/1/2022

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

% set date range to process:
dv_first = [2022,7,4]; dnum_first = datenum(dv_first);
dv_last = [2022,8,1]; dnum_last = datenum(dv_last);

prtit = true;          % set true to save plots
son_read_raw = false;   % set these true to force read of raw txt file
lic_read_raw = false;
mru_read_raw = false;

% paths
root_data_path = '/Users/byronb/Raw_Data_Files.noindex/FATIMA_2022';
path_prog = fullfile(root_data_path,'_matlab');
plot_path = fullfile(root_data_path,'raw_images');

% set matlab script path
path(pathdef);
cd(path_prog);
addpath(genpath(path_prog));

%% loop over all days
for dn = dnum_first:dnum_last
    dv = datevec(dn);
    YYYY = sprintf('%4i',dv(1));
    YY = YYYY(3:4);
    ddd = md2yd_vn(dv(1),dv(2),dv(3));
    DDD = sprintf('%03i',ddd);

    fname = [YY,DDD];  % daily folder name for raw files to be processed
    disp(['Processing ',fname])
    DDD = fname(3:5);

    %% Mean Met
    met0_path_ddd = fullfile(root_data_path,'raw','meanMet',fname);
    wxt0_path_ddd = fullfile(root_data_path,'raw','WXT520',fname);
    rosr_path = fullfile(root_data_path,'raw','rosr','sst_rosr_all.mat');
    met_path_raw_images = fullfile(plot_path,'met_daily'); mkdir(met_path_raw_images);
    z_wxt = 18;
    z_PTB = 16.25;
    % longest gap in met data to fill with est values (points)
    met_gap_lim = 5; % 5 min or less gaps in data will be filled

    % no temperature adjustments this run
    adj = [0,0,0,0,0,0]; %[tsea,td1,tc1,td2,tc2,wxt_ta]

    metm = read_met_day_FATIMA_2022(met0_path_ddd,wxt0_path_ddd,rosr_path,z_wxt,...
        z_PTB,adj,prtit,met_path_raw_images,met_gap_lim);

    %% Licor 7500
    lic_path_ddd = fullfile(root_data_path,'raw','GasLicor',fname);
    lic_path_raw_images = fullfile(plot_path,'licor_daily'); mkdir(lic_path_raw_images);

    licm = read_licor_day_FATIMA_2022(lic_path_ddd,prtit,lic_path_raw_images,...
        lic_read_raw);

    %% Gill R3 sonic
    son_path_ddd = fullfile(root_data_path,'raw','SonicGillR3',fname);
    son_path_raw_images = fullfile(plot_path,'sonic_daily'); mkdir(son_path_raw_images);

    if dn < datenum(2022,7,18,0,0,0)
        rotationsonic = true;  % leg 1 sonic - requires 30° rotation
    else
        rotationsonic = false; % leg 2 sonic - no rotation
    end
    [sonm,sonQC] = read_sonic_day_FATIMA_2022(son_path_ddd,rotationsonic,...
        son_read_raw,prtit,son_path_raw_images);

    %% MRU motion
    mru0_path_ddd = fullfile(root_data_path,'raw','MRUdata',fname);
    mru_path_raw_images = fullfile(plot_path,'mru_daily'); mkdir(mru_path_raw_images);

    motm = read_motion_day_FATIMA_2022(mru0_path_ddd,mru_read_raw,...
        prtit,mru_path_raw_images);

    %% nav data
    path_gps0_ddd = fullfile(root_data_path,'raw','GPS_Hemisphere',fname);
    path_gps1_ddd = fullfile(root_data_path,'raw','GPS_MRUP',fname);
    path_hed0_ddd = fullfile(root_data_path,'raw','HED_Hemisphere',fname);
    nav_path_raw_images = fullfile(plot_path,'nav_daily'); mkdir(nav_path_raw_images);
    PosLims = [292,313,38,50];
    navm = read_nav_day_FATIMA_2022(path_gps0_ddd,path_gps1_ddd,path_hed0_ddd,...
        nav_path_raw_images,prtit,PosLims);

    %% save daily 1 Hz / 1-min data tables
    sfl = fullfile(root_data_path,'raw_daily_merge',[fname,'.mat']);
    save(sfl,'metm','licm','sonm','sonQC','motm','navm');

    %% a few more comparison plots
    if prtit
        figure; plot([0,360],[0,360],'k-','LineWidth',1); hold on;
        plot(motm.hed,navm.hed0,'.','MarkerSize',3); hold off; grid on; grid minor;
        ylabel('Hemisphere heading'); xlabel('MRU heading');
        title(['FATIMA Day ',DDD,' Heading Comparison']); axis([0,360,0,360]);
        ppath = fullfile(nav_path_raw_images,'heading',['hemisphere_vs_mru_',DDD,'.png']);
        print('-dpng',ppath);

        q = qair_p([metm.Tair_wxt,metm.RH_wxt,metm.press]);
        dnum_1min_bin = [metm{:,1};metm{end,1}+1/1440];
        q_irga = interval_avg(licm.dnum,licm.q_lic,dnum_1min_bin);
        q_irga = q_irga(:,2);
        figure; plot([5,20],[5,20],'k-','LineWidth',1); hold on;
        plot(q,q_irga,'.','MarkerSize',4); hold off; grid on; grid minor;
        ylabel('q Licor, g/kg'); xlabel('q air'); axis([5,20,5,20]);
        title(['FATIMA Day ',DDD,' Specific Humidity Comparison']);
        ppath = fullfile(met_path_raw_images,'rh',['q_lic_vs_q_',DDD,'.png']);
        print('-dpng',ppath);
    end
    close all
end
disp('DONE');

