Add attributes to an existing Zarr storage

Viewed 22

I have a zarr store that I open using xarray and zarr:

report = xr.open_zarr(grid_file_name)

where grid_file_name is pointing to a local zarr directory.

I need to add some attribute to the store, and I can add them to the xarray object by:

report = report.assign_attrs({
    "conversion_software_version": commit_sha,
    "source_filenames_labels": pred_file_name})

where commit_sha and pred_file_name contain the information I would like to add as attributes.

Now the xarray object contains the attributes, but how can I update the zarr storage with these attributes. Is adding attributes later in violation of best practice?

I know I can write the attributes when the file is generated, but that is not my question in this post.

1 Answers

Xarray does not support incremental metadata writes to Zarr stores but Zarr itself does:

group = zarr.open_group(grid_file_name)

group.attrs.update({
    "conversion_software_version": commit_sha,
    "source_filenames_labels": pred_file_name})
Related