GeoMagneticModelLoader.java

  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. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.util.Collection;
  21. import java.util.LinkedList;
  22. import java.util.List;

  23. import org.orekit.data.DataLoader;
  24. import org.orekit.data.DataSource;

  25. /** Loads geomagnetic field models from a given input stream. A stream may contain multiple
  26.  * models, the loader reads all available models in consecutive order.
  27.  * <p>
  28.  * The format of the expected model file is either:
  29.  * <ul>
  30.  *   <li>combined format as used by the geomag software, available from the
  31.  *       <a href="http://www.ngdc.noaa.gov/IAGA/vmod/igrf.html">IGRF model site</a>;
  32.  *       supports multiple epochs per file</li>
  33.  *   <li>original format as used by the
  34.  *       <a href="http://www.ngdc.noaa.gov/geomag/WMM/DoDWMM.shtml">WMM model site</a>.
  35.  * </ul>
  36.  * <p>
  37.  * <b>Combined Format</b>
  38.  * <pre>
  39.  *     {model name} {epoch} {nMax} {nMaxSec} {nMax3} {validity start} {validity end} {minAlt} {maxAlt} {model name} {line number}
  40.  * {n} {m} {gnm} {hnm} {dgnm} {dhnm} {model name} {line number}
  41.  * </pre>
  42.  * <p>
  43.  * Example:
  44.  * </p>
  45.  * <pre>
  46.  *    WMM2010  2010.00 12 12  0 2010.00 2015.00   -1.0  600.0          WMM2010   0
  47.  * 1  0  -29496.6       0.0      11.6       0.0                        WMM2010   1
  48.  * 1  1   -1586.3    4944.4      16.5     -25.9                        WMM2010   2
  49.  * </pre>
  50.  * <p>
  51.  * <b>Original WMM Format</b>
  52.  * <pre>
  53.  *    {epoch} {model name} {validity start}
  54.  * {n} {m} {gnm} {hnm} {dgnm} {dhnm}
  55.  * </pre>
  56.  * <p>
  57.  * Example:
  58.  * </p>
  59.  * <pre>
  60.  *    2015.0            WMM-2015        12/15/2014
  61.  *  1  0  -29438.5       0.0       10.7        0.0
  62.  *  1  1   -1501.1    4796.2       17.9      -26.8
  63.  * </pre>
  64.  *
  65.  * @author Thomas Neidhart
  66.  */
  67. public class GeoMagneticModelLoader implements DataLoader {

  68.     /** The loaded models. */
  69.     private final List<GeoMagneticField> models;

  70.     /** Empty constructor.
  71.      * @since 12.0
  72.      */
  73.     public GeoMagneticModelLoader() {
  74.         models = new LinkedList<>();
  75.     }

  76.     /** Returns a {@link Collection} of the {@link GeoMagneticField} models that
  77.      * have been successfully loaded. The {@link Collection} is in
  78.      * insertion-order, thus it may not be sorted in order of the model epoch.
  79.      * @return a {@link Collection} of {@link GeoMagneticField} models
  80.      */
  81.     public Collection<GeoMagneticField> getModels() {
  82.         return models;
  83.     }

  84.     /** {@inheritDoc} */
  85.     public boolean stillAcceptsData() {
  86.         return models.isEmpty();
  87.     }

  88.     /** {@inheritDoc} */
  89.     public void loadData(final InputStream input, final String name) throws IOException {
  90.         models.addAll(new GeoMagneticModelParser().parse(new DataSource(name, () -> input)));
  91.     }

  92. }