1   /* Copyright 2011-2012 Space Applications Services
2    * Licensed to CS Communication & Systèmes (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.models.earth;
18  
19  import java.io.Serializable;
20  import java.text.DecimalFormat;
21  import java.text.NumberFormat;
22  
23  import org.hipparchus.geometry.euclidean.threed.Vector3D;
24  import org.hipparchus.util.FastMath;
25  import org.orekit.utils.units.UnitsConverter;
26  
27  /** Contains the elements to represent a magnetic field at a single point.
28   * @author Thomas Neidhart
29   */
30  public class GeoMagneticElements implements Serializable {
31  
32      /** Serializable UID. */
33      private static final long serialVersionUID = 1881493738280586855L;
34  
35      /** The magnetic field vector (East=X, North=Y, Nadir=Z). */
36      private Vector3D b;
37  
38      /** The magnetic inclination in radians. */
39      private double inclination;
40  
41      /** The magnetic declination in radians. */
42      private double declination;
43  
44      /** The magnetic total intensity, in Teslas. */
45      private double totalIntensity;
46  
47      /** The magnetic horizontal intensity, in Teslas. */
48      private double horizontalIntensity;
49  
50      /** Construct a new element with the given field vector. The other elements
51       * of the magnetic field are calculated from the field vector.
52       * @param b the magnetic field vector
53       */
54      public GeoMagneticElements(final Vector3D b) {
55          this.b = new Vector3D(UnitsConverter.NANO_TESLAS_TO_TESLAS.getFrom().getScale(), b);
56  
57          final double intensityNanoTesla = FastMath.hypot(b.getX(), b.getY());
58          horizontalIntensity = UnitsConverter.NANO_TESLAS_TO_TESLAS.convert(intensityNanoTesla);
59          totalIntensity = UnitsConverter.NANO_TESLAS_TO_TESLAS.convert(b.getNorm());
60          declination = FastMath.atan2(b.getY(), b.getX());
61          inclination = FastMath.atan2(b.getZ(), intensityNanoTesla);
62      }
63  
64      /** Returns the magnetic field vector in Tesla.
65       * @return the magnetic field vector in Tesla
66       */
67      public Vector3D getFieldVector() {
68          return b;
69      }
70  
71      /** Returns the inclination of the magnetic field in radians.
72       * @return the inclination (dip) in radians
73       */
74      public double getInclination() {
75          return inclination;
76      }
77  
78      /** Returns the declination of the magnetic field in radians.
79       * @return the declination (dec) in radians
80       */
81      public double getDeclination() {
82          return declination;
83      }
84  
85      /** Returns the total intensity of the magnetic field (= norm of the field vector).
86       * @return the total intensity in Tesla
87       */
88      public double getTotalIntensity() {
89          return totalIntensity;
90      }
91  
92      /** Returns the horizontal intensity of the magnetic field (= norm of the
93       * vector in the plane spanned by the x/y components of the field vector).
94       * @return the horizontal intensity in Tesla
95       */
96      public double getHorizontalIntensity() {
97          return horizontalIntensity;
98      }
99  
100     @Override
101     public String toString() {
102         final NumberFormat f = NumberFormat.getInstance();
103         final DecimalFormat d = new DecimalFormat("0.######E0");
104         final StringBuilder sb = new StringBuilder();
105         sb.append("MagneticField[");
106         sb.append("B=");
107         sb.append(b.toString(d));
108         sb.append(",H=");
109         sb.append(d.format(getHorizontalIntensity()));
110         sb.append(",F=");
111         sb.append(d.format(getTotalIntensity()));
112         sb.append(",I=");
113         sb.append(f.format(getInclination()));
114         sb.append(",D=");
115         sb.append(f.format(getDeclination()));
116         sb.append("]");
117         return sb.toString();
118     }
119 }