Skip to content

Commit

Permalink
Merge pull request #64 from COSIMA/ncc/cleanup-and-more-test
Browse files Browse the repository at this point in the history
More verbose name for `quad_area` -> `quadrilateral_area` + one more test
  • Loading branch information
navidcy authored Sep 4, 2023
2 parents 1e9739a + 1ff6492 commit 1df6930
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
6 changes: 3 additions & 3 deletions regional_mom6/regional_mom6.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"motu_requests",
"dz",
"angle_between",
"quad_area",
"quadilateral_area",
"rectangular_hgrid",
"experiment",
"segment",
Expand Down Expand Up @@ -300,7 +300,7 @@ def angle_between(v1, v2, v3):


# Borrowed from grid tools (GFDL)
def quad_area(lat, lon):
def quadilateral_area(lat, lon):
"""Returns area of spherical quadrilaterals (bounded by great arcs)."""

# x, y, z are 3D coordinates on the unit sphere
Expand Down Expand Up @@ -356,7 +356,7 @@ def rectangular_hgrid(λ, φ):

lon, lat = np.meshgrid(λ, φ)

area = quad_area(lat, lon) * R**2
area = quadilateral_area(lat, lon) * R**2

attrs = {
"tile": {
Expand Down
14 changes: 9 additions & 5 deletions tests/test_grid_generation.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import numpy as np
import pytest
from regional_mom6 import angle_between
from regional_mom6 import quad_area
from regional_mom6 import quadilateral_area
from regional_mom6 import rectangular_hgrid
import xarray as xr

Expand All @@ -18,17 +18,21 @@ def test_angle_between(v1, v2, v3, true_angle):


# create a lat-lon mesh that covers 1/4 of the North Hemisphere
lon, lat = np.meshgrid(np.linspace(0, 90, 5), np.linspace(0, 90, 5))
lon1, lat1 = np.meshgrid(np.linspace(0, 90, 5), np.linspace(0, 90, 5))

# create a lat-lon mesh that covers 1/4 of the whole globe
lon2, lat2 = np.meshgrid(np.linspace(-45, 45, 5), np.linspace(-90, 90, 5))


@pytest.mark.parametrize(
("lat", "lon", "true_area"),
[
(lat, lon, 0.5 * np.pi),
(lat1, lon1, 0.5 * np.pi),
(lat2, lon2, np.pi),
],
)
def test_quad_area(lat, lon, true_area):
assert np.isclose(np.sum(quad_area(lat, lon)), true_area)
def test_quadilateral_area(lat, lon, true_area):
assert np.isclose(np.sum(quadilateral_area(lat, lon)), true_area)


# a simple test that rectangular_hgrid runs without erroring
Expand Down

0 comments on commit 1df6930

Please sign in to comment.