1   /* Copyright 2002-2026 CS GROUP
2    * Licensed to CS GROUP (CS) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * CS licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.orekit.bodies;
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.nio.charset.StandardCharsets;
22  import java.text.ParseException;
23  import java.util.ArrayList;
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.SortedSet;
28  import java.util.TreeSet;
29  import java.util.concurrent.TimeUnit;
30  import java.util.concurrent.atomic.AtomicReference;
31  
32  import org.hipparchus.CalculusFieldElement;
33  import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
34  import org.hipparchus.geometry.euclidean.threed.Vector3D;
35  import org.hipparchus.util.FastMath;
36  import org.orekit.annotation.DefaultDataContext;
37  import org.orekit.data.AbstractSelfFeedingLoader;
38  import org.orekit.data.DataContext;
39  import org.orekit.data.DataLoader;
40  import org.orekit.data.DataProvidersManager;
41  import org.orekit.errors.OrekitException;
42  import org.orekit.errors.OrekitInternalError;
43  import org.orekit.errors.OrekitMessages;
44  import org.orekit.errors.TimeStampedCacheException;
45  import org.orekit.frames.Frame;
46  import org.orekit.time.AbsoluteDate;
47  import org.orekit.time.ChronologicalComparator;
48  import org.orekit.time.DateComponents;
49  import org.orekit.time.FieldAbsoluteDate;
50  import org.orekit.time.TimeComponents;
51  import org.orekit.time.TimeOffset;
52  import org.orekit.time.TimeScale;
53  import org.orekit.time.TimeScales;
54  import org.orekit.utils.Constants;
55  import org.orekit.utils.FieldPVCoordinates;
56  import org.orekit.utils.OrekitConfiguration;
57  import org.orekit.utils.PVCoordinates;
58  import org.orekit.utils.GenericTimeStampedCache;
59  import org.orekit.utils.TimeStampedGenerator;
60  import org.orekit.utils.units.UnitsConverter;
61  
62  /** Loader for JPL ephemerides binary files (DE 4xx) and similar formats (INPOP 06/08/10).
63   * <p>JPL ephemerides binary files contain ephemerides for all solar system planets.</p>
64   * <p>The JPL ephemerides binary files are recognized thanks to their base names,
65   * which must match the pattern <code>[lu]nx[mp]####.ddd</code> (or
66   * <code>[lu]nx[mp]####.ddd.gz</code> for gzip-compressed files) where # stands for a
67   * digit character and where ddd is an ephemeris type (typically 405 or 406).</p>
68   * <p>The loader supports files encoded in big-endian as well as in little-endian notation.
69   * Usually, big-endian files are named <code>unx[mp]####.ddd</code>, while little-endian files
70   * are named <code>lnx[mp]####.ddd</code>.</p>
71   * <p>The IMCCE ephemerides binary files are recognized thanks to their base names,
72   * which must match the pattern <code>inpop*.dat</code> (or
73   * <code>inpop*.dat.gz</code> for gzip-compressed files) where * stands for any string.</p>
74   * <p>The loader supports files encoded in big-endian as well as in little-endian notation.
75   * Usually, big-endian files contain <code>bigendian</code> in their names, while little-endian files
76   * contain <code>littleendian</code> in their names.</p>
77   * <p>The loader supports files in TDB or TCB time scales.</p>
78   * @author Luc Maisonobe
79   */
80  public class JPLEphemeridesLoader extends AbstractSelfFeedingLoader
81          implements CelestialBodyLoader {
82  
83      /** Default supported files name pattern for JPL DE files. */
84      public static final String DEFAULT_DE_SUPPORTED_NAMES = "^[lu]nx([mp](\\d\\d\\d\\d))+\\.(?:4\\d\\d)$";
85  
86      /** Default supported files name pattern for IMCCE INPOP files. */
87      public static final String DEFAULT_INPOP_SUPPORTED_NAMES = "^inpop.*\\.dat$";
88  
89      /** 50 days. */
90      private static final TimeOffset FIFTY_DAYS =
91              new TimeOffset(50, TimeUnit.DAYS);
92  
93      /** DE number used by INPOP files. */
94      private static final int INPOP_DE_NUMBER = 100;
95  
96      /** Maximal number of constants in headers. */
97      private static final int CONSTANTS_MAX_NUMBER           = 400;
98  
99      /** Offset of the ephemeris type in first header record. */
100     private static final int HEADER_EPHEMERIS_TYPE_OFFSET   = 2840;
101 
102     /** Offset of the record size (for INPOP files) in first header record. */
103     private static final int HEADER_RECORD_SIZE_OFFSET      = 2856;
104 
105     /** Offset of the start epoch in first header record. */
106     private static final int HEADER_START_EPOCH_OFFSET      = 2652;
107 
108     /** Offset of the end epoch in first header record. */
109     private static final int HEADER_END_EPOCH_OFFSET        = 2660;
110 
111     /** Offset of the astronomical unit in first header record. */
112     private static final int HEADER_ASTRONOMICAL_UNIT_OFFSET = 2680;
113 
114     /** Offset of the Earth-Moon mass ratio in first header record. */
115     private static final int HEADER_EM_RATIO_OFFSET         = 2688;
116 
117     /** Offset of Chebishev coefficients indices in first header record. */
118     private static final int HEADER_CHEBISHEV_INDICES_OFFSET = 2696;
119 
120     /** Offset of libration coefficients indices in first header record. */
121     private static final int HEADER_LIBRATION_INDICES_OFFSET = 2844;
122 
123     /** Offset of chunks duration in first header record. */
124     private static final int HEADER_CHUNK_DURATION_OFFSET    = 2668;
125 
126     /** Offset of the constants names in first header record. */
127     private static final int HEADER_CONSTANTS_NAMES_OFFSET  = 252;
128 
129     /** Offset of the constants values in second header record. */
130     private static final int HEADER_CONSTANTS_VALUES_OFFSET = 0;
131 
132     /** Offset of the range start in the data records. */
133     private static final int DATA_START_RANGE_OFFSET        = 0;
134 
135     /** Offset of the range end in the data records. */
136     private static final int DATE_END_RANGE_OFFSET          = 8;
137 
138     /** The constant name for the astronomical unit. */
139     private static final String CONSTANT_AU = "AU";
140 
141     /** The constant name for the earth-moon mass ratio. */
142     private static final String CONSTANT_EMRAT = "EMRAT";
143 
144     /** List of supported ephemerides types. */
145     public enum EphemerisType {
146 
147         /** Constant for solar system barycenter. */
148         SOLAR_SYSTEM_BARYCENTER,
149 
150         /** Constant for the Sun. */
151         SUN,
152 
153         /** Constant for Mercury. */
154         MERCURY,
155 
156         /** Constant for Venus. */
157         VENUS,
158 
159         /** Constant for the Earth-Moon barycenter. */
160         EARTH_MOON,
161 
162         /** Constant for the Earth. */
163         EARTH,
164 
165         /** Constant for the Moon. */
166         MOON,
167 
168         /** Constant for Mars. */
169         MARS,
170 
171         /** Constant for Jupiter. */
172         JUPITER,
173 
174         /** Constant for Saturn. */
175         SATURN,
176 
177         /** Constant for Uranus. */
178         URANUS,
179 
180         /** Constant for Neptune. */
181         NEPTUNE,
182 
183         /** Constant for Pluto. */
184         PLUTO,
185 
186         /** Constant for Lunar librations. */
187         LIBRATION
188 
189     }
190 
191     /** Interface for raw position-velocity retrieval. */
192     public interface RawPVProvider {
193 
194         /** Get the position-velocity at date.
195          * @param date date at which the position-velocity is desired
196          * @return position-velocity at the specified date
197          */
198         PVCoordinates getRawPV(AbsoluteDate date);
199 
200         /** Get the position at date.
201          * @param date date at which the position is desired
202          * @return position at the specified date
203          */
204         default Vector3D getRawPosition(final AbsoluteDate date) {
205             return getRawPV(date).getPosition();
206         }
207 
208         /** Get the position-velocity at date.
209          * @param date date at which the position-velocity is desired
210          * @param <T> type of the field elements
211          * @return position-velocity at the specified date
212          */
213         <T extends CalculusFieldElement<T>> FieldPVCoordinates<T> getRawPV(FieldAbsoluteDate<T> date);
214 
215         /** Get the position at date.
216          * @param date date at which the position is desired
217          * @param <T> type of the field elements
218          * @return position at the specified date
219          */
220         default <T extends CalculusFieldElement<T>> FieldVector3D<T> getRawPosition(final FieldAbsoluteDate<T> date) {
221             return getRawPV(date).getPosition();
222         }
223 
224     }
225 
226     /** Ephemeris for selected body. */
227     private final GenericTimeStampedCache<PosVelChebyshev> ephemerides;
228 
229     /** Constants defined in the file. */
230     private final AtomicReference<Map<String, Double>> constants;
231 
232     /** Ephemeris type to generate. */
233     private final EphemerisType generateType;
234 
235     /** Ephemeris type to load. */
236     private final EphemerisType loadType;
237 
238     /** Time scales to use when loading data. */
239     private final TimeScales timeScales;
240 
241     /** The GCRF implementation. */
242     private final Frame gcrf;
243 
244     /** Used to retrieve ICRF and EMB frames for building other bodies. */
245     private final CelestialBodies celestialBodies;
246 
247     /** Current file start epoch. */
248     private AbsoluteDate startEpoch;
249 
250     /** Current file final epoch. */
251     private AbsoluteDate finalEpoch;
252 
253     /** Chunks duration (in seconds). */
254     private double maxChunksDuration;
255 
256     /** Current file chunks duration (in seconds). */
257     private TimeOffset chunksDuration;
258 
259     /** Index of the first data for selected body. */
260     private int firstIndex;
261 
262     /** Number of coefficients for selected body. */
263     private int coeffs;
264 
265     /** Number of chunks for the selected body. */
266     private int chunks;
267 
268     /** Number of components contained in the file. */
269     private int components;
270 
271     /** Unit of the position coordinates (as a multiple of meters). */
272     private double positionUnit;
273 
274     /** Time scale of the date coordinates. */
275     private TimeScale timeScale;
276 
277     /** Indicator for binary file endianness. */
278     private boolean bigEndian;
279 
280     /** Create a loader for JPL ephemerides binary files. This constructor uses the {@link
281      * DataContext#getDefault() default data context}.
282      *
283      * @param supportedNames regular expression for supported files names
284      * @param generateType ephemeris type to generate
285      * @see #JPLEphemeridesLoader(String, EphemerisType, DataProvidersManager, TimeScales,
286      * Frame, CelestialBodies)
287      */
288     @DefaultDataContext
289     public JPLEphemeridesLoader(final String supportedNames, final EphemerisType generateType) {
290         this(supportedNames, generateType,
291                 DataContext.getDefault().getDataProvidersManager(),
292                 DataContext.getDefault().getTimeScales(),
293                 DataContext.getDefault().getFrames().getGCRF(),
294                 DataContext.getDefault().getCelestialBodies());
295     }
296 
297     /**
298      * Create a loader for JPL ephemerides binary files.
299      *
300      * <p>Requiring {@code celestialBodies} to be passed to this constructor is
301      * not a great design when this class is used to load the data for
302      * {@code celestialBodies}. But this loader needs to know the ICRF and Earth
303      * Moon Barycenter frames used by {@code celestialBodies} to avoid creating
304      * duplicate frames, as was done in Orekit 13 and before.
305      *
306      * @param supportedNames       regular expression for supported files names
307      * @param generateType         ephemeris type to generate
308      * @param dataProvidersManager provides access to the ephemeris files.
309      * @param timeScales           used to access the TCB and TDB time scales
310      *                             while loading data.
311      * @param gcrf                 Earth centered frame aligned with ICRF.
312      * @param celestialBodies      used to load the ICRF and Earth-Moon
313      *                             Barycenter frames.
314      * @since 14.0
315      */
316     public JPLEphemeridesLoader(final String supportedNames,
317                                 final EphemerisType generateType,
318                                 final DataProvidersManager dataProvidersManager,
319                                 final TimeScales timeScales,
320                                 final Frame gcrf,
321                                 final CelestialBodies celestialBodies) {
322         super(supportedNames, dataProvidersManager);
323 
324         this.timeScales = timeScales;
325         this.gcrf = gcrf;
326         this.celestialBodies = celestialBodies;
327         constants = new AtomicReference<>();
328 
329         this.generateType  = generateType;
330         if (generateType == EphemerisType.SOLAR_SYSTEM_BARYCENTER) {
331             loadType = EphemerisType.EARTH_MOON;
332         } else if (generateType == EphemerisType.EARTH_MOON) {
333             loadType = EphemerisType.MOON;
334         } else {
335             loadType = generateType;
336         }
337 
338         ephemerides = new GenericTimeStampedCache<>(
339                 2, OrekitConfiguration.getCacheSlotsNumber(),
340                 Double.POSITIVE_INFINITY, FIFTY_DAYS.toDouble(),
341                 new EphemerisParser());
342         maxChunksDuration = Double.NaN;
343         chunksDuration    = null;
344 
345     }
346 
347     /** Load celestial body.
348      * @param name name of the celestial body
349      * @return loaded celestial body
350      */
351     @Override
352     public CelestialBody loadCelestialBody(final String name) {
353 
354         final double gm       = getLoadedGravitationalCoefficient(generateType);
355         final IAUPole iauPole = PredefinedIAUPoles
356                 .getIAUPole(generateType, timeScales);
357         final double scale;
358         final Frame definingFrameAlignedWithICRF;
359         final RawPVProvider rawPVProvider;
360         switch (generateType) {
361             case SOLAR_SYSTEM_BARYCENTER : {
362                 scale = -1.0;
363                 definingFrameAlignedWithICRF = celestialBodies
364                         .getBody(CelestialBodyFactory.EARTH_MOON)
365                         .getIcrfAlignedFrame();
366                 rawPVProvider = new EphemerisRawPVProvider();
367                 break;
368             }
369             case EARTH_MOON :
370                 scale         = 1.0 / (1.0 + getLoadedEarthMoonMassRatio());
371                 definingFrameAlignedWithICRF = gcrf;
372                 rawPVProvider = new EphemerisRawPVProvider();
373                 break;
374             case EARTH :
375                 scale         = 1.0;
376                 definingFrameAlignedWithICRF = gcrf;
377                 rawPVProvider = new ZeroRawPVProvider();
378                 break;
379             case MOON :
380                 scale         =  1.0;
381                 definingFrameAlignedWithICRF = gcrf;
382                 rawPVProvider = new EphemerisRawPVProvider();
383                 break;
384             default : {
385                 scale = 1.0;
386                 definingFrameAlignedWithICRF = celestialBodies
387                         .getBody(CelestialBodyFactory.SOLAR_SYSTEM_BARYCENTER)
388                         .getIcrfAlignedFrame();
389                 rawPVProvider = new EphemerisRawPVProvider();
390             }
391         }
392 
393         // build the celestial body
394         return new JPLCelestialBody(name, getSupportedNames(), generateType, rawPVProvider,
395                                     gm, scale, iauPole, definingFrameAlignedWithICRF);
396 
397     }
398 
399     /** Load libration.
400      * @return loaded libration
401      * @since 14.0
402      */
403     public JPLLibration loadLibration() {
404         final RawPVProvider rawPVProvider = new EphemerisRawPVProvider();
405 
406         // build the libration
407         return new JPLLibration(rawPVProvider);
408     }
409 
410     /** Get astronomical unit.
411      * @return astronomical unit in meters
412      */
413     public double getLoadedAstronomicalUnit() {
414         return UnitsConverter.KILOMETRES_TO_METRES.convert(getLoadedConstant(CONSTANT_AU));
415     }
416 
417     /** Get Earth/Moon mass ratio.
418      * @return Earth/Moon mass ratio
419      */
420     public double getLoadedEarthMoonMassRatio() {
421         return getLoadedConstant(CONSTANT_EMRAT);
422     }
423 
424     /** Get the gravitational coefficient of a body.
425      * @param body body for which the gravitational coefficient is requested
426      * @return gravitational coefficient in m³/s²
427      */
428     public double getLoadedGravitationalCoefficient(final EphemerisType body) {
429 
430         // coefficient in au³/day²
431         final double rawGM;
432         switch (body) {
433             case SOLAR_SYSTEM_BARYCENTER :
434                 return getLoadedGravitationalCoefficient(EphemerisType.SUN)        +
435                         getLoadedGravitationalCoefficient(EphemerisType.MERCURY)    +
436                         getLoadedGravitationalCoefficient(EphemerisType.VENUS)      +
437                         getLoadedGravitationalCoefficient(EphemerisType.EARTH_MOON) +
438                         getLoadedGravitationalCoefficient(EphemerisType.MARS)       +
439                         getLoadedGravitationalCoefficient(EphemerisType.JUPITER)    +
440                         getLoadedGravitationalCoefficient(EphemerisType.SATURN)     +
441                         getLoadedGravitationalCoefficient(EphemerisType.URANUS)     +
442                         getLoadedGravitationalCoefficient(EphemerisType.NEPTUNE)    +
443                         getLoadedGravitationalCoefficient(EphemerisType.PLUTO);
444             case SUN :
445                 rawGM = getLoadedConstant("GMS", "GM_Sun");
446                 break;
447             case MERCURY :
448                 rawGM = getLoadedConstant("GM1", "GM_Mer");
449                 break;
450             case VENUS :
451                 rawGM = getLoadedConstant("GM2", "GM_Ven");
452                 break;
453             case EARTH_MOON :
454                 rawGM = getLoadedConstant("GMB", "GM_EMB");
455                 break;
456             case EARTH :
457                 return getLoadedEarthMoonMassRatio() *
458                         getLoadedGravitationalCoefficient(EphemerisType.MOON);
459             case MOON :
460                 return getLoadedGravitationalCoefficient(EphemerisType.EARTH_MOON) /
461                         (1.0 + getLoadedEarthMoonMassRatio());
462             case MARS :
463                 rawGM = getLoadedConstant("GM4", "GM_Mar");
464                 break;
465             case JUPITER :
466                 rawGM = getLoadedConstant("GM5", "GM_Jup");
467                 break;
468             case SATURN :
469                 rawGM = getLoadedConstant("GM6", "GM_Sat");
470                 break;
471             case URANUS :
472                 rawGM = getLoadedConstant("GM7", "GM_Ura");
473                 break;
474             case NEPTUNE :
475                 rawGM = getLoadedConstant("GM8", "GM_Nep");
476                 break;
477             case PLUTO :
478                 rawGM = getLoadedConstant("GM9", "GM_Plu");
479                 break;
480             default :
481                 throw new OrekitInternalError(null);
482         }
483 
484         final double au    = getLoadedAstronomicalUnit();
485         return rawGM * au * au * au / (Constants.JULIAN_DAY * Constants.JULIAN_DAY);
486 
487     }
488 
489     /** Get a constant defined in the ephemerides headers.
490      * <p>Note that since constants are defined in the JPL headers
491      * files, they are available as soon as one file is available, even
492      * if it doesn't match the desired central date. This is because the
493      * header must be parsed before the dates can be checked.</p>
494      * <p>
495      * There are alternate names for constants since for example JPL names are
496      * different from INPOP names (Sun gravity: GMS or GM_Sun, Mars gravity:
497      * GM4 or GM_Mar...).
498      * </p>
499      * @param names alternate names of the constant
500      * @return value of the constant of NaN if the constant is not defined
501      */
502     public double getLoadedConstant(final String... names) {
503 
504         // lazy loading of constants
505         Map<String, Double> map = constants.get();
506         if (map == null) {
507             final ConstantsParser parser = new ConstantsParser();
508             if (!feed(parser)) {
509                 throw new OrekitException(OrekitMessages.NO_JPL_EPHEMERIDES_BINARY_FILES_FOUND);
510             }
511             map = parser.getConstants();
512             constants.compareAndSet(null, map);
513         }
514 
515         for (final String name : names) {
516             if (map.containsKey(name)) {
517                 return map.get(name);
518             }
519         }
520 
521         return Double.NaN;
522 
523     }
524 
525     /** Get the maximal chunks duration.
526      * @return chunks maximal duration in seconds
527      */
528     public double getMaxChunksDuration() {
529         return maxChunksDuration;
530     }
531 
532     /** Parse the first header record.
533      * @param record first header record
534      * @param name name of the file (or zip entry)
535      */
536     private void parseFirstHeaderRecord(final byte[] record, final String name) {
537 
538         // get the ephemerides type
539         final int deNum = extractInt(record, HEADER_EPHEMERIS_TYPE_OFFSET);
540 
541         // as default, 3 polynomial coefficients for the Cartesian coordinates
542         // (x, y, z) are contained in the file, positions are in kilometers
543         // and times are in TDB
544         components   = 3;
545         positionUnit = UnitsConverter.KILOMETRES_TO_METRES.convert(1.0);
546         timeScale    = timeScales.getTDB();
547 
548         if (deNum == INPOP_DE_NUMBER) {
549             // an INPOP file may contain 6 components (including coefficients for the velocity vector)
550             final double format = getLoadedConstant("FORMAT");
551             if (!Double.isNaN(format) && (int) FastMath.IEEEremainder(format, 10) != 1) {
552                 components = 6;
553             }
554 
555             // INPOP files may have their polynomials expressed in AU
556             final double unite = getLoadedConstant("UNITE");
557             if (!Double.isNaN(unite) && (int) unite == 0) {
558                 positionUnit = getLoadedAstronomicalUnit();
559             }
560 
561             // INPOP files may have their times expressed in TCB
562             final double timesc = getLoadedConstant("TIMESC");
563             if (!Double.isNaN(timesc) && (int) timesc == 1) {
564                 timeScale = timeScales.getTCB();
565             }
566 
567         }
568 
569         // extract covered date range
570         startEpoch = extractDate(record, HEADER_START_EPOCH_OFFSET);
571         finalEpoch = extractDate(record, HEADER_END_EPOCH_OFFSET);
572         boolean ok = finalEpoch.compareTo(startEpoch) > 0;
573 
574         if (loadType == EphemerisType.LIBRATION) {
575             // indices of the Chebyshev coefficients for lunar librations
576             firstIndex = extractInt(record, HEADER_LIBRATION_INDICES_OFFSET);
577             coeffs     = extractInt(record, HEADER_LIBRATION_INDICES_OFFSET + 4);
578             chunks     = extractInt(record, HEADER_LIBRATION_INDICES_OFFSET + 8);
579             ok = ok && firstIndex >= 0 && coeffs >= 0 && chunks >= 0;
580             positionUnit = 1.0;
581         }
582         else {
583             // indices of the Chebyshev coefficients for each ephemeris
584             for (int i = 0; i < 12; ++i) {
585                 final int row1 = extractInt(record, HEADER_CHEBISHEV_INDICES_OFFSET     + 12 * i);
586                 final int row2 = extractInt(record, HEADER_CHEBISHEV_INDICES_OFFSET + 4 + 12 * i);
587                 final int row3 = extractInt(record, HEADER_CHEBISHEV_INDICES_OFFSET + 8 + 12 * i);
588                 ok = ok && row1 >= 0 && row2 >= 0 && row3 >= 0;
589                 if (i ==  0 && loadType == EphemerisType.MERCURY    ||
590                         i ==  1 && loadType == EphemerisType.VENUS      ||
591                         i ==  2 && loadType == EphemerisType.EARTH_MOON ||
592                         i ==  3 && loadType == EphemerisType.MARS       ||
593                         i ==  4 && loadType == EphemerisType.JUPITER    ||
594                         i ==  5 && loadType == EphemerisType.SATURN     ||
595                         i ==  6 && loadType == EphemerisType.URANUS     ||
596                         i ==  7 && loadType == EphemerisType.NEPTUNE    ||
597                         i ==  8 && loadType == EphemerisType.PLUTO      ||
598                         i ==  9 && loadType == EphemerisType.MOON       ||
599                         i == 10 && loadType == EphemerisType.SUN) {
600                     firstIndex = row1;
601                     coeffs     = row2;
602                     chunks     = row3;
603                 }
604             }
605         }
606 
607         // compute chunks duration
608         final double timeSpan = extractDouble(record, HEADER_CHUNK_DURATION_OFFSET);
609         ok = ok && timeSpan > 0 && timeSpan < 100;
610         chunksDuration = new TimeOffset(timeSpan).divide(chunks).multiply(86400L);
611         if (Double.isNaN(maxChunksDuration)) {
612             maxChunksDuration = chunksDuration.toDouble();
613         } else {
614             maxChunksDuration = FastMath
615                     .max(maxChunksDuration, chunksDuration.toDouble());
616         }
617 
618         // sanity checks
619         if (!ok) {
620             throw new OrekitException(OrekitMessages.NOT_A_JPL_EPHEMERIDES_BINARY_FILE, name);
621         }
622 
623     }
624 
625     /** Read first header record.
626      * @param input input stream
627      * @param name name of the file (or zip entry)
628      * @return record record where to put bytes
629      * @exception IOException if a read error occurs
630      */
631     private byte[] readFirstRecord(final InputStream input, final String name)
632         throws IOException {
633 
634         // read first part of record, up to the ephemeris type
635         final byte[] firstPart = new byte[HEADER_RECORD_SIZE_OFFSET + 4];
636         if (!readInRecord(input, firstPart, 0)) {
637             throw new OrekitException(OrekitMessages.UNABLE_TO_READ_JPL_HEADER, name);
638         }
639 
640         // detect the endian format
641         detectEndianess(firstPart);
642 
643         // get the ephemerides type
644         final int deNum = extractInt(firstPart, HEADER_EPHEMERIS_TYPE_OFFSET);
645 
646         // the record size for this file
647         final int recordSize;
648 
649         if (deNum == INPOP_DE_NUMBER) {
650             // INPOP files have an extended DE format, which includes also the record size
651             recordSize = extractInt(firstPart, HEADER_RECORD_SIZE_OFFSET) << 3;
652         } else {
653             // compute the record size for original JPL files
654             recordSize = computeRecordSize(firstPart, name);
655         }
656 
657         if (recordSize <= 0) {
658             throw new OrekitException(OrekitMessages.UNABLE_TO_READ_JPL_HEADER, name);
659         }
660 
661         // build a record with the proper size and finish read of the first complete record
662         final int start = firstPart.length;
663         final byte[] record = new byte[recordSize];
664         System.arraycopy(firstPart, 0, record, 0, firstPart.length);
665         if (!readInRecord(input, record, start)) {
666             throw new OrekitException(OrekitMessages.UNABLE_TO_READ_JPL_HEADER, name);
667         }
668 
669         return record;
670 
671     }
672 
673     /** Parse constants from first two header records.
674      * @param first first header record
675      * @param second second header record
676      * @return map of parsed constants
677      */
678     private Map<String, Double> parseConstants(final byte[] first, final byte[] second) {
679 
680         final Map<String, Double> map = new HashMap<>();
681 
682         for (int i = 0; i < CONSTANTS_MAX_NUMBER; ++i) {
683             // Note: for extracting the strings from the binary file, it makes no difference
684             //       if the file is stored in big-endian or little-endian notation
685             final String constantName = extractString(first, HEADER_CONSTANTS_NAMES_OFFSET + i * 6, 6);
686             if (constantName.length() == 0) {
687                 // no more constants to read
688                 break;
689             }
690             final double constantValue = extractDouble(second, HEADER_CONSTANTS_VALUES_OFFSET + 8 * i);
691             map.put(constantName, constantValue);
692         }
693 
694         // INPOP files do not have constants for AU and EMRAT, thus extract them from
695         // the header record and create a constant for them to be consistent with JPL files
696         if (!map.containsKey(CONSTANT_AU)) {
697             map.put(CONSTANT_AU, extractDouble(first, HEADER_ASTRONOMICAL_UNIT_OFFSET));
698         }
699 
700         if (!map.containsKey(CONSTANT_EMRAT)) {
701             map.put(CONSTANT_EMRAT, extractDouble(first, HEADER_EM_RATIO_OFFSET));
702         }
703 
704         return map;
705 
706     }
707 
708     /** Read bytes into the current record array.
709      * @param input input stream
710      * @param record record where to put bytes
711      * @param start start index where to put bytes
712      * @return true if record has been filled up
713      * @exception IOException if a read error occurs
714      */
715     private boolean readInRecord(final InputStream input, final byte[] record, final int start)
716         throws IOException {
717         int index = start;
718         while (index != record.length) {
719             final int n = input.read(record, index, record.length - index);
720             if (n < 0) {
721                 return false;
722             }
723             index += n;
724         }
725         return true;
726     }
727 
728     /** Detect whether the JPL ephemerides file is stored in big-endian or
729      * little-endian notation.
730      * @param record the array containing the binary JPL header
731      */
732     private void detectEndianess(final byte[] record) {
733 
734         // default to big-endian
735         bigEndian = true;
736 
737         // first try to read the DE number in big-endian format
738         // the number is stored as unsigned int, so we have to convert it properly
739         final long deNum = extractInt(record, HEADER_EPHEMERIS_TYPE_OFFSET) & 0xffffffffL;
740 
741         // simple heuristic: if the read value is larger than half the range of an integer
742         //                   assume the file is in little-endian format
743         if (deNum > (1 << 15)) {
744             bigEndian = false;
745         }
746 
747     }
748 
749     /** Calculate the record size of a JPL ephemerides file.
750      * @param record the byte array containing the header record
751      * @param name the name of the data file
752      * @return the record size for this file
753      */
754     private int computeRecordSize(final byte[] record, final String name) {
755 
756         int recordSize = 0;
757         boolean ok = true;
758         // JPL files always have 3 position components
759         final int nComp = 3;
760 
761         // iterate over the coefficient ptr array and sum up the record size
762         // the coeffPtr array has the dimensions [12][nComp]
763         for (int j = 0; j < 12; j++) {
764             final int nCompCur = (j == 11) ? 2 : nComp;
765 
766             // Note: the array element coeffPtr[j][0] is not needed for the calculation
767             final int idx = HEADER_CHEBISHEV_INDICES_OFFSET + j * nComp * 4;
768             final int coeffPtr1 = extractInt(record, idx + 4);
769             final int coeffPtr2 = extractInt(record, idx + 8);
770 
771             // sanity checks
772             ok = ok && (coeffPtr1 >= 0 || coeffPtr2 >= 0);
773 
774             recordSize += coeffPtr1 * coeffPtr2 * nCompCur;
775         }
776 
777         // the libration ptr array has the dimension [3]
778         // Note: the array element libratPtr[0] is not needed for the calculation
779         final int libratPtr1 = extractInt(record, HEADER_LIBRATION_INDICES_OFFSET + 4);
780         final int libratPtr2 = extractInt(record, HEADER_LIBRATION_INDICES_OFFSET + 8);
781 
782         // sanity checks
783         ok = ok && (libratPtr1 >= 0 || libratPtr2 >= 0);
784 
785         recordSize += libratPtr1 * libratPtr2 * nComp + 2;
786         recordSize <<= 3;
787 
788         if (!ok || recordSize <= 0) {
789             throw new OrekitException(OrekitMessages.NOT_A_JPL_EPHEMERIDES_BINARY_FILE, name);
790         }
791 
792         return recordSize;
793 
794     }
795 
796     /** Extract a date from a record.
797      * @param record record to parse
798      * @param offset offset of the double within the record
799      * @return extracted date
800      */
801     private AbsoluteDate extractDate(final byte[] record, final int offset) {
802 
803         final double t = extractDouble(record, offset);
804         int    jDay    = (int) FastMath.floor(t);
805         double seconds = (t + 0.5 - jDay) * Constants.JULIAN_DAY;
806         if (seconds >= Constants.JULIAN_DAY) {
807             ++jDay;
808             seconds -= Constants.JULIAN_DAY;
809         }
810         return new AbsoluteDate(new DateComponents(DateComponents.JULIAN_EPOCH, jDay),
811                                 new TimeComponents(seconds), timeScale);
812     }
813 
814     /** Extract a double from a record.
815      * <p>Double numbers are stored according to IEEE 754 standard, with
816      * most significant byte first.</p>
817      * @param record record to parse
818      * @param offset offset of the double within the record
819      * @return extracted double
820      */
821     private double extractDouble(final byte[] record, final int offset) {
822         final long l8 = ((long) record[offset + 0]) & 0xffl;
823         final long l7 = ((long) record[offset + 1]) & 0xffl;
824         final long l6 = ((long) record[offset + 2]) & 0xffl;
825         final long l5 = ((long) record[offset + 3]) & 0xffl;
826         final long l4 = ((long) record[offset + 4]) & 0xffl;
827         final long l3 = ((long) record[offset + 5]) & 0xffl;
828         final long l2 = ((long) record[offset + 6]) & 0xffl;
829         final long l1 = ((long) record[offset + 7]) & 0xffl;
830         final long l;
831         if (bigEndian) {
832             l = (l8 << 56) | (l7 << 48) | (l6 << 40) | (l5 << 32) |
833                 (l4 << 24) | (l3 << 16) | (l2 <<  8) | l1;
834         } else {
835             l = (l1 << 56) | (l2 << 48) | (l3 << 40) | (l4 << 32) |
836                 (l5 << 24) | (l6 << 16) | (l7 <<  8) | l8;
837         }
838         return Double.longBitsToDouble(l);
839     }
840 
841     /** Extract an int from a record.
842      * @param record record to parse
843      * @param offset offset of the double within the record
844      * @return extracted int
845      */
846     private int extractInt(final byte[] record, final int offset) {
847         final int l4 = ((int) record[offset + 0]) & 0xff;
848         final int l3 = ((int) record[offset + 1]) & 0xff;
849         final int l2 = ((int) record[offset + 2]) & 0xff;
850         final int l1 = ((int) record[offset + 3]) & 0xff;
851 
852         if (bigEndian) {
853             return (l4 << 24) | (l3 << 16) | (l2 <<  8) | l1;
854         } else {
855             return (l1 << 24) | (l2 << 16) | (l3 <<  8) | l4;
856         }
857     }
858 
859     /** Extract a String from a record.
860      * @param record record to parse
861      * @param offset offset of the string within the record
862      * @param length maximal length of the string
863      * @return extracted string, with whitespace characters stripped
864      */
865     private String extractString(final byte[] record, final int offset, final int length) {
866         return new String(record, offset, length, StandardCharsets.US_ASCII).trim();
867     }
868 
869     /** Local parser for header constants. */
870     private class ConstantsParser implements DataLoader {
871 
872         /** Local constants map. */
873         private Map<String, Double> localConstants;
874 
875        /** Get the local constants map.
876          * @return local constants map
877          */
878         public Map<String, Double> getConstants() {
879             return localConstants;
880         }
881 
882         /** {@inheritDoc} */
883         public boolean stillAcceptsData() {
884             return localConstants == null;
885         }
886 
887         /** {@inheritDoc} */
888         public void loadData(final InputStream input, final String name)
889             throws IOException, ParseException, OrekitException {
890 
891             // read first header record
892             final byte[] first = readFirstRecord(input, name);
893 
894             // the second record contains the values of the constants used for least-square filtering
895             final byte[] second = new byte[first.length];
896             if (!readInRecord(input, second, 0)) {
897                 throw new OrekitException(OrekitMessages.UNABLE_TO_READ_JPL_HEADER, name);
898             }
899 
900             localConstants = parseConstants(first, second);
901 
902         }
903 
904     }
905 
906     /** Local parser for Chebyshev polynomials. */
907     private class EphemerisParser implements DataLoader, TimeStampedGenerator<PosVelChebyshev> {
908 
909         /** Set of Chebyshev polynomials read. */
910         private final SortedSet<PosVelChebyshev> entries;
911 
912         /** Start of range we are interested in. */
913         private AbsoluteDate start;
914 
915         /** End of range we are interested in. */
916         private AbsoluteDate end;
917 
918         /** Simple constructor.
919          */
920         EphemerisParser() {
921             entries = new TreeSet<>(new ChronologicalComparator());
922         }
923 
924         /** {@inheritDoc} */
925         public List<PosVelChebyshev> generate(final AbsoluteDate existingDate, final AbsoluteDate date) {
926             try {
927 
928                 // prepare reading
929                 entries.clear();
930                 if (existingDate == null) {
931                     // we want ephemeris data for the first time, set up an arbitrary first range
932                     start = date.shiftedBy(FIFTY_DAYS.negate());
933                     end   = date.shiftedBy(FIFTY_DAYS);
934                 } else if (existingDate.compareTo(date) <= 0) {
935                     // we want to extend an existing range towards future dates
936                     start = existingDate;
937                     end   = date;
938                 } else {
939                     // we want to extend an existing range towards past dates
940                     start = date;
941                     end   = existingDate;
942                 }
943 
944                 // get new entries in the specified data range
945                 if (!feed(this)) {
946                     throw new OrekitException(OrekitMessages.NO_JPL_EPHEMERIDES_BINARY_FILES_FOUND);
947                 }
948 
949                 return new ArrayList<>(entries);
950 
951             } catch (OrekitException oe) {
952                 throw new TimeStampedCacheException(oe);
953             }
954         }
955 
956         /** {@inheritDoc} */
957         public boolean stillAcceptsData() {
958 
959             // special case for Earth: we do not really load any ephemeris data
960             if (generateType == EphemerisType.EARTH) {
961                 return false;
962             }
963 
964             // we have to look for data in all available ephemerides files as there may be
965             // data overlaps that result in incomplete data
966             if (entries.isEmpty()) {
967                 return true;
968             } else {
969                 // if the requested range is already filled, we do not need to look further
970                 return !(entries.first().getDate().compareTo(start) < 0 &&
971                          entries.last().getDate().compareTo(end)    > 0);
972             }
973 
974         }
975 
976         /** {@inheritDoc} */
977         public void loadData(final InputStream input, final String name)
978             throws IOException {
979 
980             // read first header record
981             final byte[] first = readFirstRecord(input, name);
982 
983             // the second record contains the values of the constants used for least-square filtering
984             final byte[] second = new byte[first.length];
985             if (!readInRecord(input, second, 0)) {
986                 throw new OrekitException(OrekitMessages.UNABLE_TO_READ_JPL_HEADER, name);
987             }
988 
989             if (constants.get() == null) {
990                 constants.compareAndSet(null, parseConstants(first, second));
991             }
992 
993             // check astronomical unit consistency
994             final double au = 1000 * extractDouble(first, HEADER_ASTRONOMICAL_UNIT_OFFSET);
995             if (au < 1.4e11 || au > 1.6e11) {
996                 throw new OrekitException(OrekitMessages.NOT_A_JPL_EPHEMERIDES_BINARY_FILE, name);
997             }
998             if (FastMath.abs(getLoadedAstronomicalUnit() - au) >= 10.0) {
999                 throw new OrekitException(OrekitMessages.INCONSISTENT_ASTRONOMICAL_UNIT_IN_FILES,
1000                                           getLoadedAstronomicalUnit(), au);
1001             }
1002 
1003             // check Earth-Moon mass ratio consistency
1004             final double emRat = extractDouble(first, HEADER_EM_RATIO_OFFSET);
1005             if (emRat < 80 || emRat > 82) {
1006                 throw new OrekitException(OrekitMessages.NOT_A_JPL_EPHEMERIDES_BINARY_FILE, name);
1007             }
1008             if (FastMath.abs(getLoadedEarthMoonMassRatio() - emRat) >= 1.0e-5) {
1009                 throw new OrekitException(OrekitMessages.INCONSISTENT_EARTH_MOON_RATIO_IN_FILES,
1010                                           getLoadedEarthMoonMassRatio(), emRat);
1011             }
1012 
1013             // parse first header record
1014             parseFirstHeaderRecord(first, name);
1015 
1016             if (startEpoch.compareTo(end) < 0 && finalEpoch.compareTo(start) > 0) {
1017                 // this file contains data in the range we are looking for, read it
1018                 final byte[] record = new byte[first.length];
1019                 while (readInRecord(input, record, 0)) {
1020                     final AbsoluteDate rangeStart = parseDataRecord(record);
1021                     if (rangeStart.compareTo(end) > 0) {
1022                         // we have already exceeded the range we were interested in,
1023                         // we interrupt parsing here
1024                         return;
1025                     }
1026                 }
1027             }
1028 
1029         }
1030 
1031         /** Parse regular ephemeris record.
1032          * @param record record to parse
1033          * @return date of the last parsed chunk
1034          */
1035         private AbsoluteDate parseDataRecord(final byte[] record) {
1036 
1037             // extract time range covered by the record
1038             final AbsoluteDate rangeStart = extractDate(record, DATA_START_RANGE_OFFSET);
1039             if (rangeStart.compareTo(startEpoch) < 0) {
1040                 throw new OrekitException(OrekitMessages.OUT_OF_RANGE_EPHEMERIDES_DATE_BEFORE,
1041                         rangeStart, startEpoch, finalEpoch, startEpoch.durationFrom(rangeStart));
1042             }
1043 
1044             final AbsoluteDate rangeEnd   = extractDate(record, DATE_END_RANGE_OFFSET);
1045             if (rangeEnd.compareTo(finalEpoch) > 0) {
1046                 throw new OrekitException(OrekitMessages.OUT_OF_RANGE_EPHEMERIDES_DATE_AFTER,
1047                         rangeEnd, startEpoch, finalEpoch, rangeEnd.durationFrom(finalEpoch));
1048             }
1049 
1050             if (rangeStart.compareTo(end) > 0 || rangeEnd.compareTo(start) < 0) {
1051                 // we are not interested in this record, don't parse it
1052                 return rangeEnd;
1053             }
1054 
1055             // loop over chunks inside the time range
1056             AbsoluteDate chunkEnd = rangeStart;
1057             final int nbChunks    = chunks;
1058             final int nbCoeffs    = coeffs;
1059             final int first       = firstIndex;
1060             final TimeOffset duration = chunksDuration;
1061             for (int i = 0; i < nbChunks; ++i) {
1062 
1063                 // set up chunk validity range
1064                 final AbsoluteDate chunkStart = chunkEnd;
1065                 if (i == nbChunks - 1) {
1066                     chunkEnd = rangeEnd;
1067                 } else {
1068                     chunkEnd = new AbsoluteDate(
1069                             rangeStart,
1070                             duration.multiply(i + 1),
1071                             timeScale);
1072                 }
1073 
1074                 // extract Chebyshev coefficients for the selected body
1075                 // and convert them from kilometers to meters
1076                 final double[] xCoeffs = new double[nbCoeffs];
1077                 final double[] yCoeffs = new double[nbCoeffs];
1078                 final double[] zCoeffs = new double[nbCoeffs];
1079 
1080                 for (int k = 0; k < nbCoeffs; ++k) {
1081                     // by now, only use the position components
1082                     // if there are also velocity components contained in the file, ignore them
1083                     final int index = first + components * i * nbCoeffs + k - 1;
1084                     xCoeffs[k] = positionUnit * extractDouble(record, 8 * index);
1085                     yCoeffs[k] = positionUnit * extractDouble(record, 8 * (index +  nbCoeffs));
1086                     zCoeffs[k] = positionUnit * extractDouble(record, 8 * (index + 2 * nbCoeffs));
1087                 }
1088 
1089                 // build the position-velocity model for current chunk
1090                 entries.add(new PosVelChebyshev(chunkStart, timeScale,
1091                         duration.toDouble(), xCoeffs, yCoeffs, zCoeffs));
1092 
1093             }
1094 
1095             return rangeStart;
1096 
1097         }
1098 
1099     }
1100 
1101     /** Raw position-velocity provider using ephemeris. */
1102     private class EphemerisRawPVProvider implements RawPVProvider {
1103 
1104 
1105         /** Retrieve correct Chebyshev polynomial for given date.
1106          * @param date date
1107          * @return PosVelChebyshev Chebyshev polynomial class for position-velocity-acceleration
1108          */
1109         private PosVelChebyshev getChebyshev(final AbsoluteDate date) {
1110             PosVelChebyshev chebyshev;
1111             try {
1112                 chebyshev = ephemerides.getNeighbors(date).findFirst().get();
1113             } catch (TimeStampedCacheException tce) {
1114                 // we cannot bracket the date, check if the last available chunk covers the specified date
1115                 chebyshev = ephemerides.getLatest();
1116                 if (!chebyshev.inRange(date)) {
1117                     // we were not able to recover from the error, the date is too far
1118                     throw tce;
1119                 }
1120             }
1121             return chebyshev;
1122         }
1123 
1124         /** {@inheritDoc} */
1125         public PVCoordinates getRawPV(final AbsoluteDate date) {
1126 
1127             // get raw PV from Chebyshev polynomials
1128             final PosVelChebyshev chebyshev = getChebyshev(date);
1129 
1130             // evaluate the Chebyshev polynomials
1131             return chebyshev.getPositionVelocityAcceleration(date);
1132 
1133         }
1134 
1135         /** {@inheritDoc} */
1136         public <T extends CalculusFieldElement<T>> FieldPVCoordinates<T> getRawPV(final FieldAbsoluteDate<T> date) {
1137 
1138             // get raw PV from Chebyshev polynomials
1139             final PosVelChebyshev chebyshev = getChebyshev(date.toAbsoluteDate());
1140 
1141             // evaluate the Chebyshev polynomials
1142             return chebyshev.getPositionVelocityAcceleration(date);
1143 
1144         }
1145 
1146         /** {@inheritDoc} */
1147         @Override
1148         public Vector3D getRawPosition(final AbsoluteDate date) {
1149             // get raw PV from Chebyshev polynomials
1150             final PosVelChebyshev chebyshev = getChebyshev(date);
1151 
1152             // evaluate the Chebyshev polynomials
1153             return chebyshev.getPosition(date);
1154         }
1155 
1156         /** {@inheritDoc} */
1157         @Override
1158         public <T extends CalculusFieldElement<T>> FieldVector3D<T> getRawPosition(final FieldAbsoluteDate<T> date) {
1159             // get raw PV from Chebyshev polynomials
1160             final PosVelChebyshev chebyshev = getChebyshev(date.toAbsoluteDate());
1161 
1162             // evaluate the Chebyshev polynomials
1163             return chebyshev.getPosition(date);
1164         }
1165 
1166     }
1167 
1168     /** Raw position-velocity provider providing always zero. */
1169     static class ZeroRawPVProvider implements RawPVProvider {
1170 
1171         /** {@inheritDoc} */
1172         public PVCoordinates getRawPV(final AbsoluteDate date) {
1173             return PVCoordinates.ZERO;
1174         }
1175 
1176         /** {@inheritDoc} */
1177         public <T extends CalculusFieldElement<T>> FieldPVCoordinates<T> getRawPV(final FieldAbsoluteDate<T> date) {
1178             return FieldPVCoordinates.getZero(date.getField());
1179         }
1180 
1181     }
1182 
1183 }