SOLFSMYDataLoader.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 SOLFSMY files for the class
  41.  * {@link JB2008SpaceEnvironmentData}. The code in this class is based of the
  42.  * CssiSpaceWeatherDataLoader.
  43.  * <p>
  44.  * The data is provided by Space Environment Technologies through their website
  45.  * <a href="https://sol.spacenvironment.net/JB2008/indices/SOLFSMY.TXT">Link</a>.
  46.  * </p>
  47.  * The work done for this class is based on the CssiWpaceWeatherDataLoader class
  48.  * by Clément Jonglez, the JB2008 interface by Pascal Parraud, and corrections for
  49.  * DataLoader implementation by Bryan Cazabonne and Evan Ward .
  50.  *
  51.  * @author Louis Aucouturier
  52.  * @since 11.2
  53.  */
  54. public class SOLFSMYDataLoader implements DataLoader {

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

  57.         /** Serializable UID. */
  58.         private static final long serialVersionUID = -9008818050532123587L;

  59.         /** Entry date. */
  60.         private final AbsoluteDate date;

  61.         /** 10.7-cm Solar flux (1e<sup>-22</sup>*Watt/(m²*Hertz))<br>
  62.          * (Tabular time 1.0 day earlier). */
  63.         private final double f10;

  64.         /** 10.7-cm Solar Flux, averaged 81-day centered on the input time.  */
  65.         private final double f10b;

  66.         /** EUV index (26-34 nm) scaled to F10. */
  67.         private final double s10;

  68.         /** UV 81-day averaged centered index. */
  69.         private final double s10b;

  70.         /** MG2 index scaled to F10. */
  71.         private final double xm10;

  72.         /** MG2 81-day average centered index. */
  73.         private final double xm10b;

  74.         /** Solar X-Ray &amp; Lya index scaled to F10. */
  75.         private final double y10;

  76.         /** Solar X-Ray &amp; Lya 81-day average centered index. */
  77.         private final double y10b;

  78.         /**
  79.          * Constructor.
  80.          * @param date  entry date
  81.          * @param f10   10.7-cm Solar Radio Flux (F10.7)
  82.          * @param f10b  10.7-cm Solar Flux, averaged 81-day centered on the input time
  83.          * @param s10   EUV index (26-34 nm) scaled to F10
  84.          * @param s10b  UV 81-day averaged centered index
  85.          * @param xm10  MG2 index scaled to F10
  86.          * @param xm10b MG2 81-day average centered index
  87.          * @param y10   Solar X-Ray &amp; Lya index scaled to F10
  88.          * @param y10b  Solar X-Ray &amp; Lya 81-day average centered index
  89.          */
  90.         public LineParameters(final AbsoluteDate date, final double f10, final double f10b, final double s10,
  91.                 final double s10b, final double xm10, final double xm10b, final double y10, final double y10b) {
  92.             this.date = date;
  93.             this.f10 = f10;
  94.             this.f10b = f10b;
  95.             this.s10 = s10;
  96.             this.s10b = s10b;
  97.             this.xm10 = xm10;
  98.             this.xm10b = xm10b;
  99.             this.y10 = y10;
  100.             this.y10b = y10b;
  101.         }

  102.         @Override
  103.         public AbsoluteDate getDate() {
  104.             return date;
  105.         }

  106.         // The getters does not take into account the lag

  107.         /** Get the value of the instantaneous solar flux index
  108.          *  (1e<sup>-22</sup>*Watt/(m²*Hertz)).
  109.          * <p>Tabular time 1.0 day earlier.</p>
  110.          * @return the instantaneous F10.7 index
  111.          */
  112.         public double getF10() {
  113.             return f10;
  114.         }

  115.         /** Get the value of the mean solar flux.
  116.          * Averaged 81-day centered F10.7 B index on the input time.
  117.          * <p>Tabular time 1.0 day earlier.</p>
  118.          * @return the mean solar flux F10.7B index
  119.          */
  120.         public double getF10B() {
  121.             return f10b;
  122.         }

  123.         /** Get the EUV index (26-34 nm) scaled to F10.
  124.          * <p>Tabular time 1.0 day earlier.</p>
  125.          * @return the the EUV S10 index
  126.          */
  127.         public double getS10() {
  128.             return s10;
  129.         }

  130.         /** Get the EUV 81-day averaged centered index.
  131.          * <p>Tabular time 1.0 day earlier.</p>
  132.          * @return the the mean EUV S10B index
  133.          */
  134.         public double getS10B() {
  135.             return s10b;
  136.         }

  137.         /** Get the MG2 index scaled to F10.
  138.          * <p>Tabular time 2.0 days earlier.</p>
  139.          * @return the the MG2 index
  140.          */
  141.         public double getXM10() {
  142.             return xm10;
  143.         }

  144.         /** Get the MG2 81-day average centered index.
  145.          * <p>Tabular time 2.0 days earlier.</p>
  146.          * @return the the mean MG2 index
  147.          */
  148.         public double getXM10B() {
  149.             return xm10b;
  150.         }

  151.         /** Get the Solar X-Ray &amp; Lya index scaled to F10.
  152.          * <p>Tabular time 5.0 days earlier.</p>
  153.          * @return the Solar X-Ray &amp; Lya index scaled to F10
  154.          */
  155.         public double getY10() {
  156.             return y10;
  157.         }

  158.         /** Get the Solar X-Ray &amp; Lya 81-day ave. centered index.
  159.          * <p>Tabular time 5.0 days earlier.</p>
  160.          * @return the Solar X-Ray &amp; Lya 81-day ave. centered index
  161.          */
  162.         public double getY10B() {
  163.             return y10b;
  164.         }

  165.     }

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

  168.     /** UTC time scale. */
  169.     private final TimeScale utc;

  170.     /** First available date. */
  171.     private AbsoluteDate firstDate;

  172.     /** Last available date. */
  173.     private AbsoluteDate lastDate;

  174.     /** Data set. */
  175.     private SortedSet<LineParameters> set;

  176.     /**
  177.      * Constructor.
  178.      * @param utc UTC time scale
  179.      */
  180.     public SOLFSMYDataLoader(final TimeScale utc) {
  181.         this.utc = utc;
  182.         firstDate = null;
  183.         lastDate = null;
  184.         set = new TreeSet<>(new ChronologicalComparator());
  185.     }

  186.     /**
  187.      * Gets the data set.
  188.      * @return the data set
  189.      */
  190.     public SortedSet<LineParameters> getDataSet() {
  191.         return set;
  192.     }

  193.     /**
  194.      * Gets the available data range minimum date.
  195.      * @return the minimum date.
  196.      */
  197.     public AbsoluteDate getMinDate() {
  198.         return firstDate;
  199.     }

  200.     /**
  201.      * Gets the available data range maximum date.
  202.      * @return the maximum date.
  203.      */
  204.     public AbsoluteDate getMaxDate() {
  205.         return lastDate;
  206.     }

  207.     /** {@inheritDoc} */
  208.     public void loadData(final InputStream input, final String name)
  209.             throws IOException, ParseException, OrekitException {

  210.         int lineNumber = 0;
  211.         String line = null;
  212.         final Set<AbsoluteDate> parsedEpochs = new HashSet<>();

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

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

  215.             for (line = reader.readLine(); line != null; line = reader.readLine()) {
  216.                 lineNumber++;
  217.                 if (line.length() > 0) {
  218.                     /** extract the data from the line
  219.                      * The data is extracted using split on a compiled regex pattern.
  220.                      * The column data are separated by spaces.
  221.                      * The format of the data is given in the SOLFSMY.txt document provided by Space Environment.
  222.                      */
  223.                     if (!(line.charAt(0) == '#')) {
  224.                         /**
  225.                          * The Julian Date is expressed as float in the text file,
  226.                          * and supposed to be taken at 12UT.
  227.                          */

  228.                         // Each column is separated by spaces. The compiled regex is PATTERN_SPACE.
  229.                         final String[] splitLine = PATTERN_SPACE.split(line);
  230.                         final double julianDay = Double.parseDouble(splitLine[3]);
  231.                         final int julianDayInt = (int) julianDay;
  232.                         final double julianSeconds = (julianDay - julianDayInt) * Constants.JULIAN_DAY;
  233.                         final AbsoluteDate date = AbsoluteDate.createJDDate(julianDayInt, julianSeconds, utc);

  234.                         if (parsedEpochs.add(date)) { // Checking if entry doesn't exist yet

  235.                             final double f10   = Double.parseDouble(splitLine[4]);
  236.                             final double f10b  = Double.parseDouble(splitLine[5]);
  237.                             final double s10   = Double.parseDouble(splitLine[6]);
  238.                             final double s10b  = Double.parseDouble(splitLine[7]);
  239.                             final double xm10  = Double.parseDouble(splitLine[8]);
  240.                             final double xm10b = Double.parseDouble(splitLine[9]);
  241.                             final double y10   = Double.parseDouble(splitLine[10]);
  242.                             final double y10b  = Double.parseDouble(splitLine[11]);

  243.                             set.add(new LineParameters(date, f10, f10b, s10, s10b, xm10,
  244.                                     xm10b, y10, y10b));

  245.                         }

  246.                     }
  247.                 }
  248.             }

  249.         } catch (NumberFormatException nfe) {
  250.             throw new OrekitException(nfe, OrekitMessages.UNABLE_TO_PARSE_LINE_IN_FILE, lineNumber, name, line);
  251.         }

  252.         try {
  253.             firstDate = set.first().getDate();
  254.             lastDate = set.last().getDate();
  255.         } catch (NoSuchElementException nse) {
  256.             throw new OrekitException(nse, OrekitMessages.NO_DATA_IN_FILE, name);
  257.         }
  258.     }

  259.     /** {@inheritDoc} */
  260.     public boolean stillAcceptsData() {
  261.         return true;
  262.     }
  263. }