1   /* Copyright 2013-2017 CS Systèmes d'Information
2    * Licensed to CS Systèmes d'Information (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.errors;
18  
19  import org.hipparchus.geometry.euclidean.threed.Vector3D;
20  import java.io.File;
21  import java.io.IOException;
22  import java.io.PrintWriter;
23  
24  import org.orekit.bodies.GeodeticPoint;
25  import org.orekit.frames.Transform;
26  import org.orekit.rugged.api.AlgorithmId;
27  import org.orekit.rugged.linesensor.LineSensor;
28  import org.orekit.rugged.linesensor.SensorMeanPlaneCrossing;
29  import org.orekit.rugged.linesensor.SensorPixel;
30  import org.orekit.rugged.raster.Tile;
31  import org.orekit.rugged.utils.ExtendedEllipsoid;
32  import org.orekit.rugged.utils.SpacecraftToObservedBody;
33  import org.orekit.time.AbsoluteDate;
34  
35  /**
36   * Class managing debug dumps.
37   * <p>
38   * <em>WARNING</em>: this class is public only for technical reasons,
39   * it is not considered to belong to the public API of the library and should
40   * not be called by user code. It is only intended to be called internally by
41   * the Rugged library itself. This class may be changed or even removed at any
42   * time, so user code should not rely on it.
43   * </p>
44   * @author Luc Maisonobe
45   */
46  public class DumpManager {
47  
48      /** Dump file (default initial value is null, i.e. nothing is dumped). */
49      private static final ThreadLocal<Dump> DUMP = new ThreadLocal<Dump>();
50  
51      /** Private constructor for utility class.
52       */
53      private DumpManager() {
54          // by default, nothing is dumped
55      }
56  
57      /** Activate debug dump.
58       * @param file dump file
59       * @exception RuggedException if debug dump is already active for this thread
60       * or if debug file cannot be opened
61       */
62      public static void activate(final File file) throws RuggedException {
63          if (isActive()) {
64              throw new RuggedException(RuggedMessages.DEBUG_DUMP_ALREADY_ACTIVE);
65          } else {
66              try {
67                  DUMP.set(new Dump(new PrintWriter(file, "UTF-8")));
68              } catch (IOException ioe) {
69                  throw new RuggedException(ioe, RuggedMessages.DEBUG_DUMP_ACTIVATION_ERROR,
70                                            file.getAbsolutePath(), ioe.getLocalizedMessage());
71              }
72          }
73      }
74  
75      /** Deactivate debug dump.
76       * @exception RuggedException if debug dump is already active for this thread
77       */
78      public static void deactivate() throws RuggedException {
79          if (isActive()) {
80              DUMP.get().deactivate();
81              DUMP.set(null);
82          } else {
83              throw new RuggedException(RuggedMessages.DEBUG_DUMP_NOT_ACTIVE);
84          }
85      }
86  
87      /** Check if dump is active for this thread.
88       * @return true if dump is active for this thread
89       */
90      public static boolean isActive() {
91          return DUMP.get() != null;
92      }
93  
94      /** Dump DEM cell data.
95       * @param tile tile to which the cell belongs
96       * @param latitudeIndex latitude index of the cell
97       * @param longitudeIndex longitude index of the cell
98       * @param elevation elevation of the cell
99       */
100     public static void dumpTileCell(final Tile tile,
101                                     final int latitudeIndex, final int longitudeIndex,
102                                     final double elevation) {
103         if (isActive()) {
104             DUMP.get().dumpTileCell(tile, latitudeIndex, longitudeIndex, elevation);
105         }
106     }
107 
108     /** Dump algorithm data.
109      * @param algorithmId algorithm ID
110      */
111     public static void dumpAlgorithm(final AlgorithmId algorithmId) {
112         if (isActive()) {
113             DUMP.get().dumpAlgorithm(algorithmId);
114         }
115     }
116 
117     /** Dump algorithm data.
118      * @param algorithmId algorithm ID
119      * @param specific algorithm specific extra data
120      */
121     public static void dumpAlgorithm(final AlgorithmId algorithmId, final double specific) {
122         if (isActive()) {
123             DUMP.get().dumpAlgorithm(algorithmId, specific);
124         }
125     }
126 
127     /** Dump ellipsoid data.
128      * @param ellipsoid ellipsoid to dump
129      */
130     public static void dumpEllipsoid(final ExtendedEllipsoid ellipsoid) {
131         if (isActive()) {
132             DUMP.get().dumpEllipsoid(ellipsoid);
133         }
134     }
135 
136     /** Dump a direct location computation.
137      * @param date date of the location
138      * @param position pixel position in spacecraft frame
139      * @param los normalized line-of-sight in spacecraft frame
140      * @param lightTimeCorrection flag for light time correction
141      * @param aberrationOfLightCorrection flag for aberration of light correction
142      * @param refractionCorrection flag for refraction correction
143      * @exception RuggedException if date cannot be converted to UTC
144      */
145     public static void dumpDirectLocation(final AbsoluteDate date, final Vector3D position, final Vector3D los,
146                                           final boolean lightTimeCorrection, final boolean aberrationOfLightCorrection,
147                                           final boolean refractionCorrection)
148         throws RuggedException {
149         if (isActive()) {
150             DUMP.get().dumpDirectLocation(date, position, los, lightTimeCorrection, aberrationOfLightCorrection,
151                     refractionCorrection);
152         }
153     }
154 
155     /** Dump a direct location result.
156      * @param gp resulting geodetic point
157      * @exception RuggedException if date cannot be converted to UTC
158      */
159     public static void dumpDirectLocationResult(final GeodeticPoint gp)
160         throws RuggedException {
161         if (isActive()) {
162             DUMP.get().dumpDirectLocationResult(gp);
163         }
164     }
165 
166     /** Dump an inverse location computation.
167      * @param sensor sensor
168      * @param point point to localize
169      * @param minLine minimum line number
170      * @param maxLine maximum line number
171      * @param lightTimeCorrection flag for light time correction
172      * @param aberrationOfLightCorrection flag for aberration of light correction
173      */
174     public static void dumpInverseLocation(final LineSensor sensor, final GeodeticPoint point,
175                                            final int minLine, final int maxLine,
176                                            final boolean lightTimeCorrection, final boolean aberrationOfLightCorrection) {
177         if (isActive()) {
178             DUMP.get().dumpInverseLocation(sensor, point, minLine, maxLine,
179                                            lightTimeCorrection, aberrationOfLightCorrection);
180         }
181     }
182 
183     /** Dump an inverse location result.
184      * @param pixel resulting sensor pixel
185      */
186     public static void dumpInverseLocationResult(final SensorPixel pixel) {
187         if (isActive()) {
188             DUMP.get().dumpInverseLocationResult(pixel);
189         }
190     }
191 
192     /** Dump an observation transform transform.
193      * @param scToBody provider for observation
194      * @param index index of the transform
195      * @param bodyToInertial transform from body frame to inertial frame
196      * @param scToInertial transfrom from spacecraft frame to inertial frame
197      * @exception RuggedException if reference date cannot be converted to UTC
198      */
199     public static void dumpTransform(final SpacecraftToObservedBody scToBody, final int index,
200                                      final Transform bodyToInertial, final Transform scToInertial)
201         throws RuggedException {
202         if (isActive()) {
203             DUMP.get().dumpTransform(scToBody, index, bodyToInertial, scToInertial);
204         }
205     }
206 
207     /** Dump a sensor mean plane.
208      * @param meanPlane mean plane associated with sensor
209      * @exception RuggedException if some frames cannot be computed at mid date
210      */
211     public static void dumpSensorMeanPlane(final SensorMeanPlaneCrossing meanPlane)
212         throws RuggedException {
213         if (isActive()) {
214             DUMP.get().dumpSensorMeanPlane(meanPlane);
215         }
216     }
217 
218     /** Dump a sensor LOS.
219      * @param sensor sensor
220      * @param date date
221      * @param i pixel index
222      * @param los pixel normalized line-of-sight
223      * @exception RuggedException if date cannot be converted to UTC
224      */
225     public static void dumpSensorLOS(final LineSensor sensor, final AbsoluteDate date, final int i, final Vector3D los)
226         throws RuggedException {
227         if (isActive()) {
228             DUMP.get().dumpSensorLOS(sensor, date, i, los);
229         }
230     }
231 
232     /** Dump a sensor datation.
233      * @param sensor sensor
234      * @param lineNumber line number
235      * @param date date
236      * @exception RuggedException if date cannot be converted to UTC
237      */
238     public static void dumpSensorDatation(final LineSensor sensor, final double lineNumber, final AbsoluteDate date)
239         throws RuggedException {
240         if (isActive()) {
241             DUMP.get().dumpSensorDatation(sensor, lineNumber, date);
242         }
243     }
244 
245     /** Dump a sensor rate.
246      * @param sensor sensor
247      * @param lineNumber line number
248      * @param rate lines rate
249      */
250     public static void dumpSensorRate(final LineSensor sensor, final double lineNumber, final double rate) {
251         if (isActive()) {
252             DUMP.get().dumpSensorRate(sensor, lineNumber, rate);
253         }
254     }
255 
256 }