0

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.

Shofus
  • 1
  • 1

1 Answers1

0

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})
jhamman
  • 5,867
  • 19
  • 39
  • 1
    Don't forget to consolidate the zarr metadata with `zarr.consolidate_metadata(grid_file_name)` in order for xarray to pick up these changes when you open again at another time! – leonard Aug 23 '23 at 17:25