From 74efd5957f060adba80c77b198e96562d2b13f7a Mon Sep 17 00:00:00 2001 From: akrherz Date: Fri, 20 Dec 2024 13:43:30 -0600 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20pyiem.grid.nav.get=5Fnav?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pyiem/grid/nav.py | 7 +++++++ src/pyiem/grid/nav.pyi | 2 ++ tests/grid/test_grid_nav.py | 6 ++++++ tests/models/test_models_gridnav.py | 20 +++++++++++++++++--- 4 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/pyiem/grid/nav.py b/src/pyiem/grid/nav.py index fe26219f..cc729416 100644 --- a/src/pyiem/grid/nav.py +++ b/src/pyiem/grid/nav.py @@ -95,6 +95,13 @@ } +def get_nav(name: str, dom: str) -> CartesianGridNavigation: + """Helper to remove some boilerplate for fetching gridnav.""" + extra = f"_{dom.upper()}" if dom != "" else "" + key = f"{name.upper()}{extra}" + return CartesianGridNavigation(**_GRID_CONFIGS[key]) + + def __getattr__(name: str): """Build stuff on the fly.""" if name in _GRID_CONFIGS: diff --git a/src/pyiem/grid/nav.pyi b/src/pyiem/grid/nav.pyi index 6975e033..c34ea8a0 100644 --- a/src/pyiem/grid/nav.pyi +++ b/src/pyiem/grid/nav.pyi @@ -12,3 +12,5 @@ class STAGE4(CartesianGridNavigation): ... class STAGE4_PRE2002(CartesianGridNavigation): ... class MRMS_IEMRE(CartesianGridNavigation): ... class PRISM(CartesianGridNavigation): ... + +def get_nav(name: str, region: str) -> CartesianGridNavigation: ... diff --git a/tests/grid/test_grid_nav.py b/tests/grid/test_grid_nav.py index a795869b..bcdbee05 100644 --- a/tests/grid/test_grid_nav.py +++ b/tests/grid/test_grid_nav.py @@ -14,3 +14,9 @@ def test_api(): def test_prism_calc(): """Test that PRISM works out to what we expect.""" assert (nav.PRISM.right - -66.50) < 0.01 + + +def test_get_nav(): + """Test that we can get a helper.""" + assert nav.get_nav("iemRE", "") + assert nav.get_nav("era5LAND", "china") diff --git a/tests/models/test_models_gridnav.py b/tests/models/test_models_gridnav.py index 9008968e..709ada20 100644 --- a/tests/models/test_models_gridnav.py +++ b/tests/models/test_models_gridnav.py @@ -31,9 +31,23 @@ def test_non_even_spacing(): ) +def test_computing_dxdy(): + """Test that we can get a dx and dy.""" + _cgn = CartesianGridNavigation( + left_edge=0, + bottom_edge=0, + nx=10, + ny=10, + right_edge=10, + top_edge=10, + ) + assert _cgn.dx == 1 + assert _cgn.dy == 1 + + def test_computing_nxny(): """Test that ny and ny can be computed.""" - cgn = CartesianGridNavigation( + _cgn = CartesianGridNavigation( left_edge=0, bottom_edge=0, dx=1, @@ -41,8 +55,8 @@ def test_computing_nxny(): right_edge=10, top_edge=10, ) - assert cgn.nx == 10 - assert cgn.ny == 10 + assert _cgn.nx == 10 + assert _cgn.ny == 10 def test_api(cgn):