1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.orekit.frames;
18
19 import java.io.Serializable;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.List;
23 import java.util.Optional;
24 import java.util.function.Consumer;
25 import java.util.function.Function;
26 import java.util.stream.Stream;
27
28 import org.hipparchus.RealFieldElement;
29 import org.hipparchus.analysis.interpolation.FieldHermiteInterpolator;
30 import org.hipparchus.analysis.interpolation.HermiteInterpolator;
31 import org.hipparchus.util.MathArrays;
32 import org.orekit.errors.OrekitException;
33 import org.orekit.errors.OrekitInternalError;
34 import org.orekit.errors.OrekitMessages;
35 import org.orekit.errors.TimeStampedCacheException;
36 import org.orekit.time.AbsoluteDate;
37 import org.orekit.time.FieldAbsoluteDate;
38 import org.orekit.time.TimeStamped;
39 import org.orekit.time.TimeVectorFunction;
40 import org.orekit.utils.Constants;
41 import org.orekit.utils.GenericTimeStampedCache;
42 import org.orekit.utils.IERSConventions;
43 import org.orekit.utils.ImmutableTimeStampedCache;
44 import org.orekit.utils.OrekitConfiguration;
45 import org.orekit.utils.TimeStampedCache;
46 import org.orekit.utils.TimeStampedGenerator;
47
48
49
50
51 public class EOPHistory implements Serializable {
52
53
54 private static final long serialVersionUID = 20131010L;
55
56
57 private static final int INTERPOLATION_POINTS = 4;
58
59
60
61
62
63
64 private final boolean hasData;
65
66
67 private final transient ImmutableTimeStampedCache<EOPEntry> cache;
68
69
70 private final IERSConventions conventions;
71
72
73 private final transient TimeVectorFunction tidalCorrection;
74
75
76
77
78
79
80 protected EOPHistory(final IERSConventions conventions,
81 final Collection<EOPEntry> data,
82 final boolean simpleEOP) {
83 this(conventions, data, simpleEOP ? null : new CachedCorrection(conventions.getEOPTidalCorrection()));
84 }
85
86
87
88
89
90
91 private EOPHistory(final IERSConventions conventions,
92 final Collection<EOPEntry> data,
93 final TimeVectorFunction tidalCorrection) {
94 this.conventions = conventions;
95 this.tidalCorrection = tidalCorrection;
96 if (data.size() >= INTERPOLATION_POINTS) {
97
98 cache = new ImmutableTimeStampedCache<EOPEntry>(INTERPOLATION_POINTS, data);
99 hasData = true;
100 } else {
101
102 cache = ImmutableTimeStampedCache.emptyCache();
103 hasData = false;
104 }
105 }
106
107
108
109
110 public EOPHistory getNonInterpolatingEOPHistory() {
111 return new EOPHistory(conventions, getEntries(), conventions.getEOPTidalCorrection());
112 }
113
114
115
116
117 public boolean usesInterpolation() {
118 return tidalCorrection != null && tidalCorrection instanceof CachedCorrection;
119 }
120
121
122
123
124 public IERSConventions getConventions() {
125 return conventions;
126 }
127
128
129
130
131 public AbsoluteDate getStartDate() {
132 return this.cache.getEarliest().getDate();
133 }
134
135
136
137
138 public AbsoluteDate getEndDate() {
139 return this.cache.getLatest().getDate();
140 }
141
142
143
144
145
146
147 public double getUT1MinusUTC(final AbsoluteDate date) {
148
149
150 if (!this.hasDataFor(date)) {
151
152 return (tidalCorrection == null) ? 0.0 : tidalCorrection.value(date)[2];
153 }
154
155
156 try {
157 final DUT1Interpolator interpolator = new DUT1Interpolator(date);
158 getNeighbors(date).forEach(interpolator);
159 double interpolated = interpolator.getInterpolated();
160 if (tidalCorrection != null) {
161 interpolated += tidalCorrection.value(date)[2];
162 }
163 return interpolated;
164 } catch (TimeStampedCacheException tce) {
165
166 throw new OrekitInternalError(tce);
167 }
168
169 }
170
171
172
173
174
175
176
177
178 public <T extends RealFieldElement<T>> T getUT1MinusUTC(final FieldAbsoluteDate<T> date) {
179
180
181 final AbsoluteDate absDate = date.toAbsoluteDate();
182 if (!this.hasDataFor(absDate)) {
183
184 return (tidalCorrection == null) ? date.getField().getZero() : tidalCorrection.value(date)[2];
185 }
186
187
188 try {
189 final FieldDUT1Interpolator<T> interpolator = new FieldDUT1Interpolator<>(date, absDate);
190 getNeighbors(absDate).forEach(interpolator);
191 T interpolated = interpolator.getInterpolated();
192 if (tidalCorrection != null) {
193 interpolated = interpolated.add(tidalCorrection.value(date)[2]);
194 }
195 return interpolated;
196 } catch (TimeStampedCacheException tce) {
197
198 throw new OrekitInternalError(tce);
199 }
200
201 }
202
203
204 private static class DUT1Interpolator implements Consumer<EOPEntry> {
205
206
207 private double firstDUT;
208
209
210 private boolean beforeLeap;
211
212
213 private final HermiteInterpolator interpolator;
214
215
216 private AbsoluteDate date;
217
218
219
220
221 DUT1Interpolator(final AbsoluteDate date) {
222 this.firstDUT = Double.NaN;
223 this.beforeLeap = true;
224 this.interpolator = new HermiteInterpolator();
225 this.date = date;
226 }
227
228
229 @Override
230 public void accept(final EOPEntry neighbor) {
231 if (Double.isNaN(firstDUT)) {
232 firstDUT = neighbor.getUT1MinusUTC();
233 }
234 final double dut;
235 if (neighbor.getUT1MinusUTC() - firstDUT > 0.9) {
236
237 dut = neighbor.getUT1MinusUTC() - 1.0;
238 if (neighbor.getDate().compareTo(date) <= 0) {
239 beforeLeap = false;
240 }
241 } else {
242 dut = neighbor.getUT1MinusUTC();
243 }
244 interpolator.addSamplePoint(neighbor.getDate().durationFrom(date),
245 new double[] {
246 dut
247 });
248 }
249
250
251
252
253 public double getInterpolated() {
254 final double interpolated = interpolator.value(0)[0];
255 return beforeLeap ? interpolated : interpolated + 1.0;
256 }
257
258 }
259
260
261 private static class FieldDUT1Interpolator<T extends RealFieldElement<T>> implements Consumer<EOPEntry> {
262
263
264 private double firstDUT;
265
266
267 private boolean beforeLeap;
268
269
270 private final FieldHermiteInterpolator<T> interpolator;
271
272
273 private FieldAbsoluteDate<T> date;
274
275
276 private AbsoluteDate absDate;
277
278
279
280
281
282 FieldDUT1Interpolator(final FieldAbsoluteDate<T> date, final AbsoluteDate absDate) {
283 this.firstDUT = Double.NaN;
284 this.beforeLeap = true;
285 this.interpolator = new FieldHermiteInterpolator<>();
286 this.date = date;
287 this.absDate = absDate;
288 }
289
290
291 @Override
292 public void accept(final EOPEntry neighbor) {
293 if (Double.isNaN(firstDUT)) {
294 firstDUT = neighbor.getUT1MinusUTC();
295 }
296 final double dut;
297 if (neighbor.getUT1MinusUTC() - firstDUT > 0.9) {
298
299 dut = neighbor.getUT1MinusUTC() - 1.0;
300 if (neighbor.getDate().compareTo(absDate) <= 0) {
301 beforeLeap = false;
302 }
303 } else {
304 dut = neighbor.getUT1MinusUTC();
305 }
306 final T[] array = MathArrays.buildArray(date.getField(), 1);
307 array[0] = date.getField().getZero().add(dut);
308 interpolator.addSamplePoint(date.durationFrom(neighbor.getDate()).negate(),
309 array);
310 }
311
312
313
314
315 public T getInterpolated() {
316 final T interpolated = interpolator.value(date.getField().getZero())[0];
317 return beforeLeap ? interpolated : interpolated.add(1.0);
318 }
319
320 }
321
322
323
324
325
326
327
328
329
330
331 protected Stream<EOPEntry> getNeighbors(final AbsoluteDate central) {
332 return cache.getNeighbors(central);
333 }
334
335
336
337
338
339
340 public double getLOD(final AbsoluteDate date) {
341
342
343 if (!this.hasDataFor(date)) {
344
345 return (tidalCorrection == null) ? 0.0 : tidalCorrection.value(date)[3];
346 }
347
348
349 double interpolated = interpolate(date, entry -> entry.getLOD());
350 if (tidalCorrection != null) {
351 interpolated += tidalCorrection.value(date)[3];
352 }
353 return interpolated;
354
355 }
356
357
358
359
360
361
362
363
364 public <T extends RealFieldElement<T>> T getLOD(final FieldAbsoluteDate<T> date) {
365
366 final AbsoluteDate aDate = date.toAbsoluteDate();
367
368
369 if (!this.hasDataFor(aDate)) {
370
371 return (tidalCorrection == null) ? date.getField().getZero() : tidalCorrection.value(date)[3];
372 }
373
374
375 T interpolated = interpolate(date, aDate, entry -> entry.getLOD());
376 if (tidalCorrection != null) {
377 interpolated = interpolated.add(tidalCorrection.value(date)[3]);
378 }
379
380 return interpolated;
381
382 }
383
384
385
386
387
388
389
390 public PoleCorrection getPoleCorrection(final AbsoluteDate date) {
391
392
393 if (!this.hasDataFor(date)) {
394
395 if (tidalCorrection == null) {
396 return PoleCorrection.NULL_CORRECTION;
397 } else {
398 final double[] correction = tidalCorrection.value(date);
399 return new PoleCorrection(correction[0], correction[1]);
400 }
401 }
402
403
404 final double[] interpolated = interpolate(date, entry -> entry.getX(), entry -> entry.getY());
405 if (tidalCorrection != null) {
406 final double[] correction = tidalCorrection.value(date);
407 interpolated[0] += correction[0];
408 interpolated[1] += correction[1];
409 }
410 return new PoleCorrection(interpolated[0], interpolated[1]);
411
412 }
413
414
415
416
417
418
419
420
421 public <T extends RealFieldElement<T>> FieldPoleCorrection<T> getPoleCorrection(final FieldAbsoluteDate<T> date) {
422
423 final AbsoluteDate aDate = date.toAbsoluteDate();
424
425
426 if (!this.hasDataFor(aDate)) {
427
428 if (tidalCorrection == null) {
429 return new FieldPoleCorrection<>(date.getField().getZero(), date.getField().getZero());
430 } else {
431 final T[] correction = tidalCorrection.value(date);
432 return new FieldPoleCorrection<>(correction[0], correction[1]);
433 }
434 }
435
436
437 final T[] interpolated = interpolate(date, aDate, entry -> entry.getX(), entry -> entry.getY());
438 if (tidalCorrection != null) {
439 final T[] correction = tidalCorrection.value(date);
440 interpolated[0] = interpolated[0].add(correction[0]);
441 interpolated[1] = interpolated[1].add(correction[1]);
442 }
443 return new FieldPoleCorrection<>(interpolated[0], interpolated[1]);
444
445 }
446
447
448
449
450
451
452
453 public double[] getEquinoxNutationCorrection(final AbsoluteDate date) {
454
455
456 if (!this.hasDataFor(date)) {
457
458 return new double[2];
459 }
460
461
462 return interpolate(date, entry -> entry.getDdPsi(), entry -> entry.getDdEps());
463
464 }
465
466
467
468
469
470
471
472
473 public <T extends RealFieldElement<T>> T[] getEquinoxNutationCorrection(final FieldAbsoluteDate<T> date) {
474
475 final AbsoluteDate aDate = date.toAbsoluteDate();
476
477
478 if (!this.hasDataFor(aDate)) {
479
480 return MathArrays.buildArray(date.getField(), 2);
481 }
482
483
484 return interpolate(date, aDate, entry -> entry.getDdPsi(), entry -> entry.getDdEps());
485
486 }
487
488
489
490
491
492
493
494 public double[] getNonRotatinOriginNutationCorrection(final AbsoluteDate date) {
495
496
497 if (!this.hasDataFor(date)) {
498
499 return new double[2];
500 }
501
502
503 return interpolate(date, entry -> entry.getDx(), entry -> entry.getDy());
504
505 }
506
507
508
509
510
511
512
513
514 public <T extends RealFieldElement<T>> T[] getNonRotatinOriginNutationCorrection(final FieldAbsoluteDate<T> date) {
515
516 final AbsoluteDate aDate = date.toAbsoluteDate();
517
518
519 if (!this.hasDataFor(aDate)) {
520
521 return MathArrays.buildArray(date.getField(), 2);
522 }
523
524
525 return interpolate(date, aDate, entry -> entry.getDx(), entry -> entry.getDy());
526
527 }
528
529
530
531
532
533
534 public ITRFVersion getITRFVersion(final AbsoluteDate date) {
535
536
537 if (!this.hasDataFor(date)) {
538
539 return ITRFVersion.ITRF_2014;
540 }
541
542 try {
543
544 final Optional<EOPEntry> first = getNeighbors(date).findFirst();
545 return first.isPresent() ? first.get().getITRFType() : ITRFVersion.ITRF_2014;
546
547 } catch (TimeStampedCacheException tce) {
548
549 throw new OrekitInternalError(tce);
550 }
551
552 }
553
554
555
556
557 public void checkEOPContinuity(final double maxGap) {
558 TimeStamped preceding = null;
559 for (final TimeStamped current : this.cache.getAll()) {
560
561
562 if ((preceding != null) && ((current.getDate().durationFrom(preceding.getDate())) > maxGap)) {
563 throw new OrekitException(OrekitMessages.MISSING_EARTH_ORIENTATION_PARAMETERS_BETWEEN_DATES,
564 preceding.getDate(), current.getDate());
565 }
566
567
568 preceding = current;
569
570 }
571 }
572
573
574
575
576
577
578
579
580
581 protected boolean hasDataFor(final AbsoluteDate date) {
582
583
584
585
586 return this.hasData && this.getStartDate().compareTo(date) <= 0 &&
587 date.compareTo(this.getEndDate()) <= 0;
588 }
589
590
591
592
593 List<EOPEntry> getEntries() {
594 return cache.getAll();
595 }
596
597
598
599
600
601
602
603
604
605 private double interpolate(final AbsoluteDate date, final Function<EOPEntry, Double> selector) {
606 try {
607 final HermiteInterpolator interpolator = new HermiteInterpolator();
608 getNeighbors(date).forEach(entry ->
609 interpolator.addSamplePoint(entry.getDate().durationFrom(date),
610 new double[] {
611 selector.apply(entry)
612 }));
613 return interpolator.value(0)[0];
614 } catch (TimeStampedCacheException tce) {
615
616 throw new OrekitInternalError(tce);
617 }
618 }
619
620
621
622
623
624
625
626
627
628
629
630 private <T extends RealFieldElement<T>> T interpolate(final FieldAbsoluteDate<T> date,
631 final AbsoluteDate aDate,
632 final Function<EOPEntry, Double> selector) {
633 try {
634 final FieldHermiteInterpolator<T> interpolator = new FieldHermiteInterpolator<>();
635 final T[] y = MathArrays.buildArray(date.getField(), 1);
636 final T zero = date.getField().getZero();
637 final FieldAbsoluteDate<T> central = new FieldAbsoluteDate<>(aDate, zero);
638
639
640 getNeighbors(aDate).forEach(entry -> {
641 y[0] = zero.add(selector.apply(entry));
642 interpolator.addSamplePoint(central.durationFrom(entry.getDate()).negate(), y);
643 });
644 return interpolator.value(date.durationFrom(central))[0];
645 } catch (TimeStampedCacheException tce) {
646
647 throw new OrekitInternalError(tce);
648 }
649 }
650
651
652
653
654
655
656
657
658
659
660 private double[] interpolate(final AbsoluteDate date,
661 final Function<EOPEntry, Double> selector1,
662 final Function<EOPEntry, Double> selector2) {
663 try {
664 final HermiteInterpolator interpolator = new HermiteInterpolator();
665 getNeighbors(date).forEach(entry ->
666 interpolator.addSamplePoint(entry.getDate().durationFrom(date),
667 new double[] {
668 selector1.apply(entry),
669 selector2.apply(entry)
670 }));
671 return interpolator.value(0);
672 } catch (TimeStampedCacheException tce) {
673
674 throw new OrekitInternalError(tce);
675 }
676 }
677
678
679
680
681
682
683
684
685
686
687
688
689 private <T extends RealFieldElement<T>> T[] interpolate(final FieldAbsoluteDate<T> date,
690 final AbsoluteDate aDate,
691 final Function<EOPEntry, Double> selector1,
692 final Function<EOPEntry, Double> selector2) {
693 try {
694 final FieldHermiteInterpolator<T> interpolator = new FieldHermiteInterpolator<>();
695 final T[] y = MathArrays.buildArray(date.getField(), 2);
696 final T zero = date.getField().getZero();
697 final FieldAbsoluteDate<T> central = new FieldAbsoluteDate<>(aDate, zero);
698
699
700 getNeighbors(aDate).forEach(entry -> {
701 y[0] = zero.add(selector1.apply(entry));
702 y[1] = zero.add(selector2.apply(entry));
703 interpolator.addSamplePoint(central.durationFrom(entry.getDate()).negate(), y);
704 });
705 return interpolator.value(date.durationFrom(central));
706 } catch (TimeStampedCacheException tce) {
707
708 throw new OrekitInternalError(tce);
709 }
710 }
711
712
713
714
715
716
717
718 private Object writeReplace() {
719 return new DataTransferObject(conventions, getEntries(), tidalCorrection == null);
720 }
721
722
723 private static class DataTransferObject implements Serializable {
724
725
726 private static final long serialVersionUID = 20131010L;
727
728
729 private final IERSConventions conventions;
730
731
732 private final List<EOPEntry> entries;
733
734
735 private final boolean simpleEOP;
736
737
738
739
740
741
742 DataTransferObject(final IERSConventions conventions,
743 final List<EOPEntry> entries,
744 final boolean simpleEOP) {
745 this.conventions = conventions;
746 this.entries = entries;
747 this.simpleEOP = simpleEOP;
748 }
749
750
751
752
753 private Object readResolve() {
754 try {
755
756 return new EOPHistory(conventions, entries, simpleEOP);
757 } catch (OrekitException oe) {
758 throw new OrekitInternalError(oe);
759 }
760 }
761
762 }
763
764
765 private static class TidalCorrectionEntry implements TimeStamped {
766
767
768 private final AbsoluteDate date;
769
770
771 private final double[] correction;
772
773
774
775
776
777 TidalCorrectionEntry(final AbsoluteDate date, final double[] correction) {
778 this.date = date;
779 this.correction = correction;
780 }
781
782
783 @Override
784 public AbsoluteDate getDate() {
785 return date;
786 }
787
788 }
789
790
791 private static class CachedCorrection
792 implements TimeVectorFunction, TimeStampedGenerator<TidalCorrectionEntry> {
793
794
795 private final TimeVectorFunction tidalCorrection;
796
797
798 private final double step;
799
800
801 private final TimeStampedCache<TidalCorrectionEntry> cache;
802
803
804
805
806 CachedCorrection(final TimeVectorFunction tidalCorrection) {
807 this.step = 60 * 60;
808 this.tidalCorrection = tidalCorrection;
809 this.cache =
810 new GenericTimeStampedCache<TidalCorrectionEntry>(8,
811 OrekitConfiguration.getCacheSlotsNumber(),
812 Constants.JULIAN_DAY * 30,
813 Constants.JULIAN_DAY,
814 this);
815 }
816
817
818 @Override
819 public double[] value(final AbsoluteDate date) {
820 try {
821
822 final HermiteInterpolator interpolator = new HermiteInterpolator();
823 cache.getNeighbors(date).forEach(entry -> interpolator.addSamplePoint(entry.date.durationFrom(date), entry.correction));
824
825
826 return interpolator.value(0.0);
827 } catch (TimeStampedCacheException tsce) {
828
829 throw new OrekitInternalError(tsce);
830 }
831 }
832
833
834 @Override
835 public <T extends RealFieldElement<T>> T[] value(final FieldAbsoluteDate<T> date) {
836 try {
837
838 final AbsoluteDate aDate = date.toAbsoluteDate();
839
840 final FieldHermiteInterpolator<T> interpolator = new FieldHermiteInterpolator<>();
841 final T[] y = MathArrays.buildArray(date.getField(), 4);
842 final T zero = date.getField().getZero();
843 final FieldAbsoluteDate<T> central = new FieldAbsoluteDate<>(aDate, zero);
844
845
846 cache.getNeighbors(aDate).forEach(entry -> {
847 for (int i = 0; i < y.length; ++i) {
848 y[i] = zero.add(entry.correction[i]);
849 }
850 interpolator.addSamplePoint(central.durationFrom(entry.getDate()).negate(), y);
851 });
852
853
854 return interpolator.value(date.durationFrom(central));
855
856 } catch (TimeStampedCacheException tsce) {
857
858 throw new OrekitInternalError(tsce);
859 }
860 }
861
862
863 @Override
864 public List<TidalCorrectionEntry> generate(final AbsoluteDatete">AbsoluteDate existingDate, final AbsoluteDate date) {
865
866 final List<TidalCorrectionEntry> generated = new ArrayList<TidalCorrectionEntry>();
867
868 if (existingDate == null) {
869
870
871 for (int i = -cache.getNeighborsSize() / 2; generated.size() < cache.getNeighborsSize(); ++i) {
872 final AbsoluteDate t = date.shiftedBy(i * step);
873 generated.add(new TidalCorrectionEntry(t, tidalCorrection.value(t)));
874 }
875
876 } else {
877
878
879
880
881 AbsoluteDate t = existingDate;
882 if (date.compareTo(t) > 0) {
883
884 do {
885 t = t.shiftedBy(step);
886 generated.add(new TidalCorrectionEntry(t, tidalCorrection.value(t)));
887 } while (t.compareTo(date) <= 0);
888 } else {
889
890 do {
891 t = t.shiftedBy(-step);
892 generated.add(0, new TidalCorrectionEntry(t, tidalCorrection.value(t)));
893 } while (t.compareTo(date) >= 0);
894 }
895 }
896
897
898 return generated;
899
900 }
901 }
902
903 }