pylawr.field#

pylawr.RadarField(data)

RadarField is a xarray.DataArray accessor to extend xarray for radar processing.

pylawr.field.array_has_tag(array, tag)

Check if an array has a given tag

pylawr.field.get_verified_grid(array[, grid])

Get a verified grid from the array and given grid.

pylawr.field.tag_array(array, tag)

Attempt to add_tag() to a given array

pylawr.field.untag_array(array, tag)

Remove a tag from an array with remove_tag()

class pylawr.RadarField(data)[source]#

Bases: object

RadarField is a xarray.DataArray accessor to extend xarray for radar processing. The accessor is called with xarray.DataArray.lawr.

Parameters

data (xarray.DataArray) – This accessor is valid for this given data array.

grid#

The grid of this RadarField. The grid is used for localization, interpolation and plotting purpose.

Type

child of BaseGrid

_convert_field(conv_func, target_var='dbz')[source]#
_dbr_to_z()[source]#
_dbz_to_z()[source]#
_decibel(x, dbzmin=-32.5)[source]#

Converts to decibel unit.

\[\mathrm{decibel} = 10 \cdot \log_{10}(\mathrm{linear})\]

Note

Due to conventions: Linear reflectivities smaller than or equal to \(0\,\mathrm{mm^6\,m^{-3}}\) will be transformed to \(-32.5\,\mathrm{dBZ}\) in decibel unit.

Parameters

x (xarray.DataArray) – The radar field with linear non-decibel unit.

Returns

_linear(x, dbzmin=-32.5)[source]#

Converts to linear unit.

\[\mathrm{linear} = 10^{\frac{\mathrm{decibel}}{10}}\]

Note

Due to conventions: Reflectivities in decibel unit smaller than or equal to \(-32\,\mathrm{dBZ}\) will be transformed to \(0\,\mathrm{mm^6\,m^{-3}}\) in linear unit.

Parameters

x (xarray.DataArray) – The radar field with decibel unit.

Returns

_r_to_z()[source]#
_rainrate(x, a=200.0, b=1.6)[source]#

Converts to rain rate r via Z/R relationship.

\[r = \sqrt[b]{\frac{z}{a}}\]
Parameters
  • x (int, float or array (radar reflectivity factor, mm**6/m**3)) –

  • a (float, optional) – Parameter a of the Z/R relationship. This is the scale factor of the Z/R relationship. Default is 200.0 from Marshall-Palmer.

  • b (float, optional) – Parameter b of the Z/R relationship. This is the polynomial grade factor of the Z/R relationship. Default is 1.6 from Marshall-Palmer.

Return type

converted x in rain rate (mm/h)

_reflectivity(x, a=200.0, b=1.6)[source]#

Converts to radar reflectivity factor via Z/R relationship.

Parameters
  • x (int, float or array (rain rate, mm/h)) –

  • a (float, optional) – Parameter a of the Z/R relationship. This is the scale factor of the Z/R relationship. Default is 200.0 from Marshall-Palmer.

  • b (float, optional) – Parameter b of the Z/R relationship. This is the polynomial grade factor of the Z/R relationship. Default is 1.6 from Marshall-Palmer.

Return type

converted x in radar reflectivity factor (mm**6/mm**3)

_z_to_z()[source]#
add_tag(newtag)[source]#

Add a tag to tags

Parameters

newtag (str) – The new tag to add

check_grid(grid)[source]#

Check if the given grid is a valid grid for this DataArray.

Parameters

grid (child of BaseGrid) – This grid is checked.

Returns

checked_grid – The grid object is returned if it is valid.

Return type

child of BaseGrid

property data#

Get the data for this accessor.

Returns

The data for this xarray.DataArray accessor.

Return type

xarray.DataArray

db_to_linear(inverse=False)[source]#

Transform this rain field from decibel units to linear units.

\[ \begin{align}\begin{aligned}\mathrm{linear} &= 10^{\frac{\mathrm{decibel}}{10}}\\\mathrm{decibel} &= 10 \cdot \log_{10}(\mathrm{linear})\end{aligned}\end{align} \]

Note

Due to conventions: Linear reflectivities smaller than or equal to \(0\,\mathrm{mm^6\,m^{-3}}\) will be transformed to \(-32.5\,\mathrm{dBZ}\) in decibel unit and vice versa.

Parameters

inverse (bool, optional) – Default is False.

Returns

converted_field – The converted rain field with Gaussian decibel or linear non-decibel units. The field has the same grid as this DataArray. The target variable is determined automatically. If name is None, the target variable will be either linear_reflectivity or reflectivity.

Return type

xarray.DataArray

filter(filter_class, *args, **kwargs)[source]#

Method to apply a filter to this class.

Parameters
  • filter_class (Python obj) – This instance of a python class is used to filter the data. The class needs a transform method with data as first argument and grid as keyword argument. The return value of the transform method should be a xarray.DataArray. A possible second return value is interpreted as new grid.

  • *args – The variable length argument list is passed to the transform method of the filter_class.

  • **kwargs – The variable keyword argument dictionary is passed to the transform method of the filter_class. This argument could be used if another array is necessary to transform this array with the given filter class.

Returns

filtered_data – The filtered data array, with the set grid.

Return type

xarray.DataArray

get_rain_mask(threshold=5)[source]#
property grid#
grid_to_array()[source]#

Convert the array to a DataArray with the grid as attributes and variables.

Returns

combined_ds – The DataArray with the added grid.

Return type

xarray.DataArray

classmethod load(load_path)[source]#

The RadarField is loaded from a NetCDF-file. This method should be only used if the field is saved with the ‘to_netcdf’ method.

Parameters

load_path (str) – Path where the file is saved. The file need to be a NetCDF.file.

Returns

loaded_field – The loaded radar field.k

Return type

xarray.DataArray

remove_tag(tag)[source]#

Remove a tag (and any duplicates) from tags. Don’t complain if the tag doesn’t exist.

Parameters

tag (str) – The tag to remove

set_grid_coordinates(grid)[source]#

Set the grid coordinates for this DataArray. The coordinates of this DataArray are replaced by the coordinates of the grid.

Parameters

grid (child of BaseGrid) – This grid is used to set the grid and the grid coordinates of the returned array.

Returns

gridded_array – The DataArray with the grid coordinates and the grid.

Return type

xarray.DataArray

set_metadata(other_array)[source]#

Set the metadata for this array based on the given other array. The metadata is the name, the attributes and the grid.

Parameters

other_array (xarray.DataArray) – The other_array is used to extract the metadata for the data of this class.

Returns

attributed_array – This array with replaced name, attributes and grid from other_array.

Return type

xarray.DataArray

set_variable(name)[source]#

Set the variable name of this DataArray to the given name. The unit, long name and short name are set according to the naming convention defined in utilities/conventions.py.

Parameters

name (str) – The name of the variable. This name needs to be defined in the naming convention.

Returns

named_array – The DataArray with the set name, unit, long name and short name. All other attributes will be also set. If a grid is defined for this DataArray it will be carried over.

Return type

xarray.DataArray

property tags#

Tags describing what has already happened to this field

Type

list

Getter

Converts the RadarField.data xarray.DataArray.attrs "tags" to a list

Setter

Sets the RadarField.data xarray.DataArray.attrs "tags" key

to_dbz()[source]#

Convert the rain field to reflectivity in decibel from any arbitrary variable. If no variable is specified in the attributes, it is assumed that the rain field is the reflectivity in decibel. If the variable of this DataArray is rain_rate zr_convert with default values will be used.

Returns

converted_field – The converted rain field as reflectivity in dB. The field has the same grid and attributes (except for the variable unit and names) as this DataArray.

Return type

xarray.DataArray

to_netcdf(save_path)[source]#

Save the RadarField to given save path as NetCDF-file. The grid is added to the attributes such that it can be restored from the attributes.

Parameters

save_path (str) – Path where the file should be saved. If the path is a directory the file name is inferred by the radar and grid.

to_z()[source]#

Convert the rain field to linear reflectivity from any arbitrary variable. If no variable is specified in the attributes, it is assumed that the rain field is the reflectivity in decibel. If the variable of this DataArray is rain_rate zr_convert with default values will be used.

Returns

converted_field – The converted rain field as linear reflectivity. The field has the same grid and attributes (except for the variable unit and names) as this DataArray.

Return type

xarray.DataArray

zr_convert(a=200.0, b=1.6, inverse=False)[source]#

Convert from z to r via Z/R relationship. The Z/R relationship is calculated with

\[r = \sqrt[b]{\frac{z}{a}}\]

for conversion from z to r. If inverse is chosen, then the unit will be converted from r to z with:

\[z = a \cdot r^{b}\]
Parameters
  • a (float, optional) – Parameter a of the Z/R relationship. This is the scale factor of the Z/R relationship. Default is 200.0 from Marshall-Palmer.

  • b (float, optional) – Parameter b of the Z/R relationship. This is the polynomial grade factor of the Z/R relationship. Default is 1.6 from Marshall-Palmer.

  • inverse (bool, optional) – If the reflectivity should be calculated based on a rain rate field (True) or if the rain rate should be calculated based on the reflectivity (False). Default is False.

Returns

converted_field – The converted rain field with r or z as unit and the same grid as this DataArray.

Return type

xarray.DataArray

Warning

UserWarning :

An UserWarning will be raised if the rain field is already the right variable or if the field is in decibel units.

pylawr.field.array_has_tag(array, tag)[source]#

Check if an array has a given tag

Parameters
  • array (array-like) – the array to check

  • tag (str) – the tag to look for

Returns

Whether the array has the tag

Return type

bool

pylawr.field.get_verified_grid(array, grid=None)[source]#

Get a verified grid from the array and given grid.

Parameters
  • array (xarray.DataArray) – If no grid is given, this array will be used to determine the grid. The grid is verified to this array.

  • grid (child of BaseGrid or None) – This grid is verified within this method. If no grid is given, the grid from the array is used.

Returns

verified_grid – The verified and inferred grid. If a grid is given and verified, this will be the same grid as given.

Return type

child of BaseGrid

Raises
  • AttributeError – An AttributeError is raised if no grid was specified and no grid is set for the array.

  • TypeError – A TypeError is raised if the given grid is not a valid grid.

  • ValueError – A ValueError is raised if the given grid does not have the same shape as the given array.

pylawr.field.tag_array(array, tag)[source]#

Attempt to add_tag() to a given array

Parameters
  • array (array-like) – the array to tag

  • tag (str) – the tag to add to the array

Returns

If tagging was successful or not

Return type

bool

pylawr.field.untag_array(array, tag)[source]#

Remove a tag from an array with remove_tag()

Parameters
  • array (array-like) – the array to operate on

  • tag (str) – the tag to remove

Returns

Whether the tag has been removed

Return type

bool