function met = auto_read_met_STRATUS_2019(dfl2,dfl3,ddd,hhh,adj)
%{
reads met2 and wxt0 files and returns 10 min avg array with the
following columns:

    metscs
    1   scs_tm          scs time
    2   scs_ws          wind speed
    3   scs_wd          wind direction
    4   scs_wsr         corrected wind speed
    5   scs_wdr         corrected wind direction
    6   sc1_txt
    7   sc2_txt         
    8   sc3_txt         
    9   sc4_txt         
    10  sc5_txt         


input parameters: dfl2 = string path to met2 file
                  dfl3 = string path to wxt0 file
                  ddd = julian date
                  hh = hour
                  adj =  1x6 array of temperature adjustments
%}

%% initialize
sig_sb = 5.67e-8;    % Stefan Boltzmann constant
C2K = 273.15;        % temp conversion constant

nr2 = 30;     % number of variables in met2
mean2 = [];   % array for raw met2 data
mean3 = [];
temp = {};

%% read met2 file
disp(['Reading met2 file for hour ',int2str(hhh)]);
flist = fopen(dfl2,'r');
while ~feof(flist)
    try
        temp = textscan(flist,['%2f%2f%3f',repmat('%f ',1,21),'%*[^\n]'],'delimiter',', ','headerlines',1);
        mean2 = [mean2 cell2mat(temp)];
    catch
        % if one line is cut short, blank it out totally
        [r,c] = size(temp{1,nr2});
        if r==0 || c==0; continue; 
        end % read next batch of lines
        for ll=1:nr2
            if length(temp{1,ll})~=r
                temp{1,ll}(r+1)=[];
            end
        end
        for ii=1:length(temp)  % Look for NaNs and blanks in data fields
            temp{ii}(ismember(temp{ii},'NAN'))=cellstr('NaN');
            temp{ii}(ismember(temp{ii},''))=cellstr('NaN');
            mean2 = [mean2 str2num(char(temp{ii}))];  % 60xnr2 array
        end
    end
end

jd_pc_2 = ddd+(hhh+(mean2(:,1)+(mean2(:,2)+mean2(:,3)/1000)/60)/60)/24;   % timestamp from PSD system
fclose(flist);

%% temperature adjustments
mean2(:,23) = mean2(:,23) + adj(1);  % sea snake
mean2(:,7) = mean2(:,7) + adj(4);  % case temperature for PIR1 Eppley
mean2(:,9) = mean2(:,9) + adj(3);  % dome temperature for PIR1 Eppley
mean2(:,11) = mean2(:,11) + adj(6);  % case temperature for PIR2 Eppley
mean2(:,13) = mean2(:,13) + adj(5);  % dome temperature for PIR2 Eppley

%% compute IR radiation
% flipping sign on raw W/m2...
pir1 = -mean2(:,15) + sig_sb*(mean2(:,7)+C2K).^4 - 4*sig_sb*((mean2(:,9)+C2K).^4-(mean2(:,7)+C2K).^4);
pir2 = -mean2(:,19) + sig_sb*(mean2(:,11)+C2K).^4 - 4*sig_sb*((mean2(:,13)+C2K).^4-(mean2(:,11)+C2K).^4);

%% format psd met array [jd,ts,psp1,pir1,psp2,pir2,tc1,td1,tc2,td2,Pmb]
met2 = [jd_pc_2,mean2(:,23),mean2(:,17)*(-1),pir1,mean2(:,21)*(-1),pir2,mean2(:,7),mean2(:,9),...
        mean2(:,11),mean2(:,13),mean2(:,24)];

%% read wxt0 file
disp(['Reading wxt0 file for hour ',int2str(hhh)]); 
flist = fopen(dfl3,'r');

temp = textscan(flist,['%2f%2f%3f ',repmat('%f ',1,13),'%*[^\n]'],'delimiter',',');
mean3 = [mean3 cell2mat(temp)];

[wxtU,wxtV] = sd_to_uv(mean3(:,5),mean3(:,4));  % compute wind components
mean3 = [mean3,wxtU,wxtV];                      % add UV cols 17,18 to array

jd_pc_3 = ddd+(hhh+(mean3(:,1)+(mean3(:,2)+mean3(:,3)/1000)/60)/60)/24;   % timestamp from PSD system
fclose(flist);

mean3(:,6) = mean3(:,6) + adj(2);  % air temp adjustment

%% format wxt array [jd,ra,rh,Pmb,rain,U,V]
wxt = [jd_pc_3,mean3(:,6:8),mean3(:,11),mean3(:,17:18)];
    
%% average to 10 min
start = (ddd+hhh/24);
delta = double(10/1440);
last = start + 6*delta;
jd_bin = start:delta:last;

met10 = interval_avg(met2(:,1),met2(:,2:end),jd_bin');
wxt10 = interval_avg(wxt(:,1),wxt(:,2:end),jd_bin');

[spd,dir] = uv_to_sd(wxt10(:,5),wxt10(:,6));    % recompute rwspd/rwdir

%% format output array
met = [met10,spd,dir,wxt10(:,2:end)];

end
