MODProvider.java

  1. /* Copyright 2002-2024 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.frames;

  18. import java.io.Serializable;
  19. import java.util.HashMap;
  20. import java.util.Map;

  21. import org.hipparchus.Field;
  22. import org.hipparchus.CalculusFieldElement;
  23. import org.hipparchus.geometry.euclidean.threed.FieldRotation;
  24. import org.hipparchus.geometry.euclidean.threed.Rotation;
  25. import org.hipparchus.geometry.euclidean.threed.RotationConvention;
  26. import org.hipparchus.geometry.euclidean.threed.RotationOrder;
  27. import org.hipparchus.geometry.euclidean.threed.Vector3D;
  28. import org.orekit.annotation.DefaultDataContext;
  29. import org.orekit.data.DataContext;
  30. import org.orekit.errors.OrekitException;
  31. import org.orekit.errors.OrekitInternalError;
  32. import org.orekit.time.AbsoluteDate;
  33. import org.orekit.time.FieldAbsoluteDate;
  34. import org.orekit.time.TimeScalarFunction;
  35. import org.orekit.time.TimeScales;
  36. import org.orekit.time.TimeVectorFunction;
  37. import org.orekit.utils.IERSConventions;

  38. /** Mean Equator, Mean Equinox Frame.
  39.  * <p>This frame handles precession effects according to to selected IERS conventions.</p>
  40.  * <p>Its parent frame is the GCRF frame.
  41.  * <p>It is sometimes called Mean of Date (MoD) frame.
  42.  * @author Pascal Parraud
  43.  */
  44. class MODProvider implements TransformProvider {

  45.     /** Serializable UID. */
  46.     private static final long serialVersionUID = 20130920L;

  47.     /** Conventions. */
  48.     private final IERSConventions conventions;

  49.     /** Function computing the precession angles. */
  50.     private final transient TimeVectorFunction precessionFunction;

  51.     /** Constant rotation between ecliptic and equator poles at J2000.0. */
  52.     private final Rotation r4;

  53.     /** Constant rotations between ecliptic and equator poles at J2000.0. */
  54.     private final transient Map<Field<? extends CalculusFieldElement<?>>, FieldRotation<? extends CalculusFieldElement<?>>> fieldR4;

  55.     /** Simple constructor.
  56.      * @param conventions IERS conventions to apply
  57.      * @param timeScales used to define this frame.
  58.      */
  59.     MODProvider(final IERSConventions conventions, final TimeScales timeScales) {
  60.         this.conventions        = conventions;
  61.         this.precessionFunction = conventions.getPrecessionFunction(timeScales);
  62.         final TimeScalarFunction epsilonAFunction =
  63.                 conventions.getMeanObliquityFunction(timeScales);
  64.         final AbsoluteDate date0 = conventions.getNutationReferenceEpoch(timeScales);
  65.         final double epsilon0 = epsilonAFunction.value(date0);
  66.         r4 = new Rotation(Vector3D.PLUS_I, epsilon0, RotationConvention.FRAME_TRANSFORM);
  67.         fieldR4 = new HashMap<>();
  68.     }

  69.     /** {@inheritDoc} */
  70.     @Override
  71.     public Transform getTransform(final AbsoluteDate date) {

  72.         // compute the precession angles phiA, omegaA, chiA
  73.         final double[] angles = precessionFunction.value(date);

  74.         // complete precession
  75.         final Rotation precession = r4.compose(new Rotation(RotationOrder.ZXZ, RotationConvention.FRAME_TRANSFORM,
  76.                                                             -angles[0], -angles[1], angles[2]),
  77.                                                RotationConvention.FRAME_TRANSFORM);

  78.         // set up the transform from parent GCRF
  79.         return new Transform(date, precession);

  80.     }

  81.     /** {@inheritDoc} */
  82.     @Override
  83.     public <T extends CalculusFieldElement<T>> FieldTransform<T> getTransform(final FieldAbsoluteDate<T> date) {

  84.         // compute the precession angles phiA, omegaA, chiA
  85.         final T[] angles = precessionFunction.value(date);

  86.         @SuppressWarnings("unchecked")
  87.         FieldRotation<T> fR4 = (FieldRotation<T>) fieldR4.get(date.getField());
  88.         if (fR4 == null) {
  89.             fR4 = new FieldRotation<>(date.getField(), r4);
  90.             fieldR4.put(date.getField(), fR4);
  91.         }

  92.         // complete precession
  93.         final FieldRotation<T> precession = fR4.compose(new FieldRotation<>(RotationOrder.ZXZ, RotationConvention.FRAME_TRANSFORM,
  94.                                                                             angles[0].negate(),
  95.                                                                             angles[1].negate(),
  96.                                                                             angles[2]),
  97.                                                         RotationConvention.FRAME_TRANSFORM);

  98.         // set up the transform from parent GCRF
  99.         return new FieldTransform<>(date, precession);

  100.     }

  101.     /** Replace the instance with a data transfer object for serialization.
  102.      * <p>
  103.      * This intermediate class serializes only the frame key.
  104.      * </p>
  105.      * @return data transfer object that will be serialized
  106.      */
  107.     @DefaultDataContext
  108.     private Object writeReplace() {
  109.         return new DataTransferObject(conventions);
  110.     }

  111.     /** Internal class used only for serialization. */
  112.     @DefaultDataContext
  113.     private static class DataTransferObject implements Serializable {

  114.         /** Serializable UID. */
  115.         private static final long serialVersionUID = 20131209L;

  116.         /** Conventions. */
  117.         private final IERSConventions conventions;

  118.         /** Simple constructor.
  119.          * @param conventions IERSConventions conventions
  120.          */
  121.         DataTransferObject(final IERSConventions conventions) {
  122.             this.conventions = conventions;
  123.         }

  124.         /** Replace the deserialized data transfer object with a {@link MODProvider}.
  125.          * @return replacement {@link MODProvider}
  126.          */
  127.         private Object readResolve() {
  128.             try {
  129.                 // retrieve a managed frame
  130.                 return new MODProvider(conventions,
  131.                         DataContext.getDefault().getTimeScales());
  132.             } catch (OrekitException oe) {
  133.                 throw new OrekitInternalError(oe);
  134.             }
  135.         }

  136.     }

  137. }