XRadar Util#

has_import

get_first_angle

Return name of first angle dimension from given dataset.

get_second_angle

Return name of second angle coordinate from given dataset.

remove_duplicate_rays

Remove duplicate rays.

reindex_angle

Reindex along first angle.

reindex_range

Reindex along range onto a regular grid.

extract_angle_parameters

Extract sweep angle parameters from dataset.

ipol_time

Interpolate/extrapolate missing time steps (NaT).

rolling_dim

Return array with rolling dimension of window-length added at the end.

get_sweep_keys

Return which nodes in the datatree contain sweep variables

get_ray_dim

Return the ray dimension (azimuth/elevation/time) of a radar object.

is_sweep

Check whether a Dataset holds a radar sweep.

apply_to_sweeps

Applies a given function to all sweep nodes in the radar volume.

apply_to_volume

Alias for apply_to_sweeps.

map_over_sweeps

Decorator to apply a function only to sweep nodes in a DataTree.

get_sweep_dataset_vars

Get the sweep dataset variables, i.e. variables with (time, range) dimensions.

get_sweep_metadata_vars

Get the sweep metadata variables, i.e. variables without (time, range) dimensions.

select_sweep_dataset_vars

Select sweep dataset variables while keeping metadata variables.

create_volume

Combine sweeps into a single stacked radar volume with optional time and angle filtering.

xradar.util.apply_to_sweeps(dtree, func, *args, **kwargs)[source]#

Applies a given function to all sweep nodes in the radar volume.

Parameters:
  • dtree (xarray.DataTree) – The DataTree object representing the radar volume.

  • func (function) – The function to apply to each sweep.

  • *args (tuple) – Additional positional arguments to pass to the function.

  • **kwargs (dict) – Additional keyword arguments to pass to the function. The inherit key, if present, is popped and forwarded to xarray.DataTree.to_dataset() instead of func. Accepted values (see xarray docs for details):

    • True or "indexes": inherit only indexed coordinates.

    • "all_coords": inherit all coordinates, including non-index coordinates.

    • False: only include coordinates defined at the sweep node.

    Defaults to "all_coords" for backward compatibility.

Returns:

xarray.DataTree – A new DataTree object with the function applied to all sweeps.

xradar.util.apply_to_volume(dtree, func, *args, **kwargs)[source]#

Alias for apply_to_sweeps. Applies a given function to all sweep nodes in the radar volume.

Parameters:
  • dtree (xarray.DataTree) – The DataTree object representing the radar volume.

  • func (function) – The function to apply to each sweep.

  • *args (tuple) – Additional positional arguments to pass to the function.

  • **kwargs (dict) – Additional keyword arguments to pass to the function.

Returns:

xarray.DataTree – A new DataTree object with the function applied to all sweeps.

xradar.util.create_volume(sweeps, time_coverage_start=None, time_coverage_end=None, min_angle=None, max_angle=None, volume_number=0)[source]#

Combine sweeps into a single stacked radar volume with optional time and angle filtering.

xradar.util.extract_angle_parameters(ds)[source]#

Extract sweep angle parameters from dataset.

Parameters:

ds (xarray.Dataset) – Dateset to reindex first angle.

Returns:

angle_dict (dict) – Dictionary with parameters.

xradar.util.get_first_angle(ds)[source]#

Return name of first angle dimension from given dataset.

Parameters:

ds (xarray.Dataset) – Dateset to get first angle name from.

Returns:

first_angle (str) – Name of first angle dimension.

xradar.util.get_ray_dim(obj)[source]#

Return the ray dimension (azimuth/elevation/time) of a radar object.

Adapted from wradlib.util.dim0().

Parameters:

obj (xarray.Dataset or xarray.DataArray)

Returns:

ray_dim (str or None) – azimuth or elevation if present, else time. None if obj has no range dimension.

Raises:

ValueError – If obj has a range dimension, but no ray dimension, or both azimuth and elevation dimensions.

xradar.util.get_second_angle(ds)[source]#

Return name of second angle coordinate from given dataset.

Parameters:

ds (xarray.Dataset) – Dateset to get second angle name from.

Returns:

out (str) – Name of second angle coordinate.

xradar.util.get_sweep_dataset_vars(sweep, non_standard=False)[source]#

Get the sweep dataset variables, i.e. variables with (time, range) dimensions.

Parameters:
  • sweep (xarray.Dataset) – radar sweep dataset following WMO conventions

  • non_standard (boolean) – true to return also non-standard dataset variables

Returns:

dataset_vars (list of strings) – dataset variables

xradar.util.get_sweep_keys(dtree)[source]#

Return which nodes in the datatree contain sweep variables

Parameters:

dtree (xarray.DataTree) – Datatree to check for sweep_n keys

Returns:

keys (list) – List of associated keys with sweep_n

xradar.util.get_sweep_metadata_vars(sweep, optional=False)[source]#

Get the sweep metadata variables, i.e. variables without (time, range) dimensions.

Parameters:
  • sweep (xarray.Dataset) – radar sweep dataset following WMO conventions

  • optional (boolean) – true to return also optional metadata

Returns:

metadata_vars (dict) – Dictionary of DataArray objects corresponding to metadata variables

xradar.util.has_import(pkg_name)[source]#
xradar.util.ipol_time(ds, *, a1gate_idx=None, direction=None, **kwargs)[source]#

Interpolate/extrapolate missing time steps (NaT).

Parameters:

ds (xarray.Dataset) – Dataset to interpolate/extrapolate missing timesteps.

Keyword Arguments:
  • a1gate_idx (int | None) – First measured gate. 0 assumed, if None.

  • direction (int | None) – 1: CW, -1: CCW, Clockwise assumed, if None.

Returns:

ds (xarray.Dataset) – Dataset with interpolated/extrapolated timesteps.

xradar.util.is_sweep(obj, strict=False)[source]#

Check whether a Dataset holds a radar sweep.

The check is based on the structure, not on names:

  • a range dimension and one ray dimension (time, azimuth or elevation),

  • azimuth and elevation variables along the ray dimension,

  • at least one data variable with dimensions (ray, range).

With strict=True the mandatory sweep metadata variables are required, too (sweep_number, sweep_mode, follow_mode, prt_mode, sweep_fixed_angle, see FM301 Table 301-7a). Not all backends fill these yet (e.g. HPL and Metek), so their sweeps are only recognized with strict=False.

Parameters:
  • obj (xarray.Dataset or xarray.DataTree) – Dataset or DataTree node to check.

  • strict (bool, optional) – Also require the mandatory sweep metadata variables. Defaults to False.

Returns:

sweep (bool) – True if obj holds a radar sweep.

xradar.util.map_over_sweeps(func)[source]#

Decorator to apply a function only to sweep nodes in a DataTree.

The function is applied to the sweep nodes (sweep_*), all other nodes are left unchanged.

Parameters:

func (callable()) – A function that operates on an xarray Dataset. The function must accept a Dataset as its first argument and return a modified Dataset.

Returns:

callable() – A function that can be applied to all sweep nodes in a DataTree.

Examples

>>> @map_over_sweeps
>>> def calculate_rain_rate(ds, ref_field='DBZH'):
>>>     # Function logic to calculate rain rate
>>>     return ds
xradar.util.reindex_angle(ds, start_angle=None, stop_angle=None, angle_res=None, direction=None, method='nearest', tolerance=None)[source]#

Reindex along first angle.

Missing values will be filled by variable’s _FillValue.

Parameters:

ds (xarray.Dataset) – Dateset to reindex first angle.

Keyword Arguments:
  • start_angle (float) – Start angle of dataset.

  • stop_angle (float) – Stop angle of dataset.

  • angle_res (float) – Angle resolution of the dataset.

  • direction (int) – Sweep direction, -1 -> CCW, 1 -> CW.

  • method (str) – Reindexing method, defaults to “nearest”. See xarray.Dataset.reindex().

  • tolerance (float) – Angle tolerance up to which angles should be considered for used method. Defaults to angle_res / 2.

Returns:

ds (xarray.Dataset) – Reindexed dataset

xradar.util.reindex_range(ds, start_range=None, stop_range=None, range_res=None, method='nearest', tolerance=None)[source]#

Reindex along range onto a regular grid.

Useful to align sweeps whose range coordinates differ slightly (e.g. by floating point jitter of the first gate) before combining them.

Missing values will be filled by variable’s _FillValue.

Parameters:

ds (xarray.Dataset) – Dataset to reindex range.

Keyword Arguments:
  • start_range (float) – Range of the first gate center. Defaults to the first range value.

  • stop_range (float) – Range of the last gate center (inclusive). Defaults to the last range value.

  • range_res (float) – Gate spacing. Defaults to the median range spacing.

  • method (str) – Reindexing method, defaults to “nearest”. See xarray.Dataset.reindex().

  • tolerance (float) – Range tolerance up to which gates should be considered for used method. Defaults to range_res / 2.

Returns:

ds (xarray.Dataset) – Reindexed dataset

xradar.util.remove_duplicate_rays(ds)[source]#

Remove duplicate rays.

Parameters:

ds (xarray.Dataset) – Dateset to remove duplicate rays.

Returns:

ds (xarray.Dataset) – Dataset with duplicate rays removed

xradar.util.rolling_dim(data, window)[source]#

Return array with rolling dimension of window-length added at the end.

xradar.util.select_sweep_dataset_vars(sweep, select, ancillary=False, optional_metadata=False)[source]#

Select sweep dataset variables while keeping metadata variables.

Parameters:
  • sweep (xarray.Dataset) – radar sweep dataset following WMO conventions

  • select (list of strings) – dataset variables to be selected

  • ancillary (boolean) – true to keep ancillary variables associated with dataset variables

  • optional_metadata (boolean) – true to return optional metadata

Returns:

sweep_out (str) – sweep with selected dataset variables