1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.orekit.files.iirv;
18
19 import org.hipparchus.exception.LocalizedCoreFormats;
20 import org.orekit.data.DataSource;
21 import org.orekit.errors.OrekitException;
22 import org.orekit.errors.OrekitIllegalArgumentException;
23 import org.orekit.errors.OrekitMessages;
24 import org.orekit.files.general.EphemerisFileParser;
25 import org.orekit.time.UTCScale;
26 import org.orekit.utils.Constants;
27
28 import java.io.BufferedReader;
29 import java.io.IOException;
30 import java.io.Reader;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.Collections;
34 import java.util.List;
35 import java.util.regex.Pattern;
36
37
38
39
40
41
42
43 public class IIRVParser implements EphemerisFileParser<IIRVEphemerisFile> {
44
45
46 public static final int DEFAULT_INTERPOLATION_SAMPLE = 10;
47
48
49 private static final Pattern LINE_SEPARATOR_PATTERN = Pattern.compile(IIRVVector.LINE_SEPARATOR);
50
51
52 private final double mu;
53
54
55 private final int interpolationSamples;
56
57
58 private final int year;
59
60
61 private final UTCScale utc;
62
63
64
65
66
67
68
69
70
71
72 public IIRVParser(final int year, final UTCScale utc) {
73 this(Constants.IERS96_EARTH_MU, DEFAULT_INTERPOLATION_SAMPLE, year, utc);
74 }
75
76
77
78
79
80
81
82
83
84 public IIRVParser(final double mu, final int interpolationSamples, final int year, final UTCScale utc) {
85 this.mu = mu;
86 this.interpolationSamples = interpolationSamples;
87 this.year = year;
88 this.utc = utc;
89 }
90
91
92 @Override
93 public IIRVEphemerisFile parse(final DataSource source) {
94 if (source == null) {
95 throw new OrekitIllegalArgumentException(OrekitMessages.NULL_ARGUMENT, "source");
96 }
97
98 final ArrayList<String> messageLines = new ArrayList<>();
99
100 try (Reader reader = source.getOpener().openReaderOnce();
101 BufferedReader bufferedReader = (reader == null) ? null : new BufferedReader(reader)) {
102
103 if (bufferedReader == null) {
104 throw new OrekitException(OrekitMessages.UNABLE_TO_FIND_FILE, source.getName());
105 }
106
107
108 final List<String> vectorLines = new ArrayList<>(Collections.nCopies(6, ""));
109 int currentIIRVLine = vectorLines.indexOf("");
110 String line = bufferedReader.readLine();
111
112 if (line == null) {
113 throw new OrekitException(OrekitMessages.NO_DATA_IN_FILE, source.getName());
114 }
115
116 while (line != null) {
117 messageLines.add(line);
118 vectorLines.set(currentIIRVLine, line);
119
120
121
122 if (currentIIRVLine == 5) {
123 for (int i = 0; i < 6; i++) {
124 vectorLines.set(i, "");
125 }
126 currentIIRVLine = 0;
127 } else {
128 currentIIRVLine++;
129 }
130
131
132 final String linebreak1 = bufferedReader.readLine();
133 final String linebreak2 = bufferedReader.readLine();
134 if (linebreak1 == null || linebreak2 == null) {
135 break;
136 } else if (!linebreak1.isEmpty() || !linebreak2.isEmpty()) {
137 throw new OrekitException(OrekitMessages.IIRV_MISSING_LINEBREAK_IN_FILE, currentIIRVLine, source.getName());
138 }
139
140 line = bufferedReader.readLine();
141 }
142
143 } catch (IOException ioe) {
144 throw new OrekitException(ioe, LocalizedCoreFormats.SIMPLE_MESSAGE, ioe.getLocalizedMessage());
145 }
146 return parse(messageLines);
147 }
148
149
150
151
152
153
154
155
156
157 public IIRVEphemerisFile parse(final String iirv) {
158 return parse(Arrays.asList(LINE_SEPARATOR_PATTERN.split(iirv)));
159 }
160
161
162
163
164
165
166
167
168 public IIRVEphemerisFile parse(final List<String> iirvVectorStrings) {
169 final ArrayList<IIRVVector> vectors = new ArrayList<>();
170
171 if (iirvVectorStrings == null) {
172 throw new OrekitIllegalArgumentException(OrekitMessages.NULL_ARGUMENT, "iirvVectorStrings");
173 }
174
175 if (iirvVectorStrings.isEmpty()) {
176 throw new OrekitIllegalArgumentException(OrekitMessages.IIRV_INVALID_LINE_IN_VECTOR, 1, "");
177 }
178
179
180 Pattern line1Pattern = IIRVVector.LINE_1_PATTERN_METADATA_INCLUDED;
181
182
183 final List<String> vectorLines = new ArrayList<>(Collections.nCopies(6, ""));
184 int currentIIRVLine = vectorLines.indexOf("");
185
186 for (String line : iirvVectorStrings) {
187
188
189
190 if (vectors.size() == 1 && currentIIRVLine == 0 && IIRVVector.LINE_1_PATTERN_METADATA_OMITTED.matcher(line).matches()) {
191 line1Pattern = IIRVVector.LINE_1_PATTERN_METADATA_OMITTED;
192 }
193
194
195 final boolean line1Valid = currentIIRVLine == 0 && line1Pattern.matcher(line).matches();
196
197 boolean isValidLine2to6 = false;
198 for (int i = currentIIRVLine; i < 6; i++) {
199 final boolean isValidForLineI = IIRVVector.validateLine(i, line);
200 if (isValidForLineI) {
201 if (currentIIRVLine == i) {
202 isValidLine2to6 = true;
203 break;
204 } else {
205
206 throw new OrekitException(OrekitMessages.IIRV_INVALID_LINE_IN_VECTOR,
207 currentIIRVLine, line);
208 }
209 }
210 }
211
212
213 if (line1Valid || isValidLine2to6) {
214 vectorLines.set(currentIIRVLine, line);
215
216
217
218 if (currentIIRVLine == 5) {
219 IIRVVector newVector = new IIRVVector(vectorLines, utc);
220
221
222 if (!vectors.isEmpty() && line1Pattern == IIRVVector.LINE_1_PATTERN_METADATA_OMITTED) {
223 vectorLines.set(0, vectors.get(0).buildLine1(true));
224 newVector = new IIRVVector(vectorLines, utc);
225 }
226
227 vectors.add(newVector);
228
229 for (int i = 0; i < 6; i++) {
230 vectorLines.set(i, "");
231 }
232 currentIIRVLine = 0;
233 } else {
234 currentIIRVLine++;
235 }
236 }
237 }
238 return new IIRVEphemerisFile(mu, interpolationSamples, year, new IIRVMessage(vectors));
239 }
240 }