TdmWriter.java

  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.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.Header;
  23. import org.orekit.files.ccsds.section.Segment;
  24. import org.orekit.files.ccsds.utils.ContextBinding;
  25. import org.orekit.files.ccsds.utils.generation.AbstractMessageWriter;
  26. import org.orekit.files.ccsds.utils.generation.Generator;
  27. import org.orekit.utils.IERSConventions;

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

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

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

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

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

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

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

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

  83.     }

  84. }