Foundations to Frontier
Tests
C. Programming, Tools and HPC › C7 Scientific Data Formats

Array Data: HDF5, NetCDF and Zarr

Simulations, climate models, satellite instruments and labs produce large multidimensional arrays, not tables. HDF5 stores them as typed datasets inside a hierarchy of groups with attributes; NetCDF-4 (built on HDF5) adds named dimensions, coordinate variables and the CF metadata conventions used across Earth science; Zarr stores the same model as one object per compressed chunk, which suits parallel writers and cloud object storage. This topic covers the shared data model, chunking and compression, CF conventions (units, packing with scale_factor/add_offset, fill values, time encoding), the Python tools (h5py, netCDF4, xarray, zarr, Dask) and how chunk shape decides read performance. In the exercises you build a Zarr-style chunked store with region reads and writes and CF decoding; then you measure five chunk shapes for a year of daily global temperature against three access patterns on local disk and object storage.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Cutting a big cube into bricks

A year of daily global temperature maps is a 3-D block of numbers: time × latitude × longitude. Storing it as one huge piece means you load everything even to look at one day; storing it as chunks (bricks) lets you fetch only the bricks you need. The catch: the best brick shape depends on how you will read the data, a whole map at one time or a long history at one place.

Chunk shape versus access pattern
query:
space (grid points) →time →
chunks fetched: 4 of 36 · values fetched 256 for 64 needed (4.0× read amplification) · est. time 50.3 ms
Blue is the data the query needs; orange chunks must be fetched and decompressed whole. Long thin chunks serve one access pattern perfectly and the other terribly; many tiny chunks multiply requests, which dominate on high-latency object storage. Balanced chunks of moderate size (roughly 1–10 MB in real stores) keep every query's worst case acceptable.
Why engineers care

Weather and climate model output, satellite imagery, CFD and structural simulation results, and spectrometer cubes are all stored this way. Choosing chunks well can turn a 50-second read into a 50-millisecond one.

📝 Quick check (Level 0)
0/3 answered
  1. 1. A chunk is…
  2. 2. Chunks of one full map per time step are good for…
  3. 3. Reading part of a chunk fetches…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

One data model, three containers

All three store arrays (typed, n-dimensional, chunked, compressed) organised in groups with attributes (key/value metadata).

HDF5 NetCDF-4 Zarr
Container one binary file one HDF5 file directory / object-store prefix of many objects
Adds groups, datasets, attributes named dimensions, coordinate variables, CF conventions JSON metadata, one object per chunk
Parallel writes MPI-IO (parallel HDF5) via HDF5 independent chunks, no locking
Cloud needs range requests same native
import xarray as xr
ds = xr.open_dataset("era5_2024.nc", chunks={"time": 30})           # lazy, Dask-backed
t2m = ds["t2m"].sel(time="2024-07", latitude=slice(60, 30)).mean("time")
ds.to_zarr("s3://bucket/era5_2024.zarr", encoding={"t2m": {"chunks": (30, 90, 90)}})

h5py gives NumPy-like access to HDF5 datasets (f["/sim/pressure"][100, :, :]); xarray adds labelled dimensions and CF decoding on top of NetCDF or Zarr.

📝 Quick check (Level 1)
0/3 answered
  1. 1. NetCDF-4 is built on…
  2. 2. Zarr stores each chunk as…
  3. 3. xarray's .sel(time='2024-07') selects by…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 35 minUndergraduate students and practising engineers.

Chunks and compression

An array of shape (365, 90, 180) with chunks (30, 45, 45) has ⌈365/30⌉ × 2 × 4 = 104 chunks. Element (100, 45, 90) lives in chunk (3, 1, 2) at offset (10, 0, 0). A read fetches and decompresses every chunk its selection overlaps; a partial write must read-modify-write the chunks it touches. Filters (shuffle, delta) and codecs (zlib, Zstd, Blosc, LZ4) apply per chunk. Chunks never written are not stored and read as the fill value, so sparse arrays are cheap.

CF conventions

  • units and standard_name on every variable; coordinate variables (time, lat, lon) with the same name as their dimension.
  • Packing: store int16 p with scale_factor s and add_offset o; value = p·s + o. Precision is s/2, and 16 bits span 65 535·s.
  • Missing data: _FillValue (or missing_value) decoded to NaN.
  • Time: numbers with units like days since 2000-01-01 and a calendar attribute (standard, noleap, 360_day in climate models).
🐍 Exercise 1: a Zarr-style chunked store with CF decoding
Store arrays as JSON metadata plus one zlib-compressed object per chunk in a dict; find chunks a selection touches; write regions by read-modify-write and read regions while counting chunks and bytes fetched; list arrays and attributes; implement CF packing/unpacking with a fill value and CF time decoding.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Chunks touched by rows 30–39, cols 0–9 with 32×32 chunks?
  2. 2. Packing precision with scale_factor 0.01?
  3. 3. An unwritten Zarr chunk reads as…

Level 3: Advanced

★★★★☆ Advanced⏱ 30 minGraduate level, with full derivations and proofs.

Choosing chunks

  • Match chunks to the dominant access pattern; for mixed patterns use balanced chunks over all dimensions.
  • Aim for chunks of roughly 1–100 MB uncompressed (smaller for local random access, larger for cloud stores where each request costs tens of milliseconds).
  • Avoid millions of tiny chunks: object-store request overhead and metadata dominate.
  • If two very different access patterns are both critical, store two copies with different chunking (rechunking with the rechunker library or xarray + Dask).
  • For Zarr v3, sharding packs many small chunks into one object, decoupling read granularity from object count.
  • For existing HDF5/NetCDF files in the cloud, Kerchunk/VirtualiZarr index the chunk byte ranges so they can be read like Zarr without converting.
📝 Quick check (Level 3)
0/3 answered
  1. 1. Why are tiny chunks bad on object storage?
  2. 2. Zarr v3 sharding…
  3. 3. Two critical but opposite access patterns: best option?

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

Chunking, measured

Exercise 1. A 100 × 120 float32 array with 32 × 32 chunks has 16 chunks; reading rows 30–39, columns 50–69 fetches 4. Before any write, reads return the fill value and the store holds no chunk objects. Overwriting a 3 × 3 patch at (31–33, 31–33) rewrites 4 chunks and leaves their other values intact. CF packing at 0.01 K stays within 0.005 K and maps NaN to the fill value; “days since 2000-01-01” decodes 59.5 to 2000-02-29 12:00.

Exercise 2: one year of daily global temperature (365 × 90 × 180, 2° grid, int16 at 0.01 K = 11.83 MB raw, zlib level 5 per chunk). Storage models: local SSD (0.1 ms per request, 2 GB/s) and object storage (50 ms per request, 16 in flight, 500 MB/s).

Chunking Chunks Stored Map of one day One point, all year 30 days, 10 × 10 box Worst case (object store)
map (1, 90, 180) 365 9.01 MB 1 chunk, 25 kB 365 chunks, 9.0 MB 30 chunks, 745 kB 1.17 s
time series (365, 1, 1) 16 200 10.27 MB 16 200 chunks, 10.3 MB 1 chunk, 0.6 kB 100 chunks, 61 kB 50.7 s
pencil (365, 15, 15) 72 8.76 MB 72 chunks, 8.8 MB 1 chunk, 120 kB 1 chunk, 120 kB 0.27 s
cube (30, 45, 45) 104 9.27 MB 8 chunks, 764 kB 13 chunks, 1.16 MB 1 chunk, 97 kB 52 ms
one chunk 1 9.08 MB 9.1 MB 9.1 MB 9.1 MB 68 ms

Each specialised layout is ideal for its own query and terrible for the other (over 10× more bytes, or 16 200 requests); tiny chunks also compress worst (10.27 MB). The balanced cube chunking has the best worst case on object storage. The single chunk looks competitive here only because the cube is small; at real sizes (ERA5 hourly is terabytes) reading everything for every query is impossible. On a local SSD the latency term is small and bytes dominate, so even the map chunking’s time-series read takes only 41 ms. (Compressed sizes can differ slightly between zlib builds.)

🐍 Exercise 2 (research): chunk shape versus access pattern
Generate a packed int16 temperature cube (365 × 90 × 180); for five chunk shapes compress every chunk to measure storage, then count the chunks and bytes three typical queries must fetch and estimate their time on local SSD and on object storage.

Array formats in AI, robotics and aerospace

  • Weather and climate: ERA5, CMIP6 and NOAA data in NetCDF and analysis-ready cloud Zarr; ML weather models (GraphCast and successors) train from Zarr archives.
  • Earth observation: satellite swaths in HDF5/NetCDF (NASA HDF-EOS, Sentinel products).
  • Simulation: CFD and structural codes write HDF5 (often through XDMF or CGNS conventions).
  • Lab and instrument data: NeXus/HDF5 at synchrotrons; ML training sets stored as HDF5 or Zarr arrays.

References

  1. The HDF Group. HDF5 User’s Guide (hdfgroup.org).
  2. R. Rew, G. Davis (1990). “NetCDF: an interface for scientific data access.” IEEE Computer Graphics and Applications 10(4).
  3. B. Eaton et al. NetCDF Climate and Forecast (CF) Metadata Conventions, version 1.11 (cfconventions.org).
  4. Zarr Developers. Zarr storage specification, versions 2 and 3 (zarr-specs.readthedocs.io).
  5. S. Hoyer, J. Hamman (2017). “xarray: N-D labeled arrays and datasets in Python.” Journal of Open Research Software 5(1).
  6. R. Abernathey et al. (2021). “Cloud-native repositories for big scientific data.” Computing in Science & Engineering 23(2).