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