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