Write outputs
to_file() saves a dataset. DEFAIR picks the writer from the file
extension, or you name one with writer=. Writing is the moment the
pipeline runs: the writer pulls values through every transformation you
chained and stores them chunk by chunk (a chunk is one block of a large
array, read and processed on its own).
Prerequisites
No. 1 A loaded dataset and a writable destination
Install DEFAIR as described in Install DEFAIR, and
have a DEFAIR Dataset available as dataset. For local output,
choose a writable path. For S3 output, prepare the destination endpoint,
bucket and credentials with write permission. Read access to an input
bucket does not give write permission.
The built-in writers
Writer |
Format |
Use it for |
|---|---|---|
zarrv2 |
Zarr version 2 |
Machine learning inputs and cloud storage, with the widest tool support |
zarrv3 |
Zarr version 3 |
The newer Zarr format |
netcdf4 |
NetCDF4 |
Self-describing files for science tools |
hdf5 |
HDF5 |
Self-describing files without the NetCDF layer |
parquet |
Parquet |
One row per observation, for sounder, scatterometer and wind products |
geoparquet |
GeoParquet |
Parquet with a point geometry column |
csv |
CSV |
Small tables |
geotiff |
GeoTIFF |
One georeferenced image per variable and time step |
png, jpeg |
PNG, JPEG |
Images for viewing |
from defair_data.writers import list_writers
print(list_writers())
# ['csv', 'geoparquet', 'geotiff', 'hdf5', 'jpeg', 'netcdf4', 'parquet', 'png', 'zarrv2', 'zarrv3']
Zarr
Zarr is a storage format that keeps a large array as many small files, so a program can read one piece without loading all of it.
# Write to Zarr v2 (widely compatible)
dataset.to_file(
"/path/to/output.zarr",
writer="zarrv2",
mode="w", # Overwrite if exists
consolidated=True, # Faster metadata reads
auto_chunk=True, # Automatic chunking
chunk_size_mb=10, # Target chunk size
)
# Write to Zarr v3 (newer format)
dataset.to_file("/path/to/output.zarr", writer="zarrv3")
mode is "w" to overwrite, "w-" to refuse an existing store,
or "a" to add to one. To append scans along the time axis, pass
append_dim="time" as well: mode="a" on its own rewrites the
stored variables in place. A streaming read (streaming=True) appends
each batch this way for you. An append whose coordinates do not match
the store fails with an error, so a mismatched scan is never stored in
the wrong place.
NetCDF4
dataset.to_file(
"/path/to/output.nc",
writer="netcdf4",
)
Write to cloud storage
Give an s3:// path (S3 is cloud object storage) and the connection details:
dataset.to_file(
"s3://bucket/path/to/output.zarr",
writer="zarrv2",
storage_options={
"key": "YOUR_KEY",
"secret": "YOUR_SECRET",
"endpoint_url": "https://s3.example.com",
},
)
What every writer adds
The writers add a latitude and longitude for every pixel to a store when
the reader did not attach them, so a written file is complete on its
own. They record the processing steps in the file’s history
attribute, following the CF conventions, the metadata rules most Earth
science tools expect.
The latitude and longitude step and the contracts of the image and column writers are in Writer output contracts. The history tracking is in Preserve metadata and configure encoding.
What to do next
See Write a writer plugin to write a writer for a format of your own.