AbsoluteDateArrayHandling.java

  1. /* Copyright 2013-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.rugged.utils;

  18. import org.hipparchus.exception.LocalizedCoreFormats;
  19. import org.orekit.errors.OrekitException;
  20. import org.orekit.time.AbsoluteDate;

  21. /** AbsoluteDateArrayHandling consist of additions to AbsoluteDate to handle arrays.
  22.  * @author Melina Vanel
  23.  */
  24. public class AbsoluteDateArrayHandling {

  25.     /** Dates array on which we want to apply time shift or compute duration. */
  26.     private final AbsoluteDate[] dates;

  27.     /** Simple constructor.
  28.      * @param dates is an array of absolute dates on which we want to apply time shift or
  29.      * compute duration
  30.      */
  31.     public AbsoluteDateArrayHandling(final AbsoluteDate[] dates) {
  32.         this.dates = dates.clone();
  33.     }

  34.     /** Get instance dates array.
  35.      * @return dates array
  36.      */
  37.     public AbsoluteDate[] getDates() {
  38.         return this.dates.clone();
  39.     }

  40.     /** Get time-shifted dates for several dates or several time shifts.
  41.      * If instance dates = [date1, date2, ..., daten] and argument
  42.      * dts = [dts1, dts2, ..., dtsn] then this function will return a matrix
  43.      * [[date1 shiftedby dts1, date1 shiftedBy dts2, ..., date1 shiftedBy dtsn],
  44.      * [date2 shiftedby dts1, date2 shiftedBy dts2, ..., date2 shiftedBy dtsn],
  45.      * [...]
  46.      * [daten shiftedby dts1, daten shiftedBy dts2, ..., date1 shiftedBy dtsn]].
  47.      * If ones want to apply only 1 time shift corresponding to 1 date see
  48.      * {@link #shiftedBy(double[])}.
  49.      * @param dts time shifts array in seconds we want to apply to dates
  50.      * @return a matrix of new dates, shifted with respect to wanted time
  51.      * shifts. If instance dates = [date1, date2, ..., daten] each line
  52.      * correspond to one date (for example date1 shiftedBy all timeshifts
  53.      * (building the different columns))
  54.      */
  55.     public AbsoluteDate[][] multipleShiftedBy(final double[] dts) {

  56.         final AbsoluteDate[][] datesShifted = new AbsoluteDate[dates.length][dts.length];
  57.         int index_dates = 0;

  58.         for (AbsoluteDate date: this.dates) {
  59.             final AbsoluteDate[] dateShifted = new AbsoluteDate[dts.length];
  60.             int index_dts = 0;
  61.             for (double dt: dts) {
  62.                 dateShifted[index_dts] = date.shiftedBy(dt);
  63.                 index_dts += 1;
  64.             }
  65.             datesShifted[index_dates] = dateShifted;
  66.             index_dates += 1;

  67.         }
  68.         return (AbsoluteDate[][]) datesShifted;
  69.     }

  70.     /** Get time-shifted dates for several dates and corresponding time shifts.
  71.      * If instance dates = [date1, date2, ..., daten] and argument
  72.      * dts = [dts1, dts2, ..., dtsn] then this function will return
  73.      * [date1 shiftedby dts1, date2 shiftedBy dts2, ..., daten shiftedBy dtsn]. If
  74.      * several time shift want to be applied on each date see
  75.      * {@link #multipleShiftedBy(double[])}.
  76.      * @param dts time shifts array in seconds we want to apply to corresponding dates.
  77.      * Warning, must be same length as dates.
  78.      * @return an 1D array of new dates, shifted with respect to wanted corresponding time
  79.      * shifts.
  80.      */
  81.     public AbsoluteDate[] shiftedBy(final double[] dts) {

  82.         // Check same dimensions
  83.         if (dates.length != dts.length) {
  84.             throw new OrekitException(LocalizedCoreFormats.DIMENSIONS_MISMATCH,
  85.                                       dates.length, dts.length);
  86.         }

  87.         final AbsoluteDate[] datesShifted = new AbsoluteDate[dates.length];
  88.         int index_dates = 0;

  89.         for (AbsoluteDate date: this.dates) {
  90.             datesShifted[index_dates] = date.shiftedBy(dts[index_dates]);
  91.             index_dates += 1;

  92.         }
  93.         return datesShifted;
  94.     }

  95.     /** Get array with durations between instances dates and given dates
  96.      * If instance dates = [date1, date2, ..., daten] and argument
  97.      * datesForDuration = [d1, d2, ..., dn] then this function will return a matrix
  98.      * [[date1 durationFrom d1, date1 durationFrom d2, ..., date1 durationFrom dn],
  99.      * [date2 durationFrom d1, date2 durationFrom d2, ..., date2 durationFrom dn],
  100.      * [...]
  101.      * [daten durationFrom d1, daten durationFrom d2, ..., date1 durationFrom dn]].
  102.      * If ones want to compute duration from only 1 date corresponding to 1 instance date see
  103.      * {@link #durationFrom(AbsoluteDate[])}.
  104.      * @param datesForDuration dates for which we want to compute the duration form instances dates
  105.      * @return a matrix of double representing durations from instance dates
  106.      * If instance dates = [date1, date2, ..., daten] each line
  107.      * correspond to one date (for example date1 duration from all given dates in arguments
  108.      * (building the different columns))
  109.      */
  110.     public double[][] multipleDurationFrom(final AbsoluteDate[] datesForDuration) {

  111.         final double[][] durationsFromDates = new double[dates.length][datesForDuration.length];
  112.         int index_dates = 0;

  113.         for (AbsoluteDate date: this.dates) {
  114.             final double[] durationFromDate = new double[datesForDuration.length];
  115.             int index_datesForDuration = 0;
  116.             for (AbsoluteDate dateForDuration: datesForDuration) {
  117.                 durationFromDate[index_datesForDuration] = date.durationFrom(dateForDuration);
  118.                 index_datesForDuration += 1;
  119.             }
  120.             durationsFromDates[index_dates] = durationFromDate;
  121.             index_dates += 1;

  122.         }
  123.         return (double[][]) durationsFromDates;
  124.     }

  125.     /** Get array with durations between instances dates and corresponding given dates
  126.      * If instance dates = [date1, date2, ..., daten] and argument
  127.      * datesForDuration = [d1, d2, ..., dn] then this function will return
  128.      * [date1 durationFrom d1, date2 durationFrom d2, ..., daten durationFrom dn]. If
  129.      * duration from from all arguments dates wants to be compute on each date see
  130.      * {@link #multipleDurationFrom(AbsoluteDate[])}.
  131.      * @param datesForDuration dates for which we want to compute the duration form instances dates.
  132.      * Warning must have same length as instance dates.
  133.      * @return a array of double representing durations between instance dates and corresponding
  134.      * argument dates
  135.      */
  136.     public double[] durationFrom(final AbsoluteDate[] datesForDuration) {

  137.         // Check same dimensions
  138.         if (dates.length != datesForDuration.length) {
  139.             throw new OrekitException(LocalizedCoreFormats.DIMENSIONS_MISMATCH,
  140.                                       dates.length, datesForDuration.length);
  141.         }

  142.         final double[] durationsFromDates = new double[dates.length];
  143.         int index_dates = 0;

  144.         for (AbsoluteDate date: this.dates) {
  145.             durationsFromDates[index_dates] = date.durationFrom(datesForDuration[index_dates]);
  146.             index_dates += 1;

  147.         }
  148.         return durationsFromDates;
  149.     }

  150. }