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.modifiers;
18  
19  import java.util.ArrayList;
20  import java.util.Collections;
21  import java.util.List;
22  
23  import org.orekit.estimation.measurements.EstimatedMeasurement;
24  import org.orekit.estimation.measurements.EstimatedMeasurementBase;
25  import org.orekit.estimation.measurements.EstimationModifier;
26  import org.orekit.estimation.measurements.ObservedMeasurement;
27  import org.orekit.utils.ParameterDriver;
28  import org.orekit.utils.TimeSpanMap.Span;
29  
30  /** Class modeling a measurement bias.
31   * @param <T> the type of the measurement
32   * @author Luc Maisonobe
33   * @since 8.0
34   */
35  public class Bias<T extends ObservedMeasurement<T>> implements EstimationModifier<T> {
36  
37      /** Parameters holding the bias value components. */
38      private final List<ParameterDriver> drivers;
39  
40      /** Partial derivatives. */
41      private final double[][] derivatives;
42  
43      /** Simple constructor.
44       * @param name name of the bias
45       * @param bias reference value of the bias
46       * @param scale scale of the bias, for normalization
47       * @param min minimum value of the bias
48       * @param max maximum value of the bias
49       */
50      public Bias(final String[] name, final double[] bias, final double[] scale,
51                  final double[] min, final double[] max) {
52  
53          drivers = new ArrayList<>(bias.length);
54          for (int i = 0; i < bias.length; ++i) {
55              drivers.add(new ParameterDriver(name[i], bias[i], scale[i], min[i], max[i]));
56          }
57  
58          derivatives = new double[bias.length][bias.length];
59          for (int i = 0; i < bias.length; ++i) {
60              // derivatives are computed with respect to the physical parameters,
61              // not with respect to the normalized parameters (normalization is
62              // performed later on), so the derivative is really 1.0 and not scale[i]
63              derivatives[i][i] = 1.0;
64          }
65  
66      }
67  
68      /** {@inheritDoc}
69       * <p>
70       * For a bias, there are {@link ObservedMeasurement#getDimension()} parameter drivers,
71       * sorted in components order.
72       * </p>
73       */
74      @Override
75      public List<ParameterDriver> getParametersDrivers() {
76          return Collections.unmodifiableList(drivers);
77      }
78  
79      /** {@inheritDoc} */
80      @Override
81      public void modifyWithoutDerivatives(final EstimatedMeasurementBase<T> estimated) {
82  
83          // apply the bias to the measurement value
84          final double[] value = estimated.getEstimatedValue();
85          int nb = 0;
86          for (final ParameterDriver driver : drivers) {
87              for (Span<String> span = driver.getNamesSpanMap().getFirstSpan();
88                   span != null; span = span.next()) {
89                  value[nb++] += driver.getValue(span.getStart());
90              }
91          }
92          estimated.modifyEstimatedValue(this, value);
93  
94      }
95  
96      /** {@inheritDoc} */
97      @Override
98      public void modify(final EstimatedMeasurement<T> estimated) {
99  
100         // apply the bias to the measurement value
101         int nb = 0;
102         for (final ParameterDriver driver : drivers) {
103             for (Span<String> span = driver.getNamesSpanMap().getFirstSpan();
104                  span != null; span = span.next()) {
105                 if (driver.isSelected()) {
106                     // add the partial derivatives
107                     estimated.setParameterDerivatives(driver, span.getStart(),
108                                                       derivatives[nb++]);
109                 }
110             }
111         }
112 
113         modifyWithoutDerivatives(estimated);
114 
115     }
116 
117 }