Index: trunk/BNC/src/bncsp3.cpp
===================================================================
--- trunk/BNC/src/bncsp3.cpp	(revision 10613)
+++ trunk/BNC/src/bncsp3.cpp	(revision 11179)
@@ -58,4 +58,8 @@
 ////////////////////////////////////////////////////////////////////////////
 bncSP3::~bncSP3() {
+  if (_inpOut == output && _lastEpoTime.valid()) {
+    writeEpoch(_lastEpoTime, _oStr.str()); // last epoch, otherwise printed with the next one
+    closeFile();
+  }
   delete _currEpoch;
   delete _prevEpoch;
@@ -70,50 +74,46 @@
   _oStr.setf(ios::fixed);
 
-  if (reopen(GPSweek, GPSweeks) == success) {
-
-    bncTime epoTime(GPSweek, GPSweeks);
-
-    if (epoTime != _lastEpoTime) {
-
-      // print out epoch before
-      if (_lastEpoTime.valid()) {
-        _out << _oStr.str();
-        if (_lastEpoTime.daysec() != 0.0) {
-          _oStr.str(std::string());
-        }
-        // Check the sampling interval (print empty epochs)
-        // ------------------------------------------------
-        if (_sampl > 0) {
-          for (bncTime ep = _lastEpoTime +_sampl; ep < epoTime; ep = ep +_sampl) {
-            _oStr << "\n*  " << ep.datestr(' ') << ' ' << ep.timestr(8, ' ');
-          }
+  if (_sampl != 0 && fmod(GPSweeks, _sampl) != 0.0) {
+    return failure;
+  }
+
+  bncTime epoTime(GPSweek, GPSweeks);
+
+  if (epoTime != _lastEpoTime) {
+
+    // print out epoch before (complete now)
+    if (_lastEpoTime.valid()) {
+      writeEpoch(_lastEpoTime, _oStr.str());
+      _oStr.str(std::string());
+      // Check the sampling interval (print empty epochs)
+      // ------------------------------------------------
+      if (_sampl > 0) {
+        for (bncTime ep = _lastEpoTime +_sampl; ep < epoTime; ep = ep +_sampl) {
+          _oStr << "\n*  " << ep.datestr(' ') << ' ' << ep.timestr(8, ' ');
         }
       }
-
-      // Print the new epoch
-      // -------------------
-      _oStr << "\n*  " << epoTime.datestr(' ') << ' ' << epoTime.timestr(8, ' ');
-
-      _lastEpoTime = epoTime;
-    }
-
-    _oStr << "\nP" << prn.toLatin1().data()
-         << setw(14) << setprecision(6) << xCoM(1) / 1000.0 // [km]
-         << setw(14) << setprecision(6) << xCoM(2) / 1000.0 // [km]
-         << setw(14) << setprecision(6) << xCoM(3) / 1000.0 // [km]
-         << setw(14) << setprecision(6) << sp3Clk * 1e6;     // microseconds
-
-    if (sp3ClkRate) {
-      _oStr << "\nV" << prn.toLatin1().data()
-           << setw(14) << setprecision(6) << v(1) * 10.0      // [dm/s]
-           << setw(14) << setprecision(6) << v(2) * 10.0      // [dm/s]
-           << setw(14) << setprecision(6) << v(3) * 10.0      // [dm/s]
-           << setw(14) << setprecision(6) << sp3ClkRate * 1e2; // 10^⁻4 microseconds/sec
-    }
-    return success;
-  }
-  else {
-    return failure;
-  }
+    }
+
+    // Print the new epoch
+    // -------------------
+    _oStr << "\n*  " << epoTime.datestr(' ') << ' ' << epoTime.timestr(8, ' ');
+
+    _lastEpoTime = epoTime;
+  }
+
+  _oStr << "\nP" << prn.toLatin1().data()
+       << setw(14) << setprecision(6) << xCoM(1) / 1000.0 // [km]
+       << setw(14) << setprecision(6) << xCoM(2) / 1000.0 // [km]
+       << setw(14) << setprecision(6) << xCoM(3) / 1000.0 // [km]
+       << setw(14) << setprecision(6) << sp3Clk * 1e6;     // microseconds
+
+  if (sp3ClkRate) {
+    _oStr << "\nV" << prn.toLatin1().data()
+         << setw(14) << setprecision(6) << v(1) * 10.0      // [dm/s]
+         << setw(14) << setprecision(6) << v(2) * 10.0      // [dm/s]
+         << setw(14) << setprecision(6) << v(3) * 10.0      // [dm/s]
+         << setw(14) << setprecision(6) << sp3ClkRate * 1e2; // 10^-4 microseconds/sec
+  }
+  return success;
 }
 
@@ -121,5 +121,7 @@
 ////////////////////////////////////////////////////////////////////////////
 void bncSP3::closeFile() {
-  _out << "\nEOF" << endl;
+  if (_out.is_open()) {
+    _out << "\nEOF" << endl;
+  }
   bncoutf::closeFile();
 }
