AggregateBoundedAttitudeProvider.java

  1. /* Copyright 2002-2022 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.attitudes;

  18. import java.util.Collection;
  19. import java.util.Map.Entry;
  20. import java.util.NavigableMap;
  21. import java.util.TreeMap;

  22. import org.hipparchus.CalculusFieldElement;
  23. import org.orekit.errors.OrekitException;
  24. import org.orekit.errors.OrekitMessages;
  25. import org.orekit.frames.Frame;
  26. import org.orekit.time.AbsoluteDate;
  27. import org.orekit.time.FieldAbsoluteDate;
  28. import org.orekit.utils.FieldPVCoordinatesProvider;
  29. import org.orekit.utils.PVCoordinatesProvider;

  30. /**
  31.  * A {@link BoundedAttitudeProvider} that covers a larger time span from several constituent
  32.  * attitude providers that cover shorter time spans.
  33.  *
  34.  * @author Bryan Cazabonne
  35.  * @since 10.3
  36.  */
  37. public class AggregateBoundedAttitudeProvider implements BoundedAttitudeProvider {

  38.     /** Constituent attitude provider. */
  39.     private final NavigableMap<AbsoluteDate, BoundedAttitudeProvider> providers;

  40.     /**
  41.      * Constructor.
  42.      * @param providers attitude providers that provide the backing data for this instance.
  43.      *                  There must be at least one attitude provider in the collection.
  44.      *                  If there are gaps between the {@link BoundedAttitudeProvider#getMaxDate()}
  45.      *                  of one attitude provider and the {@link BoundedAttitudeProvider#getMinDate()}
  46.      *                  of the next attitude provider an exception may be thrown by any method of
  47.      *                  this class at any time. If there are overlaps between the the {@link
  48.      *                  BoundedAttitudeProvider#getMaxDate()} of one attitude provider and the {@link
  49.      *                  BoundedAttitudeProvider#getMinDate()} of the next attitude provider then the
  50.      *                  attitude provider with the latest {@link BoundedAttitudeProvider#getMinDate()}
  51.      *                  is used.
  52.      */
  53.     public AggregateBoundedAttitudeProvider(final Collection<? extends BoundedAttitudeProvider> providers) {

  54.         // Check if the collection is empty
  55.         if (providers.isEmpty()) {
  56.             throw new OrekitException(OrekitMessages.NOT_ENOUGH_ATTITUDE_PROVIDERS);
  57.         }

  58.         // Initialize map
  59.         this.providers = new TreeMap<>();

  60.         // Loop on providers
  61.         for (final BoundedAttitudeProvider provider : providers) {
  62.             // Fill collection
  63.             this.providers.put(provider.getMinDate(), provider);
  64.         }

  65.     }

  66.     /** {@inheritDoc} */
  67.     @Override
  68.     public Attitude getAttitude(final PVCoordinatesProvider pvProv, final AbsoluteDate date,
  69.                                 final Frame frame) {

  70.         // Get the attitude provider for the given date
  71.         final BoundedAttitudeProvider provider = getAttitudeProvider(date);

  72.         // Build attitude
  73.         return provider.getAttitude(pvProv, date, frame);

  74.     }

  75.     /** {@inheritDoc} */
  76.     @Override
  77.     public <T extends CalculusFieldElement<T>> FieldAttitude<T> getAttitude(final FieldPVCoordinatesProvider<T> pvProv,
  78.                                                                         final FieldAbsoluteDate<T> date, final Frame frame) {

  79.         // Get the attitude provider for the given date
  80.         final BoundedAttitudeProvider provider = getAttitudeProvider(date.toAbsoluteDate());

  81.         // Build attitude
  82.         return provider.getAttitude(pvProv, date, frame);

  83.     }

  84.     /** {@inheritDoc} */
  85.     @Override
  86.     public AbsoluteDate getMinDate() {
  87.         return providers.firstEntry().getValue().getMinDate();
  88.     }

  89.     /** {@inheritDoc} */
  90.     @Override
  91.     public AbsoluteDate getMaxDate() {
  92.         return providers.lastEntry().getValue().getMaxDate();
  93.     }

  94.     /**
  95.      * Get the attitude provider to use for the given date.
  96.      * @param date of query
  97.      * @return attitude provider to use on date.
  98.      */
  99.     private BoundedAttitudeProvider getAttitudeProvider(final AbsoluteDate date) {
  100.         final Entry<AbsoluteDate, BoundedAttitudeProvider> attitudeEntry = providers.floorEntry(date);
  101.         if (attitudeEntry != null) {
  102.             return attitudeEntry.getValue();
  103.         } else {
  104.             // Let the first attitude provider throw the exception
  105.             return providers.firstEntry().getValue();
  106.         }
  107.     }

  108. }