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