TruncatedCcsdsFormatter.java

  1. /* Contributed in the public domain.
  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.utils;

  18. import java.math.RoundingMode;
  19. import java.text.DecimalFormat;
  20. import java.text.NumberFormat;

  21. /** Formatter used to produce strings from data that are compliant with CCSDS standards.
  22.  * <p>
  23.  * Formats a double number to achieve CCSDS formatting standards for: OPM, OMM, OEM, or OCM (502.0-B-3 7.5.6),
  24.  * CDM (508.0-B-1 6.3.2.2), TDM (503.0-B-2 4.3.4), and ADM (504.0-B-2 6.8.4.1).
  25.  * This states that the mantissa shall not exceed 16 digits.
  26.  * </p>
  27.  * <p>
  28.  * This does NOT ensure round-trip safety. See {@link AccurateFormatter} for a formatter that ensures round trip safety.
  29.  * </p>
  30.  * @author John Ajamian
  31.  * @since 13.0
  32.  */
  33. public class TruncatedCcsdsFormatter implements Formatter {

  34.     /** Maximum digits allowed by CCSDS standards. */
  35.     private static final int MAXIMUM_ODM_DIGITS = 16;

  36.     /** Used to format double to be compliant with CCSDS standards. */
  37.     private static final String CCSDS_FORMAT = "0.0##############E0##";

  38.     /** Used to make sure seconds is only 16 digits. */
  39.     private static final String SECOND_FORMAT = "00.0#############";

  40.     /** Public constructor.
  41.      */
  42.     public TruncatedCcsdsFormatter() {
  43.         // nothing to do
  44.     }

  45.     /** Format a double number. Formats to CCSDS compliant standards.
  46.      * @param value number to format
  47.      * @return number formatted to full accuracy or CCSDS standards
  48.      */
  49.     @Override
  50.     public String toString(final double value) {
  51.         final DecimalFormat formatter = (DecimalFormat) NumberFormat.getInstance(STANDARDIZED_LOCALE);
  52.         formatter.applyLocalizedPattern(CCSDS_FORMAT);
  53.         formatter.setRoundingMode(RoundingMode.HALF_UP);
  54.         formatter.setMaximumFractionDigits(MAXIMUM_ODM_DIGITS - 1);
  55.         return formatter.format(value);
  56.     }

  57.     /** Formats to CCSDS 16 digit standard for the seconds variable.
  58.      * {@inheritDoc}
  59.      */
  60.     @Override
  61.     public String toString(final int year, final int month, final int day,
  62.                            final int hour, final int minute, final double seconds) {

  63.         final DecimalFormat formatter = (DecimalFormat) NumberFormat.getInstance(STANDARDIZED_LOCALE);
  64.         formatter.applyLocalizedPattern(SECOND_FORMAT);
  65.         formatter.setRoundingMode(RoundingMode.DOWN);
  66.         formatter.setMaximumFractionDigits(MAXIMUM_ODM_DIGITS - 2);
  67.         formatter.setMinimumIntegerDigits(2);

  68.         return String.format(STANDARDIZED_LOCALE, DATE_FORMAT,
  69.                 year, month, day,
  70.                 hour, minute, formatter.format(seconds));
  71.     }
  72. }