FrameFacade.java

  1. /* Copyright 2002-2022 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 org.orekit.data.DataContext;
  19. import org.orekit.errors.OrekitException;
  20. import org.orekit.frames.Frame;
  21. import org.orekit.utils.IERSConventions;

  22. /** Facade in front of several frames types in CCSDS messages.
  23.  * @author Luc Maisonobe
  24.  * @since 11.0
  25.  */
  26. public class FrameFacade {

  27.     /** Reference to node in Orekit frames tree. */
  28.     private final Frame frame;

  29.     /** Reference to celestial body centered frame. */
  30.     private final CelestialBodyFrame celestialBodyFrame;

  31.     /** Reference to orbit-relative frame. */
  32.     private final OrbitRelativeFrame orbitRelativeFrame;

  33.     /** Reference to spacecraft body frame. */
  34.     private final SpacecraftBodyFrame spacecraftBodyFrame;

  35.     /** Name of the frame. */
  36.     private final String name;

  37.     /** Simple constructor.
  38.      * <p>
  39.      * At most one of {@code celestialBodyFrame}, {@code orbitRelativeFrame}
  40.      * or {@code spacecraftBodyFrame} may be non null. They may all be null
  41.      * if frame is unknown, in which case only the name will be available.
  42.      * </p>
  43.      * @param frame reference to node in Orekit frames tree (may be null)
  44.      * @param celestialBodyFrame reference to celestial body centered frame (may be null)
  45.      * @param orbitRelativeFrame reference to orbit-relative frame (may be null)
  46.      * @param spacecraftBodyFrame reference to spacecraft body frame (may be null)
  47.      * @param name name of the frame
  48.      */
  49.     public FrameFacade(final Frame frame,
  50.                        final CelestialBodyFrame celestialBodyFrame,
  51.                        final OrbitRelativeFrame orbitRelativeFrame,
  52.                        final SpacecraftBodyFrame spacecraftBodyFrame,
  53.                        final String name) {
  54.         this.frame               = frame;
  55.         this.celestialBodyFrame  = celestialBodyFrame;
  56.         this.orbitRelativeFrame  = orbitRelativeFrame;
  57.         this.spacecraftBodyFrame = spacecraftBodyFrame;
  58.         this.name                = name;
  59.     }

  60.     /**
  61.      * Get the associated frame tree node.
  62.      * @return associated frame tree node, or null if none exists
  63.      */
  64.     public Frame asFrame() {
  65.         return frame;
  66.     }

  67.     /** Get the associated {@link CelestialBodyFrame celestial body frame}.
  68.      * @return associated celestial body frame, or null if frame is
  69.      * associated to a {@link #asOrbitRelativeFrame() orbit},
  70.      * a {@link #asSpacecraftBodyFrame spacecraft} or is not supported
  71.      */
  72.     public CelestialBodyFrame asCelestialBodyFrame() {
  73.         return celestialBodyFrame;
  74.     }

  75.     /** Get the associated {@link OrbitRelativeFrame orbit relative frame}.
  76.      * @return associated orbit relative frame, or null if frame is
  77.      * associated to a {@link #asCelestialBodyFrame() celestial body},
  78.      * a {@link #asSpacecraftBodyFrame spacecraft} or is not supported
  79.      */
  80.     public OrbitRelativeFrame asOrbitRelativeFrame() {
  81.         return orbitRelativeFrame;
  82.     }

  83.     /** Get the associated {@link SpacecraftBodyFrame spacecraft body frame}.
  84.      * @return associated spacecraft body frame, or null if frame is
  85.      * associated to a {@link #asCelestialBodyFrame() celestial body},
  86.      * an {@link #asOrbitRelativeFrame orbit} or is not supported
  87.      */
  88.     public SpacecraftBodyFrame asSpacecraftBodyFrame() {
  89.         return spacecraftBodyFrame;
  90.     }

  91.     /** Get the CCSDS name for the frame.
  92.      * @return CCSDS name
  93.      */
  94.     public String getName() {
  95.         return name;
  96.     }

  97.     /** Map an Orekit frame to a CCSDS frame facade.
  98.      * @param frame a reference frame.
  99.      * @return the CCSDSFrame corresponding to the Orekit frame
  100.      */
  101.     public static FrameFacade map(final Frame frame) {
  102.         final CelestialBodyFrame cbf = CelestialBodyFrame.map(frame);
  103.         return new FrameFacade(frame, cbf, null, null, cbf.getName());
  104.     }

  105.     /** Simple constructor.
  106.      * @param name name of the frame
  107.      * @param conventions IERS conventions to use
  108.      * @param simpleEOP if true, tidal effects are ignored when interpolating EOP
  109.      * @param dataContext to use when creating the frame
  110.      * @param allowCelestial if true, {@link CelestialBodyFrame} are allowed
  111.      * @param allowOrbit if true, {@link OrbitRelativeFrame} are allowed
  112.      * @param allowSpacecraft if true, {@link SpacecraftBodyFrame} are allowed
  113.      * @return frame facade corresponding to the CCSDS name
  114.      */
  115.     public static FrameFacade parse(final String name,
  116.                                     final IERSConventions conventions,
  117.                                     final boolean simpleEOP,
  118.                                     final DataContext dataContext,
  119.                                     final boolean allowCelestial,
  120.                                     final boolean allowOrbit,
  121.                                     final boolean allowSpacecraft) {
  122.         try {
  123.             final CelestialBodyFrame cbf = CelestialBodyFrame.parse(name);
  124.             if (allowCelestial) {
  125.                 return new FrameFacade(cbf.getFrame(conventions, simpleEOP, dataContext),
  126.                                        cbf, null, null, cbf.getName());
  127.             }
  128.         } catch (IllegalArgumentException iaeC) {
  129.             try {
  130.                 final OrbitRelativeFrame orf = OrbitRelativeFrame.valueOf(name.replace(' ', '_'));
  131.                 if (allowOrbit) {
  132.                     return new FrameFacade(null, null, orf, null, orf.name());
  133.                 }
  134.             } catch (IllegalArgumentException iaeO) {
  135.                 try {
  136.                     final SpacecraftBodyFrame sbf = SpacecraftBodyFrame.parse(name.replace(' ', '_'));
  137.                     if (allowSpacecraft) {
  138.                         return new FrameFacade(null, null, null, sbf, sbf.toString());
  139.                     }
  140.                 } catch (OrekitException | IllegalArgumentException e) {
  141.                     // nothing to do here, use fallback below
  142.                 }
  143.             }
  144.         }

  145.         // we don't know any frame with this name, just store the name itself
  146.         return new FrameFacade(null, null, null, null, name);

  147.     }

  148. }