CcsdsTimeScale.java

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

  18. import org.orekit.annotation.DefaultDataContext;
  19. import org.orekit.data.DataContext;
  20. import org.orekit.errors.OrekitException;
  21. import org.orekit.errors.OrekitMessages;
  22. import org.orekit.time.AbsoluteDate;
  23. import org.orekit.time.DateTimeComponents;
  24. import org.orekit.time.TimeScale;
  25. import org.orekit.time.TimeScales;
  26. import org.orekit.utils.Constants;
  27. import org.orekit.utils.IERSConventions;

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

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

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

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

  99.         @Override
  100.         public TimeScale getTimeScale(final IERSConventions conventions, final TimeScales timeScales) {
  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, final TimeScales timeScales) {
  110.             return timeScales.getTAI();
  111.         }
  112.     },
  113.     /** Barycentric Coordinate Time. */
  114.     TCB {
  115.         @Override
  116.         public TimeScale getTimeScale(final IERSConventions conventions, final TimeScales timeScales) {
  117.             return timeScales.getTCB();
  118.         }
  119.     },
  120.     /** Barycentric Dynamical Time. */
  121.     TDB {
  122.         @Override
  123.         public TimeScale getTimeScale(final IERSConventions conventions, final TimeScales timeScales) {
  124.             return timeScales.getTDB();
  125.         }
  126.     },
  127.     /** Geocentric Coordinate Time. */
  128.     TCG {
  129.         @Override
  130.         public TimeScale getTimeScale(final IERSConventions conventions, final TimeScales timeScales) {
  131.             return timeScales.getTCG();
  132.         }
  133.     },
  134.     /** Terrestrial Time. */
  135.     TT {
  136.         @Override
  137.         public TimeScale getTimeScale(final IERSConventions conventions, final TimeScales timeScales) {
  138.             return timeScales.getTT();
  139.         }
  140.     },
  141.     /** Universal Time. */
  142.     UT1 {
  143.         @Override
  144.         public TimeScale getTimeScale(final IERSConventions conventions, final TimeScales timeScales) {
  145.             return timeScales.getUT1(conventions, false);
  146.         }
  147.     },
  148.     /** Universal Coordinated Time. */
  149.     UTC {
  150.         @Override
  151.         public TimeScale getTimeScale(final IERSConventions conventions, final TimeScales timeScales) {
  152.             return timeScales.getUTC();
  153.         }
  154.     };

  155.     /**
  156.      * Parse a date in this time scale.
  157.      *
  158.      * <p>This method uses the {@link DataContext#getDefault() default data context}.
  159.      *
  160.      * @param date                 a CCSDS date string.
  161.      * @param conventions          IERS conventions for {@link #UT1} and {@link #GMST}.
  162.      * @param missionReferenceDate epoch for {@link #MET} and {@link #MRT}.
  163.      * @return parsed {@code date}.
  164.      * @see #parseDate(String, IERSConventions, AbsoluteDate, TimeScales)
  165.      */
  166.     @DefaultDataContext
  167.     public AbsoluteDate parseDate(final String date,
  168.                                   final IERSConventions conventions,
  169.                                   final AbsoluteDate missionReferenceDate) {
  170.         return parseDate(date, conventions, missionReferenceDate,
  171.                 DataContext.getDefault().getTimeScales());
  172.     }

  173.     /**
  174.      * Parse a date in this time scale.
  175.      *
  176.      * @param date                 a CCSDS date string.
  177.      * @param conventions          IERS conventions for {@link #UT1} and {@link #GMST}.
  178.      * @param missionReferenceDate epoch for {@link #MET} and {@link #MRT}.
  179.      * @param timeScales the set of time scales to use.
  180.      * @return parsed {@code date}.
  181.      * @since 10.1
  182.      */
  183.     public AbsoluteDate parseDate(final String date,
  184.                                   final IERSConventions conventions,
  185.                                   final AbsoluteDate missionReferenceDate,
  186.                                   final TimeScales timeScales) {
  187.         return new AbsoluteDate(date, this.getTimeScale(conventions, timeScales));
  188.     }

  189.     /**
  190.      * Get the corresponding {@link TimeScale}.
  191.      *
  192.      * <p>This method uses the {@link DataContext#getDefault() default data context}.
  193.      *
  194.      * @param conventions IERS Conventions for the {@link #GMST} and {@link #UT1} scales.
  195.      * @return the time scale.
  196.      */
  197.     @DefaultDataContext
  198.     public TimeScale getTimeScale(final IERSConventions conventions) {
  199.         return getTimeScale(conventions, DataContext.getDefault().getTimeScales());
  200.     }

  201.     /**
  202.      * Get the corresponding {@link TimeScale}.
  203.      *
  204.      * @param conventions IERS Conventions for the {@link #GMST} and {@link #UT1} scales.
  205.      * @param timeScales the set of time scales to use.
  206.      * @return the time scale.
  207.      */
  208.     public abstract TimeScale getTimeScale(IERSConventions conventions,
  209.                                            TimeScales timeScales);

  210.     /**
  211.      * Check if {@code timeScale} is one of the values supported by this enum.
  212.      *
  213.      * @param timeScale specifier.
  214.      * @return {@code true} if {@link #valueOf(String)} will not throw an exception with
  215.      * the same string.
  216.      */
  217.     public static boolean contains(final String timeScale) {
  218.         for (final CcsdsTimeScale scale : values()) {
  219.             if (scale.name().equals(timeScale)) {
  220.                 return true;
  221.             }
  222.         }
  223.         return false;
  224.     }

  225. }