Skip to content

Instantly share code, notes, and snippets.

@ashiklom
Created March 8, 2023 17:58
Show Gist options
  • Save ashiklom/09fcd78b3d4d7ebf933109f5c6810e19 to your computer and use it in GitHub Desktop.
Save ashiklom/09fcd78b3d4d7ebf933109f5c6810e19 to your computer and use it in GitHub Desktop.
Experimenting with Zarr chunking
import xarray as xr
import numpy as np
d1 = np.random.rand(133, 10, 10)
t1 = np.arange(1, 134)
xs = np.arange(1, 11)
ys = np.arange(1, 11)
chunking = {"time": 100, "x": 10, "y": 10}
dat1 = xr.Dataset(
{"data": (["time", "x", "y"], d1)},
coords = {"time": t1}
)#.chunk(chunking)
dat1.to_zarr("test_1.zarr", mode="w")
dat1.to_zarr("test_append.zarr", mode="w")
dat1z = xr.open_zarr("test_1.zarr")
d2 = np.random.rand(3, 10, 10)
t2 = np.arange(np.max(t1)+1, np.max(t1)+1+d2.shape[0])
dat2 = xr.Dataset(
{"data": (["time", "x", "y"], d2)},
coords = {"time": t2}
).chunk({"time": 3})
dat2.to_zarr("test_append.zarr", mode="a", append_dim="time")
d3 = np.random.rand(15, 10, 10)
t3 = np.arange(np.max(t2)+1, np.max(t2)+1+d3.shape[0])
dat3 = xr.Dataset(
{"data": (["time", "x", "y"], d3)},
coords = {"time": t3}
).chunk({"time": 15})
dat3.to_zarr("test_append.zarr", mode="a", append_dim="time")
dat_123 = xr.open_zarr("test_append.zarr")
dat_123.chunks
dat_combined = xr.concat((dat1z, dat2, dat3), dim="time")
dat_combined.to_zarr("test_combined.zarr", mode="w", safe_chunks=False)
dat_combinedz = xr.open_zarr("test_combined.zarr")
dat_combinedz.chunks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment