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.io.InputStreamReader;
  21. import java.io.StreamTokenizer;
  22. import java.text.ParseException;
  23. import java.util.Collection;
  24. import java.util.LinkedList;
  25. import java.util.List;

  26. import org.orekit.data.DataLoader;

  27. /** Loads geomagnetic field models from a given input stream. A stream may contain multiple
  28.  * models, the loader reads all available models in consecutive order.
  29.  * <p>
  30.  * The format of the expected model file is the following:
  31.  * </p>
  32.  * <pre>
  33.  *     {model name} {epoch} {nMax} {nMaxSec} {nMax3} {validity start} {validity end} {minAlt} {maxAlt} {model name} {line number}
  34.  * {n} {m} {gnm} {hnm} {dgnm} {dhnm} {model name} {line number}
  35.  * </pre>
  36.  * <p>
  37.  * Example:
  38.  * </p>
  39.  * <pre>
  40.  *    WMM2010  2010.00 12 12  0 2010.00 2015.00   -1.0  600.0          WMM2010   0
  41.  * 1  0  -29496.6       0.0      11.6       0.0                        WMM2010   1
  42.  * 1  1   -1586.3    4944.4      16.5     -25.9                        WMM2010   2
  43.  * </pre>
  44.  * @author Thomas Neidhart
  45.  */
  46. public class GeoMagneticModelLoader implements DataLoader {

  47.     /** The loaded models. */
  48.     private List<GeoMagneticField> models = new LinkedList<GeoMagneticField>();

  49.     /** Returns a {@link Collection} of the {@link GeoMagneticField} models that
  50.      * have been successfully loaded. The {@link Collection} is in
  51.      * insertion-order, thus it may not be sorted in order of the model epoch.
  52.      * @return a {@link Collection} of {@link GeoMagneticField} models
  53.      */
  54.     public Collection<GeoMagneticField> getModels() {
  55.         return models;
  56.     }

  57.     /** {@inheritDoc} */
  58.     public boolean stillAcceptsData() {
  59.         return models == null || models.isEmpty();
  60.     }

  61.     /** {@inheritDoc} */
  62.     public void loadData(final InputStream input, final String name)
  63.         throws IOException, ParseException {

  64.         // open data file and parse values
  65.         final StreamTokenizer str = new StreamTokenizer(new InputStreamReader(input, "UTF-8"));

  66.         while (true) {
  67.             final GeoMagneticField model = readModel(str);
  68.             if (model != null) {
  69.                 models.add(model);
  70.             } else {
  71.                 break;
  72.             }
  73.         }
  74.     }

  75.     /** Read the model from the given {@link StreamTokenizer}.
  76.      * @param stream the stream to read the model from
  77.      * @return the parsed geomagnetic field model
  78.      * @throws IOException if an I/O error occurs
  79.      */
  80.     private GeoMagneticField readModel(final StreamTokenizer stream) throws IOException {

  81.         // check whether there is another model available in the stream
  82.         final int ttype = stream.nextToken();
  83.         if (ttype == StreamTokenizer.TT_EOF) {
  84.             return null;
  85.         }

  86.         final String modelName = stream.sval;
  87.         stream.nextToken();
  88.         final double epoch = stream.nval;
  89.         stream.nextToken();
  90.         final int nMax = (int) stream.nval;
  91.         stream.nextToken();
  92.         final int nMaxSecVar = (int) stream.nval;

  93.         // ignored
  94.         stream.nextToken();

  95.         stream.nextToken();
  96.         final double startYear = stream.nval;

  97.         stream.nextToken();
  98.         final double endYear = stream.nval;

  99.         final GeoMagneticField model = new GeoMagneticField(modelName, epoch, nMax, nMaxSecVar,
  100.                                                             startYear, endYear);

  101.         // the rest is ignored
  102.         stream.nextToken();
  103.         @SuppressWarnings("unused")
  104.         final double altmin = stream.nval;

  105.         stream.nextToken();
  106.         @SuppressWarnings("unused")
  107.         final double altmax = stream.nval;

  108.         // the min/max altitude values are ignored by now

  109.         stream.nextToken();
  110.         stream.nextToken();

  111.         // loop to get model data from file
  112.         boolean done = false;
  113.         int n;
  114.         int m;

  115.         do {
  116.             stream.nextToken();
  117.             n = (int) stream.nval;
  118.             stream.nextToken();
  119.             m = (int) stream.nval;

  120.             stream.nextToken();
  121.             final double gnm = stream.nval;
  122.             stream.nextToken();
  123.             final double hnm = stream.nval;
  124.             stream.nextToken();
  125.             final double dgnm = stream.nval;
  126.             stream.nextToken();
  127.             final double dhnm = stream.nval;

  128.             model.setMainFieldCoefficients(n, m, gnm, hnm);
  129.             if (n <= nMaxSecVar && m <= nMaxSecVar) {
  130.                 model.setSecularVariationCoefficients(n, m, dgnm, dhnm);
  131.             }

  132.             stream.nextToken();
  133.             stream.nextToken();

  134.             done = n == nMax && m == nMax;
  135.         } while (!done);

  136.         return model;
  137.     }
  138. }