1   /* Copyright 2002-2016 CS Systèmes d'Information
2    * Licensed to CS Systèmes d'Information (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.geometry.euclidean.threed.Rotation;
22  import org.hipparchus.geometry.euclidean.threed.Vector3D;
23  import org.hipparchus.util.FastMath;
24  import org.orekit.errors.OrekitException;
25  import org.orekit.errors.OrekitInternalError;
26  import org.orekit.time.AbsoluteDate;
27  import org.orekit.time.TimeFunction;
28  
29  /** Celestial Intermediate Reference Frame 2000.
30   * <p>This provider includes precession effects according to either the IAU 2006 precession
31   * (also known as Nicole Capitaines's P03 precession theory) and IAU 2000A_R06 nutation
32   * for IERS 2010 conventions or the IAU 2000A precession-nutation model for IERS 2003
33   * conventions. These models replaced the older IAU-76 precession (Lieske) and IAU-80
34   * theory of nutation (Wahr) which were used in the classical equinox-based paradigm.
35   * It <strong>must</strong> be used with the Earth Rotation Angle (REA) defined by
36   * Capitaine's model and <strong>not</strong> IAU-82 sidereal
37   * time which is consistent with the older models only.</p>
38   * <p>Its parent frame is the GCRF frame.
39   */
40  class CIRFProvider implements EOPBasedTransformProvider {
41  
42      /** Serializable UID. */
43      private static final long serialVersionUID = 20130806L;
44  
45      /** Function computing CIP/CIO components. */
46      private final transient TimeFunction<double[]> xysPxy2Function;
47  
48      /** EOP history. */
49      private final EOPHistory eopHistory;
50  
51      /** Simple constructor.
52       * @param eopHistory EOP history
53       * @exception OrekitException if the nutation model data embedded in the
54       * library cannot be read.
55       * @see Frame
56       */
57      CIRFProvider(final EOPHistory eopHistory)
58          throws OrekitException {
59  
60          // load the nutation model
61          xysPxy2Function = eopHistory.getConventions().getXYSpXY2Function();
62  
63          // store correction to the model
64          this.eopHistory = eopHistory;
65  
66      }
67  
68      /** {@inheritDoc} */
69      @Override
70      public EOPHistory getEOPHistory() {
71          return eopHistory;
72      }
73  
74      /** {@inheritDoc} */
75      @Override
76      public CIRFProvider getNonInterpolatingProvider()
77          throws OrekitException {
78          return new CIRFProvider(eopHistory.getNonInterpolatingEOPHistory());
79      }
80  
81      /** Get the transform from GCRF to CIRF2000 at the specified date.
82       * <p>The transform considers the nutation and precession effects from IERS data.</p>
83       * @param date new value of the date
84       * @return transform at the specified date
85       * @exception OrekitException if the nutation model data embedded in the
86       * library cannot be read
87       */
88      public Transform getTransform(final AbsoluteDate date) throws OrekitException {
89  
90          final double[] xys  = xysPxy2Function.value(date);
91          final double[] dxdy = eopHistory.getNonRotatinOriginNutationCorrection(date);
92  
93          // position of the Celestial Intermediate Pole (CIP)
94          final double xCurrent = xys[0] + dxdy[0];
95          final double yCurrent = xys[1] + dxdy[1];
96  
97          // position of the Celestial Intermediate Origin (CIO)
98          final double sCurrent = xys[2] - xCurrent * yCurrent / 2;
99  
100         // set up the bias, precession and nutation rotation
101         final double x2Py2  = xCurrent * xCurrent + yCurrent * yCurrent;
102         final double zP1    = 1 + FastMath.sqrt(1 - x2Py2);
103         final double r      = FastMath.sqrt(x2Py2);
104         final double sPe2   = 0.5 * (sCurrent + FastMath.atan2(yCurrent, xCurrent));
105         final double cos    = FastMath.cos(sPe2);
106         final double sin    = FastMath.sin(sPe2);
107         final double xPr    = xCurrent + r;
108         final double xPrCos = xPr * cos;
109         final double xPrSin = xPr * sin;
110         final double yCos   = yCurrent * cos;
111         final double ySin   = yCurrent * sin;
112         final Rotation bpn  = new Rotation(zP1 * (xPrCos + ySin), -r * (yCos + xPrSin),
113                                            r * (xPrCos - ySin), zP1 * (yCos - xPrSin),
114                                            true);
115 
116         return new Transform(date, bpn, Vector3D.ZERO);
117 
118     }
119 
120     /** Replace the instance with a data transfer object for serialization.
121      * <p>
122      * This intermediate class serializes only the frame key.
123      * </p>
124      * @return data transfer object that will be serialized
125      */
126     private Object writeReplace() {
127         return new DataTransferObject(eopHistory);
128     }
129 
130     /** Internal class used only for serialization. */
131     private static class DataTransferObject implements Serializable {
132 
133         /** Serializable UID. */
134         private static final long serialVersionUID = 20131209L;
135 
136         /** EOP history. */
137         private final EOPHistory eopHistory;
138 
139         /** Simple constructor.
140          * @param eopHistory EOP history
141          */
142         DataTransferObject(final EOPHistory eopHistory) {
143             this.eopHistory = eopHistory;
144         }
145 
146         /** Replace the deserialized data transfer object with a {@link CIRFProvider}.
147          * @return replacement {@link CIRFProvider}
148          */
149         private Object readResolve() {
150             try {
151                 // retrieve a managed frame
152                 return new CIRFProvider(eopHistory);
153             } catch (OrekitException oe) {
154                 throw new OrekitInternalError(oe);
155             }
156         }
157 
158     }
159 
160 }