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.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.getNonInterpolatingEOPHistory(), 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     /** Get the Earth Rotation Angle at the current date.
137      * @param  date the date
138      * @return Earth Rotation Angle at the current date in radians
139      */
140     public double getEarthRotationAngle(final AbsoluteDate date) {
141         return MathUtils.normalizeAngle(era.value(date), 0);
142     }
143 
144     /** Replace the instance with a data transfer object for serialization.
145      * <p>
146      * This intermediate class serializes only the frame key.
147      * </p>
148      * @return data transfer object that will be serialized
149      */
150     @DefaultDataContext
151     private Object writeReplace() {
152         return new DataTransferObject(eopHistory);
153     }
154 
155     /** Internal class used only for serialization. */
156     @DefaultDataContext
157     private static class DataTransferObject implements Serializable {
158 
159         /** Serializable UID. */
160         private static final long serialVersionUID = 20131209L;
161 
162         /** EOP history. */
163         private final EOPHistory eopHistory;
164 
165         /** Simple constructor.
166          * @param eopHistory EOP history
167          */
168         DataTransferObject(final EOPHistory eopHistory) {
169             this.eopHistory = eopHistory;
170         }
171 
172         /** Replace the deserialized data transfer object with a {@link TIRFProvider}.
173          * @return replacement {@link TIRFProvider}
174          */
175         private Object readResolve() {
176             try {
177                 // retrieve a managed frame
178                 return new TIRFProvider(eopHistory,
179                         DataContext.getDefault().getTimeScales().getUT1(eopHistory));
180             } catch (OrekitException oe) {
181                 throw new OrekitInternalError(oe);
182             }
183         }
184 
185     }
186 
187 }