FESCnmSnmReader.java

  1. /* Copyright 2002-2015 CS Systèmes d'Information
  2.  * Licensed to CS Systèmes d'Information (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.forces.gravity.potential;

  18. import java.io.BufferedReader;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.io.InputStreamReader;
  22. import java.util.regex.Matcher;
  23. import java.util.regex.Pattern;

  24. import org.orekit.errors.OrekitException;
  25. import org.orekit.errors.OrekitMessages;

  26. /** Reader for ocean tides files following the fes2004_Cnm-Snm.dat format.
  27.  * @since 6.1
  28.  * @author Luc Maisonobe
  29.  */
  30. public class FESCnmSnmReader extends OceanTidesReader {

  31.     /** Default pattern for fields with unknown type (non-space characters). */
  32.     private static final String  UNKNOWN_TYPE_PATTERN = "\\S+";

  33.     /** Pattern for fields with integer type. */
  34.     private static final String  INTEGER_TYPE_PATTERN = "[-+]?\\p{Digit}+";

  35.     /** Pattern for fields with real type. */
  36.     private static final String  REAL_TYPE_PATTERN = "[-+]?(?:(?:\\p{Digit}+(?:\\.\\p{Digit}*)?)|(?:\\.\\p{Digit}+))(?:[eE][-+]?\\p{Digit}+)?";

  37.     /** Pattern for fields with Doodson number. */
  38.     private static final String  DOODSON_TYPE_PATTERN = "\\p{Digit}{2,3}[.,]\\p{Digit}{3}";

  39.     /** Scale of the Cnm, Snm parameters. */
  40.     private final double scale;

  41.     /** Simple constructor.
  42.      * @param supportedNames regular expression for supported files names
  43.      * @param scale scale of the Cnm, Snm parameters
  44.      */
  45.     public FESCnmSnmReader(final String supportedNames, final double scale) {
  46.         super(supportedNames);
  47.         this.scale = scale;
  48.     }

  49.     /** {@inheritDoc} */
  50.     @Override
  51.     public void loadData(final InputStream input, final String name)
  52.         throws OrekitException, IOException {

  53.         // FES ocean tides models have the following form:
  54.         //    Coefficients to compute variations in normalized Stokes coefficients (unit = 10^-12)
  55.         //    Ocean tide model: FES2004 normalized model (fev. 2004) up to (100,100)
  56.         //    (long period from FES2002 up to (50,50) + equilibrium Om1/Om2, atmospheric tide NOT included)
  57.         //    Doodson Darw  l   m    DelC+     DelS+       DelC-     DelS-
  58.         //     55.565 Om1   2   0   6.58128  -0.00000    -0.00000  -0.00000
  59.         //     55.575 Om2   2   0  -0.06330   0.00000     0.00000   0.00000
  60.         //     56.554 Sa    1   0  -0.00000  -0.00000    -0.00000  -0.00000
  61.         //     56.554 Sa    2   0   0.56720   0.01099    -0.00000  -0.00000
  62.         //     56.554 Sa    3   0   0.00908  -0.00050    -0.00000  -0.00000
  63.         final String[] fieldsPatterns = new String[] {
  64.             DOODSON_TYPE_PATTERN,
  65.             UNKNOWN_TYPE_PATTERN,
  66.             INTEGER_TYPE_PATTERN,
  67.             INTEGER_TYPE_PATTERN,
  68.             REAL_TYPE_PATTERN,
  69.             REAL_TYPE_PATTERN,
  70.             REAL_TYPE_PATTERN,
  71.             REAL_TYPE_PATTERN
  72.         };
  73.         final StringBuilder builder = new StringBuilder("^\\p{Space}*");
  74.         for (int i = 0; i < fieldsPatterns.length; ++i) {
  75.             builder.append("(");
  76.             builder.append(fieldsPatterns[i]);
  77.             builder.append(")");
  78.             builder.append((i < fieldsPatterns.length - 1) ? "\\p{Space}+" : "\\p{Space}*$");
  79.         }
  80.         final Pattern regularLinePattern = Pattern.compile(builder.toString());

  81.         // parse the file
  82.         startParse(name);
  83.         final BufferedReader r = new BufferedReader(new InputStreamReader(input, "UTF-8"));
  84.         int lineNumber      = 0;
  85.         boolean dataStarted = false;
  86.         for (String line = r.readLine(); line != null; line = r.readLine()) {
  87.             ++lineNumber;
  88.             final Matcher regularMatcher = regularLinePattern.matcher(line);
  89.             if (regularMatcher.matches()) {
  90.                 // we have found a regular data line

  91.                 // parse Doodson, degree and order fields
  92.                 final int doodson = Integer.parseInt(regularMatcher.group(1).replaceAll("[.,]", ""));
  93.                 final int n       = Integer.parseInt(regularMatcher.group(3));
  94.                 final int m       = Integer.parseInt(regularMatcher.group(4));

  95.                 if (canAdd(n, m)) {

  96.                     // parse coefficients
  97.                     final double cPlus  = scale * Double.parseDouble(regularMatcher.group(5));
  98.                     final double sPlus  = scale * Double.parseDouble(regularMatcher.group(6));
  99.                     final double cMinus = scale * Double.parseDouble(regularMatcher.group(7));
  100.                     final double sMinus = scale * Double.parseDouble(regularMatcher.group(8));

  101.                     // store parsed fields
  102.                     addWaveCoefficients(doodson, n, m, cPlus,  sPlus, cMinus, sMinus, lineNumber, line);
  103.                     dataStarted = true;

  104.                 }

  105.             } else if (dataStarted) {
  106.                 // once the first data line has been encountered,
  107.                 // all remaining lines should also be data lines
  108.                 throw new OrekitException(OrekitMessages.UNABLE_TO_PARSE_LINE_IN_FILE,
  109.                                           lineNumber, name, line);
  110.             }
  111.         }
  112.         endParse();

  113.     }

  114. }