FieldCellInterpolator.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.Function;

  19. import org.hipparchus.CalculusFieldElement;
  20. import org.hipparchus.Field;
  21. import org.hipparchus.analysis.interpolation.FieldBilinearInterpolatingFunction;
  22. import org.hipparchus.util.MathArrays;

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

  29.     /** Latitude of point of interest. */
  30.     private final T latitude;

  31.     /** Longitude of point of interest. */
  32.     private final T longitude;

  33.     /** South-West grid entry. */
  34.     private final FieldEvaluatedGridEntry<T> southWest;

  35.     /** South-East grid entry. */
  36.     private final FieldEvaluatedGridEntry<T> southEast;

  37.     /** North-West grid entry. */
  38.     private final FieldEvaluatedGridEntry<T> northWest;

  39.     /** North-East grid entry. */
  40.     private final FieldEvaluatedGridEntry<T> northEast;

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

  59.     /** Interpolate a grid function.
  60.      * @param gridGetter getter for the grid function
  61.      * @return interpolated function
  62.      * @since 13.0
  63.      */
  64.     T fieldInterpolate(final Function<FieldEvaluatedGridEntry<T>, T> gridGetter) {

  65.         final Field<T> field = latitude.getField();
  66.         final T        zero  = field.getZero();

  67.         // cell surrounding the point
  68.         final T[] xVal = MathArrays.buildArray(field, 2);
  69.         xVal[0] = zero.newInstance(southWest.getEntry().getLongitude());
  70.         xVal[1] = zero.newInstance(northEast.getEntry().getLongitude());
  71.         final T[] yVal = MathArrays.buildArray(field, 2);
  72.         yVal[0] = zero.newInstance(southWest.getEntry().getLatitude());
  73.         yVal[1] = zero.newInstance(northEast.getEntry().getLatitude());

  74.         // evaluate grid points at specified day
  75.         final T[][] fval = MathArrays.buildArray(field, 2, 2);
  76.         fval[0][0] = gridGetter.apply(southWest);
  77.         fval[0][1] = gridGetter.apply(northWest);
  78.         fval[1][0] = gridGetter.apply(southEast);
  79.         fval[1][1] = gridGetter.apply(northEast);

  80.         // perform interpolation in the grid
  81.         return new FieldBilinearInterpolatingFunction<>(xVal, yVal, fval).value(longitude, latitude);

  82.     }

  83. }