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.estimation.measurements;
18  
19  import java.util.Arrays;
20  
21  import org.hipparchus.analysis.differentiation.Gradient;
22  import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
23  import org.hipparchus.geometry.euclidean.threed.Vector3D;
24  import org.hipparchus.util.MathUtils;
25  import org.orekit.frames.FieldStaticTransform;
26  import org.orekit.frames.Frame;
27  import org.orekit.frames.StaticTransform;
28  import org.orekit.propagation.SpacecraftState;
29  import org.orekit.time.AbsoluteDate;
30  import org.orekit.utils.ParameterDriver;
31  import org.orekit.utils.TimeSpanMap.Span;
32  import org.orekit.utils.TimeStampedFieldPVCoordinates;
33  import org.orekit.utils.TimeStampedPVCoordinates;
34  
35  /** Class modeling a Right Ascension - Declination measurement from a ground point (station, telescope).
36   * The angles are given in an inertial reference frame.
37   * The motion of the spacecraft during the signal flight time is taken into
38   * account. The date of the measurement corresponds to the reception on
39   * ground of the reflected signal.
40   *
41   * @author Thierry Ceolin
42   * @author Maxime Journot
43   * @since 9.0
44   */
45  public class AngularRaDec extends GroundReceiverMeasurement<AngularRaDec> {
46  
47      /** Type of the measurement. */
48      public static final String MEASUREMENT_TYPE = "AngularRaDec";
49  
50      /** Reference frame in which the right ascension - declination angles are given. */
51      private final Frame referenceFrame;
52  
53      /** Simple constructor.
54       * @param station ground station from which measurement is performed
55       * @param referenceFrame Reference frame in which the right ascension - declination angles are given
56       * @param date date of the measurement
57       * @param angular observed value
58       * @param sigma theoretical standard deviation
59       * @param baseWeight base weight
60       * @param satellite satellite related to this measurement
61       * @since 9.3
62       */
63      public AngularRaDec(final GroundStation station, final Frame referenceFrame, final AbsoluteDate date,
64                          final double[] angular, final double[] sigma, final double[] baseWeight,
65                          final ObservableSatellite satellite) {
66          super(station, false, date, angular, sigma, baseWeight, satellite);
67          this.referenceFrame = referenceFrame;
68      }
69  
70      /** Get the reference frame in which the right ascension - declination angles are given.
71       * @return reference frame in which the right ascension - declination angles are given
72       */
73      public Frame getReferenceFrame() {
74          return referenceFrame;
75      }
76  
77      /** {@inheritDoc} */
78      @Override
79      protected EstimatedMeasurementBase<AngularRaDec> theoreticalEvaluationWithoutDerivatives(final int iteration,
80                                                                                               final int evaluation,
81                                                                                               final SpacecraftState[] states) {
82  
83          final GroundReceiverCommonParametersWithoutDerivatives common = computeCommonParametersWithout(states[0]);
84          final TimeStampedPVCoordinates transitPV = common.getTransitPV();
85  
86          // Station-satellite vector expressed in inertial frame
87          final Vector3D staSatInertial = transitPV.getPosition().subtract(common.getStationDownlink().getPosition());
88  
89          // Field transform from inertial to reference frame at station's reception date
90          final StaticTransform inertialToReferenceDownlink = common.getState().getFrame().
91                                                              getStaticTransformTo(referenceFrame, common.getStationDownlink().getDate());
92  
93          // Station-satellite vector in reference frame
94          final Vector3D staSatReference = inertialToReferenceDownlink.transformVector(staSatInertial);
95  
96          // Compute right ascension and declination
97          final double baseRightAscension = staSatReference.getAlpha();
98          final double twoPiWrap          = MathUtils.normalizeAngle(baseRightAscension, getObservedValue()[0]) - baseRightAscension;
99          final double rightAscension     = baseRightAscension + twoPiWrap;
100         final double declination        = staSatReference.getDelta();
101 
102         // Prepare the estimation
103         final EstimatedMeasurementBase<AngularRaDec> estimated =
104                         new EstimatedMeasurementBase<>(this, iteration, evaluation,
105                                                        new SpacecraftState[] {
106                                                            common.getTransitState()
107                                                        }, new TimeStampedPVCoordinates[] {
108                                                            transitPV,
109                                                            common.getStationDownlink()
110                                                        });
111 
112         // azimuth - elevation values
113         estimated.setEstimatedValue(rightAscension, declination);
114 
115         return estimated;
116 
117     }
118 
119     /** {@inheritDoc} */
120     @Override
121     protected EstimatedMeasurement<AngularRaDec> theoreticalEvaluation(final int iteration, final int evaluation,
122                                                                        final SpacecraftState[] states) {
123 
124         final SpacecraftState state = states[0];
125 
126         // Right Ascension/elevation (in reference frame )derivatives are computed with respect to spacecraft state in inertial frame
127         // and station parameters
128         // ----------------------
129         //
130         // Parameters:
131         //  - 0..2 - Position of the spacecraft in inertial frame
132         //  - 3..5 - Velocity of the spacecraft in inertial frame
133         //  - 6..n - station parameters (clock offset, station offsets, pole, prime meridian...)
134         final GroundReceiverCommonParametersWithDerivatives common = computeCommonParametersWithDerivatives(state);
135         final TimeStampedFieldPVCoordinates<Gradient> transitPV = common.getTransitPV();
136 
137         // Station-satellite vector expressed in inertial frame
138         final FieldVector3D<Gradient> staSatInertial = transitPV.getPosition().subtract(common.getStationDownlink().getPosition());
139 
140         // Field transform from inertial to reference frame at station's reception date
141         final FieldStaticTransform<Gradient> inertialToReferenceDownlink =
142                         state.getFrame().getStaticTransformTo(referenceFrame, common.getStationDownlink().getDate());
143 
144         // Station-satellite vector in reference frame
145         final FieldVector3D<Gradient> staSatReference = inertialToReferenceDownlink.transformVector(staSatInertial);
146 
147         // Compute right ascension and declination
148         final Gradient baseRightAscension = staSatReference.getAlpha();
149         final double   twoPiWrap          = MathUtils.normalizeAngle(baseRightAscension.getReal(),
150                                                                                 getObservedValue()[0]) - baseRightAscension.getReal();
151         final Gradient rightAscension     = baseRightAscension.add(twoPiWrap);
152         final Gradient declination        = staSatReference.getDelta();
153 
154         // Prepare the estimation
155         final EstimatedMeasurement<AngularRaDec> estimated =
156                         new EstimatedMeasurement<>(this, iteration, evaluation,
157                                                    new SpacecraftState[] {
158                                                        common.getTransitState()
159                                                    }, new TimeStampedPVCoordinates[] {
160                                                        transitPV.toTimeStampedPVCoordinates(),
161                                                        common.getStationDownlink().toTimeStampedPVCoordinates()
162                                                    });
163 
164         // azimuth - elevation values
165         estimated.setEstimatedValue(rightAscension.getValue(), declination.getValue());
166 
167         // Partial derivatives of right ascension/declination in reference frame with respect to state
168         final double[] raDerivatives  = rightAscension.getGradient();
169         final double[] decDerivatives = declination.getGradient();
170         estimated.setStateDerivatives(0,
171                                       Arrays.copyOfRange(raDerivatives, 0, 6), Arrays.copyOfRange(decDerivatives, 0, 6));
172 
173         // Partial derivatives with respect to parameters
174         for (final ParameterDriver driver : getParametersDrivers()) {
175             for (Span<String> span = driver.getNamesSpanMap().getFirstSpan(); span != null; span = span.next()) {
176                 final Integer index = common.getIndices().get(span.getData());
177                 if (index != null) {
178                     estimated.setParameterDerivatives(driver, span.getStart(), raDerivatives[index], decDerivatives[index]);
179                 }
180             }
181         }
182 
183         return estimated;
184 
185     }
186 
187     /** Calculate the Line Of Sight of the given measurement.
188      * @param outputFrame output frame of the line of sight vector
189      * @return Vector3D the line of Sight of the measurement
190      * @since 12.0
191      */
192     public Vector3D getObservedLineOfSight(final Frame outputFrame) {
193         return referenceFrame.getStaticTransformTo(outputFrame, getDate())
194             .transformVector(new Vector3D(getObservedValue()[0], getObservedValue()[1]));
195     }
196 }