1   /* Copyright 2002-2024 Thales Alenia Space
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.models.earth.weather;
18  
19  import java.util.function.ToDoubleFunction;
20  
21  import org.hipparchus.CalculusFieldElement;
22  import org.hipparchus.analysis.interpolation.BilinearInterpolatingFunction;
23  
24  /** Interpolator within a grid cell.
25   * @param <T> type of the field elements
26   * @author Luc Maisonobe
27   * @since 12.1
28   */
29  public class FieldCellInterpolator<T extends CalculusFieldElement<T>> {
30  
31      /** Latitude of point of interest. */
32      private final T latitude;
33  
34      /** Longitude of point of interest. */
35      private final T longitude;
36  
37      /** South-West grid entry. */
38      private final GridEntry southWest;
39  
40      /** South-East grid entry. */
41      private final GridEntry southEast;
42  
43      /** North-West grid entry. */
44      private final GridEntry northWest;
45  
46      /** North-East grid entry. */
47      private final GridEntry northEast;
48  
49      /** Simple constructor.
50       * @param latitude latitude of point of interest
51       * @param longitude longitude of point of interest
52       * @param southWest South-West grid entry
53       * @param southEast South-East grid entry
54       * @param northWest North-West grid entry
55       * @param northEast North-East grid entry
56       */
57      FieldCellInterpolator(final T latitude, final T longitude,
58                            final GridEntry southWest, final GridEntry southEast,
59                            final GridEntry northWest, final GridEntry northEast) {
60          this.latitude  = latitude;
61          this.longitude = longitude;
62          this.southWest = southWest;
63          this.southEast = southEast;
64          this.northWest = northWest;
65          this.northEast = northEast;
66      }
67  
68      /** Interpolate a grid function.
69       * @param gridGetter getter for the grid function
70       * @return interpolated function"
71       */
72      T interpolate(final ToDoubleFunction<GridEntry> gridGetter) {
73  
74          // cell surrounding the point
75          final double[] xVal = new double[] {
76              southWest.getLongitude(), southEast.getLongitude()
77          };
78          final double[] yVal = new double[] {
79              southWest.getLatitude(), northWest.getLatitude()
80          };
81  
82          // evaluate grid points at specified day
83          final double[][] fval = new double[][] {
84              {
85                  gridGetter.applyAsDouble(southWest),
86                  gridGetter.applyAsDouble(northWest)
87              }, {
88                  gridGetter.applyAsDouble(southEast),
89                  gridGetter.applyAsDouble(northEast)
90              }
91          };
92  
93          // perform interpolation in the grid
94          return new BilinearInterpolatingFunction(xVal, yVal, fval).value(longitude, latitude);
95  
96      }
97  
98  }