XRadar Util#
Return name of first angle dimension from given dataset. |
|
Return name of second angle coordinate from given dataset. |
|
Remove duplicate rays. |
|
Reindex along first angle. |
|
Reindex along range onto a regular grid. |
|
Extract sweep angle parameters from dataset. |
|
Interpolate/extrapolate missing time steps (NaT). |
|
Return array with rolling dimension of window-length added at the end. |
|
Return which nodes in the datatree contain sweep variables |
|
Return the ray dimension (azimuth/elevation/time) of a radar object. |
|
Check whether a Dataset holds a radar sweep. |
|
Applies a given function to all sweep nodes in the radar volume. |
|
Alias for apply_to_sweeps. |
|
Decorator to apply a function only to sweep nodes in a DataTree. |
|
Get the sweep dataset variables, i.e. variables with (time, range) dimensions. |
|
Get the sweep metadata variables, i.e. variables without (time, range) dimensions. |
|
Select sweep dataset variables while keeping metadata variables. |
|
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. Theinheritkey, if present, is popped and forwarded toxarray.DataTree.to_dataset()instead of func. Accepted values (see xarray docs for details):Trueor"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.Datasetorxarray.DataArray)- Returns:
ray_dim (
strorNone) –azimuthorelevationif present, elsetime. None ifobjhas norangedimension.- Raises:
ValueError – If
objhas arangedimension, but no ray dimension, or bothazimuthandelevationdimensions.
- 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 conventionsnon_standard (
boolean) – true to return also non-standard dataset variables
- Returns:
dataset_vars (
listofstrings) – 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 conventionsoptional (
boolean) – true to return also optional metadata
- Returns:
metadata_vars (
dict) – Dictionary of DataArray objects corresponding to metadata variables
- 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
rangedimension and one ray dimension (time,azimuthorelevation),azimuthandelevationvariables along the ray dimension,at least one data variable with dimensions (ray,
range).
With
strict=Truethe 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 withstrict=False.- Parameters:
obj (
xarray.Datasetorxarray.DataTree) – Dataset or DataTree node to check.strict (
bool, optional) – Also require the mandatory sweep metadata variables. Defaults to False.
- Returns:
sweep (
bool) – True ifobjholds 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”. Seexarray.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”. Seexarray.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 conventionsselect (
listofstrings) – dataset variables to be selectedancillary (
boolean) – true to keep ancillary variables associated with dataset variablesoptional_metadata (
boolean) – true to return optional metadata
- Returns:
sweep_out (
str) – sweep with selected dataset variables