Skip to content

Inconsistent File Hashes When Resaving NetCDF Files with Chunks #10028

Closed
@NoraLoose

Description

@NoraLoose

What happened?

When resaving a NetCDF file using xarray, the resulting file has a consistent hash if opened without chunks. However, when the dataset is opened with chunks and resaved, the file hash changes with each save, even if the data remains unchanged. This behavior suggests non-deterministic output when working with chunked datasets.

What did you expect to happen?

I expect that resaving a dataset, whether opened with or without chunks, should produce deterministic file output if the data remains unchanged. This is particularly important for workflows that rely on file integrity checks.

Minimal Complete Verifiable Example

import hashlib
import xarray as xr
import numpy as np

def calculate_file_hash(filepath, hash_algorithm="sha256"):
    """Calculate the hash of a file using the specified hash algorithm."""
    hash_func = hashlib.new(hash_algorithm)
    with open(filepath, "rb") as f:
        for chunk in iter(lambda: f.read(4096), b""):
            hash_func.update(chunk)
    return hash_func.hexdigest()

# Create and save the dataset
ds = xr.Dataset(
    data_vars=dict(
        omega=(["nc"], [0.00014052, 0.00014544]),
        lon=(["ny", "nx"], np.array([
            [216.83334, 217.],
            [216.83334, 217.]
        ], dtype=np.float32))
    ),
    coords=dict(
        nc=[0, 1],
        ny=[0, 1],
        nx=[0, 1]
    )
)
fname = "test_data.nc"
ds.to_netcdf(fname)
print("Original file hash:", calculate_file_hash(fname))

# Resave without chunks
ds_without_chunks = xr.open_dataset(fname, chunks=None)
fname_resaved_without_chunks = "test_data_resaved_without_chunks.nc"
ds_without_chunks.to_netcdf(fname_resaved_without_chunks)
print("Resaved without chunks:", calculate_file_hash(fname_resaved_without_chunks))

# Resave with chunks (inconsistent hash)
ds_with_chunks = xr.open_dataset(fname, chunks={"nc": 1})
fname_resaved_with_chunks = "test_data_resaved_with_chunks.nc"
ds_with_chunks.to_netcdf(fname_resaved_with_chunks)
print("Resaved with chunks (first save):", calculate_file_hash(fname_resaved_with_chunks))

fname_resaved_once_more_with_chunks = "test_data_resaved_once_more_with_chunks.nc"
ds_with_chunks.to_netcdf(fname_resaved_once_more_with_chunks)
print("Resaved with chunks (second save):", calculate_file_hash(fname_resaved_once_more_with_chunks))

MVCE confirmation

  • Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
  • Complete example — the example is self-contained, including all data and the text of any traceback.
  • Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
  • New issue — a search of GitHub Issues suggests this is not a duplicate.
  • Recent environment — the issue occurs with the latest version of xarray and its dependencies.

Relevant log output

Original file hash:                   34a4af2dd6ec064dc1812ca0e1bbeb0feb3c698c78969c2bbeb25b0ec6fd7af0
Resaved without chunks:               34a4af2dd6ec064dc1812ca0e1bbeb0feb3c698c78969c2bbeb25b0ec6fd7af0
Resaved with chunks (first save):     64bdecb4517884dbce259e6207da93be2b9f9d1d40a7a9fa1c0ef8752ec7a8d0
Resaved with chunks (second save):    b90ef43fd7d15a476d0db24e334218f19c30a47ddb9b354538d1fb571c2322e5

Anything else we need to know?

No response

Environment

INSTALLED VERSIONS

commit: None
python: 3.12.8 | packaged by conda-forge | (main, Dec 5 2024, 14:24:40) [GCC 13.3.0]
python-bits: 64
OS: Linux
OS-release: 5.14.21-150400.24.46-default
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: en_US.UTF-8
LANG: en_US.UTF-8
LOCALE: ('en_US', 'UTF-8')
libhdf5: 1.14.2
libnetcdf: 4.9.4-development

xarray: 2024.10.0
pandas: 2.2.3
numpy: 1.26.4
scipy: 1.14.1
netCDF4: 1.7.2
pydap: None
h5netcdf: None
h5py: None
zarr: 2.18.4
cftime: 1.6.4.post1
nc_time_axis: None
iris: None
bottleneck: 1.4.2
dask: 2025.1.0
distributed: 2025.1.0
matplotlib: 3.9.2
cartopy: 0.24.1
seaborn: None
numbagg: None
fsspec: 2024.12.0
cupy: None
pint: None
sparse: 0.15.4
flox: None
numpy_groupies: None
setuptools: 69.5.1
pip: 24.0
conda: None
pytest: 8.3.4
mypy: None
IPython: 8.29.0
sphinx: None

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