Skip to content

add CF's style _FillValue attribute at array level #171

Closed

Description

@vincentsarago

there is a bug in xarray (pydata/xarray#11345) which doesn't surface the fill_value property in the encoding metadata.

When we add _FillValue attribute, using DataArray.to_masked_array() will correctly mask the nodata/missing data specified by the fill_value property.

import zarr
import xarray
import numpy
from xarray.backends.zarr import FillValueCoder

store = zarr.storage.MemoryStore()
dataset = zarr.create_group(store=store)
array = dataset.create_array(
    "data",
    shape=(1800, 3600),
    chunks=(50, 50),
    dtype="float32",
    fill_value=-9999.0,
    dimension_names=("y", "x"),
    attributes={
        "_FillValue": FillValueCoder.encode(-9999.0, numpy.dtype("float32")),
    },
)
array[:] = numpy.arange(0.0, 3600 * 1800, dtype="float32").reshape(1800, 3600)
array[0:50, 0:50] = -9999.0  # Add some nodata
zarr.consolidate_metadata(dataset.store)

ds = xarray.open_dataset(
    store,
    decode_times=False,
    decode_coords=False,
    consolidated=True,
    use_zarr_fill_value_as_mask=True,
    engine="zarr",
)

ds["data"].to_masked_array()
>> masked_array(
  data=[[--, --, --, ..., 3597.0, 3598.0, 3599.0],
        [--, --, --, ..., 7197.0, 7198.0, 7199.0],
        [--, --, --, ..., 10797.0, 10798.0, 10799.0],
        ...,
        [6469200.0, 6469201.0, 6469202.0, ..., 6472797.0, 6472798.0,
         6472799.0],
        [6472800.0, 6472801.0, 6472802.0, ..., 6476397.0, 6476398.0,
         6476399.0],
        [6476400.0, 6476401.0, 6476402.0, ..., 6479997.0, 6479998.0,
         6479999.0]],
  mask=[[ True,  True,  True, ..., False, False, False],
        [ True,  True,  True, ..., False, False, False],
        [ True,  True,  True, ..., False, False, False],
        ...,
        [False, False, False, ..., False, False, False],
        [False, False, False, ..., False, False, False],
        [False, False, False, ..., False, False, False]],
  fill_value=np.float64(1e+20),
  dtype=float32)

Activity

  1. added 2 commits that reference this issue on Jun 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions