1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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.Collections;
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.regex.Matcher;
27 import java.util.regex.Pattern;
28
29 import org.hipparchus.util.FastMath;
30 import org.orekit.data.DataLoader;
31 import org.orekit.errors.OrekitException;
32 import org.orekit.errors.OrekitMessages;
33
34
35
36
37
38
39 public class AstronomicalAmplitudeReader implements DataLoader {
40
41
42 private static final String OPTIONAL_FIELD_PATTERN = "\\S*";
43
44
45 private static final String DOODSON_TYPE_PATTERN = "\\p{Digit}{2,3}[.,]\\p{Digit}{3}";
46
47
48 private static final String REAL_TYPE_PATTERN =
49 "[-+]?(?:(?:\\p{Digit}+(?:\\.\\p{Digit}*)?)|(?:\\.\\p{Digit}+))(?:[eE][-+]?\\p{Digit}+)?";
50
51
52 private final String supportedNames;
53
54
55 private final Pattern regularLinePattern;
56
57
58 private final int columnDoodson;
59
60
61 private final int columnHf;
62
63
64 private final double scale;
65
66
67 private final Map<Integer, Double> amplitudesMap;
68
69
70
71
72
73
74
75
76 public AstronomicalAmplitudeReader(final String supportedNames, final int columns,
77 final int columnDoodson, final int columnHf,
78 final double scale) {
79
80
81 final StringBuilder builder = new StringBuilder("^\\p{Space}*");
82 for (int i = 1; i <= columns; ++i) {
83 builder.append("(");
84 if (i == columnDoodson) {
85 builder.append(DOODSON_TYPE_PATTERN);
86 } else if (i == columnHf) {
87 builder.append(REAL_TYPE_PATTERN);
88 } else {
89 builder.append(OPTIONAL_FIELD_PATTERN);
90 }
91 builder.append(")");
92 builder.append(i < FastMath.max(columnDoodson, columnHf) ? "\\p{Space}+" : "\\p{Space}*");
93 }
94 builder.append('$');
95 this.regularLinePattern = Pattern.compile(builder.toString());
96
97 this.supportedNames = supportedNames;
98 this.columnDoodson = columnDoodson;
99 this.columnHf = columnHf;
100 this.scale = scale;
101
102 this.amplitudesMap = new HashMap<Integer, Double>();
103
104 }
105
106
107
108
109 public String getSupportedNames() {
110 return supportedNames;
111 }
112
113
114 @Override
115 public boolean stillAcceptsData() {
116 return amplitudesMap.isEmpty();
117 }
118
119
120 @Override
121 public void loadData(final InputStream input, final String name)
122 throws IOException {
123
124
125 final BufferedReader r = new BufferedReader(new InputStreamReader(input, "UTF-8"));
126 int lineNumber = 0;
127 for (String line = r.readLine(); line != null; line = r.readLine()) {
128 ++lineNumber;
129
130 try {
131
132
133
134 line = line.replace('\u2212', '-');
135
136 final Matcher regularMatcher = regularLinePattern.matcher(line);
137 if (regularMatcher.matches()) {
138
139 final int doodson = Integer.parseInt(regularMatcher.group(columnDoodson).replaceAll("[.,]", ""));
140 final double hf = scale * Double.parseDouble(regularMatcher.group(columnHf));
141 amplitudesMap.put(doodson, hf);
142 }
143
144 } catch (NumberFormatException nfe) {
145 throw new OrekitException(OrekitMessages.UNABLE_TO_PARSE_LINE_IN_FILE,
146 lineNumber, name, line);
147 }
148
149 }
150
151 if (amplitudesMap.isEmpty()) {
152 throw new OrekitException(OrekitMessages.NOT_A_SUPPORTED_IERS_DATA_FILE, name);
153 }
154
155 }
156
157
158
159
160
161 public Map<Integer, Double> getAstronomicalAmplitudesMap() {
162 return Collections.unmodifiableMap(amplitudesMap);
163 }
164
165 }