pylawr.datahandler#
The data handler is responsible for a single opened file. |
|
The HDF5Handler is constructed to read in DWD HDF5 files for single radar sites. |
|
The LawrHandler is constructed to read in LawrText files. |
pylawr.datahandler.base.DataHandler#
- class pylawr.datahandler.base.DataHandler(fh)[source]#
Bases:
objectThe 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.
- property available_dates#
- 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:
DataHandlerThe 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
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
- 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.datetime64or 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, thenset_grid_coordinates()with this grid will be called.
- Returns
dataarray – The data array with the set coordinates.
- Return type
- _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.DataArrayfrom given node.- Parameters
node (
h5py.Group) – Thexarray.DataArrayis 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
- _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
- _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
- property file#
- get_datasets()[source]#
Get all datasets within the hdf5 file as
xarray.Dataset. The data nodes of the datasets are transformed intoxarray.DataArray.- Returns
datasets – A dict with all extracted datasets from this HDF5 file. The data nodes are transformed into
xarray.DataArraywith 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
- 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
pylawr.datahandler.LawrHandler#
- class pylawr.datahandler.LawrHandler(fh)[source]#
Bases:
DataHandlerThe 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.
- 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
- 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.
- 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.