Skip to content

Commit

Permalink
FIX: Updates code to fix scalar warnings.
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
zssherman committed Oct 23, 2024
1 parent 258fb58 commit 500076a
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 32 deletions.
27 changes: 16 additions & 11 deletions pyart/map/gates_to_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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(radars[0].latitude["data"])
== 1 & len(radars[0].longitude["data"])
== 1 & len(radars[0].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
Expand Down
27 changes: 16 additions & 11 deletions pyart/map/grid_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(radars[0].latitude["data"])
== 1 & len(radars[0].longitude["data"])
== 1 & len(radars[0].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)))

Expand Down
6 changes: 3 additions & 3 deletions pyart/retrieve/kdp_proc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions pyart/util/radar_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion tests/core/test_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand Down

0 comments on commit 500076a

Please sign in to comment.