function [gpsmeas] = calGPSSGMP(meas, aiding, est);
%---------------------------------------------------------
% Calculate satellite position, clk bias and correct pseudorange
% Ju-Yong Do,  Feb 3, 2009
%---------------------------------------------------------
% input  : eph, pseudorange, oldpos
% output : satPos, satClkBias, corrected pseudorange
%=========================================================
%=========================================================
if ~est.valid, gpsmeas=[]; return; end;
global opt const

%=========================================================
%Calculate satellite position, clk bias, pr
[satPos, satClkBias]    = calSatSGMP(aiding.eph, meas, est.oldpos); 
[elevation, azimuth]    = calElAzSGMP(satPos, est.oldpos, est.rotmat);

%Pseudo range correction for sat clk bias, iono err, tropos err
pr              = [meas.tx.prL1]';                  %L1 pseudorange [m]
pr              = pr + satClkBias;                  %pr correction to compensate errors;    

% %compensate ionosphere and troposhere errors
% [oldLat, oldLon, oldAlt] = wgsxyz2lla(est.oldpos);  %latitude, longitude, altitude
% ionoErr         = calIono(oldLat, oldLon, azimuth, elevation, meas.tow); %iono error
% tropErr         = tropcorr(elevation);              %troposhere error
% pr              = pr - ionoErr - tropErr;           %pr correction to compensate errors;    

%weight based on CRLB
weight          = eye(meas.numtx);%10.^([meas.tx(:).snrL1]/20)*(const.gpsL1BW/const.speedOfLight); 
% %weight based on elevation
% weight          = diag([1./cos(asin(sin(elevation*pi/180)*6371/(6371+350))).^2]);    

%=========================================================
%remove bad data 
ihighsnr        = ([meas.tx.snrL1] >=const.lmt.snr);%tx w. high snr
ihighel         = (elevation >= const.lmt.angle)';  %tx w. high elevation angle
igood           = find(ihighsnr & ihighel);         %index to valid measurement set

gpsmeas.numtx   = length(igood);                    %number of measurements
gpsmeas.dim     = 3;                                %posfix dimension (3D fix is default)
gpsmeas.pr      = pr(igood);                        %psuedo range
gpsmeas.txpos   = satPos(igood,:);                  %satellite position
gpsmeas.el      = elevation(igood);                 %satellite elevation
gpsmeas.az      = azimuth(igood);                   %satellite azimuth
gpsmeas.weight  = weight(igood,igood);              %weighting matrix for pr

%check validity of this epoch
est.valid       = meas.numtx>=const.lmt.numtx; 

return;
%=========================================================
%=========================================================