Grid.java

  1. /* Copyright 2002-2024 CS GROUP
  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.List;
  19. import java.util.SortedSet;

  20. import org.hipparchus.CalculusFieldElement;
  21. import org.hipparchus.util.FastMath;
  22. import org.hipparchus.util.MathUtils;
  23. import org.orekit.errors.OrekitException;
  24. import org.orekit.errors.OrekitMessages;

  25. /** Container for a complete grid.
  26.  * @author Bryan Cazabonne
  27.  * @author Luc Maisonobe
  28.  * @since 12.1
  29.  */
  30. class Grid {

  31.     /** Latitude sample. */
  32.     private final SortedSet<Integer> latitudeSample;

  33.     /** Longitude sample. */
  34.     private final SortedSet<Integer> longitudeSample;

  35.     /** Grid entries. */
  36.     private final GridEntry[][] entries;

  37.     /** Simple constructor.
  38.      * @param latitudeSample latitude sample
  39.      * @param longitudeSample longitude sample
  40.      * @param loadedEntries loaded entries, organized as a simple list
  41.      * @param name file name
  42.      */
  43.     Grid(final SortedSet<Integer> latitudeSample, final SortedSet<Integer> longitudeSample,
  44.          final List<GridEntry> loadedEntries, final String name) {

  45.         final int nA         = latitudeSample.size();
  46.         final int nO         = longitudeSample.size() + 1; // we add one here for wrapping the grid
  47.         this.entries         = new GridEntry[nA][nO];
  48.         this.latitudeSample  = latitudeSample;
  49.         this.longitudeSample = longitudeSample;

  50.         // organize entries in the regular grid
  51.         for (final GridEntry entry : loadedEntries) {
  52.             final int latitudeIndex  = latitudeSample.headSet(entry.getLatKey() + 1).size() - 1;
  53.             final int longitudeIndex = longitudeSample.headSet(entry.getLonKey() + 1).size() - 1;
  54.             entries[latitudeIndex][longitudeIndex] = entry;
  55.         }

  56.         // finalize the grid
  57.         for (final GridEntry[] row : entries) {

  58.             // check for missing entries
  59.             for (int longitudeIndex = 0; longitudeIndex < nO - 1; ++longitudeIndex) {
  60.                 if (row[longitudeIndex] == null) {
  61.                     throw new OrekitException(OrekitMessages.IRREGULAR_OR_INCOMPLETE_GRID, name);
  62.                 }
  63.             }

  64.             // wrap the grid around the Earth in longitude
  65.             row[nO - 1] = row[0].buildWrappedEntry();

  66.         }

  67.     }

  68.     /** Get index of South entries in the grid.
  69.      * @param latitude latitude to locate (radians)
  70.      * @return index of South entries in the grid
  71.      */
  72.     private int getSouthIndex(final double latitude) {

  73.         final int latKey = (int) FastMath.rint(FastMath.toDegrees(latitude) * GridEntry.DEG_TO_MAS);
  74.         final int index  = latitudeSample.headSet(latKey + 1).size() - 1;

  75.         // make sure we have at least one point remaining on North by clipping to size - 2
  76.         return FastMath.min(index, latitudeSample.size() - 2);

  77.     }

  78.     /** Get index of West entries in the grid.
  79.      * @param longitude longitude to locate (radians)
  80.      * @return index of West entries in the grid
  81.      */
  82.     private int getWestIndex(final double longitude) {

  83.         final int lonKey = (int) FastMath.rint(FastMath.toDegrees(longitude) * GridEntry.DEG_TO_MAS);
  84.         final int index  = longitudeSample.headSet(lonKey + 1).size() - 1;

  85.         // we don't do clipping in longitude because we have added a row to wrap around the Earth
  86.         return index;

  87.     }

  88.     /** Get interpolator within a cell.
  89.      * @param latitude latitude of point of interest
  90.      * @param longitude longitude of point of interest
  91.      * @return interpolator for the cell
  92.      */
  93.     CellInterpolator getInterpolator(final double latitude, final double longitude) {

  94.         // keep longitude within grid range
  95.         final double normalizedLongitude =
  96.                         MathUtils.normalizeAngle(longitude,
  97.                                                  entries[0][0].getLongitude() + FastMath.PI);

  98.         // find neighboring grid entries
  99.         final int southIndex = getSouthIndex(latitude);
  100.         final int westIndex  = getWestIndex(normalizedLongitude);

  101.         // build interpolator
  102.         return new CellInterpolator(latitude, normalizedLongitude,
  103.                                     entries[southIndex    ][westIndex    ],
  104.                                     entries[southIndex    ][westIndex + 1],
  105.                                     entries[southIndex + 1][westIndex    ],
  106.                                     entries[southIndex + 1][westIndex + 1]);

  107.     }

  108.     /** Get interpolator within a cell.
  109.      * @param <T> type of the field elements
  110.      * @param latitude latitude of point of interest
  111.      * @param longitude longitude of point of interest
  112.      * @return interpolator for the cell
  113.      */
  114.     <T extends CalculusFieldElement<T>> FieldCellInterpolator<T> getInterpolator(final T latitude, final T longitude) {

  115.         // keep longitude within grid range
  116.         final T normalizedLongitude =
  117.                         MathUtils.normalizeAngle(longitude,
  118.                                                  longitude.newInstance(entries[0][0].getLongitude() + FastMath.PI));

  119.         // find neighboring grid entries
  120.         final int southIndex = getSouthIndex(latitude.getReal());
  121.         final int westIndex  = getWestIndex(normalizedLongitude.getReal());

  122.         // build interpolator
  123.         return new FieldCellInterpolator<>(latitude, normalizedLongitude,
  124.                                            entries[southIndex    ][westIndex    ],
  125.                                            entries[southIndex    ][westIndex + 1],
  126.                                            entries[southIndex + 1][westIndex    ],
  127.                                            entries[southIndex + 1][westIndex + 1]);

  128.     }

  129.     /** Check if grid contains all specified models.
  130.      * @param types models types
  131.      * @return true if grid contain the model
  132.      */
  133.     boolean hasModels(final SeasonalModelType... types) {
  134.         boolean hasAll = true;
  135.         for (final SeasonalModelType type : types) {
  136.             hasAll &= entries[0][0].getModel(type) != null;
  137.         }
  138.         return hasAll;
  139.     }

  140. }