Skip to content

Commit

Permalink
CLN: Port x_rotation_conv.c to python
Browse files Browse the repository at this point in the history
  • Loading branch information
JB Lovland authored and janbjorge committed Nov 29, 2023
1 parent d342388 commit 08db8fb
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 118 deletions.
2 changes: 0 additions & 2 deletions src/clib/xtg/libxtg.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,6 @@ double
x_avg_angles(double *swig_np_dbl_in_v1, // *angles
long n_swig_np_dbl_in_v1); // nsize,

double
x_rotation_conv(double ain, int aimode, int mode, int option);

double
x_tetrahedron_volume(double *swig_np_dbl_inplaceflat_v1,
Expand Down
101 changes: 0 additions & 101 deletions src/clib/xtg/x_rotation_conv.c

This file was deleted.

29 changes: 29 additions & 0 deletions src/xtgeo/common/_angles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from math import degrees, radians


def _deg_angle2azimuth(deg: float) -> float:
"""Converts an angle from X-axis anti-clockwise orientation
to Y-axis clockwise azimuth.
"""
return (-deg + 90) % 360


def _deg_azimuth2angle(deg: float) -> float:
"""Converts azimuth from Y-axis clockwise orientation to X-axis
anti-clockwise angle.
"""
return (450 - deg) % 360


def _rad_angle2azimuth(rad: float) -> float:
"""Converts an angle from X-axis anti-clockwise orientation to Y-axis clockwise
azimuth in radians.
"""
return radians(_deg_angle2azimuth(degrees(rad)))


def _rad_azimuth2angle(deg: float) -> float:
"""Converts azimuth from Y-axis clockwise orientation to X-axis anti-clockwise
angle in radians.
"""
return radians(_deg_azimuth2angle(degrees(deg)))
26 changes: 11 additions & 15 deletions src/xtgeo/common/calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import numpy as np

from xtgeo import XTGeoCLibError, _cxtgeo
from xtgeo.common import XTGeoDialog, null_logger
from xtgeo.common import XTGeoDialog, _angles, null_logger

xtg = XTGeoDialog()
logger = null_logger(__name__)
Expand Down Expand Up @@ -207,13 +207,11 @@ def angle2azimuth(
Return:
Azimuth angle (in degrees or radian)
"""
nmode1 = 0
nmode2 = 2
if mode == "radians":
nmode1 += 1
nmode2 += 1

return _cxtgeo.x_rotation_conv(inangle, nmode1, nmode2, 0)
return (
_angles._deg_angle2azimuth(inangle)
if mode == "degrees"
else _angles._rad_angle2azimuth(inangle)
)


def azimuth2angle(
Expand All @@ -232,13 +230,11 @@ def azimuth2angle(
Return:
Angle (in degrees or radians)
"""
nmode1 = 2
nmode2 = 0
if mode == "radians":
nmode1 += 1
nmode2 += 1

return _cxtgeo.x_rotation_conv(inangle, nmode1, nmode2, 0)
return (
_angles._deg_azimuth2angle(inangle)
if mode == "degrees"
else _angles._rad_azimuth2angle(inangle)
)


def tetrehedron_volume(vertices: Sequence[float]) -> float:
Expand Down
32 changes: 32 additions & 0 deletions tests/test_common/test_calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import numpy as np
import pytest
from hypothesis import given
from hypothesis import strategies as st

import xtgeo
import xtgeo.common.calc as xcalc
Expand Down Expand Up @@ -190,6 +192,36 @@ def test_azimuth2angle():
assert res == 120


@given(
x=st.floats(
min_value=0,
max_value=180,
allow_infinity=False,
allow_nan=False,
)
)
def test_angle2azimuth_azimuth2angle_deg(x: float) -> None:
assert (
pytest.approx(xcalc.azimuth2angle(xcalc.angle2azimuth(x, "degrees"), "degrees"))
== x
)


@given(
x=st.floats(
min_value=0,
max_value=math.pi,
allow_infinity=False,
allow_nan=False,
)
)
def test_angle2azimuth_azimuth2angle_rad(x: float) -> None:
assert (
pytest.approx(xcalc.azimuth2angle(xcalc.angle2azimuth(x, "radians"), "radians"))
== x
)


def test_averageangle():
"""Test finding an average angle"""

Expand Down

0 comments on commit 08db8fb

Please sign in to comment.