earthkit.data.encoders.grib¶
Classes¶
The object representing the encoded GRIB message. |
|
Encoder for GRIB format. |
|
Create a new GribCodesHandle from a template, field or metadata. |
Module Contents¶
- class earthkit.data.encoders.grib.GribEncodedData(handle, template_field=None)¶
Bases:
earthkit.data.encoders.EncodedDataThe object representing the encoded GRIB message.
- get(key, default=None)¶
Get a value from the GRIB message metadata.
- Parameters:
key (
str) – The key of the metadata to retrieve.default (
any, optional) – The default value to return if the key is not found.
- Return type:
The value associated with the key, orthe default value if the key is not found.
- handle¶
- prefer_file_path = False¶
- to_bytes()¶
Return the GRIB message as bytes.
- to_field()¶
Convert the GRIB message to a Field object.
- to_file(f)¶
Write the GRIB message to a file.
- Parameters:
f (
file-like object) – The file-like object to write the GRIB message to.
- class earthkit.data.encoders.grib.GribEncoder(template=None, metadata=None, **kwargs)¶
Bases:
earthkit.data.encoders.EncoderEncoder for GRIB format.
This class is used to encode data to GRIB format via the
encode()method.- Parameters:
template (
Field,GribCodesHandle,bytes,str,int,None) – A preset template to use for encoding whenencode()is called without a template. It can be aField, aGribCodesHandle, a GRIB message as bytes, an ecCodes GRIB sample name as string, a raw ecCodes handle as an integer, or None. Seeencode()for more details on how the template is used.metadata (
dict) – A preset metadata to encode. The keys must be ecCodes GRIB keys, optionally prefixed with “metadata.”. This metadata is used as default whenencode()is called without metadata. If metadata is provided in theencode()method, it is merged with this preset metadata, with the metadata provided in theencode()method taking precedence.kwargs (
dict) – Additional keyword arguments interpreted as metadata to encode. The keys must be ecCodes GRIB keys, optionally prefixed with “metadata.”.
Examples
See the howto examples for more details and examples of encoding GRIB data with
GribEncoder.Using with a preset template and metadata:
>>> import earthkit.data as ekd >>> template = ekd.from_source("sample", "test.grib").to_fieldlist()[0] >>> template.get("metadata.shortName") '2t' >>> encoder = GribEncoder(template=template, metadata={"shortName": "msl"}) >>> d = encoder.encode(values=template.values + 1.0, step=6) >>> f = d.to_field() >>> f.get("parameter.variable") 'msl' >>> f.get("parameter.shortName") 'msl' >>> f.get("parameter.units") 'hPa' >>> f.get("step") 6
Using without preset template and metadata: >>> encoder = GribEncoder() >>> d = encoder.encode(values=template.values + 1.0, metadata={“shortName”: “msl”}, step=6) >>> f = d.to_field() >>> f.get(“parameter.variable”) ‘msl’ >>> f.get(“parameter.shortName”) ‘msl’ >>> f.get(“parameter.units”) ‘hPa’ >>> f.get(“step”) 6
- encode(data=None, values=None, check_nans=True, metadata=None, template=None, missing_value=9999, target=None, **kwargs)¶
Encode new GRIB message(s).
- Parameters:
data (
Field,FieldList,Numpy array, orNone) – The data to encode. Can be aField, aFieldList, a Numpy array, or None. If None, the new GRIB message(s) will be created from thevalues,metadataandtemplate. Cannot be specified together withvaluesandtemplate. Whentemplateis also provided, it will be used as a basis for encoding, but the values will be taken fromdata.values (
numpy.ndarrayorNone) – The values to encode. If None, the values will be taken from thedataortemplate. Takes precedence over the values indataortemplateif any of them are provided. If the values contain NaNs, they will be replaced with themissing_valueprovided. Cannot be specified together withdataandtemplate.check_nans (
bool) – Check for NaNs in the values and replace them withmissing_value.metadata (
dict) – Metadata to encode. The keys can be ecCodes GRIB keys, optionally prefixed with “metadata.” The format independent keys fromFieldmetadata are also accepted. If format independent keys are provided, they are applied first to create a new handle, then if ecCodes GRIB keys are provided too, they are applied on top of the handle.template (
Field,GribCodesHandle,bytes,str,int,None) – A template to use for encoding. It can be aField, aGribCodesHandle, a GRIB message as bytes, an ecCodes GRIB sample name as string, a raw ecCodes handle as an integer, or None. If None, a theGribEncoder.templatewill be used if provided. Otherwise a new handle will be created from thedata,valuesandmetadata. Takes precedence over thedatain forming the new GRIB message, but values are taken from thedataif no provided directly. Cannot be specified together withdataandvalues.missing_value (
float) – The value to use for NaNs. Default is 9999, which is the default missing value used by ecCode when encoding with a template that does not have a valid “bitsPerValue” key.kwargs (
dict) – Additional metadata to encode.
- Returns:
The object representing the encoded GRIB message(s). When a single GRIB message is encoded, a
GribEncodedDataobject is returned. When multiple GRIB messages are encoded, a generator ofGribEncodedDataobjects is returned that can be processed in a loop.- Return type:
GribEncodedDataorgeneratorofGribEncodedData
Notes
data,valuesandtemplatecannot be specified together. If more than one of them are provided, the following rules applied:data,value: The values will be taken from thevaluesargument.data,template: Thetemplatewill be used as a basis for encoding, butthe values will be taken from the
dataargument.
values,template: Thetemplatewill be used as a basis for encoding, butthe values will be taken from the
valuesargument.
When no
dataandtemplateare provided, a new GRIB message will be created from thevaluesandmetadata. This is an experimental feature and only works for certain metadata keys and the grid has to be either global lat-lon or reduced Gaussian grid. The geography is inferred from the shape of the specifiedvalues.Examples
See the howto examples for more details and examples of encoding GRIB data with
GribEncoder.
- metadata¶
- template¶
- class earthkit.data.encoders.grib.GribHandleMaker(template=None)¶
Create a new GribCodesHandle from a template, field or metadata.
- static handle_from_field(field, values_shape=None)¶
- handle_from_metadata(values_shape, metadata, compulsory)¶
- static handle_from_template(template, values_shape=None, field_metadata=None, clone=True)¶
- make(values_shape=None, metadata=None, template=None, field_metadata=None)¶
Create a new GribCodesHandle from a template, field or metadata.
May modify existing metadata
- Parameters:
values_shape (
tuple, optional) – The shape of the values to encodemetadata (
dict, optional) – Metadata to encodetemplate (
GribCoder, optional) – A template to use for encodingfield_metadata (
dict, optional) – Metadata to be set on the field before encoding.
- template¶
- update_metadata_from_template(metadata, template, handle)¶