CenterName.java

  1. /* Copyright 2002-2025 CS GROUP
  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.definitions;

  18. import java.util.Locale;
  19. import java.util.function.Function;

  20. import org.orekit.annotation.DefaultDataContext;
  21. import org.orekit.bodies.CelestialBodies;
  22. import org.orekit.bodies.CelestialBody;
  23. import org.orekit.bodies.CelestialBodyFactory;
  24. import org.orekit.data.DataContext;
  25. import org.orekit.frames.FactoryManagedFrame;
  26. import org.orekit.frames.Frame;
  27. import org.orekit.frames.Predefined;

  28. /** Orbit central bodies for which a Celestial body can be created.
  29.  * @author sports
  30.  * @since 6.1
  31.  */
  32. public enum CenterName {
  33.     /** Solar system barycenter aggregated body. */
  34.     SOLAR_SYSTEM_BARYCENTER(CelestialBodies::getSolarSystemBarycenter),

  35.     /** Sun body. */
  36.     SUN(CelestialBodies::getSun),

  37.     /** Mercury body. */
  38.     MERCURY(CelestialBodies::getMercury),

  39.     /** Venus body. */
  40.     VENUS(CelestialBodies::getVenus),

  41.     /** Earth-Moon barycenter bodies pair. */
  42.     EARTH_MOON(CelestialBodies::getEarthMoonBarycenter),

  43.     /** Earth body. */
  44.     EARTH(CelestialBodies::getEarth),

  45.     /** Moon body. */
  46.     MOON(CelestialBodies::getMoon),

  47.     /** Mars body. */
  48.     MARS(CelestialBodies::getMars),

  49.     /** Jupiter body. */
  50.     JUPITER(CelestialBodies::getJupiter),

  51.     /** Saturn body. */
  52.     SATURN(CelestialBodies::getSaturn),

  53.     /** Uranus body. */
  54.     URANUS(CelestialBodies::getUranus),

  55.     /** Neptune body. */
  56.     NEPTUNE(CelestialBodies::getNeptune),

  57.     /** Pluto body. */
  58.     PLUTO(CelestialBodies::getPluto);

  59.     /** Suffix of the name of the inertial frame attached to a planet. */
  60.     private static final String INERTIAL_FRAME_SUFFIX = "/inertial";

  61.     /** Suffix of the name of the rotating frame attached to a planet. */
  62.     private static final String ROTATING_FRAME_SUFFIX = "/rotating";

  63.     /**
  64.      * Standardized locale to use, to ensure files can be exchanged without
  65.      * internationalization issues.
  66.      */
  67.     private static final Locale STANDARDIZED_LOCALE = Locale.US;

  68.     /** Substring common to all ITRF frames. */
  69.     /** Celestial body getter.
  70.      * @return getter for celestial body
  71.      */
  72.     private final transient Function<CelestialBodies, CelestialBody> celestialBodyGetter;

  73.     /** Simple constructor.
  74.      * @param celestialBodyGetter getter for celestial body
  75.      */
  76.     CenterName(final Function<CelestialBodies, CelestialBody> celestialBodyGetter) {
  77.         this.celestialBodyGetter = celestialBodyGetter;
  78.     }

  79.     /**
  80.      * Get the celestial body corresponding to the CCSDS constant.
  81.      *
  82.      * <p>This method uses the {@link DataContext#getDefault() default data context}.
  83.      *
  84.      * @return celestial body corresponding to the CCSDS constant
  85.      * @see #getCelestialBody(CelestialBodies)
  86.      */
  87.     @DefaultDataContext
  88.     public CelestialBody getCelestialBody() {
  89.         return getCelestialBody(DataContext.getDefault().getCelestialBodies());
  90.     }

  91.     /**
  92.      * Get the celestial body corresponding to the CCSDS constant.
  93.      *
  94.      * @param celestialBodies the set of celestial bodies to use.
  95.      * @return celestial body corresponding to the CCSDS constant
  96.      * @since 10.1
  97.      */
  98.     public CelestialBody getCelestialBody(final CelestialBodies celestialBodies) {
  99.         return celestialBodyGetter.apply(celestialBodies);
  100.     }

  101.     /**
  102.      * Guess the name of the center of the reference frame.
  103.      *
  104.      * @param frame a reference frame for ephemeris output.
  105.      * @return the string to use in the OEM file to describe the origin of {@code frame}.
  106.      */
  107.     public static String guessCenter(final Frame frame) {
  108.         final String name = frame.getName();
  109.         if (name.endsWith(INERTIAL_FRAME_SUFFIX) || name.endsWith(ROTATING_FRAME_SUFFIX)) {
  110.             return name.substring(0, name.length() - 9).toUpperCase(STANDARDIZED_LOCALE);
  111.         } else if (frame instanceof ModifiedFrame) {
  112.             return ((ModifiedFrame) frame).getCenterName();
  113.         } else if (frame.getName().equals(Predefined.ICRF.getName())) {
  114.             return CelestialBodyFactory.SOLAR_SYSTEM_BARYCENTER.toUpperCase(STANDARDIZED_LOCALE);
  115.         } else if (frame.getDepth() == 0 || frame instanceof FactoryManagedFrame) {
  116.             return "EARTH";
  117.         } else {
  118.             return "UNKNOWN";
  119.         }
  120.     }

  121.     /**
  122.      * Map an Orekit frame to a CCSDS center.
  123.      *
  124.      * @param frame a reference frame.
  125.      * @return the string to use in the OEM file to describe the origin of {@code frame},
  126.      * or null if no such center can be found
  127.      */
  128.     public static CenterName map(final Frame frame) {
  129.         try {
  130.             return CenterName.valueOf(guessCenter(frame));
  131.         } catch (IllegalArgumentException iae) {
  132.             // we were unable to find a match
  133.             return null;
  134.         }
  135.     }

  136. }