function rad = read_rad(ddd,dfl_rad)

%{
    reads daily TGT RAD-RAW files
    
    input:  ddd = day of year variable
            dfl_rad = full path string to file
%}

%% reference 1 Hz timestamp
disp(['Reading WIR00 file for jd ',int2str(ddd)]);
jd_ref = ddd:1/86400:ddd+1-1/86400; jd_ref = jd_ref(:);

%% read data if file exists, if not return NaNs
if exist(dfl_rad,'file')==2
    %% read file
    flist = fopen(dfl_rad);
    temp = {};    % empty arrays for textscan
    stx = [];

    while feof(flist)==0    % read entire file into cell array
        try
            % temp will be 1x11 cell array, cells will be ~36000x1 arrays
            temp = textscan(flist,'%2f/%2f/%4f %2f:%2f:%6.3f %*6c %*2f/%*2f/%*2f %*2f:%*2f:%*2f %f %f %f %f %f %f %*[^\n]','delimiter',',','headerlines',0,'emptyvalue',NaN);
            stx = [stx cell2mat(temp)'];  % 12x~86400 array
        catch
            for ii=1:12      % length of last cell reflects missing values from any/all fields
                 if length(temp{1,ii})~=length(temp{1,12})
                     temp{1,ii}(length(temp{1,12})+1)=[];  %truncate length of all cells to length(temp{1,11})
                 end
            end
            if ~isempty(temp{1,1})  % save good values from last read, if any
                stx = [stx cell2mat(temp)'];
            end
        end
    end
    fclose(flist);
    stx = stx';
    jd_rad = md2yd_vn(stx(:,3),stx(:,1),stx(:,2));
    jd_rad = jd_rad + stx(:,4)/24 + stx(:,5)/1440 + stx(:,6)/86400;
    
    %% copy to 1 Hz array
    raw = [stx(:,8:12)];
    rad = NaN(86400,6);
    rad(:,1) = jd_ref;
    for xx=1:length(jd_rad)
        [~,zz] = min(abs(jd_ref - jd_rad(xx)));
        rad(zz,2:6) = raw(xx,:);
    end
    
    % fill in 1Hz array with const values over each 10 sec period
    kk = jd_ref>=jd_rad(1) & jd_ref<=jd_rad(end);
    rad(kk,2) = replace_NaN_nearest_neighbor(rad(kk,2));
    rad(kk,3) = replace_NaN_nearest_neighbor(rad(kk,3));
    rad(kk,4) = replace_NaN_nearest_neighbor(rad(kk,4));
    rad(kk,5) = replace_NaN_nearest_neighbor(rad(kk,5));
    rad(kk,6) = replace_NaN_nearest_neighbor(rad(kk,6));
    
else
    %% return NaN array if file not found
    rad = NaN(86400,6);
    rad(:,1) = jd_ref;
end

end