FESCHatEpsilonReader.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.Map;
  23. import java.util.regex.Matcher;
  24. import java.util.regex.Pattern;

  25. import org.apache.commons.math3.util.FastMath;
  26. import org.orekit.errors.OrekitException;
  27. import org.orekit.errors.OrekitMessages;

  28. /** Reader for ocean tides files following the fes2004.dat format.
  29.  * @since 6.1
  30.  * @author Luc Maisonobe
  31.  */
  32. public class FESCHatEpsilonReader extends OceanTidesReader {

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

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

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

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

  41.     /** Sea water fensity. */
  42.     private static final double RHO   = 1025;

  43.     /** Gravitational constant (from IERS 2010, chapter 1). */
  44.     private static final double BIG_G = 6.67428e-11;

  45.     /** Earth mean gravity AT EQUATOR (from IERS 2010, chapter 1). */
  46.     private static final double GE    = 9.7803278;

  47.     /** Scale of the CHat parameters. */
  48.     private final double scaleCHat;

  49.     /** Scale of the epsilon parameters. */
  50.     private final double scaleEpsilon;

  51.     /** Load deformation coefficients for ocean tides. */
  52.     private final OceanLoadDeformationCoefficients oldc;

  53.     /** Map for astronomical amplitudes. */
  54.     private final Map<Integer, Double> astronomicalAmplitudes;

  55.     /** Simple constructor.
  56.      * @param supportedNames regular expression for supported files names
  57.      * @param scaleCHat scale of the CHat parameters
  58.      * @param scaleEpsilon scale of the epsilon parameters
  59.      * @param oldc load deformation coefficients for ocean tides
  60.      * @param astronomicalAmplitudes map for astronomical amplitudes
  61.      * @see AstronomicalAmplitudeReader#getAstronomicalAmplitudesMap()
  62.      */
  63.     public FESCHatEpsilonReader(final String supportedNames,
  64.                                 final double scaleCHat, final double scaleEpsilon,
  65.                                 final OceanLoadDeformationCoefficients oldc,
  66.                                 final Map<Integer, Double> astronomicalAmplitudes) {
  67.         super(supportedNames);
  68.         this.scaleCHat              = scaleCHat;
  69.         this.scaleEpsilon           = scaleEpsilon;
  70.         this.oldc                   = oldc;
  71.         this.astronomicalAmplitudes = astronomicalAmplitudes;
  72.     }

  73.     /** {@inheritDoc} */
  74.     @Override
  75.     public void loadData(final InputStream input, final String name)
  76.         throws OrekitException, IOException {

  77.         // FES ocean tides models have the following form:
  78.         //   Ocean tide model: FES2004 normalized model (fev. 2004) up to (100,100) in cm
  79.         //   (long period from FES2002 up to (50,50) + equilibrium Om1/Om2, atmospheric tide NOT included)
  80.         //   Doodson Darw  n   m    Csin+     Ccos+       Csin-     Ccos-       C+   eps+      C-   eps-
  81.         //    55.565 Om1   2   0 -0.540594  0.000000    0.000000  0.000000   0.5406 270.000 0.0000   0.000
  82.         //    55.575 Om2   2   0 -0.005218  0.000000    0.000000  0.000000   0.0052 270.000 0.0000   0.000
  83.         //    56.554 Sa    1   0  0.017233  0.000013    0.000000  0.000000   0.0172  89.957 0.0000   0.000
  84.         //    56.554 Sa    2   0 -0.046604 -0.000903    0.000000  0.000000   0.0466 268.890 0.0000   0.000
  85.         //    56.554 Sa    3   0 -0.000889  0.000049    0.000000  0.000000   0.0009 273.155 0.0000   0.000
  86.         final String[] fieldsPatterns = new String[] {
  87.             DOODSON_TYPE_PATTERN,
  88.             UNKNOWN_TYPE_PATTERN,
  89.             INTEGER_TYPE_PATTERN,
  90.             INTEGER_TYPE_PATTERN,
  91.             REAL_TYPE_PATTERN,
  92.             REAL_TYPE_PATTERN,
  93.             REAL_TYPE_PATTERN,
  94.             REAL_TYPE_PATTERN,
  95.             REAL_TYPE_PATTERN,
  96.             REAL_TYPE_PATTERN,
  97.             REAL_TYPE_PATTERN,
  98.             REAL_TYPE_PATTERN
  99.         };
  100.         final StringBuilder builder = new StringBuilder("^\\p{Space}*");
  101.         for (int i = 0; i < fieldsPatterns.length; ++i) {
  102.             builder.append("(");
  103.             builder.append(fieldsPatterns[i]);
  104.             builder.append(")");
  105.             builder.append((i < fieldsPatterns.length - 1) ? "\\p{Space}+" : "\\p{Space}*$");
  106.         }
  107.         final Pattern regularLinePattern = Pattern.compile(builder.toString());

  108.         final double commonFactor = 4 * FastMath.PI * BIG_G * RHO / GE;
  109.         final double[] kPrime = oldc.getCoefficients();

  110.         // parse the file
  111.         startParse(name);
  112.         final BufferedReader r = new BufferedReader(new InputStreamReader(input, "UTF-8"));
  113.         int lineNumber      = 0;
  114.         for (String line = r.readLine(); line != null; line = r.readLine()) {
  115.             ++lineNumber;
  116.             final Matcher regularMatcher = regularLinePattern.matcher(line);
  117.             if (regularMatcher.matches()) {
  118.                 // we have found a regular data line

  119.                 // parse fields
  120.                 final int doodson = Integer.parseInt(regularMatcher.group(1).replaceAll("[.,]", ""));
  121.                 final int n       = Integer.parseInt(regularMatcher.group(3));
  122.                 final int m       = Integer.parseInt(regularMatcher.group(4));

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

  124.                     final double cHatPlus  = scaleCHat    * Double.parseDouble(regularMatcher.group(9));
  125.                     final double ePlus     = scaleEpsilon * Double.parseDouble(regularMatcher.group(10));
  126.                     final double cHatMinus = scaleCHat    * Double.parseDouble(regularMatcher.group(11));
  127.                     final double eMinus    = scaleEpsilon * Double.parseDouble(regularMatcher.group(12));

  128.                     // compute bias from table 6.6
  129.                     final double hf = astronomicalAmplitudes.containsKey(doodson) ? astronomicalAmplitudes.get(doodson) : 0.0;
  130.                     final int cGamma = doodson / 100000;
  131.                     final double chiF;
  132.                     if (cGamma == 0) {
  133.                         chiF = hf > 0 ? FastMath.PI : 0.0;
  134.                     } else if (cGamma == 1) {
  135.                         chiF = hf > 0 ? 0.5 * FastMath.PI : -0.5 * FastMath.PI;
  136.                     } else if (cGamma == 2) {
  137.                         chiF = hf > 0 ? 0.0 : FastMath.PI;
  138.                     } else {
  139.                         chiF = 0;
  140.                     }

  141.                     // compute reference gravity coefficients by converting height coefficients
  142.                     // IERS conventions 2010, equation 6.21
  143.                     if (n >= kPrime.length) {
  144.                         throw new OrekitException(OrekitMessages.OCEAN_TIDE_LOAD_DEFORMATION_LIMITS,
  145.                                                   kPrime.length - 1, n, name);
  146.                     }
  147.                     final double termFactor = (1 + kPrime[n]) / (2 * n + 1);

  148.                     // an update on IERS conventions from 2012-08-10 states that for FES model:
  149.                     //      Note that, for zonal terms, FES2004 takes the approach to set
  150.                     //      the retrograde coefficients C-f,nO and S-f,n0 to zero and to double
  151.                     //      the prograde coefficients C+f,nO and S+f,n0. Therefore, after
  152.                     //      applying Equation (6.15), the ΔCn0 have the expected value but the
  153.                     //      ΔSn0 must be set to zero.
  154.                     // (see ftp://tai.bipm.org/iers/convupdt/chapter6/icc6.pdf)
  155.                     final double cPlus  =                  commonFactor * termFactor * cHatPlus  * FastMath.sin(ePlus  + chiF);
  156.                     final double sPlus  =                  commonFactor * termFactor * cHatPlus  * FastMath.cos(ePlus  + chiF);
  157.                     final double cMinus = (m == 0) ? 0.0 : commonFactor * termFactor * cHatMinus * FastMath.sin(eMinus + chiF);
  158.                     final double sMinus = (m == 0) ? 0.0 : commonFactor * termFactor * cHatMinus * FastMath.cos(eMinus + chiF);

  159.                     // store parsed fields
  160.                     addWaveCoefficients(doodson, n, m, cPlus,  sPlus, cMinus, sMinus, lineNumber, line);

  161.                 }

  162.             }
  163.         }
  164.         endParse();

  165.     }

  166. }