NsgfV00Filter.java

  1. /* Copyright 2022-2025 Luc Maisonobe
  2.  * Licensed to CS GROUP (CS) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * CS licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *   http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.orekit.files.sp3;

  18. import java.io.IOException;
  19. import java.util.function.Function;
  20. import java.util.regex.Matcher;
  21. import java.util.regex.Pattern;

  22. import org.orekit.data.DataFilter;
  23. import org.orekit.data.DataSource;
  24. import org.orekit.data.LineOrientedFilteringReader;

  25. /** Filter for some non-official files from CDDIS.
  26.  * <p>
  27.  * Some files produced by UKRI/NERC/British Geological Survey Space Geodesy Facility (SGF)
  28.  * claim to be SP3c but are really SP3d since they have more than 4 comments lines. This
  29.  * filter can be used to parse them.
  30.  * </p>
  31.  * @see <a href="https://forum.orekit.org/t/solved-sp3-precise-orbit-file-is-not-compliant-with-sp3-format-c-extra-comment-line">SP3
  32.  * precise orbit file is not compliant with SP3 format c (extra comment line)</a>
  33.  * @since 12.1
  34.  */
  35. public class NsgfV00Filter implements DataFilter {

  36.     /** Default regular expression for NSGF V00 files. */
  37.     public static final String DEFAULT_V00_PATTERN = ".*nsgf\\.orb\\.[^.]+\\.v00\\.sp3$";

  38.     /** Pattern matching file names to which filtering should be applied. */
  39.     private final Pattern pattern;

  40.     /** Renaming function. */
  41.     private final Function<String, String> renaming;

  42.     /** Simple constructor.
  43.      * @param nameRegexp regular expression matching file names to which filtering should be applied
  44.      * @param renaming function to apply for renaming files (and avoid the filter to be applied in infinite recursion)
  45.      */
  46.     public NsgfV00Filter(final String nameRegexp, final Function<String, String> renaming) {
  47.         this.pattern  = Pattern.compile(nameRegexp);
  48.         this.renaming = renaming;
  49.     }

  50.     /** Simple constructor.
  51.      * <p>
  52.      * This uses {@link #DEFAULT_V00_PATTERN} as the regular expression matching files
  53.      * that must be filtered, and replaces "v00" by "v70" to generate the filtered name.
  54.      * </p>
  55.      */
  56.     public NsgfV00Filter() {
  57.         this(DEFAULT_V00_PATTERN, s -> s.replace("v00", "v70"));
  58.     }

  59.     /** {@inheritDoc} */
  60.     @Override
  61.     public DataSource filter(final DataSource original) throws IOException {
  62.         final Matcher matcher = pattern.matcher(original.getName());
  63.         if (matcher.matches()) {
  64.             // this is a v00 file from NSGF
  65.             // we need to parse it as an SP3d file even if it claims being an SP3c file
  66.             final String oName = original.getName();
  67.             final String fName = renaming.apply(oName);
  68.             return new DataSource(fName,
  69.                                   () -> new LineOrientedFilteringReader(oName, original.getOpener().openReaderOnce()) {

  70.                                       /** {@inheritDoc} */
  71.                                       @Override
  72.                                       protected CharSequence filterLine(final int lineNumber, final String originalLine) {
  73.                                           if (lineNumber == 1 && originalLine.startsWith("#c")) {
  74.                                               // the 'c' format marker appears in the first header line
  75.                                               // we replace it by a 'd' format marker
  76.                                               return "#d" + originalLine.substring(2);
  77.                                           } else {
  78.                                               // don't filter any other lines
  79.                                               return originalLine;
  80.                                           }
  81.                                       }

  82.                                   });
  83.         } else {
  84.             // this is a regular file, no need to filter it
  85.             return original;
  86.         }
  87.     }

  88. }