pylawr.datahandler#

pylawr.datahandler.base.DataHandler(fh)

The data handler is responsible for a single opened file.

pylawr.datahandler.DWDHDF5Handler(fh)

The HDF5Handler is constructed to read in DWD HDF5 files for single radar sites.

pylawr.datahandler.LawrHandler(fh)

The LawrHandler is constructed to read in LawrText files.

pylawr.datahandler.base.DataHandler#

class pylawr.datahandler.base.DataHandler(fh)[source]#

Bases: object

The data handler is responsible for a single opened file. The data handler has methods to decode the data from this file.

Parameters

fh (filelike object) – The data is read from this filelike object.

abstract _get_available_dates()[source]#
property available_dates#
close()[source]#
property fh#
abstract get_reflectivity()[source]#

Get the radar reflectivity from this file handler.

Returns

radar_field – The radar field from this handler with the date, azimuth and range as dimension. The attributes are the decoded header information.

Return type

xarray.DataArray or None

property grid#

The get_grid method could be used to decode the grid from the file.

Returns

grid – The readed grid from the file.

Return type

Gridlike type

Raises

ValueError – The grid couldn’t decoded from the file.

pylawr.datahandler.DWDHDF5Handler#

class pylawr.datahandler.DWDHDF5Handler(fh)[source]#

Bases: DataHandler

The HDF5Handler is constructed to read in DWD HDF5 files for single radar sites. DWD’s HDF5 files are following the OPERA Data Information Model (ODIM) such that it should be possible to read in any ODIM HDF5 file. All methods are written to extract horizontal reflectivity from DWD radars, except pylawr.datahandler.hdf5.DWDHDF5Handler.get_datasets().

Parameters

fh (filelike object) – The data is read from this filelike object.

static _bytes_to_hdf5(byte_data)[source]#

Method to read in a byte string as HDF5-file. This is based on: https://stackoverflow.com/a/45900556/4750376

Returns

hdf5_file – The opened HDF5-file with data from given byte string and an temporary filename, which doesn’t exist.

Return type

h5py.File

Warning

This method is not back-checked with unittest!

static _decode_datetime_from_node(node)[source]#

Decode startdate and starttime string from given node into a datatime object.

Parameters

node (h5py.Group) – The datetime is decoded from this dataset. startdate and starttime of the what node of this dataset are used to decode the datetime object.

Returns

ds_datetime – The decoded dataset datetime as numpy.datetime64 object, because xarray uses numpy.datetime64 to represent datetime data. The timezone UTC is assumed as specified in the ODIM documentation.

Return type

numpy.datetime64

static _define_data_coords(dataarray, valid_dt=None, grid=None)[source]#

Define the coordinates of the given dataarray.

Parameters
  • dataarray (xarray.DataArray) – The coordinates of this data array will be set.

  • valid_dt (numpy.datetime64 or None) – If this datetime is set, then a time axis will be prepended with this datetime as value.

  • grid (child of BaseGrid) – If this grid is set, then set_grid_coordinates() with this grid will be called.

Returns

dataarray – The data array with the set coordinates.

Return type

xarray.DataArray

_get_available_dates()[source]#
_get_center()[source]#

Get the center from global where attributes within the HDF5 file.

Returns

  • lat (float) – Longitude position of the radar antenna (degrees), normalized to the WGS-84 reference ellipsoid and datum. Fractions of a degree are given in decimal notation.

  • lon (float) – Latitude position of the radar antenna (degrees), normalized to the WGS-84 reference ellipsoid and datum. Fractions of a degree are given in decimal notation.

  • height (float) – Height of the centre of the antenna in meters above sea level.

_get_dataarray_from_node(node)[source]#

Get a xarray.DataArray from given node.

Parameters

node (h5py.Group) – The xarray.DataArray is created for this HDF5 node. The data is extracted from data within this node.

Returns

dataarray – The extracted dataarray from given node. All correction mechanisms with gain, offset, nodata and undetect are already applied.

Return type

xarray.DataArray

_get_dataset_from_node(node)[source]#
_get_grid_data(node)[source]#

Extract informations of the grid

Parameters

node (h5py.Group) – The grid data is extracted for this HDF5 group. The group needs a where node with nrays, nbins, rscale, rstart, elangle as attributes. If these attributes cannot be extracted, a GridNotAvailableError will be raised.

Returns

grid_data – The grid dict, which can be used to construct a PolarGrid.

Return type

dict

_get_grid_from_node(node)[source]#
_get_metadata_from_node(node)[source]#

Decode the metadata from given dataset into a dictionary.

Parameters

node (h5py.Group) – The metadata is extracted for this HDF5 group. To extract the metadata the attributes of /, where, what, how are used.

Returns

metadata – The extracted metadata from the given node. All keys with an array as value will be deleted. All byte values will be decoded into UTF-8 strings.

Return type

dict(str)

property file#
get_datasets()[source]#

Get all datasets within the hdf5 file as xarray.Dataset. The data nodes of the datasets are transformed into xarray.DataArray.

Returns

datasets – A dict with all extracted datasets from this HDF5 file. The data nodes are transformed into xarray.DataArray with quantity as their array name. The attributes within where, what and how nodes are transformed into attributes of the datasets resp. dataarrays.

Return type

dict(xarray.Dataset)

get_reflectivity(var='DBZH')[source]#

Get the reflectivity from this array.

Parameters

var (str) – The variable name of the reflectivity. This variable need to have dBZ as unit within the ODIM HDF5-file. This variable has a lower priority than data_name. `

Returns

reflectivity – The decoded reflectivity from dataset1 within the HDF5 file in dBZ. If possible, the grid is set for this reflectivity.

Return type

xarray.DataArray

property grid#

The get_grid method could be used to decode the grid from the file.

Returns

grid – The readed grid from the file.

Return type

Gridlike type

Raises

ValueError – The grid couldn’t decoded from the file.

hdf5_validation(hdf5_file)[source]#

Method to validate an opened HDF5 file. It is checked if object and version attributes within what group are the same as specified. :param hdf5_file: An opened HDF5 file, which should be validated. :type hdf5_file: h5py.File

Returns

valid – If the given HDF5 file is valid or not.

Return type

bool

pylawr.datahandler.LawrHandler#

class pylawr.datahandler.LawrHandler(fh)[source]#

Bases: DataHandler

The LawrHandler is constructed to read in LawrText files.

Parameters

fh (filelike object) – The data is read from this filelike object.

_decode_data(data_lines)[source]#

Decode the given data lines into a numpy array.

Parameters

data_lines (list(str)) – The data lines which should be decoded.

Returns

decoded_array – The decoded data array. The first dimension are the azimuth angles, the second dimension is the range.

Return type

numpy.ndarray

static _decode_datetime(dt_str, tz_str=None)[source]#

Decode a given datetime string to a datetime object in UTC.

Parameters
  • dt_str (str) – This datetime string is decoded and transformed to a datetime object. The format should be %y%m%d%H%M%S.

  • tz_str (str or None, optional) – The timezone information string. The timezone is converted to pytz timezone. If the timezone is None, the timezone will not be set. Default is None.

Returns

decoded_dt – The decoded datetime object. If a timzone string was given, the object will be localized and returned in UTC without timezone information.

Return type

datetime.datetime

_decode_header(header)[source]#

Decode the given header line into a lawr header.

Parameters

header (str) – This header line is decoded.

Returns

decoded_header – The decoded header dict.

Return type

dict(str, value)

static _decode_keyval(header_list)[source]#

A key value dict will be created by given list. Equal signs are searched, the list value before the sign is used as key and the value after the sign is used as value.

Parameters

header_list (list(str)) – The equal sign are searched within this list.

Returns

decoded_keyval – The decoded key-value dictionary. If the dict is empty there was no key value pair found.

Return type

dict(str, float)

_get_available_dates()[source]#

Get a list of the available dates as datetime.datetime.

static _prepare_dataline(data_line)[source]#

Clean and split the given data line.

Parameters

data_line (str) – This data line is cleaned and split.

Returns

prepared_line – The cleaned and split data line.

Return type

list

static _prepare_header(header)[source]#

Clean the header string and split the header string.

Parameters

header (str) – This header is cleaned and splitted.

Returns

prepared_header – The cleaned and splitted substrings of the header.

Return type

list

property file#
get_reflectivity()[source]#

Get the data from the file handler.

Returns

radar_field – The decoded radar field.

Return type

xr.DataArray

property header#

Get the decoded header of the file as dict.