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

    1   jd_met          1 min timestamp, decimal DOY @ start of interval
    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  rwdir_wxt       rel wind direction ,0-360 deg from bow
    12  rwspd_wxt       rel wind speed, m/s
    13  Tair_wxt        air temperature, C
    14  RH_wxt          RH, %
    15  Pmb_wxt         air pressure, mb or hPa
    16  rain_wxt        rain rate, mm/hr
    17  U_wxt           U wind component, WXT
    18  V_wxt           V wind component, WXT


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
                        [tsea,tair,td1,tc1,td2,tc2]

    Mean 2 raw file format:
    1:3 PSD timecode: MMSSsss
    4:6 Campbell mm/dd/yyyy
    7:9 Campbell hh:mm:ss
    10 Sample(1,Batt_Volt,FP2)
    11 Sample (1,PTemp,FP2)
    12 Average (1,Case1_R,IEEE4,0)
    13 Average (1,Case1_T,FP2,0)
    14 Average (1,Dome1_R,IEEE4,0)
    15 Average (1,Dome1_T,FP2,0)
    16 Average (1,Case2_R,IEEE4,0)
    17 Average (1,Case2_T,FP2,0)
    18 Average (1,Dome2_R,IEEE4,0)
    19 Average (1,Dome2_T,FP2,0)
    20 Average (1,PIR1_V,FP2,0)
    21 Average (1,PIR1_wm2,FP2,0)
    22 Average (1,PSP1_V,FP2,0)
    23 Average (1,PSP1_wm2,FP2,0)
    24 Average (1,PIR2_V,FP2,0)
    25 Average (1,PIR2_wm2,FP2,0)
    26 Average (1,PSP2_V,FP2,0)
    27 Average (1,PSP2_wm2,FP2,0)
    28 Average(1,Snake_r4w,IEEE4,0)
    29 Average(1,SnakeC_4W,IEEE4,0)

    wxt0 file format:
    1:3 PSD timecode: MMSSsss
    4   wind direction, degrees
    5   wind speed, m/s
    6   air temperature, C
    7   RH, %
    8   pressure, mb or hPa
    9   cumulative rain, mm
    10  rain event duration, sec
    11  rain intensity, mm/hr
    12  cumulative hail, hits/cm^2
    13  hail event duration, s
    14  hail intensity, hits/cm^2/hr
    15  heater temperature, C
    16  heater voltage

    NOTE: wxt0 FILE IS GENERATED FROM RAW met3 FILES WITH PYTHON SCRIPT
%}

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

nr2 = 29;     % 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 %f/%f/%f %f:%f:%f ',repmat('%f ',1,20),'%*[^\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(:,29) = mean2(:,29) + adj(1); % sea snake
mean2(:,13) = mean2(:,13) + adj(4);  % case temperature for PIR1 Eppley
mean2(:,15) = mean2(:,15) + adj(3);  % dome temperature for PIR1 Eppley
mean2(:,17) = mean2(:,17) + adj(6);  % case temperature for PIR2 Eppley
mean2(:,19) = mean2(:,19) + adj(5);  % dome temperature for PIR2 Eppley

%% compute IR radiation
pir1 = mean2(:,21)+sig_sb*(mean2(:,13)+C2K).^4-4*sig_sb*((mean2(:,15)+C2K).^4-(mean2(:,13)+C2K).^4);
pir2 = mean2(:,25)+sig_sb*(mean2(:,17)+C2K).^4-4*sig_sb*((mean2(:,19)+C2K).^4-(mean2(:,17)+C2K).^4);

%% format psd met array [jd,ts,psp1,pir1,psp2,pir2,tc1,td1,tc2,td2]
met2 = [jd_pc_2,mean2(:,29),mean2(:,23),pir1,mean2(:,27),pir2,mean2(:,13),mean2(:,15),...
        mean2(:,17),mean2(:,19)];

%% 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
