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.             return TimeScalesFactory.getGMST(conventions, false);
  37.         }
  38.     },
  39.     /** Global Positioning System. */
  40.     GPS {
  41.         @Override
  42.         public TimeScale getTimeScale(final IERSConventions conventions) {
  43.             return TimeScalesFactory.getGPS();
  44.         }
  45.     },
  46.     /** Mission Elapsed Time. */
  47.     MET {
  48.         @Override
  49.         public AbsoluteDate parseDate(final String date,
  50.                                       final IERSConventions conventions,
  51.                                       final AbsoluteDate missionReferenceDate) {
  52.             final DateTimeComponents clock = DateTimeComponents.parseDateTime(date);
  53.             final double offset = clock.getDate().getYear() * Constants.JULIAN_YEAR +
  54.                     clock.getDate().getDayOfYear() * Constants.JULIAN_DAY +
  55.                     clock.getTime().getSecondsInUTCDay();
  56.             return missionReferenceDate.shiftedBy(offset);
  57.         }

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

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

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

  150.     /**
  151.      * Parse a date in this time scale.
  152.      *
  153.      * @param date                 a CCSDS date string.
  154.      * @param conventions          IERS conventions for {@link #UT1} and {@link #GMST}.
  155.      * @param missionReferenceDate epoch for {@link #MET} and {@link #MRT}.
  156.      * @return parsed {@code date}.
  157.      */
  158.     public AbsoluteDate parseDate(final String date,
  159.                                   final IERSConventions conventions,
  160.                                   final AbsoluteDate missionReferenceDate) {
  161.         return new AbsoluteDate(date, this.getTimeScale(conventions));
  162.     }

  163.     /**
  164.      * Get the corresponding {@link TimeScale}.
  165.      *
  166.      * @param conventions IERS Conventions for the {@link #GMST} and {@link #UT1} scales.
  167.      * @return the time scale.
  168.      */
  169.     public abstract TimeScale getTimeScale(IERSConventions conventions);

  170.     /**
  171.      * Check if {@code timeScale} is one of the values supported by this enum.
  172.      *
  173.      * @param timeScale specifier.
  174.      * @return {@code true} if {@link #valueOf(String)} will not throw an exception with
  175.      * the same string.
  176.      */
  177.     public static boolean contains(final String timeScale) {
  178.         for (final CcsdsTimeScale scale : values()) {
  179.             if (scale.name().equals(timeScale)) {
  180.                 return true;
  181.             }
  182.         }
  183.         return false;
  184.     }

  185. }