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.forces.maneuvers.trigger;
18  
19  import java.util.Arrays;
20  import java.util.List;
21  
22  import org.hipparchus.CalculusFieldElement;
23  import org.hipparchus.Field;
24  import org.orekit.propagation.events.FieldAbstractDetector;
25  import org.orekit.propagation.events.FieldEventDetector;
26  import org.orekit.propagation.events.FieldParameterDrivenDateIntervalDetector;
27  import org.orekit.propagation.events.ParameterDrivenDateIntervalDetector;
28  import org.orekit.time.AbsoluteDate;
29  import org.orekit.utils.ParameterDriver;
30  
31  /** Maneuver triggers based on a start and end date, with no parameter drivers.
32   * @author Maxime Journot
33   * @since 10.2
34   */
35  public class DateBasedManeuverTriggers extends IntervalEventTrigger<ParameterDrivenDateIntervalDetector> {
36  
37      /** Default name for trigger. */
38      public static final String DEFAULT_NAME = "";
39  
40      /** Name of the trigger (used as prefix for start and stop parameters drivers). */
41      private final String name;
42  
43      /** Simple constructor.
44       * @param date start (or end) data of the maneuver
45       * @param duration maneuver duration (if positive, maneuver is from date to date + duration,
46       * if negative, maneuver will be from date - duration to date)
47       */
48      public DateBasedManeuverTriggers(final AbsoluteDate date, final double duration) {
49          this(DEFAULT_NAME, date, duration);
50      }
51  
52      /** Simple constructor.
53       * @param name name of the trigger (used as prefix for start and stop parameters drivers)
54       * @param date start (or end) data of the maneuver
55       * @param duration maneuver duration (if positive, maneuver is from date to date + duration,
56       * if negative, maneuver will be from date - duration to date)
57       * @since 11.1
58       */
59      public DateBasedManeuverTriggers(final String name, final AbsoluteDate date, final double duration) {
60          super(createDetector(name, date, duration));
61          this.name = name;
62      }
63  
64      /** Create a date detector from one boundary and signed duration.
65       * @param prefix for start and stop parameters drivers
66       * @param date start (or end) data of the maneuver
67       * @param duration maneuver duration (if positive, maneuver is from date to date + duration,
68       * if negative, maneuver will be from date - duration to date)
69       * @return date detector
70       * @since 11.1
71       */
72      private static ParameterDrivenDateIntervalDetector createDetector(final String prefix, final AbsoluteDate date, final double duration) {
73          if (duration >= 0) {
74              return new ParameterDrivenDateIntervalDetector(prefix, date, date.shiftedBy(duration));
75          } else {
76              return new ParameterDrivenDateIntervalDetector(prefix, date.shiftedBy(duration), date);
77          }
78      }
79  
80      /** {@inheritDoc} */
81      @Override
82      public String getName() {
83          return name;
84      }
85  
86      /** Get the start date.
87       * @return the start date
88       */
89      public AbsoluteDate getStartDate() {
90          return getFiringIntervalDetector().getStartDriver().getDate();
91      }
92  
93      /** Get the end date.
94       * @return the end date
95       */
96      public AbsoluteDate getEndDate() {
97          return getFiringIntervalDetector().getStopDriver().getDate();
98      }
99  
100     /** Get the duration of the maneuver (s).
101      * duration = endDate - startDate
102      * @return the duration of the maneuver (s)
103      */
104     public double getDuration() {
105         return getEndDate().durationFrom(getStartDate());
106     }
107 
108     /** {@inheritDoc} */
109     @Override
110     protected <D extends FieldEventDetector<S>, S extends CalculusFieldElement<S>>
111         FieldAbstractDetector<D, S> convertIntervalDetector(final Field<S> field, final ParameterDrivenDateIntervalDetector detector) {
112 
113         final FieldParameterDrivenDateIntervalDetector<S> fd =
114                         new FieldParameterDrivenDateIntervalDetector<S>(field, "",
115                                                                         detector.getStartDriver().getBaseDate(),
116                                                                         detector.getStopDriver().getBaseDate());
117         fd.getStartDriver().setName(detector.getStartDriver().getName());
118         fd.getStopDriver().setName(detector.getStopDriver().getName());
119         fd.getMedianDriver().setName(detector.getMedianDriver().getName());
120         fd.getDurationDriver().setName(detector.getDurationDriver().getName());
121 
122         @SuppressWarnings("unchecked")
123         final FieldAbstractDetector<D, S> converted = (FieldAbstractDetector<D, S>) fd;
124         return converted;
125 
126     }
127 
128     /** {@inheritDoc} */
129     @Override
130     public List<ParameterDriver> getParametersDrivers() {
131         return Arrays.asList(getFiringIntervalDetector().getStartDriver(),
132                              getFiringIntervalDetector().getStopDriver(),
133                              getFiringIntervalDetector().getMedianDriver(),
134                              getFiringIntervalDetector().getDurationDriver());
135     }
136 
137 }