1   /* Copyright 2002-2024 Thales Alenia Space
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.weather;
18  
19  import org.hipparchus.CalculusFieldElement;
20  import org.hipparchus.util.FastMath;
21  import org.orekit.models.earth.weather.water.WaterVaporPressureProvider;
22  
23  /** Converter for weather parameters that change with height.
24   * <p>
25   * Height variations correspond to equations 5.98, 5.99 and 5.100 from
26   * Guochang Xu, GPS - Theory, Algorithms and Applications, Springer, 2007
27   * </p>
28   * @author Luc Maisonobe
29   * @since 12.1
30   */
31  public class HeightDependentPressureTemperatureHumidityConverter {
32  
33      /** Water pressure provider for water vapor pressure. */
34      private final WaterVaporPressureProvider provider;
35  
36      /** Simple constructor.
37       * <p>
38       * Points outside of altitude range will be silently clipped back to range.
39       * </p>
40       * @param provider provider for water vapor pressure
41       */
42      public HeightDependentPressureTemperatureHumidityConverter(final WaterVaporPressureProvider provider) {
43          this.provider = provider;
44      }
45  
46      /** Convert weather parameters.
47       * @param pth0 weather at reference altitude
48       * @param h altitude at which weather is requested
49       * @return converted weather
50       */
51      public PressureTemperatureHumidity convert(final PressureTemperatureHumidity pth0,
52                                                 final double h) {
53  
54          // retrieve parameters at reference altitude
55          final double rh0 = provider.relativeHumidity(pth0.getPressure(), pth0.getTemperature(), pth0.getWaterVaporPressure());
56  
57          // compute changes due to altitude change
58          final double dh = h - pth0.getAltitude();
59          final double p  = pth0.getPressure() * FastMath.pow(1.0 - 2.26e-5 * dh, 5.225);
60          final double t  = pth0.getTemperature() - 6.5e-3 * dh;
61          final double rh = rh0 * FastMath.exp(-6.396e-4 * dh);
62  
63          return new PressureTemperatureHumidity(h, p, t, provider.waterVaporPressure(p, t, rh),
64                                                 pth0.getTm(), pth0.getLambda());
65  
66      }
67  
68      /** Convert weather parameters.
69       * @param <T> type of the elements
70       * @param pth0 weather at reference altitude
71       * @param h altitude at which weather is requested
72       * @return converted weather
73       */
74      public <T extends CalculusFieldElement<T>> FieldPressureTemperatureHumidity<T> convert(final FieldPressureTemperatureHumidity<T> pth0,
75                                                                                             final T h) {
76          // retrieve parameters at reference altitude
77          final T rh0 = provider.relativeHumidity(pth0.getPressure(), pth0.getTemperature(), pth0.getWaterVaporPressure());
78  
79          // compute changes due to altitude change
80          final T dh = h.subtract(pth0.getAltitude());
81          final T t  = pth0.getTemperature().subtract(dh.multiply(6.5e-3));
82          final T p  = pth0.getPressure().multiply(dh.multiply(2.26e-5).negate().add(1.0).pow(5.225));
83          final T rh = rh0.multiply(FastMath.exp(dh.multiply(-6.396e-4)));
84          return new FieldPressureTemperatureHumidity<>(h, p, t, provider.waterVaporPressure(p, t, rh),
85                                                        pth0.getTm(), pth0.getLambda());
86      }
87  
88  }