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