Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify angle_between + refactor quadrilateral_area #65

Merged
merged 25 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
ea9bbbe
simplify angle_between
navidcy Sep 5, 2023
812a302
black format
navidcy Sep 5, 2023
3d3179a
remove random unit vector; add 2 more tests
navidcy Sep 5, 2023
a8c9ad4
Merge branch 'main' into ncc/simplify-angle-between
navidcy Sep 5, 2023
6721c47
refactor quadrilateral_area
navidcy Sep 5, 2023
d414ea1
black format
navidcy Sep 5, 2023
30254bd
merge main
navidcy Sep 5, 2023
9ad060e
adds comment
navidcy Sep 5, 2023
ad69968
better docstring for quadilateral_areas
navidcy Sep 5, 2023
d34d2f8
add test for quadilateral_area
navidcy Sep 5, 2023
283778a
add latlon_to_cartesian + test
navidcy Sep 5, 2023
4cf6a3b
one more test for quadilateral_area
navidcy Sep 5, 2023
d4f8668
remove debuggint print statements
navidcy Sep 5, 2023
2366869
cleaner test_quadilateral_areas
navidcy Sep 5, 2023
b5877f3
add clarification in latlon_to_cartesian doc
navidcy Sep 5, 2023
00ad0b5
add clarification in latlon_to_cartesian doc
navidcy Sep 5, 2023
2ee793d
add clarification in angle_between docs
navidcy Sep 5, 2023
d091f32
latlon_to_cartesian, quadilateral_area(s) now know R
navidcy Sep 6, 2023
a18ff47
black formating
navidcy Sep 6, 2023
3147a51
correct phrasing in docstring
navidcy Sep 6, 2023
139ac70
better exception error
navidcy Sep 6, 2023
aa5ed7a
test the exception error by quadilateral_area
navidcy Sep 6, 2023
c423f52
Exception -> ValueError
navidcy Sep 6, 2023
dccdede
Implement quadrilateral_areas using broadcasting
angus-g Sep 6, 2023
8d45e19
Fix quadrilateral area exception test
angus-g Sep 6, 2023
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
27 changes: 9 additions & 18 deletions regional_mom6/regional_mom6.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,6 @@ def dz(npoints, ratio, target_depth, min_dz=0.0001, tolerance=1):

Returns:
numpy.array: An array containing the thickness profile.

"""

profile = min_dz + 0.5 * (np.abs(ratio) * min_dz - min_dz) * (
Expand All @@ -279,24 +278,16 @@ def dz(npoints, ratio, target_depth, min_dz=0.0001, tolerance=1):
return dz(npoints, ratio, target_depth, min_dz * err_ratio)


# Borrowed from grid tools (GFDL)
def angle_between(v1, v2, v3):
"""Returns angle v2-v1-v3 i.e betweeen v1-v2 and v1-v3."""

# vector product between v1 and v2
px = v1[1] * v2[2] - v1[2] * v2[1]
py = v1[2] * v2[0] - v1[0] * v2[2]
pz = v1[0] * v2[1] - v1[1] * v2[0]
# vector product between v1 and v3
qx = v1[1] * v3[2] - v1[2] * v3[1]
qy = v1[2] * v3[0] - v1[0] * v3[2]
qz = v1[0] * v3[1] - v1[1] * v3[0]

ddd = (px * px + py * py + pz * pz) * (qx * qx + qy * qy + qz * qz)
ddd = (px * qx + py * qy + pz * qz) / np.sqrt(ddd)
angle = np.arccos(ddd)

return angle
"""Returns angle v2-v1-v3 that is between the vectors v1-v2 and v1-v3."""

v1xv2 = np.cross(v1, v2)
v1xv3 = np.cross(v1, v3)
cosangle = np.dot(v1xv2, v1xv3) / np.sqrt(
np.dot(v1xv2, v1xv2) * np.dot(v1xv3, v1xv3)
)

return np.arccos(cosangle)
navidcy marked this conversation as resolved.
Show resolved Hide resolved


# Borrowed from grid tools (GFDL)
Expand Down
8 changes: 8 additions & 0 deletions tests/test_grid_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
import xarray as xr


def random_unit_vector():
"""Return a random unit vector on the unit sphere."""
z = 2 * np.random.rand(1)[0] - 1
z
θ = 2 * np.pi * np.random.rand(1)[0]
return [np.sqrt(1 - z**2) * np.cos(θ), np.sqrt(1 - z**2) * np.sin(θ), z]


navidcy marked this conversation as resolved.
Show resolved Hide resolved
@pytest.mark.parametrize(
("v1", "v2", "v3", "true_angle"),
[
Expand Down
Loading