1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.orekit.propagation.analytical.tle;
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.Comparator;
24 import java.util.Set;
25 import java.util.SortedSet;
26 import java.util.TreeSet;
27
28 import org.orekit.data.DataLoader;
29 import org.orekit.data.DataProvidersManager;
30 import org.orekit.errors.OrekitException;
31 import org.orekit.errors.OrekitInternalError;
32 import org.orekit.errors.OrekitMessages;
33 import org.orekit.time.AbsoluteDate;
34 import org.orekit.time.TimeStamped;
35 import org.orekit.utils.PVCoordinates;
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 @Deprecated
58 public class TLESeries implements DataLoader {
59
60
61 private static final String DEFAULT_SUPPORTED_NAMES = ".*\\.tle$";
62
63
64 private final String supportedNames;
65
66
67 private final Set<Integer> availableSatNums;
68
69
70 private final SortedSet<TimeStamped> tles;
71
72
73 private int filterSatelliteNumber;
74
75
76 private int filterLaunchYear;
77
78
79 private int filterLaunchNumber;
80
81
82 private String filterLaunchPiece;
83
84
85 private TLE previous;
86
87
88 private TLE next;
89
90
91 private TLE lastTLE;
92
93
94 private TLEPropagator lastPropagator;
95
96
97 private AbsoluteDate firstDate;
98
99
100 private AbsoluteDate lastDate;
101
102
103 private final boolean ignoreNonTLELines;
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119 public TLESeries(final String supportedNames, final boolean ignoreNonTLELines) {
120
121 this.supportedNames = (supportedNames == null) ? DEFAULT_SUPPORTED_NAMES : supportedNames;
122 availableSatNums = new TreeSet<Integer>();
123 this.ignoreNonTLELines = ignoreNonTLELines;
124 filterSatelliteNumber = -1;
125 filterLaunchYear = -1;
126 filterLaunchNumber = -1;
127 filterLaunchPiece = null;
128
129 tles = new TreeSet<TimeStamped>(new TLEComparator());
130 previous = null;
131 next = null;
132
133 }
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148 public void loadTLEData() throws OrekitException {
149
150 availableSatNums.clear();
151
152
153 filterSatelliteNumber = -1;
154 filterLaunchYear = -1;
155 filterLaunchNumber = -1;
156 filterLaunchPiece = null;
157
158
159 tles.clear();
160 previous = null;
161 next = null;
162 DataProvidersManager.getInstance().feed(supportedNames, this);
163 if (tles.isEmpty()) {
164 throw new OrekitException(OrekitMessages.NO_TLE_DATA_AVAILABLE);
165 }
166
167 }
168
169
170
171
172
173
174 public Set<Integer> getAvailableSatelliteNumbers() throws OrekitException {
175 if (availableSatNums.isEmpty()) {
176 loadTLEData();
177 }
178 return availableSatNums;
179 }
180
181
182
183
184
185
186
187
188
189
190
191
192 public void loadTLEData(final int satelliteNumber) throws OrekitException {
193
194 if (satelliteNumber < 0) {
195
196 loadTLEData();
197 } else {
198
199 filterSatelliteNumber = satelliteNumber;
200 filterLaunchYear = -1;
201 filterLaunchNumber = -1;
202 filterLaunchPiece = null;
203
204
205 tles.clear();
206 previous = null;
207 next = null;
208 DataProvidersManager.getInstance().feed(supportedNames, this);
209 if (tles.isEmpty()) {
210 throw new OrekitException(OrekitMessages.NO_TLE_FOR_OBJECT, satelliteNumber);
211 }
212 }
213
214 }
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230 public void loadTLEData(final int launchYear, final int launchNumber,
231 final String launchPiece) throws OrekitException {
232
233 if ((launchYear < 0) || (launchNumber < 0) ||
234 (launchPiece == null) || (launchPiece.length() == 0)) {
235
236 loadTLEData();
237 } else {
238
239 filterSatelliteNumber = -1;
240 filterLaunchYear = launchYear;
241 filterLaunchNumber = launchNumber;
242 filterLaunchPiece = launchPiece;
243
244
245 tles.clear();
246 previous = null;
247 next = null;
248 DataProvidersManager.getInstance().feed(supportedNames, this);
249 if (tles.isEmpty()) {
250 throw new OrekitException(OrekitMessages.NO_TLE_FOR_LAUNCH_YEAR_NUMBER_PIECE,
251 launchYear, launchNumber, launchPiece);
252 }
253 }
254
255 }
256
257
258 public boolean stillAcceptsData() {
259 return tles.isEmpty();
260 }
261
262
263 public void loadData(final InputStream input, final String name)
264 throws IOException, OrekitException {
265
266 final BufferedReader r = new BufferedReader(new InputStreamReader(input, "UTF-8"));
267 try {
268
269 int lineNumber = 0;
270 String pendingLine = null;
271 for (String line = r.readLine(); line != null; line = r.readLine()) {
272
273 ++lineNumber;
274
275 if (pendingLine == null) {
276
277
278 pendingLine = line;
279
280 } else {
281
282
283 if (!TLE.isFormatOK(pendingLine, line)) {
284 if (ignoreNonTLELines) {
285
286 pendingLine = line;
287 continue;
288 } else {
289 throw new OrekitException(OrekitMessages.NOT_TLE_LINES,
290 lineNumber - 1, lineNumber, pendingLine, line);
291 }
292 }
293
294 final TLE tle = new TLE(pendingLine, line);
295
296 if (filterSatelliteNumber < 0) {
297 if ((filterLaunchYear < 0) ||
298 ((tle.getLaunchYear() == filterLaunchYear) &&
299 (tle.getLaunchNumber() == filterLaunchNumber) &&
300 tle.getLaunchPiece().equals(filterLaunchPiece))) {
301
302 filterSatelliteNumber = tle.getSatelliteNumber();
303 }
304 }
305
306 availableSatNums.add(tle.getSatelliteNumber());
307
308 if (tle.getSatelliteNumber() == filterSatelliteNumber) {
309
310 tles.add(tle);
311 }
312
313
314 pendingLine = null;
315
316 }
317
318 }
319
320 if ((pendingLine != null) && !ignoreNonTLELines) {
321
322 throw new OrekitException(OrekitMessages.MISSING_SECOND_TLE_LINE,
323 lineNumber, pendingLine);
324 }
325
326 } finally {
327 r.close();
328 }
329
330 }
331
332
333
334
335
336
337
338
339 public PVCoordinates getPVCoordinates(final AbsoluteDate date)
340 throws OrekitException {
341 final TLE toExtrapolate = getClosestTLE(date);
342 if (toExtrapolate != lastTLE) {
343 lastTLE = toExtrapolate;
344 lastPropagator = TLEPropagator.selectExtrapolator(lastTLE);
345 }
346 return lastPropagator.getPVCoordinates(date);
347 }
348
349
350
351
352
353 public TLE getClosestTLE(final AbsoluteDate date) {
354
355
356 if ((previous != null) && (date.durationFrom(previous.getDate()) >= 0) &&
357 (next != null) && (date.durationFrom(next.getDate()) <= 0)) {
358
359 if (next.getDate().durationFrom(date) > date.durationFrom(previous.getDate())) {
360 return previous;
361 } else {
362 return next;
363 }
364 }
365
366 previous = null;
367 next = null;
368 final SortedSet<TimeStamped> headSet = tles.headSet(date);
369 final SortedSet<TimeStamped> tailSet = tles.tailSet(date);
370
371
372 if (headSet.isEmpty()) {
373 return (TLE) tailSet.first();
374 }
375 if (tailSet.isEmpty()) {
376 return (TLE) headSet.last();
377 }
378 previous = (TLE) headSet.last();
379 next = (TLE) tailSet.first();
380
381 if (next.getDate().durationFrom(date) > date.durationFrom(previous.getDate())) {
382 return previous;
383 } else {
384 return next;
385 }
386 }
387
388
389
390
391 public AbsoluteDate getFirstDate() {
392 if (firstDate == null) {
393 firstDate = tles.first().getDate();
394 }
395 return firstDate;
396 }
397
398
399
400
401 public AbsoluteDate getLastDate() {
402 if (lastDate == null) {
403 lastDate = tles.last().getDate();
404 }
405 return lastDate;
406 }
407
408
409
410
411 public TLE getFirst() {
412 return (TLE) tles.first();
413 }
414
415
416
417
418 public TLE getLast() {
419 return (TLE) tles.last();
420 }
421
422
423
424
425 private static class TLEComparator implements Comparator<TimeStamped> {
426
427 @Override
428 public int compare(final TimeStamped timeStamped1, final TimeStamped timeStamped2) {
429 final int dateCompare = timeStamped1.getDate().compareTo(timeStamped2.getDate());
430 if (dateCompare == 0 && timeStamped1 instanceof TLE && timeStamped2 instanceof TLE) {
431 try {
432 final TLE tle1 = (TLE) timeStamped1;
433 final TLE tle2 = (TLE) timeStamped2;
434 final int line1Compare = tle1.getLine1().compareTo(tle2.getLine1());
435 return (line1Compare == 0) ?
436 tle1.getLine2().compareTo(tle2.getLine2()) :
437 line1Compare;
438 } catch (OrekitException oe) {
439
440 throw new OrekitInternalError(oe);
441 }
442 }
443 return dateCompare;
444 }
445 }
446
447 }