Commit e31cbb26 authored by Cecilia Nievas's avatar Cecilia Nievas
Browse files

Added function determine_cardinal_point and its test

parent 2bfbb2a4
......@@ -746,3 +746,103 @@ def guarantee_radians(in_theta, theta_thr, inputangle):
)
return in_theta, theta_thr
def determine_cardinal_point(in_theta, theta_thr, inputangle="radians"):
"""This function "translates" each angle in the array in_theta into a direction in terms of
"cardinal points". The array in_theta contains angles between an E-W ("horizontal") line
and another line, in the range [-pi, +pi], measured from the E-W ("horizontal") line. If
positive, it is measured counter-clockwise, if positive, it is measured clockwise. They are
expected to stem from the use of numpy.arctan2(), which yields an angle in this range,
under this same convention. The "translation" is done in terms of the tautological
definition of cardinal points (e.g. that 0 is east, pi/2 is north, pi and -pi are west,
-pi/2 is south) and an angle theta_thr that defines how much can the line deviate from,
for example, pi/2, and still be considered north. The exact ranges associated with each
of the eight possible outcomes (N, S, E, W, NE, NW, SE, SW) are defined within the code.
This function does exactly the same as determine_cardinal_point_indiv, but this function
needs in_theta to be an array while in determine_cardinal_point_indiv in_theta is a float.
Args:
in_theta (array of floats): Angle between an E-W ("horizontal") line and another line,
in the range [-pi, +pi], measure from the E-W
("horizontal") line. If positive, it is measured counter-
clockwise, if negative, it is measured clockwise. It is
expected to stem from the use of numpy.arctan2(), which
yields an angle in this range, under this same convention.
If in_theta is outside the range [-pi, +pi], this function
will return an array with 'UU', meaning "unknown".
theta_thr (float): Angle used to divide all possible values of in_theta into east (E),
north-east (NE), north (N), north-west (NW), and so on. It needs to
be positive. The ranges that relate to each categories are defined
in the code.
inputangle (str): Units of in_theta and theta_thr, either "radians" or "degrees".
Returns:
out_card (array of str): Array of the same length as in_theta. Each individual value
can only be one of these values: N, S, E, W, NE, NW, SE, SW,
or U, the latter meaning "unknown", i.e. something is wrong
in the input.
"""
in_theta, theta_thr = guarantee_radians(in_theta, theta_thr, inputangle)
# Initialise output array with "unknown":
out_card = np.array(["UU" for i in range(len(in_theta))])
if theta_thr < 0.0: # theta_thr cannot be negative
return out_card
# East
out_card[np.where(np.logical_and(in_theta >= -theta_thr, in_theta <= theta_thr))[0]] = "E"
# North-east
out_card[
np.where(np.logical_and(in_theta > theta_thr, in_theta < (np.pi / 2.0 - theta_thr)))[0]
] = "NE"
# North
out_card[
np.where(
np.logical_and(
in_theta >= (np.pi / 2.0 - theta_thr), in_theta <= (np.pi / 2.0 + theta_thr)
)
)[0]
] = "N"
# North-west
out_card[
np.where(
np.logical_and(in_theta > (np.pi / 2.0 + theta_thr), in_theta < (np.pi - theta_thr))
)[0]
] = "NW"
# West (below, *1.00001 is the marging for floating point precision)
out_card[
np.where(
np.logical_or(
np.logical_and(in_theta >= (np.pi - theta_thr), in_theta <= np.pi * 1.00001),
np.logical_and(in_theta >= -np.pi * 1.00001, in_theta <= (-np.pi + theta_thr)),
)
)[0]
] = "W"
# South-west
out_card[
np.where(
np.logical_and(
in_theta > (-np.pi + theta_thr), in_theta < (-np.pi / 2.0 - theta_thr)
)
)[0]
] = "SW"
# South
out_card[
np.where(
np.logical_and(
in_theta >= (-np.pi / 2.0 - theta_thr), in_theta <= (-np.pi / 2.0 + theta_thr)
)
)[0]
] = "S"
# South-east
out_card[
np.where(np.logical_and(in_theta > (-np.pi / 2.0 + theta_thr), in_theta < -theta_thr))[
0
]
] = "SE"
return out_card
......@@ -746,3 +746,97 @@ def test_swell_cells_with_buffer():
for i in range(len(expected_geometries)):
assert expected_geometries[i] == function_gdf["geometry"].values[i]
def test_determine_cardinal_point():
# Create input data for a realistic test:
theta_threshold = 25.0
angles_deg = np.array(
[
0.0,
24.0,
26.0,
64.0,
66.0,
90.0,
114.0,
116.0,
154.0,
156.0,
180.0,
185.0,
-0.1,
-24.0,
-26.0,
-64.0,
-66.0,
-90.0,
-114.0,
-116.0,
-154.0,
-156.0,
-180.0,
-185.0,
]
)
expected_output = np.array(
[
"E",
"E",
"NE",
"NE",
"N",
"N",
"N",
"NW",
"NW",
"W",
"W",
"UU",
"E",
"E",
"SE",
"SE",
"S",
"S",
"S",
"SW",
"SW",
"W",
"W",
"UU",
]
)
# Call function to test:
function_cardinal_pts = gdet_cr_ind.determine_cardinal_point(
angles_deg, theta_threshold, inputangle="degrees"
)
# Go one by one the elements of the output array:
for i in range(len(expected_output)):
assert function_cardinal_pts[i] == expected_output[i]
# Same test as above, but giving the angles in radians instead of degrees:
angles_rad = np.deg2rad(angles_deg)
theta_threshold_rad = np.deg2rad(theta_threshold)
# Call function to test:
function_cardinal_pts = gdet_cr_ind.determine_cardinal_point(
angles_rad, theta_threshold_rad, inputangle="radians"
)
# Go one by one the elements of the output array:
for i in range(len(expected_output)):
assert function_cardinal_pts[i] == expected_output[i]
# Test that all results are unknown ("UU") if the threshold angle is negative:
theta_threshold = -25.0
# Call function to test:
function_cardinal_pts = gdet_cr_ind.determine_cardinal_point(
angles_deg, theta_threshold, inputangle="degrees"
)
# Go one by one the elements of the output array:
for i in range(len(expected_output)):
assert function_cardinal_pts[i] == "UU"
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment