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  
22  import org.hipparchus.complex.Quaternion;
23  import org.hipparchus.linear.RealMatrix;
24  import org.orekit.files.ccsds.definitions.TimeConverter;
25  import org.orekit.files.ccsds.definitions.Units;
26  import org.orekit.files.ccsds.section.AbstractWriter;
27  import org.orekit.files.ccsds.utils.generation.Generator;
28  import org.orekit.utils.units.Unit;
29  
30  /** Writer for physical properties data.
31   * @author Luc Maisonobe
32   * @since 11.0
33   */
34  class OrbitPhysicalPropertiesWriter extends AbstractWriter {
35  
36      /** Physical properties block. */
37      private final OrbitPhysicalProperties phys;
38  
39      /** Converter for dates. */
40      private final TimeConverter timeConverter;
41  
42      /** Create a writer.
43       * @param phys physical properties to write
44       * @param timeConverter converter for dates
45       */
46      OrbitPhysicalPropertiesWriter(final OrbitPhysicalProperties phys, final TimeConverter timeConverter) {
47          super(OcmDataSubStructureKey.phys.name(), OcmDataSubStructureKey.PHYS.name());
48          this.phys          = phys;
49          this.timeConverter = timeConverter;
50      }
51  
52      /** {@inheritDoc} */
53      @Override
54      protected void writeContent(final Generator generator) throws IOException {
55  
56          // physical properties block
57          generator.writeComments(phys.getComments());
58  
59          generator.writeOptionalStringEntry(OrbitPhysicalPropertiesKey.MANUFACTURER.name(), phys.getManufacturer(), null, false);
60          generator.writeOptionalStringEntry(OrbitPhysicalPropertiesKey.BUS_MODEL.name(),    phys.getBusModel(),     null, false);
61          generator.writeEntry(OrbitPhysicalPropertiesKey.DOCKED_WITH.name(),                phys.getDockedWith(),         false);
62  
63          // drag
64          generator.writeOptionalDoubleEntry(OrbitPhysicalPropertiesKey.DRAG_CONST_AREA.name(),  phys.getDragConstantArea(), Units.M2,    false);
65          generator.writeOptionalDoubleEntry(OrbitPhysicalPropertiesKey.DRAG_COEFF_NOM.name(),   phys.getDragCoefficient(), Unit.ONE,     false);
66          generator.writeOptionalDoubleEntry(OrbitPhysicalPropertiesKey.DRAG_UNCERTAINTY.name(), phys.getDragUncertainty(), Unit.PERCENT, false);
67  
68          // mass
69          generator.writeOptionalDoubleEntry(OrbitPhysicalPropertiesKey.INITIAL_WET_MASS.name(), phys.getInitialWetMass(), Unit.KILOGRAM, false);
70          generator.writeOptionalDoubleEntry(OrbitPhysicalPropertiesKey.WET_MASS.name(),         phys.getWetMass(), Unit.KILOGRAM,        false);
71          generator.writeOptionalDoubleEntry(OrbitPhysicalPropertiesKey.DRY_MASS.name(),         phys.getDryMass(), Unit.KILOGRAM,        false);
72  
73          // Optimally Enclosing Box
74          generator.writeEntry(OrbitPhysicalPropertiesKey.OEB_PARENT_FRAME.name(), phys.getOebParentFrame().getName(), null, false);
75          // oebParentFrameEpoch may be null. Usually checked in writeEntry(...)
76          if (phys.getOebParentFrameEpoch().isPresent() &&
77              !timeConverter.getReferenceDate().equals(phys.getOebParentFrameEpoch().get()) &&
78              phys.getOebParentFrame().asOrbitRelativeFrame().isEmpty() &&
79              phys.getOebParentFrame().asSpacecraftBodyFrame().isEmpty()) {
80              generator.writeOptionalDateEntry(OrbitPhysicalPropertiesKey.OEB_PARENT_FRAME_EPOCH.name(), timeConverter, phys.getOebParentFrameEpoch(), true, false);
81          }
82          generator.writeEntry(OrbitPhysicalPropertiesKey.OEB_Q1.name(),             phys.getOebQ().map(Quaternion::getQ1).orElse(Double.NaN), Unit.ONE,   false);
83          generator.writeEntry(OrbitPhysicalPropertiesKey.OEB_Q2.name(),             phys.getOebQ().map(Quaternion::getQ2).orElse(Double.NaN), Unit.ONE,   false);
84          generator.writeEntry(OrbitPhysicalPropertiesKey.OEB_Q3.name(),             phys.getOebQ().map(Quaternion::getQ3).orElse(Double.NaN), Unit.ONE,   false);
85          generator.writeEntry(OrbitPhysicalPropertiesKey.OEB_QC.name(),             phys.getOebQ().map(Quaternion::getQ0).orElse(Double.NaN), Unit.ONE,   false);
86          generator.writeOptionalDoubleEntry(OrbitPhysicalPropertiesKey.OEB_MAX.name(),            phys.getOebMax(),                                         Unit.METRE, false);
87          generator.writeOptionalDoubleEntry(OrbitPhysicalPropertiesKey.OEB_INT.name(),            phys.getOebIntermediate(),                                Unit.METRE, false);
88          generator.writeOptionalDoubleEntry(OrbitPhysicalPropertiesKey.OEB_MIN.name(),            phys.getOebMin(),                                         Unit.METRE, false);
89          generator.writeOptionalDoubleEntry(OrbitPhysicalPropertiesKey.AREA_ALONG_OEB_MAX.name(), phys.getOebAreaAlongMax(),                                Units.M2,   false);
90          generator.writeOptionalDoubleEntry(OrbitPhysicalPropertiesKey.AREA_ALONG_OEB_INT.name(), phys.getOebAreaAlongIntermediate(),                       Units.M2,   false);
91          generator.writeOptionalDoubleEntry(OrbitPhysicalPropertiesKey.AREA_ALONG_OEB_MIN.name(), phys.getOebAreaAlongMin(),                                Units.M2,   false);
92  
93          // collision probability
94          generator.writeOptionalDoubleEntry(OrbitPhysicalPropertiesKey.AREA_MIN_FOR_PC.name(), phys.getMinAreaForCollisionProbability(), Units.M2, false);
95          generator.writeOptionalDoubleEntry(OrbitPhysicalPropertiesKey.AREA_MAX_FOR_PC.name(), phys.getMaxAreaForCollisionProbability(), Units.M2, false);
96          generator.writeOptionalDoubleEntry(OrbitPhysicalPropertiesKey.AREA_TYP_FOR_PC.name(), phys.getTypAreaForCollisionProbability(), Units.M2, false);
97  
98          // radar cross section
99          generator.writeOptionalDoubleEntry(OrbitPhysicalPropertiesKey.RCS.name(),     phys.getRcs(), Units.M2,    false);
100         generator.writeOptionalDoubleEntry(OrbitPhysicalPropertiesKey.RCS_MIN.name(), phys.getMinRcs(), Units.M2, false);
101         generator.writeOptionalDoubleEntry(OrbitPhysicalPropertiesKey.RCS_MAX.name(), phys.getMaxRcs(), Units.M2, false);
102 
103         // solar radiation pressure
104         generator.writeOptionalDoubleEntry(OrbitPhysicalPropertiesKey.SRP_CONST_AREA.name(),        phys.getSrpConstantArea(), Units.M2,    false);
105         generator.writeOptionalDoubleEntry(OrbitPhysicalPropertiesKey.SOLAR_RAD_COEFF.name(),       phys.getSrpCoefficient(), Unit.ONE,     false);
106         generator.writeOptionalDoubleEntry(OrbitPhysicalPropertiesKey.SOLAR_RAD_UNCERTAINTY.name(), phys.getSrpUncertainty(), Unit.PERCENT, false);
107 
108         // visual magnitude
109         generator.writeOptionalDoubleEntry(OrbitPhysicalPropertiesKey.VM_ABSOLUTE.name(),     phys.getVmAbsolute(),    Unit.ONE, false);
110         generator.writeOptionalDoubleEntry(OrbitPhysicalPropertiesKey.VM_APPARENT_MIN.name(), phys.getVmApparentMin(), Unit.ONE, false);
111         generator.writeOptionalDoubleEntry(OrbitPhysicalPropertiesKey.VM_APPARENT.name(),     phys.getVmApparent(),    Unit.ONE, false);
112         generator.writeOptionalDoubleEntry(OrbitPhysicalPropertiesKey.VM_APPARENT_MAX.name(), phys.getVmApparentMax(), Unit.ONE, false);
113         generator.writeOptionalDoubleEntry(OrbitPhysicalPropertiesKey.REFLECTANCE.name(),     phys.getReflectance(),   Unit.ONE, false);
114 
115         // attitude
116         generator.writeOptionalStringEntry(OrbitPhysicalPropertiesKey.ATT_CONTROL_MODE.name(),  phys.getAttitudeControlMode(),       null,        false);
117         generator.writeOptionalStringEntry(OrbitPhysicalPropertiesKey.ATT_ACTUATOR_TYPE.name(), phys.getAttitudeActuatorType(),      null,        false);
118         generator.writeOptionalDoubleEntry(OrbitPhysicalPropertiesKey.ATT_KNOWLEDGE.name(),     phys.getAttitudeKnowledgeAccuracy(), Unit.DEGREE, false);
119         generator.writeOptionalDoubleEntry(OrbitPhysicalPropertiesKey.ATT_CONTROL.name(),       phys.getAttitudeControlAccuracy(),   Unit.DEGREE, false);
120         generator.writeOptionalDoubleEntry(OrbitPhysicalPropertiesKey.ATT_POINTING.name(),      phys.getAttitudePointingAccuracy(),  Unit.DEGREE, false);
121 
122         // maneuvers
123         generator.writeOptionalDoubleEntry(OrbitPhysicalPropertiesKey.AVG_MANEUVER_FREQ.name(), phys.getManeuversFrequency(), Units.NB_PER_Y, false);
124         generator.writeOptionalDoubleEntry(OrbitPhysicalPropertiesKey.MAX_THRUST.name(),        phys.getMaxThrust(),          Unit.NEWTON,    false);
125         generator.writeOptionalDoubleEntry(OrbitPhysicalPropertiesKey.DV_BOL.name(),            phys.getBolDv(),              Units.KM_PER_S, false);
126         generator.writeOptionalDoubleEntry(OrbitPhysicalPropertiesKey.DV_REMAINING.name(),      phys.getRemainingDv(),        Units.KM_PER_S, false);
127 
128         // inertia
129         if (phys.getInertiaMatrix().isPresent()) {
130             final RealMatrix inertia = phys.getInertiaMatrix().get();
131             generator.writeEntry(OrbitPhysicalPropertiesKey.IXX.name(), inertia.getEntry(0, 0), Units.KG_M2, false);
132             generator.writeEntry(OrbitPhysicalPropertiesKey.IYY.name(), inertia.getEntry(1, 1), Units.KG_M2, false);
133             generator.writeEntry(OrbitPhysicalPropertiesKey.IZZ.name(), inertia.getEntry(2, 2), Units.KG_M2, false);
134             generator.writeEntry(OrbitPhysicalPropertiesKey.IXY.name(), inertia.getEntry(0, 1), Units.KG_M2, false);
135             generator.writeEntry(OrbitPhysicalPropertiesKey.IXZ.name(), inertia.getEntry(0, 2), Units.KG_M2, false);
136             generator.writeEntry(OrbitPhysicalPropertiesKey.IYZ.name(), inertia.getEntry(1, 2), Units.KG_M2, false);
137         }
138 
139     }
140 
141 }