DumpManager.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.errors;

  18. import org.hipparchus.geometry.euclidean.threed.Vector3D;
  19. import java.io.File;
  20. import java.io.IOException;
  21. import java.io.PrintWriter;

  22. import org.orekit.bodies.GeodeticPoint;
  23. import org.orekit.frames.Transform;
  24. import org.orekit.rugged.api.AlgorithmId;
  25. import org.orekit.rugged.linesensor.LineSensor;
  26. import org.orekit.rugged.linesensor.SensorMeanPlaneCrossing;
  27. import org.orekit.rugged.linesensor.SensorPixel;
  28. import org.orekit.rugged.raster.Tile;
  29. import org.orekit.rugged.utils.ExtendedEllipsoid;
  30. import org.orekit.rugged.utils.SpacecraftToObservedBody;
  31. import org.orekit.time.AbsoluteDate;

  32. /**
  33.  * Class managing debug dumps.
  34.  * <p>
  35.  * <em>WARNING</em>: this class is public only for technical reasons,
  36.  * it is not considered to belong to the public API of the library and should
  37.  * not be called by user code. It is only intended to be called internally by
  38.  * the Rugged library itself. This class may be changed or even removed at any
  39.  * time, so user code should not rely on it.
  40.  * </p>
  41.  * @author Luc Maisonobe
  42.  * @author Guylaine Prat
  43.  */
  44. public class DumpManager {

  45.     /** Dump file (default initial value is null, i.e. nothing is dumped). */
  46.     private static final ThreadLocal<Dump> DUMP = new ThreadLocal<>();

  47.     /** Boolean to check if the dump is suspended. */
  48.     private static boolean isSuspended = false;

  49.     /** Private constructor for utility class.
  50.      */
  51.     private DumpManager() {
  52.         // by default, nothing is dumped
  53.     }

  54.     /** Activate debug dump.
  55.      * @param file dump file
  56.      */
  57.     public static void activate(final File file) {
  58.         if (isActive()) {
  59.             throw new RuggedException(RuggedMessages.DEBUG_DUMP_ALREADY_ACTIVE);
  60.         } else {
  61.             try {
  62.                 DUMP.set(new Dump(new PrintWriter(file, "UTF-8")));
  63.             } catch (IOException ioe) {
  64.                 throw new RuggedException(ioe, RuggedMessages.DEBUG_DUMP_ACTIVATION_ERROR,
  65.                                           file.getAbsolutePath(), ioe.getLocalizedMessage());
  66.             }
  67.         }
  68.     }

  69.     /** Deactivate debug dump.
  70.      */
  71.     public static void deactivate() {
  72.         if (isActive()) {
  73.             DUMP.get().deactivate();
  74.             DUMP.set(null);
  75.         } else {
  76.             throw new RuggedException(RuggedMessages.DEBUG_DUMP_NOT_ACTIVE);
  77.         }
  78.     }

  79.     /** Suspend the dump.
  80.      * In case the dump is already suspended, keep the previous status in order to
  81.      * correctly deal the resume stage.
  82.      * @return a flag to tell if the dump is already suspended (true; false otherwise)
  83.      */
  84.     public static Boolean suspend() {
  85.         // Check if the dump is already suspended
  86.         if (isSuspended) {
  87.             return isSuspended;
  88.         } else {
  89.             isSuspended = true;
  90.             return false;
  91.         }
  92.     }

  93.     /** Resume the dump, only if it was not already suspended.
  94.      * @param wasSuspended flag to tell if the dump was already suspended (true; false otherwise)
  95.      */
  96.     public static void resume(final Boolean wasSuspended) {
  97.         if (!wasSuspended) {
  98.             isSuspended = false;
  99.         }
  100.     }

  101.     /** In case dump is suspended and an exception is thrown,
  102.      * allows the dump to end nicely.
  103.      */
  104.     public static void endNicely() {
  105.         isSuspended = false;
  106.         if (isActive()) deactivate();

  107.     }

  108.     /** Check if dump is active for this thread.
  109.      * @return true if dump is active for this thread
  110.      */
  111.     public static boolean isActive() {
  112.         return DUMP.get() != null && !isSuspended;
  113.     }

  114.     /** Dump DEM cell data.
  115.      * @param tile tile to which the cell belongs
  116.      * @param latitudeIndex latitude index of the cell
  117.      * @param longitudeIndex longitude index of the cell
  118.      * @param elevation elevation of the cell
  119.      */
  120.     public static void dumpTileCell(final Tile tile,
  121.                                     final int latitudeIndex, final int longitudeIndex,
  122.                                     final double elevation) {
  123.         if (isActive()) {
  124.             DUMP.get().dumpTileCell(tile, latitudeIndex, longitudeIndex, elevation);
  125.         }
  126.     }

  127.     /** Dump algorithm data.
  128.      * @param algorithmId algorithm ID
  129.      */
  130.     public static void dumpAlgorithm(final AlgorithmId algorithmId) {
  131.         if (isActive()) {
  132.             DUMP.get().dumpAlgorithm(algorithmId);
  133.         }
  134.     }

  135.     /** Dump algorithm data.
  136.      * @param algorithmId algorithm ID
  137.      * @param specific algorithm specific extra data
  138.      */
  139.     public static void dumpAlgorithm(final AlgorithmId algorithmId, final double specific) {
  140.         if (isActive()) {
  141.             DUMP.get().dumpAlgorithm(algorithmId, specific);
  142.         }
  143.     }

  144.     /** Dump ellipsoid data.
  145.      * @param ellipsoid ellipsoid to dump
  146.      */
  147.     public static void dumpEllipsoid(final ExtendedEllipsoid ellipsoid) {
  148.         if (isActive()) {
  149.             DUMP.get().dumpEllipsoid(ellipsoid);
  150.         }
  151.     }

  152.     /** Dump a direct location computation.
  153.      * @param date date of the location
  154.      * @param sensorPosition sensor position in spacecraft frame
  155.      * @param los normalized line-of-sight in spacecraft frame
  156.      * @param lightTimeCorrection flag for light time correction
  157.      * @param aberrationOfLightCorrection flag for aberration of light correction
  158.      * @param refractionCorrection flag for refraction correction
  159.      */
  160.     public static void dumpDirectLocation(final AbsoluteDate date, final Vector3D sensorPosition, final Vector3D los,
  161.                                           final boolean lightTimeCorrection, final boolean aberrationOfLightCorrection,
  162.                                           final boolean refractionCorrection) {
  163.         if (isActive()) {
  164.             DUMP.get().dumpDirectLocation(date, sensorPosition, los, lightTimeCorrection, aberrationOfLightCorrection,
  165.                                           refractionCorrection);
  166.         }
  167.     }

  168.     /** Dump a direct location result.
  169.      * @param gp resulting geodetic point
  170.      */
  171.     public static void dumpDirectLocationResult(final GeodeticPoint gp) {
  172.         if (isActive()) {
  173.             DUMP.get().dumpDirectLocationResult(gp);
  174.         }
  175.     }

  176.     /** Dump an inverse location computation.
  177.      * @param sensor sensor
  178.      * @param point point to localize
  179.      * @param ellipsoid the used ellipsoid
  180.      * @param minLine minimum line number
  181.      * @param maxLine maximum line number
  182.      * @param lightTimeCorrection flag for light time correction
  183.      * @param aberrationOfLightCorrection flag for aberration of light correction
  184.      * @param refractionCorrection flag for refraction correction
  185.      */
  186.     public static void dumpInverseLocation(final LineSensor sensor, final GeodeticPoint point,
  187.                                            final ExtendedEllipsoid ellipsoid,
  188.                                            final int minLine, final int maxLine,
  189.                                            final boolean lightTimeCorrection, final boolean aberrationOfLightCorrection,
  190.                                            final boolean refractionCorrection) {
  191.         if (isActive()) {
  192.             DUMP.get().dumpInverseLocation(sensor, point, minLine, maxLine,
  193.                                            lightTimeCorrection, aberrationOfLightCorrection, refractionCorrection);
  194.             DUMP.get().dumpEllipsoid(ellipsoid);
  195.         }
  196.     }

  197.     /** Dump an inverse location result.
  198.      * @param pixel resulting sensor pixel
  199.      */
  200.     public static void dumpInverseLocationResult(final SensorPixel pixel) {
  201.         if (isActive()) {
  202.             DUMP.get().dumpInverseLocationResult(pixel);
  203.         }
  204.     }

  205.     /** Dump an observation transform transform.
  206.      * @param scToBody provider for observation
  207.      * @param index index of the transform
  208.      * @param bodyToInertial transform from body frame to inertial frame
  209.      * @param scToInertial transfrom from spacecraft frame to inertial frame
  210.      */
  211.     public static void dumpTransform(final SpacecraftToObservedBody scToBody, final int index,
  212.                                      final Transform bodyToInertial, final Transform scToInertial) {
  213.         if (isActive()) {
  214.             DUMP.get().dumpTransform(scToBody, index, bodyToInertial, scToInertial);
  215.         }
  216.     }

  217.     /** Dump a sensor mean plane.
  218.      * @param meanPlane mean plane associated with sensor
  219.      */
  220.     public static void dumpSensorMeanPlane(final SensorMeanPlaneCrossing meanPlane) {
  221.         if (isActive()) {
  222.             DUMP.get().dumpSensorMeanPlane(meanPlane);
  223.         }
  224.     }

  225.     /** Dump a sensor LOS.
  226.      * @param sensor sensor
  227.      * @param date date
  228.      * @param i pixel index
  229.      * @param los pixel normalized line-of-sight
  230.      */
  231.     public static void dumpSensorLOS(final LineSensor sensor, final AbsoluteDate date, final int i, final Vector3D los) {
  232.         if (isActive()) {
  233.             DUMP.get().dumpSensorLOS(sensor, date, i, los);
  234.         }
  235.     }

  236.     /** Dump a sensor datation.
  237.      * @param sensor sensor
  238.      * @param lineNumber line number
  239.      * @param date date
  240.      */
  241.     public static void dumpSensorDatation(final LineSensor sensor, final double lineNumber, final AbsoluteDate date) {
  242.         if (isActive()) {
  243.             DUMP.get().dumpSensorDatation(sensor, lineNumber, date);
  244.         }
  245.     }

  246.     /** Dump a sensor rate.
  247.      * @param sensor sensor
  248.      * @param lineNumber line number
  249.      * @param rate lines rate
  250.      */
  251.     public static void dumpSensorRate(final LineSensor sensor, final double lineNumber, final double rate) {
  252.         if (isActive()) {
  253.             DUMP.get().dumpSensorRate(sensor, lineNumber, rate);
  254.         }
  255.     }

  256. }