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