1   /* Copyright 2002-2026 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  
18  package org.orekit.files.ccsds.ndm.odm.ocm;
19  
20  import java.io.IOException;
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import org.orekit.errors.OrekitException;
25  import org.orekit.errors.OrekitMessages;
26  import org.orekit.files.ccsds.definitions.BodyFacade;
27  import org.orekit.files.ccsds.definitions.TimeConverter;
28  import org.orekit.files.ccsds.definitions.Units;
29  import org.orekit.files.ccsds.section.AbstractWriter;
30  import org.orekit.files.ccsds.utils.generation.Generator;
31  import org.orekit.utils.units.Unit;
32  
33  /** Writer for perturbations parameters data.
34   * @author Luc Maisonobe
35   * @since 11.0
36   */
37  class PerturbationsWriter extends AbstractWriter {
38  
39      /** Perturbation parameters block. */
40      private final Perturbations perturbations;
41  
42      /** Converter for dates. */
43      private final TimeConverter timeConverter;
44  
45      /** Create a writer.
46       * @param perturbations perturbations parameters to write
47       * @param timeConverter converter for dates
48       */
49      PerturbationsWriter(final Perturbations perturbations, final TimeConverter timeConverter) {
50          super(OcmDataSubStructureKey.pert.name(), OcmDataSubStructureKey.PERT.name());
51          this.perturbations = perturbations;
52          this.timeConverter = timeConverter;
53      }
54  
55      /** {@inheritDoc} */
56      @Override
57      protected void writeContent(final Generator generator) throws IOException {
58  
59          // perturbations parameters block
60          generator.writeComments(perturbations.getComments());
61  
62          // atmosphere
63          generator.writeOptionalStringEntry(PerturbationsKey.ATMOSPHERIC_MODEL.name(), perturbations.getAtmosphericModel(), null, false);
64  
65          // gravity
66          if (perturbations.getGravityModel().isPresent()) {
67              final String model =
68                              new StringBuilder().
69                              append(perturbations.getGravityModel().get()).
70                              append(": ").
71                              append(perturbations.getGravityDegree().orElseThrow(() -> new OrekitException(OrekitMessages.CCSDS_MISSING_OPTIONAL_VALUE))).
72                              append("D ").
73                              append(perturbations.getGravityOrder().orElseThrow(() -> new OrekitException(OrekitMessages.CCSDS_MISSING_OPTIONAL_VALUE))).
74                              append('O').
75                              toString();
76              generator.writeEntry(PerturbationsKey.GRAVITY_MODEL.name(), model, null, false);
77          }
78          generator.writeOptionalDoubleEntry(PerturbationsKey.EQUATORIAL_RADIUS.name(),    perturbations.getEquatorialRadius(), Unit.KILOMETRE, false);
79          generator.writeOptionalDoubleEntry(PerturbationsKey.GM.name(),                   perturbations.getGm(), Units.KM3_PER_S2,             false);
80          if (perturbations.getNBodyPerturbations() != null && !perturbations.getNBodyPerturbations().isEmpty()) {
81              final List<String> names = new ArrayList<>();
82              for (BodyFacade bf : perturbations.getNBodyPerturbations()) {
83                  names.add(bf.getName());
84              }
85              generator.writeEntry(PerturbationsKey.N_BODY_PERTURBATIONS.name(), names, false);
86          }
87          generator.writeOptionalDoubleEntry(PerturbationsKey.CENTRAL_BODY_ROTATION.name(), perturbations.getCentralBodyRotation(), Units.DEG_PER_S,       false);
88          generator.writeOptionalDoubleEntry(PerturbationsKey.OBLATE_FLATTENING.name(),     perturbations.getOblateFlattening(), Unit.ONE,                 false);
89          generator.writeOptionalStringEntry(PerturbationsKey.OCEAN_TIDES_MODEL.name(),     perturbations.getOceanTidesModel(),                      null, false);
90          generator.writeOptionalStringEntry(PerturbationsKey.SOLID_TIDES_MODEL.name(),     perturbations.getSolidTidesModel(),                      null, false);
91          generator.writeOptionalStringEntry(PerturbationsKey.REDUCTION_THEORY.name(),      perturbations.getReductionTheory(),                      null, false);
92  
93          // radiation
94          generator.writeOptionalStringEntry(PerturbationsKey.ALBEDO_MODEL.name(),    perturbations.getAlbedoModel(),    null, false);
95          generator.writeOptionalIntEntry(PerturbationsKey.ALBEDO_GRID_SIZE.name(),   perturbations.getAlbedoGridSize(),       false);
96          generator.writeOptionalEnumEntry(PerturbationsKey.SHADOW_MODEL.name(),      perturbations.getShadowModel(),          false);
97          if (perturbations.getShadowBodies() != null && !perturbations.getShadowBodies().isEmpty()) {
98              final List<String> names = new ArrayList<>();
99              for (BodyFacade bf : perturbations.getShadowBodies()) {
100                 names.add(bf.getName());
101             }
102             generator.writeEntry(PerturbationsKey.SHADOW_BODIES.name(), names, false);
103         }
104         generator.writeOptionalStringEntry(PerturbationsKey.SRP_MODEL.name(),          perturbations.getSrpModel(), null, false);
105 
106         // data source
107         generator.writeOptionalStringEntry(PerturbationsKey.SW_DATA_SOURCE.name(),    perturbations.getSpaceWeatherSource(),                    null, false);
108         generator.writeOptionalDateEntry(PerturbationsKey.SW_DATA_EPOCH.name(),       timeConverter, perturbations.getSpaceWeatherEpoch(),      true, false);
109         generator.writeOptionalStringEntry(PerturbationsKey.SW_INTERP_METHOD.name(),  perturbations.getInterpMethodSW(),                        null, false);
110         generator.writeOptionalDoubleEntry(PerturbationsKey.FIXED_GEOMAG_KP.name(),   perturbations.getFixedGeomagneticKp(), Units.NANO_TESLA,        false);
111         generator.writeOptionalDoubleEntry(PerturbationsKey.FIXED_GEOMAG_AP.name(),   perturbations.getFixedGeomagneticAp(), Units.NANO_TESLA,        false);
112         generator.writeOptionalDoubleEntry(PerturbationsKey.FIXED_GEOMAG_DST.name(),  perturbations.getFixedGeomagneticDst(), Units.NANO_TESLA,       false);
113         generator.writeOptionalDoubleEntry(PerturbationsKey.FIXED_F10P7.name(),       perturbations.getFixedF10P7(), Unit.SOLAR_FLUX_UNIT,            false);
114         generator.writeOptionalDoubleEntry(PerturbationsKey.FIXED_F10P7_MEAN.name(),  perturbations.getFixedF10P7Mean(), Unit.SOLAR_FLUX_UNIT,        false);
115         generator.writeOptionalDoubleEntry(PerturbationsKey.FIXED_M10P7.name(),       perturbations.getFixedM10P7(), Unit.SOLAR_FLUX_UNIT,            false);
116         generator.writeOptionalDoubleEntry(PerturbationsKey.FIXED_M10P7_MEAN.name(),  perturbations.getFixedM10P7Mean(), Unit.SOLAR_FLUX_UNIT,        false);
117         generator.writeOptionalDoubleEntry(PerturbationsKey.FIXED_S10P7.name(),       perturbations.getFixedS10P7(), Unit.SOLAR_FLUX_UNIT,            false);
118         generator.writeOptionalDoubleEntry(PerturbationsKey.FIXED_S10P7_MEAN.name(),  perturbations.getFixedS10P7Mean(), Unit.SOLAR_FLUX_UNIT,        false);
119         generator.writeOptionalDoubleEntry(PerturbationsKey.FIXED_Y10P7.name(),       perturbations.getFixedY10P7(), Unit.SOLAR_FLUX_UNIT,            false);
120         generator.writeOptionalDoubleEntry(PerturbationsKey.FIXED_Y10P7_MEAN.name(),  perturbations.getFixedY10P7Mean(), Unit.SOLAR_FLUX_UNIT,        false);
121 
122     }
123 
124 }