SatInSystem.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.gnss;

  18. import java.util.Locale;

  19. /** Container for satellite system and PRN.
  20.  * @author Luc Maisonobe
  21.  * @since 12.0
  22.  */
  23. public class SatInSystem {

  24.     /** Value representing all PRNs in the system. */
  25.     public static final int ANY_PRN = -1;

  26.     /** PRN shift for SBAS system. */
  27.     private static final int SBAS_SHIFT = 100;

  28.     /** PRN shift for QZSS system. */
  29.     private static final int QZSS_SHIFT = 192;

  30.     /** Satellite system. */
  31.     private final SatelliteSystem system;

  32.     /** PRN number. */
  33.     private final int prn;

  34.     /** Simple constructor.
  35.      * @param system satellite system
  36.      * @param prn Pseudo Random Number or {@link #ANY_PRN} to represent any satellite in the system
  37.      */
  38.     public SatInSystem(final SatelliteSystem system, final int prn) {
  39.         this.system = system;
  40.         this.prn    = prn;
  41.     }

  42.     /** Simple constructor.
  43.      * <p>
  44.      * The RINEX 3 characters code starts with a letter representing the
  45.      * {@link SatelliteSystem#getKey() satellite system key} followed
  46.      * by a 2 digits integer which represents either
  47.      * </p>
  48.      * <ul>
  49.      *   <li>the Pseudo Random Number in the general case</li>
  50.      *   <li>the Pseudo Random Number minus 100 for {@link SatelliteSystem#SBAS}</li>
  51.      *   <li>the Pseudo Random Number minus 192 for {@link SatelliteSystem#QZSS}</li>
  52.      * </ul>
  53.      * <p>
  54.      * if only the letter is present, then prn is set to {@link #ANY_PRN}
  55.      * </p>
  56.      * @param rinexCode RINEX 3 characters code
  57.      * @since 13.0
  58.      */
  59.     public SatInSystem(final String rinexCode) {

  60.         // parse system
  61.         this.system = SatelliteSystem.parseSatelliteSystem(rinexCode.substring(0, 1));

  62.         // parse PRN
  63.         final String trimmed = rinexCode.substring(1).trim();
  64.         if (trimmed.isEmpty()) {
  65.             this.prn = ANY_PRN;
  66.         } else {
  67.             final int index = Integer.parseInt(trimmed);
  68.             if (system == SatelliteSystem.SBAS) {
  69.                 this.prn = index + SBAS_SHIFT;
  70.             } else if (system == SatelliteSystem.QZSS) {
  71.                 this.prn = index + QZSS_SHIFT;
  72.             } else {
  73.                 this.prn = index;
  74.             }
  75.         }

  76.     }

  77.     /** Get the system this satellite belongs to.
  78.      * @return system this satellite belongs to
  79.      */
  80.     public SatelliteSystem getSystem() {
  81.         return system;
  82.     }

  83.     /** Get the Pseudo Random Number of the satellite.
  84.      * @return Pseudo Random Number of the satellite, or {@link #ANY_PRN} to represent
  85.      * any PRN in the system
  86.      */
  87.     public int getPRN() {
  88.         return prn;
  89.     }

  90.     /** Get a 2-digits Pseudo Random Number for RINEX files.
  91.      * @return 2-digits Pseudo Random Number for RINEX files
  92.      */
  93.     public int getTwoDigitsRinexPRN() {
  94.         return system == SatelliteSystem.SBAS ?
  95.                prn - SBAS_SHIFT :
  96.                (system == SatelliteSystem.QZSS ? prn - QZSS_SHIFT : prn);
  97.     }

  98.     /** {@inheritDoc} */
  99.     @Override
  100.     public String toString() {
  101.         return prn < 0 ?
  102.                String.format(Locale.US, "%c  ", system.getKey()) :
  103.                String.format(Locale.US, "%c%02d", system.getKey(), getTwoDigitsRinexPRN());
  104.     }

  105.     @Override
  106.     public boolean equals(final Object object) {
  107.         if (object instanceof SatInSystem) {
  108.             final SatInSystem other = (SatInSystem) object;
  109.             return getSystem().equals(other.getSystem()) && getPRN() == other.getPRN();
  110.         }
  111.         return false;
  112.     }

  113.     @Override
  114.     public int hashCode() {
  115.         return Character.hashCode(getSystem().getKey()) ^ getPRN();
  116.     }

  117. }