Skip to content

Commit

Permalink
Update rainbow_wrl.py (#1497)
Browse files Browse the repository at this point in the history
* Update rainbow_wrl.py

Prevent program from crashing when bins changes between sweeps.

* ADD: Add new properties for _range in rainbow_wrl.py

Add two properties, "meters_between_gates" and "meters_to_center_of_first_gate", required by the uf format file.

* Update rainbow_wrl.py

Raise error when range resolution changes between sweeps.

* refactor: Fix linting

* refactor: Fix linting
  • Loading branch information
HappyXiaoAnAn authored Jan 10, 2024
1 parent 297e9eb commit 3b3418f
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions pyart/aux_io/rainbow_wrl.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ def read_rainbow_wrl(

if single_slice:
rays_per_sweep[0] = int(common_slice_info["slicedata"]["rawdata"]["@rays"])
nbins = int(common_slice_info["slicedata"]["rawdata"]["@bins"])
maxbin = int(common_slice_info["slicedata"]["rawdata"]["@bins"])
ssri = np.array([0], dtype="int32")
seri = np.array([rays_per_sweep[0] - 1], dtype="int32")
else:
Expand All @@ -263,8 +263,12 @@ def read_rainbow_wrl(

# all sweeps have to have the same number of range bins
if any(nbins_sweep != nbins_sweep[0]):
raise ValueError("number of range bins changes between sweeps")
nbins = nbins_sweep[0]
# raise ValueError("number of range bins changes between sweeps")
print(
"WARNING: number of range bins changes between sweeps "
+ "max number of bins will be used"
)
maxbin = nbins_sweep.max()
ssri = np.cumsum(np.append([0], rays_per_sweep[:-1])).astype("int32")
seri = np.cumsum(rays_per_sweep).astype("int32") - 1

Expand All @@ -274,22 +278,31 @@ def read_rainbow_wrl(
sweep_end_ray_index["data"] = seri

# range
if not single_slice:
# all sweeps have to have the same range resolution
for i in range(nslices):
slice_info = rbf["volume"]["scan"]["slice"][i]
if slice_info["rangestep"] != common_slice_info["rangestep"]:
raise ValueError("range resolution changes between sweeps")

r_res = float(common_slice_info["rangestep"]) * 1000.0
if "start_range" in common_slice_info.keys():
start_range = float(common_slice_info["start_range"]) * 1000.0
else:
start_range = 0.0
_range["data"] = np.linspace(
start_range + r_res / 2.0, float(nbins - 1.0) * r_res + r_res / 2.0, nbins
start_range + r_res / 2.0, float(maxbin - 1.0) * r_res + r_res / 2.0, maxbin
).astype("float32")
_range["meters_between_gates"] = r_res
_range["meters_to_center_of_first_gate"] = _range["data"][0]

# containers for data
t_fixed_angle = np.empty(nslices, dtype="float64")
moving_angle = np.empty(total_rays, dtype="float64")
static_angle = np.empty(total_rays, dtype="float64")
time_data = np.empty(total_rays, dtype="float64")
fdata = np.ma.zeros(
(total_rays, nbins), dtype="float32", fill_value=get_fillvalue()
(total_rays, maxbin), dtype="float32", fill_value=get_fillvalue()
)

# read data from file
Expand Down Expand Up @@ -338,7 +351,10 @@ def read_rainbow_wrl(

# data
fdata[ssri[i] : seri[i] + 1, :] = _get_data(
slice_info["slicedata"]["rawdata"], rays_per_sweep[i], nbins
slice_info["slicedata"]["rawdata"],
rays_per_sweep[i],
nbins_sweep[i],
maxbin,
)

if bfile.endswith(".vol") or bfile.endswith(".azi"):
Expand Down Expand Up @@ -439,7 +455,7 @@ def _extract_angles(data):
return moving_angle, angle_start, angle_stop


def _get_data(rawdata, nrays, nbins):
def _get_data(rawdata, nrays, nbins, maxbin):
"""
Obtains the raw data
Expand Down Expand Up @@ -480,7 +496,12 @@ def _get_data(rawdata, nrays, nbins):
data = np.reshape(data, [nrays, nbins])
mask = np.reshape(mask, [nrays, nbins])

masked_data = np.ma.array(data, mask=mask, fill_value=get_fillvalue())
data_tmp = np.full((nrays, maxbin), get_fillvalue())
data_tmp[:nrays, :nbins] = data
mask_tmp = np.full((nrays, maxbin), True)
mask_tmp[:nrays, :nbins] = mask

masked_data = np.ma.array(data_tmp, mask=mask_tmp, fill_value=get_fillvalue())

return masked_data

Expand Down

0 comments on commit 3b3418f

Please sign in to comment.