From 90c43e075c413d520b315354a6210e3600de8eec Mon Sep 17 00:00:00 2001 From: Wahaj18 <48466561+Wahaj18@users.noreply.github.com> Date: Sun, 11 Oct 2026 08:56:56 +0500 Subject: [PATCH] FIX: convert each Py-ART Grid only once in get_dd_wind_field A list of Py-ART Grids was converted inside a redundant outer loop, so every grid went through read_from_pyart_grid once per grid in the list. The conversion pops the radar location data from the Grid, so the second pass raised KeyError: 'data' for any list of two or more Py-ART Grids. Closes #167 --- pydda/retrieval/wind_retrieve.py | 3 +-- pydda/tests/test_retrieval.py | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/pydda/retrieval/wind_retrieve.py b/pydda/retrieval/wind_retrieve.py index 8cdccc3b..70cc86d1 100644 --- a/pydda/retrieval/wind_retrieve.py +++ b/pydda/retrieval/wind_retrieve.py @@ -1716,8 +1716,7 @@ def get_dd_wind_field( if isinstance(Grids, list): if isinstance(Grids[0], pyart.core.Grid): - for x in Grids: - new_grids = [read_from_pyart_grid(x) for x in Grids] + new_grids = [read_from_pyart_grid(x) for x in Grids] else: new_grids = Grids elif isinstance(Grids, pyart.core.Grid): diff --git a/pydda/tests/test_retrieval.py b/pydda/tests/test_retrieval.py index a2c5c01e..53bdbbcd 100644 --- a/pydda/tests/test_retrieval.py +++ b/pydda/tests/test_retrieval.py @@ -80,6 +80,30 @@ def test_make_updraft_from_convergence_field(): assert np.ma.max(new_w > 3) +def test_list_of_pyart_grids(): + """A list of several Py-ART Grids is converted once per grid.""" + Grids = [] + for _ in range(2): + Grid = pyart.testing.make_empty_grid( + (20, 40, 40), ((0, 10000), (-20000, 20000), (-20000, 20000)) + ) + Grid.add_field("one_field", {"data": np.ma.ones((20, 40, 40))}) + Grids.append(Grid) + + u = v = w = np.zeros((20, 40, 40)) + new_grids, _ = pydda.retrieval.get_dd_wind_field( + Grids, + u, + v, + w, + max_iterations=1, + vel_name="one_field", + refl_field="one_field", + ) + assert len(new_grids) == 2 + assert all(isinstance(g, xr.Dataset) for g in new_grids) + + def _twpice_grids(): """The TWP-ICE grid pair used by the retrieval tests, with a first guess taken from the sounding (which has w = 0 everywhere)."""