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  
18  package org.orekit.models.earth.atmosphere.data;
19  
20  import java.io.BufferedReader;
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.io.InputStreamReader;
24  import java.io.Serializable;
25  import java.nio.charset.StandardCharsets;
26  import java.text.ParseException;
27  import java.util.NoSuchElementException;
28  import java.util.SortedSet;
29  import java.util.TreeSet;
30  import java.util.regex.Pattern;
31  import java.util.HashSet;
32  import java.util.Set;
33  
34  import org.orekit.data.DataLoader;
35  import org.orekit.errors.OrekitException;
36  import org.orekit.errors.OrekitMessages;
37  import org.orekit.time.AbsoluteDate;
38  import org.orekit.time.ChronologicalComparator;
39  import org.orekit.time.TimeScale;
40  import org.orekit.time.TimeStamped;
41  import org.orekit.utils.Constants;
42  
43  
44  /**
45   * This class reads solar activity data from SOLFSMY files for the class
46   * {@link JB2008SpaceEnvironmentData}. The code in this class is based of the
47   * CssiSpaceWeatherDataLoader.
48   * <p>
49   * The data is provided by Space Environment Technologies through their website
50   * <a href="https://sol.spacenvironment.net/JB2008/indices/SOLFSMY.TXT">Link</a>.
51   * </p>
52   * The work done for this class is based on the CssiWpaceWeatherDataLoader class
53   * by Clément Jonglez, the JB2008 interface by Pascal Parraud, and corrections for
54   * DataLoader implementation by Bryan Cazabonne and Evan Ward .
55   *
56   * @author Louis Aucouturier
57   * @since 11.2
58   */
59  public class SOLFSMYDataLoader implements DataLoader {
60  
61      /** Container class for Solar activity indexes. */
62      public static class LineParameters implements TimeStamped, Serializable {
63  
64          /** Serializable UID. */
65          private static final long serialVersionUID = -9008818050532123587L;
66  
67          /** Entry date. */
68          private final AbsoluteDate date;
69  
70          /** 10.7-cm Solar flux (1e<sup>-22</sup>*Watt/(m²*Hertz))<br>
71           * (Tabular time 1.0 day earlier). */
72          private final double f10;
73  
74          /** 10.7-cm Solar Flux, averaged 81-day centered on the input time.  */
75          private final double f10b;
76  
77          /** EUV index (26-34 nm) scaled to F10. */
78          private final double s10;
79  
80          /** UV 81-day averaged centered index. */
81          private final double s10b;
82  
83          /** MG2 index scaled to F10. */
84          private final double xm10;
85  
86          /** MG2 81-day average centered index. */
87          private final double xm10b;
88  
89          /** Solar X-Ray &amp; Lya index scaled to F10. */
90          private final double y10;
91  
92          /** Solar X-Ray &amp; Lya 81-day average centered index. */
93          private final double y10b;
94  
95          /**
96           * Constructor.
97           * @param date  entry date
98           * @param f10   10.7-cm Solar Radio Flux (F10.7)
99           * @param f10b  10.7-cm Solar Flux, averaged 81-day centered on the input time
100          * @param s10   EUV index (26-34 nm) scaled to F10
101          * @param s10b  UV 81-day averaged centered index
102          * @param xm10  MG2 index scaled to F10
103          * @param xm10b MG2 81-day average centered index
104          * @param y10   Solar X-Ray &amp; Lya index scaled to F10
105          * @param y10b  Solar X-Ray &amp; Lya 81-day average centered index
106          */
107         public LineParameters(final AbsoluteDate date, final double f10, final double f10b, final double s10,
108                 final double s10b, final double xm10, final double xm10b, final double y10, final double y10b) {
109             this.date = date;
110             this.f10 = f10;
111             this.f10b = f10b;
112             this.s10 = s10;
113             this.s10b = s10b;
114             this.xm10 = xm10;
115             this.xm10b = xm10b;
116             this.y10 = y10;
117             this.y10b = y10b;
118         }
119 
120         @Override
121         public AbsoluteDate getDate() {
122             return date;
123         }
124 
125         // The getters does not take into account the lag
126 
127         /** Get the value of the instantaneous solar flux index
128          *  (1e<sup>-22</sup>*Watt/(m²*Hertz)).
129          * <p>Tabular time 1.0 day earlier.</p>
130          * @return the instantaneous F10.7 index
131          */
132         public double getF10() {
133             return f10;
134         }
135 
136         /** Get the value of the mean solar flux.
137          * Averaged 81-day centered F10.7 B index on the input time.
138          * <p>Tabular time 1.0 day earlier.</p>
139          * @return the mean solar flux F10.7B index
140          */
141         public double getF10B() {
142             return f10b;
143         }
144 
145         /** Get the EUV index (26-34 nm) scaled to F10.
146          * <p>Tabular time 1.0 day earlier.</p>
147          * @return the the EUV S10 index
148          */
149         public double getS10() {
150             return s10;
151         }
152 
153         /** Get the EUV 81-day averaged centered index.
154          * <p>Tabular time 1.0 day earlier.</p>
155          * @return the the mean EUV S10B index
156          */
157         public double getS10B() {
158             return s10b;
159         }
160 
161         /** Get the MG2 index scaled to F10.
162          * <p>Tabular time 2.0 days earlier.</p>
163          * @return the the MG2 index
164          */
165         public double getXM10() {
166             return xm10;
167         }
168 
169         /** Get the MG2 81-day average centered index.
170          * <p>Tabular time 2.0 days earlier.</p>
171          * @return the the mean MG2 index
172          */
173         public double getXM10B() {
174             return xm10b;
175         }
176 
177         /** Get the Solar X-Ray &amp; Lya index scaled to F10.
178          * <p>Tabular time 5.0 days earlier.</p>
179          * @return the Solar X-Ray &amp; Lya index scaled to F10
180          */
181         public double getY10() {
182             return y10;
183         }
184 
185         /** Get the Solar X-Ray &amp; Lya 81-day ave. centered index.
186          * <p>Tabular time 5.0 days earlier.</p>
187          * @return the Solar X-Ray &amp; Lya 81-day ave. centered index
188          */
189         public double getY10B() {
190             return y10b;
191         }
192 
193     }
194 
195     /** Pattern for regular data. */
196     private static final Pattern PATTERN_SPACE = Pattern.compile("\\s+");
197 
198     /** UTC time scale. */
199     private final TimeScale utc;
200 
201     /** First available date. */
202     private AbsoluteDate firstDate;
203 
204     /** Last available date. */
205     private AbsoluteDate lastDate;
206 
207     /** Data set. */
208     private SortedSet<LineParameters> set;
209 
210     /**
211      * Constructor.
212      * @param utc UTC time scale
213      */
214     public SOLFSMYDataLoader(final TimeScale utc) {
215         this.utc = utc;
216         firstDate = null;
217         lastDate = null;
218         set = new TreeSet<>(new ChronologicalComparator());
219     }
220 
221     /**
222      * Gets the data set.
223      * @return the data set
224      */
225     public SortedSet<LineParameters> getDataSet() {
226         return set;
227     }
228 
229     /**
230      * Gets the available data range minimum date.
231      * @return the minimum date.
232      */
233     public AbsoluteDate getMinDate() {
234         return firstDate;
235     }
236 
237     /**
238      * Gets the available data range maximum date.
239      * @return the maximum date.
240      */
241     public AbsoluteDate getMaxDate() {
242         return lastDate;
243     }
244 
245     /** {@inheritDoc} */
246     public void loadData(final InputStream input, final String name)
247             throws IOException, ParseException, OrekitException {
248 
249         int lineNumber = 0;
250         String line = null;
251         final Set<AbsoluteDate> parsedEpochs = new HashSet<>();
252 
253         try (BufferedReader br = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8))) {
254 
255             final CommonLineReader reader = new CommonLineReader(br);
256 
257             for (line = reader.readLine(); line != null; line = reader.readLine()) {
258                 lineNumber++;
259                 if (line.length() > 0) {
260                     /** extract the data from the line
261                      * The data is extracted using split on a compiled regex pattern.
262                      * The column data are separated by spaces.
263                      * The format of the data is given in the SOLFSMY.txt document provided by Space Environment.
264                      */
265                     if (!(line.charAt(0) == '#')) {
266                         /**
267                          * The Julian Date is expressed as float in the text file,
268                          * and supposed to be taken at 12UT.
269                          */
270 
271                         // Each column is separated by spaces. The compiled regex is PATTERN_SPACE.
272                         final String[] splitLine = PATTERN_SPACE.split(line);
273                         final double julianDay = Double.parseDouble(splitLine[3]);
274                         final int julianDayInt = (int) julianDay;
275                         final double julianSeconds = (julianDay - julianDayInt) * Constants.JULIAN_DAY;
276                         final AbsoluteDate date = AbsoluteDate.createJDDate(julianDayInt, julianSeconds, utc);
277 
278                         if (parsedEpochs.add(date)) { // Checking if entry doesn't exist yet
279 
280                             final double f10   = Double.parseDouble(splitLine[4]);
281                             final double f10b  = Double.parseDouble(splitLine[5]);
282                             final double s10   = Double.parseDouble(splitLine[6]);
283                             final double s10b  = Double.parseDouble(splitLine[7]);
284                             final double xm10  = Double.parseDouble(splitLine[8]);
285                             final double xm10b = Double.parseDouble(splitLine[9]);
286                             final double y10   = Double.parseDouble(splitLine[10]);
287                             final double y10b  = Double.parseDouble(splitLine[11]);
288 
289                             set.add(new LineParameters(date, f10, f10b, s10, s10b, xm10,
290                                     xm10b, y10, y10b));
291 
292                         }
293 
294                     }
295                 }
296             }
297 
298         } catch (NumberFormatException nfe) {
299             throw new OrekitException(nfe, OrekitMessages.UNABLE_TO_PARSE_LINE_IN_FILE, lineNumber, name, line);
300         }
301 
302         try {
303             firstDate = set.first().getDate();
304             lastDate = set.last().getDate();
305         } catch (NoSuchElementException nse) {
306             throw new OrekitException(nse, OrekitMessages.NO_DATA_IN_FILE, name);
307         }
308     }
309 
310     /** {@inheritDoc} */
311     public boolean stillAcceptsData() {
312         return true;
313     }
314 }