GRIB encoder¶
GribEncoder encodes data into GRIB messages.
It is selected automatically when writing to a file with a .grib, .grb,
.grib1, .grib2, .grb1, or .grb2 extension, or explicitly by passing
encoder="grib" to to_target().
Constructing a GribEncoder¶
from earthkit.data import create_encoder
# No preset — template and metadata supplied per encode() call
enc = create_encoder("grib")
# Preset template and metadata applied to every encode() call
enc = create_encoder("grib", template=field, shortName="2t")
Parameter |
Description |
|---|---|
|
Default template used as the basis for every encoded message. Accepts a
|
|
Dictionary of default ecCodes GRIB keys applied to every message. Keys may
optionally be prefixed with |
|
Additional keyword arguments treated as metadata keys, merged into |
The encode() method¶
- GribEncoder.encode(data=None, values=None, metadata={}, template=None, check_nans=True, missing_value=9999, target=None, **kwargs)¶
Encode one or more GRIB messages.
- Parameters:
data – Source data. Accepted types:
Field,FieldList, NumPy array, or xarray Dataset/DataArray. Cannot be combined with bothvaluesandtemplatesimultaneously.values (numpy.ndarray) – Raw values array. Takes precedence over any values carried by
dataortemplatewhen provided. If the array containsNaNvalues they are replaced withmissing_valuewhencheck_nans=True.metadata (dict) – Per-call metadata merged on top of the encoder’s preset metadata. Accepts ecCodes GRIB keys, keys prefixed with
"metadata.", and format-independent dotted keys such as"parameter.shortName".template – Per-call template overriding the encoder’s preset template. Accepted types are the same as for the constructor. Takes precedence over
datawhen forming the GRIB handle, but values are still taken fromdataunlessvaluesis also provided. Cannot be combined with bothdataandvalues.check_nans (bool) – When
True(default),NaNvalues invaluesare replaced withmissing_valueandbitmapPresentis set in the encoded message.missing_value (float) – Replacement value for
NaNentries. Default is9999.target – Target object passed through from
to_target(). Some encoders use this to choose a more efficient output path (e.g. bypassing re-encoding when writing an unchanged GRIB file to disk).kwargs – Additional metadata keys, merged with
metadata.
- Returns:
A
GribEncodedDatainstance when a single message is produced, or a generator ofGribEncodedDataobjects when encoding a FieldList or xarray Dataset.
Combining data, values, and template¶
data, values, and template cannot all be specified at once. When two of
them are provided together the following rules apply:
Combination |
Behaviour |
|---|---|
|
Template comes from |
|
The GRIB handle comes from |
|
The GRIB handle comes from |
Encoding from values and metadata only¶
When neither data nor template is supplied, a new GRIB handle is constructed
entirely from values and metadata. This is an experimental path and only
supports:
Regular lat/lon grids — inferred from a 2-D
valuesshape(Nj, Ni).Reduced Gaussian grids — inferred from a 1-D
valuesshape matching a known Gaussian grid size.
Compulsory metadata keys (such as shortName or param) must be present in
metadata or the encoder’s preset; a ValueError is raised if any are missing.
Templates¶
A template is the GRIB message used as the structural basis for a new encoded message. The encoder copies the template handle and then applies new values and metadata on top.
Accepted template types:
Type |
Description |
|---|---|
Any earthkit-data Field object. Its GRIB handle is cloned as the starting point. |
|
|
An earthkit-data internal GRIB handle wrapper. |
|
A raw GRIB message buffer. Parsed into a handle via ecCodes. |
|
The name of an ecCodes GRIB sample (e.g. |
|
A raw ecCodes handle integer, e.g. obtained from a low-level ecCodes call. |
|
No template. A handle is constructed from |
Metadata keys¶
All three sources of metadata (constructor preset, metadata argument, and
**kwargs) are merged together before encoding, with later sources winning:
Encoder preset (set at construction time).
metadatadict passed toencode().**kwargspassed toencode().
Keys are accepted in three formats:
Plain ecCodes keys — e.g.
shortName="2t",step=6,edition=2.``”metadata.”``-prefixed keys — e.g.
"metadata.shortName"; the prefix is stripped before the key is applied.Format-independent dotted keys — e.g.
"parameter.shortName"; these are applied first to produce a base handle, then any plain ecCodes keys are applied on top.
NaN handling¶
When check_nans=True (the default), the encoder scans values for NaN entries.
If any are found:
NaNvalues are replaced withmissing_value(default9999).bitmapPresent = 1andmissingValue = missing_valueare set on the encoded message automatically.
Usage examples¶
Encode with a preset template and override a metadata key:
import earthkit.data as ekd
from earthkit.data import create_encoder
template = ekd.from_source("sample", "test.grib").to_fieldlist()[0]
encoder = create_encoder("grib", template=template, shortName="msl")
result = encoder.encode(values=template.values + 1.0, step=6)
field = result.to_field()
field.get("parameter.shortName") # "msl"
field.get("step") # 6
Encode without a preset template (template supplied per call):
encoder = create_encoder("grib")
result = encoder.encode(
values=template.values + 1.0,
template=template,
metadata={"shortName": "msl"},
step=6,
)
Encode an entire FieldList and write to a file:
import earthkit.data as ekd
fs = ekd.from_source("sample", "test.grib").to_fieldlist()
ekd.to_target("file", "out.grib", data=fs, encoder="grib")
Encode a FieldList using the encoder directly and iterate over results:
encoder = create_encoder("grib", template=template)
for result in encoder.encode(data=fs):
# result is a GribEncodedData for one field
result.to_file(open("out.grib", "ab"))