TdmWriter.java

  1. /* Copyright 2002-2024 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.files.ccsds.ndm.tdm;

  18. import java.io.IOException;

  19. import org.orekit.data.DataContext;
  20. import org.orekit.files.ccsds.definitions.TimeSystem;
  21. import org.orekit.files.ccsds.ndm.ParsedUnitsBehavior;
  22. import org.orekit.files.ccsds.section.Segment;
  23. import org.orekit.files.ccsds.utils.ContextBinding;
  24. import org.orekit.files.ccsds.utils.generation.AbstractMessageWriter;
  25. import org.orekit.files.ccsds.utils.generation.Generator;
  26. import org.orekit.utils.IERSConventions;

  27. /**
  28.  * Writer for CCSDS Tracking Data Message.
  29.  *
  30.  * @author Luc Maisonobe
  31.  * @since 11.0
  32.  */
  33. public class TdmWriter extends AbstractMessageWriter<TdmHeader, Segment<TdmMetadata, ObservationsBlock>, Tdm> {

  34.     /** Version number implemented. **/
  35.     public static final double CCSDS_TDM_VERS = 2.0;

  36.     /** Padding width for aligning the '=' sign. */
  37.     public static final int KVN_PADDING_WIDTH = 29;

  38.     /** Converter for {@link RangeUnits#RU Range Units}. */
  39.     private final RangeUnitsConverter converter;

  40.     /** Complete constructor.
  41.      * <p>
  42.      * Calling this constructor directly is not recommended. Users should rather use
  43.      * {@link org.orekit.files.ccsds.ndm.WriterBuilder#buildTdmWriter()
  44.      * writerBuilder.buildTdmWriter()}.
  45.      * </p>
  46.      * @param conventions IERS Conventions
  47.      * @param dataContext used to retrieve frames, time scales, etc.
  48.      * @param converter converter for {@link RangeUnits#RU Range Units} (may be null if there
  49.      * are no range observations in {@link RangeUnits#RU Range Units})
  50.      */
  51.     public TdmWriter(final IERSConventions conventions, final DataContext dataContext,
  52.                      final RangeUnitsConverter converter) {
  53.         super(Tdm.ROOT, Tdm.FORMAT_VERSION_KEY, CCSDS_TDM_VERS,
  54.               new ContextBinding(
  55.                   () -> conventions, () -> false, () -> dataContext,
  56.                   () -> ParsedUnitsBehavior.STRICT_COMPLIANCE,
  57.                   () -> null, () -> TimeSystem.UTC,
  58.                   () -> 0.0, () -> 1.0));
  59.         this.converter = converter;
  60.     }

  61.     /** {@inheritDoc} */
  62.     @Override
  63.     protected void writeSegmentContent(final Generator generator, final double formatVersion,
  64.                                        final Segment<TdmMetadata, ObservationsBlock> segment)
  65.         throws IOException {

  66.         // write the metadata
  67.         final ContextBinding oldContext = getContext();
  68.         final TdmMetadata    metadata   = segment.getMetadata();
  69.         setContext(new ContextBinding(oldContext::getConventions,
  70.                                       oldContext::isSimpleEOP,
  71.                                       oldContext::getDataContext,
  72.                                       oldContext::getParsedUnitsBehavior,
  73.                                       oldContext::getReferenceDate,
  74.                                       metadata::getTimeSystem,
  75.                                       oldContext::getClockCount,
  76.                                       oldContext::getClockRate));
  77.         new TdmMetadataWriter(metadata, getTimeConverter()).
  78.         write(generator);

  79.         // write the observations block
  80.         new ObservationsBlockWriter(segment.getData(), getTimeConverter(), segment.getMetadata(), converter).
  81.         write(generator);

  82.     }

  83. }