Index: /trunk/BNC/RTCM3/ephemeris.cpp
===================================================================
--- /trunk/BNC/RTCM3/ephemeris.cpp	(revision 4017)
+++ /trunk/BNC/RTCM3/ephemeris.cpp	(revision 4018)
@@ -15,49 +15,19 @@
 using namespace std;
 
-#define PI          3.1415926535898
-// Returns nearest integer value
-////////////////////////////////////////////////////////////////////////////
-static double NearestInt(double fl, double * remain)
-{
-  bool isneg = fl < 0.0;
-  double intval;
-  if(isneg) fl *= -1.0;
-  intval = (double)((unsigned long)(fl+0.5));
-  if(isneg) {fl *= -1.0; intval *= -1.0;}
-  if(remain)
-    *remain = fl-intval;
-  return intval;
-} /* NearestInt() */
-
 // Returns CRC24
 ////////////////////////////////////////////////////////////////////////////
-static unsigned long CRC24(long size, const unsigned char *buf)
-{
+static unsigned long CRC24(long size, const unsigned char *buf) {
   unsigned long crc = 0;
-  int i;
-
-  while(size--)
-  {
+  int ii;
+  while (size--) {
     crc ^= (*buf++) << (16);
-    for(i = 0; i < 8; i++)
-    {
+    for(ii = 0; ii < 8; ii++) {
       crc <<= 1;
-      if(crc & 0x1000000)
+      if (crc & 0x1000000) {
         crc ^= 0x01864cfb;
+      }
     }
   }
   return crc;
-} /* CRC24 */
-
-// 
-////////////////////////////////////////////////////////////////////////////
-bool t_eph::isNewerThan(const t_eph* eph) const {
-  if (_GPSweek >  eph->_GPSweek ||
-      (_GPSweek == eph->_GPSweek && _GPSweeks > eph->_GPSweeks)) {
-    return true;
-  }
-  else {
-    return false;
-  }
 }
 
@@ -68,43 +38,43 @@
   _prn = QString("G%1").arg(ee->satellite, 2, 10, QChar('0'));
 
-  // TODO: check if following two lines are correct
-  _GPSweek  = ee->GPSweek;
-  _GPSweeks = ee->TOE;
-
-  _TOW  = ee->TOW;
-  _TOC  = ee->TOC;
-  _TOE  = ee->TOE;
-  _IODE = ee->IODE;
-  _IODC = ee->IODC;
-
+  _TOC.set(ee->GPSweek, ee->TOC);
   _clock_bias      = ee->clock_bias;
   _clock_drift     = ee->clock_drift;
   _clock_driftrate = ee->clock_driftrate;
 
+  _IODE     = ee->IODE;
   _Crs      = ee->Crs;
   _Delta_n  = ee->Delta_n;
   _M0       = ee->M0;
+
   _Cuc      = ee->Cuc;
   _e        = ee->e;
   _Cus      = ee->Cus;
   _sqrt_A   = ee->sqrt_A;
+
+  _TOEsec   = ee->TOE;
   _Cic      = ee->Cic;
   _OMEGA0   = ee->OMEGA0;
   _Cis      = ee->Cis;
+
   _i0       = ee->i0;
   _Crc      = ee->Crc;
   _omega    = ee->omega;
   _OMEGADOT = ee->OMEGADOT;
+
   _IDOT     = ee->IDOT;
-
+  _L2Codes  = 0.0;
+  _TOEweek  = ee->TOW;
+  _L2PFlag  = 0.0;
+
+  _ura      = 0.0;
+  _health   = ee->SVhealth;
   _TGD      = ee->TGD;
+  _IODC     = ee->IODC;
+
+  _TOT         = 0.9999e9;
+  _fitInterval = 0.0;
 
   _ok       = true;
-
-  /* FIXME: convert URAindex and flags! */
-  _ura = 0;
-  _L2Codes = 0;
-  _L2PFlag = 0;
-  _health = ee->SVhealth;
 }
 
@@ -112,8 +82,8 @@
 ////////////////////////////////////////////////////////////////////////////
 void t_ephGPS::position(int GPSweek, double GPSweeks, 
-			double* xc,
-			double* vv) const {
-
-  static const double secPerWeek = 7 * 86400.0;
+                        double* xc,
+                        double* vv) const {
+
+
   static const double omegaEarth = 7292115.1467e-11;
   static const double gmWGS      = 398.6005e12;
@@ -128,8 +98,8 @@
 
   double n0 = sqrt(gmWGS/(a0*a0*a0));
-  double tk = GPSweeks - _TOE;
-  if (GPSweek != _GPSweek) {  
-    tk += (GPSweek - _GPSweek) * secPerWeek;
-  }
+
+  bncTime tt(GPSweek, GPSweeks);
+  double tk = tt - bncTime(_TOEweek, _TOEsec);
+
   double n  = n0 + _Delta_n;
   double M  = _M0 + n*tk;
@@ -150,5 +120,5 @@
   double yp     = r*sin(u);
   double OM     = _OMEGA0 + (_OMEGADOT - omegaEarth)*tk - 
-                   omegaEarth*_TOE;
+                   omegaEarth*_TOEsec;
   
   double sinom = sin(OM);
@@ -160,8 +130,5 @@
   xc[2] = yp*sini;                 
   
-  double tc = GPSweeks - _TOC;
-  if (GPSweek != _GPSweek) {  
-    tc += (GPSweek - _GPSweek) * secPerWeek;
-  }
+  double tc = tt - _TOC;
   xc[3] = _clock_bias + _clock_drift*tc + _clock_driftrate*tc*tc;
 
@@ -192,5 +159,4 @@
   // Relativistic Correction
   // -----------------------
-  //  xc(4) -= 4.442807633e-10 * _e * sqrt(a0) *sin(E);
   xc[3] -= 2.0 * (xc[0]*vv[0] + xc[1]*vv[1] + xc[2]*vv[2]) / t_CST::c / t_CST::c;
 }
@@ -198,5 +164,5 @@
 // build up RTCM3 for GPS
 ////////////////////////////////////////////////////////////////////////////
-#define GPSTOINT(type, value) static_cast<type>(NearestInt(value,0))
+#define GPSTOINT(type, value) static_cast<type>(round(value))
 
 #define GPSADDBITS(a, b) {bitbuffer = (bitbuffer<<(a)) \
@@ -205,9 +171,9 @@
                        while(numbits >= 8) { \
                        buffer[size++] = bitbuffer>>(numbits-8);numbits -= 8;}}
+
 #define GPSADDBITSFLOAT(a,b,c) {long long i = GPSTOINT(long long,(b)/(c)); \
                              GPSADDBITS(a,i)};
 
-int t_ephGPS::RTCM3(unsigned char *buffer)
-{
+int t_ephGPS::RTCM3(unsigned char *buffer) {
 
   unsigned char *startbuffer = buffer;
@@ -267,11 +233,11 @@
   GPSADDBITS(12, 1019)
   GPSADDBITS(6,_prn.right((_prn.length()-1)).toInt())
-  GPSADDBITS(10, _GPSweek)
+  GPSADDBITS(10, _TOC.gpsw())
   GPSADDBITS(4, _ura)
   GPSADDBITS(2,_L2Codes)
-  GPSADDBITSFLOAT(14, _IDOT, PI/static_cast<double>(1<<30)
+  GPSADDBITSFLOAT(14, _IDOT, M_PI/static_cast<double>(1<<30)
   /static_cast<double>(1<<13))
   GPSADDBITS(8, _IODE)
-  GPSADDBITS(16, static_cast<int>(_TOC)>>4)
+  GPSADDBITS(16, static_cast<int>(_TOC.gpssec())>>4)
   GPSADDBITSFLOAT(8, _clock_driftrate, 1.0/static_cast<double>(1<<30)
   /static_cast<double>(1<<25))
@@ -282,21 +248,21 @@
   GPSADDBITS(10, _IODC)
   GPSADDBITSFLOAT(16, _Crs, 1.0/static_cast<double>(1<<5))
-  GPSADDBITSFLOAT(16, _Delta_n, PI/static_cast<double>(1<<30)
+  GPSADDBITSFLOAT(16, _Delta_n, M_PI/static_cast<double>(1<<30)
   /static_cast<double>(1<<13))
-  GPSADDBITSFLOAT(32, _M0, PI/static_cast<double>(1<<30)/static_cast<double>(1<<1))
+  GPSADDBITSFLOAT(32, _M0, M_PI/static_cast<double>(1<<30)/static_cast<double>(1<<1))
   GPSADDBITSFLOAT(16, _Cuc, 1.0/static_cast<double>(1<<29))
   GPSADDBITSFLOAT(32, _e, 1.0/static_cast<double>(1<<30)/static_cast<double>(1<<3))
   GPSADDBITSFLOAT(16, _Cus, 1.0/static_cast<double>(1<<29))
   GPSADDBITSFLOAT(32, _sqrt_A, 1.0/static_cast<double>(1<<19))
-  GPSADDBITS(16, static_cast<int>(_TOE)>>4)
+  GPSADDBITS(16, static_cast<int>(_TOEsec)>>4)
   GPSADDBITSFLOAT(16, _Cic, 1.0/static_cast<double>(1<<29))
-  GPSADDBITSFLOAT(32, _OMEGA0, PI/static_cast<double>(1<<30)
+  GPSADDBITSFLOAT(32, _OMEGA0, M_PI/static_cast<double>(1<<30)
   /static_cast<double>(1<<1))
   GPSADDBITSFLOAT(16, _Cis, 1.0/static_cast<double>(1<<29))
-  GPSADDBITSFLOAT(32, _i0, PI/static_cast<double>(1<<30)/static_cast<double>(1<<1))
+  GPSADDBITSFLOAT(32, _i0, M_PI/static_cast<double>(1<<30)/static_cast<double>(1<<1))
   GPSADDBITSFLOAT(16, _Crc, 1.0/static_cast<double>(1<<5))
-  GPSADDBITSFLOAT(32, _omega, PI/static_cast<double>(1<<30)
+  GPSADDBITSFLOAT(32, _omega, M_PI/static_cast<double>(1<<30)
   /static_cast<double>(1<<1))
-  GPSADDBITSFLOAT(24, _OMEGADOT, PI/static_cast<double>(1<<30)
+  GPSADDBITSFLOAT(24, _OMEGADOT, M_PI/static_cast<double>(1<<30)
   /static_cast<double>(1<<13))
   GPSADDBITSFLOAT(8, _TGD, 1.0/static_cast<double>(1<<30)/static_cast<double>(1<<1))
@@ -358,5 +324,4 @@
                         double* xc, double* vv) const {
 
-  static const double secPerWeek  = 7 * 86400.0;
   static const double nominalStep = 10.0;
 
@@ -364,8 +329,5 @@
   memset(vv, 0, 3*sizeof(double));
 
-  double dtPos = GPSweeks - _tt;
-  if (GPSweek != _GPSweek) {  
-    dtPos += (GPSweek - _GPSweek) * secPerWeek;
-  }
+  double dtPos = bncTime(GPSweek, GPSweeks) - _tt;
 
   int nSteps  = int(fabs(dtPos) / nominalStep) + 1;
@@ -377,6 +339,6 @@
   acc[2] = _z_acceleration * 1.e3;
   for (int ii = 1; ii <= nSteps; ii++) { 
-    _xv = rungeKutta4(_tt, _xv, step, acc, glo_deriv);
-    _tt += step;
+    _xv = rungeKutta4(_tt.gpssec(), _xv, step, acc, glo_deriv);
+    _tt = _tt + step;
   }
 
@@ -393,8 +355,5 @@
   // Clock Correction
   // ----------------
-  double dtClk = GPSweeks - _GPSweeks;
-  if (GPSweek != _GPSweek) {  
-    dtClk += (GPSweek - _GPSweek) * secPerWeek;
-  }
+  double dtClk = bncTime(GPSweek, GPSweeks) - _TOC;
   xc[3] = -_tau + _gamma * dtClk;
 }
@@ -403,6 +362,5 @@
 ////////////////////////////////////////////////////////////////////////////
 int t_ephGlo::IOD() const {
-  bncTime tGPS(_GPSweek, _GPSweeks);
-  bncTime tMoscow = tGPS - _gps_utc + 3 * 3600.0;
+  bncTime tMoscow = _TOC - _gps_utc + 3 * 3600.0;
   return int(tMoscow.daysec() / 900);
 }
@@ -472,6 +430,5 @@
   _gps_utc = gnumleap(year, month, day);
 
-  _GPSweek           = ww;
-  _GPSweeks          = tow;
+  _TOC.set(ww, tow);
   _E                 = ee->E;
   _tau               = ee->tau;
@@ -492,5 +449,5 @@
   // Initialize status vector
   // ------------------------
-  _tt = _GPSweeks;
+  _tt = _TOC;
 
   _xv(1) = _x_pos * 1.e3; 
@@ -506,5 +463,5 @@
 // build up RTCM3 for GLONASS
 ////////////////////////////////////////////////////////////////////////////
-#define GLONASSTOINT(type, value) static_cast<type>(NearestInt(value,0))
+#define GLONASSTOINT(type, value) static_cast<type>(round(value))
 
 #define GLONASSADDBITS(a, b) {bitbuffer = (bitbuffer<<(a)) \
@@ -549,5 +506,5 @@
   GLONASSADDBITS(1, _health) 
   GLONASSADDBITS(1, 0)
-  unsigned long long timeofday = (static_cast<int>(_tt+3*60*60-_gps_utc)%86400);
+  unsigned long long timeofday = (static_cast<int>(_tt.gpssec()+3*60*60-_gps_utc)%86400);
   GLONASSADDBITS(7, timeofday/60/15)
   GLONASSADDBITSFLOATM(24, _x_velocity*1000, 1000.0/static_cast<double>(1<<20))
@@ -597,34 +554,38 @@
   _prn = QString("E%1").arg(ee->satellite, 2, 10, QChar('0'));
 
-  _GPSweek  = ee->Week;
-  _GPSweeks = ee->TOE;
-
-  _TOC    = ee->TOC;
-  _TOE    = ee->TOE;
-  _IODnav = ee->IODnav;
-
+  _TOC.set(ee->Week, ee->TOC);
   _clock_bias      = ee->clock_bias;
   _clock_drift     = ee->clock_drift;
   _clock_driftrate = ee->clock_driftrate;
 
+  _IODnav   = ee->IODnav;
   _Crs      = ee->Crs;
   _Delta_n  = ee->Delta_n;
   _M0       = ee->M0;
+
   _Cuc      = ee->Cuc;
   _e        = ee->e;
   _Cus      = ee->Cus;
   _sqrt_A   = ee->sqrt_A;
+
+  _TOEsec   = ee->TOE;
   _Cic      = ee->Cic;
   _OMEGA0   = ee->OMEGA0;
   _Cis      = ee->Cis;
+
   _i0       = ee->i0;
   _Crc      = ee->Crc;
   _omega    = ee->omega;
   _OMEGADOT = ee->OMEGADOT;
+
   _IDOT     = ee->IDOT;
+  _TOEweek  = ee->Week;
+
   _SISA     = ee->SISA;
+  _E5aHS    = ee->E5aHS;
   _BGD_1_5A = ee->BGD_1_5A;
   _BGD_1_5B = ee->BGD_1_5B;
-  _E5aHS    = ee->E5aHS;
+
+  _TOT      = 0.9999e9;
 
   _ok = true;
@@ -634,8 +595,7 @@
 ////////////////////////////////////////////////////////////////////////////
 void t_ephGal::position(int GPSweek, double GPSweeks, 
-			double* xc,
-			double* vv) const {
-
-  static const double secPerWeek = 7 * 86400.0;
+                        double* xc,
+                        double* vv) const {
+
   static const double omegaEarth = 7292115.1467e-11;
   static const double gmWGS      = 398.6005e12;
@@ -650,8 +610,8 @@
 
   double n0 = sqrt(gmWGS/(a0*a0*a0));
-  double tk = GPSweeks - _TOE;
-  if (GPSweek != _GPSweek) {  
-    tk += (GPSweek - _GPSweek) * secPerWeek;
-  }
+
+  bncTime tt(GPSweek, GPSweeks);
+  double tk = tt - bncTime(_TOC.gpsw(), _TOEsec);
+
   double n  = n0 + _Delta_n;
   double M  = _M0 + n*tk;
@@ -672,5 +632,5 @@
   double yp     = r*sin(u);
   double OM     = _OMEGA0 + (_OMEGADOT - omegaEarth)*tk - 
-                   omegaEarth*_TOE;
+                  omegaEarth*_TOEsec;
   
   double sinom = sin(OM);
@@ -682,8 +642,5 @@
   xc[2] = yp*sini;                 
   
-  double tc = GPSweeks - _TOC;
-  if (GPSweek != _GPSweek) {  
-    tc += (GPSweek - _GPSweek) * secPerWeek;
-  }
+  double tc = tt - _TOC;
   xc[3] = _clock_bias + _clock_drift*tc + _clock_driftrate*tc*tc;
 
@@ -720,5 +677,5 @@
 // build up RTCM3 for Galileo
 ////////////////////////////////////////////////////////////////////////////
-#define GALILEOTOINT(type, value) static_cast<type>(NearestInt(value, 0))
+#define GALILEOTOINT(type, value) static_cast<type>(round(value))
 
 #define GALILEOADDBITS(a, b) {bitbuffer = (bitbuffer<<(a)) \
@@ -739,10 +696,10 @@
   GALILEOADDBITS(12, /*inav ? 1046 :*/ 1045)
   GALILEOADDBITS(6, _prn.right((_prn.length()-1)).toInt())
-  GALILEOADDBITS(12, _GPSweek)
+  GALILEOADDBITS(12, _TOC.gpsw())
   GALILEOADDBITS(10, _IODnav)
   GALILEOADDBITS(8, _SISA)
-  GALILEOADDBITSFLOAT(14, _IDOT, PI/static_cast<double>(1<<30)
+  GALILEOADDBITSFLOAT(14, _IDOT, M_PI/static_cast<double>(1<<30)
   /static_cast<double>(1<<13))
-  GALILEOADDBITS(14, _TOC/60)
+  GALILEOADDBITS(14, _TOC.gpssec()/60)
   GALILEOADDBITSFLOAT(6, _clock_driftrate, 1.0/static_cast<double>(1<<30)
   /static_cast<double>(1<<29))
@@ -752,21 +709,21 @@
   /static_cast<double>(1<<4))
   GALILEOADDBITSFLOAT(16, _Crs, 1.0/static_cast<double>(1<<5))
-  GALILEOADDBITSFLOAT(16, _Delta_n, PI/static_cast<double>(1<<30)
+  GALILEOADDBITSFLOAT(16, _Delta_n, M_PI/static_cast<double>(1<<30)
   /static_cast<double>(1<<13))
-  GALILEOADDBITSFLOAT(32, _M0, PI/static_cast<double>(1<<30)/static_cast<double>(1<<1))
+  GALILEOADDBITSFLOAT(32, _M0, M_PI/static_cast<double>(1<<30)/static_cast<double>(1<<1))
   GALILEOADDBITSFLOAT(16, _Cuc, 1.0/static_cast<double>(1<<29))
   GALILEOADDBITSFLOAT(32, _e, 1.0/static_cast<double>(1<<30)/static_cast<double>(1<<3))
   GALILEOADDBITSFLOAT(16, _Cus, 1.0/static_cast<double>(1<<29))
   GALILEOADDBITSFLOAT(32, _sqrt_A, 1.0/static_cast<double>(1<<19))
-  GALILEOADDBITS(14, _TOE/60)
+  GALILEOADDBITS(14, _TOEsec/60)
   GALILEOADDBITSFLOAT(16, _Cic, 1.0/static_cast<double>(1<<29))
-  GALILEOADDBITSFLOAT(32, _OMEGA0, PI/static_cast<double>(1<<30)
+  GALILEOADDBITSFLOAT(32, _OMEGA0, M_PI/static_cast<double>(1<<30)
   /static_cast<double>(1<<1))
   GALILEOADDBITSFLOAT(16, _Cis, 1.0/static_cast<double>(1<<29))
-  GALILEOADDBITSFLOAT(32, _i0, PI/static_cast<double>(1<<30)/static_cast<double>(1<<1))
+  GALILEOADDBITSFLOAT(32, _i0, M_PI/static_cast<double>(1<<30)/static_cast<double>(1<<1))
   GALILEOADDBITSFLOAT(16, _Crc, 1.0/static_cast<double>(1<<5))
-  GALILEOADDBITSFLOAT(32, _omega, PI/static_cast<double>(1<<30)
+  GALILEOADDBITSFLOAT(32, _omega, M_PI/static_cast<double>(1<<30)
   /static_cast<double>(1<<1))
-  GALILEOADDBITSFLOAT(24, _OMEGADOT, PI/static_cast<double>(1<<30)
+  GALILEOADDBITSFLOAT(24, _OMEGADOT, M_PI/static_cast<double>(1<<30)
   /static_cast<double>(1<<13))
   GALILEOADDBITSFLOAT(10, _BGD_1_5A, 1.0/static_cast<double>(1<<30)
@@ -784,6 +741,6 @@
     GALILEOADDBITS(1, /*flags & MNFGALEPHF_E5ADINVALID*/0)
   }
-  _TOE = 0.9999E9;
-  GALILEOADDBITS(20, _TOE)
+  _TOEsec = 0.9999E9;
+  GALILEOADDBITS(20, _TOEsec)
 
   GALILEOADDBITS(/*inav ? 1 :*/ 3, 0) /* fill up */
@@ -846,9 +803,5 @@
       }
 
-      bncTime hlpTime;
-      hlpTime.set(year, month, day, hour, min, sec);
-      _GPSweek  = hlpTime.gpsw();
-      _GPSweeks = hlpTime.gpssec();
-      _TOC      = _GPSweeks;
+      _TOC.set(year, month, day, hour, min, sec);
 
       if ( readDbl(line, pos[1], fieldLen, _clock_bias     ) ||
@@ -878,5 +831,5 @@
 
     else if ( iLine == 3 ) {
-      if ( readDbl(line, pos[0], fieldLen, _TOE   )  ||
+      if ( readDbl(line, pos[0], fieldLen, _TOEsec)  ||
            readDbl(line, pos[1], fieldLen, _Cic   )  ||
            readDbl(line, pos[2], fieldLen, _OMEGA0)  ||
@@ -896,9 +849,8 @@
 
     else if ( iLine == 5 ) {
-      double dummy, TOEw;
-      if ( readDbl(line, pos[0], fieldLen, _IDOT) ||
-           readDbl(line, pos[1], fieldLen, dummy) ||
-           readDbl(line, pos[2], fieldLen, TOEw ) ||
-           readDbl(line, pos[3], fieldLen, dummy) ) {
+      if ( readDbl(line, pos[0], fieldLen, _IDOT   ) ||
+           readDbl(line, pos[1], fieldLen, _L2Codes) ||
+           readDbl(line, pos[2], fieldLen, _TOEweek  ) ||
+           readDbl(line, pos[3], fieldLen, _L2PFlag) ) {
         return;
       }
@@ -906,6 +858,5 @@
 
     else if ( iLine == 6 ) {
-      double dummy;
-      if ( readDbl(line, pos[0], fieldLen, dummy  ) ||
+      if ( readDbl(line, pos[0], fieldLen, _ura   ) ||
            readDbl(line, pos[1], fieldLen, _health) ||
            readDbl(line, pos[2], fieldLen, _TGD   ) ||
@@ -916,6 +867,6 @@
 
     else if ( iLine == 7 ) {
-      double TOT;
-      if ( readDbl(line, pos[0], fieldLen, TOT) ) {
+      if ( readDbl(line, pos[0], fieldLen, _TOT)         ||
+           readDbl(line, pos[1], fieldLen, _fitInterval) ) {
         return;
       }
@@ -972,19 +923,12 @@
       }
 
-      bncTime hlpTime;
-      hlpTime.set(year, month, day, hour, min, sec);
-
       _gps_utc = gnumleap(year, month, day);
-      hlpTime  = hlpTime + _gps_utc;
-
-      _GPSweek  = hlpTime.gpsw();
-      _GPSweeks = hlpTime.gpssec();
-
-      _tki     = 0.0; // TODO ?
-
-      double second_tot;
-      if ( readDbl(line, pos[1], fieldLen, _tau      ) ||
-           readDbl(line, pos[2], fieldLen, _gamma    ) ||
-           readDbl(line, pos[3], fieldLen, second_tot) ) {
+
+      _TOC.set(year, month, day, hour, min, sec);
+      _TOC  = _TOC + _gps_utc;
+
+      if ( readDbl(line, pos[1], fieldLen, _tau  ) ||
+           readDbl(line, pos[2], fieldLen, _gamma) ||
+           readDbl(line, pos[3], fieldLen, _tki  ) ) {
         return;
       }
@@ -1023,8 +967,6 @@
   // Initialize status vector
   // ------------------------
-  _tt = _GPSweeks;
-
-   _xv.ReSize(6); 
-
+  _tt = _TOC;
+  _xv.ReSize(6); 
   _xv(1) = _x_pos * 1.e3; 
   _xv(2) = _y_pos * 1.e3; 
@@ -1062,9 +1004,8 @@
   QString rnxStr;
   
-  bncTime tt(_GPSweek, _GPSweeks);
   unsigned year, month, day, hour, min;
-  double sec;
-  tt.civil_date(year, month, day);
-  tt.civil_time(hour, min, sec);
+  double   sec;
+  _TOC.civil_date(year, month, day);
+  _TOC.civil_time(hour, min, sec);
   
   QTextStream out(&rnxStr);
@@ -1110,5 +1051,5 @@
 
   out << QString(fmt)
-    .arg(_TOE,    19, 'e', 12)
+    .arg(_TOEsec, 19, 'e', 12)
     .arg(_Cic,    19, 'e', 12)
     .arg(_OMEGA0, 19, 'e', 12)
@@ -1122,11 +1063,11 @@
 
   out << QString(fmt)
-    .arg(_IDOT, 19, 'e', 12)
-    .arg(0.0,   19, 'e', 12)
-    .arg(0.0,   19, 'e', 12)
-    .arg(0.0,   19, 'e', 12);
+    .arg(_IDOT,    19, 'e', 12)
+    .arg(_L2Codes, 19, 'e', 12)
+    .arg(_TOEweek, 19, 'e', 12)
+    .arg(_L2PFlag, 19, 'e', 12);
 
   out << QString(fmt)
-    .arg(0.0,     19, 'e', 12)
+    .arg(_ura,    19, 'e', 12)
     .arg(_health, 19, 'e', 12)
     .arg(_TGD,    19, 'e', 12)
@@ -1134,6 +1075,6 @@
 
   out << QString(fmt)
-    .arg(0.0, 19, 'e', 12)
-    .arg(0.0, 19, 'e', 12)
+    .arg(_TOT,         19, 'e', 12)
+    .arg(_fitInterval, 19, 'e', 12)
     .arg("")
     .arg("");
Index: /trunk/BNC/RTCM3/ephemeris.h
===================================================================
--- /trunk/BNC/RTCM3/ephemeris.h	(revision 4017)
+++ /trunk/BNC/RTCM3/ephemeris.h	(revision 4018)
@@ -6,4 +6,5 @@
 #include <stdio.h>
 #include <string>
+#include "bnctime.h"
 extern "C" {
 #include "rtcm3torinex.h"
@@ -15,36 +16,28 @@
   enum e_type {unknown, GPS, GLONASS, Galileo};
 
-  static bool earlierTime(const t_eph* eph1, const t_eph* eph2) {
-    if      (eph1->_GPSweek < eph2->_GPSweek) {
-      return true;
-    }
-    else if (eph1->_GPSweek == eph2->_GPSweek) {
-      return eph1->_GPSweeks < eph2->_GPSweeks;
-    }
-    else {
-      return false;
-    }
-  }
-
   t_eph() {_ok = false;}
   virtual ~t_eph() {};
 
+  static bool earlierTime(const t_eph* eph1, const t_eph* eph2) {
+    return eph1->_TOC < eph2->_TOC;
+  }
+
   virtual e_type type() const = 0;
-
   virtual QString toString(double version) const = 0;
-
-  bool     ok() const {return _ok;}
-  bool     isNewerThan(const t_eph* eph) const;
-  QString  prn() const {return _prn;}
-  void    setReceptDateTime(const QDateTime& dateTime) {
+  virtual void position(int GPSweek, double GPSweeks, 
+                        double* xc, double* vv) const = 0;
+  virtual int  IOD() const = 0;
+  virtual int  RTCM3(unsigned char *) = 0;
+
+  bool ok() const {return _ok;}
+  bncTime TOC() const {return _TOC;}
+  bool isNewerThan(const t_eph* eph) const {
+    return earlierTime(this, eph);
+  }
+  QString prn() const {return _prn;}
+  void  setReceptDateTime(const QDateTime& dateTime) {
     _receptDateTime = dateTime;
   }
   const QDateTime& receptDateTime() const {return _receptDateTime;}
-
-  int    GPSweek()  const { return _GPSweek; }
-  double GPSweeks() const { return _GPSweeks; }
-
-  virtual void position(int GPSweek, double GPSweeks, 
-                        double* xc, double* vv) const = 0;
 
   void position(int GPSweek, double GPSweeks, 
@@ -52,5 +45,4 @@
     double tmp_xx[4];
     double tmp_vv[4];
-
     position(GPSweek, GPSweeks, tmp_xx, tmp_vv);
 
@@ -61,12 +53,7 @@
   }
 
-  virtual int  IOD() const = 0;
-  
-  virtual int  RTCM3(unsigned char *) = 0;
-
  protected:  
   QString   _prn;
-  int       _GPSweek;
-  double    _GPSweeks;
+  bncTime   _TOC;
   QDateTime _receptDateTime;
   bool      _ok;
@@ -84,6 +71,4 @@
   virtual QString toString(double version) const;
 
-  double TOC() const {return _TOC;}
-
   void set(const gpsephemeris* ee);
 
@@ -97,134 +82,149 @@
 
  private:
-  double  _TOW;              //  [s]    
-  double  _TOC;              //  [s]    
-  double  _TOE;              //  [s]    
-  double  _IODE;             
-  double  _IODC;             
-
+  double  _clock_bias;      // [s]    
+  double  _clock_drift;     // [s/s]  
+  double  _clock_driftrate; // [s/s^2]
+
+  double  _IODE;            
+  double  _Crs;             // [m]    
+  double  _Delta_n;         // [rad/s]
+  double  _M0;              // [rad]  
+
+  double  _Cuc;             // [rad]  
+  double  _e;               //        
+  double  _Cus;             // [rad]  
+  double  _sqrt_A;          // [m^0.5]
+
+  double  _TOEsec;          // [s]    
+  double  _Cic;             // [rad]  
+  double  _OMEGA0;          // [rad]  
+  double  _Cis;             // [rad]  
+
+  double  _i0;              // [rad]  
+  double  _Crc;             // [m]    
+  double  _omega;           // [rad]  
+  double  _OMEGADOT;        // [rad/s]
+
+  double  _IDOT;            // [rad/s]
+  double  _L2Codes;         // Codes on L2 channel 
+  double  _TOEweek;
+  double  _L2PFlag;         // L2 P data flag
+
+  double  _ura;             // SV accuracy
+  double  _health;          // SV health
+  double  _TGD;             // [s]    
+  double  _IODC;            
+
+  double  _TOT;             // Transmisstion time 
+  double  _fitInterval;     // Fit interval
+};
+
+class t_ephGlo : public t_eph {
+ public:
+  t_ephGlo() { _xv.ReSize(6); }
+  t_ephGlo(float rnxVersion, const QStringList& lines);
+
+  virtual ~t_ephGlo() {}
+
+  virtual e_type type() const {return t_eph::GLONASS;}
+
+  virtual QString toString(double version) const;
+
+  virtual void position(int GPSweek, double GPSweeks, 
+                        double* xc,
+                        double* vv) const;
+
+  virtual int  IOD() const;
+
+  virtual int  RTCM3(unsigned char *);
+
+  void set(const glonassephemeris* ee);
+
+  int  slotNum() const {return int(_frequency_number);}
+
+ private:
+  static ColumnVector glo_deriv(double /* tt */, const ColumnVector& xv,
+                                double* acc);
+
+  mutable bncTime      _tt;  // time 
+  mutable ColumnVector _xv;  // status vector (position, velocity) at time _tt
+
+  double  _gps_utc;
+  double  _tau;              // [s]      
+  double  _gamma;            //          
+  double  _tki;              // message frame time
+
+  double  _x_pos;            // [km]     
+  double  _x_velocity;       // [km/s]   
+  double  _x_acceleration;   // [km/s^2] 
+  double  _health;           // 0 = O.K. 
+
+  double  _y_pos;            // [km]     
+  double  _y_velocity;       // [km/s]   
+  double  _y_acceleration;   // [km/s^2] 
+  double  _frequency_number; // ICD-GLONASS data position 
+
+  double  _z_pos;            // [km]     
+  double  _z_velocity;       // [km/s]   
+  double  _z_acceleration;   // [km/s^2] 
+  double  _E;                // Age of Information [days]   
+};
+
+class t_ephGal : public t_eph {
+ public:
+  t_ephGal() { }
+  t_ephGal(float rnxVersion, const QStringList& lines);
+  virtual ~t_ephGal() {}
+
+  virtual QString toString(double version) const;
+
+  virtual e_type type() const {return t_eph::Galileo;}
+
+  void set(const galileoephemeris* ee);
+
+  virtual void position(int GPSweek, double GPSweeks, 
+                        double* xc,
+                        double* vv) const;
+
+  virtual int  IOD() const { return static_cast<int>(_IODnav); }
+
+  virtual int  RTCM3(unsigned char *);
+
+ private:
   double  _clock_bias;       //  [s]    
   double  _clock_drift;      //  [s/s]  
   double  _clock_driftrate;  //  [s/s^2]
 
+  double  _IODnav;             
   double  _Crs;              //  [m]    
   double  _Delta_n;          //  [rad/s]
   double  _M0;               //  [rad]  
+
   double  _Cuc;              //  [rad]  
   double  _e;                //         
   double  _Cus;              //  [rad]  
   double  _sqrt_A;           //  [m^0.5]
+
+  double  _TOEsec;           //  [s]    
   double  _Cic;              //  [rad]  
   double  _OMEGA0;           //  [rad]  
   double  _Cis;              //  [rad]  
+
   double  _i0;               //  [rad]  
   double  _Crc;              //  [m]    
   double  _omega;            //  [rad]  
   double  _OMEGADOT;         //  [rad/s]
+
   double  _IDOT;             //  [rad/s]
-
-  double  _TGD;              //  [s]    
-  double _health;            //  SV health
-  double _ura;               //  SV accuracy
-  double _L2PFlag;           //  L2 P data flag
-  double _L2Codes;           //  Codes on L2 channel 
-};
-
-class t_ephGlo : public t_eph {
- public:
-  t_ephGlo() { _xv.ReSize(6); }
-  t_ephGlo(float rnxVersion, const QStringList& lines);
-
-  virtual ~t_ephGlo() {}
-
-  virtual e_type type() const {return t_eph::GLONASS;}
-
-  virtual QString toString(double version) const;
-
-  virtual void position(int GPSweek, double GPSweeks, 
-                        double* xc,
-                        double* vv) const;
-
-  virtual int  IOD() const;
-
-  virtual int  RTCM3(unsigned char *);
-
-  void set(const glonassephemeris* ee);
-
-  int  slotNum() const {return int(_frequency_number);}
-
- private:
-  static ColumnVector glo_deriv(double /* tt */, const ColumnVector& xv,
-                                double* acc);
-
-  mutable double       _tt;  // time in seconds of GPSweek
-  mutable ColumnVector _xv;  // status vector (position, velocity) at time _tt
-
-  double  _gps_utc;
-  double  _E;                // [days]   
-  double  _tau;              // [s]      
-  double  _gamma;            //          
-  double  _x_pos;            // [km]     
-  double  _x_velocity;       // [km/s]   
-  double  _x_acceleration;   // [km/s^2] 
-  double  _y_pos;            // [km]     
-  double  _y_velocity;       // [km/s]   
-  double  _y_acceleration;   // [km/s^2] 
-  double  _z_pos;            // [km]     
-  double  _z_velocity;       // [km/s]   
-  double  _z_acceleration;   // [km/s^2] 
-  double  _health;           // 0 = O.K. 
-  double  _frequency_number; // ICD-GLONASS data position 
-  double  _tki;              // message frame time
-};
-
-class t_ephGal : public t_eph {
- public:
-  t_ephGal() { }
-  t_ephGal(float rnxVersion, const QStringList& lines);
-  virtual ~t_ephGal() {}
-
-  virtual QString toString(double version) const;
-
-  virtual e_type type() const {return t_eph::Galileo;}
-
-  double TOC() const {return _TOC;}
-
-  void set(const galileoephemeris* ee);
-
-  virtual void position(int GPSweek, double GPSweeks, 
-                        double* xc,
-                        double* vv) const;
-
-  virtual int  IOD() const { return static_cast<int>(_IODnav); }
-
-  virtual int  RTCM3(unsigned char *);
-
- private:
-  double  _IODnav;             
-  double  _TOC;              //  [s]    
-  double  _TOE;              //  [s]    
-  double  _clock_bias;       //  [s]    
-  double  _clock_drift;      //  [s/s]  
-  double  _clock_driftrate;  //  [s/s^2]
-  double  _Crs;              //  [m]    
-  double  _Delta_n;          //  [rad/s]
-  double  _M0;               //  [rad]  
-  double  _Cuc;              //  [rad]  
-  double  _e;                //         
-  double  _Cus;              //  [rad]  
-  double  _sqrt_A;           //  [m^0.5]
-  double  _Cic;              //  [rad]  
-  double  _OMEGA0;           //  [rad]  
-  double  _Cis;              //  [rad]  
-  double  _i0;               //  [rad]  
-  double  _Crc;              //  [m]    
-  double  _omega;            //  [rad]  
-  double  _OMEGADOT;         //  [rad/s]
-  double  _IDOT;             //  [rad/s]
+  //
+  double _TOEweek;
+  // spare
+
+  int     _SISA;             //  Signal In Space Accuracy
+  int     _E5aHS;            //  E5a Health Status
   double  _BGD_1_5A;         //  group delay [s] 
   double  _BGD_1_5B;         //  group delay [s] 
-  int     _SISA;             //  Signal In Space Accuracy
-  int     _E5aHS;            //  E5a Health Status
-
+
+  double _TOT;               // [s]
 };
 
Index: /trunk/BNC/bncephuser.cpp
===================================================================
--- /trunk/BNC/bncephuser.cpp	(revision 4017)
+++ /trunk/BNC/bncephuser.cpp	(revision 4018)
@@ -81,7 +81,6 @@
   if (_eph.contains(prn)) {
     t_ephGPS* eLast = static_cast<t_ephGPS*>(_eph.value(prn)->last);
-    if ( (eLast->GPSweek() <  gpseph.GPSweek) || 
-         (eLast->GPSweek() == gpseph.GPSweek &&  
-          eLast->TOC()     <  gpseph.TOC) ) {
+    bncTime toc(gpseph.GPSweek, gpseph.TOC);
+    if (eLast->TOC() < toc) {
       delete static_cast<t_ephGPS*>(_eph.value(prn)->prev);
       _eph.value(prn)->prev = _eph.value(prn)->last;
@@ -110,6 +109,6 @@
     updatetime(&ww, &tow, gloeph.tb*1000, 0);  // Moscow -> GPS
     t_ephGlo* eLast = static_cast<t_ephGlo*>(_eph.value(prn)->last);
-    if (eLast->GPSweek() < ww || 
-        (eLast->GPSweek()  == ww &&  eLast->GPSweeks() <  tow)) {  
+    bncTime toc(ww, tow);
+    if (eLast->TOC() < toc) {
       delete static_cast<t_ephGlo*>(_eph.value(prn)->prev);
       _eph.value(prn)->prev = _eph.value(prn)->last;
@@ -135,7 +134,6 @@
   if (_eph.contains(prn)) {
     t_ephGal* eLast = static_cast<t_ephGal*>(_eph.value(prn)->last);
-    if ( (eLast->GPSweek() <  galeph.Week) || 
-         (eLast->GPSweek() == galeph.Week &&  
-          eLast->TOC()     <  galeph.TOC) ) {
+    bncTime toc(galeph.Week, galeph.TOC);
+    if (eLast->TOC() < toc) {
       delete static_cast<t_ephGal*>(_eph.value(prn)->prev);
       _eph.value(prn)->prev = _eph.value(prn)->last;
Index: /trunk/BNC/rinex/rnxnavfile.cpp
===================================================================
--- /trunk/BNC/rinex/rnxnavfile.cpp	(revision 4017)
+++ /trunk/BNC/rinex/rnxnavfile.cpp	(revision 4018)
@@ -223,6 +223,5 @@
       t_eph* eph = *it;
 
-      bncTime ephTime(eph->GPSweek(), eph->GPSweeks());
-      double dt = ephTime - tt;
+      double dt = eph->TOC() - tt;
 
       if (dt < 2*3600.0) {
