DtcDataLoader.java

  1. /* Copyright 2002-2025 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.atmosphere.data;

  18. import java.io.BufferedReader;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.io.InputStreamReader;
  22. import java.io.Serializable;
  23. import java.nio.charset.StandardCharsets;
  24. import java.text.ParseException;
  25. import java.util.NoSuchElementException;
  26. import java.util.SortedSet;
  27. import java.util.TreeSet;
  28. import java.util.regex.Pattern;
  29. import java.util.HashSet;
  30. import java.util.Set;

  31. import org.orekit.data.DataLoader;
  32. import org.orekit.errors.OrekitException;
  33. import org.orekit.errors.OrekitMessages;
  34. import org.orekit.time.AbsoluteDate;
  35. import org.orekit.time.ChronologicalComparator;
  36. import org.orekit.time.TimeScale;
  37. import org.orekit.time.TimeStamped;
  38. import org.orekit.utils.Constants;


  39. /**
  40.  * This class reads solar activity data from DTCFILE files for the class
  41.  * {@link JB2008SpaceEnvironmentData}. The code in this class is based of the
  42.  * CssiSpaceWeatherDataLoader class.
  43.  * The DTCFILE file contain pre-computed data from Space Environment using the Dst indices
  44.  * as well as Ap indices. This computation can be realised using the Fortran code provided
  45.  * by Space Environment Technologies. See <a href="https://sol.spacenvironment.net/JB2008/indices/DTCFILE.TXT">
  46.  * this link</a> for more information.
  47.  * <p>
  48.  * The data is provided by Space Environment Technologies through their website
  49.  * <a href="https://sol.spacenvironment.net/JB2008/indices/DTCFILE.TXT">Link</a>.
  50.  * </p>
  51.  * The work done for this class is based on the CssiSpaceWeatherDataLoader class
  52.  * by ClĂ©ment Jonglez, the JB2008 interface by Pascal Parraud, and corrections for
  53.  * DataLoader implementation by Bryan Cazabonne and Evan Ward .
  54.  *
  55.  * @author Louis Aucouturier
  56.  * @since 11.2
  57.  */
  58. public class DtcDataLoader implements DataLoader {

  59.     /** Container class for Solar activity indexes. */
  60.     public static class LineParameters implements TimeStamped, Serializable {

  61.         /** Serializable UID. */
  62.         private static final long serialVersionUID = 8239275953453087629L;

  63.         /** Entry date. */
  64.         private final AbsoluteDate date;

  65.         /** dTc temperature correction data. */
  66.         private final double dtc;

  67.         /**
  68.          * Constructor.
  69.          * @param date  entry date
  70.          * @param dtc   Temperature correction for geomagnetic storms
  71.          */
  72.         public LineParameters(final AbsoluteDate date, final double dtc) {
  73.             this.date = date;
  74.             this.dtc = dtc;
  75.         }

  76.         @Override
  77.         public AbsoluteDate getDate() {
  78.             return date;
  79.         }

  80.         /**
  81.          * Get the DSTDTC parameter.
  82.          * <p>
  83.          * It represents the temperature correction for geomagnetic storms.
  84.          * </p>
  85.          * @return dTc  Temperature correction for geomagnetic storms
  86.          */
  87.         public double getDSTDTC() {
  88.             return dtc;
  89.         }

  90.     }

  91.     /** Pattern for regular data. */
  92.     private static final Pattern PATTERN_SPACE = Pattern.compile("\\s+");

  93.     /** UTC time scale. */
  94.     private final TimeScale utc;

  95.     /** First available date. */
  96.     private AbsoluteDate firstDate;

  97.     /** Last available date. */
  98.     private AbsoluteDate lastDate;

  99.     /** Data set. */
  100.     private SortedSet<LineParameters> set;

  101.     /**
  102.      * Constructor.
  103.      * @param utc UTC time scale
  104.      */
  105.     public DtcDataLoader(final TimeScale utc) {
  106.         this.utc = utc;
  107.         firstDate = null;
  108.         lastDate = null;
  109.         set = new TreeSet<>(new ChronologicalComparator());
  110.     }

  111.     /**
  112.      * Getter for the data set.
  113.      * @return the data set
  114.      */
  115.     public SortedSet<LineParameters> getDataSet() {
  116.         return set;
  117.     }

  118.     /**
  119.      * Gets the available data range minimum date.
  120.      * @return the minimum date.
  121.      */
  122.     public AbsoluteDate getMinDate() {
  123.         return firstDate;
  124.     }

  125.     /**
  126.      * Gets the available data range maximum date.
  127.      * @return the maximum date.
  128.      */
  129.     public AbsoluteDate getMaxDate() {
  130.         return lastDate;
  131.     }

  132.     /** {@inheritDoc} */
  133.     public void loadData(final InputStream input, final String name)
  134.             throws IOException, ParseException, OrekitException {

  135.         int lineNumber = 0;
  136.         String line = null;
  137.         final int nHours = 24;
  138.         final Set<AbsoluteDate> parsedEpochs = new HashSet<>();

  139.         try (BufferedReader br = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8))) {

  140.             final CommonLineReader reader = new CommonLineReader(br);

  141.             for (line = reader.readLine(); line != null; line = reader.readLine()) {
  142.                 lineNumber++;
  143.                 if (!reader.isEmptyLine()) {
  144.                     /** extract the data from the line
  145.                      * The data is extracted from substrings as the spacing between
  146.                      * columns is constant.
  147.                      */

  148.                     /**
  149.                      * The date is expressed as a year and the day-number in this year.
  150.                      * Then the dTc is expressed in each column at a different hour, with
  151.                      * column 4 being the first hour of  the day and column 28 the last hour
  152.                      * of the day.
  153.                      * Each column is converted to a single LineParameters object.
  154.                      */

  155.                     final String[] splitLine = PATTERN_SPACE.split(line);
  156.                     final int year = Integer.parseInt(splitLine[1]);
  157.                     final int dayYear = Integer.parseInt(splitLine[2]);
  158.                     final AbsoluteDate initDate = new AbsoluteDate(year, 1, 1, utc);
  159.                     final AbsoluteDate currDate = initDate.shiftedBy((dayYear - 1) * Constants.JULIAN_DAY);

  160.                     for (int i = 0; i < nHours; i++) {

  161.                         final AbsoluteDate date = currDate.shiftedBy(3600 * i);

  162.                         if (parsedEpochs.add(date)) { // Checking if entry doesn't exist yet
  163.                             final double dtc = Integer.parseInt(splitLine[3 + i]);
  164.                             set.add(new LineParameters(date, dtc));
  165.                         }
  166.                     }
  167.                 }
  168.             }
  169.         } catch (NumberFormatException nfe) {
  170.             throw new OrekitException(nfe, OrekitMessages.UNABLE_TO_PARSE_LINE_IN_FILE, lineNumber, name, line);
  171.         }

  172.         try {
  173.             firstDate = set.first().getDate();
  174.             lastDate = set.last().getDate();
  175.         } catch (NoSuchElementException nse) {
  176.             throw new OrekitException(nse, OrekitMessages.NO_DATA_IN_FILE, name);
  177.         }
  178.     }

  179.     /** {@inheritDoc} */
  180.     public boolean stillAcceptsData() {
  181.         return true;
  182.     }
  183. }