From ec7759c31faad5467c35a11f67f8e190670a99e4 Mon Sep 17 00:00:00 2001 From: Zach Sherman <19153455+zssherman@users.noreply.github.com> Date: Wed, 23 Oct 2024 15:27:45 -0500 Subject: [PATCH] FIX: Updates code to fix scalar warnings. (#1673) * FIX: Updates code to fix scalar warnings. I fixed this by calling .items() to pull from the array. Also checks similar to radar utils if the arrays are 1 or more (mobile radar). * FIX: Forgot to check for singular radar later on. --- pyart/map/gates_to_grid.py | 27 ++++++++++++++++----------- pyart/map/grid_mapper.py | 27 ++++++++++++++++----------- pyart/retrieve/kdp_proc.py | 6 +++--- pyart/util/radar_utils.py | 12 ++++++------ tests/core/test_grid.py | 2 +- 5 files changed, 42 insertions(+), 32 deletions(-) diff --git a/pyart/map/gates_to_grid.py b/pyart/map/gates_to_grid.py index 95a6e888248..576afa92252 100644 --- a/pyart/map/gates_to_grid.py +++ b/pyart/map/gates_to_grid.py @@ -114,9 +114,9 @@ def map_gates_to_grid( skip_transform = True if grid_origin_alt is None: - try: - grid_origin_alt = float(radars[0].altitude["data"]) - except TypeError: + if len(radars[0].altitude["data"]) == 1: + grid_origin_alt = float(radars[0].altitude["data"].item()) + else: grid_origin_alt = np.mean(radars[0].altitude["data"]) # convert input h_factor and dist_factor from scalar, tuple, or list to array @@ -242,10 +242,10 @@ def _find_projparams(grid_origin, radars, grid_projection): # parse grid_origin if grid_origin is None: - try: - lat = float(radars[0].latitude["data"]) - lon = float(radars[0].longitude["data"]) - except TypeError: + if len(radars[0].latitude["data"]) == 1 & len(radars[0].longitude["data"]) == 1: + lat = float(radars[0].latitude["data"].item()) + lon = float(radars[0].longitude["data"].item()) + else: lat = np.mean(radars[0].latitude["data"]) lon = np.mean(radars[0].longitude["data"]) grid_origin = (lat, lon) @@ -293,10 +293,15 @@ def _find_offsets(radars, projparams, grid_origin_alt): x_disp, y_disp = geographic_to_cartesian( radar.longitude["data"], radar.latitude["data"], projparams ) - try: - z_disp = float(radar.altitude["data"]) - grid_origin_alt - offsets.append((z_disp, float(y_disp), float(x_disp))) - except TypeError: + if ( + len(radar.latitude["data"]) + == 1 & len(radar.longitude["data"]) + == 1 & len(radar.altitude["data"]) + == 1 + ): + z_disp = float(radar.altitude["data"].item()) - grid_origin_alt + offsets.append((z_disp, float(y_disp.item()), float(x_disp.item()))) + else: z_disp = np.mean(radar.altitude["data"]) - grid_origin_alt offsets.append((z_disp, np.mean(y_disp), np.mean(x_disp))) return offsets diff --git a/pyart/map/grid_mapper.py b/pyart/map/grid_mapper.py index 96c0e60487a..bb4fb6e0b3c 100644 --- a/pyart/map/grid_mapper.py +++ b/pyart/map/grid_mapper.py @@ -481,19 +481,19 @@ def map_to_grid( # find the grid origin if not given if grid_origin is None: - try: - lat = float(radars[0].latitude["data"]) - lon = float(radars[0].longitude["data"]) - except TypeError: + if len(radars[0].latitude["data"]) == 1 & len(radars[0].longitude["data"]) == 1: + lat = float(radars[0].latitude["data"].item()) + lon = float(radars[0].longitude["data"].item()) + else: lat = np.mean(radars[0].latitude["data"]) lon = np.mean(radars[0].longitude["data"]) grid_origin = (lat, lon) grid_origin_lat, grid_origin_lon = grid_origin if grid_origin_alt is None: - try: - grid_origin_alt = float(radars[0].altitude["data"]) - except TypeError: + if len(radars[0].altitude["data"]) == 1: + grid_origin_alt = float(radars[0].altitude["data"].item()) + else: grid_origin_alt = np.mean(radars[0].altitude["data"]) # fields which should be mapped, None for fields which are in all radars @@ -544,10 +544,15 @@ def map_to_grid( x_disp, y_disp = geographic_to_cartesian( radar.longitude["data"], radar.latitude["data"], projparams ) - try: - z_disp = float(radar.altitude["data"]) - grid_origin_alt - offsets.append((z_disp, float(y_disp), float(x_disp))) - except TypeError: + if ( + len(radar.latitude["data"]) + == 1 & len(radar.longitude["data"]) + == 1 & len(radar.altitude["data"]) + == 1 + ): + z_disp = float(radar.altitude["data"].item()) - grid_origin_alt + offsets.append((z_disp, float(y_disp.item()), float(x_disp.item()))) + else: z_disp = np.mean(radar.altitude["data"]) - grid_origin_alt offsets.append((z_disp, np.mean(y_disp), np.mean(x_disp))) diff --git a/pyart/retrieve/kdp_proc.py b/pyart/retrieve/kdp_proc.py index e5c042aa280..7818be1911d 100755 --- a/pyart/retrieve/kdp_proc.py +++ b/pyart/retrieve/kdp_proc.py @@ -301,7 +301,7 @@ def _kdp_estimation_backward_fixed( p = np.dot((identity_i - np.dot(k, h_plus)), p_pred) # Fill the output - kdp[ii] = s[0] + kdp[ii] = s[0].item() kdp_error[ii] = p[0, 0] # Shift @@ -407,9 +407,9 @@ def _kdp_estimation_forward_fixed( p = np.dot((identity_i - np.dot(k, h_plus)), p_pred) # Fill the output - kdp[ii] = s[0] + kdp[ii] = s[0].item() kdp_error[ii] = p[0, 0] - phidp[ii] = s[2] + phidp[ii] = s[2].item() # Shift dummy = np.copy(kdp) diff --git a/pyart/util/radar_utils.py b/pyart/util/radar_utils.py index ba7063a98cb..bc6827e83e2 100644 --- a/pyart/util/radar_utils.py +++ b/pyart/util/radar_utils.py @@ -573,12 +573,12 @@ def join_radar(radar1, radar2): == 1 & len(radar2.altitude["data"]) == 1 ): - lat1 = float(radar1.latitude["data"]) - lon1 = float(radar1.longitude["data"]) - alt1 = float(radar1.altitude["data"]) - lat2 = float(radar2.latitude["data"]) - lon2 = float(radar2.longitude["data"]) - alt2 = float(radar2.altitude["data"]) + lat1 = float(radar1.latitude["data"].item()) + lon1 = float(radar1.longitude["data"].item()) + alt1 = float(radar1.altitude["data"].item()) + lat2 = float(radar2.latitude["data"].item()) + lon2 = float(radar2.longitude["data"].item()) + alt2 = float(radar2.altitude["data"].item()) if (lat1 != lat2) or (lon1 != lon2) or (alt1 != alt2): ones1 = np.ones(len(radar1.time["data"]), dtype="float32") diff --git a/tests/core/test_grid.py b/tests/core/test_grid.py index 0fedbe0450e..986da33dc26 100644 --- a/tests/core/test_grid.py +++ b/tests/core/test_grid.py @@ -92,7 +92,7 @@ def test_grid_to_xarray(): time = np.array([netCDF4.num2date(grid.time["data"][0], grid.time["units"])]) # Check dimensions - assert ds.dims == {"time": 1, "z": 2, "y": 400, "x": 320, "nradar": 1} + assert ds.sizes == {"time": 1, "z": 2, "y": 400, "x": 320, "nradar": 1} # Check coordinate data assert np.array_equal(ds.x.data, grid.x["data"])