function [meas] = parseNSTBtracking(fid);
%---------------------------------------------------------
% parse NSTB message type 1 (satellite tracking data)
% Ju-Yong Do,  Jan 14, 2009
%---------------------------------------------------------
% input  : NSTB file indicator
% output : satellite tracking data in "meas"
% 01/23/09 : code clean up
%=========================================================
%=========================================================
global rx option

%read header
receiverId  = fread(fid, 1, 'uint16');          %receiver id
gpsweek     = fread(fid, 1, 'uint16');          %time of validity (week)
meas.tow    = fread(fid, 1, 'uint32')*1e-3;     %time of validity in time of week (ms->sec)
epochCnt    = fread(fid, 1, 'uint16');          %TRS epoch counter
nDualFreq   = fread(fid, 1, 'uint8');           %number of dual frequency channels
nSingleFreq = fread(fid, 1, 'uint8');           %number of single frequency channels
meas.numtx  = nDualFreq+nSingleFreq;            %number of transmitters (differ from numch)
meas.type   = 'gps';                            %number of transmitters (differ from numch)

%=========================================================
%read dual frequency measurements (L1 and L2)
temp1=zeros(3,1); temp2=zeros(5,1); %temporary variables
for i=1:nDualFreq
    meas.tx(i).prn      = fread(fid, 1, '*uint8');  %prn number
    meas.tx(i).numch    = uint8(2);                 %number of observed channels (1:L1 only, 2:L1/L2)
    meas.tx(i).status   = fread(fid, 1, '*uint32'); %status flag
    
    %to save time, read in bulk and distribute
    temp1               = fread(fid, 3, 'float64'); %pr(1),cr(1),cr(2)
    temp2               = fread(fid, 5, 'float32'); %pr2-pr1,dp(1),dp(2),snr(1),snr(2)        
    meas.tx(i).prL1     = temp1(1);                 %pseudo-range in L1 (meter)        
    meas.tx(i).crL1     = temp1(2);                 %carrier-range in L1 (meter)    
    meas.tx(i).dpL1     = temp2(2);                 %doppler in L1 (meter/second)    
    meas.tx(i).snrL1    = temp2(4);                 %signal-to-noise ratio in L1 (dBHz)    
    %cyclic slip counter parsed from status (bit(31:0))
    meas.tx(i).slipL1   = uint8(bitand(meas.tx(i).status, uint32(28))/4);     %bit(4:2) hex2dec('0000001C')    
    
    meas.tx(i).prL2     = temp1(1)+temp2(1);        %pseudo-range in L2 (meter)        
    meas.tx(i).crL2     = temp1(3);                 %carrier-range in L2 (meter)    
    meas.tx(i).dpL2     = temp2(3);                 %doppler in L2 (meter/second)    
    meas.tx(i).snrL2    = temp2(5);                 %signal-to-noise ratio in L2 (dBHz)    
    %cyclic slip counter parsed from status (bit(31:0))
    meas.tx(i).slipL2   = uint8(bitand(meas.tx(i).status, uint32(224))/32);   %bit(7:5) hex2dec('000000E0')
end

%read single frequency measurements (L1)
for i=[1:nSingleFreq]+nDualFreq
    meas.tx(i).prn      = fread(fid, 1, '*uint8');  %prn number
    meas.tx(i).numch    = uint8(1);                 %number of observed channels (1:L1 only, 2:L1/L2)
    meas.tx(i).status   = fread(fid, 1, '*uint32'); %status flag
    meas.tx(i).prL1     = fread(fid, 1, 'float64'); %pseudo-range in L1 (meter)
    meas.tx(i).crL1     = fread(fid, 1, 'float64'); %carrier-range in L1 (meter)    
    meas.tx(i).dpL1     = fread(fid, 1, 'float32'); %doppler in L1 (meter/second)
    meas.tx(i).snrL1    = fread(fid, 1, 'float32'); %signal-to-noise ratio in L1 (dBHz)
    
    %cyclic slip counter parsed from status
    meas.tx(i).slipL1   = uint8(bitand(meas.tx(i).status, uint32(28))/4);     %bit(4:2) hex2dec('0000001C')
end

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

%=========================================================
% CRC test for sanity check
crcTestNSTB(fid, 13, 49*nDualFreq + 29*nSingleFreq, 2); %(fid,headLen,bodyLen,tailLen)

if option.plot, 
    disp('=======MESSAGE 1 SAT TRACKING================================');
    disp(['tow= ',num2str(meas.tow),' sec, ',num2str(meas.numtx),' transmitters']);
    disp('prn       pr1(m)    pr2(m)    cr1(m)    cr2(m)    dp1(m/s)  dp2(m/s)  snr1(dBHz)snr2(dBHz)');    
    for i=1:nDualFreq        
        disp(num2str([double(meas.tx(i).prn) meas.tx(i).pr meas.tx(i).cr meas.tx(i).dp meas.tx(i).snr], '%-10.0f'));
    end
    for i=[1:nSingleFreq]+nDualFreq
        disp(num2str([double(meas.tx(i).prn) meas.tx(i).pr meas.tx(i).cr meas.tx(i).dp meas.tx(i).snr], '%-10.0f%-20.0f%-20.0f%-20.0f%-20.0f'));
    end    
end

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