function [tus] = parseNSTBtus(fid);
%---------------------------------------------------------
% parse NSTB message type 41 (TUS)
% Ju-Yong Do,  Jan 22, 2009
%---------------------------------------------------------
% input  : NSTB file indicator
% output : tus message in "tus"
%=========================================================
%=========================================================
global rx option

%read header
tus.id      = fread(fid, 1, '*uint16');         %TUS identification number
gpsweek     = fread(fid, 1, 'uint16');
tus.tow     = fread(fid, 1, 'uint32')*1e-3;     %time of validity in time of week (ms->sec)
tus.status  = fread(fid, 1, '*uint16');         %TUS status flags
tus.numtms  = fread(fid, 1, '*uint8');          %number of TMSs sending message type 40s
tus.numgps  = fread(fid, 1, '*uint8');          %number of GPS satellites tracked
tus.starttow= fread(fid, 1, 'uint32')*1e-3;     %time first bit of WAAS message broadcast to GEO in time of week (ms->sec)
tus.endtow  = fread(fid, 1, 'uint32')*1e-3;     %time last bit of WAAS message broadcast to GEO in time of week (ms->sec)

%=========================================================
%read GEO satellite data
tus.geo.prn         = fread(fid, 1, '*uint8');  %prn number
tus.geo.waasmsg     = fread(fid, 32, '*uint8'); %FEC decoded WAAS message received from GEO
tus.geo.pr          = fread(fid, 1, 'float64'); %pseudo-range in L1 (meter)
tus.geo.cr          = fread(fid, 1, 'float64'); %carrier-range in L1 (meter)    
tus.geo.dp          = fread(fid, 1, 'float32'); %doppler in L1 (meter/second)
tus.geo.snr         = fread(fid, 1, 'float32'); %signal-to-noise ratio in L2 (dBHz)
tus.geo.range       = fread(fid, 1, 'float64'); %computed geometric range (meter)
tus.geo.ionodelay   = fread(fid, 1, 'float32'); %computed ionospheric delay (meter)
tus.geo.tropodelay  = fread(fid, 1, 'float32'); %computed tropospheric delay (meter)

%read tms message
for i=1:tus.numtms 
    tus.tms(i).id       = fread(fid, 1, '*uint16');     %tms identification number
    tus.tms(i).status   = fread(fid, 1, '*uint16');     %TMS status flags
    tus.tms(i).msgcnt   = fread(fid, 1, '*uint8');      %message sequence counter (0,1, or 2)
    tus.tms(i).rxtow    = fread(fid, 1, 'uint32')*1e-3; %time message type 40 was received from TMS (ms->sec)
end

%read GEO satellite data
for i=1:tus.numgps 
    tus.gps(i).prn      = fread(fid, 1, '*uint8');  %prn number
    tus.gps(i).status   = fread(fid, 1, '*uint32'); %satellite status flags
    tus.gps(i).pr       = fread(fid, 1, 'float64'); %pseudo-range in L1 (meter)
    tus.gps(i).cr       = fread(fid, 1, 'float64'); %carrier-range in L1 (meter)    
    tus.gps(i).dp       = fread(fid, 1, 'float32'); %doppler in L1 (meter/second)
    tus.gps(i).snr      = fread(fid, 1, 'float32'); %signal-to-noise ratio in L2 (dBHz)
end

%stack up what's collected
rx.aiding.tus        = [rx.aiding.tus tus];

%=========================================================
% CRC test for sanity check
crcTestNSTB(fid, 21, 77 + 9*double(tus.numtms) + 29*double(tus.numgps), 2); %(fid,headLen,bodyLen,tailLen)

if option.plot,         
    disp('=======MESSAGE 41 TUS====================================');
    disp(['tow= ',num2str(tus.tow),' sec']); 
end

return
%=========================================================
%=========================================================
