SEMParser.java

  1. /* Copyright 2002-2019 CS Systèmes d'Information
  2.  * Licensed to CS Systèmes d'Information (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.text.ParseException;
  23. import java.util.ArrayList;
  24. import java.util.List;

  25. import org.orekit.data.DataLoader;
  26. import org.orekit.data.DataProvidersManager;
  27. import org.orekit.errors.OrekitException;
  28. import org.orekit.errors.OrekitMessages;
  29. import org.orekit.propagation.analytical.gnss.GPSOrbitalElements;


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

  46.     // Constants
  47.     /** The source of the almanacs. */
  48.     private static final String SOURCE = "SEM";

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

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

  53.     /** Separator for parsing. */
  54.     private static final String SEPARATOR = "\\s+";

  55.     // Fields
  56.     /** Regular expression for supported files names. */
  57.     private final String supportedNames;

  58.     /** the list of all the almanacs read from the file. */
  59.     private final List<GPSAlmanac> almanacs;

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

  62.     /** Simple constructor.
  63.      *
  64.      * <p>This constructor does not load any data by itself. Data must be loaded
  65.      * later on by calling one of the {@link #loadData() loadData()} method or
  66.      * the {@link #loadData(InputStream, String) loadData(inputStream, fileName)}
  67.      * method.</p>
  68.      *
  69.      * <p>The supported files names are used when getting data from the
  70.      * {@link #loadData() loadData()} method that relies on the
  71.      * {@link DataProvidersManager data providers manager}. They are useless when
  72.      * getting data from the {@link #loadData(InputStream, String) loadData(input, name)}
  73.      * method.</p>
  74.      *
  75.      * @param supportedNames regular expression for supported files names
  76.      * (if null, a default pattern matching files with a ".al3" extension will be used)
  77.      * @see #loadData()
  78.      */
  79.     public SEMParser(final String supportedNames) {
  80.         this.supportedNames = (supportedNames == null) ? DEFAULT_SUPPORTED_NAMES : supportedNames;
  81.         this.almanacs =  new ArrayList<GPSAlmanac>();
  82.         this.prnList = new ArrayList<Integer>();
  83.     }

  84.     /**
  85.      * Loads almanacs.
  86.      *
  87.      * <p>The almanacs already loaded in the instance will be discarded
  88.      * and replaced by the newly loaded data.</p>
  89.      * <p>This feature is useful when the file selection is already set up by
  90.      * the {@link DataProvidersManager data providers manager} configuration.</p>
  91.      *
  92.      */
  93.     public void loadData() {
  94.         // load the data from the configured data providers
  95.         DataProvidersManager.getInstance().feed(supportedNames, this);
  96.         if (almanacs.isEmpty()) {
  97.             throw new OrekitException(OrekitMessages.NO_SEM_ALMANAC_AVAILABLE);
  98.         }
  99.     }

  100.     @Override
  101.     public void loadData(final InputStream input, final String name)
  102.         throws IOException, ParseException, OrekitException {

  103.         // Clears the lists
  104.         almanacs.clear();
  105.         prnList.clear();

  106.         // Creates the reader
  107.         final BufferedReader reader = new BufferedReader(new InputStreamReader(input, "UTF-8"));

  108.         try {
  109.             // Reads the number of almanacs in the file from the first line
  110.             String[] token = getTokens(reader);
  111.             final int almanacNb = Integer.parseInt(token[0].trim());

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

  116.             // Loop over data blocks
  117.             for (int i = 0; i < almanacNb; i++) {
  118.                 // Reads the next lines to get one almanac from
  119.                 readAlmanac(reader, week, toa);
  120.             }
  121.         } catch (IndexOutOfBoundsException ioobe) {
  122.             throw new OrekitException(OrekitMessages.NOT_A_SUPPORTED_SEM_ALMANAC_FILE, name);
  123.         } catch (IOException ioe) {
  124.             throw new OrekitException(OrekitMessages.NOT_A_SUPPORTED_SEM_ALMANAC_FILE, name);
  125.         }
  126.     }

  127.     @Override
  128.     public boolean stillAcceptsData() {
  129.         return almanacs.isEmpty();
  130.     }

  131.     /**
  132.      * Gets all the {@link GPSAlmanac GPS almanacs} read from the file.
  133.      *
  134.      * @return the list of {@link GPSAlmanac} from the file
  135.      */
  136.     public List<GPSAlmanac> getAlmanacs() {
  137.         return almanacs;
  138.     }

  139.     /**
  140.      * Gets the PRN numbers of all the {@link GPSAlmanac GPS almanacs} read from the file.
  141.      *
  142.      * @return the PRN numbers of all the {@link GPSAlmanac GPS almanacs} read from the file
  143.      */
  144.     public List<Integer> getPRNNumbers() {
  145.         return prnList;
  146.     }

  147.     /** Get the supported names for data files.
  148.      * @return regular expression for the supported names for data files
  149.      */
  150.     public String getSupportedNames() {
  151.         return supportedNames;
  152.     }

  153.     /**
  154.      * Builds {@link GPSAlmanac GPS almanacs} from data read in the file.
  155.      *
  156.      * @param reader the reader
  157.      * @param week the GPS week
  158.      * @param toa the Time of Applicability
  159.      * @throws IOException if GPSAlmanacs can't be built from the file
  160.      */
  161.     private void readAlmanac(final BufferedReader reader, final int week, final double toa)
  162.         throws IOException {
  163.         // Skips the empty line
  164.         reader.readLine();

  165.         try {
  166.             // Reads the PRN number from the first line
  167.             String[] token = getTokens(reader);
  168.             final int prn = Integer.parseInt(token[0].trim());

  169.             // Reads the SV number from the second line
  170.             token = getTokens(reader);
  171.             final int svn = Integer.parseInt(token[0].trim());

  172.             // Reads the average URA number from the third line
  173.             token = getTokens(reader);
  174.             final int ura = Integer.parseInt(token[0].trim());

  175.             // Reads the fourth line to get ecc, inc and dom
  176.             token = getTokens(reader);
  177.             final double ecc = Double.parseDouble(token[0].trim());
  178.             final double inc = getInclination(Double.parseDouble(token[1].trim()));
  179.             final double dom = toRadians(Double.parseDouble(token[2].trim()));

  180.             // Reads the fifth line to get sqa, raan and aop
  181.             token = getTokens(reader);
  182.             final double sqa  = Double.parseDouble(token[0].trim());
  183.             final double om0 = toRadians(Double.parseDouble(token[1].trim()));
  184.             final double aop  = toRadians(Double.parseDouble(token[2].trim()));

  185.             // Reads the sixth line to get anom, af0 and af1
  186.             token = getTokens(reader);
  187.             final double anom = toRadians(Double.parseDouble(token[0].trim()));
  188.             final double af0 = Double.parseDouble(token[1].trim());
  189.             final double af1 = Double.parseDouble(token[2].trim());

  190.             // Reads the seventh line to get health
  191.             token = getTokens(reader);
  192.             final int health = Integer.parseInt(token[0].trim());

  193.             // Reads the eighth line to get Satellite Configuration
  194.             token = getTokens(reader);
  195.             final int conf = Integer.parseInt(token[0].trim());

  196.             // Adds the almanac to the list
  197.             almanacs.add(new GPSAlmanac(SOURCE, prn, svn, week, toa, sqa, ecc, inc, om0,
  198.                                         dom, aop, anom, af0, af1, health, ura, conf));

  199.             // Adds the PRN to the list
  200.             prnList.add(prn);
  201.         } catch (IndexOutOfBoundsException aioobe) {
  202.             throw new IOException();
  203.         }
  204.     }

  205.     /** Read a line and get tokens from.
  206.      *  @param reader the reader
  207.      *  @return the tokens from the read line
  208.      *  @throws IOException if the line is null
  209.      */
  210.     private String[] getTokens(final BufferedReader reader) throws IOException {
  211.         final String line = reader.readLine();
  212.         if (line != null) {
  213.             return line.trim().split(SEPARATOR);
  214.         } else {
  215.             throw new IOException();
  216.         }
  217.     }

  218.     /**
  219.      * Gets the inclination from the inclination offset.
  220.      *
  221.      * @param incOffset the inclination offset (semicircles)
  222.      * @return the inclination (rad)
  223.      */
  224.     private double getInclination(final double incOffset) {
  225.         return toRadians(INC_REF + incOffset);
  226.     }

  227.     /**
  228.      * Converts an angular value from semicircles to radians.
  229.      *
  230.      * @param semicircles the angular value in semicircles
  231.      * @return the angular value in radians
  232.      */
  233.     private double toRadians(final double semicircles) {
  234.         return GPSOrbitalElements.GPS_PI * semicircles;
  235.     }

  236. }