1 /* Copyright 2002-2019 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
19 import java.io.BufferedReader;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.InputStreamReader;
23 import java.util.regex.Matcher;
24 import java.util.regex.Pattern;
25
26 import org.orekit.errors.OrekitException;
27 import org.orekit.errors.OrekitMessages;
28
29 /** Reader for ocean tides files following the fes2004_Cnm-Snm.dat format.
30 * @since 6.1
31 * @author Luc Maisonobe
32 */
33 public class FESCnmSnmReader extends OceanTidesReader {
34
35 /** Default pattern for fields with unknown type (non-space characters). */
36 private static final String UNKNOWN_TYPE_PATTERN = "\\S+";
37
38 /** Pattern for fields with integer type. */
39 private static final String INTEGER_TYPE_PATTERN = "[-+]?\\p{Digit}+";
40
41 /** Pattern for fields with real type. */
42 private static final String REAL_TYPE_PATTERN = "[-+]?(?:(?:\\p{Digit}+(?:\\.\\p{Digit}*)?)|(?:\\.\\p{Digit}+))(?:[eE][-+]?\\p{Digit}+)?";
43
44 /** Pattern for fields with Doodson number. */
45 private static final String DOODSON_TYPE_PATTERN = "\\p{Digit}{2,3}[.,]\\p{Digit}{3}";
46
47 /** Scale of the Cnm, Snm parameters. */
48 private final double scale;
49
50 /** Simple constructor.
51 * @param supportedNames regular expression for supported files names
52 * @param scale scale of the Cnm, Snm parameters
53 */
54 public FESCnmSnmReader(final String supportedNames, final double scale) {
55 super(supportedNames);
56 this.scale = scale;
57 }
58
59 /** {@inheritDoc} */
60 @Override
61 public void loadData(final InputStream input, final String name)
62 throws IOException {
63
64 // FES ocean tides models have the following form:
65 // Coefficients to compute variations in normalized Stokes coefficients (unit = 10^-12)
66 // Ocean tide model: FES2004 normalized model (fev. 2004) up to (100,100)
67 // (long period from FES2002 up to (50,50) + equilibrium Om1/Om2, atmospheric tide NOT included)
68 // Doodson Darw l m DelC+ DelS+ DelC- DelS-
69 // 55.565 Om1 2 0 6.58128 -0.00000 -0.00000 -0.00000
70 // 55.575 Om2 2 0 -0.06330 0.00000 0.00000 0.00000
71 // 56.554 Sa 1 0 -0.00000 -0.00000 -0.00000 -0.00000
72 // 56.554 Sa 2 0 0.56720 0.01099 -0.00000 -0.00000
73 // 56.554 Sa 3 0 0.00908 -0.00050 -0.00000 -0.00000
74 final String[] fieldsPatterns = new String[] {
75 DOODSON_TYPE_PATTERN,
76 UNKNOWN_TYPE_PATTERN,
77 INTEGER_TYPE_PATTERN,
78 INTEGER_TYPE_PATTERN,
79 REAL_TYPE_PATTERN,
80 REAL_TYPE_PATTERN,
81 REAL_TYPE_PATTERN,
82 REAL_TYPE_PATTERN
83 };
84 final StringBuilder builder = new StringBuilder("^\\p{Space}*");
85 for (int i = 0; i < fieldsPatterns.length; ++i) {
86 builder.append("(");
87 builder.append(fieldsPatterns[i]);
88 builder.append(")");
89 builder.append((i < fieldsPatterns.length - 1) ? "\\p{Space}+" : "\\p{Space}*$");
90 }
91 final Pattern regularLinePattern = Pattern.compile(builder.toString());
92
93 // parse the file
94 startParse(name);
95 final BufferedReader r = new BufferedReader(new InputStreamReader(input, "UTF-8"));
96 int lineNumber = 0;
97 boolean dataStarted = false;
98 for (String line = r.readLine(); line != null; line = r.readLine()) {
99 ++lineNumber;
100 final Matcher regularMatcher = regularLinePattern.matcher(line);
101 if (regularMatcher.matches()) {
102 // we have found a regular data line
103
104 // parse Doodson, degree and order fields
105 final int doodson = Integer.parseInt(regularMatcher.group(1).replaceAll("[.,]", ""));
106 final int n = Integer.parseInt(regularMatcher.group(3));
107 final int m = Integer.parseInt(regularMatcher.group(4));
108
109 if (canAdd(n, m)) {
110
111 // parse coefficients
112 final double cPlus = scale * Double.parseDouble(regularMatcher.group(5));
113 final double sPlus = scale * Double.parseDouble(regularMatcher.group(6));
114 final double cMinus = scale * Double.parseDouble(regularMatcher.group(7));
115 final double sMinus = scale * Double.parseDouble(regularMatcher.group(8));
116
117 // store parsed fields
118 addWaveCoefficients(doodson, n, m, cPlus, sPlus, cMinus, sMinus, lineNumber, line);
119 dataStarted = true;
120
121 }
122
123 } else if (dataStarted) {
124 // once the first data line has been encountered,
125 // all remaining lines should also be data lines
126 throw new OrekitException(OrekitMessages.UNABLE_TO_PARSE_LINE_IN_FILE,
127 lineNumber, name, line);
128 }
129 }
130 endParse();
131
132 }
133
134 }