function vtg = read_vtg(ddd,dfl_vtg)

%{
    reads daily TGT POSMV-V5-INVTG-RAW files
    
    input:  ddd = day of year variable
            dfl_vtg = full path string to file
%}

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

%% read data if file exists, if not return NaNs
if exist(dfl_vtg,'file')==2
    %% read file
    flist = fopen(dfl_vtg);
    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 %f %*c %*c %*c %f %*[^\n]','delimiter',',','headerlines',0,'emptyvalue',NaN);
            stx = [stx cell2mat(temp)'];  % 8x~86400 array
        catch
            for ii=1:8      % length of last cell reflects missing values from any/all fields
                 if length(temp{1,ii})~=length(temp{1,8})
                     temp{1,ii}(length(temp{1,8})+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_gps = md2yd_vn(stx(:,3),stx(:,1),stx(:,2));
    jd_gps = jd_gps + stx(:,4)/24 + stx(:,5)/1440 + stx(:,6)/86400;
    
    %% copy to 1 Hz array
    raw = stx(:,7:8);  % sog in kts
    vtg = interval_avg(jd_gps,raw,jd_bin);
    
else
    %% return NaN array if file not found
    vtg = NaN(86400,3);
    vtg(:,1) = jd_bin(1:end-1);
end

end