Encoders¶
An encoder converts data into a serialisable format that can be written to a
target. Encoders are most commonly used implicitly through
to_target(), but can also be created and used directly.
Note
The encoders are still under development. The most advanced encoder is the GRIB encoder, which can handle a variety of data types. Currently, the other encoders only support trivial encoding of data into a serialisable format. For example, the NetCDF encoder simply calls xarray.to_netcdf() on an xarray Dataset/DataArray.
Creating an encoder¶
Use create_encoder() to instantiate an encoder by name:
>>> from earthkit.data import create_encoder
>>> enc = create_encoder("grib")
>>> enc = create_encoder("grib", template=my_field, shortName="2t")
- create_encoder(name, *args, **kwargs)¶
Create an encoder identified by name.
- Parameters:
name (str) – Name of an encoder (see table below).
args – Positional arguments forwarded to the encoder constructor.
kwargs – Keyword arguments forwarded to the encoder constructor.
- Return type:
Common constructor parameters¶
All encoders accept the following keyword arguments in their constructors:
Parameter |
Description |
|---|---|
|
A default template object used as the basis for encoding. The exact accepted types depend on the encoder. |
|
A dictionary of default metadata applied to every encoded item. Metadata supplied
directly to |
|
Any additional keyword arguments are interpreted as metadata keys and merged into
|
Encoding data¶
All encoders expose an encode() method:
>>> enc = create_encoder("grib", template=field)
>>> result = enc.encode(values=arr, shortName="2t", step=6)
>>> result.to_file(open("out.grib", "wb"))
- Encoder.encode(data=None, values=None, metadata={}, template=None, check_nans=False, missing_value=9999, target=None, **kwargs)¶
Encode data and return an
EncodedDataobject.- Parameters:
data – Earthkit data object to encode (Field, FieldList, xarray Dataset/DataArray, …).
values – Raw values array to encode.
metadata (dict) – Per-call metadata merged on top of the encoder’s default metadata.
template – Per-call template overriding the encoder’s default template.
check_nans (bool) – Replace
NaNvalues with missing_value before encoding.missing_value – Replacement value used when check_nans is
True.target – Target object; some encoders use this to optimise the output format.
- Return type:
Automatic encoder selection¶
When writing to a file target without specifying an encoder explicitly, the encoder is selected automatically from the file extension:
Extension(s) |
Encoder |
|---|---|
|
|
|
|
|
|
|
|
|
|
Available encoders¶
Name |
Description |
Class |
|---|---|---|
|
Encode data as GRIB. Supports Fields, FieldLists, and NumPy arrays. Requires a template or sufficient metadata to construct a valid GRIB message. |
|
|
Encode data as NetCDF via xarray. Accepts Fields, FieldLists, and xarray Datasets/DataArrays. |
|
|
Encode data as GeoTIFF. Requires geo-referenced raster data. |
|
|
Encode tabular data as comma-separated values. |
|
|
Encode data as BUFR (Binary Universal Form for the Representation of meteorological data). |
|
|
Encode data as ODB (Observational DataBase) format. |
|
|
Encode data as a Zarr array store. |
|
|
Encode data as CoverageJSON (CovJSON). |
|
|
Encode data as GeoJSON. |
|
|
Encode data as Met Office PP (UM binary) format. |
|
|
Encode data as plain text. |