Skip to content

Commit b983c7d

Browse files
add test grid cell area
1 parent 9537568 commit b983c7d

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

climada/util/coordinates.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ def get_gridcellarea(lat, resolution=0.5, unit="ha"):
460460
return area
461461

462462

463-
def compute_grid_cell_area(res: float) -> tuple[np.ndarray]:
463+
def compute_grid_cell_area_validation(res: float) -> tuple[np.ndarray]:
464464
"""
465465
This function computes the area of each grid cell on a sphere (Earth), using latitude and
466466
longitude bins based on the given resolution. The area is computed using the spherical cap
@@ -524,10 +524,10 @@ def compute_grid_cell_area(res: float) -> tuple[np.ndarray]:
524524
areas[:, np.newaxis], (1, len(lon_bins) - 1)
525525
) # each row as same area
526526

527-
return grid_area, [lat_bins, lon_bins]
527+
return grid_area, lat_bins, lon_bins
528528

529529

530-
def compute_grid_cell_area_(
530+
def compute_grid_cell_area(
531531
res: float = 1.0, projection: str = "WGS84", units: str = "km^2"
532532
) -> np.ndarray:
533533
"""
@@ -556,7 +556,6 @@ def compute_grid_cell_area_(
556556
>>> area = compute_grid_areas(res = 1, projection ="sphere", units = "m^2")
557557
"""
558558
geod = Geod(ellps=projection) # Use specified ellipsoid model
559-
560559
lat_edges = np.linspace(-90, 90, int(180 / res)) # Latitude edges
561560
lon_edges = np.linspace(-180, 180, int(360 / res)) # Longitude edges
562561

climada/util/test/test_coordinates.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -565,8 +565,30 @@ def test_get_gridcellarea(self):
565565
self.assertAlmostEqual(area2[0], 1781.5973363005)
566566
self.assertTrue(area2[0] <= 2500)
567567

568-
def test_compute_grid_area_():
569-
pass
568+
def test_compute_grid_area(self):
569+
"""Test that the two twin functions calculate the area of a gridcell correctly. Using
570+
an absolute reference and mutual validation by comparison."""
571+
res = 1
572+
area = u_coord.compute_grid_cell_area(
573+
res=res, projection="sphere", units="km^2"
574+
)
575+
area_test, *_ = u_coord.compute_grid_cell_area_validation(res=res)
576+
577+
self.assertEqual(area.shape, (179, 359))
578+
self.assertEqual(area_test.shape, (179, 359))
579+
# check that all rows have equal area with 1e-5 tolerance for relative and absolute precision
580+
for i in range(area.shape[0]):
581+
self.assertTrue(
582+
np.all(np.isclose(area[i, :], area[i, 0], rtol=1e-5, atol=1e-5))
583+
)
584+
self.assertTrue(
585+
np.all(
586+
np.isclose(area_test[i, :], area_test[i, 0], rtol=1e-5, atol=1e-5)
587+
)
588+
)
589+
590+
# check that both methods give similar results with 0.01% tolerance in relative difference
591+
self.assertTrue(np.allclose(area, area_test, rtol=1e-2, atol=1e-5))
570592

571593
def test_read_vector_pass(self):
572594
"""Test one columns data"""

0 commit comments

Comments
 (0)