function [meas, aiding, est] = getDataSGMP(meas, aiding, est);
%---------------------------------------------------------
% Find valid measurements and aiding info for a given epoch
% Ju-Yong Do,  Feb 2, 2009
%---------------------------------------------------------
% input  : meas, eph data
% output : meas, eph (for current epoch)
%=========================================================
%=========================================================
global opt const

%search ephemeris data for each satellite
eph =[];
for i=meas.numtx:-1:1    
    %find candidate eph close to meas.tow
    ieph = find([aiding.eph.prn]==meas.tx(i).prn);%candidate eph        
    reltoe = [aiding.eph(ieph).toe]-meas.tow;
    ieph = ieph(find((reltoe>=0) & (reltoe<7200)));%within 2 hour
    if isempty(ieph),                           %if no matching eph,
        meas.tx     = meas.tx([1:i-1, i+1:end]);%remove meas  
        meas.numtx  = meas.numtx-1;             %one less channel
        continue;
    end
    
    %use new or least old one
    jeph = find([aiding.eph(ieph).tow]>=meas.tow);%newer eph
    if ~isempty(jeph),                          %there are newer ones
        eph = [aiding.eph(ieph(jeph(1))) eph];  %use earliest      
    else                                        %there is no newer one
        eph = [aiding.eph(ieph(end)) eph];      %use latest
    end
end

%=========================================================
%put together relevant aiding information
aiding      = [];           %clear all aiding information
aiding.eph  = eph;          %return ephemeris for this epoch    
est.tow     = meas.tow;     %time of week for this epoch
est.valid   = meas.numtx>=const.lmt.numtx; %check validity of this epoch

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