1   /* Copyright 2011-2012 Space Applications Services
2    * Licensed to CS Communication & Systèmes (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.models.earth.troposphere;
18  
19  import java.util.Collections;
20  import java.util.List;
21  
22  import org.hipparchus.CalculusFieldElement;
23  import org.hipparchus.analysis.interpolation.PiecewiseBicubicSplineInterpolatingFunction;
24  import org.hipparchus.analysis.interpolation.PiecewiseBicubicSplineInterpolator;
25  import org.hipparchus.util.FastMath;
26  import org.hipparchus.util.MathUtils;
27  import org.orekit.annotation.DefaultDataContext;
28  import org.orekit.bodies.FieldGeodeticPoint;
29  import org.orekit.bodies.GeodeticPoint;
30  import org.orekit.data.DataContext;
31  import org.orekit.data.DataProvidersManager;
32  import org.orekit.errors.OrekitException;
33  import org.orekit.errors.OrekitMessages;
34  import org.orekit.time.AbsoluteDate;
35  import org.orekit.time.FieldAbsoluteDate;
36  import org.orekit.utils.FieldTrackingCoordinates;
37  import org.orekit.utils.InterpolationTableLoader;
38  import org.orekit.utils.ParameterDriver;
39  import org.orekit.utils.TrackingCoordinates;
40  
41  /** A static tropospheric model that interpolates the actual tropospheric delay
42   * based on values read from a configuration file (tropospheric-delay.txt) via
43   * the {@link DataProvidersManager}.
44   * @author Thomas Neidhart
45   */
46  public class FixedTroposphericDelay implements TroposphericModel {
47  
48      /** Singleton object for the default model. */
49      private static FixedTroposphericDelay defaultModel;
50  
51      /** Abscissa grid for the bi-variate interpolation function read from the file. */
52      private final double[] xArr;
53  
54      /** Ordinate grid for the bi-variate interpolation function read from the file. */
55      private final double[] yArr;
56  
57      /** Values samples for the bi-variate interpolation function read from the file. */
58      private final double[][] fArr;
59  
60      /** Interpolation function for the tropospheric delays. */
61      private final PiecewiseBicubicSplineInterpolatingFunction delayFunction;
62  
63      /** Creates a new {@link FixedTroposphericDelay} instance.
64       * @param xArr abscissa grid for the interpolation function
65       * @param yArr ordinate grid for the interpolation function
66       * @param fArr values samples for the interpolation function
67       */
68      public FixedTroposphericDelay(final double[] xArr, final double[] yArr, final double[][] fArr) {
69          this.xArr = xArr.clone();
70          this.yArr = yArr.clone();
71          this.fArr = fArr.clone();
72          delayFunction = new PiecewiseBicubicSplineInterpolator().interpolate(xArr, yArr, fArr);
73      }
74  
75      /** Creates a new {@link FixedTroposphericDelay} instance, and loads the
76       * delay values from the given resource via the {@link DataContext#getDefault()
77       * default data context}.
78       *
79       * @param supportedName a regular expression for supported resource names
80       * @see #FixedTroposphericDelay(String, DataProvidersManager)
81       */
82      @DefaultDataContext
83      public FixedTroposphericDelay(final String supportedName) {
84          this(supportedName, DataContext.getDefault().getDataProvidersManager());
85      }
86  
87      /**
88       * Creates a new {@link FixedTroposphericDelay} instance, and loads the delay values
89       * from the given resource via the specified data manager.
90       *
91       * @param supportedName a regular expression for supported resource names
92       * @param dataProvidersManager provides access to auxiliary data.
93       * @since 10.1
94       */
95      public FixedTroposphericDelay(final String supportedName,
96                                    final DataProvidersManager dataProvidersManager) {
97  
98          final InterpolationTableLoader loader = new InterpolationTableLoader();
99          dataProvidersManager.feed(supportedName, loader);
100 
101         if (!loader.stillAcceptsData()) {
102             xArr = loader.getAbscissaGrid();
103             yArr = loader.getOrdinateGrid();
104             for (int i = 0; i < yArr.length; ++i) {
105                 yArr[i] = FastMath.toRadians(yArr[i]);
106             }
107             fArr = loader.getValuesSamples();
108             delayFunction = new PiecewiseBicubicSplineInterpolator().interpolate(xArr, yArr, fArr);
109         } else {
110             throw new OrekitException(OrekitMessages.UNABLE_TO_FIND_RESOURCE, supportedName);
111         }
112     }
113 
114     /** Returns the default model, loading delay values from the file
115      * "tropospheric-delay.txt" via the {@link DataContext#getDefault() default data
116      * context}.
117      *
118      * <p>This method uses the {@link DataContext#getDefault() default data context}.
119      *
120      * @return the default model
121      */
122     @DefaultDataContext
123     public static FixedTroposphericDelay getDefaultModel() {
124         synchronized (FixedTroposphericDelay.class) {
125             if (defaultModel == null) {
126                 defaultModel = new FixedTroposphericDelay("^tropospheric-delay\\.txt$");
127             }
128         }
129         return defaultModel;
130     }
131 
132     /** {@inheritDoc}
133      * <p>
134      * All delays are affected to {@link TroposphericDelay#getZh() hydrostatic zenith}
135      * and {@link TroposphericDelay#getSh() hydrostatic slanted} delays, the wet delays
136      * are arbitrarily set to 0.
137      * </p>
138      */
139     @Override
140     public TroposphericDelay pathDelay(final TrackingCoordinates trackingCoordinates, final GeodeticPoint point,
141                                        final double[] parameters, final AbsoluteDate date) {
142         // limit the height to 5000 m
143         final double h = FastMath.min(FastMath.max(0, point.getAltitude()), 5000);
144         // limit the elevation to 0 - π
145         final double ele = FastMath.min(FastMath.PI, FastMath.max(0d, trackingCoordinates.getElevation()));
146         // mirror elevation at the right angle of π/2
147         final double e = ele > 0.5 * FastMath.PI ? FastMath.PI - ele : ele;
148 
149         return new TroposphericDelay(delayFunction.value(h, MathUtils.SEMI_PI), 0.0,
150                                      delayFunction.value(h, e), 0.0);
151     }
152 
153     /** {@inheritDoc}
154      * <p>
155      * All delays are affected to {@link FieldTroposphericDelay#getZh() hydrostatic zenith}
156      * and {@link FieldTroposphericDelay#getSh() hydrostatic slanted} delays, the wet delays
157      * are arbitrarily set to 0.
158      * </p>
159      */
160     @Override
161     public <T extends CalculusFieldElement<T>> FieldTroposphericDelay<T> pathDelay(final FieldTrackingCoordinates<T> trackingCoordinates,
162                                                                                    final FieldGeodeticPoint<T> point,
163                                                                                    final T[] parameters, final FieldAbsoluteDate<T> date) {
164         final T zero = date.getField().getZero();
165         final T pi   = zero.getPi();
166         // limit the height to 5000 m
167         final T h = FastMath.min(FastMath.max(zero, point.getAltitude()), zero.newInstance(5000));
168         // limit the elevation to 0 - π
169         final T ele = FastMath.min(pi, FastMath.max(zero, trackingCoordinates.getElevation()));
170         // mirror elevation at the right angle of π/2
171         final T e = ele.getReal() > pi.multiply(0.5).getReal() ? ele.negate().add(pi) : ele;
172 
173         return new FieldTroposphericDelay<>(delayFunction.value(h, date.getField().getZero().newInstance(MathUtils.SEMI_PI)),
174                                             date.getField().getZero(),
175                                             delayFunction.value(h, e),
176                                             date.getField().getZero());
177 
178     }
179 
180     /** {@inheritDoc} */
181     @Override
182     public List<ParameterDriver> getParametersDrivers() {
183         return Collections.emptyList();
184     }
185 
186 }