SEMParser.java

  1. /* Copyright 2002-2025 CS GROUP
  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.gnss;

  18. import java.io.BufferedReader;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.io.InputStreamReader;
  22. import java.nio.charset.StandardCharsets;
  23. import java.text.ParseException;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. import java.util.regex.Pattern;

  27. import org.orekit.annotation.DefaultDataContext;
  28. import org.orekit.data.AbstractSelfFeedingLoader;
  29. import org.orekit.data.DataContext;
  30. import org.orekit.data.DataLoader;
  31. import org.orekit.data.DataProvidersManager;
  32. import org.orekit.errors.OrekitException;
  33. import org.orekit.errors.OrekitMessages;
  34. import org.orekit.propagation.analytical.gnss.data.GNSSConstants;
  35. import org.orekit.propagation.analytical.gnss.data.GPSAlmanac;
  36. import org.orekit.time.TimeScales;


  37. /**
  38.  * This class reads SEM almanac files and provides {@link GPSAlmanac GPS almanacs}.
  39.  *
  40.  * <p>The definition of a SEM almanac comes from the
  41.  * <a href="http://www.navcen.uscg.gov/?pageName=gpsSem">U.S. COAST GUARD NAVIGATION CENTER</a>.</p>
  42.  *
  43.  * <p>The format of the files holding SEM almanacs is not precisely specified,
  44.  * so the parsing rules have been deduced from the downloadable files at
  45.  * <a href="http://www.navcen.uscg.gov/?pageName=gpsAlmanacs">NAVCEN</a>
  46.  * and at <a href="https://celestrak.com/GPS/almanac/SEM/">CelesTrak</a>.</p>
  47.  *
  48.  * @author Pascal Parraud
  49.  * @since 8.0
  50.  *
  51.  */
  52. public class SEMParser extends AbstractSelfFeedingLoader implements DataLoader {

  53.     // Constants
  54.     /** The source of the almanacs. */
  55.     private static final String SOURCE = "SEM";

  56.     /** the reference value for the inclination of GPS orbit: 0.30 semicircles. */
  57.     private static final double INC_REF = 0.30;

  58.     /** Default supported files name pattern. */
  59.     private static final String DEFAULT_SUPPORTED_NAMES = ".*\\.al3$";

  60.     /** Separator for parsing. */
  61.     private static final Pattern SEPARATOR = Pattern.compile("\\s+");

  62.     // Fields
  63.     /** the list of all the almanacs read from the file. */
  64.     private final List<GPSAlmanac> almanacs;

  65.     /** the list of all the PRN numbers of all the almanacs read from the file. */
  66.     private final List<Integer> prnList;

  67.     /** Set of time scales to use. */
  68.     private final TimeScales timeScales;

  69.     /** Simple constructor.
  70.      *
  71.      * <p>This constructor does not load any data by itself. Data must be loaded
  72.      * later on by calling one of the {@link #loadData() loadData()} method or
  73.      * the {@link #loadData(InputStream, String) loadData(inputStream, fileName)}
  74.      * method.</p>
  75.      *
  76.      * <p>The supported files names are used when getting data from the
  77.      * {@link #loadData() loadData()} method that relies on the
  78.      * {@link DataContext#getDefault() default data context}. They are useless when
  79.      * getting data from the {@link #loadData(InputStream, String) loadData(input, name)}
  80.      * method.</p>
  81.      *
  82.      * @param supportedNames regular expression for supported files names
  83.      * (if null, a default pattern matching files with a ".al3" extension will be used)
  84.      * @see #loadData()
  85.      * @see #SEMParser(String, DataProvidersManager, TimeScales)
  86.      */
  87.     @DefaultDataContext
  88.     public SEMParser(final String supportedNames) {
  89.         this(supportedNames,
  90.                 DataContext.getDefault().getDataProvidersManager(),
  91.                 DataContext.getDefault().getTimeScales());
  92.     }

  93.     /**
  94.      * Create a SEM loader/parser with the given source of SEM auxiliary data files.
  95.      *
  96.      * <p>This constructor does not load any data by itself. Data must be loaded
  97.      * later on by calling one of the {@link #loadData() loadData()} method or
  98.      * the {@link #loadData(InputStream, String) loadData(inputStream, fileName)}
  99.      * method.</p>
  100.      *
  101.      * <p>The supported files names are used when getting data from the
  102.      * {@link #loadData() loadData()} method that relies on the
  103.      * {@code dataProvidersManager}. They are useless when
  104.      * getting data from the {@link #loadData(InputStream, String) loadData(input, name)}
  105.      * method.</p>
  106.      *
  107.      * @param supportedNames regular expression for supported files names
  108.      * (if null, a default pattern matching files with a ".al3" extension will be used)
  109.      * @param dataProvidersManager provides access to auxiliary data.
  110.      * @param timeScales to use when parsing the GPS dates.
  111.      * @see #loadData()
  112.      * @since 10.1
  113.      */
  114.     public SEMParser(final String supportedNames,
  115.                      final DataProvidersManager dataProvidersManager,
  116.                      final TimeScales timeScales) {
  117.         super((supportedNames == null) ? DEFAULT_SUPPORTED_NAMES : supportedNames,
  118.                 dataProvidersManager);
  119.         this.almanacs = new ArrayList<>();
  120.         this.prnList = new ArrayList<>();
  121.         this.timeScales = timeScales;
  122.     }

  123.     /**
  124.      * Loads almanacs.
  125.      *
  126.      * <p>The almanacs already loaded in the instance will be discarded
  127.      * and replaced by the newly loaded data.</p>
  128.      * <p>This feature is useful when the file selection is already set up by
  129.      * the {@link DataProvidersManager data providers manager} configuration.</p>
  130.      *
  131.      */
  132.     public void loadData() {
  133.         // load the data from the configured data providers
  134.         feed(this);
  135.         if (almanacs.isEmpty()) {
  136.             throw new OrekitException(OrekitMessages.NO_SEM_ALMANAC_AVAILABLE);
  137.         }
  138.     }

  139.     @Override
  140.     public void loadData(final InputStream input, final String name)
  141.         throws IOException, ParseException, OrekitException {

  142.         // Clears the lists
  143.         almanacs.clear();
  144.         prnList.clear();

  145.         // Creates the reader
  146.         try (BufferedReader reader = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8))) {
  147.             // Reads the number of almanacs in the file from the first line
  148.             String[] token = getTokens(reader);
  149.             final int almanacNb = Integer.parseInt(token[0].trim());

  150.             // Reads the week number and the time of applicability from the second line
  151.             token = getTokens(reader);
  152.             final int week = Integer.parseInt(token[0].trim());
  153.             final double toa = Double.parseDouble(token[1].trim());

  154.             // Loop over data blocks
  155.             for (int i = 0; i < almanacNb; i++) {
  156.                 // Reads the next lines to get one almanac from
  157.                 readAlmanac(reader, week, toa);
  158.             }
  159.         } catch (IndexOutOfBoundsException | IOException e) {
  160.             throw new OrekitException(e, OrekitMessages.NOT_A_SUPPORTED_SEM_ALMANAC_FILE, name);
  161.         }
  162.     }

  163.     @Override
  164.     public boolean stillAcceptsData() {
  165.         return almanacs.isEmpty();
  166.     }

  167.     /**
  168.      * Gets all the {@link GPSAlmanac GPS almanacs} read from the file.
  169.      *
  170.      * @return the list of {@link GPSAlmanac} from the file
  171.      */
  172.     public List<GPSAlmanac> getAlmanacs() {
  173.         return almanacs;
  174.     }

  175.     /**
  176.      * Gets the PRN numbers of all the {@link GPSAlmanac GPS almanacs} read from the file.
  177.      *
  178.      * @return the PRN numbers of all the {@link GPSAlmanac GPS almanacs} read from the file
  179.      */
  180.     public List<Integer> getPRNNumbers() {
  181.         return prnList;
  182.     }

  183.     @Override
  184.     public String getSupportedNames() {
  185.         return super.getSupportedNames();
  186.     }

  187.     /**
  188.      * Builds {@link GPSAlmanac GPS almanacs} from data read in the file.
  189.      *
  190.      * @param reader the reader
  191.      * @param week the GPS week
  192.      * @param toa the Time of Applicability
  193.      * @throws IOException if GPSAlmanacs can't be built from the file
  194.      */
  195.     private void readAlmanac(final BufferedReader reader, final int week, final double toa)
  196.         throws IOException {
  197.         // Skips the empty line
  198.         reader.readLine();

  199.         // Create an empty GPS almanac and set the source
  200.         final GPSAlmanac almanac = new GPSAlmanac(timeScales, SatelliteSystem.GPS);
  201.         almanac.setSource(SOURCE);

  202.         try {
  203.             // Reads the PRN number from the first line
  204.             String[] token = getTokens(reader);
  205.             almanac.setPRN(Integer.parseInt(token[0].trim()));

  206.             // Reads the SV number from the second line
  207.             token = getTokens(reader);
  208.             almanac.setSVN(Integer.parseInt(token[0].trim()));

  209.             // Reads the average URA number from the third line
  210.             token = getTokens(reader);
  211.             almanac.setURA(Integer.parseInt(token[0].trim()));

  212.             // Reads the fourth line to get ecc, inc and dom
  213.             token = getTokens(reader);
  214.             almanac.setE(Double.parseDouble(token[0].trim()));
  215.             almanac.setI0(getInclination(Double.parseDouble(token[1].trim())));
  216.             almanac.setOmegaDot(toRadians(Double.parseDouble(token[2].trim())));

  217.             // Reads the fifth line to get sqa, raan and aop
  218.             token = getTokens(reader);
  219.             almanac.setSqrtA(Double.parseDouble(token[0].trim()));
  220.             almanac.setOmega0(toRadians(Double.parseDouble(token[1].trim())));
  221.             almanac.setPa(toRadians(Double.parseDouble(token[2].trim())));

  222.             // Reads the sixth line to get anom, af0 and af1
  223.             token = getTokens(reader);
  224.             almanac.setM0(toRadians(Double.parseDouble(token[0].trim())));
  225.             almanac.setAf0(Double.parseDouble(token[1].trim()));
  226.             almanac.setAf1(Double.parseDouble(token[2].trim()));

  227.             // Reads the seventh line to get health
  228.             token = getTokens(reader);
  229.             almanac.setHealth(Integer.parseInt(token[0].trim()));

  230.             // Reads the eighth line to get Satellite Configuration
  231.             token = getTokens(reader);
  232.             almanac.setSatConfiguration(Integer.parseInt(token[0].trim()));

  233.             // Adds the almanac to the list
  234.             almanac.setTime(toa);
  235.             almanac.setWeek(week);
  236.             almanacs.add(almanac);

  237.             // Adds the PRN to the list
  238.             prnList.add(almanac.getPRN());
  239.         } catch (IndexOutOfBoundsException aioobe) {
  240.             throw new IOException(aioobe);
  241.         }
  242.     }

  243.     /** Read a line and get tokens from.
  244.      *  @param reader the reader
  245.      *  @return the tokens from the read line
  246.      *  @throws IOException if the line is null
  247.      */
  248.     private String[] getTokens(final BufferedReader reader) throws IOException {
  249.         final String line = reader.readLine();
  250.         if (line != null) {
  251.             return SEPARATOR.split(line.trim());
  252.         } else {
  253.             throw new IOException();
  254.         }
  255.     }

  256.     /**
  257.      * Gets the inclination from the inclination offset.
  258.      *
  259.      * @param incOffset the inclination offset (semicircles)
  260.      * @return the inclination (rad)
  261.      */
  262.     private double getInclination(final double incOffset) {
  263.         return toRadians(INC_REF + incOffset);
  264.     }

  265.     /**
  266.      * Converts an angular value from semicircles to radians.
  267.      *
  268.      * @param semicircles the angular value in semicircles
  269.      * @return the angular value in radians
  270.      */
  271.     private double toRadians(final double semicircles) {
  272.         return GNSSConstants.GNSS_PI * semicircles;
  273.     }

  274. }