1   /* Copyright 2011-2012 Space Applications Services
2    * Licensed to CS Communication & Systèmes (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;
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.util.Collection;
22  import java.util.LinkedList;
23  import java.util.List;
24  
25  import org.orekit.data.DataLoader;
26  import org.orekit.data.DataSource;
27  
28  /** Loads geomagnetic field models from a given input stream. A stream may contain multiple
29   * models, the loader reads all available models in consecutive order.
30   * <p>
31   * The format of the expected model file is either:
32   * <ul>
33   *   <li>combined format as used by the geomag software, available from the
34   *       <a href="https://www.ngdc.noaa.gov/IAGA/vmod/igrf.html">IGRF model site</a>;
35   *       supports multiple epochs per file</li>
36   *   <li>original format as used by the
37   *       <a href="https://www.ngdc.noaa.gov/geomag/WMM/DoDWMM.shtml">WMM model site</a>.
38   * </ul>
39   * <p>
40   * <b>Combined Format</b>
41   * <pre>
42   *     {model name} {epoch} {nMax} {nMaxSec} {nMax3} {validity start} {validity end} {minAlt} {maxAlt} {model name} {line number}
43   * {n} {m} {gnm} {hnm} {dgnm} {dhnm} {model name} {line number}
44   * </pre>
45   * <p>
46   * Example:
47   * </p>
48   * <pre>
49   *    WMM2010  2010.00 12 12  0 2010.00 2015.00   -1.0  600.0          WMM2010   0
50   * 1  0  -29496.6       0.0      11.6       0.0                        WMM2010   1
51   * 1  1   -1586.3    4944.4      16.5     -25.9                        WMM2010   2
52   * </pre>
53   * <p>
54   * <b>Original WMM Format</b>
55   * <pre>
56   *    {epoch} {model name} {validity start}
57   * {n} {m} {gnm} {hnm} {dgnm} {dhnm}
58   * </pre>
59   * <p>
60   * Example:
61   * </p>
62   * <pre>
63   *    2015.0            WMM-2015        12/15/2014
64   *  1  0  -29438.5       0.0       10.7        0.0
65   *  1  1   -1501.1    4796.2       17.9      -26.8
66   * </pre>
67   *
68   * @author Thomas Neidhart
69   */
70  public class GeoMagneticModelLoader implements DataLoader {
71  
72      /** The loaded models. */
73      private final List<GeoMagneticField> models;
74  
75      /** Empty constructor.
76       * @since 12.0
77       */
78      public GeoMagneticModelLoader() {
79          models = new LinkedList<>();
80      }
81  
82      /** Returns a {@link Collection} of the {@link GeoMagneticField} models that
83       * have been successfully loaded. The {@link Collection} is in
84       * insertion-order, thus it may not be sorted in order of the model epoch.
85       * @return a {@link Collection} of {@link GeoMagneticField} models
86       */
87      public Collection<GeoMagneticField> getModels() {
88          return models;
89      }
90  
91      /** {@inheritDoc} */
92      public boolean stillAcceptsData() {
93          return models.isEmpty();
94      }
95  
96      /** {@inheritDoc} */
97      public void loadData(final InputStream input, final String name) throws IOException {
98          models.addAll(new GeoMagneticModelParser().parse(new DataSource(name, () -> input)));
99      }
100 
101 }