Index: trunk/BNC/src/rinex/rnxnavfile.cpp
===================================================================
--- trunk/BNC/src/rinex/rnxnavfile.cpp	(revision 11192)
+++ trunk/BNC/src/rinex/rnxnavfile.cpp	(revision 11200)
@@ -52,4 +52,5 @@
 ////////////////////////////////////////////////////////////////////////////
 t_rnxNavFile::t_rnxNavHeader::t_rnxNavHeader() {
+  _mergedFiles = 0;
   _version = 0.0;
   _glonass = false;
@@ -86,4 +87,13 @@
     else if (key == "PGM / RUN BY / DATE") {
       _runByDate.append(value.trimmed());
+    }
+    else if (key == "MERGED FILE") {
+      // number of merged files, set for the written file
+      _mergedFiles = qMax(1, value.toInt());
+    }
+    else if (!key.isEmpty()) {
+      // e.g. LEAP SECONDS, IONOSPHERIC CORR, TIME SYSTEM CORR, ION ALPHA,
+      // ION BETA, DELTA-UTC: A0,A1,T,W, DOI, LICENSE OF USE: kept verbatim
+      _otherRecords.append(line.left(60).leftJustified(60) + key);
     }
   }
@@ -153,4 +163,5 @@
   QString prn;
   bool    ephPending = false; // RINEX 4: "> EPH" line read, data lines follow
+  bool    recordPending = false; // RINEX 4: other record, data lines follow
 
   while (stream->status() == QTextStream::Ok && !stream->atEnd()) {
@@ -177,8 +188,22 @@
       navType = "";
     }
-    // RINEX version 4: data lines of an EPH record follow its "> EPH" line;
-    // anything else (e.g. the remainder of a record that could not be read)
-    // is skipped
+    // RINEX version 4: data lines of an EPH record follow its "> EPH" line,
+    // those of other records (STO, EOP, ION, unknown types) are kept
+    // verbatim up to the next ">" line; anything else is skipped
     else if (version() >= 4.0 && firstStr != ">") {
+      if (recordPending) {
+        t_rnxNavRecord& record = _records.last();
+        if (record.lines.size() == 1) { // first data line: epoch
+          QTextStream in(line.mid(4, 19).toLatin1(), QIODevice::ReadOnly);
+          int    year = 0, month = 0, day = 0, hour = 0, min = 0;
+          double sec = 0.0;
+          in >> year >> month >> day >> hour >> min >> sec;
+          if (year > 0) {
+            record.tt.set(year, month, day, hour, min, sec);
+          }
+        }
+        record.lines << line;
+        continue;
+      }
       if (!ephPending || line.left(1) != prn.left(1)) {
         ephPending = false;
@@ -189,5 +214,6 @@
     }
     else if (version() >= 4.0 && firstStr == ">") {
-      ephPending = false;
+      ephPending    = false;
+      recordPending = false;
       if (hlp.size() < 4) {
         continue;
@@ -197,43 +223,13 @@
       sys = prn[0].toLatin1();
       navType = hlp.at(3);
-      ephPending = (key == "EPH");
-
-      // ALL Non-EPH messages are currently ignored
-      int lines2skip = 0;
-      // STO
-      if (key == "STO") {
-        lines2skip = 2;
-      }
-      // EOP
-      else if (key == "EOP") {
-        lines2skip = 3;
-      }
-      // ION
-      else if (key == "ION") {
-        if (sys == 'R') {
-          lines2skip = 1; //if (navType == "LXOC") {}
-        }
-        else if (sys == 'E') {
-          lines2skip = 2;
-        }
-        else if (sys == 'G' || sys == 'C' ||
-                 sys == 'J' || sys == 'I') {
-          lines2skip = 3;
-          if ((sys == 'I' && navType == "L1NV") || // I: KLOB, NEQN
-              (sys == 'J' && navType == "CNVX")) { // J: WIDE, JAPN
-            QString navSubType = hlp.value(4); // RINEX >= 4.02 only
-            if      (navSubType == "KLOB") {
-              lines2skip += 1;
-            }
-            else if (navSubType == "NEQN") {
-              lines2skip += 4;
-            }
-          }
-        }
-      }
-      if (lines2skip) {
-        for (int ii = 0; ii < lines2skip; ii++) {
-          stream->readLine();
-        }
+      if (key == "EPH") {
+        ephPending = true;
+      }
+      else {
+        t_rnxNavRecord record;
+        record.key = key;
+        record.lines << line;
+        _records.append(record);
+        recordPending = true;
       }
       continue;
@@ -379,5 +375,18 @@
 //
 ////////////////////////////////////////////////////////////////////////////
-void t_rnxNavFile::writeHeader(const QMap<QString, QString>* txtMap, int numMergedFiles, int leapSecs) {
+void t_rnxNavFile::writeHeader(const QMap<QString, QString>* txtMap, int numMergedFiles, int leapSecs,
+                               const QStringList* headerRecords) {
+
+  // LEAP SECONDS of the input header (may hold future leap seconds) preferred
+  // ---------------------------------------------------------------------------
+  bool haveLeapSecs = false;
+  if (headerRecords) {
+    for (int ii = 0; ii < headerRecords->size(); ii++) {
+      if (headerRecords->at(ii).mid(60).trimmed() == "LEAP SECONDS") {
+        haveLeapSecs = true;
+      }
+    }
+  }
+
 
   QString     runBy = BNC_CORE->userName();
@@ -459,9 +468,13 @@
 	       << "PGM / RUN BY / DATE\n";
 	}
-    *_stream << QString("%1").arg(leapSecs, 6, 10, QLatin1Char(' ')).left(60).leftJustified(60)
-        << "LEAP SECONDS\n";
-
-    *_stream << QString("%1").arg(numMergedFiles, 19, 10, QLatin1Char(' ')).leftJustified(60)
-        << "MERGED FILE\n";
+    if (!haveLeapSecs) {
+      *_stream << QString("%1").arg(leapSecs, 6, 10, QLatin1Char(' ')).left(60).leftJustified(60)
+          << "LEAP SECONDS\n";
+    }
+
+    if (numMergedFiles > 1) {
+      *_stream << QString("%1").arg(numMergedFiles, 9).leftJustified(60)
+          << "MERGED FILE\n";
+    }
   }
 
@@ -471,4 +484,10 @@
   }
 
+  if (headerRecords) {
+    for (int ii = 0; ii < headerRecords->size(); ii++) {
+      *_stream << headerRecords->at(ii) << "\n";
+    }
+  }
+
   *_stream << QString()
     .leftJustified(60)
@@ -476,4 +495,21 @@
 }
 
+// Write RINEX 4 STO, EOP or ION record (verbatim)
+////////////////////////////////////////////////////////////////////////////
+void t_rnxNavFile::writeRecord(const t_rnxNavRecord& record) {
+  if (version() < 4.0) {
+    return;
+  }
+  for (int ii = 0; ii < record.lines.size(); ii++) {
+    QString line = record.lines[ii];
+    int len = line.length();
+    while (len > 0 && line[len-1] == ' ') {
+      len--;
+    }
+    line.truncate(len);
+    *_stream << line << "\n";
+  }
+}
+
 //
 ////////////////////////////////////////////////////////////////////////////
