Target objects¶
Each target is represented by a Target object. The target object can be created using create_target() and then used to write data. For the list of built in targets see Built in targets.
A Target object has the following methods/properties:
Method/property |
Description |
|---|---|
|
Write data to the target. Attempting to write to a closed target raises a ValueError. |
|
Close the target and release resources. Once closed, the target cannot be written to anymore. Attempting to call write, close or flush on a closed target raises a ValueError. |
|
Flush the target. This method is optional and may not be implemented by all targets. Attempting to flush to a closed target raises a ValueError. |
|
True if the target is closed, False otherwise. |
Usage¶
This example writes a FieldList to a target.
import earthkit.data as ekd
# read GRIB data into a fieldlist
ds = ekd.from_source("file", "docs/how-tos/test.grib").to_fieldlist()
# writing in a loop field by field
t = ekd.create_target("file", "_my_res.grib")
for f in ds:
t.write(f)
t.close()
# t cannot be written to anymore
# writing in one go
t = ekd.create_target("file", "_my_res_1.grib")
t.write(ds)
t.close()
# t cannot be written to anymore
Target objects can also be used a context manager. When used as a context manager, the target is automatically closed when the context is exited.
import earthkit.data as ekd
# read GRIB data into a fieldlist
ds = ekd.from_source("file", "docs/how-tos/test.grib").to_fieldlist()
with ekd.create_target("file", "_my_res.grib") as t:
for f in ds:
t.write(f)
create_target()¶
Create a new target Target object.
- create_target(name, *args, **kwargs)¶
Create a new target
Targetobject.- Parameters:
name (str) – the target (see Built in targets)
*args (tuple) –
specify target parameters
**kwargs (dict) –
specify additional target parameters. Also specify the encoder parameters.