CellInterpolator.java

  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. import java.util.function.ToDoubleFunction;

  19. import org.hipparchus.analysis.interpolation.BilinearInterpolatingFunction;

  20. /** Interpolator within a grid cell.
  21.  * @author Luc Maisonobe
  22.  * @since 12.1
  23.  */
  24. public class CellInterpolator {

  25.     /** Latitude of point of interest. */
  26.     private final double latitude;

  27.     /** Longitude of point of interest. */
  28.     private final double longitude;

  29.     /** South-West grid entry. */
  30.     private final EvaluatedGridEntry southWest;

  31.     /** South-East grid entry. */
  32.     private final EvaluatedGridEntry southEast;

  33.     /** North-West grid entry. */
  34.     private final EvaluatedGridEntry northWest;

  35.     /** North-East grid entry. */
  36.     private final EvaluatedGridEntry northEast;

  37.     /** Simple constructor.
  38.      * @param latitude latitude of point of interest
  39.      * @param longitude longitude of point of interest
  40.      * @param southWest South-West grid entry
  41.      * @param southEast South-East grid entry
  42.      * @param northWest North-West grid entry
  43.      * @param northEast North-East grid entry
  44.      */
  45.     CellInterpolator(final double latitude, final double longitude,
  46.                      final EvaluatedGridEntry southWest, final EvaluatedGridEntry southEast,
  47.                      final EvaluatedGridEntry northWest, final EvaluatedGridEntry northEast) {
  48.         this.latitude  = latitude;
  49.         this.longitude = longitude;
  50.         this.southWest = southWest;
  51.         this.southEast = southEast;
  52.         this.northWest = northWest;
  53.         this.northEast = northEast;
  54.     }

  55.     /** Interpolate a grid function.
  56.      * @param gridGetter getter for the grid function
  57.      * @return interpolated function
  58.      */
  59.     double interpolate(final ToDoubleFunction<EvaluatedGridEntry> gridGetter) {

  60.         // cell surrounding the point
  61.         final double[] xVal = new double[] {
  62.             southWest.getEntry().getLongitude(), southEast.getEntry().getLongitude()
  63.         };
  64.         final double[] yVal = new double[] {
  65.             southWest.getEntry().getLatitude(), northWest.getEntry().getLatitude()
  66.         };

  67.         // evaluate grid points at specified day
  68.         final double[][] fval = new double[][] {
  69.             {
  70.                 gridGetter.applyAsDouble(southWest),
  71.                 gridGetter.applyAsDouble(northWest)
  72.             }, {
  73.                 gridGetter.applyAsDouble(southEast),
  74.                 gridGetter.applyAsDouble(northEast)
  75.             }
  76.         };

  77.         // perform interpolation in the grid
  78.         return new BilinearInterpolatingFunction(xVal, yVal, fval).value(longitude, latitude);

  79.     }

  80. }