Index: trunk/BNC/src/bncrinex.cpp
===================================================================
--- trunk/BNC/src/bncrinex.cpp	(revision 11196)
+++ trunk/BNC/src/bncrinex.cpp	(revision 11200)
@@ -475,5 +475,6 @@
       return;
     }
-    _header.setDefault(_statID, intHeaderVers);
+    // marker name from the stream name, e.g. WTZR00DEU0 -> WTZR00DEU
+    _header.setDefault(_statID.left(9), intHeaderVers);
   }
 
Index: trunk/BNC/src/rinex/reqcedit.cpp
===================================================================
--- trunk/BNC/src/rinex/reqcedit.cpp	(revision 11196)
+++ trunk/BNC/src/rinex/reqcedit.cpp	(revision 11200)
@@ -327,4 +327,6 @@
   // Loop over all input observation files
   // -------------------------------------
+  bncTime lastWritten;     // epochs of overlapping files are written once
+  int     numDuplicates = 0;
   for (int ii = 0; ii < _rnxObsFiles.size(); ii++) {
     t_rnxObsFile* obsFile = _rnxObsFiles[ii];
@@ -334,4 +336,24 @@
             << obsFile->startTime().timestr(0).c_str() << Qt::endl;
     }
+    // header records changed with respect to the previous file (after the
+    // header editing options): event with header information (flag 4)
+    editRnxObsHeader(*obsFile);
+    QList<t_rnxObsFile::t_rnxEvent> headerChange;
+    if (ii > 0) {
+      QStringList records = headerChangeRecords(_rnxObsFiles[ii-1]->header(),
+                                                obsFile->header(),
+                                                outObsFile.header().version());
+      if (!records.isEmpty()) {
+        t_rnxObsFile::t_rnxEvent event;
+        event.flag    = 4;
+        event.version = outObsFile.header().version();
+        event.records = records;
+        headerChange.append(event);
+        if (_logStream) {
+          *_logStream << "Header changes    : " << records.size() << " records ("
+                      << obsFile->fileName() << ")" << Qt::endl;
+        }
+      }
+    }
     if (ii == 0) {
       outObsFile.setHeader(obsFile->header(), int(_rnxVersion), &useObsTypes,
@@ -365,5 +387,5 @@
       // sampling) are written with the next epoch; events with an epoch
       // outside the time window are skipped
-      QList<t_rnxObsFile::t_rnxEvent> pendingEvents;
+      QList<t_rnxObsFile::t_rnxEvent> pendingEvents = headerChange;
       bool pendingPowerFail = false;
       bool endReached       = false;
@@ -379,4 +401,8 @@
           endReached = true;
           break;
+        }
+        if (lastWritten.valid() && !(epo->tt > lastWritten)) {
+          ++numDuplicates; // already written from the previous file
+          continue;
         }
 
@@ -396,4 +422,5 @@
           }
           outObsFile.writeEpoch(epo);
+          lastWritten = epo->tt;
         }
         else {
@@ -426,4 +453,48 @@
     }
   }
+  if (_logStream && numDuplicates) {
+    *_logStream << "Epochs skipped    : " << numDuplicates
+                << " (not later than the last epoch written, e.g. overlapping files)" << Qt::endl;
+  }
+}
+
+// Station header records of header2 differing from header1 (e.g. new
+// receiver or antenna in the next file), in the output RINEX version
+////////////////////////////////////////////////////////////////////////////
+QStringList t_reqcEdit::headerChangeRecords(const t_rnxObsHeader& header1,
+                                            const t_rnxObsHeader& header2,
+                                            double outVersion) {
+  QMap<QString, QStringList> records1, records2;
+  QStringList labels;
+  for (int iHdr = 0; iHdr < 2; iHdr++) {
+    const t_rnxObsHeader& header = (iHdr == 0) ? header1 : header2;
+    QMap<QString, QStringList>& records = (iHdr == 0) ? records1 : records2;
+    QStringList lines = header.stationRecords(outVersion);
+    QStringList other = header.otherRecords();
+    for (int ii = 0; ii < other.size(); ii++) {
+      QString record = t_rnxObsHeader::convertRecord(other[ii], outVersion,
+                                                     header.otherRecordsVersion());
+      if (!record.isEmpty()) {
+        lines << record;
+      }
+    }
+    if (header.rcvClkOffsAppl() < 0) { // not given: default 0
+      lines << QString("%1").arg(0, 6).leftJustified(60) + "RCV CLOCK OFFS APPL";
+    }
+    for (int ii = 0; ii < lines.size(); ii++) {
+      QString label = lines[ii].mid(60).trimmed();
+      records[label] << lines[ii];
+      if (iHdr == 1 && !labels.contains(label)) {
+        labels << label;
+      }
+    }
+  }
+  QStringList changed;
+  for (int ii = 0; ii < labels.size(); ii++) {
+    if (records1.value(labels[ii]) != records2[labels[ii]]) {
+      changed << records2[labels[ii]];
+    }
+  }
+  return changed;
 }
 
@@ -709,9 +780,14 @@
     comments.append(comment);
   }
-  QStringListIterator it(_navFileNames);
+  // (number of merged files: input files already merged count with their
+  // MERGED FILE value)
+  int mergedNavFiles = 0;
+  QStringList navFiles = expandFileNames(_navFileNames);
+  QStringListIterator it(navFiles);
   while (it.hasNext()) {
     QString fileName = it.next();
 
     t_rnxNavFile rnxNavFile(fileName, t_rnxNavFile::input);
+    mergedNavFiles += qMax(1, rnxNavFile.mergedFiles());
     QStringListIterator itCmnt(rnxNavFile.comments());
     while (itCmnt.hasNext()) {
@@ -813,5 +889,12 @@
   }
 
-  int mergedNavFiles = _navFileNames.size();
+  // Header records and RINEX 4 STO/EOP/ION records of all input files:
+  // header records of the same RINEX version (the first one of a kind),
+  // records without duplicates, in the time window
+  // ---------------------------------------------------------------------
+  QStringList headerRecords;
+  QList<t_rnxNavFile::t_rnxNavRecord> records;
+  collectNavRecords(outNavFile, headerRecords, records);
+
   unsigned year, month, day;
   int gps_utc = 0;
@@ -820,5 +903,11 @@
     gps_utc = gnumleap(year, month, day);
   }
-  outNavFile.writeHeader(&txtMap, mergedNavFiles, gps_utc);
+  outNavFile.writeHeader(&txtMap, mergedNavFiles, gps_utc, &headerRecords);
+
+  // records without epoch first, then in time order with the ephemerides
+  int iRec = 0;
+  while (iRec < records.size() && !records[iRec].tt.valid()) {
+    outNavFile.writeRecord(records[iRec++]);
+  }
 
   // Loop over all ephemerides
@@ -850,5 +939,461 @@
       }
     }
+    while (iRec < records.size() && records[iRec].tt <= eph->TOC()) {
+      outNavFile.writeRecord(records[iRec++]);
+    }
     outNavFile.writeEph(eph);
+  }
+  while (iRec < records.size()) {
+    outNavFile.writeRecord(records[iRec++]);
+  }
+}
+
+// File names with wildcards expanded (as in readEphemerides)
+////////////////////////////////////////////////////////////////////////////
+QStringList t_reqcEdit::expandFileNames(const QStringList& fileNames) {
+  QStringList expanded;
+  QStringListIterator itNames(fileNames);
+  while (itNames.hasNext()) {
+    QString fileName = itNames.next();
+    if (fileName.indexOf('*') != -1 || fileName.indexOf('?') != -1) {
+      QFileInfo fileInfo(fileName);
+      QDir dir = fileInfo.dir();
+      QStringList filters; filters << fileInfo.fileName();
+      QListIterator<QFileInfo> it(dir.entryInfoList(filters));
+      while (it.hasNext()) {
+        expanded << it.next().filePath();
+      }
+    }
+    else {
+      expanded << fileName;
+    }
+  }
+  return expanded;
+}
+
+// Key of a navigation header record: one record per kind is written (the
+// first one), IONOSPHERIC CORR and TIME SYSTEM CORR per correction type
+////////////////////////////////////////////////////////////////////////////
+static QString navHeaderKey(const QString& record) {
+  QString label = record.mid(60).trimmed();
+  if (label == "IONOSPHERIC CORR") {
+    return label + record.left(4) + record.mid(54, 6); // type, time mark, SV
+  }
+  if (label == "TIME SYSTEM CORR") {
+    return label + record.left(4);
+  }
+  static const QStringList single = QStringList()
+    << "LEAP SECONDS" << "ION ALPHA" << "ION BETA" << "DELTA-UTC: A0,A1,T,W"
+    << "CORR TO SYSTEM TIME" << "D-UTC A0,A1,T,W,S,U" << "DOI";
+  if (single.contains(label)) {
+    return label;
+  }
+  return record; // e.g. LICENSE OF USE, STATION INFORMATION: identical ones once
+}
+
+// Conversion of navigation header records between RINEX versions and of
+// RINEX 4 STO/ION records to RINEX 3/2 header records (RINEX 3.05 Table A5,
+// RINEX 2.11, RINEX 4.02 Tables 26-28, A33-A37: same sign conventions)
+////////////////////////////////////////////////////////////////////////////
+namespace {
+
+double navField(const QString& line, int pos, int len = 19) {
+  double value = 0.0;
+  readDbl(line, pos, len, value);
+  return value;
+}
+
+bool navEpoch(const QString& line, bncTime& tt) {
+  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) {
+    return false;
+  }
+  tt.set(year, month, day, hour, min, sec);
+  return true;
+}
+
+QString navRecord(const QString& value, const QString& label) {
+  return value.leftJustified(60) + label;
+}
+
+// RINEX 3 IONOSPHERIC CORR: A4,1X,4D12.4,1X,A1,1X,I2
+QString ionCorrV3(const QString& type, const double par[4], QChar timeMark, int sv) {
+  QString value = type.leftJustified(4) + " ";
+  for (int ii = 0; ii < 4; ii++) {
+    value += QString("%1").arg(par[ii], 12, 'E', 4);
+  }
+  value += QString(" %1 ").arg(timeMark);
+  value += (sv > 0) ? QString("%1").arg(sv, 2) : QString("  ");
+  return navRecord(value, "IONOSPHERIC CORR");
+}
+
+// RINEX 3 TIME SYSTEM CORR: A4,1X,D17.10,D16.9,1X,I6,1X,I4,1X,A5,1X,I2
+QString timeCorrV3(const QString& type, double a0, double a1, int tt, int week,
+                   const QString& svId, int utcId) {
+  QString value = type.leftJustified(4) + " "
+    + QString("%1").arg(a0, 17, 'E', 10) + QString("%1").arg(a1, 16, 'E', 9)
+    + QString(" %1 %2 %3 %4").arg(tt, 6).arg(week, 4).arg(svId, 5).arg(utcId, 2);
+  return navRecord(value, "TIME SYSTEM CORR");
+}
+
+// RINEX 2 ION ALPHA/ION BETA: 2X,4D12.4
+QString ionV2(const QString& label, const double par[4]) {
+  QString value = "  ";
+  for (int ii = 0; ii < 4; ii++) {
+    value += QString("%1").arg(par[ii], 12, 'E', 4);
+  }
+  return navRecord(value, label);
+}
+
+// RINEX 2 DELTA-UTC: A0,A1,T,W: 3X,2D19.12,2I9
+QString deltaUtcV2(double a0, double a1, int tt, int week) {
+  QString value = "   " + QString("%1").arg(a0, 19, 'E', 12) + QString("%1").arg(a1, 19, 'E', 12)
+    + QString("%1%2").arg(tt, 9).arg(week, 9);
+  return navRecord(value, "DELTA-UTC: A0,A1,T,W");
+}
+
+// RINEX 2 CORR TO SYSTEM TIME: 3I6,3X,D19.12 (-TauC)
+QString corrSysTimeV2(int year, int month, int day, double minusTauC) {
+  QString value = QString("%1%2%3   ").arg(year, 6).arg(month, 6).arg(day, 6)
+    + QString("%1").arg(minusTauC, 19, 'E', 12);
+  return navRecord(value, "CORR TO SYSTEM TIME");
+}
+
+// UTC identifier of RINEX 3 TIME SYSTEM CORR from the RINEX 4 UTC ID
+int utcIdV3(const QString& utcId) {
+  if (utcId == "UTC(NIST)")                         return 1;
+  if (utcId == "UTC(USNO)")                         return 2;
+  if (utcId == "UTC(SU)")                           return 3;
+  if (utcId == "UTC(BIPM)")                         return 4;
+  if (utcId == "UTCGAL")                            return 5; // UTC(Europe Lab)
+  if (utcId == "UTC(CRL)" || utcId == "UTC(NICT)")  return 6;
+  if (utcId == "UTC(NTSC)")                         return 7;
+  return 0;
+}
+
+// Priority of BDS satellites for the ionosphere parameters (RINEX 3.05
+// Table A5 Note 2): GEO, IGSO, MEO
+int bdsPriority(int prn) {
+  if (prn <= 5 || (prn >= 59 && prn <= 62)) {
+    return 0;
+  }
+  if ((prn >= 6 && prn <= 10) || prn == 13 || prn == 16 || prn == 31 ||
+      (prn >= 38 && prn <= 40) || prn == 56) {
+    return 1;
+  }
+  return 2;
+}
+
+} // namespace
+
+// RINEX 4 STO/ION records -> RINEX 3 or 2 header records (one per kind:
+// the first record in time, BDS by satellite priority); returns the number
+// of records not convertible
+////////////////////////////////////////////////////////////////////////////
+int t_reqcEdit::navRecordsToHeader(const QList<t_rnxNavFile::t_rnxNavRecord>& records,
+                                   double outVersion, bool outGlonass,
+                                   QStringList& headerRecords, QStringList& notConverted) {
+  QMap<QString, QPair<int, QString> > converted; // key -> (priority, record)
+  int numNotConverted = 0;
+
+  for (int iRec = 0; iRec < records.size(); iRec++) {
+    const t_rnxNavFile::t_rnxNavRecord& rec = records[iRec];
+    QStringList hlp = rec.lines[0].split(QRegularExpression("\\s+"), Qt::SkipEmptyParts);
+    QString prnStr  = hlp.value(2);
+    QString navType = hlp.value(3);
+    QString subType = hlp.value(4);
+    char    sys     = prnStr.isEmpty() ? ' ' : prnStr[0].toLatin1();
+    int     prnNum  = prnStr.mid(1).toInt();
+    QString key;
+    QString record;
+    int     priority = 0;
+
+    // (rec.lines[0] is the "> " line, the data lines follow)
+    if (rec.key == "ION" && rec.lines.size() >= 4 && subType.isEmpty() &&
+        ((sys == 'G' && (navType == "LNAV" || navType == "CNVX")) ||
+         (sys == 'J' &&  navType == "LNAV") ||
+         (sys == 'C' &&  navType == "D1D2") ||
+         (sys == 'I' &&  navType == "LNAV"))) {
+      // Klobuchar
+      double alpha[4], beta[4];
+      alpha[0] = navField(rec.lines[1], 23);
+      alpha[1] = navField(rec.lines[1], 42);
+      alpha[2] = navField(rec.lines[1], 61);
+      alpha[3] = navField(rec.lines[2],  4);
+      beta[0]  = navField(rec.lines[2], 23);
+      beta[1]  = navField(rec.lines[2], 42);
+      beta[2]  = navField(rec.lines[2], 61);
+      beta[3]  = navField(rec.lines[3],  4);
+      priority = (navType == "CNVX") ? 1 : 0; // LNAV preferred
+      if (outVersion >= 3.0) {
+        QString prefix = (sys == 'G') ? "GPS" : (sys == 'J') ? "QZS" : (sys == 'C') ? "BDS" : "IRN";
+        QChar timeMark = ' ';
+        int   sv       = 0;
+        if (sys == 'C') { // mandatory for BDS: hour of the transmission time, SV
+          bncTime tt;
+          if (navEpoch(rec.lines[1], tt)) {
+            unsigned hour, min; double sec;
+            tt.civil_time(hour, min, sec);
+            timeMark = QChar('A' + int(hour));
+          }
+          sv       = prnNum;
+          priority = bdsPriority(prnNum);
+        }
+        key    = prefix;
+        record = ionCorrV3(prefix + "A", alpha, timeMark, sv) + "\n" +
+                 ionCorrV3(prefix + "B", beta,  timeMark, sv);
+      }
+      else if (sys == 'G' && !outGlonass) {
+        key    = "ION";
+        record = ionV2("ION ALPHA", alpha) + "\n" + ionV2("ION BETA", beta);
+      }
+    }
+    else if (rec.key == "ION" && rec.lines.size() >= 2 && sys == 'E' && navType == "IFNV" &&
+             outVersion >= 3.0) {
+      // NeQuick-G
+      double ai[4];
+      ai[0] = navField(rec.lines[1], 23);
+      ai[1] = navField(rec.lines[1], 42);
+      ai[2] = navField(rec.lines[1], 61);
+      ai[3] = 0.0;
+      key    = "GAL";
+      record = ionCorrV3("GAL", ai, ' ', 0);
+      record = record.left(5 + 36) + QString().leftJustified(12) + record.mid(5 + 48); // ai3 blank
+    }
+    else if (rec.key == "STO" && rec.lines.size() >= 3) {
+      QString type  = rec.lines[1].mid(24, 18).trimmed();
+      QString sbas  = rec.lines[1].mid(43, 18).trimmed();
+      QString utcId = rec.lines[1].mid(62, 18).trimmed();
+      double  a0    = navField(rec.lines[2], 23);
+      double  a1    = navField(rec.lines[2], 42);
+      double  a2    = navField(rec.lines[2], 61);
+      bncTime tot;
+      static const QStringList v3Types = QStringList()
+        << "GPUT" << "GLUT" << "GAUT" << "BDUT" << "QZUT" << "IRUT" << "SBUT"
+        << "GLGP" << "GAGP" << "QZGP" << "IRGP";
+      if (a2 == 0.0 && navEpoch(rec.lines[1], tot) && v3Types.contains(type)) {
+        int week = 0, sow = 0;
+        if (!type.startsWith("GL")) {
+          week = int(tot.gpsw());
+          sow  = int(nint(tot.gpssec()));
+          if (type.startsWith("BD")) {
+            week -= 1356; // BDS week
+          }
+        }
+        // one record per offset type (RINEX 3: no UTC ID for UTC(NPLI));
+        // NavIC: UTCIRN preferred to UTC(NPLI), SBAS: per SBAS ID
+        key = type + (type == "SBUT" ? sbas : QString());
+        if (type == "IRUT" && utcId != "UTCIRN") {
+          priority = 1;
+        }
+        if (outVersion >= 3.0) {
+          QString svId = (type == "SBUT" && !sbas.isEmpty()) ? sbas : prnStr;
+          record = timeCorrV3(type, a0, a1, sow, week, svId.length() > 1 ? svId : QString(),
+                              utcIdV3(utcId));
+        }
+        else if (type == "GPUT" && !outGlonass) {
+          key    = "GPUT";
+          record = deltaUtcV2(a0, a1, sow, week);
+        }
+        else if (type == "GLUT" && outGlonass) {
+          unsigned year, month, day;
+          tot.civil_date(year, month, day);
+          key    = "GLUT";
+          record = corrSysTimeV2(int(year), int(month), int(day), a0);
+        }
+      }
+    }
+
+    if (record.isEmpty()) {
+      // not convertible to the output version (e.g. EOP, BDGIM, NavIC
+      // L1NV, STO with A2, STO types not in RINEX 3) or not used in a
+      // RINEX 2 file of another system
+      bool usable = !(outVersion < 3.0 &&
+                      ((outGlonass && sys != 'R') || (!outGlonass && sys != 'G')));
+      if (usable) {
+        ++numNotConverted;
+        QString what = rec.key + " " + QString(sys) + " " + navType
+                       + (subType.isEmpty() ? QString() : " " + subType);
+        if (!notConverted.contains(what)) {
+          notConverted << what;
+        }
+      }
+      continue;
+    }
+    if (!converted.contains(key) || priority < converted[key].first) {
+      converted[key] = QPair<int, QString>(priority, record);
+    }
+  }
+
+  QMapIterator<QString, QPair<int, QString> > it(converted);
+  while (it.hasNext()) {
+    it.next();
+    headerRecords << it.value().second.split("\n");
+  }
+  return numNotConverted;
+}
+
+// Navigation header record converted between RINEX 2 and 3 (GPS ionosphere
+// and UTC parameters, GLONASS time correction); empty list: not convertible
+////////////////////////////////////////////////////////////////////////////
+QStringList t_reqcEdit::convertNavHeaderRecord(const QString& record, double inVersion,
+                                               double outVersion, bool outGlonass) {
+  QStringList result;
+  QString label = record.mid(60).trimmed();
+  if (int(inVersion) == 2 && int(outVersion) == 3) {
+    if (label == "ION ALPHA" || label == "ION BETA") {
+      double par[4];
+      for (int ii = 0; ii < 4; ii++) {
+        par[ii] = navField(record, 2 + 12*ii, 12);
+      }
+      result << ionCorrV3(label == "ION ALPHA" ? "GPSA" : "GPSB", par, ' ', 0);
+    }
+    else if (label == "DELTA-UTC: A0,A1,T,W") {
+      int tt = 0, week = 0;
+      readInt(record, 41, 9, tt);
+      readInt(record, 50, 9, week);
+      result << timeCorrV3("GPUT", navField(record, 3), navField(record, 22),
+                           tt, week, QString(), 2); // GPS: UTC(USNO)
+    }
+    else if (label == "CORR TO SYSTEM TIME") {
+      result << timeCorrV3("GLUT", navField(record, 21), 0.0, 0, 0, QString(), 3); // UTC(SU)
+    }
+  }
+  else if (int(inVersion) == 3 && int(outVersion) == 2 && !outGlonass) {
+    QString type = record.left(4);
+    if (label == "IONOSPHERIC CORR" && (type == "GPSA" || type == "GPSB")) {
+      double par[4];
+      for (int ii = 0; ii < 4; ii++) {
+        par[ii] = navField(record, 5 + 12*ii, 12);
+      }
+      result << ionV2(type == "GPSA" ? "ION ALPHA" : "ION BETA", par);
+    }
+    else if (label == "TIME SYSTEM CORR" && type == "GPUT") {
+      int tt = 0, week = 0;
+      readInt(record, 39, 6, tt);
+      readInt(record, 46, 4, week);
+      result << deltaUtcV2(navField(record, 5, 17), navField(record, 22, 16), tt, week);
+    }
+  }
+  return result;
+}
+
+// Header records and STO/EOP/ION records of the input navigation files
+////////////////////////////////////////////////////////////////////////////
+void t_reqcEdit::collectNavRecords(const t_rnxNavFile& outNavFile,
+                                   QStringList& headerRecords,
+                                   QList<t_rnxNavFile::t_rnxNavRecord>& records) {
+  QSet<QString> headerKeys;
+  QSet<QString> recordKeys;
+  int numNotConverted = 0;
+  int numRecordsLost  = 0;
+  QStringList notConvertedLabels;
+
+  QStringList navFiles = expandFileNames(_navFileNames);
+
+  for (int iFile = 0; iFile < navFiles.size(); iFile++) {
+    t_rnxNavFile navFile(navFiles[iFile], t_rnxNavFile::input);
+
+    // header records: same RINEX version (RINEX 2: same file type)
+    bool sameVersion = (int(navFile.version()) == int(outNavFile.version()));
+    if (sameVersion && outNavFile.version() < 3.0 && navFile.glonass() != outNavFile.glonass()) {
+      sameVersion = false;
+    }
+    QStringList hdr = navFile.headerRecords();
+    for (int ii = 0; ii < hdr.size(); ii++) {
+      QString label  = hdr[ii].mid(60).trimmed();
+      QString record = hdr[ii];
+      if (label == "LEAP SECONDS") { // RINEX 3 and 4: same format, RINEX 2: I6
+        if (outNavFile.version() < 3.0) {
+          record = record.left(6).leftJustified(60) + label;
+        }
+      }
+      else if (!sameVersion) {
+        QStringList conv = convertNavHeaderRecord(record, navFile.version(),
+                                                  outNavFile.version(), outNavFile.glonass());
+        if (conv.isEmpty()) {
+          ++numNotConverted;
+          if (!notConvertedLabels.contains(label)) {
+            notConvertedLabels << label;
+          }
+        }
+        for (int iConv = 0; iConv < conv.size(); iConv++) {
+          QString key = navHeaderKey(conv[iConv]);
+          if (!headerKeys.contains(key)) {
+            headerKeys.insert(key);
+            headerRecords << conv[iConv];
+          }
+        }
+        continue;
+      }
+      QString key = navHeaderKey(record);
+      if (!headerKeys.contains(key)) {
+        headerKeys.insert(key);
+        headerRecords << record;
+      }
+    }
+
+    // STO/EOP/ION records (RINEX 4)
+    const QList<t_rnxNavFile::t_rnxNavRecord>& recs = navFile.records();
+    for (int ii = 0; ii < recs.size(); ii++) {
+      const t_rnxNavFile::t_rnxNavRecord& rec = recs[ii];
+      if (rec.tt.valid() &&
+          ((_begTime.valid() && rec.tt < _begTime) ||
+           (_endTime.valid() && rec.tt > _endTime))) {
+        continue;
+      }
+      QStringList trimmed;
+      for (int iLine = 0; iLine < rec.lines.size(); iLine++) {
+        trimmed << rec.lines[iLine].trimmed();
+      }
+      QString key = trimmed.join("|");
+      if (recordKeys.contains(key)) {
+        continue;
+      }
+      recordKeys.insert(key);
+      records.append(rec);
+    }
+  }
+
+  std::stable_sort(records.begin(), records.end(),
+                   [](const t_rnxNavFile::t_rnxNavRecord& r1,
+                      const t_rnxNavFile::t_rnxNavRecord& r2) {
+                     if (!r1.tt.valid() || !r2.tt.valid()) {
+                       return !r1.tt.valid() && r2.tt.valid();
+                     }
+                     return r1.tt < r2.tt;
+                   });
+
+  // RINEX 3/2 output: STO/ION records as header records (header records of
+  // the input files of the output version have priority)
+  QStringList notConvertedRecords;
+  if (outNavFile.version() < 4.0) {
+    QStringList conv;
+    numRecordsLost = navRecordsToHeader(records, outNavFile.version(), outNavFile.glonass(),
+                                        conv, notConvertedRecords);
+    for (int iConv = 0; iConv < conv.size(); iConv++) {
+      QString key = navHeaderKey(conv[iConv]);
+      if (!headerKeys.contains(key)) {
+        headerKeys.insert(key);
+        headerRecords << conv[iConv];
+      }
+    }
+    records.clear();
+  }
+
+  if (_logStream) {
+    if (numNotConverted) {
+      *_logStream << "Nav header records not converted to RINEX version "
+                  << outNavFile.version() << ": " << numNotConverted
+                  << " (" << notConvertedLabels.join(", ") << ")" << Qt::endl;
+    }
+    if (numRecordsLost) {
+      *_logStream << "STO/EOP/ION records not convertible to RINEX version "
+                  << outNavFile.version() << ": " << numRecordsLost
+                  << " (" << notConvertedRecords.join(", ") << ")" << Qt::endl;
+    }
   }
 }
Index: trunk/BNC/src/rinex/reqcedit.h
===================================================================
--- trunk/BNC/src/rinex/reqcedit.h	(revision 11196)
+++ trunk/BNC/src/rinex/reqcedit.h	(revision 11200)
@@ -28,4 +28,5 @@
 #include <QtCore>
 #include "rnxobsfile.h"
+#include "rnxnavfile.h"
 #include "rnxnavfile.h"
 #include "ephemeris.h"
@@ -66,4 +67,14 @@
   void editEphemerides();
   void editRnxObsHeader(t_rnxObsFile& obsFile);
+  static QStringList expandFileNames(const QStringList& fileNames);
+  static QStringList headerChangeRecords(const t_rnxObsHeader& header1,
+                                         const t_rnxObsHeader& header2, double outVersion);
+  static int navRecordsToHeader(const QList<t_rnxNavFile::t_rnxNavRecord>& records,
+                                double outVersion, bool outGlonass,
+                                QStringList& headerRecords, QStringList& notConverted);
+  static QStringList convertNavHeaderRecord(const QString& record, double inVersion,
+                                            double outVersion, bool outGlonass);
+  void collectNavRecords(const t_rnxNavFile& outNavFile, QStringList& headerRecords,
+                         QList<t_rnxNavFile::t_rnxNavRecord>& records);
   void collectEvents(const QList<t_rnxObsFile::t_rnxEvent>& events,
                      QList<t_rnxObsFile::t_rnxEvent>& pendingEvents) const;
Index: trunk/BNC/src/rinex/rnxnavfile.cpp
===================================================================
--- trunk/BNC/src/rinex/rnxnavfile.cpp	(revision 11196)
+++ 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";
+  }
+}
+
 //
 ////////////////////////////////////////////////////////////////////////////
Index: trunk/BNC/src/rinex/rnxnavfile.h
===================================================================
--- trunk/BNC/src/rinex/rnxnavfile.h	(revision 11196)
+++ trunk/BNC/src/rinex/rnxnavfile.h	(revision 11200)
@@ -55,7 +55,17 @@
     QStringList     _comments;
     QStringList     _runByDate;
+    QStringList     _otherRecords; // other header records, verbatim
+    int             _mergedFiles;  // MERGED FILE (0: not given)
   };
 
  public:
+  // RINEX 4 STO, EOP and ION records (and records of unknown type), verbatim
+  class t_rnxNavRecord {
+   public:
+    QString     key;    // STO, EOP, ION, ...
+    bncTime     tt;     // epoch of the first data line (for sorting)
+    QStringList lines;  // incl. the "> " line
+  };
+
   t_rnxNavFile(const QString& fileName, e_inpOut inpOut);
   ~t_rnxNavFile();
@@ -68,9 +78,14 @@
   QStringList comments() const {return _header._comments;}
   QStringList runByDate() const {return _header._runByDate;}
+  QStringList headerRecords() const {return _header._otherRecords;}
+  int         mergedFiles() const {return _header._mergedFiles;}
+  const QList<t_rnxNavRecord>& records() const {return _records;}
   t_eph::e_system satSystem() const {return _header._satSys;}
   void   setGlonass(bool glo) {_header._glonass = glo;}
   void   setGnssTypeV3(t_eph::e_system system) {_header._satSys = system;}
-  void   writeHeader(const QMap<QString, QString>* txtMap = 0, int numMergedFiles = 0, int leapSecs = 0);
+  void   writeHeader(const QMap<QString, QString>* txtMap = 0, int numMergedFiles = 0, int leapSecs = 0,
+                     const QStringList* headerRecords = 0);
   void   writeEph(const t_eph* eph);
+  void   writeRecord(const t_rnxNavRecord& record);
 
  protected:
@@ -88,4 +103,5 @@
   QTextStream*        _stream;
   std::vector<t_eph*> _ephs;
+  QList<t_rnxNavRecord> _records;
   t_rnxNavHeader      _header;
 };
Index: trunk/BNC/src/rinex/rnxobsfile.cpp
===================================================================
--- trunk/BNC/src/rinex/rnxobsfile.cpp	(revision 11196)
+++ trunk/BNC/src/rinex/rnxobsfile.cpp	(revision 11200)
@@ -701,4 +701,79 @@
 }
 
+// Station records of the header (marker, observer, receiver, antenna,
+// position), also used for the header changes between concatenated files
+////////////////////////////////////////////////////////////////////////////
+QStringList t_rnxObsHeader::stationRecords(double version) const {
+  if (version == 0.0) {
+    version = _version;
+  }
+  QStringList records;
+  // RINEX 3/4: MARKER NAME A60, RINEX 2: 4-character station ID
+  QString markerName = _markerName;
+  if (version < 3.0) {
+    markerName = _markerName.left(4);
+  }
+  records << QString("%1")
+    .arg(markerName, -60)
+    .leftJustified(60)
+           + "MARKER NAME";
+
+
+  if (!_markerNumber.isEmpty()) {
+    records << QString("%1")
+      .arg(_markerNumber, -20)
+      .leftJustified(60)
+             + "MARKER NUMBER";
+  }
+
+  if (version >= 3.0) {
+    records << QString("%1")
+      .arg(_markerType, -60)
+      .leftJustified(60)
+             + "MARKER TYPE";
+  }
+
+  records << QString("%1%2")
+    .arg(_observer, -20)
+    .arg(_agency,   -40)
+    .leftJustified(60)
+           + "OBSERVER / AGENCY";
+
+  records << QString("%1%2%3")
+    .arg(_receiverNumber,  -20)
+    .arg(_receiverType,    -20)
+    .arg(_receiverVersion, -20)
+    .leftJustified(60)
+           + "REC # / TYPE / VERS";
+
+  records << QString("%1%2")
+    .arg(_antennaNumber, -20)
+    .arg(_antennaName,   -20)
+    .leftJustified(60)
+           + "ANT # / TYPE";
+
+  records << QString("%1%2%3")
+    .arg(_xyz(1), 14, 'f', 4)
+    .arg(_xyz(2), 14, 'f', 4)
+    .arg(_xyz(3), 14, 'f', 4)
+    .leftJustified(60)
+           + "APPROX POSITION XYZ";
+
+  records << QString("%1%2%3")
+    .arg(_antNEU(3), 14, 'f', 4)
+    .arg(_antNEU(2), 14, 'f', 4)
+    .arg(_antNEU(1), 14, 'f', 4)
+    .leftJustified(60)
+           + "ANTENNA: DELTA H/E/N";
+
+  if (_rcvClkOffsAppl >= 0) {
+    records << QString("%1")
+      .arg(_rcvClkOffsAppl, 6)
+      .leftJustified(60)
+             + "RCV CLOCK OFFS APPL";
+  }
+  return records;
+}
+
 // Write Header
 ////////////////////////////////////////////////////////////////////////////
@@ -764,67 +839,5 @@
     *stream << itCmnt.next().trimmed().left(60).leftJustified(60) << "COMMENT\n";
   }
-  QString markerName = _markerName.left(9);
-  if (_version < 3.0) {
-    markerName = _markerName.left(4);
-  }
-  *stream << QString("%1")
-    .arg(markerName, -60)
-    .leftJustified(60)
-           << "MARKER NAME\n";
-
-
-  if (!_markerNumber.isEmpty()) {
-    *stream << QString("%1")
-      .arg(_markerNumber, -20)
-      .leftJustified(60)
-             << "MARKER NUMBER\n";
-  }
-
-  if (_version >= 3.0) {
-    *stream << QString("%1")
-      .arg(_markerType, -60)
-      .leftJustified(60)
-             << "MARKER TYPE\n";
-  }
-
-  *stream << QString("%1%2")
-    .arg(_observer, -20)
-    .arg(_agency,   -40)
-    .leftJustified(60)
-           << "OBSERVER / AGENCY\n";
-
-  *stream << QString("%1%2%3")
-    .arg(_receiverNumber,  -20)
-    .arg(_receiverType,    -20)
-    .arg(_receiverVersion, -20)
-    .leftJustified(60)
-           << "REC # / TYPE / VERS\n";
-
-  *stream << QString("%1%2")
-    .arg(_antennaNumber, -20)
-    .arg(_antennaName,   -20)
-    .leftJustified(60)
-           << "ANT # / TYPE\n";
-
-  *stream << QString("%1%2%3")
-    .arg(_xyz(1), 14, 'f', 4)
-    .arg(_xyz(2), 14, 'f', 4)
-    .arg(_xyz(3), 14, 'f', 4)
-    .leftJustified(60)
-           << "APPROX POSITION XYZ\n";
-
-  *stream << QString("%1%2%3")
-    .arg(_antNEU(3), 14, 'f', 4)
-    .arg(_antNEU(2), 14, 'f', 4)
-    .arg(_antNEU(1), 14, 'f', 4)
-    .leftJustified(60)
-           << "ANTENNA: DELTA H/E/N\n";
-
-  if (_rcvClkOffsAppl >= 0) {
-    *stream << QString("%1")
-      .arg(_rcvClkOffsAppl, 6)
-      .leftJustified(60)
-             << "RCV CLOCK OFFS APPL\n";
-  }
+  *stream << stationRecords().join("\n") << "\n";
 
   if (_version < 3.0) {
Index: trunk/BNC/src/rinex/rnxobsfile.h
===================================================================
--- trunk/BNC/src/rinex/rnxobsfile.h	(revision 11196)
+++ trunk/BNC/src/rinex/rnxobsfile.h	(revision 11200)
@@ -76,4 +76,7 @@
   int         scaleFactor(char sys, const QString& type) const;
   static QString convertRecord(const QString& line, double outVersion, double inVersion);
+  QStringList stationRecords(double version = 0.0) const;
+  QStringList otherRecords() const {return _otherRecords;}
+  double      otherRecordsVersion() const {return _otherRecordsVersion;}
   void        setStartTime(const bncTime& startTime) {_startTime = startTime;}
 
