function hed = read_hed_SeaState_2015(dfl,ddd,hhh)
%{
reads head/pitch file specified by dfl and returns array with the
following columns:

1   jd_ref      10 Hz timestamp
2   hed         heading
3   pitch       pitch angle

input parameters: dfl = string path to scs file
                  ddd = julian date
                  hhh = hour
%}

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

temp = {};    % empty arrays for textscan
stx = [];

%% read file
while feof(flist)==0,	% read entire file into cell array
    try
        % temp will be 1x7 cell array, cells will be ~36000x1 arrays
        temp =textscan(flist,'%2f%2f%3f %*5c %*3c %f %f %f %f %*[^\n]','delimiter', ', ', 'headerlines', 1,  'emptyvalue', NaN);
        stx = [stx cell2mat(temp)'];  % 11x~36000 array
    catch
        for ii=1:7      % length of last cell reflects missing values from any/all fields
             if length(temp{1,ii})~=length(temp{1,7})
                 temp{1,ii}(length(temp{1,7})+1)=[];  %truncate length of all cells to length(temp{1,7})
             end;
        end;
        if ~isempty(temp{1,1}); % save good values from last read, if any
            stx = [stx cell2mat(temp)'];
        end;
    end;
end;
fclose(flist);
jd_hed = ddd + (hhh + (stx(1,:)+(stx(2,:)+stx(3,:)/1000)/60)/60)/24;

%% format variables
[b,m,n]=unique(jd_hed);
jd_hed = jd_hed(m);
stx = stx(:,m);
heading = stx(5,:);
pitch = stx(6,:);
heading = unwrap(heading*pi/180);

%% interp if necessary
jd_ref = ddd+hhh/24+(0:1/10:35999/10)/60/60/24;   % exact 10Hz timestamp
hed10 = interp1(jd_hed,heading,jd_ref,'linear','extrap');
pit10 = interp1(jd_hed,pitch,jd_ref,'linear','extrap');
hed10 = mod((hed10*180/pi)+360, 360);

%% output
hed = [jd_ref',hed10',pit10'];

