function r = fillnavhdg(instclass, fullfilebase, r, nb2gga_coef);
% r = fillnavhdg(instclass, fullfilebase, r, nb2gga_coef);
% heading, navigation, time correction
% get it from aux: split_aux.py made a file called [a_file,'.hdg']
% nmea_gps.prl made a file called [a_file,'.mat']

%the file has been read but not concatenated

aux = read_aux(instclass, fullfilebase);

   %fill heading and nav fields
   % these fields don't exist until here 
   % I'm going to assume
   %      - the ue4 logging PC clock matches UTC
   %      - the gga time matches UTC
   %      - the first timestamp from GGA and from a ping are the same

   % 
   
%this is a really bad assumption
% QQQ so r.nav.sec_pc_minus_utc is subtracted from nb time to get utc???


dday0 = r.dday - nb2gga_coef(1); 
r.dday = nb2gga_coef(2) + nb2gga_coef(3)*dday0 ;

r.nav.sec_pc_minus_utc = zeros(size(r.dday));  %already fixed the time

%index of ggadday to use for table1

goodi = [1:length(aux.ggadday)]';

if (isempty(r.dday))
   error('no data in r.dday (fillnavhdg)')
end

if (isfield(aux, 'ggadday') & ...
    isfield(aux, 'lon') & ...
    isfield(aux, 'lat'))
   r.nav.txy1 = [r.dday ...
                 table1([aux.ggadday(goodi) aux.lon(goodi)],r.dday) ...
                 table1([aux.ggadday(goodi) aux.lat(goodi)],r.dday) ]';
   r.nav.txy2 = r.nav.txy1;   
end

%replace with data att data: 
if (isfield(aux, 'gyro') & isfield(aux, 'ashgyrodiff'))
   ash_heading = table1(  ...
   [aux.ggadday(goodi) aux.gyro(goodi)+aux.ashgyrodiff(goodi)], r.dday);
   gyroheading = r.heading;
   [mdiff, sdiff] = mstdgap(ash_heading - gyroheading );
   shd = std(r.heading);  %checking to see whether heading is updated in r
   if (length(r.dday) > 1)
      if (shd ~= 0) %measured headings differ
         r.heading = ash_heading;
         clg
         fprintf('measheading-auxheading: mean (%f) std (%f)\n', mdiff, sdiff)
         if (sdiff>.2)
            warning('possible heading error detected in fillnavhdg')
            clg
            plot(r.dday, ash_heading,'r.')
            hold on
            plot(r.dday, gyroheading,'g.')
            title('gyro: green; ashtech: red')
            paused
         end
      else %measured headings do not change
         fprintf('no headings available\n')
      end
   end
end

