1   /* Copyright 2002-2025 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  
19  import java.io.IOException;
20  
21  import org.orekit.data.DataContext;
22  import org.orekit.files.ccsds.definitions.TimeSystem;
23  import org.orekit.files.ccsds.ndm.ParsedUnitsBehavior;
24  import org.orekit.files.ccsds.section.Segment;
25  import org.orekit.files.ccsds.utils.ContextBinding;
26  import org.orekit.files.ccsds.utils.generation.AbstractMessageWriter;
27  import org.orekit.files.ccsds.utils.generation.Generator;
28  import org.orekit.utils.IERSConventions;
29  
30  /**
31   * Writer for CCSDS Tracking Data Message.
32   *
33   * @author Luc Maisonobe
34   * @since 11.0
35   */
36  public class TdmWriter extends AbstractMessageWriter<TdmHeader, Segment<TdmMetadata, ObservationsBlock>, Tdm> {
37  
38      /** Version number implemented. **/
39      public static final double CCSDS_TDM_VERS = 2.0;
40  
41      /** Padding width for aligning the '=' sign. */
42      public static final int KVN_PADDING_WIDTH = 29;
43  
44      /** Converter for {@link RangeUnits#RU Range Units}. */
45      private final RangeUnitsConverter converter;
46  
47      /** Complete constructor.
48       * <p>
49       * Calling this constructor directly is not recommended. Users should rather use
50       * {@link org.orekit.files.ccsds.ndm.WriterBuilder#buildTdmWriter()
51       * writerBuilder.buildTdmWriter()}.
52       * </p>
53       * @param conventions IERS Conventions
54       * @param dataContext used to retrieve frames, time scales, etc.
55       * @param converter converter for {@link RangeUnits#RU Range Units} (may be null if there
56       * are no range observations in {@link RangeUnits#RU Range Units})
57       */
58      public TdmWriter(final IERSConventions conventions, final DataContext dataContext,
59                       final RangeUnitsConverter converter) {
60          super(Tdm.ROOT, Tdm.FORMAT_VERSION_KEY, CCSDS_TDM_VERS,
61                new ContextBinding(
62                    () -> conventions, () -> false, () -> dataContext,
63                    () -> ParsedUnitsBehavior.STRICT_COMPLIANCE,
64                    () -> null, () -> TimeSystem.UTC,
65                    () -> 0.0, () -> 1.0));
66          this.converter = converter;
67      }
68  
69      /** {@inheritDoc} */
70      @Override
71      protected void writeSegmentContent(final Generator generator, final double formatVersion,
72                                         final Segment<TdmMetadata, ObservationsBlock> segment)
73          throws IOException {
74  
75          // write the metadata
76          final ContextBinding oldContext = getContext();
77          final TdmMetadata    metadata   = segment.getMetadata();
78          setContext(new ContextBinding(oldContext::getConventions,
79                                        oldContext::isSimpleEOP,
80                                        oldContext::getDataContext,
81                                        oldContext::getParsedUnitsBehavior,
82                                        oldContext::getReferenceDate,
83                                        metadata::getTimeSystem,
84                                        oldContext::getClockCount,
85                                        oldContext::getClockRate));
86          new TdmMetadataWriter(metadata, getTimeConverter()).
87          write(generator);
88  
89          // write the observations block
90          new ObservationsBlockWriter(segment.getData(), getTimeConverter(), segment.getMetadata(), converter).
91          write(generator);
92  
93      }
94  
95  }