1   /* Copyright 2002-2020 CS GROUP
2    * Licensed to CS GROUP (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  
19  import java.io.BufferedReader;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.io.InputStreamReader;
23  import java.nio.charset.StandardCharsets;
24  import java.util.Map;
25  import java.util.regex.Matcher;
26  import java.util.regex.Pattern;
27  
28  import org.hipparchus.util.FastMath;
29  import org.orekit.errors.OrekitException;
30  import org.orekit.errors.OrekitMessages;
31  
32  /** Reader for ocean tides files following the fes2004.dat format.
33   * @since 6.1
34   * @author Luc Maisonobe
35   */
36  public class FESCHatEpsilonReader extends OceanTidesReader {
37  
38      /** Default pattern for fields with unknown type (non-space characters). */
39      private static final String  UNKNOWN_TYPE_PATTERN = "\\S+";
40  
41      /** Pattern for fields with integer type. */
42      private static final String  INTEGER_TYPE_PATTERN = "[-+]?\\p{Digit}+";
43  
44      /** Pattern for fields with real type. */
45      private static final String  REAL_TYPE_PATTERN = "[-+]?(?:(?:\\p{Digit}+(?:\\.\\p{Digit}*)?)|(?:\\.\\p{Digit}+))(?:[eE][-+]?\\p{Digit}+)?";
46  
47      /** Pattern for fields with Doodson number. */
48      private static final String  DOODSON_TYPE_PATTERN = "\\p{Digit}{2,3}[.,]\\p{Digit}{3}";
49  
50      /** Pattern for regular data. */
51      private static final Pattern PATTERN = Pattern.compile("[.,]");
52  
53      /** Sea water fensity. */
54      private static final double RHO   = 1025;
55  
56      /** Gravitational constant (from IERS 2010, chapter 1). */
57      private static final double BIG_G = 6.67428e-11;
58  
59      /** Earth mean gravity AT EQUATOR (from IERS 2010, chapter 1). */
60      private static final double GE    = 9.7803278;
61  
62      /** Scale of the CHat parameters. */
63      private final double scaleCHat;
64  
65      /** Scale of the epsilon parameters. */
66      private final double scaleEpsilon;
67  
68      /** Load deformation coefficients for ocean tides. */
69      private final OceanLoadDeformationCoefficients oldc;
70  
71      /** Map for astronomical amplitudes. */
72      private final Map<Integer, Double> astronomicalAmplitudes;
73  
74      /** Simple constructor.
75       * @param supportedNames regular expression for supported files names
76       * @param scaleCHat scale of the CHat parameters
77       * @param scaleEpsilon scale of the epsilon parameters
78       * @param oldc load deformation coefficients for ocean tides
79       * @param astronomicalAmplitudes map for astronomical amplitudes
80       * @see AstronomicalAmplitudeReader#getAstronomicalAmplitudesMap()
81       */
82      public FESCHatEpsilonReader(final String supportedNames,
83                                  final double scaleCHat, final double scaleEpsilon,
84                                  final OceanLoadDeformationCoefficients oldc,
85                                  final Map<Integer, Double> astronomicalAmplitudes) {
86          super(supportedNames);
87          this.scaleCHat              = scaleCHat;
88          this.scaleEpsilon           = scaleEpsilon;
89          this.oldc                   = oldc;
90          this.astronomicalAmplitudes = astronomicalAmplitudes;
91      }
92  
93      /** {@inheritDoc} */
94      @Override
95      public void loadData(final InputStream input, final String name)
96          throws IOException {
97  
98          // FES ocean tides models have the following form:
99          //   Ocean tide model: FES2004 normalized model (fev. 2004) up to (100,100) in cm
100         //   (long period from FES2002 up to (50,50) + equilibrium Om1/Om2, atmospheric tide NOT included)
101         //   Doodson Darw  n   m    Csin+     Ccos+       Csin-     Ccos-       C+   eps+      C-   eps-
102         //    55.565 Om1   2   0 -0.540594  0.000000    0.000000  0.000000   0.5406 270.000 0.0000   0.000
103         //    55.575 Om2   2   0 -0.005218  0.000000    0.000000  0.000000   0.0052 270.000 0.0000   0.000
104         //    56.554 Sa    1   0  0.017233  0.000013    0.000000  0.000000   0.0172  89.957 0.0000   0.000
105         //    56.554 Sa    2   0 -0.046604 -0.000903    0.000000  0.000000   0.0466 268.890 0.0000   0.000
106         //    56.554 Sa    3   0 -0.000889  0.000049    0.000000  0.000000   0.0009 273.155 0.0000   0.000
107         final String[] fieldsPatterns = new String[] {
108             DOODSON_TYPE_PATTERN,
109             UNKNOWN_TYPE_PATTERN,
110             INTEGER_TYPE_PATTERN,
111             INTEGER_TYPE_PATTERN,
112             REAL_TYPE_PATTERN,
113             REAL_TYPE_PATTERN,
114             REAL_TYPE_PATTERN,
115             REAL_TYPE_PATTERN,
116             REAL_TYPE_PATTERN,
117             REAL_TYPE_PATTERN,
118             REAL_TYPE_PATTERN,
119             REAL_TYPE_PATTERN
120         };
121         final StringBuilder builder = new StringBuilder("^\\p{Space}*");
122         for (int i = 0; i < fieldsPatterns.length; ++i) {
123             builder.append("(");
124             builder.append(fieldsPatterns[i]);
125             builder.append(")");
126             builder.append((i < fieldsPatterns.length - 1) ? "\\p{Space}+" : "\\p{Space}*$");
127         }
128         final Pattern regularLinePattern = Pattern.compile(builder.toString());
129 
130         final double commonFactor = 4 * FastMath.PI * BIG_G * RHO / GE;
131         final double[] kPrime = oldc.getCoefficients();
132 
133         // parse the file
134         startParse(name);
135         try (BufferedReader r = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8))) {
136             int lineNumber      = 0;
137             for (String line = r.readLine(); line != null; line = r.readLine()) {
138                 ++lineNumber;
139                 final Matcher regularMatcher = regularLinePattern.matcher(line);
140                 if (regularMatcher.matches()) {
141                     // we have found a regular data line
142 
143                     // parse fields
144                     final int doodson = Integer.parseInt(PATTERN.matcher(regularMatcher.group(1)).replaceAll(""));
145                     final int n       = Integer.parseInt(regularMatcher.group(3));
146                     final int m       = Integer.parseInt(regularMatcher.group(4));
147 
148                     if (canAdd(n, m)) {
149 
150                         final double cHatPlus  = scaleCHat    * Double.parseDouble(regularMatcher.group(9));
151                         final double ePlus     = scaleEpsilon * Double.parseDouble(regularMatcher.group(10));
152                         final double cHatMinus = scaleCHat    * Double.parseDouble(regularMatcher.group(11));
153                         final double eMinus    = scaleEpsilon * Double.parseDouble(regularMatcher.group(12));
154 
155                         // compute bias from table 6.6
156                         final double hf = astronomicalAmplitudes.getOrDefault(doodson, 0.0);
157                         final int cGamma = doodson / 100000;
158                         final double chiF;
159                         if (cGamma == 0) {
160                             chiF = hf > 0 ? FastMath.PI : 0.0;
161                         } else if (cGamma == 1) {
162                             chiF = hf > 0 ? 0.5 * FastMath.PI : -0.5 * FastMath.PI;
163                         } else if (cGamma == 2) {
164                             chiF = hf > 0 ? 0.0 : FastMath.PI;
165                         } else {
166                             chiF = 0;
167                         }
168 
169                         // compute reference gravity coefficients by converting height coefficients
170                         // IERS conventions 2010, equation 6.21
171                         if (n >= kPrime.length) {
172                             throw new OrekitException(OrekitMessages.OCEAN_TIDE_LOAD_DEFORMATION_LIMITS,
173                                                       kPrime.length - 1, n, name);
174                         }
175                         final double termFactor = (1 + kPrime[n]) / (2 * n + 1);
176 
177                         // an update on IERS conventions from 2012-08-10 states that for FES model:
178                         //      Note that, for zonal terms, FES2004 takes the approach to set
179                         //      the retrograde coefficients C-f,nO and S-f,n0 to zero and to double
180                         //      the prograde coefficients C+f,nO and S+f,n0. Therefore, after
181                         //      applying Equation (6.15), the ΔCn0 have the expected value but the
182                         //      ΔSn0 must be set to zero.
183                         // (see ftp://tai.bipm.org/iers/convupdt/chapter6/icc6.pdf)
184                         final double cPlus  =                  commonFactor * termFactor * cHatPlus  * FastMath.sin(ePlus  + chiF);
185                         final double sPlus  =                  commonFactor * termFactor * cHatPlus  * FastMath.cos(ePlus  + chiF);
186                         final double cMinus = (m == 0) ? 0.0 : commonFactor * termFactor * cHatMinus * FastMath.sin(eMinus + chiF);
187                         final double sMinus = (m == 0) ? 0.0 : commonFactor * termFactor * cHatMinus * FastMath.cos(eMinus + chiF);
188 
189                         // store parsed fields
190                         addWaveCoefficients(doodson, n, m, cPlus,  sPlus, cMinus, sMinus, lineNumber, line);
191 
192                     }
193 
194                 }
195             }
196         }
197         endParse();
198 
199     }
200 
201 }