VersionedITRFProvider.java

  1. /* Copyright 2002-2024 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.frames;

  18. import java.io.Serializable;
  19. import java.util.concurrent.atomic.AtomicReference;

  20. import org.hipparchus.CalculusFieldElement;
  21. import org.orekit.annotation.DefaultDataContext;
  22. import org.orekit.data.DataContext;
  23. import org.orekit.time.AbsoluteDate;
  24. import org.orekit.time.FieldAbsoluteDate;
  25. import org.orekit.time.TimeScale;


  26. /** Provider for a specific version of International Terrestrial Reference Frame.
  27.  * <p>
  28.  * This class ensure the ITRF it defines is a specific version, regardless of
  29.  * the version of the underlying {@link EOPEntry Earth Orientation Parameters}.
  30.  * </p>
  31.  * @author Luc Maisonobe
  32.  * @since 9.2
  33.  */
  34. class VersionedITRFProvider implements EOPBasedTransformProvider {

  35.     /** Serializable UID. */
  36.     private static final long serialVersionUID = 20180403L;

  37.     /** ITRF version this provider should generate. */
  38.     private final ITRFVersion version;

  39.     /** Raw ITRF provider. */
  40.     private final ITRFProvider rawProvider;

  41.     /** Converter between different ITRF versions. */
  42.     private final AtomicReference<ITRFVersion.Converter> converter;

  43.     /** TT time scale. */
  44.     private final TimeScale tt;

  45.     /** Simple constructor.
  46.      * @param version ITRF version this provider should generate
  47.      * @param rawProvider raw ITRF provider
  48.      * @param tt TT time scale.
  49.      */
  50.     VersionedITRFProvider(final ITRFVersion version,
  51.                           final ITRFProvider rawProvider,
  52.                           final TimeScale tt) {
  53.         this.version     = version;
  54.         this.rawProvider = rawProvider;
  55.         this.converter   = new AtomicReference<>();
  56.         this.tt = tt;
  57.     }

  58.     /** Get the ITRF version.
  59.      * @return ITRF version
  60.      */
  61.     public ITRFVersion getITRFVersion() {
  62.         return version;
  63.     }

  64.     /** {@inheritDoc} */
  65.     @Override
  66.     public EOPHistory getEOPHistory() {
  67.         return rawProvider.getEOPHistory();
  68.     }

  69.     /** {@inheritDoc} */
  70.     @Override
  71.     public VersionedITRFProvider getNonInterpolatingProvider() {
  72.         return new VersionedITRFProvider(version, rawProvider.getNonInterpolatingProvider(), tt);
  73.     }

  74.     /** {@inheritDoc} */
  75.     @Override
  76.     public Transform getTransform(final AbsoluteDate date) {

  77.         // get the transform from the current EOP
  78.         final Transform rawTransform = rawProvider.getTransform(date);

  79.         // add the conversion layer
  80.         final ITRFVersion.Converter converterForDate = getConverter(date);
  81.         if (converterForDate == null) {
  82.             return rawTransform;
  83.         } else {
  84.             return new Transform(date, rawTransform, converterForDate.getTransform(date));
  85.         }

  86.     }

  87.     /** {@inheritDoc} */
  88.     @Override
  89.     public KinematicTransform getKinematicTransform(final AbsoluteDate date) {

  90.         // get the transform from the current EOP
  91.         final KinematicTransform rawTransform = rawProvider.getKinematicTransform(date);

  92.         // add the conversion layer
  93.         final ITRFVersion.Converter converterForDate = getConverter(date);
  94.         if (converterForDate == null) {
  95.             return rawTransform;
  96.         } else {
  97.             return KinematicTransform.compose(date, rawTransform, converterForDate.getKinematicTransform(date));
  98.         }

  99.     }

  100.     /** {@inheritDoc} */
  101.     @Override
  102.     public StaticTransform getStaticTransform(final AbsoluteDate date) {

  103.         // get the transform from the current EOP
  104.         final StaticTransform rawTransform = rawProvider.getStaticTransform(date);

  105.         // add the conversion layer
  106.         final ITRFVersion.Converter converterForDate = getConverter(date);
  107.         if (converterForDate == null) {
  108.             return rawTransform;
  109.         } else {
  110.             return StaticTransform.compose(
  111.                     date,
  112.                     rawTransform,
  113.                     converterForDate.getStaticTransform(date));
  114.         }

  115.     }

  116.     /** {@inheritDoc} */
  117.     @Override
  118.     public <T extends CalculusFieldElement<T>> FieldTransform<T> getTransform(final FieldAbsoluteDate<T> date) {

  119.         // get the transform from the current EOP
  120.         final FieldTransform<T> rawTransform = rawProvider.getTransform(date);

  121.         // add the conversion layer
  122.         final ITRFVersion.Converter converterForDate = getConverter(date.toAbsoluteDate());
  123.         if (converterForDate == null) {
  124.             return rawTransform;
  125.         } else {
  126.             return new FieldTransform<>(date, rawTransform, converterForDate.getTransform(date));
  127.         }

  128.     }

  129.     /** {@inheritDoc} */
  130.     @Override
  131.     public <T extends CalculusFieldElement<T>> FieldKinematicTransform<T> getKinematicTransform(final FieldAbsoluteDate<T> date) {

  132.         // get the transform from the current EOP
  133.         final FieldKinematicTransform<T> rawTransform = rawProvider.getKinematicTransform(date);

  134.         // add the conversion layer
  135.         final ITRFVersion.Converter converterForDate = getConverter(date.toAbsoluteDate());
  136.         if (converterForDate == null) {
  137.             return rawTransform;
  138.         } else {
  139.             return FieldKinematicTransform.compose(date, rawTransform, converterForDate.getKinematicTransform(date));
  140.         }

  141.     }

  142.     /** {@inheritDoc} */
  143.     @Override
  144.     public <T extends CalculusFieldElement<T>> FieldStaticTransform<T> getStaticTransform(final FieldAbsoluteDate<T> date) {

  145.         // get the transform from the current EOP
  146.         final FieldStaticTransform<T> rawTransform = rawProvider.getStaticTransform(date);

  147.         // add the conversion layer
  148.         final ITRFVersion.Converter converterForDate = getConverter(date.toAbsoluteDate());
  149.         if (converterForDate == null) {
  150.             return rawTransform;
  151.         } else {
  152.             return FieldStaticTransform.compose(
  153.                     date,
  154.                     rawTransform,
  155.                     converterForDate.getStaticTransform(date));
  156.         }

  157.     }

  158.     /** Get a converter for the date.
  159.      * @param date date to check
  160.      * @return converter that should be applied for this date, or null
  161.      * if no converter is needed
  162.      */
  163.     private ITRFVersion.Converter getConverter(final AbsoluteDate date) {

  164.         // check if the current EOP already provides the version we want
  165.         final ITRFVersion rawVersion = getEOPHistory().getITRFVersion(date);
  166.         if (rawVersion == version) {
  167.             // we already have what we need
  168.             return null;
  169.         }

  170.         final ITRFVersion.Converter existing = converter.get();
  171.         if (existing != null && existing.getOrigin() == rawVersion) {
  172.             // the current converter can handle this date
  173.             return existing;
  174.         }

  175.         // we need to create a new converter from raw version to desired version
  176.         final ITRFVersion.Converter newConverter =
  177.                 ITRFVersion.getConverter(rawVersion, version, tt);
  178.         converter.compareAndSet(null, newConverter);
  179.         return newConverter;

  180.     }

  181.     /** Replace the instance with a data transfer object for serialization.
  182.      * @return data transfer object that will be serialized
  183.      */
  184.     @DefaultDataContext
  185.     private Object writeReplace() {
  186.         return new DataTransferObject(version, rawProvider);
  187.     }

  188.     /** Internal class used only for serialization. */
  189.     @DefaultDataContext
  190.     private static class DataTransferObject implements Serializable {

  191.         /** Serializable UID. */
  192.         private static final long serialVersionUID = 20180403L;

  193.         /** ITRF version this provider should generate. */
  194.         private final ITRFVersion version;

  195.         /** Raw ITRF provider. */
  196.         private final ITRFProvider rawProvider;

  197.         /** Simple constructor.
  198.          * @param version ITRF version this provider should generate
  199.          * @param rawProvider raw ITRF provider
  200.          */
  201.         DataTransferObject(final ITRFVersion version, final ITRFProvider rawProvider) {
  202.             this.version     = version;
  203.             this.rawProvider = rawProvider;
  204.         }

  205.         /** Replace the deserialized data transfer object with a {@link VersionedITRFProvider}.
  206.          * @return replacement {@link VersionedITRFProvider}
  207.          */
  208.         private Object readResolve() {
  209.             return new VersionedITRFProvider(version, rawProvider,
  210.                     DataContext.getDefault().getTimeScales().getTT());
  211.         }

  212.     }

  213. }