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.BiFunction;
25 import java.util.function.Consumer;
26 import java.util.function.Function;
27 import java.util.stream.Stream;
28
29 import org.hipparchus.CalculusFieldElement;
30 import org.hipparchus.analysis.interpolation.FieldHermiteInterpolator;
31 import org.hipparchus.analysis.interpolation.HermiteInterpolator;
32 import org.hipparchus.util.FastMath;
33 import org.hipparchus.util.MathArrays;
34 import org.orekit.annotation.DefaultDataContext;
35 import org.orekit.data.DataContext;
36 import org.orekit.errors.OrekitException;
37 import org.orekit.errors.OrekitInternalError;
38 import org.orekit.errors.OrekitMessages;
39 import org.orekit.errors.TimeStampedCacheException;
40 import org.orekit.time.AbsoluteDate;
41 import org.orekit.time.FieldAbsoluteDate;
42 import org.orekit.time.TimeScales;
43 import org.orekit.time.TimeStamped;
44 import org.orekit.time.TimeVectorFunction;
45 import org.orekit.utils.Constants;
46 import org.orekit.utils.GenericTimeStampedCache;
47 import org.orekit.utils.IERSConventions;
48 import org.orekit.utils.ImmutableTimeStampedCache;
49 import org.orekit.utils.OrekitConfiguration;
50 import org.orekit.utils.TimeStampedCache;
51 import org.orekit.utils.TimeStampedGenerator;
52
53
54
55
56
57 public class EOPHistory implements Serializable {
58
59
60
61
62 public static final int DEFAULT_INTERPOLATION_DEGREE = 3;
63
64
65 private static final long serialVersionUID = 20231022L;
66
67
68
69
70 private final int interpolationDegree;
71
72
73
74
75
76
77 private final boolean hasData;
78
79
80 private final transient ImmutableTimeStampedCache<EOPEntry> cache;
81
82
83 private final IERSConventions conventions;
84
85
86 private final transient TimeVectorFunction tidalCorrection;
87
88
89 private final transient TimeScales timeScales;
90
91
92
93
94
95
96
97
98
99
100
101 @DefaultDataContext
102 protected EOPHistory(final IERSConventions conventions,
103 final int interpolationDegree,
104 final Collection<? extends EOPEntry> data,
105 final boolean simpleEOP) {
106 this(conventions, interpolationDegree, data, simpleEOP, DataContext.getDefault().getTimeScales());
107 }
108
109
110
111
112
113
114
115
116
117 public EOPHistory(final IERSConventions conventions,
118 final int interpolationDegree,
119 final Collection<? extends EOPEntry> data,
120 final boolean simpleEOP,
121 final TimeScales timeScales) {
122 this(conventions, interpolationDegree, data,
123 simpleEOP ? null : new CachedCorrection(conventions.getEOPTidalCorrection(timeScales)),
124 timeScales);
125 }
126
127
128
129
130
131
132
133
134
135 private EOPHistory(final IERSConventions conventions,
136 final int interpolationDegree,
137 final Collection<? extends EOPEntry> data,
138 final TimeVectorFunction tidalCorrection,
139 final TimeScales timeScales) {
140
141
142 final int k = (interpolationDegree + 1) / 4;
143 if (interpolationDegree != 4 * k - 1) {
144 throw new OrekitException(OrekitMessages.WRONG_EOP_INTERPOLATION_DEGREE, interpolationDegree);
145 }
146
147 this.conventions = conventions;
148 this.interpolationDegree = interpolationDegree;
149 this.tidalCorrection = tidalCorrection;
150 this.timeScales = timeScales;
151 if (data.size() >= 1) {
152
153 if (missSomeDerivatives(data)) {
154
155 final ImmutableTimeStampedCache<EOPEntry> rawCache =
156 new ImmutableTimeStampedCache<>(FastMath.min(interpolationDegree + 1, data.size()), data);
157 final List<EOPEntry>fixedData = new ArrayList<>();
158 for (final EOPEntry entry : rawCache.getAll()) {
159 fixedData.add(fixDerivatives(entry, rawCache));
160 }
161 cache = new ImmutableTimeStampedCache<>(FastMath.min(interpolationDegree + 1, fixedData.size()), fixedData);
162 } else {
163 cache = new ImmutableTimeStampedCache<>(FastMath.min(interpolationDegree + 1, data.size()), data);
164 }
165 hasData = true;
166 } else {
167
168 cache = ImmutableTimeStampedCache.emptyCache();
169 hasData = false;
170 }
171 }
172
173
174
175
176
177
178 public boolean isSimpleEop() {
179 return tidalCorrection == null;
180 }
181
182
183
184
185
186 public int getInterpolationDegree() {
187 return interpolationDegree;
188 }
189
190
191
192
193
194
195
196 public TimeScales getTimeScales() {
197 return timeScales;
198 }
199
200
201
202
203
204 public EOPHistory getEOPHistoryWithoutCachedTidalCorrection() {
205 return new EOPHistory(conventions, interpolationDegree, getEntries(),
206 conventions.getEOPTidalCorrection(timeScales),
207 timeScales);
208 }
209
210
211
212
213
214 public boolean cachesTidalCorrection() {
215 return tidalCorrection instanceof CachedCorrection;
216 }
217
218
219
220
221 public IERSConventions getConventions() {
222 return conventions;
223 }
224
225
226
227
228 public AbsoluteDate getStartDate() {
229 return this.cache.getEarliest().getDate();
230 }
231
232
233
234
235 public AbsoluteDate getEndDate() {
236 return this.cache.getLatest().getDate();
237 }
238
239
240
241
242
243
244 public double getUT1MinusUTC(final AbsoluteDate date) {
245
246
247 if (!this.hasDataFor(date)) {
248
249 return (tidalCorrection == null) ? 0.0 : tidalCorrection.value(date)[2];
250 }
251
252
253 try {
254 final DUT1Interpolator interpolator = new DUT1Interpolator(date);
255 getNeighbors(date, interpolationDegree + 1).forEach(interpolator);
256 double interpolated = interpolator.getInterpolated();
257 if (tidalCorrection != null) {
258 interpolated += tidalCorrection.value(date)[2];
259 }
260 return interpolated;
261 } catch (TimeStampedCacheException tce) {
262
263 throw new OrekitInternalError(tce);
264 }
265
266 }
267
268
269
270
271
272
273
274
275 public <T extends CalculusFieldElement<T>> T getUT1MinusUTC(final FieldAbsoluteDate<T> date) {
276
277
278 final AbsoluteDate absDate = date.toAbsoluteDate();
279 if (!this.hasDataFor(absDate)) {
280
281 return (tidalCorrection == null) ? date.getField().getZero() : tidalCorrection.value(date)[2];
282 }
283
284
285 try {
286 final FieldDUT1Interpolator<T> interpolator = new FieldDUT1Interpolator<>(date, absDate);
287 getNeighbors(absDate, interpolationDegree + 1).forEach(interpolator);
288 T interpolated = interpolator.getInterpolated();
289 if (tidalCorrection != null) {
290 interpolated = interpolated.add(tidalCorrection.value(date)[2]);
291 }
292 return interpolated;
293 } catch (TimeStampedCacheException tce) {
294
295 throw new OrekitInternalError(tce);
296 }
297
298 }
299
300
301 private static class DUT1Interpolator implements Consumer<EOPEntry> {
302
303
304 private double firstDUT;
305
306
307 private boolean beforeLeap;
308
309
310 private final HermiteInterpolator interpolator;
311
312
313 private AbsoluteDate date;
314
315
316
317
318 DUT1Interpolator(final AbsoluteDate date) {
319 this.firstDUT = Double.NaN;
320 this.beforeLeap = true;
321 this.interpolator = new HermiteInterpolator();
322 this.date = date;
323 }
324
325
326 @Override
327 public void accept(final EOPEntry neighbor) {
328 if (Double.isNaN(firstDUT)) {
329 firstDUT = neighbor.getUT1MinusUTC();
330 }
331 final double dut;
332 if (neighbor.getUT1MinusUTC() - firstDUT > 0.9) {
333
334 dut = neighbor.getUT1MinusUTC() - 1.0;
335
336
337
338
339 if (neighbor.getDate().shiftedBy(-1).compareTo(date) <= 0) {
340 beforeLeap = false;
341 }
342 } else {
343 dut = neighbor.getUT1MinusUTC();
344 }
345 interpolator.addSamplePoint(neighbor.getDate().durationFrom(date),
346 new double[] {
347 dut
348 });
349 }
350
351
352
353
354 public double getInterpolated() {
355 final double interpolated = interpolator.value(0)[0];
356 return beforeLeap ? interpolated : interpolated + 1.0;
357 }
358
359 }
360
361
362 private static class FieldDUT1Interpolator<T extends CalculusFieldElement<T>> implements Consumer<EOPEntry> {
363
364
365 private double firstDUT;
366
367
368 private boolean beforeLeap;
369
370
371 private final FieldHermiteInterpolator<T> interpolator;
372
373
374 private FieldAbsoluteDate<T> date;
375
376
377 private AbsoluteDate absDate;
378
379
380
381
382
383 FieldDUT1Interpolator(final FieldAbsoluteDate<T> date, final AbsoluteDate absDate) {
384 this.firstDUT = Double.NaN;
385 this.beforeLeap = true;
386 this.interpolator = new FieldHermiteInterpolator<>();
387 this.date = date;
388 this.absDate = absDate;
389 }
390
391
392 @Override
393 public void accept(final EOPEntry neighbor) {
394 if (Double.isNaN(firstDUT)) {
395 firstDUT = neighbor.getUT1MinusUTC();
396 }
397 final double dut;
398 if (neighbor.getUT1MinusUTC() - firstDUT > 0.9) {
399
400 dut = neighbor.getUT1MinusUTC() - 1.0;
401 if (neighbor.getDate().compareTo(absDate) <= 0) {
402 beforeLeap = false;
403 }
404 } else {
405 dut = neighbor.getUT1MinusUTC();
406 }
407 final T[] array = MathArrays.buildArray(date.getField(), 1);
408 array[0] = date.getField().getZero().add(dut);
409 interpolator.addSamplePoint(date.durationFrom(neighbor.getDate()).negate(),
410 array);
411 }
412
413
414
415
416 public T getInterpolated() {
417 final T interpolated = interpolator.value(date.getField().getZero())[0];
418 return beforeLeap ? interpolated : interpolated.add(1.0);
419 }
420
421 }
422
423
424
425
426
427
428
429
430
431
432
433
434 protected Stream<EOPEntry> getNeighbors(final AbsoluteDate central, final int n) {
435 return cache.getNeighbors(central, n);
436 }
437
438
439
440
441
442
443 public double getLOD(final AbsoluteDate date) {
444
445
446 if (!this.hasDataFor(date)) {
447
448 return (tidalCorrection == null) ? 0.0 : tidalCorrection.value(date)[3];
449 }
450
451
452 double interpolated = interpolate(date, entry -> entry.getLOD());
453 if (tidalCorrection != null) {
454 interpolated += tidalCorrection.value(date)[3];
455 }
456 return interpolated;
457
458 }
459
460
461
462
463
464
465
466
467 public <T extends CalculusFieldElement<T>> T getLOD(final FieldAbsoluteDate<T> date) {
468
469 final AbsoluteDate aDate = date.toAbsoluteDate();
470
471
472 if (!this.hasDataFor(aDate)) {
473
474 return (tidalCorrection == null) ? date.getField().getZero() : tidalCorrection.value(date)[3];
475 }
476
477
478 T interpolated = interpolate(date, aDate, entry -> entry.getLOD());
479 if (tidalCorrection != null) {
480 interpolated = interpolated.add(tidalCorrection.value(date)[3]);
481 }
482
483 return interpolated;
484
485 }
486
487
488
489
490
491
492
493 public PoleCorrection getPoleCorrection(final AbsoluteDate date) {
494
495
496 if (!this.hasDataFor(date)) {
497
498 if (tidalCorrection == null) {
499 return PoleCorrection.NULL_CORRECTION;
500 } else {
501 final double[] correction = tidalCorrection.value(date);
502 return new PoleCorrection(correction[0], correction[1]);
503 }
504 }
505
506
507 final double[] interpolated = interpolate(date,
508 entry -> entry.getX(), entry -> entry.getY(),
509 entry -> entry.getXRate(), entry -> entry.getYRate());
510 if (tidalCorrection != null) {
511 final double[] correction = tidalCorrection.value(date);
512 interpolated[0] += correction[0];
513 interpolated[1] += correction[1];
514 }
515 return new PoleCorrection(interpolated[0], interpolated[1]);
516
517 }
518
519
520
521
522
523
524
525
526 public <T extends CalculusFieldElement<T>> FieldPoleCorrection<T> getPoleCorrection(final FieldAbsoluteDate<T> date) {
527
528 final AbsoluteDate aDate = date.toAbsoluteDate();
529
530
531 if (!this.hasDataFor(aDate)) {
532
533 if (tidalCorrection == null) {
534 return new FieldPoleCorrection<>(date.getField().getZero(), date.getField().getZero());
535 } else {
536 final T[] correction = tidalCorrection.value(date);
537 return new FieldPoleCorrection<>(correction[0], correction[1]);
538 }
539 }
540
541
542 final T[] interpolated = interpolate(date, aDate, entry -> entry.getX(), entry -> entry.getY());
543 if (tidalCorrection != null) {
544 final T[] correction = tidalCorrection.value(date);
545 interpolated[0] = interpolated[0].add(correction[0]);
546 interpolated[1] = interpolated[1].add(correction[1]);
547 }
548 return new FieldPoleCorrection<>(interpolated[0], interpolated[1]);
549
550 }
551
552
553
554
555
556
557
558 public double[] getEquinoxNutationCorrection(final AbsoluteDate date) {
559
560
561 if (!this.hasDataFor(date)) {
562
563 return new double[2];
564 }
565
566
567 return interpolate(date, entry -> entry.getDdPsi(), entry -> entry.getDdEps());
568
569 }
570
571
572
573
574
575
576
577
578 public <T extends CalculusFieldElement<T>> T[] getEquinoxNutationCorrection(final FieldAbsoluteDate<T> date) {
579
580 final AbsoluteDate aDate = date.toAbsoluteDate();
581
582
583 if (!this.hasDataFor(aDate)) {
584
585 return MathArrays.buildArray(date.getField(), 2);
586 }
587
588
589 return interpolate(date, aDate, entry -> entry.getDdPsi(), entry -> entry.getDdEps());
590
591 }
592
593
594
595
596
597
598
599 public double[] getNonRotatinOriginNutationCorrection(final AbsoluteDate date) {
600
601
602 if (!this.hasDataFor(date)) {
603
604 return new double[2];
605 }
606
607
608 return interpolate(date, entry -> entry.getDx(), entry -> entry.getDy());
609
610 }
611
612
613
614
615
616
617
618
619 public <T extends CalculusFieldElement<T>> T[] getNonRotatinOriginNutationCorrection(final FieldAbsoluteDate<T> date) {
620
621 final AbsoluteDate aDate = date.toAbsoluteDate();
622
623
624 if (!this.hasDataFor(aDate)) {
625
626 return MathArrays.buildArray(date.getField(), 2);
627 }
628
629
630 return interpolate(date, aDate, entry -> entry.getDx(), entry -> entry.getDy());
631
632 }
633
634
635
636
637
638
639 public ITRFVersion getITRFVersion(final AbsoluteDate date) {
640
641
642 if (!this.hasDataFor(date)) {
643
644 return ITRFVersion.ITRF_2014;
645 }
646
647 try {
648
649 final Optional<EOPEntry> first = getNeighbors(date, 1).findFirst();
650 return first.isPresent() ? first.get().getITRFType() : ITRFVersion.ITRF_2014;
651
652 } catch (TimeStampedCacheException tce) {
653
654 throw new OrekitInternalError(tce);
655 }
656
657 }
658
659
660
661
662 public void checkEOPContinuity(final double maxGap) {
663 TimeStamped preceding = null;
664 for (final TimeStamped current : this.cache.getAll()) {
665
666
667 if (preceding != null && (current.getDate().durationFrom(preceding.getDate())) > maxGap) {
668 throw new OrekitException(OrekitMessages.MISSING_EARTH_ORIENTATION_PARAMETERS_BETWEEN_DATES_GAP,
669 preceding.getDate(), current.getDate(),
670 current.getDate().durationFrom(preceding.getDate()));
671 }
672
673
674 preceding = current;
675
676 }
677 }
678
679
680
681
682
683
684
685
686
687 protected boolean hasDataFor(final AbsoluteDate date) {
688
689
690
691
692 return this.hasData && this.getStartDate().compareTo(date) <= 0 &&
693 date.compareTo(this.getEndDate()) <= 0;
694 }
695
696
697
698
699 public List<EOPEntry> getEntries() {
700 return cache.getAll();
701 }
702
703
704
705
706
707
708
709
710
711 private double interpolate(final AbsoluteDate date, final Function<EOPEntry, Double> selector) {
712 try {
713 final HermiteInterpolator interpolator = new HermiteInterpolator();
714 getNeighbors(date, interpolationDegree + 1).
715 forEach(entry -> interpolator.addSamplePoint(entry.getDate().durationFrom(date),
716 new double[] {
717 selector.apply(entry)
718 }));
719 return interpolator.value(0)[0];
720 } catch (TimeStampedCacheException tce) {
721
722 throw new OrekitInternalError(tce);
723 }
724 }
725
726
727
728
729
730
731
732
733
734
735
736 private <T extends CalculusFieldElement<T>> T interpolate(final FieldAbsoluteDate<T> date,
737 final AbsoluteDate aDate,
738 final Function<EOPEntry, Double> selector) {
739 try {
740 final FieldHermiteInterpolator<T> interpolator = new FieldHermiteInterpolator<>();
741 final T[] y = MathArrays.buildArray(date.getField(), 1);
742 final T zero = date.getField().getZero();
743 final FieldAbsoluteDate<T> central = new FieldAbsoluteDate<>(aDate, zero);
744
745
746 getNeighbors(aDate, interpolationDegree + 1).
747 forEach(entry -> {
748 y[0] = zero.add(selector.apply(entry));
749 interpolator.addSamplePoint(central.durationFrom(entry.getDate()).negate(), y);
750 });
751 return interpolator.value(date.durationFrom(central))[0];
752 } catch (TimeStampedCacheException tce) {
753
754 throw new OrekitInternalError(tce);
755 }
756 }
757
758
759
760
761
762
763
764
765
766
767 private double[] interpolate(final AbsoluteDate date,
768 final Function<EOPEntry, Double> selector1,
769 final Function<EOPEntry, Double> selector2) {
770 try {
771 final HermiteInterpolator interpolator = new HermiteInterpolator();
772 getNeighbors(date, interpolationDegree + 1).
773 forEach(entry -> interpolator.addSamplePoint(entry.getDate().durationFrom(date),
774 new double[] {
775 selector1.apply(entry),
776 selector2.apply(entry)
777 }));
778 return interpolator.value(0);
779 } catch (TimeStampedCacheException tce) {
780
781 throw new OrekitInternalError(tce);
782 }
783 }
784
785
786
787
788
789
790
791
792
793
794
795
796
797 private double[] interpolate(final AbsoluteDate date,
798 final Function<EOPEntry, Double> selector1,
799 final Function<EOPEntry, Double> selector2,
800 final Function<EOPEntry, Double> selector1Rate,
801 final Function<EOPEntry, Double> selector2Rate) {
802 try {
803 final HermiteInterpolator interpolator = new HermiteInterpolator();
804 getNeighbors(date, (interpolationDegree + 1) / 2).
805 forEach(entry -> interpolator.addSamplePoint(entry.getDate().durationFrom(date),
806 new double[] {
807 selector1.apply(entry),
808 selector2.apply(entry)
809 },
810 new double[] {
811 selector1Rate.apply(entry),
812 selector2Rate.apply(entry)
813 }));
814 return interpolator.value(0);
815 } catch (TimeStampedCacheException tce) {
816
817 throw new OrekitInternalError(tce);
818 }
819 }
820
821
822
823
824
825
826
827
828
829
830
831
832 private <T extends CalculusFieldElement<T>> T[] interpolate(final FieldAbsoluteDate<T> date,
833 final AbsoluteDate aDate,
834 final Function<EOPEntry, Double> selector1,
835 final Function<EOPEntry, Double> selector2) {
836 try {
837 final FieldHermiteInterpolator<T> interpolator = new FieldHermiteInterpolator<>();
838 final T[] y = MathArrays.buildArray(date.getField(), 2);
839 final T zero = date.getField().getZero();
840 final FieldAbsoluteDate<T> central = new FieldAbsoluteDate<>(aDate, zero);
841
842
843 getNeighbors(aDate, interpolationDegree + 1).
844 forEach(entry -> {
845 y[0] = zero.add(selector1.apply(entry));
846 y[1] = zero.add(selector2.apply(entry));
847 interpolator.addSamplePoint(central.durationFrom(entry.getDate()).negate(), y);
848 });
849 return interpolator.value(date.durationFrom(central));
850 } catch (TimeStampedCacheException tce) {
851
852 throw new OrekitInternalError(tce);
853 }
854 }
855
856
857
858
859
860
861 private boolean missSomeDerivatives(final Collection<? extends EOPEntry> raw) {
862 for (final EOPEntry entry : raw) {
863 if (Double.isNaN(entry.getLOD() + entry.getXRate() + entry.getYRate())) {
864 return true;
865 }
866 }
867 return false;
868 }
869
870
871
872
873
874
875
876 private EOPEntry fixDerivatives(final EOPEntry entry, final ImmutableTimeStampedCache<EOPEntry> rawCache) {
877
878
879 final BiFunction<EOPEntry, Function<EOPEntry, Double>, Double> differentiator =
880 (e, selector) -> {
881 final HermiteInterpolator interpolator = new HermiteInterpolator();
882 rawCache.getNeighbors(e.getDate()).
883 forEach(f -> interpolator.addSamplePoint(f.getDate().durationFrom(e.getDate()),
884 new double[] {
885 selector.apply(f)
886 }));
887 return interpolator.derivatives(0.0, 1)[1][0];
888 };
889
890 if (Double.isNaN(entry.getLOD() + entry.getXRate() + entry.getYRate())) {
891 final double lod = Double.isNaN(entry.getLOD()) ?
892 -differentiator.apply(entry, e -> e.getUT1MinusUTC()) :
893 entry.getLOD();
894 final double xRate = Double.isNaN(entry.getXRate()) ?
895 differentiator.apply(entry, e -> e.getX()) :
896 entry.getXRate();
897 final double yRate = Double.isNaN(entry.getYRate()) ?
898 differentiator.apply(entry, e -> e.getY()) :
899 entry.getYRate();
900 return new EOPEntry(entry.getMjd(),
901 entry.getUT1MinusUTC(), lod,
902 entry.getX(), entry.getY(), xRate, yRate,
903 entry.getDdPsi(), entry.getDdEps(),
904 entry.getDx(), entry.getDy(),
905 entry.getITRFType(), entry.getDate());
906 } else {
907
908 return entry;
909 }
910 }
911
912
913
914
915 @DefaultDataContext
916 private Object writeReplace() {
917 return new DataTransferObject(conventions, interpolationDegree, getEntries(), tidalCorrection == null);
918 }
919
920
921 @DefaultDataContext
922 private static class DataTransferObject implements Serializable {
923
924
925 private static final long serialVersionUID = 20231122L;
926
927
928 private final IERSConventions conventions;
929
930
931
932
933 private final int interpolationDegree;
934
935
936 private final List<EOPEntry> entries;
937
938
939 private final boolean simpleEOP;
940
941
942
943
944
945
946
947
948 DataTransferObject(final IERSConventions conventions,
949 final int interpolationDegree,
950 final List<EOPEntry> entries,
951 final boolean simpleEOP) {
952 this.conventions = conventions;
953 this.interpolationDegree = interpolationDegree;
954 this.entries = entries;
955 this.simpleEOP = simpleEOP;
956 }
957
958
959
960
961 private Object readResolve() {
962 try {
963 return new EOPHistory(conventions, interpolationDegree, entries, simpleEOP);
964 } catch (OrekitException oe) {
965 throw new OrekitInternalError(oe);
966 }
967 }
968
969 }
970
971
972 private static class TidalCorrectionEntry implements TimeStamped {
973
974
975 private final AbsoluteDate date;
976
977
978 private final double[] correction;
979
980
981
982
983
984 TidalCorrectionEntry(final AbsoluteDate date, final double[] correction) {
985 this.date = date;
986 this.correction = correction;
987 }
988
989
990 @Override
991 public AbsoluteDate getDate() {
992 return date;
993 }
994
995 }
996
997
998 private static class CachedCorrection
999 implements TimeVectorFunction, TimeStampedGenerator<TidalCorrectionEntry> {
1000
1001
1002 private final TimeVectorFunction tidalCorrection;
1003
1004
1005 private final double step;
1006
1007
1008 private final TimeStampedCache<TidalCorrectionEntry> cache;
1009
1010
1011
1012
1013 CachedCorrection(final TimeVectorFunction tidalCorrection) {
1014 this.step = 60 * 60;
1015 this.tidalCorrection = tidalCorrection;
1016 this.cache =
1017 new GenericTimeStampedCache<TidalCorrectionEntry>(8,
1018 OrekitConfiguration.getCacheSlotsNumber(),
1019 Constants.JULIAN_DAY * 30,
1020 Constants.JULIAN_DAY,
1021 this);
1022 }
1023
1024
1025 @Override
1026 public double[] value(final AbsoluteDate date) {
1027 try {
1028
1029 final HermiteInterpolator interpolator = new HermiteInterpolator();
1030 cache.getNeighbors(date).forEach(entry -> interpolator.addSamplePoint(entry.date.durationFrom(date), entry.correction));
1031
1032
1033 return interpolator.value(0.0);
1034 } catch (TimeStampedCacheException tsce) {
1035
1036 throw new OrekitInternalError(tsce);
1037 }
1038 }
1039
1040
1041 @Override
1042 public <T extends CalculusFieldElement<T>> T[] value(final FieldAbsoluteDate<T> date) {
1043 try {
1044
1045 final AbsoluteDate aDate = date.toAbsoluteDate();
1046
1047 final FieldHermiteInterpolator<T> interpolator = new FieldHermiteInterpolator<>();
1048 final T[] y = MathArrays.buildArray(date.getField(), 4);
1049 final T zero = date.getField().getZero();
1050 final FieldAbsoluteDate<T> central = new FieldAbsoluteDate<>(aDate, zero);
1051
1052
1053 cache.getNeighbors(aDate).forEach(entry -> {
1054 for (int i = 0; i < y.length; ++i) {
1055 y[i] = zero.add(entry.correction[i]);
1056 }
1057 interpolator.addSamplePoint(central.durationFrom(entry.getDate()).negate(), y);
1058 });
1059
1060
1061 return interpolator.value(date.durationFrom(central));
1062
1063 } catch (TimeStampedCacheException tsce) {
1064
1065 throw new OrekitInternalError(tsce);
1066 }
1067 }
1068
1069
1070 @Override
1071 public List<TidalCorrectionEntry> generate(final AbsoluteDate existingDate, final AbsoluteDate date) {
1072
1073 final List<TidalCorrectionEntry> generated = new ArrayList<TidalCorrectionEntry>();
1074
1075 if (existingDate == null) {
1076
1077
1078 for (int i = -cache.getMaxNeighborsSize() / 2; generated.size() < cache.getMaxNeighborsSize(); ++i) {
1079 final AbsoluteDate t = date.shiftedBy(i * step);
1080 generated.add(new TidalCorrectionEntry(t, tidalCorrection.value(t)));
1081 }
1082
1083 } else {
1084
1085
1086
1087
1088 AbsoluteDate t = existingDate;
1089 if (date.compareTo(t) > 0) {
1090
1091 do {
1092 t = t.shiftedBy(step);
1093 generated.add(new TidalCorrectionEntry(t, tidalCorrection.value(t)));
1094 } while (t.compareTo(date) <= 0);
1095 } else {
1096
1097 do {
1098 t = t.shiftedBy(-step);
1099 generated.add(0, new TidalCorrectionEntry(t, tidalCorrection.value(t)));
1100 } while (t.compareTo(date) >= 0);
1101 }
1102 }
1103
1104
1105 return generated;
1106
1107 }
1108 }
1109
1110 }