openradar/xradar

Tree can't be exported with to_cfradial2 if time is first dimension

Closed this issue · 1 comments

  • xradar version: 0.1.0
  • Python version: 3.11
  • Operating System: Linux

Description

While trying to export a DataTree (opened with open_odim_datatree) with to_cfradial2 an error is raised.

ValueError: cannot swap from dimension 'azimuth' because it is not an existing dimension

This doesn't happen with, if the first_dim-kwarg is omitted.

What I Did

import xarray as xr
import xradar as xd
from open_radar_data import DATASETS
filename = DATASETS.fetch("71_20181220_060628.pvol.h5")
dtree = xd.io.open_odim_datatree(filename, first_dim="time")
xd.io.to_cfradial2(dtree, "odim_as_cfradial2_2.nc")

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Input In [26], in <cell line: 7>()
      5 dtree = xd.io.open_odim_datatree(filename, first_dim="time")
      6 display(dtree)
----> 7 xd.io.to_cfradial2(dtree.copy(), "odim_as_cfradial2_2.nc")

File /home/kai/miniconda/envs/wradlib_310/lib/python3.10/site-packages/xradar/io/export/cfradial2.py:73, in to_cfradial2(dtree, filename, engine, timestep)
     70 for grp in dtree.groups:
     71     if "sweep" in grp:
     72         dtree[grp] = DataTree(
---> 73             conform_cfradial2_sweep_group(
     74                 dtree[grp].to_dataset(), optional=False, dim0="azimuth"
     75             )
     76         )
     78 root = dtree["/"].to_dataset()
     79 # fix Conventions

File /home/kai/miniconda/envs/wradlib_310/lib/python3.10/site-packages/xradar/model.py:1005, in conform_cfradial2_sweep_group(obj, optional, dim0)
   1003 # swap dims, if needed
   1004 if dim0 != "time":
-> 1005     out = out.swap_dims({dim0: "time"})
   1006 # sort in any case
   1007 out = out.sortby("time")

File /home/kai/miniconda/envs/wradlib_310/lib/python3.10/site-packages/xarray/core/dataset.py:3824, in Dataset.swap_dims(self, dims_dict, **dims_kwargs)
   3822 for k, v in dims_dict.items():
   3823     if k not in self.dims:
-> 3824         raise ValueError(
   3825             f"cannot swap from dimension {k!r} because it is "
   3826             "not an existing dimension"
   3827         )
   3828     if v in self.variables and self.variables[v].dims != (k,):
   3829         raise ValueError(
   3830             f"replacement dimension {v!r} is not a 1D "
   3831             f"variable along the old dimension {k!r}"
   3832         )

ValueError: cannot swap from dimension 'azimuth' because it is not an existing dimension

Error Source:

# iterate over DataTree and make subgroups cfradial2 compliant
for grp in dtree.groups:
if "sweep" in grp:
dtree[grp] = DataTree(
conform_cfradial2_sweep_group(
dtree[grp].to_dataset(), optional=False, dim0="azimuth"
)
)

Need to check for dim0 there and do not unconditionally set dim0="azimuth".