ModifiedSaastamoinenModel.java

  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. import org.hipparchus.CalculusFieldElement;
  19. import org.hipparchus.Field;
  20. import org.hipparchus.analysis.interpolation.BilinearInterpolatingFunction;
  21. import org.hipparchus.analysis.interpolation.LinearInterpolator;
  22. import org.hipparchus.analysis.polynomials.PolynomialSplineFunction;
  23. import org.hipparchus.util.FastMath;
  24. import org.orekit.annotation.DefaultDataContext;
  25. import org.orekit.bodies.FieldGeodeticPoint;
  26. import org.orekit.bodies.GeodeticPoint;
  27. import org.orekit.data.DataContext;
  28. import org.orekit.data.DataProvidersManager;
  29. import org.orekit.errors.OrekitException;
  30. import org.orekit.errors.OrekitMessages;
  31. import org.orekit.models.earth.weather.ConstantPressureTemperatureHumidityProvider;
  32. import org.orekit.models.earth.weather.FieldPressureTemperatureHumidity;
  33. import org.orekit.models.earth.weather.HeightDependentPressureTemperatureHumidityConverter;
  34. import org.orekit.models.earth.weather.PressureTemperatureHumidity;
  35. import org.orekit.models.earth.weather.PressureTemperatureHumidityProvider;
  36. import org.orekit.models.earth.weather.water.Wang1988;
  37. import org.orekit.time.AbsoluteDate;
  38. import org.orekit.time.FieldAbsoluteDate;
  39. import org.orekit.utils.FieldTrackingCoordinates;
  40. import org.orekit.utils.InterpolationTableLoader;
  41. import org.orekit.utils.ParameterDriver;
  42. import org.orekit.utils.TrackingCoordinates;

  43. import java.util.Collections;
  44. import java.util.List;
  45. import java.util.regex.Pattern;

  46. /** The modified Saastamoinen model. Estimates the path delay imposed to
  47.  * electro-magnetic signals by the troposphere according to the formula:
  48.  * <pre>
  49.  * δ = 2.277e-3 / cos z * (P + (1255 / T + 0.05) * e - B * tan² z) + δR
  50.  * </pre>
  51.  * with the following input data provided to the model:
  52.  * <ul>
  53.  * <li>z: zenith angle</li>
  54.  * <li>P: atmospheric pressure</li>
  55.  * <li>T: temperature</li>
  56.  * <li>e: partial pressure of water vapour</li>
  57.  * <li>B, δR: correction terms</li>
  58.  * </ul>
  59.  * <p>
  60.  * The model supports custom δR correction terms to be read from a
  61.  * configuration file (saastamoinen-correction.txt) via the
  62.  * {@link DataProvidersManager}.
  63.  * </p>
  64.  * @author Thomas Neidhart
  65.  * @see "Guochang Xu, GPS - Theory, Algorithms and Applications, Springer, 2007"
  66.  * @since 12.0
  67.  */
  68. public class ModifiedSaastamoinenModel implements TroposphericModel {

  69.     /** Default file name for δR correction term table. */
  70.     public static final String DELTA_R_FILE_NAME = "^saastamoinen-correction\\.txt$";

  71.     /** Default lowest acceptable elevation angle [rad]. */
  72.     public static final double DEFAULT_LOW_ELEVATION_THRESHOLD = 0.05;

  73.     /** Provider for water pressure. */
  74.     public static final Wang1988 WATER = new Wang1988();

  75.     /** First pattern for δR correction term table. */
  76.     private static final Pattern FIRST_DELTA_R_PATTERN = Pattern.compile("^\\^");

  77.     /** Second pattern for δR correction term table. */
  78.     private static final Pattern SECOND_DELTA_R_PATTERN = Pattern.compile("\\$$");

  79.     /** Base delay coefficient. */
  80.     private static final double L0 = 2.277e-5;

  81.     /** Temperature numerator. */
  82.     private static final double T_NUM = 1255;

  83.     /** Wet offset. */
  84.     private static final double WET_OFFSET = 0.05;

  85.     /** X values for the B function. */
  86.     private static final double[] X_VALUES_FOR_B = {
  87.         0.0, 500.0, 1000.0, 1500.0, 2000.0, 2500.0, 3000.0, 4000.0, 5000.0
  88.     };

  89.     /** Y values for the B function.
  90.      * <p>
  91.      * The values have been scaled up by a factor 100.0 due to conversion from hPa to Pa.
  92.      * </p>
  93.      */
  94.     private static final double[] Y_VALUES_FOR_B = {
  95.         115.6, 107.9, 100.6, 93.8, 87.4, 81.3, 75.7, 65.4, 56.3
  96.     };

  97.     /** Interpolation function for the B correction term. */
  98.     private static final PolynomialSplineFunction B_FUNCTION = new LinearInterpolator().interpolate(X_VALUES_FOR_B, Y_VALUES_FOR_B);

  99.     /** Interpolation function for the delta R correction term. */
  100.     private final BilinearInterpolatingFunction deltaRFunction;

  101.     /** Provider for atmospheric pressure, temperature and humidity at reference altitude. */
  102.     private final PressureTemperatureHumidityProvider pth0Provider;

  103.     /** Height dependent converter for pressure, temperature and humidity. */
  104.     private final HeightDependentPressureTemperatureHumidityConverter converter;

  105.     /** Lowest acceptable elevation angle [rad]. */
  106.     private double lowElevationThreshold;

  107.     /**
  108.      * Create a new Saastamoinen model for the troposphere using the given environmental
  109.      * conditions and table from the reference book.
  110.      *
  111.      * @param pth0Provider provider for atmospheric pressure, temperature and humidity at reference altitude
  112.      * @see #ModifiedSaastamoinenModel(PressureTemperatureHumidityProvider, String, DataProvidersManager)
  113.      */
  114.     public ModifiedSaastamoinenModel(final PressureTemperatureHumidityProvider pth0Provider) {
  115.         this(pth0Provider, defaultDeltaR());
  116.     }

  117.     /** Create a new Saastamoinen model for the troposphere using the given
  118.      * environmental conditions. This constructor uses the {@link DataContext#getDefault()
  119.      * default data context} if {@code deltaRFileName != null}.
  120.      *
  121.      * @param pth0Provider provider for atmospheric pressure, temperature and humidity at reference altitude
  122.      * @param deltaRFileName regular expression for filename containing δR
  123.      * correction term table (typically {@link #DELTA_R_FILE_NAME}), if null
  124.      * default values from the reference book are used
  125.      * @see #ModifiedSaastamoinenModel(PressureTemperatureHumidityProvider, String, DataProvidersManager)
  126.      */
  127.     @DefaultDataContext
  128.     public ModifiedSaastamoinenModel(final PressureTemperatureHumidityProvider pth0Provider,
  129.                                      final String deltaRFileName) {
  130.         this(pth0Provider, deltaRFileName,
  131.              DataContext.getDefault().getDataProvidersManager());
  132.     }

  133.     /** Create a new Saastamoinen model for the troposphere using the given
  134.      * environmental conditions. This constructor allows the user to specify the source of
  135.      * of the δR file.
  136.      *
  137.      * @param pth0Provider provider for atmospheric pressure, temperature and humidity at reference altitude
  138.      * @param deltaRFileName regular expression for filename containing δR
  139.      * correction term table (typically {@link #DELTA_R_FILE_NAME}), if null
  140.      * default values from the reference book are used
  141.      * @param dataProvidersManager provides access to auxiliary data.
  142.      */
  143.     public ModifiedSaastamoinenModel(final PressureTemperatureHumidityProvider pth0Provider,
  144.                                      final String deltaRFileName,
  145.                                      final DataProvidersManager dataProvidersManager) {
  146.         this(pth0Provider,
  147.              deltaRFileName == null ?
  148.                      defaultDeltaR() :
  149.                      loadDeltaR(deltaRFileName, dataProvidersManager));
  150.     }

  151.     /** Create a new Saastamoinen model.
  152.      *
  153.      * @param pth0Provider provider for atmospheric pressure, temperature and humidity at reference altitude
  154.      * @param deltaR δR correction term function
  155.      */
  156.     private ModifiedSaastamoinenModel(final PressureTemperatureHumidityProvider pth0Provider,
  157.                                       final BilinearInterpolatingFunction deltaR) {
  158.         this.pth0Provider          = pth0Provider;
  159.         this.converter             = new HeightDependentPressureTemperatureHumidityConverter(WATER);
  160.         this.deltaRFunction        = deltaR;
  161.         this.lowElevationThreshold = DEFAULT_LOW_ELEVATION_THRESHOLD;
  162.     }

  163.     /** Create a new Saastamoinen model using a standard atmosphere model.
  164.      *
  165.      * <ul>
  166.      * <li>altitude: 0m</li>
  167.      * <li>temperature: 18 degree Celsius</li>
  168.      * <li>pressure: 1013.25 mbar</li>
  169.      * <li>humidity: 50%</li>
  170.      * <li>@link {@link Wang1988 Wang 1988} model to compute water vapor pressure</li>
  171.      * </ul>
  172.      *
  173.      * @return a Saastamoinen model with standard environmental values
  174.      */
  175.     public static ModifiedSaastamoinenModel getStandardModel() {
  176.         final double altitude    = 0;
  177.         final double pressure    = TroposphericModelUtils.HECTO_PASCAL.toSI(1013.25);
  178.         final double temperature = 273.15 + 18;
  179.         final double humidity    = 0.5;
  180.         final PressureTemperatureHumidity pth = new PressureTemperatureHumidity(altitude,
  181.                                                                                 pressure,
  182.                                                                                 temperature,
  183.                                                                                 WATER.waterVaporPressure(pressure,
  184.                                                                                                          temperature,
  185.                                                                                                          humidity),
  186.                                                                                 Double.NaN,
  187.                                                                                 Double.NaN);
  188.         final PressureTemperatureHumidityProvider pth0Provider = new ConstantPressureTemperatureHumidityProvider(pth);
  189.         return new ModifiedSaastamoinenModel(pth0Provider);
  190.     }

  191.     /** Get provider for atmospheric pressure, temperature and humidity at reference altitude.
  192.      * @return provider for atmospheric pressure, temperature and humidity at reference altitude
  193.      */
  194.     public PressureTemperatureHumidityProvider getPth0Provider() {
  195.         return pth0Provider;
  196.     }

  197.     /** {@inheritDoc}
  198.      * <p>
  199.      * The Saastamoinen model is not defined for altitudes below 0.0. for continuity
  200.      * reasons, we use the value for h = 0 when altitude is negative.
  201.      * </p>
  202.      * <p>
  203.      * There are also numerical issues for elevation angles close to zero. For continuity reasons,
  204.      * elevations lower than a threshold will use the value obtained
  205.      * for the threshold itself.
  206.      * </p>
  207.      * @see #getLowElevationThreshold()
  208.      * @see #setLowElevationThreshold(double)
  209.      */
  210.     @Override
  211.     public TroposphericDelay pathDelay(final TrackingCoordinates trackingCoordinates,
  212.                                        final GeodeticPoint point,
  213.                                        final double[] parameters, final AbsoluteDate date) {

  214.         // limit the height to model range
  215.         final double fixedHeight = FastMath.min(FastMath.max(point.getAltitude(), X_VALUES_FOR_B[0]),
  216.                                                 X_VALUES_FOR_B[X_VALUES_FOR_B.length - 1]);

  217.         final PressureTemperatureHumidity pth0 = pth0Provider.getWeatherParameters(point, date);
  218.         final PressureTemperatureHumidity pth  = converter.convert(pth0, fixedHeight);

  219.         // interpolate the b correction term
  220.         final double B = B_FUNCTION.value(fixedHeight);

  221.         // calculate the zenith angle from the elevation
  222.         final double z = FastMath.abs(0.5 * FastMath.PI -
  223.                                       FastMath.max(trackingCoordinates.getElevation(), lowElevationThreshold));

  224.         // get correction factor
  225.         final double deltaR = getDeltaR(fixedHeight, z);

  226.         // calculate the path delay in m
  227.         // beware since version 12.1 pressures are in Pa and not in hPa, hence the scaling has changed
  228.         final double invCos = 1.0 / FastMath.cos(z);
  229.         final double tan    = FastMath.tan(z);
  230.         final double zh     = L0 * pth.getPressure();
  231.         final double zw     = L0 * (T_NUM / pth.getTemperature() + WET_OFFSET) * pth.getWaterVaporPressure();
  232.         final double sh     = zh * invCos;
  233.         final double sw     = (zw - L0 * B * tan * tan) * invCos + deltaR;
  234.         return new TroposphericDelay(zh, zw, sh, sw);

  235.     }

  236.     /** {@inheritDoc}
  237.      * <p>
  238.      * The Saastamoinen model is not defined for altitudes below 0.0. for continuity
  239.      * reasons, we use the value for h = 0 when altitude is negative.
  240.      * </p>
  241.      * <p>
  242.      * There are also numerical issues for elevation angles close to zero. For continuity reasons,
  243.      * elevations lower than a threshold will use the value obtained
  244.      * for the threshold itself.
  245.      * </p>
  246.      * @see #getLowElevationThreshold()
  247.      * @see #setLowElevationThreshold(double)
  248.      */
  249.     @Override
  250.     public <T extends CalculusFieldElement<T>> FieldTroposphericDelay<T> pathDelay(final FieldTrackingCoordinates<T> trackingCoordinates,
  251.                                                                                    final FieldGeodeticPoint<T> point,
  252.                                                                                    final T[] parameters, final FieldAbsoluteDate<T> date) {

  253.         // limit the height to model range
  254.         final T fixedHeight = FastMath.min(FastMath.max(point.getAltitude(), X_VALUES_FOR_B[0]),
  255.                                            X_VALUES_FOR_B[X_VALUES_FOR_B.length - 1]);

  256.         final FieldPressureTemperatureHumidity<T> pth0 = pth0Provider.getWeatherParameters(point, date);
  257.         final FieldPressureTemperatureHumidity<T> pth  = converter.convert(pth0, fixedHeight);

  258.         final Field<T> field = date.getField();
  259.         final T zero = field.getZero();

  260.         // interpolate the b correction term
  261.         final T B = B_FUNCTION.value(fixedHeight);

  262.         // calculate the zenith angle from the elevation
  263.         final T z = FastMath.abs(FastMath.max(trackingCoordinates.getElevation(),
  264.                                               zero.newInstance(lowElevationThreshold)).negate().
  265.                                  add(zero.getPi().multiply(0.5)));

  266.         // get correction factor
  267.         final T deltaR = getDeltaR(fixedHeight, z, field);

  268.         // calculate the path delay in m
  269.         // beware since version 12.1 pressures are in Pa and not in hPa, hence the scaling has changed
  270.         final T invCos = FastMath.cos(z).reciprocal();
  271.         final T tan    = FastMath.tan(z);
  272.         final T zh     = pth.getPressure().multiply(L0);
  273.         final T zw     = pth.getTemperature().reciprocal().multiply(T_NUM).add(WET_OFFSET).
  274.                          multiply(pth.getWaterVaporPressure()).multiply(L0);
  275.         final T sh     = zh.multiply(invCos);
  276.         final T sw     = zw.subtract(B.multiply(tan).multiply(tan).multiply(L0)).multiply(invCos).add(deltaR);
  277.         return new FieldTroposphericDelay<>(zh, zw, sh, sw);

  278.     }

  279.     /** Calculates the delta R correction term using linear interpolation.
  280.      * @param height the height of the station in m
  281.      * @param zenith the zenith angle of the satellite
  282.      * @return the delta R correction term in m
  283.      */
  284.     private double getDeltaR(final double height, final double zenith) {
  285.         // limit the height to a range of [0, 5000] m
  286.         final double h = FastMath.min(FastMath.max(0, height), 5000);
  287.         // limit the zenith angle to 90 degree
  288.         // Note: the function is symmetric for negative zenith angles
  289.         final double z = FastMath.min(Math.abs(zenith), 0.5 * FastMath.PI);
  290.         return deltaRFunction.value(h, z);
  291.     }

  292.     /** Calculates the delta R correction term using linear interpolation.
  293.      * @param <T> type of the elements
  294.      * @param height the height of the station in m
  295.      * @param zenith the zenith angle of the satellite
  296.      * @param field field used by default
  297.      * @return the delta R correction term in m
  298.      */
  299.     private  <T extends CalculusFieldElement<T>> T getDeltaR(final T height, final T zenith,
  300.                                                          final Field<T> field) {
  301.         final T zero = field.getZero();
  302.         // limit the height to a range of [0, 5000] m
  303.         final T h = FastMath.min(FastMath.max(zero, height), zero.add(5000));
  304.         // limit the zenith angle to 90 degree
  305.         // Note: the function is symmetric for negative zenith angles
  306.         final T z = FastMath.min(zenith.abs(), zero.getPi().multiply(0.5));
  307.         return deltaRFunction.value(h, z);
  308.     }

  309.     /** Load δR function.
  310.      * @param deltaRFileName regular expression for filename containing δR
  311.      * correction term table
  312.      * @param dataProvidersManager provides access to auxiliary data.
  313.      * @return δR function
  314.      */
  315.     private static BilinearInterpolatingFunction loadDeltaR(
  316.             final String deltaRFileName,
  317.             final DataProvidersManager dataProvidersManager) {

  318.         // read the δR interpolation function from the config file
  319.         final InterpolationTableLoader loader = new InterpolationTableLoader();
  320.         dataProvidersManager.feed(deltaRFileName, loader);
  321.         if (!loader.stillAcceptsData()) {
  322.             final double[] elevations = loader.getOrdinateGrid();
  323.             for (int i = 0; i < elevations.length; ++i) {
  324.                 elevations[i] = FastMath.toRadians(elevations[i]);
  325.             }
  326.             return new BilinearInterpolatingFunction(loader.getAbscissaGrid(), elevations,
  327.                                                      loader.getValuesSamples());
  328.         }
  329.         throw new OrekitException(OrekitMessages.UNABLE_TO_FIND_FILE,
  330.                                   SECOND_DELTA_R_PATTERN.
  331.                                   matcher(FIRST_DELTA_R_PATTERN.matcher(deltaRFileName).replaceAll("")).
  332.                                   replaceAll(""));
  333.     }

  334.     /** Create the default δR function.
  335.      * @return δR function
  336.      */
  337.     private static BilinearInterpolatingFunction defaultDeltaR() {

  338.         // the correction table in the referenced book only contains values for an angle of 60 - 80
  339.         // degree, thus for 0 degree, the correction term is assumed to be 0, for degrees > 80 it
  340.         // is assumed to be the same value as for 80.

  341.         // the height in m
  342.         final double[] xValForR = {
  343.             0, 500, 1000, 1500, 2000, 3000, 4000, 5000
  344.         };

  345.         // the zenith angle
  346.         final double[] yValForR = {
  347.             FastMath.toRadians( 0.00), FastMath.toRadians(60.00), FastMath.toRadians(66.00), FastMath.toRadians(70.00),
  348.             FastMath.toRadians(73.00), FastMath.toRadians(75.00), FastMath.toRadians(76.00), FastMath.toRadians(77.00),
  349.             FastMath.toRadians(78.00), FastMath.toRadians(78.50), FastMath.toRadians(79.00), FastMath.toRadians(79.50),
  350.             FastMath.toRadians(79.75), FastMath.toRadians(80.00), FastMath.toRadians(90.00)
  351.         };

  352.         final double[][] fval = new double[][] {
  353.             {
  354.                 0.000, 0.003, 0.006, 0.012, 0.020, 0.031, 0.039, 0.050, 0.065, 0.075, 0.087, 0.102, 0.111, 0.121, 0.121
  355.             }, {
  356.                 0.000, 0.003, 0.006, 0.011, 0.018, 0.028, 0.035, 0.045, 0.059, 0.068, 0.079, 0.093, 0.101, 0.110, 0.110
  357.             }, {
  358.                 0.000, 0.002, 0.005, 0.010, 0.017, 0.025, 0.032, 0.041, 0.054, 0.062, 0.072, 0.085, 0.092, 0.100, 0.100
  359.             }, {
  360.                 0.000, 0.002, 0.005, 0.009, 0.015, 0.023, 0.029, 0.037, 0.049, 0.056, 0.065, 0.077, 0.083, 0.091, 0.091
  361.             }, {
  362.                 0.000, 0.002, 0.004, 0.008, 0.013, 0.021, 0.026, 0.033, 0.044, 0.051, 0.059, 0.070, 0.076, 0.083, 0.083
  363.             }, {
  364.                 0.000, 0.002, 0.003, 0.006, 0.011, 0.017, 0.021, 0.027, 0.036, 0.042, 0.049, 0.058, 0.063, 0.068, 0.068
  365.             }, {
  366.                 0.000, 0.001, 0.003, 0.005, 0.009, 0.014, 0.017, 0.022, 0.030, 0.034, 0.040, 0.047, 0.052, 0.056, 0.056
  367.             }, {
  368.                 0.000, 0.001, 0.002, 0.004, 0.007, 0.011, 0.014, 0.018, 0.024, 0.028, 0.033, 0.039, 0.043, 0.047, 0.047
  369.             }
  370.         };

  371.         // the actual delta R is interpolated using a a bilinear interpolator
  372.         return new BilinearInterpolatingFunction(xValForR, yValForR, fval);

  373.     }

  374.     /** {@inheritDoc} */
  375.     @Override
  376.     public List<ParameterDriver> getParametersDrivers() {
  377.         return Collections.emptyList();
  378.     }

  379.     /** Get the low elevation threshold value for path delay computation.
  380.      * @return low elevation threshold, in rad.
  381.      * @see #pathDelay(TrackingCoordinates, GeodeticPoint, double[], AbsoluteDate)
  382.      * @see #pathDelay(FieldTrackingCoordinates, FieldGeodeticPoint, CalculusFieldElement[], FieldAbsoluteDate)
  383.      * @since 10.2
  384.      */
  385.     public double getLowElevationThreshold() {
  386.         return lowElevationThreshold;
  387.     }

  388.     /** Set the low elevation threshold value for path delay computation.
  389.      * @param lowElevationThreshold The new value for the threshold [rad]
  390.      * @see #pathDelay(TrackingCoordinates, GeodeticPoint, double[], AbsoluteDate)
  391.      * @see #pathDelay(FieldTrackingCoordinates, FieldGeodeticPoint, CalculusFieldElement[], FieldAbsoluteDate)
  392.      * @since 10.2
  393.      */
  394.     public void setLowElevationThreshold(final double lowElevationThreshold) {
  395.         this.lowElevationThreshold = lowElevationThreshold;
  396.     }
  397. }