function metm = read_met_day_FATIMA_2022(met0_path_ddd,wxt0_path_ddd,rosr_path,...
                z_wxt,z_PTB,adj,prtit,path_raw_images,met_gap_lim)
%{
Reads hourly met files for one day and plots daily diagnostics.
Loops through 24 hours, calling read_met_FATIMA_2022 for each hour.

Inputs: met0_path_ddd: path to daily folder containing hourly met0 files
        wxt0_path_ddd: path to daily folder containing hourly wxt_parsed files
        z_wxt: wxt height above SL, m
        z_PTB: PTB330 height above SL, m
        adj:  1x6 array of temperature adjustments:[tsea,td1,tc1,td2,tc2,wxt_ta]
        prtit: set 'true' to save spectra plots
        path_raw_images: string path to root daily folder for saving plots
        path_python: string path to python script folder with WXT parse script

Output: metm: 1440x19 table of bulk met data columns
        Returns NaN for data gaps

    1   dnum            matlab datenum timestamp
    2   Tsea            sea snake surface SST
    3   psp1            psp1 radiation, W/m2
    4   pir1            pir1 radiation, W/m2
    5   psp2            psp2 radiation, W/m2
    6   pir2            pir2 radiation, W/m2
    7   Tc1             pir1 case temperature
    8   Td1             pir1 dome temperature
    9   Tc2             pir2 case temperature
    10  Td2             pir2 dome temperature
    11  Pmb             sea level air pressure, mb / hPa, Vaisala PTB330
    12  rwdir_wxt       rel wind direction ,0-360 deg from bow
    13  rwspd_wxt       rel wind speed, m/s
    14  Tair_wxt        air temperature, C
    15  RH_wxt          RH, %
    16  Pmb_wxt         sea level air pressure, mb / hPa
    17  rain_wxt        rain rate, mm/hr
    18  U_wxt           rel U wind component, WXT
    19  V_wxt           rel V wind component, WXT
%}
fclose all;
warning ('off','MATLAB:MKDIR:DirectoryExists');
mkdir(fullfile(path_raw_images,'Tsea'));
mkdir(fullfile(path_raw_images,'rh'));
mkdir(fullfile(path_raw_images,'temps'));
mkdir(fullfile(path_raw_images,'pir'));
mkdir(fullfile(path_raw_images,'psp'));
mkdir(fullfile(path_raw_images,'pressure'));
mkdir(fullfile(path_raw_images,'rain'));
mkdir(fullfile(path_raw_images,'rwdir'));
mkdir(fullfile(path_raw_images,'rwspd'));

[~,fldr_name,~] = fileparts(met0_path_ddd);
YY = fldr_name(1:2); yyyy = 2000 + str2double(YY);
DDD = fldr_name(3:5); ddd = str2double(DDD);
disp(['Mean met for day ',DDD]);

vars = {'dnum','Tsea','psp1','pir1','psp2','pir2','Tc1','Td1','Tc2',...
        'Td2','press','rwdir_wxt','rwspd_wxt','Tair_wxt','RH_wxt',...
        'Pmb_wxt','rain_wxt','U_wxt','V_wxt','sst_rosr'};
metm = array2table(NaN(1440,20),'VariableNames',vars);
start = datenum(yyyy-1,12,31) + ddd;
delta = double(1.0/1440);
last = start + 1439*delta;
dnum = start:delta:last; dnum = dnum(:);	% ref 1-min timestamp
metm.dnum = dnum;

%% parse raw WXT files if necessary
% files1 = dir(fullfile(wxt0_path_ddd,'wxt0*.txt'));
% files2 = dir(fullfile(wxt0_path_ddd,'wxt_parsed_*.txt'));
% if length(files2)<length(files1)
%     savedir = pwd;
%     cd(wxt0_path_ddd);
%     system(['python ',fullfile(path_python,'parseWXTFiles.py')]);
%     cd(savedir);
% end

%% process files
for hh = 0:23  % cycle thru 24 hourly met files
    HH = sprintf('%02i',hh);
    dfl1 = fullfile(met0_path_ddd,['met0',YY,DDD,HH,'_raw.txt']);
    dfl2 = fullfile(wxt0_path_ddd,['wxt_parsed_',YY,DDD,HH,'_raw.txt']);
    % read this hours 1-min avg data from both systems if files exist
    % met is 60x20 table
    met = read_met_FATIMA_2022(dfl1,dfl2,rosr_path,z_wxt,z_PTB,adj,met_gap_lim);
    
    % copy this hours data into metm array
    for jj = 1:height(met)
        diff = dnum - met.dnum(jj);
        [val,idx] = min(abs(diff));
        % allow 2 sec tolerance for timestamp match
        if val<double(2.0/86400) && length(idx)==1
            metm(idx,2:20) = met(jj,2:20);
        end
    end
end

% set rwdir to +/-180° format
ii = metm.rwdir_wxt>180;
metm.rwdir_wxt(ii) = metm.rwdir_wxt(ii) - 360;

dt1 = datetime(dnum,'ConvertFrom','datenum');
if prtit
    figure; plot(dt1,metm.Tsea,dt1,metm.sst_rosr,'.','LineWidth',1.5,'Markersize',8);
    ylabel('SST, °C'); grid on; grid minor;
    title(['FATIMA Day ',DDD,' SST']);
    legend('snake','rosr','location','best');
    ppath = fullfile(path_raw_images,'Tsea',['sst_',DDD,'.png']);
    print('-dpng',ppath);

    figure; plot(dt1,metm.RH_wxt,'LineWidth',1.5);
    ylabel('RH, %'); grid on; grid minor;
    title(['FATIMA Day ',DDD,' WXT Relative Humidity']);
    ppath = fullfile(path_raw_images,'rh',['rh_wxt_',DDD,'.png']);
    print('-dpng',ppath);
    
    figure; plot(dt1,metm.rain_wxt,'LineWidth',1.5);
    ylabel('Rain Rate, mm/hr'); grid on; grid minor;
    title(['FATIMA Day ',DDD,' WXT Rain Rate']);
    ppath = fullfile(path_raw_images,'rain',['rain_wxt_',DDD,'.png']);
    print('-dpng',ppath);

    figure; plot(dt1,zeros(size(dt1)),'k-','LineWidth',1); hold on;
    plot(dt1,metm.rwdir_wxt,'LineWidth',1.5); hold off;
    ylim([-182,182]); ylabel('Rel. Wind Dir., deg'); grid on; grid minor;
    title(['FATIMA Day ',DDD,' WXT Relative Wind Direction']);
    ppath = fullfile(path_raw_images,'rwdir',['rwdir_',DDD,'.png']);
    print('-dpng',ppath);

    figure; plot(dt1,metm.rwspd_wxt,'LineWidth',1.5);
    ylabel('Rel. Wind Spd., m/s'); grid on; grid minor;
    title(['FATIMA Day ',DDD,' WXT Relative Wind Speed']);
    ppath = fullfile(path_raw_images,'rwspd',['rwspd_',DDD,'.png']);
    print('-dpng',ppath);

    figure; plot(dt1,metm.pir1,dt1,metm.pir2,'LineWidth',1.5);
    ylabel('IR, W/m2'); grid on; grid minor;
    legend('pir1','pir2','location','north');
    title(['FATIMA Day ',DDD,' Downwelling IR']);
    ppath = fullfile(path_raw_images,'pir',['pir_',DDD,'.png']);
    print('-dpng',ppath);
    
    figure; plot(dt1,metm.psp1,dt1,metm.psp2,'LineWidth',1.5);
    ylabel('Solar Radiation, W/m2'); grid on; grid minor;
    legend('psp1','psp2','location','north');
    title(['FATIMA Day ',DDD,' Downwelling Solar Radiation']);
    ppath = fullfile(path_raw_images,'psp',['psp_',DDD,'.png']);
    print('-dpng',ppath);

    figure; plot(dt1,metm.press,dt1,metm.Pmb_wxt,'LineWidth',1.5);
    ylabel('Sea Level Pressure, mb'); grid on; grid minor;
    legend('ptb330','wxt','location','north');
    title(['FATIMA Day ',DDD,' Pressure']);
    ppath = fullfile(path_raw_images,'pressure',['pressure_',DDD,'.png']);
    print('-dpng',ppath);

    figure; plot(dt1,metm.Tc1,dt1,metm.Tc2,dt1,metm.Td1,...
            dt1,metm.Td2,dt1,metm.Tair_wxt,'LineWidth',1.5);
    ylabel('Temperatures, °C'); grid on; grid minor;
    legend('Tc1','Tc2','Td1','Td2','Ta wxt','location','best');
    title(['FATIMA Day ',DDD,' Temperatures']);
    ppath = fullfile(path_raw_images,'temps',['temps_',DDD,'.png']);
    print('-dpng',ppath);
end
