NEP 2.7.0
NetCDF Extension Pack
Loading...
Searching...
No Matches
Macros | Functions
nczarr_simple.c File Reference

Demonstrate creating, writing, and reading a local NcZarr dataset. More...

#include <netcdf.h>
#include <stdio.h>
#include <stdlib.h>

Macros

#define FILE_URL   "file://simple_nczarr.zarr#mode=nczarr"
 
#define NY   4
 
#define NX   5
 
#define NDIMS   2
 
#define ERR(e)
 

Functions

int main (void)
 

Detailed Description

Demonstrate creating, writing, and reading a local NcZarr dataset.

This example introduces the NetCDF-Zarr (NcZarr) storage format, which extends the netCDF-4 enhanced data model to Zarr, a cloud-native array storage system. Unlike classic NetCDF or NetCDF-4/HDF5, which store a dataset in a single binary file, NcZarr stores each variable's metadata and chunks as separate objects in a directory tree (for local filesystem storage) or in an object store (for cloud storage). This design is optimized for random access over HTTP and scales naturally to cloud object storage such as Amazon S3, Google Cloud Storage, and Azure Blob Storage.

NcZarr is accessed through the familiar netCDF-C API. The only difference at create/open time is the storage URL: a local NcZarr store is selected with the file:// scheme and the #mode=nczarr fragment, which tells the netCDF-C dispatch layer to use the NcZarr implementation. Once the file is open, nc_def_dim(), nc_def_var(), nc_put_att_xxx(), nc_put_var_xxx(), and nc_get_var_xxx() work exactly as they do for NetCDF-4/HDF5 files.

This program creates a small 2D temperature array on a 4x5 y-x grid, stores it as a local NcZarr directory named simple_nczarr.zarr, attaches units, long_name, and _FillValue attributes, closes the file, reopens it read-only, and verifies that the metadata and data values match what was written.

Learning Objectives:

Key Concepts:

Comparison with Other NetCDF Formats:

Feature Classic NetCDF-4/HDF5 NcZarr
Single file Yes Yes No (directory tree)
Cloud object storage No No Yes
Compression No Yes (HDF5 filters) Yes (Zarr codecs)
Groups/user types No Yes Yes
HTTP random access No No Yes

Prerequisites:

Related Examples:

Compilation:

gcc -o nczarr_simple nczarr_simple.c -lnetcdf

Usage:

./nczarr_simple
ncdump 'file://simple_nczarr.zarr#mode=nczarr'

Expected Output: Creates the directory simple_nczarr.zarr containing:

Note
Companion code for "The NetCDF Developer's Handbook: The Authoritative Guide to Writing High-Performance Programs for Scientific Data Management, Second Edition" (https://www.amazon.com/dp/B0H7Q1Z75L)
Author
Edward Hartnett, Intelligent Data Design, Inc.
Date
2026-06-26

Macro Definition Documentation

◆ ERR

#define ERR (   e)
Value:
do { \
if (e) { \
fprintf(stderr, "Error: %s at line %d\n", nc_strerror(e), __LINE__); \
return 1; \
} \
} while(0)

◆ FILE_URL

#define FILE_URL   "file://simple_nczarr.zarr#mode=nczarr"

◆ NDIMS

#define NDIMS   2

◆ NX

#define NX   5

◆ NY

#define NY   4

Function Documentation

◆ main()

int main ( void  )