Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 5 additions & 15 deletions harmonica/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ def magnetic_vec_to_angles(magnetic_e, magnetic_n, magnetic_u, degrees=True):
Inclination is measured positive downward from the horizontal plane and
declination is measured with respect to North and it is positive east.


Parameters
----------
magnetic_e : float or array
Expand Down Expand Up @@ -119,7 +118,7 @@ def magnetic_vec_to_angles(magnetic_e, magnetic_n, magnetic_u, degrees=True):

.. math::

D = \arcsin \frac{B_e}{\sqrt{B_e^2 + B_n^2}}
D = \arctan \frac{B_e}{B_n}

Examples
--------
Expand All @@ -130,23 +129,14 @@ def magnetic_vec_to_angles(magnetic_e, magnetic_n, magnetic_u, degrees=True):
# Compute the intensity as a norm
intensity = np.sqrt(magnetic_e**2 + magnetic_n**2 + magnetic_u**2)
# Compute the horizontal component of the magnetic vector
horizontal_component = np.atleast_1d(np.sqrt(magnetic_e**2 + magnetic_n**2))
# Mask the values equal to zero in the horizontal component
horizontal_component = np.ma.masked_values(horizontal_component, 0.0)
# Calculate the inclination and declination using the mask
inclination = np.arctan(-magnetic_u / horizontal_component)
declination = np.arcsin(magnetic_e / horizontal_component)
# Fill the masked values
inclination = inclination.filled(-np.sign(magnetic_u) * np.pi / 2)
declination = declination.filled(0)
horizontal_component = np.sqrt(magnetic_e**2 + magnetic_n**2)
# Calculate the inclination and declination
inclination = np.arctan2(-magnetic_u, horizontal_component)
declination = np.arctan2(magnetic_e, magnetic_n)
# Convert to degree if needed
if degrees:
inclination = np.degrees(inclination)
declination = np.degrees(declination)
# Cast to floats if all components are floats
if intensity.ndim == 0:
(inclination,) = inclination
(declination,) = declination
return intensity, inclination, declination


Expand Down
15 changes: 14 additions & 1 deletion harmonica/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,22 @@
[0, 0, -1], # Over -z axis
[1, 0, 0], # Over east (y) axis
[0, 1, 0], # Over north (x) axis
[0, 0, -1e-13], # Over -z axis
[1e-13, 0, 0], # Over east (y) axis
[0, 1e-13, 0], # Over north (x) axis
]

ANGLES = [[1, 45, 45], [1, -45, 45.0], [1, 45, -45], [1, 90, 0], [1, 0, 90], [1, 0, 0]]
ANGLES = [
[1, 45, 45],
[1, -45, 45.0],
[1, 45, -45],
[1, 90, 0],
[1, 0, 90],
[1, 0, 0],
[1e-13, 90, 0],
[1e-13, 0, 90],
[1e-13, 0, 0],
]


@pytest.mark.parametrize("angles, vector", [(a, v) for a, v in zip(ANGLES, VECTORS)])
Expand Down
Loading