Index: trunk/BNC/src/PPP/pppClient.cpp
===================================================================
--- trunk/BNC/src/PPP/pppClient.cpp	(revision 9288)
+++ trunk/BNC/src/PPP/pppClient.cpp	(revision 9386)
@@ -309,5 +309,5 @@
     }
     if (!std::isnan(maxRes) && maxRes < BLUNDER) {
-      if (print) {
+      if (print && _numEpoProcessing == 1) {
         LOG.setf(ios::fixed);
         LOG << string(epoTime) << " BANCROFT:"        << ' '
@@ -370,6 +370,5 @@
         offGR = 0.0;
       }
-
-      if (fabs(maxRes) > 1000.0) {
+      if (fabs(maxRes) > OPT->_aprSigOGC) {
         LOG << "t_pppClient::cmpOffGR outlier " << maxResPrn.toString() << " " << maxRes << endl;
         delete obsVector.at(maxResIndex);
@@ -424,5 +423,5 @@
       }
 
-      if (fabs(maxRes) > 1000.0) {
+      if (fabs(maxRes) > OPT->_aprSigOGE) {
         LOG << "t_pppClient::cmpOffGE outlier " << maxResPrn.toString() << " " << maxRes << endl;
         delete obsVector.at(maxResIndex);
@@ -477,5 +476,5 @@
       }
 
-      if (fabs(maxRes) > 1000.0) {
+      if (fabs(maxRes) > OPT->_aprSigOGC) {
         LOG << "t_pppClient::cmpOffGC outlier " << maxResPrn.toString() << " " << maxRes << endl;
         delete obsVector.at(maxResIndex);
@@ -529,6 +528,6 @@
     _output->_trpStdev = _filter->trpStdev();
 
-    _output->_numSat     = _filter->numSat();
-    _output->_hDop       = _filter->HDOP();
+    _output->_numSat   = _filter->numSat();
+    _output->_hDop     = _filter->HDOP();
     _output->_error = false;
   }
@@ -593,22 +592,28 @@
 void t_pppClient::processEpoch(const vector<t_satObs*>& satObs, t_output* output) {
 
-  _historicalRefSats.clear();
   try {
     initOutput(output);
-    int num = 0;
     bool epochReProcessing = false;
-
+    _numEpoProcessing = 0;
     do {
-      num++;
-      if (num == 1) {
-        LOG << "\nPPP of Epoch ";
-        if (!_epoTimeRover.undef()) LOG << string(_epoTimeRover);
-        LOG << "\n---------------------------------------------------------------\n";
-      }
-
+      _numEpoProcessing++;
+      if (_obsPool->refSatChanged()) {
+        if(_filter->datumTransformation() != success) {
+          return finish(failure);
+        }
+        else {
+          _obsPool->saveLastEpoRefSats();
+        }
+      }
       // Prepare Observations of the Rover
       // ---------------------------------
       if (prepareObs(satObs, _obsRover, _epoTimeRover) != success) {
         return finish(failure);
+      }
+
+      if (_numEpoProcessing == 1) {
+        LOG << "\nPPP of Epoch ";
+        if (!_epoTimeRover.undef()) LOG << string(_epoTimeRover);
+        LOG << "\n---------------------------------------------------------------\n";
       }
 
@@ -624,27 +629,19 @@
       }
 
+      if (_opt->_refSatRequired) {
+        if (handleRefSatellites(_obsRover) != success) {
+          _historicalRefSats.clear();
+          return finish(failure);
+        }
+        if (_obsPool->refSatChanged()) {
+            epochReProcessing = true;
+            continue;
+        }
+      }
+
       _offGR = cmpOffGR(_obsRover);
       _offGE = cmpOffGE(_obsRover);
       _offGC = cmpOffGC(_obsRover);
 
-      if (_opt->_refSatRequired) {
-        setRefSatellites(_obsRover);
-        LOG.setf(ios::fixed);
-        QMapIterator<char, t_pppRefSat*> it(_obsPool->getRefSatMap());
-        while (it.hasNext()) {
-          it.next();
-          char   sys = it.key();
-          string prn = it.value()->prn().toString();
-          if (num == 1) {
-            LOG << "set   ";
-          }
-          else {
-            LOG << "reset ";
-          }
-          LOG << string(_epoTimeRover) << " REFSAT  " << sys << ": " << prn << endl;
-        }
-      }
-
-
       // Prepare Pseudo Observations of the Rover
       // ----------------------------------------
@@ -656,13 +653,11 @@
       }
 
-
       // Store last epoch of data
       // ------------------------
       _obsPool->putEpoch(_epoTimeRover, _obsRover, _pseudoObsIono);
-      _obsPool->setHistoricalRefSatList(_historicalRefSats);
 
       // Process Epoch in Filter
       // -----------------------
-      if (_filter->processEpoch(num) != success) {
+      if (_filter->processEpoch() != success) {
         return finish(failure);
       }
@@ -670,16 +665,12 @@
       // Epoch re-processing required?
       // -----------------------------
-      if (_obsPool->refSatChangeRequired()) {
+      if (_obsPool->refSatChangeRequired()) {//SLIP
         epochReProcessing = true;
         _obsPool->deleteLastEpoch();
-        QMapIterator<char, t_pppRefSat*> it(_obsPool->getRefSatMap());
-        while (it.hasNext()) {
-          it.next();
-          _historicalRefSats.append(it.value()->prn());
-        }
+        setHistoricalRefSats();
       }
       else {
         epochReProcessing = false;
-
+        _historicalRefSats.clear();
       }
     } while (epochReProcessing);
@@ -789,21 +780,31 @@
   // reference satellite definition per system
   for (unsigned iSys = 0; iSys < _opt->systems().size(); iSys++) {
-    char system = _opt->systems()[iSys];
+    char sys = _opt->systems()[iSys];
     bool refSatDefined = false;
-    t_pppRefSat* refSat = _obsPool->getRefSatMapElement(system);
+    t_pppRefSat* refSat = _obsPool->getRefSatMapElement(sys);
     for (unsigned ii = 0; ii < obsVector.size(); ii++) {
       t_pppSatObs* satObs = obsVector.at(ii);
-      if (satObs->eleSat() < _opt->_minEle) {continue;}
+      if (satObs->eleSat() < _opt->_minEle) {
+        continue;
+      }
       // reference satellite is unchanged
-      if      (!_obsPool->refSatChangeRequired() &&  refSat->prn() == satObs->prn()) {
+      if      (!_obsPool->refSatChangeRequired(sys) && refSat->prn() == satObs->prn()) {
         refSatDefined = true;
         obsVector[ii]->setAsReference();
+#ifdef BNC_DEBUG_PPP
+        LOG << "=> unchanged refsatprn: "  <<  satObs->prn().toString() << endl;
+#endif
       }
       // reference satellite has changed
-      else if ( _obsPool->refSatChangeRequired() && refSat->prn() != satObs->prn() && !_historicalRefSats.contains(satObs->prn())) {
-        if (satObs->prn().system() == system) {
+      else if ( _obsPool->refSatChangeRequired(sys) &&
+                refSat->prn() != satObs->prn() &&
+                !_historicalRefSats[sys].contains(satObs->prn())) {
+        if (satObs->prn().system() == sys) {
           refSatDefined = true;
           obsVector[ii]->setAsReference();
           refSat->setPrn(satObs->prn());
+#ifdef BNC_DEBUG_PPP
+          LOG << "=> set refsatprn: "  <<  satObs->prn().toString() << endl;
+#endif
         }
       }
@@ -822,7 +823,11 @@
           continue;
         }
-        if (satObs->prn().system() == system) {
+        if (satObs->prn().system() == sys &&
+            !_historicalRefSats[sys].contains(satObs->prn())) {
           obsVector[ii]->setAsReference();
           refSat->setPrn(satObs->prn());
+#ifdef BNC_DEBUG_PPP
+          LOG << " => set refsatprn: "  <<  satObs->prn().toString() << endl;
+#endif
           if (OPT->_pseudoObsIono) {
             refSat->setStecValue(satObs->getIonoCodeDelay(t_frequency::G1));
@@ -833,7 +838,52 @@
       }
     }
-  }
-  _obsPool->setRefSatChangeRequired(false);
-
+    if (!refSatDefined) {
+      refSat->setPrn(t_prn());
+    }
+    _obsPool->setRefSatChangeRequired(sys, false);
+  }
+}
+
+//
+//////////////////////////////////////////////////////////////////////////////
+t_irc t_pppClient::handleRefSatellites(std::vector<t_pppSatObs*>& obsVector) {
+
+  // set refSats in current epoch
+  // ============================
+  setRefSatellites(obsVector); // current epoch
+  LOG.setf(ios::fixed);
+  QMapIterator<char, t_pppRefSat*> it(_obsPool->getRefSatMap());
+  while (it.hasNext()) {
+    it.next();
+    char  sys = it.key();
+    t_prn prn = it.value()->prn();
+    if (prn.number() == 0) {
+      return failure;
+    }
+    QString str;
+    if (prn == _obsPool->getRefSatMapElementLastEpoch(sys) ||
+               _obsPool->getRefSatMapElementLastEpoch(sys) == t_prn() )  {
+      _obsPool->setRefSatChanged(sys, false);
+      str = " SET   ";
+    }
+    else {
+      _obsPool->setRefSatChanged(sys, true);
+      str = " RESET ";
+    }
+    LOG << string(_epoTimeRover) <<  str.toStdString() << " REFSAT  " << sys << ": " << prn.toString() << endl;
+  }
+  return success;
+}
+
+void t_pppClient::setHistoricalRefSats() {
+  QMapIterator<char, t_pppRefSat*> it(_obsPool->getRefSatMap());
+  while (it.hasNext()) {
+    it.next();
+    t_prn prn = it.value()->prn();
+    char sys = prn.system();
+    if (_obsPool->refSatChangeRequired(sys)){
+      _historicalRefSats[sys].append(prn);
+    }
+  }
 }
 
