SEMParser.java

  1. /* Copyright 2002-2018 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.      * @exception OrekitException if some data can't be read, some
  93.      * file content is corrupted or no GPS almanac is available.
  94.      */
  95.     public void loadData() throws OrekitException {
  96.         // load the data from the configured data providers
  97.         DataProvidersManager.getInstance().feed(supportedNames, this);
  98.         if (almanacs.isEmpty()) {
  99.             throw new OrekitException(OrekitMessages.NO_SEM_ALMANAC_AVAILABLE);
  100.         }
  101.     }

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

  105.         // Clears the lists
  106.         almanacs.clear();
  107.         prnList.clear();

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  238. }