CcsdsTimeScale.java

  1. /* Contributed in the public domain.
  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.files.ccsds;

  18. import org.orekit.errors.OrekitException;
  19. import org.orekit.errors.OrekitMessages;
  20. import org.orekit.time.AbsoluteDate;
  21. import org.orekit.time.DateTimeComponents;
  22. import org.orekit.time.TimeScale;
  23. import org.orekit.time.TimeScalesFactory;
  24. import org.orekit.utils.Constants;
  25. import org.orekit.utils.IERSConventions;

  26. /**
  27.  * The set of time scales defined in Annex A of the ODM CCSDS standard 502.0-B-2.
  28.  *
  29.  * @author Evan Ward
  30.  */
  31. public enum CcsdsTimeScale {

  32.     /** Greenwich Mean Sidereal Time. */
  33.     GMST {
  34.         @Override
  35.         public TimeScale getTimeScale(final IERSConventions conventions)
  36.                 throws OrekitException {
  37.             return TimeScalesFactory.getGMST(conventions, false);
  38.         }
  39.     },
  40.     /** Global Positioning System. */
  41.     GPS {
  42.         @Override
  43.         public TimeScale getTimeScale(final IERSConventions conventions) {
  44.             return TimeScalesFactory.getGPS();
  45.         }
  46.     },
  47.     /** Mission Elapsed Time. */
  48.     MET {
  49.         @Override
  50.         public AbsoluteDate parseDate(final String date,
  51.                                       final IERSConventions conventions,
  52.                                       final AbsoluteDate missionReferenceDate) {
  53.             final DateTimeComponents clock = DateTimeComponents.parseDateTime(date);
  54.             final double offset = clock.getDate().getYear() * Constants.JULIAN_YEAR +
  55.                     clock.getDate().getDayOfYear() * Constants.JULIAN_DAY +
  56.                     clock.getTime().getSecondsInUTCDay();
  57.             return missionReferenceDate.shiftedBy(offset);
  58.         }

  59.         @Override
  60.         public TimeScale getTimeScale(final IERSConventions conventions)
  61.                 throws OrekitException {
  62.             throw new OrekitException(
  63.                     OrekitMessages.CCSDS_NO_CORRESPONDING_TIME_SCALE,
  64.                     "MET");
  65.         }
  66.     },
  67.     /** Mission Relative Time. */
  68.     MRT {
  69.         @Override
  70.         public AbsoluteDate parseDate(final String date,
  71.                                       final IERSConventions conventions,
  72.                                       final AbsoluteDate missionReferenceDate) {
  73.             final DateTimeComponents clock = DateTimeComponents.parseDateTime(date);
  74.             final double offset = clock.getDate().getYear() * Constants.JULIAN_YEAR +
  75.                     clock.getDate().getDayOfYear() * Constants.JULIAN_DAY +
  76.                     clock.getTime().getSecondsInUTCDay();
  77.             return missionReferenceDate.shiftedBy(offset);
  78.         }

  79.         @Override
  80.         public TimeScale getTimeScale(final IERSConventions conventions)
  81.                 throws OrekitException {
  82.             throw new OrekitException(
  83.                     OrekitMessages.CCSDS_NO_CORRESPONDING_TIME_SCALE,
  84.                     "MRT");
  85.         }
  86.     },
  87.     /** Spacecraft Clock. Not currently Implemented. */
  88.     SCLK {
  89.         @Override
  90.         public AbsoluteDate parseDate(final String date,
  91.                                       final IERSConventions conventions,
  92.                                       final AbsoluteDate missionReferenceDate)
  93.                 throws OrekitException {
  94.             throw new OrekitException(
  95.                     OrekitMessages.CCSDS_TIME_SYSTEM_NOT_IMPLEMENTED,
  96.                     this.name());
  97.         }

  98.         @Override
  99.         public TimeScale getTimeScale(final IERSConventions conventions)
  100.                 throws OrekitException {
  101.             throw new OrekitException(
  102.                     OrekitMessages.CCSDS_NO_CORRESPONDING_TIME_SCALE,
  103.                     this.name());
  104.         }
  105.     },
  106.     /** International Atomic Time. */
  107.     TAI {
  108.         @Override
  109.         public TimeScale getTimeScale(final IERSConventions conventions) {
  110.             return TimeScalesFactory.getTAI();
  111.         }
  112.     },
  113.     /** Barycentric Coordinate Time. */
  114.     TCB {
  115.         @Override
  116.         public TimeScale getTimeScale(final IERSConventions conventions) {
  117.             return TimeScalesFactory.getTCB();
  118.         }
  119.     },
  120.     /** Barycentric Dynamical Time. */
  121.     TDB {
  122.         @Override
  123.         public TimeScale getTimeScale(final IERSConventions conventions)
  124.                 throws OrekitException {
  125.             return TimeScalesFactory.getTDB();
  126.         }
  127.     },
  128.     /** Geocentric Coordinate Time. */
  129.     TCG {
  130.         @Override
  131.         public TimeScale getTimeScale(final IERSConventions conventions) {
  132.             return TimeScalesFactory.getTCG();
  133.         }
  134.     },
  135.     /** Terrestrial Time. */
  136.     TT {
  137.         @Override
  138.         public TimeScale getTimeScale(final IERSConventions conventions) {
  139.             return TimeScalesFactory.getTT();
  140.         }
  141.     },
  142.     /** Universal Time. */
  143.     UT1 {
  144.         @Override
  145.         public TimeScale getTimeScale(final IERSConventions conventions)
  146.                 throws OrekitException {
  147.             return TimeScalesFactory.getUT1(conventions, false);
  148.         }
  149.     },
  150.     /** Universal Coordinated Time. */
  151.     UTC {
  152.         @Override
  153.         public TimeScale getTimeScale(final IERSConventions conventions)
  154.                 throws OrekitException {
  155.             return TimeScalesFactory.getUTC();
  156.         }
  157.     };

  158.     /**
  159.      * Parse a date in this time scale.
  160.      *
  161.      * @param date                 a CCSDS date string.
  162.      * @param conventions          IERS conventions for {@link #UT1} and {@link #GMST}.
  163.      * @param missionReferenceDate epoch for {@link #MET} and {@link #MRT}.
  164.      * @return parsed {@code date}.
  165.      * @throws OrekitException if an {@link AbsoluteDate} cannot be created from {@code
  166.      *                         date} in this time scale.
  167.      */
  168.     public AbsoluteDate parseDate(final String date,
  169.                                   final IERSConventions conventions,
  170.                                   final AbsoluteDate missionReferenceDate)
  171.             throws OrekitException {
  172.         return new AbsoluteDate(date, this.getTimeScale(conventions));
  173.     }

  174.     /**
  175.      * Get the corresponding {@link TimeScale}.
  176.      *
  177.      * @param conventions IERS Conventions for the {@link #GMST} and {@link #UT1} scales.
  178.      * @return the time scale.
  179.      * @throws OrekitException if the time scale cannot be constructed.
  180.      */
  181.     public abstract TimeScale getTimeScale(IERSConventions conventions)
  182.             throws OrekitException;

  183.     /**
  184.      * Check if {@code timeScale} is one of the values supported by this enum.
  185.      *
  186.      * @param timeScale specifier.
  187.      * @return {@code true} if {@link #valueOf(String)} will not throw an exception with
  188.      * the same string.
  189.      */
  190.     public static boolean contains(final String timeScale) {
  191.         for (final CcsdsTimeScale scale : values()) {
  192.             if (scale.name().equals(timeScale)) {
  193.                 return true;
  194.             }
  195.         }
  196.         return false;
  197.     }

  198. }