function [sqm] = parseNSTBsqm(fid);
%---------------------------------------------------------
% parse NSTB message type 50 (SQM)
% Ju-Yong Do,  Jan 22, 2009
%---------------------------------------------------------
% input  : NSTB file indicator
% output : sqm message in "sqm"
%=========================================================
%=========================================================
global rx option

%read header
receiverId  = fread(fid, 1, 'uint16');      
gpsweek     = fread(fid, 1, 'uint16');
sqm.tow     = fread(fid, 1, 'uint32')*1e-3; %time of validity in time of week (ms->sec)
sqm.numprn  = fread(fid, 1, '*uint8');      %number of PRNs

%=========================================================
%read sqm message
bodyLen = 0;    %length of body text
for i=1:sqm.numprn
    sqm.prn(i).prn      = fread(fid, 1, '*uint8');      %prn number
    sqm.prn(i).numch    = fread(fid, 1, '*uint8');      %number of channels for this prn
    
    for j=1:sqm.prn(i).numch        
        sqm.prn(i).ch(j).accum1     = fread(fid, 1, 'uint32');     %accumulation 1   
        sqm.prn(i).ch(j).accum2     = fread(fid, 1, 'uint32');     %accumulation 2   
        sqm.prn(i).ch(j).accum3     = fread(fid, 1, 'uint32');     %accumulation 3   
        sqm.prn(i).ch(j).accum4     = fread(fid, 1, 'uint32');     %accumulation 4   
        sqm.prn(i).ch(j).accum5     = fread(fid, 1, 'uint32');     %accumulation 5   
        sqm.prn(i).ch(j).sync       = fread(fid, 1, '*uint8');     %sync flag (0=in sync, 1=not sync)   
    end    
    bodyLen = bodyLen + (2 + double(sqm.prn(i).numch)*21);
end

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

%=========================================================
% CRC test for sanity check (done after parsing all because message size is unknown till now)
crcTestNSTB(fid, 10, bodyLen, 2); %(fid,headLen,bodyLen,tailLen)

if option.plot,         
    disp('=======MESSAGE 50 SQM====================================');
    disp(['tow= ',num2str(sqm.tow),' sec']); 
end

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