function [eph] = parseNSTBeph(fid);
%---------------------------------------------------------
% parse NSTB message type 20 (ephemeris data)
% Ju-Yong Do,  Jan 14, 2009
%---------------------------------------------------------
% input  : NSTB file indicator
% output : ephemeris data in "eph"
%=========================================================
%=========================================================
global rx option

%do not parse if it's same as previous ephemeris
% if option.compress
%     if ~isNewEph(fid), 
%         eph=[]; 
%         fseek(fid, -1+9+66+2, 'cof');    
%         return; 
%     end
% end

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

%=========================================================
%read ephemeris message
eph.prn     = fread(fid, 1, '*uint8');          %prn number
eph.rxtow   = fread(fid, 1, 'uint32');          %received time of msg (sec)
eph.ura     = fread(fid, 1, 'uint8');           %user range accuracy (0~15)
eph.health  = fread(fid, 1, '*uint8');          %satellite health
eph.iodc    = fread(fid, 1, '*uint16');         %issue of data, clock
eph.tgd     = fread(fid, 1, 'int8')*2^-31;      %group delay (sec)
eph.toc     = fread(fid, 1, 'uint16')*2^4;      %reference time of clock parameter (second)
eph.af2     = fread(fid, 1, 'int8')*2^-55;      %clk correction coef (s/s/s)
eph.af1     = fread(fid, 1, 'int16')*2^-43;     %clk correction coef (s/s)
eph.af0     = fread(fid, 1, 'int32')*2^-31;     %clk correction coef (sec)
eph.m0      = fread(fid, 1, 'int32')*pi*2^-31;  %mean anomaly at ref time (rad)
eph.dn      = fread(fid, 1, 'int16')*pi*2^-43;  %mean motion correction [rad/s]
eph.e       = fread(fid, 1, 'uint32')*2^-33;    %eccentricity
eph.sqrta   = fread(fid, 1, 'uint32')*2^-19;    %sqrt(semi-major axis) (sqrt_m)
eph.omg0    = fread(fid, 1, 'int32')*pi*2^-31;  %longitude of ascending node (rad)
eph.i0      = fread(fid, 1, 'int32')*pi*2^-31;  %inclination angles at ref time (rad)
eph.w       = fread(fid, 1, 'int32')*pi*2^-31;  %argument of perigee (rad)
eph.odot    = fread(fid, 1, 'int32')*pi*2^-43;  %rate of right ascension (rad/s)
eph.idot    = fread(fid, 1, 'int16')*pi*2^-43;  %rate of inclination angle (rad/s)
eph.cuc     = fread(fid, 1, 'int16')*2^-29;     %argument of latitude correciton, cosine (rad)
eph.cus     = fread(fid, 1, 'int16')*2^-29;     %argument of latitude correciton, sine (rad)
eph.crc     = fread(fid, 1, 'int16')*2^-5;      %radius correciton, cosine (meter)
eph.crs     = fread(fid, 1, 'int16')*2^-5;      %radius correciton, sine (meter)
eph.cic     = fread(fid, 1, 'int16')*2^-29;     %inclination correciton, cosine (rad)
eph.cis     = fread(fid, 1, 'int16')*2^-29;     %inclination correciton, sine (rad)
eph.toe     = fread(fid, 1, 'uint16')*2^4;      %reference time of ephemeris parameter (second)
eph.iode    = fread(fid, 1, '*uint8');          %issue of data, ephemeris

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

%=========================================================
% CRC test for sanity check
crcTestNSTB(fid, 9, 66, 2); %(fid,headLen,bodyLen,tailLen)

if option.plot, 
    disp('=======MESSAGE 20 EPHEMERIS==================================');
    disp(['tow= ',num2str(eph.tow),' sec, prn= ',num2str(eph.prn),]); 
end

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


