FieldCellInterpolator.java

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

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

  21. /** Interpolator within a grid cell.
  22.  * @param <T> type of the field elements
  23.  * @author Luc Maisonobe
  24.  * @since 12.1
  25.  */
  26. public class FieldCellInterpolator<T extends CalculusFieldElement<T>> {

  27.     /** Latitude of point of interest. */
  28.     private final T latitude;

  29.     /** Longitude of point of interest. */
  30.     private final T longitude;

  31.     /** South-West grid entry. */
  32.     private final GridEntry southWest;

  33.     /** South-East grid entry. */
  34.     private final GridEntry southEast;

  35.     /** North-West grid entry. */
  36.     private final GridEntry northWest;

  37.     /** North-East grid entry. */
  38.     private final GridEntry northEast;

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

  57.     /** Interpolate a grid function.
  58.      * @param gridGetter getter for the grid function
  59.      * @return interpolated function"
  60.      */
  61.     T interpolate(final ToDoubleFunction<GridEntry> gridGetter) {

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

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

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

  81.     }

  82. }