Index: trunk/BNC/src/PPP/pppClient.cpp
===================================================================
--- trunk/BNC/src/PPP/pppClient.cpp	(revision 8912)
+++ trunk/BNC/src/PPP/pppClient.cpp	(revision 8956)
@@ -160,4 +160,5 @@
   // -------
   epoTime.reset();
+  clearObs();
 
   // Create vector of valid observations
@@ -174,48 +175,4 @@
       }
     }
-  }
-
-  // (re)set reference satellites per system if required
-  // ---------------------------------------------------
-  if (_opt->_refSatRequired) {
-    // reference satellite definition per system
-    for (unsigned iSys = 0; iSys < _opt->systems().size(); iSys++) {
-      char system = _opt->systems()[iSys];
-      bool refSatDefined = false;
-      t_pppRefSat* refSat = _obsPool->getRefSatMapElement(system);
-      for (unsigned ii = 0; ii < obsVector.size(); ii++) {
-        const t_pppSatObs* satObs = obsVector.at(ii);
-        // reference satellite is unchanged
-        if      (!_obsPool->refSatChangeRequired() && refSat->prn() == satObs->prn()) {
-          refSatDefined = true;
-          obsVector[ii]->setAsReference();
-          refSat->setStatus(t_pppRefSat::unchanged);
-        }
-        // reference satellite has changed
-        else if ( _obsPool->refSatChangeRequired() && refSat->prn() != satObs->prn()) {
-          if (satObs->prn().system() == system) {
-            refSatDefined = true;
-            obsVector[ii]->setAsReference();
-            refSat->setStatus(t_pppRefSat::changed);
-            refSat->setPrn(satObs->prn());
-          }
-        }
-        if (refSatDefined) {
-          break;
-        }
-      }
-      // reference satellite has to be initialized
-      if (!refSatDefined) {
-        for (unsigned ii = 0; ii < obsVector.size(); ii++) {
-          const t_pppSatObs* satObs = obsVector.at(ii);
-          if (satObs->prn().system() == system) {
-            obsVector[ii]->setAsReference();
-            refSat->setStatus(t_pppRefSat::initialized);
-            refSat->setPrn(satObs->prn());
-          }
-        }
-      }
-    }
-    _obsPool->setRefSatChangeRequired(false);
   }
 
@@ -497,9 +454,4 @@
         satObs->eleSat() >= _opt->_minEle &&
         modelSetup == success) {
-      if (satObs->isReference() && _opt->_pseudoObsIono) {
-        char system = satObs->prn().system();
-        t_pppRefSat* refSat = _obsPool->getRefSatMapElement(system);
-        refSat->setStecValue(satObs->getIonoCodeDelay(t_frequency::G1));
-      }
       ++it;
     }
@@ -517,4 +469,5 @@
 void t_pppClient::processEpoch(const vector<t_satObs*>& satObs, t_output* output) {
 
+  _historicalRefSats.clear();
   try {
     initOutput(output);
@@ -524,4 +477,9 @@
     do {
       num++;
+      if (num == 1) {
+        LOG << "\nPPP of Epoch ";
+        if (!_epoTimeRover.undef()) LOG << string(_epoTimeRover);
+        LOG << "\n---------------------------------------------------------------\n";
+      }
 
       // Prepare Observations of the Rover
@@ -530,8 +488,4 @@
         return finish(failure);
       }
-
-      LOG << "\nPPP of Epoch ";
-      if (!_epoTimeRover.undef()) LOG << string(_epoTimeRover);
-      LOG << "\n---------------------------------------------------------------\n";
 
       for (int iter = 1; iter <= 2; iter++) {
@@ -548,13 +502,6 @@
       _offGG = cmpOffGG(_obsRover);
 
-      // Prepare Pseudo Observations of the Rover
-      // ----------------------------------------
-      _pseudoObsIono = preparePseudoObs(_obsRover);
-      if (int(_obsRover.size()) < _opt->_minObs) {
-        LOG << "t_pppClient::processEpoch not enough observations" << endl;
-        return finish(failure);
-      }
-
       if (_opt->_refSatRequired) {
+        setRefSatellites(_obsRover);
         LOG.setf(ios::fixed);
         QMapIterator<char, t_pppRefSat*> it(_obsPool->getRefSatMap());
@@ -563,12 +510,29 @@
           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
+      // ----------------------------------------
+      _pseudoObsIono = preparePseudoObs(_obsRover);
+
+      if (int(_obsRover.size()) < _opt->_minObs) {
+        LOG << "t_pppClient::processEpoch not enough observations" << endl;
+        return finish(failure);
+      }
+
 
       // Store last epoch of data
       // ------------------------
-      //_obsRover.resize(2);
       _obsPool->putEpoch(_epoTimeRover, _obsRover, _pseudoObsIono);
+      _obsPool->setHistoricalRefSatList(_historicalRefSats);
 
       // Process Epoch in Filter
@@ -582,14 +546,15 @@
       if (_obsPool->refSatChangeRequired()) {
         epochReProcessing = true;
+        _obsPool->deleteLastEpoch();
+        QMapIterator<char, t_pppRefSat*> it(_obsPool->getRefSatMap());
+        while (it.hasNext()) {
+          it.next();
+          _historicalRefSats.append(it.value()->prn());
+        }
       }
       else {
         epochReProcessing = false;
-      }
-      // Datum transformation required?
-      // ------------------------------
-      if (num > 1 && epochReProcessing) {
-        _filter->datumTransformation();
-      }
-
+
+      }
     } while (epochReProcessing);
   }
@@ -694,4 +659,59 @@
 //
 //////////////////////////////////////////////////////////////////////////////
+void t_pppClient::setRefSatellites(std::vector<t_pppSatObs*>& obsVector) {
+
+  // reference satellite definition per system
+  for (unsigned iSys = 0; iSys < _opt->systems().size(); iSys++) {
+    char system = _opt->systems()[iSys];
+    bool refSatDefined = false;
+    t_pppRefSat* refSat = _obsPool->getRefSatMapElement(system);
+    for (unsigned ii = 0; ii < obsVector.size(); ii++) {
+      t_pppSatObs* satObs = obsVector.at(ii);
+      if (satObs->eleSat() < _opt->_minEle) {continue;}
+      // reference satellite is unchanged
+      if      (!_obsPool->refSatChangeRequired() &&  refSat->prn() == satObs->prn()) {
+        refSatDefined = true;
+        obsVector[ii]->setAsReference();
+      }
+      // reference satellite has changed
+      else if ( _obsPool->refSatChangeRequired() && refSat->prn() != satObs->prn() && !_historicalRefSats.contains(satObs->prn())) {
+        if (satObs->prn().system() == system) {
+          refSatDefined = true;
+          obsVector[ii]->setAsReference();
+          refSat->setPrn(satObs->prn());
+        }
+      }
+      if (refSatDefined) {
+        if (OPT->_pseudoObsIono) {
+          refSat->setStecValue(satObs->getIonoCodeDelay(t_frequency::G1));
+        }
+        break;
+      }
+    }
+    // reference satellite has to be initialized
+    if (!refSatDefined) {
+      for (unsigned ii = 0; ii < obsVector.size(); ii++) {
+        t_pppSatObs* satObs = obsVector.at(ii);
+        if (satObs->eleSat() < _opt->_minEle) {
+          continue;
+        }
+        if (satObs->prn().system() == system) {
+          obsVector[ii]->setAsReference();
+          refSat->setPrn(satObs->prn());
+          if (OPT->_pseudoObsIono) {
+            refSat->setStecValue(satObs->getIonoCodeDelay(t_frequency::G1));
+          }
+          refSatDefined = true;
+          break;
+        }
+      }
+    }
+  }
+  _obsPool->setRefSatChangeRequired(false);
+
+}
+
+//
+//////////////////////////////////////////////////////////////////////////////
 void t_pppClient::reset() {
 
