Skip to content

Commit 1abe269

Browse files
Skip regriding for two datasets on same lat-lon grids (#959)
* skip regriding for two datasets on same lat-lon grids * Extract check for equal grids to a function - Add unit tests * Update e3sm_diags/driver/utils/regrid.py --------- Co-authored-by: tomvothecoder <[email protected]>
1 parent 3af7532 commit 1abe269

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

e3sm_diags/driver/utils/regrid.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,12 @@ def align_grids_to_lower_res(
367367
ds_a_new = ds_a.copy()
368368
ds_b_new = ds_b.copy()
369369

370+
# If grids are equal, then no regridding is required. This ensures no
371+
# performance cost is incurred.
372+
equal_grids = _are_grids_equal(ds_a_new, ds_b_new)
373+
if equal_grids:
374+
return ds_a_new, ds_b_new
375+
370376
ds_a_new = _drop_unused_ilev_axis(ds_a_new)
371377
ds_b_new = _drop_unused_ilev_axis(ds_b_new)
372378

@@ -391,6 +397,35 @@ def align_grids_to_lower_res(
391397
return ds_a_regrid, ds_b_new
392398

393399

400+
def _are_grids_equal(ds_a: xr.Dataset, ds_b: xr.Dataset) -> bool:
401+
"""Check if the grids of two datasets are identical.
402+
403+
This function checks if the grids of the two datasets have the same shape
404+
and same values. If they do, then the grids are identical
405+
406+
Parameters
407+
----------
408+
ds_a : xr.Dataset
409+
The first dataset.
410+
ds_b : xr.Dataset
411+
The second dataset.
412+
413+
Returns
414+
-------
415+
bool
416+
True if the grids are identical, False otherwise.
417+
"""
418+
lat_a, lat_b = xc.get_dim_coords(ds_a, axis="Y"), xc.get_dim_coords(ds_b, axis="Y")
419+
lon_a, lon_b = xc.get_dim_coords(ds_a, axis="X"), xc.get_dim_coords(ds_b, axis="X")
420+
421+
return (
422+
lat_a.shape == lat_b.shape
423+
and lon_a.shape == lon_b.shape
424+
and np.allclose(lat_a.values, lat_b.values)
425+
and np.allclose(lon_a.values, lon_b.values)
426+
)
427+
428+
394429
def _drop_unused_ilev_axis(ds: xr.Dataset) -> xr.Dataset:
395430
"""Drop the unused ilev axis in a dataset.
396431

tests/e3sm_diags/driver/utils/test_regrid.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,19 @@ def test_regrids_to_second_dataset_with_conservative_normed_method(self, tool):
375375

376376
assert_identical(result_b, expected_b)
377377

378+
@pytest.mark.parametrize("tool", ("xesmf", "regrid2"))
379+
def test_returns_original_datasets_if_grids_are_equal(self, tool):
380+
ds_a = generate_lev_dataset("pressure", pressure_vars=False)
381+
ds_b = generate_lev_dataset("pressure", pressure_vars=False)
382+
383+
result_a, result_b = align_grids_to_lower_res(
384+
ds_a, ds_b, "so", tool, "conservative_normed"
385+
)
386+
387+
# Since the grids are equal, the original datasets should be returned
388+
assert_identical(result_a, ds_a)
389+
assert_identical(result_b, ds_b)
390+
378391

379392
class TestRegridZAxisToPlevs:
380393
@pytest.fixture(autouse=True)

0 commit comments

Comments
 (0)