import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
import numpy as np
import warnings
import wradlib as wrl
warnings.filterwarnings("ignore")
# Assuming grid_files is defined as in your code
grid_files = glob.glob("D:\\radar_data\\radinout\\gridded_20_jun_2022\\gridded_20_jun_2022grid_ZDR_*nc")
radar_lon= 75.8175
radar_radius_km=280.0
cir1=plt.Circle((radar_lon,radar_lat),radar_radius_km/111.0,edgecolor='black',facecolor='none')
cir2=plt.Circle((radar_lon,radar_lat),radar_radius_km/111.0,edgecolor='black',facecolor='none')
rain_rate_stratiform = np.zeros((len(grid_files), 250, 250))
rain_rate_convective= np.zeros((len(grid_files), 250, 250))
for i, file in enumerate(grid_files):
grid_data = pyart.io.read_grid(file)
ref_data = grid_data.fields['REF']['data']
xg_files = grid_data.to_xarray()
Z = wrl.trafo.idecibel(xg_files.REF[0, 0, :, :].values)
#rain_rate = wrl.zr.z_to_r(Z, a=200, b=1.5)
#total_rain_rate[i, :, :] = rain_rate
grid_data = pyart.io.read_grid(grid_file)
xg_files = grid_data.to_xarray()
ref_data = grid_data.fields['REF']['data']
feature_dict = pyart.retrieve.feature_detection(
grid_data,
dx=2000,
dy=2000,
level_m=500,
field="REF",
always_core_thres=30,
bkg_rad_km=20,
use_cosine=True,
max_diff=5,
zero_diff_cos_val=55,
weak_echo_thres=10,
max_rad_km=4,
)
# Reshape the feature_detection field data
feature_data = feature_dict["feature_detection"]["data"][0, :, :]
feature_data_reshaped = np.broadcast_to(feature_data, (grid_data.nz,) + feature_data.shape)
# Mask the feature data
feature_masked = np.ma.masked_equal(feature_data_reshaped, 0)
feature_masked = np.ma.masked_equal(feature_masked, 3)
# Add the masked data to the grid
grid_data.add_field(
"feature_detection", {"data": feature_masked}, replace_existing=True
)
# Masked array for stratiform and convective reflectivity
stratiform_reflectivity = ma.masked_where(feature_masked != 1, ref_data)
convective_reflectivity = ma.masked_where(feature_masked != 2, ref_data)
# Convert reflectivity to rainfall rate using wradlib
Zs_stratiform = wrl.trafo.idecibel(stratiform_reflectivity)
Zs_convective = wrl.trafo.idecibel(convective_reflectivity)
rain_rate_stratiform = wrl.zr.z_to_r(Zs_stratiform, a=251, b=1.48)
rain_rate_convective = wrl.zr.z_to_r(Zs_convective, a=178, b=1.51)
# Apply mask to rainfall rates
rain_rate_stratiform[i, :, :] = np.ma.masked_greater(rain_rate_stratiform, 20)
rain_rate_convective[i, :, :] = np.ma.masked_greater(rain_rate_convective, 20)
# Define the Plate Carree projection
proj = ccrs.PlateCarree()
# Create the figure and axes instances
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 5), subplot_kw={'projection': proj})
# Plot the first contourf
im1 = ax1.contourf(xg_files['lon'].values, xg_files['lat'].values, rain_rate_stratiform,
levels=np.linspace(0, 20, 10), cmap='jet', origin="lower")
# Plot the marker
ax1.plot(75.8175, 26.8207, marker="x", color="black", markersize=15, markeredgewidth=2)
# Add colorbar
cbar1 = plt.colorbar(im1, ax=ax1, label='Rainfall rate in mm/h', ticks=np.arange(0, 21, 2))
cbar1.ax.set_yticklabels(np.arange(0, 21, 2))
# Add title
ax1.set_title("Stratiform Rainfall")
ax1.add_patch(cir1)
# Add gridlines
gl1 = ax1.gridlines(crs=proj, draw_labels=True, color='none')
gl1.xformatter = LONGITUDE_FORMATTER
gl1.yformatter = LATITUDE_FORMATTER
# Add basemap
ax1.add_feature(cfeature.COASTLINE)
ax1.add_feature(cfeature.BORDERS, linewidth=1)
ax1.add_feature(cfeature.STATES, linewidth=1)
# Plot the second contourf
im2 = ax2.contourf(xg_files['lon'].values, xg_files['lat'].values, rain_rate_convective,
levels=np.linspace(0, 20, 10), cmap='jet', origin="lower")
# Plot the marker
ax2.plot(75.8175, 26.8207, marker="x", color="black", markersize=15, markeredgewidth=2)
# Add colorbar
cbar2 = plt.colorbar(im2, ax=ax2, label='Rainfall rate in mm/h', ticks=np.arange(0, 21, 2))
cbar2.ax.set_yticklabels(np.arange(0, 21, 2))
ax2.add_patch(cir2)
# Add title
ax2.set_title("Convective Rainfall")
# Add gridlines
gl2 = ax2.gridlines(crs=proj, draw_labels=True, color='none')
gl2.xformatter = LONGITUDE_FORMATTER
gl2.yformatter = LATITUDE_FORMATTER
# Add basemap
ax2.add_feature(cfeature.COASTLINE)
ax2.add_feature(cfeature.BORDERS, linewidth=1)
ax2.add_feature(cfeature.STATES, linewidth=1)
# Show the plot
plt.show()
this showing error:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[178], line 79
76 rain_rate_convective = wrl.zr.z_to_r(Zs_convective, a=178, b=1.51)
78 # Apply mask to rainfall rates
---> 79 rain_rate_stratiform[i, :, :] = np.ma.masked_greater(rain_rate_stratiform, 20)
80 rain_rate_convective[i, :, :] = np.ma.masked_greater(rain_rate_convective, 20)
82 # Define the Plate Carree projection
File ~\PYTHON\envs\pcf\lib\contextlib.py:79, in ContextDecorator.__call__.<locals>.inner(*args, **kwds)
76 @wraps(func)
77 def inner(*args, **kwds):
78 with self._recreate_cm():
---> 79 return func(*args, **kwds)
File ~\PYTHON\envs\pcf\lib\site-packages\numpy\ma\core.py:3397, in MaskedArray.__setitem__(self, indx, value)
3395 _data[indx.data] = dval
3396 else:
-> 3397 _data[indx] = dval
3398 _mask[indx] = mval
3399 elif hasattr(indx, 'dtype') and (indx.dtype == MaskType):
ValueError: could not broadcast input array from shape (40,250,250) into shape (250,250)