Data quality and storing file

Hi
Wradlib version 2.0.3
Python version 3.12.2
I am using gabella filter on radar data using wradlib. I want to store the filtered data to store the output as cfradial so that it can be used in part easily but during writing file is showing error as below

Cell In[8], line 39
xradar.io.export.to_cfradial2(vol,fid_write)

File ~\anaconda3\envs\wradlib\Lib\site-packages\xradar\io\export\cfradial2.py:85 in to_cfradial2
root.attrs[“history”] += f": xradar v{xradar_version} CfRadial2 export"

KeyError: ‘history’

the code is as follows:
import xarray as xr
import wradlib as wrl
import xradar
import glob
import matplotlib.pyplot as plt

def extract_clutter(da, refl, cmap, wsize=3, thrsnorain=0, tr1=6.0, n_p=6, tr2=1.3, rm_nans=False):
if refl in da.variables:
da = da.assign({cmap: xr.apply_ufunc(
wrl.classify.filter_gabella,
da[refl],
input_core_dims=[[“azimuth”, “range”]],
output_core_dims=[[“azimuth”, “range”]],
dask=“parallelized”,
kwargs=dict(
wsize=wsize,
thrsnorain=thrsnorain,
tr1=tr1,
n_p=n_p,
tr2=tr2,
rm_nans=rm_nans,
),
)})
return da

files = glob.glob(‘BHP*.nc*’)
for ff in range(0,1):
fname= files[ff]
fid_write = “test.nc”
vol = xradar.io.open_cfradial1_datatree(fname)
vol = vol.map_over_subtree(extract_clutter, “reflectivity”, “CMAP”, wsize=3, thrsnorain=0.0, tr1=14, n_p=2, tr2=1.5, rm_nans=False)
xradar.io.export.to_cfradial2(vol,fid_write)

I need help in storing volume data in cfradial format in such a way that it would be easier to handle using part.
kindly have some suggestions.

Hey

If you read your file with:
vol = xradar.io.open_cfradial1_datatree(fname)
Check if your vol has “history” attribute, for example:
print(vol.history)
or
vol.root.attrs['history'] (As the Error message suggests)

If such attribute does not exist, you can manually define one with:
vol.root.attrs['history'] = 'None'
My file had ‘None’ there… I guess you can write here what you wish. But the export wants to add the xradar info there with root.attrs[“history”] += f": xradar v{xradar_version} CfRadial2 export"

Thank you very much for your reply. your suggestion is working and the output is stored in cfradial but I am trying to read the stored cfradial radial file using pyart , the error is displaying as

File ~\anaconda3\envs\wradlib\Lib\site-packages\pyart\io\cfradial.py:150 in read_cfradial
time = _ncvar_to_dict(ncvars[“time”])

I am using
xradar.io.export.to_cfradial2(vol,fid_write, engine = “netcdf4” )
to write cfradial file using Wradlib…

I don’t use PyART myself, therefore I am not familiar with the import structure there. Maybe someone from PyART side can help here.

Just guessing here (never worked with cfradial format myself). You first import cfradial1 files and then export to cfradial2. Could there be any conflict there? Have you checked this tutorial?

Py-ART only supports the reading of cfradial1, not cfradial2, my recommendation would be to read it back in with xradar, then use the Py-ART to xradar bridge, which is described in this example

https://arm-doe.github.io/pyart/examples/xradar/plot_xradar.html

Thanks,

Max

1 Like

Thank you very much for your suggestion./. Now it is working…

2 Likes