1   /* Copyright 2022-2025 Luc Maisonobe
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.adm.acm;
19  
20  import java.io.IOException;
21  
22  import org.hipparchus.linear.RealMatrix;
23  import org.orekit.files.ccsds.definitions.Units;
24  import org.orekit.files.ccsds.section.AbstractWriter;
25  import org.orekit.files.ccsds.utils.generation.Generator;
26  import org.orekit.utils.units.Unit;
27  
28  /** Writer for physical properties data.
29   * @author Luc Maisonobe
30   * @since 12.0
31   */
32  class AttitudePhysicalPropertiesWriter extends AbstractWriter {
33  
34      /** Physical properties block. */
35      private final AttitudePhysicalProperties phys;
36  
37      /** Create a writer.
38       * @param phys physical properties to write
39       */
40      AttitudePhysicalPropertiesWriter(final AttitudePhysicalProperties phys) {
41          super(AcmDataSubStructureKey.phys.name(), AcmDataSubStructureKey.PHYS.name());
42          this.phys = phys;
43      }
44  
45      /** {@inheritDoc} */
46      @Override
47      protected void writeContent(final Generator generator) throws IOException {
48  
49          // physical properties block
50          generator.writeComments(phys.getComments());
51  
52          // drag
53          generator.writeEntry(AttitudePhysicalPropertiesKey.DRAG_COEFF.name(), phys.getDragCoefficient(), Unit.ONE,     false);
54  
55          // mass
56          generator.writeEntry(AttitudePhysicalPropertiesKey.WET_MASS.name(),   phys.getWetMass(), Unit.KILOGRAM,        false);
57          generator.writeEntry(AttitudePhysicalPropertiesKey.DRY_MASS.name(),   phys.getDryMass(), Unit.KILOGRAM,        false);
58  
59          // center of pressure
60          if (phys.getCenterOfPressureReferenceFrame() != null) {
61              generator.writeEntry(AttitudePhysicalPropertiesKey.CP_REF_FRAME.name(), phys.getCenterOfPressureReferenceFrame().getName(), null, false);
62          }
63          if (phys.getCenterOfPressure() != null) {
64              final StringBuilder cp = new StringBuilder();
65              cp.append(generator.doubleToString(Unit.METRE.fromSI(phys.getCenterOfPressure().getX())));
66              cp.append(' ');
67              cp.append(generator.doubleToString(Unit.METRE.fromSI(phys.getCenterOfPressure().getY())));
68              cp.append(' ');
69              cp.append(generator.doubleToString(Unit.METRE.fromSI(phys.getCenterOfPressure().getZ())));
70              generator.writeEntry(AttitudePhysicalPropertiesKey.CP.name(), cp.toString(), Unit.METRE, false);
71          }
72  
73          // inertia
74          if (phys.getInertiaReferenceFrame() != null) {
75              generator.writeEntry(AttitudePhysicalPropertiesKey.INERTIA_REF_FRAME.name(), phys.getInertiaReferenceFrame().getName(), null, false);
76          }
77          final RealMatrix inertia = phys.getInertiaMatrix();
78          if (inertia != null) {
79              generator.writeEntry(AttitudePhysicalPropertiesKey.IXX.name(), inertia.getEntry(0, 0), Units.KG_M2, true);
80              generator.writeEntry(AttitudePhysicalPropertiesKey.IYY.name(), inertia.getEntry(1, 1), Units.KG_M2, true);
81              generator.writeEntry(AttitudePhysicalPropertiesKey.IZZ.name(), inertia.getEntry(2, 2), Units.KG_M2, true);
82              generator.writeEntry(AttitudePhysicalPropertiesKey.IXY.name(), inertia.getEntry(0, 1), Units.KG_M2, true);
83              generator.writeEntry(AttitudePhysicalPropertiesKey.IXZ.name(), inertia.getEntry(0, 2), Units.KG_M2, true);
84              generator.writeEntry(AttitudePhysicalPropertiesKey.IYZ.name(), inertia.getEntry(1, 2), Units.KG_M2, true);
85          }
86  
87      }
88  
89  }