function hed = auto_read_hed_pitch_WHOTS_2019(dfl,ddd,hh)
%{
reads heading/pitch file from PSD Hemisphere system
if gps antennas mounted in pitch orientation
outputs:
1   jd_ref      10 Hz timestamp
2   hed         heading, deg
3   pit         pitch angle, deg

input parameters: dfl = string, full path to hed0 file
                  ddd = variable, day of year (julian date)
                  hh = variable, hour, 0-23
%}

%% initialize
disp(['Reading hed file for hour ',int2str(hh)]);
flist = fopen(dfl);

temphed = {};    % empty arrays for textscan
%temphed = NaN(3600,7);
stx = [];

%% read file
while feof(flist)==0	% read entire file into cell array
    try
        % temphed will be 1x7 cell array, cells will be ~36000x1 arrays
        % 0000106 $PSAT,HPR,000002.00,184.19,0.53,,N*3D
        % 1752124 $PSAT,HPR,061754.00,,,,*73
        temphed = textscan(flist,'%2f%2f%3f %*5c %*3c %f %f %f %f %*[^\n]','delimiter',', ','headerlines',1,'emptyvalue', NaN);
        
        stx = [stx cell2mat(temphed)'];  % 6x~36000 array
 
    catch
        for ii=1:6      % length of last cell reflects missing values from any/all fields
             if length(temphed{1,ii})~=length(temphed{1,6})
                 temphed{1,ii}(length(temphed{1,6})+1)=[];  %truncate length of all cells to length(temphed{1,11})
             end
        end
        if ~isempty(temphed{1,1}) % save good values from last read, if any
            stx = [stx cell2mat(temphed)];
        end
    end
end
fclose(flist);
jd_hed = ddd + (hh + (stx(1,:)+(stx(2,:)+stx(3,:)/1000)/60)/60)/24;
heading = replace_NaN_nearest_neighbor(stx(5,:));
roll = replace_NaN_nearest_neighbor(stx(7,:));
pitch = replace_NaN_nearest_neighbor(stx(6,:));


%% format to 3x3600 1 Hz output array

start = (ddd+hh/24);
delta = double(1.0/86400);
last = start + 3599*delta;
tref = start:delta:last;
hed = zeros(3600,3);
hed(:,1) = tref;
hed(:,2) = interp1(jd_hed,heading,tref,'linear');
hed(:,3) = interp1(jd_hed,pitch,tref,'linear');
