1   /* Copyright 2002-2023 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  
19  import java.io.Serializable;
20  
21  import org.hipparchus.CalculusFieldElement;
22  import org.hipparchus.geometry.euclidean.threed.FieldRotation;
23  import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
24  import org.hipparchus.geometry.euclidean.threed.Rotation;
25  import org.hipparchus.geometry.euclidean.threed.RotationConvention;
26  import org.hipparchus.geometry.euclidean.threed.Vector3D;
27  import org.hipparchus.util.MathUtils;
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.TimeScale;
36  import org.orekit.utils.Constants;
37  
38  /** Terrestrial Intermediate Reference Frame.
39   * <p> The pole motion is not considered : Pseudo Earth Fixed Frame. It handles
40   * the earth rotation angle, its parent frame is the {@link CIRFProvider}</p>
41   */
42  class TIRFProvider implements EOPBasedTransformProvider {
43  
44      /** Serializable UID. */
45      private static final long serialVersionUID = 20130919L;
46  
47      /** Angular velocity of the Earth, in rad/s. */
48      private static final double AVE = 7.292115146706979e-5;
49  
50      /** EOP history. */
51      private final EOPHistory eopHistory;
52  
53      /** UT1 time scale. */
54      private final transient TimeScale ut1;
55  
56      /** ERA function. */
57      private final transient TimeScalarFunction era;
58  
59      /** Simple constructor.
60       * @param eopHistory EOP history
61       * @param ut1 the UT1 time scale.
62       */
63      protected TIRFProvider(final EOPHistory eopHistory, final TimeScale ut1) {
64  
65          this.ut1        = ut1;
66          this.eopHistory = eopHistory;
67          this.era        = eopHistory.getConventions().getEarthOrientationAngleFunction(
68                  ut1,
69                  eopHistory.getTimeScales().getTAI());
70  
71      }
72  
73      /** {@inheritDoc} */
74      @Override
75      public EOPHistory getEOPHistory() {
76          return eopHistory;
77      }
78  
79      /** {@inheritDoc} */
80      @Override
81      public TIRFProvider getNonInterpolatingProvider() {
82          return new TIRFProvider(eopHistory.getEOPHistoryWithoutCachedTidalCorrection(), ut1);
83      }
84  
85      /** {@inheritDoc} */
86      @Override
87      public Transform getTransform(final AbsoluteDate date) {
88  
89          // compute proper rotation
90          final double correctedERA = era.value(date);
91  
92          // compute true angular rotation of Earth, in rad/s
93          final double lod = (eopHistory == null) ? 0.0 : eopHistory.getLOD(date);
94          final double omp = AVE * (1 - lod / Constants.JULIAN_DAY);
95          final Vector3D rotationRate = new Vector3D(omp, Vector3D.PLUS_K);
96  
97          // set up the transform from parent CIRF
98          final Rotation rotation     = new Rotation(Vector3D.PLUS_K, correctedERA, RotationConvention.FRAME_TRANSFORM);
99          return new Transform(date, rotation, rotationRate);
100 
101     }
102 
103     /** {@inheritDoc} */
104     @Override
105     public StaticTransform getStaticTransform(final AbsoluteDate date) {
106         // compute proper rotation
107         final double correctedERA = era.value(date);
108         // set up the transform from parent CIRF
109         final Rotation rotation = new Rotation(
110                 Vector3D.PLUS_K,
111                 correctedERA,
112                 RotationConvention.FRAME_TRANSFORM);
113         return StaticTransform.of(date, rotation);
114     }
115 
116     /** {@inheritDoc} */
117     @Override
118     public <T extends CalculusFieldElement<T>> FieldTransform<T> getTransform(final FieldAbsoluteDate<T> date) {
119 
120         // compute proper rotation
121         final T correctedERA = era.value(date);
122 
123         // compute true angular rotation of Earth, in rad/s
124         final T lod = (eopHistory == null) ? date.getField().getZero() : eopHistory.getLOD(date);
125         final T omp = lod.divide(Constants.JULIAN_DAY).subtract(1).multiply(-AVE);
126         final FieldVector3D<T> rotationRate = new FieldVector3D<>(omp, Vector3D.PLUS_K);
127 
128         // set up the transform from parent CIRF
129         final FieldRotation<T> rotation = new FieldRotation<>(FieldVector3D.getPlusK(date.getField()),
130                                                               correctedERA,
131                                                               RotationConvention.FRAME_TRANSFORM);
132         return new FieldTransform<>(date, rotation, rotationRate);
133 
134     }
135 
136     /** {@inheritDoc} */
137     @Override
138     public <T extends CalculusFieldElement<T>> FieldStaticTransform<T> getStaticTransform(final FieldAbsoluteDate<T> date) {
139         // compute proper rotation
140         final T correctedERA = era.value(date);
141         // set up the transform from parent CIRF
142         final FieldRotation<T> rotation = new FieldRotation<>(
143                 FieldVector3D.getPlusK(date.getField()),
144                 correctedERA,
145                 RotationConvention.FRAME_TRANSFORM);
146         return FieldStaticTransform.of(date, rotation);
147     }
148 
149     /** Get the Earth Rotation Angle at the current date.
150      * @param  date the date
151      * @return Earth Rotation Angle at the current date in radians
152      */
153     public double getEarthRotationAngle(final AbsoluteDate date) {
154         return MathUtils.normalizeAngle(era.value(date), 0);
155     }
156 
157     /** Replace the instance with a data transfer object for serialization.
158      * <p>
159      * This intermediate class serializes only the frame key.
160      * </p>
161      * @return data transfer object that will be serialized
162      */
163     @DefaultDataContext
164     private Object writeReplace() {
165         return new DataTransferObject(eopHistory);
166     }
167 
168     /** Internal class used only for serialization. */
169     @DefaultDataContext
170     private static class DataTransferObject implements Serializable {
171 
172         /** Serializable UID. */
173         private static final long serialVersionUID = 20131209L;
174 
175         /** EOP history. */
176         private final EOPHistory eopHistory;
177 
178         /** Simple constructor.
179          * @param eopHistory EOP history
180          */
181         DataTransferObject(final EOPHistory eopHistory) {
182             this.eopHistory = eopHistory;
183         }
184 
185         /** Replace the deserialized data transfer object with a {@link TIRFProvider}.
186          * @return replacement {@link TIRFProvider}
187          */
188         private Object readResolve() {
189             try {
190                 // retrieve a managed frame
191                 return new TIRFProvider(eopHistory,
192                         DataContext.getDefault().getTimeScales().getUT1(eopHistory));
193             } catch (OrekitException oe) {
194                 throw new OrekitInternalError(oe);
195             }
196         }
197 
198     }
199 
200 }