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 public void loadTLEData() {
147
148 availableSatNums.clear();
149
150
151 filterSatelliteNumber = -1;
152 filterLaunchYear = -1;
153 filterLaunchNumber = -1;
154 filterLaunchPiece = null;
155
156
157 tles.clear();
158 previous = null;
159 next = null;
160 DataProvidersManager.getInstance().feed(supportedNames, this);
161 if (tles.isEmpty()) {
162 throw new OrekitException(OrekitMessages.NO_TLE_DATA_AVAILABLE);
163 }
164
165 }
166
167
168
169
170
171 public Set<Integer> getAvailableSatelliteNumbers() {
172 if (availableSatNums.isEmpty()) {
173 loadTLEData();
174 }
175 return availableSatNums;
176 }
177
178
179
180
181
182
183
184
185
186
187 public void loadTLEData(final int satelliteNumber) {
188
189 if (satelliteNumber < 0) {
190
191 loadTLEData();
192 } else {
193
194 filterSatelliteNumber = satelliteNumber;
195 filterLaunchYear = -1;
196 filterLaunchNumber = -1;
197 filterLaunchPiece = null;
198
199
200 tles.clear();
201 previous = null;
202 next = null;
203 DataProvidersManager.getInstance().feed(supportedNames, this);
204 if (tles.isEmpty()) {
205 throw new OrekitException(OrekitMessages.NO_TLE_FOR_OBJECT, satelliteNumber);
206 }
207 }
208
209 }
210
211
212
213
214
215
216
217
218
219
220
221
222
223 public void loadTLEData(final int launchYear, final int launchNumber,
224 final String launchPiece) {
225
226 if ((launchYear < 0) || (launchNumber < 0) ||
227 (launchPiece == null) || (launchPiece.length() == 0)) {
228
229 loadTLEData();
230 } else {
231
232 filterSatelliteNumber = -1;
233 filterLaunchYear = launchYear;
234 filterLaunchNumber = launchNumber;
235 filterLaunchPiece = launchPiece;
236
237
238 tles.clear();
239 previous = null;
240 next = null;
241 DataProvidersManager.getInstance().feed(supportedNames, this);
242 if (tles.isEmpty()) {
243 throw new OrekitException(OrekitMessages.NO_TLE_FOR_LAUNCH_YEAR_NUMBER_PIECE,
244 launchYear, launchNumber, launchPiece);
245 }
246 }
247
248 }
249
250
251 public boolean stillAcceptsData() {
252 return tles.isEmpty();
253 }
254
255
256 public void loadData(final InputStream input, final String name)
257 throws IOException, OrekitException {
258
259 final BufferedReader r = new BufferedReader(new InputStreamReader(input, "UTF-8"));
260 try {
261
262 int lineNumber = 0;
263 String pendingLine = null;
264 for (String line = r.readLine(); line != null; line = r.readLine()) {
265
266 ++lineNumber;
267
268 if (pendingLine == null) {
269
270
271 pendingLine = line;
272
273 } else {
274
275
276 if (!TLE.isFormatOK(pendingLine, line)) {
277 if (ignoreNonTLELines) {
278
279 pendingLine = line;
280 continue;
281 } else {
282 throw new OrekitException(OrekitMessages.NOT_TLE_LINES,
283 lineNumber - 1, lineNumber, pendingLine, line);
284 }
285 }
286
287 final TLEion/analytical/tle/TLE.html#TLE">TLE tle = new TLE(pendingLine, line);
288
289 if (filterSatelliteNumber < 0) {
290 if ((filterLaunchYear < 0) ||
291 ((tle.getLaunchYear() == filterLaunchYear) &&
292 (tle.getLaunchNumber() == filterLaunchNumber) &&
293 tle.getLaunchPiece().equals(filterLaunchPiece))) {
294
295 filterSatelliteNumber = tle.getSatelliteNumber();
296 }
297 }
298
299 availableSatNums.add(tle.getSatelliteNumber());
300
301 if (tle.getSatelliteNumber() == filterSatelliteNumber) {
302
303 tles.add(tle);
304 }
305
306
307 pendingLine = null;
308
309 }
310
311 }
312
313 if ((pendingLine != null) && !ignoreNonTLELines) {
314
315 throw new OrekitException(OrekitMessages.MISSING_SECOND_TLE_LINE,
316 lineNumber, pendingLine);
317 }
318
319 } finally {
320 r.close();
321 }
322
323 }
324
325
326
327
328
329
330
331 public PVCoordinates getPVCoordinates(final AbsoluteDate date) {
332 final TLE toExtrapolate = getClosestTLE(date);
333 if (toExtrapolate != lastTLE) {
334 lastTLE = toExtrapolate;
335 lastPropagator = TLEPropagator.selectExtrapolator(lastTLE);
336 }
337 return lastPropagator.getPVCoordinates(date);
338 }
339
340
341
342
343
344 public TLE getClosestTLE(final AbsoluteDate date) {
345
346
347 if ((previous != null) && (date.durationFrom(previous.getDate()) >= 0) &&
348 (next != null) && (date.durationFrom(next.getDate()) <= 0)) {
349
350 if (next.getDate().durationFrom(date) > date.durationFrom(previous.getDate())) {
351 return previous;
352 } else {
353 return next;
354 }
355 }
356
357 previous = null;
358 next = null;
359 final SortedSet<TimeStamped> headSet = tles.headSet(date);
360 final SortedSet<TimeStamped> tailSet = tles.tailSet(date);
361
362
363 if (headSet.isEmpty()) {
364 return (TLE) tailSet.first();
365 }
366 if (tailSet.isEmpty()) {
367 return (TLE) headSet.last();
368 }
369 previous = (TLE) headSet.last();
370 next = (TLE) tailSet.first();
371
372 if (next.getDate().durationFrom(date) > date.durationFrom(previous.getDate())) {
373 return previous;
374 } else {
375 return next;
376 }
377 }
378
379
380
381
382 public AbsoluteDate getFirstDate() {
383 if (firstDate == null) {
384 firstDate = tles.first().getDate();
385 }
386 return firstDate;
387 }
388
389
390
391
392 public AbsoluteDate getLastDate() {
393 if (lastDate == null) {
394 lastDate = tles.last().getDate();
395 }
396 return lastDate;
397 }
398
399
400
401
402 public TLE getFirst() {
403 return (TLE) tles.first();
404 }
405
406
407
408
409 public TLE getLast() {
410 return (TLE) tles.last();
411 }
412
413
414
415
416 private static class TLEComparator implements Comparator<TimeStamped> {
417
418 @Override
419 public int compare(final TimeStampedStamped">TimeStamped timeStamped1, final TimeStamped timeStamped2) {
420 final int dateCompare = timeStamped1.getDate().compareTo(timeStamped2.getDate());
421 if (dateCompare == 0 && timeStamped1 instanceof TLEkit/propagation/analytical/tle/TLE.html#TLE">TLE && timeStamped2 instanceof TLE) {
422 try {
423 final TLEref="../../../../../org/orekit/propagation/analytical/tle/TLE.html#TLE">TLE tle1 = (TLE) timeStamped1;
424 final TLEref="../../../../../org/orekit/propagation/analytical/tle/TLE.html#TLE">TLE tle2 = (TLE) timeStamped2;
425 final int line1Compare = tle1.getLine1().compareTo(tle2.getLine1());
426 return (line1Compare == 0) ?
427 tle1.getLine2().compareTo(tle2.getLine2()) :
428 line1Compare;
429 } catch (OrekitException oe) {
430
431 throw new OrekitInternalError(oe);
432 }
433 }
434 return dateCompare;
435 }
436 }
437
438 }