Writing GRIB to file-pattern target¶
[1]:
# get input GRIB data
import earthkit.data as ekd
ds = ekd.from_source("sample", "tuv_pl.grib").to_fieldlist()
Using to_target() on the data object¶
We use to_target() to write the GRIB fieldlist/field with a :ref:`targets-file-pattern`to disc. The encoder is automatically guessed from the input data. We use the following output file name pattern:
[2]:
# for each shortName-level combination a separate output file will be created
out_pattern = "_res_pattern_{metadata.shortName}_{metadata.level}.grib"
[3]:
# writing only temperature fields
ds.sel({"parameter.variable": "t"}).to_target("file-pattern", out_pattern)
# writing a whole fieldlist
ds.to_target("file-pattern", out_pattern)
# checking one output file
ekd.from_source("file", "_res_pattern_t_850.grib").to_fieldlist().ls()
[3]:
| parameter.variable | time.valid_datetime | time.base_datetime | time.step | vertical.level | vertical.level_type | ensemble.member | geography.grid_type | |
|---|---|---|---|---|---|---|---|---|
| 0 | t | 2018-08-01 12:00:00 | 2018-08-01 12:00:00 | 0 days | 850 | pressure | 0 | regular_ll |
[4]:
# setting GRIB keys for the output
ds.to_target("file-pattern", out_pattern, metadata={"date": 20250108}, bitsPerValue=8)
# checking one output file
ekd.from_source("file", "_res_pattern_t_850.grib").to_fieldlist().ls(extra_keys=["metadata.bitsPerValue"])
[4]:
| parameter.variable | time.valid_datetime | time.base_datetime | time.step | vertical.level | vertical.level_type | ensemble.member | geography.grid_type | metadata.bitsPerValue | |
|---|---|---|---|---|---|---|---|---|---|
| 0 | t | 2025-01-08 12:00:00 | 2025-01-08 12:00:00 | 0 days | 850 | pressure | 0 | regular_ll | 8 |
Using a Target object¶
We can create a target object with get_target() and use write() to write/add data to it.
[5]:
# basic usage
target = ekd.create_target("file-pattern", out_pattern)
target.write(ds)
target.close()
# can be used as a context manager, no need to call close() in the end
with ekd.create_target("file-pattern", out_pattern) as target:
target.write(ds)
# a fieldlist can be written field by field into the target
with ekd.create_target("file-pattern", out_pattern) as target:
for f in ds:
target.write(f)
Using encoders¶
These calls are equivalent.
[6]:
# setting metadata and other GRIB keys for the output
ds.to_target("file-pattern", out_pattern, metadata={"date": 20250108}, bitsPerValue=8)
# explicitly specifying the encoder
ds.to_target("file-pattern", out_pattern, encoder="grib", metadata={"date": 20250108}, bitsPerValue=8)
# using an encoder object
encoder = ekd.create_encoder("grib", metadata={"date": 20250108})
ds.to_target("file-pattern", out_pattern, encoder=encoder, bitsPerValue=8)
The same can be done with a target object.
[7]:
encoder = ekd.create_encoder("grib", metadata={"date": 20250108})
with ekd.create_target("file-pattern", out_pattern) as target:
target.write(ds, encoder=encoder, bitsPerValue=8)