NEP 4.3.0
NetCDF Expansion Pack
Loading...
Searching...
No Matches
nxCDF Backend

nxCDF is a clean-room rewrite of the NetCDF-4/HDF5 backend delivered as a NEP User Defined Format (UDF) expansion pack. A UDF is a plug-in file format registered with netcdf-c's dispatch mechanism; released NetCDF-C 4.10.1 provides 10 UDF slots and the pinned development revision provides 64, and nxCDF occupies slot 9 (NC_UDF9) in both schemes. The backend provides the full NetCDF-4 enhanced data model, variable I/O, storage features, and a set of new atomic types while coexisting with netcdf-c's built-in HDF5 backend. See nxCDF Architecture for how the dispatch table, per-object state, and hidden attributes fit together.

Explicit selection required: because HDF5 files already belong to netcdf-c's built-in NetCDF-4 backend, nxCDF does not register the HDF5 magic number. Open and create nxCDF files by passing NC_NXCDF.

Feature coverage: Not all NetCDF-4 features are implemented; see the Known Limitations section below.

Fortran binding

With nxCDF and Fortran enabled, use nep_nxcdf provides initialization, mode/type constants and nf90_{get,put}_var_{float16,bfloat16} bit-buffer generics (scalar–rank 7, optional start/count/stride). NetCDF-Fortran >=4.6.2 and compatible compiler modules are required. See nxCDF Fortran for one-based selection conventions, installed NEP::nepnxcdff linkage and a complete example. Reference wrappers and broader enhanced-type value interfaces are deferred beyond v4.3.0; exported constants alone do not provide those interfaces.

Internal conversion engine

nxCDF converts all supported numeric variable and attribute data with the private, NEP-owned nxcdf_convert_type() engine, selected as the permanent engine after Sprint 1H's OFF/ON comparison. It replaced the temporary NEP_INTERNAL_CONVERT CMake option; passing that option now fails at configure time. There is no runtime or file-format switch.

Standard float/double calls on bfloat16 use round-to-nearest-even, with single rounding from double and signed infinity on finite overflow. For example, 449 narrows to 448 instead of HDF5's 450. IEEE float16 conversion joined the engine in Sprint 1J (v4.3.0), with the same round-to-nearest-even and NC_ERANGE semantics; the former HDF5 H5Tconvert path was removed. HDF5 also handles file byte order and storage representation.

Matching-type raw-bit I/O, FP8/FP6/FP4 typed-call restrictions and the independent nc_*_bfloat16/nc_*_float16 wrappers are unchanged. Float/double conversion for bfloat16 attributes is supported; fill-value limitations still apply. Variable/attribute conversions and quantized writes complete all values, then return NC_ERANGE on finite overflow, integer range violations or nonfinite-to-integer conversion (#381). A range warning does not roll back writes: every value is processed, attributes may be replaced and unlimited dimensions may grow. Ordinary precision loss, fractional truncation and floating underflow do not cause range errors; floating NaN/infinity inputs remain valid for floating destinations. Structural, permission and HDF5 failures take precedence over range warnings. Historical HDF5-engine benchmark artifacts are kept for comparison in examples/nxcdf/conversion_*.csv; new measurements use the internal engine.

Creating and Opening Files

#include <netcdf.h>
int ncid;
/* Create a native nxCDF file */
nc_create("example.nc", NC_NXCDF | NC_CLOBBER, &ncid);
/* Open an existing nxCDF file */
nc_open("example.nc", NC_NXCDF | NC_NOWRITE, &ncid);
#define NC_NXCDF
Definition nxcdf.h:35

nxCDF also supports .ncrc autoload via UDF slot 9 (NETCDF.UDF9.LIBRARY and NETCDF.UDF9.INIT), but files must still be selected with NC_NXCDF; autoload only avoids calling NC_NXCDF_initialize() explicitly.

Enabling

CMake:

cmake -B build -DNEP_ENABLE_NXCDF=ON

Dependencies:

  • Unmodified NetCDF-C >=4.10.1 with NC_Dispatch and NC_UDF9 support. Released 4.10.1's 10-slot scheme and development revision 5baa62e6966d3e505b895574dafbd531a4aed360's 64-slot scheme are supported. CMake detects the scheme automatically; no dependency patch or feature-reduction option is required. This supersedes Sprint 1M's 64-slot-only policy.
  • HDF5 2.1.1 or newer (required).

NC_NXCDF and NC_FORMATX_NXCDF come from the selected C headers. The Fortran nf90_nxcdf parameter is selected and checked against those headers at build time. Rebuild NEP, its Fortran module and applications when switching UDF encodings; cross-scheme binary compatibility is not supported. File-format and caller-owned allocation contracts are unchanged: reclaim dynamic results before closing the file. See Architecture for the legacy private metadata-model adaptation.

nxCDF availability is advertised at compile time through nep_meta.h:

#include <nep_meta.h>
#if NEP_HAS_NXCDF
/* nxCDF is compiled in. */
#endif

Compatibility Modes

Mode Base flag Compatibility flag Superblock Purpose
Native NC_NXCDF — v3 Full enhanced model and all new types
Classic model NC_NXCDF NC_CLASSIC_MODEL v3 NetCDF-3 data model on HDF5
NetCDF-4 model NC_NXCDF NC_NETCDF4_MODEL v1 Files readable by upstream netcdf-c

Native mode allows all nxCDF-specific types and HDF5 2.1.1 features.

NC_CLASSIC_MODEL restricts the file to the classic NetCDF-3 data model: root group only, one unlimited dimension, classic atomic types, no chunking, compression, filters, or user-defined types.

NC_NETCDF4_MODEL produces files that upstream netcdf-c linked against HDF5 1.10+ can open. It allows the standard enhanced NetCDF-4 data model (groups, user-defined types, chunking, compression) but forbids nxCDF-specific atomic types (NC_FLOAT16, small floats, complex, bitfield, reference) so the file remains fully compatible with upstream.

NC_CLASSIC_MODEL and NC_NETCDF4_MODEL are mutually exclusive.

Fill defaults, cache control and group renaming

nc_set_fill(ncid, NC_FILL/NC_NOFILL, &old_mode) changes a session default shared by all groups. Each session begins with NC_FILL; eligible variables capture the default when defined, not at nc_enddef. Existing variables are unchanged. Standard numeric/character and NEP fixed-size numeric/complex/bitfield variables inherit it; strings, user-defined types and references retain their existing fill rules. nc_def_var_fill remains the per-variable override before commitment. Read-only calls return NC_EPERM, and invalid modes return NC_EINVAL. Unwritten no-fill values are unspecified. Changing the file default neither rebuilds datasets nor persists a new file-wide attribute.

nc_set_var_chunk_cache(ncid, varid, bytes, slots, preemption) applies real HDF5 raw chunk-cache settings. Preemption must be finite and in [0,1]; zero bytes/slots are allowed. Calls work in define/data mode, read-only sessions and all compatibility modes. Pending variables retain requests until creation; live datasets reopen with the new access properties, closing/reopening coordinate-scale aliases together. Values, filters, attributes and dimension attachments are preserved. nc_get_var_chunk_cache reports effective live chunked-dataset properties (stored requests for pending/nonchunked variables) and accepts NULL outputs. Scalar/contiguous variables accept settings but have no raw chunk cache to tune. Overrides last only for the open session; definitions and opens start from nc_set_chunk_cache global defaults.

nc_rename_grp(grpid, name) works in native and NC_NETCDF4_MODEL modes, as does group creation. Rename moves the parent link and flushes it, preserving group/descendant IDs, data, types and dimension relationships; inquiries immediately use the new path. Enhanced-mode rename may automatically enter define mode; call nc_enddef before subsequent reads. Root rename returns NC_EBADGRPID, read-only rename NC_EPERM, and duplicate names (including the existing name) NC_ENAMEINUSE. Classic-model groups remain unsupported (NC_ENOTNC4). Object-reference identity is preserved; previously saved path strings are not rewritten.

Unsupported file modes and parallel I/O

nxCDF supports ordinary serial disk files and optional MPI-IO sessions in v4.3.0. Its create/open entries reject NC_DISKLESS, NC_INMEMORY, NC_MPIIO/NC_MPIPOSIX (aliases), NC_SHARE, NC_PERSIST and NC_MMAP with NC_EINVAL, before creating, clobbering or opening a file. NetCDF-C may reject incompatible flag combinations before backend dispatch. Use NetCDF-C's memory APIs when testing memory requests: passing NC_INMEMORY directly to ordinary nc_open without an image parameter can assert inside upstream model inference before nxCDF is reached.

Parallel I/O

nxCDF supports MPI-IO file access through the upstream parallel entry points when built with -DNEP_ENABLE_NXCDF_PARALLEL=ON (default OFF). The option requires an MPI C compiler, an MPI-enabled HDF5 (HDF5_IS_PARALLEL), and a NetCDF-C built with parallel support (NC_HAS_PARALLEL4); a missing piece is a configure-time FATAL_ERROR. Availability is reported as NEP_HAS_NXCDF_PARALLEL in nep_meta.h. The parallel ctest label is nxcdf_parallel, gated on NEP_ENABLE_PARALLEL_TESTS.

/* Create is collective over comm; upstream nc_create_par requires
* NC_NETCDF4 in the mode flags, so OR it with NC_NXCDF. The UDF
* flag still selects the nxCDF dispatcher. */
nc_create_par(path, NC_NXCDF | NC_NETCDF4, comm, info, &ncid);
/* Open needs only NC_NXCDF (plus optional NC_WRITE). */
nc_open_par(path, NC_NXCDF, comm, info, &ncid);
nc_open_par(path, NC_NXCDF | NC_WRITE, comm, info, &ncid);

The communicator and info handle arrive in the dispatch parameters pointer, are duplicated into file state (freed at nc_close), and configure the MPI-IO virtual file driver (H5Pset_fapl_mpio) plus collective metadata operations (H5Pset_all_coll_metadata_ops, H5Pset_coll_metadata_write) on the file access property list — the same semantics as upstream's built-in NetCDF-4/HDF5 backend. Consequently all ranks in the communicator must make identical define-mode calls (nc_def_dim, nc_def_var, nc_put_att, nc_enddef, nc_redef, nc_sync, nc_close); metadata operations are collective.

nc_var_par_access(ncid, varid, NC_COLLECTIVE) selects collective transfers for one variable; NC_INDEPENDENT restores independent mode. Access preferences are session-only, start independent on definition/open and survive dataset rebuilds/cache-handle reopens. The selector works in define/data mode and read-only parallel sessions, returns NC_ENOPAR on serial sessions/builds, NC_EINVAL for an invalid mode or independent filtered access, and the normal identifier errors for invalid IDs. All ranks must select the same mode and follow matching collective call sequences; call nc_enddef collectively before independent data access.

Collective data calls require every rank in the file communicator, including ranks with zero counts or reads satisfied entirely by fill. Zero-count buffers may be NULL; nonempty selections and scalars require a buffer. Independent in-bounds transfers need not be called by every rank. Overlapping writes require application coordination; examples/tests use disjoint selections. Collective unlimited-dimension writes negotiate the maximum extent across ranks and update dimension scales without shrinking shared logical lengths. Independent writes that need physical dataset growth return NC_ECANTEXTEND before mutation. Unwritten fill regions retain the variable's fill value; no-fill values remain unspecified.

Adding any filter in a parallel session forces that variable to collective access. Per the HDF5 parallel filter rule, independent filtered writes return NC_EINVAL, as does selecting independent access on a filtered variable; independent filtered reads are permitted, matching upstream. Reopening resets preferences to independent, so explicitly select collective mode before writing a filtered variable. Built-in deflate, shuffle and fletcher32 are covered in four-rank CI by the ported upstream nc_test4 parallel tests (tst_parallel_zlib, tst_parallel_compress, tst_quantize_par), and the external zstd plugin — plus bzip2 when its plugin is installed — is exercised in parallel through HDF5_PLUGIN_PATH by tst_nxcdf_parallel_plugin. No new plugin installation/path is required.

Parallel variable transfers support fixed-size standard types, enum/opaque/recursively fixed-size compounds, complex/bitfields and small floats. Float16/bfloat16 retain NEP typed float/double conversion; FP8/FP6/FP4 remain raw-bit only. NC_STRING, recursively dynamic user types and NC_STD_REF variable data I/O return NC_ENOTBUILT before automatic enddef, conversion or extent mutation; serial support is unchanged. Parallel reference-helper support is not certified. Conversion range warnings (NC_ERANGE) are rank-local, returned after completed I/O; they do not skip collective participation. Invalid rank-local arguments/allocation failures are not made recoverable through a general error-consensus protocol: applications must provide valid arguments and matching collective order.

NEP requests a collective HDF5 DXPL, but HDF5 may internally choose independent I/O; the API does not promise a particular physical MPI algorithm. The tested local/CI stack uses Open MPI, parallel HDF5 2.1.1 and parallel NetCDF-C with 64-slot UDF support; .github/workflows/ci-parallel-mpich.yml additionally runs the nxcdf_parallel label on a separate cached MPICH + parallel HDF5 2.1.1 + NetCDF-C main stack, so both MPI implementations are covered. MPICH is an alternative MPI implementation, not an additional dependency. The focused four-rank CTest programs have 180-second timeouts; run ctest --test-dir build -L nxcdf_parallel --output-on-failure with NEP_ENABLE_PARALLEL_TESTS=ON.

Upstream nc_test4 parallel tests are ported near-verbatim in test/nxcdf/nc_test4/ (tst_parallel, tst_parallel3-6, tst_parallel_zlib, tst_parallel_compress, tst_quantize_par); every port runs in both native NC_NXCDF and NC_NXCDF | NC_NETCDF4_MODEL mode. tst_nxcdf_interchange verifies interchange in both directions: nxCDF opens unmarked upstream NetCDF-4 files, and upstream NetCDF-C reads nxCDF NC_NETCDF4_MODEL files. A disk-bound four-rank collective write+read benchmark lives at examples/nxcdf/parallel_perf.c (under NEP_ENABLE_BENCHMARKS), with committed reference results in reference_parallel_*.csv/reference_parallel_machine.txt. SWMR is a separate serial writer/reader interface described below; it cannot be combined with MPI-IO.

When NEP is built without parallel support, nc_create_par/nc_open_par on a nxCDF file return NC_ENOPAR from the backend. Deprecated NC_MPIIO/NC_MPIPOSIX mode flags remain rejected with NC_EINVAL; NC_PAR is not the interface. In-memory image files remain unsupported. Native serial SWMR writer and reader sessions are supported as described below.

SWMR writer and reader sessions

Native nxCDF supports a single serial writer publishing data to independent HDF5 SWMR readers. This is distinct from MPI-IO: a parallel-built HDF5 library can serve a serial writer, but an actual MPI-IO session cannot use SWMR. No new CMake option or dependency is needed beyond NEP_ENABLE_NXCDF. Include nep.h and link libncnxcdf for these interfaces:

  • NC_SWMR_WRITE (0x08000000) requests writer activation at create/open. Creates stay in define mode until nc_enddef or automatic enddef on the first write. Writable opens activate after loading and validating the existing schema.
  • nc_start_swmr_write(int ncid) explicitly starts an ordinary writable session, committing pending definitions first; repeated calls on an active writer succeed.
  • nc_swmr_flush(int ncid) flushes all variable datasets, dimension scales and the file, including subgroups. Active-writer nc_sync uses the same publication path. File or subgroup IDs address the whole file. Closing an inactive requested file finalizes ordinary metadata without activating SWMR.

The following complete program checks every call. It creates an unlimited coordinate and data variable, activates the writer, appends and publishes a record.

#include <stdio.h>
#include <netcdf.h>
#include <nep.h>
#include <nxcdf/nxcdf.h>
#define CHECK(call) do { int err = (call); if (err) { \
fprintf(stderr, "%s\n", nc_strerror(err)); return 1; } } while (0)
int main(void)
{
int ncid, dimid, timeid, varid;
size_t start = 0, count = 1;
int time = 0;
float value = 273.15f;
if (!NC_NXCDF_initialize()) return 1;
CHECK(nc_create("stream.nc", NC_NXCDF | NC_SWMR_WRITE, &ncid));
CHECK(nc_def_dim(ncid, "time", NC_UNLIMITED, &dimid));
CHECK(nc_def_var(ncid, "time", NC_INT, 1, &dimid, &timeid));
CHECK(nc_def_var(ncid, "temperature", NC_FLOAT, 1, &dimid, &varid));
CHECK(nc_enddef(ncid));
CHECK(nc_put_vara_int(ncid, timeid, &start, &count, &time));
CHECK(nc_put_vara_float(ncid, varid, &start, &count, &value));
CHECK(nc_close(ncid));
return 0;
}
#define CHECK(call)
Definition conversion_perf.c:37
int main(void)
Definition external_refs.c:108
NetCDF Expansion Pack (NEP) - Public API.
Public API for the nxCDF UDF backend.
#define NC_SWMR_WRITE
Definition nxcdf.h:53
NC_Dispatch * NC_NXCDF_initialize(void)
Register the nxCDF dispatch table with netcdf-c.
Definition nxcdf_dispatch.c:140
int nc_swmr_flush(int ncid)
Publish a writer session as specified by nc_swmr_flush() in nep.h. See nep.h for parameter,...
Definition nxcdf_file.c:223

To start on an existing native file, instead use nc_open(path, NC_NXCDF | NC_WRITE, &ncid) followed by nc_start_swmr_write(ncid), or open with NC_NXCDF | NC_WRITE | NC_SWMR_WRITE. Opening without the flag later restores an ordinary session; SWMR activation is not a persistent NEP mode marker.

Only native files with actual superblock v3 and a SWMR-capable VFD are accepted; old files are not upgraded. NC_CLASSIC_MODEL, NC_NETCDF4_MODEL (including modes recovered from file markers), MPI-IO sessions and read-only flagged opens return NC_EINVAL. Explicit start on a read-only file returns NC_EPERM. Variable types must be fixed-size atomic: standard numeric/character types, small floats, complex and bitfields. String/reference/user-defined variables and any declared user type, even unused or attribute-only, return NC_ENOTBUILT. Existing atomic attributes may remain but are frozen. Failed start leaves an inactive, closable session; definitions already committed by enddef are not rolled back.

Once active, schema and attributes cannot change: define/rename/group/type/attribute operations, nc_redef, reference creation and file-default fill changes return NC_EPERM. Dataset-property setters return NC_ELATEDEF. Reads, in-bounds writes, unlimited growth, existing conversion/range behavior and session chunk-cache tuning remain available. Define filters before activation: unfiltered, deflate, shuffle+deflate and fletcher32 datasets have live-reader coverage. This does not certify arbitrary external plugins. Missing-plugin behavior is unchanged.

Publication means reader visibility, not power-loss durability or an atomic multi-variable timestep. HDF5 may flush before an explicit flush; lengths can grow before all values of a logical record are written. Use an application completion protocol for records spanning variables. A partial flush failure returns NC_EHDFERR without rollback. The filesystem must provide the consistency semantics required by HDF5 SWMR; do not disable file locking to bypass errors.

NEP readers open with NC_SWMR_READ (0x10000000), a read-only flag combined with NC_NXCDF at nc_open. The file opens through HDF5's H5F_ACC_RDONLY | H5F_ACC_SWMR_READ access (there is no H5Fstart_swmr_read function); combining it with NC_WRITE, NC_SWMR_WRITE, NC_CLASSIC_MODEL, deprecated mode flags or MPI parameters returns NC_EINVAL, and passing it to nc_create is invalid. Any file HDF5 accepts for SWMR read opens: a live writer's file, a quiescent file after the writer closed, or a file that was never SWMR-written — including NC_NETCDF4_MODEL files whose actual superblock is v3. Pre-v3 superblock files return NC_EINVAL and are never upgraded.

nc_swmr_refresh(int ncid) refreshes every variable dataset and every dimension-scale dataset through H5Drefresh, then reloads each dimension's cached length from its scale dataset's current extent, so records the writer appended and published with nc_swmr_flush()/nc_sync() become visible through ordinary nc_inq_dimlen() and nc_get_vara() calls. Calling it on an ordinary read-only, writable or writer session returns NC_EINVAL; nc_swmr_flush on a reader session returns NC_EPERM. The effective mode reported by nc_inq_format_extended() carries NC_SWMR_READ.

Refresh is not a re-open: because the writer froze its schema and attributes at activation, new variables, dimensions, attributes or types are never expected and are never discovered mid-session — reopen to see such changes. Reads mid-append are well defined at the dataset level, but a logical record spanning several variables still needs an application completion protocol. One implementation detail: HDF5 currently requires a single open handle per object for correct SWMR reads, so the backend collapses each coordinate variable's scale/variable handle pair when the reader session opens.

/* Reader: watch the records published by the writer above. */
int rncid, rdim, rvar;
if (!NC_NXCDF_initialize()) return 1;
CHECK(nc_open("stream.nc", NC_NXCDF | NC_SWMR_READ, &rncid));
CHECK(nc_inq_dimid(rncid, "time", &rdim));
CHECK(nc_inq_varid(rncid, "temperature", &rvar));
for (;;) {
size_t len = 0, start = 0;
float values[16] = {0};
/* Wait for the application-level publish signal, then refresh. */
CHECK(nc_inq_dimlen(rncid, rdim, &len));
CHECK(nc_get_vara_float(rncid, rvar, &start, &len, values));
/* len records are now readable; exit on the app's own protocol. */
}
CHECK(nc_close(rncid));
#define NC_SWMR_READ
Definition nxcdf.h:69
int nc_swmr_refresh(int ncid)
Refresh a reader session as specified by nc_swmr_refresh() in nep.h. See nep.h for parameter,...
Definition nxcdf_file.c:380

tst_nxcdf_swmr launches a separately executed raw-HDF5 reader and a NEP reader (tst_nxcdf_swmr_nep_reader) with bounded pipe handshakes, including two simultaneous readers and both writer/reader close orders; it runs under the nxcdf_swmr label in existing normal, sanitizer and coverage full-suite CI.

Diagnostics and Logging

nxCDF uses the distinct ELOG(()) macro, patterned after netcdf-c's LOG(()) convention in libsrc4 and libhdf5. NEP logging is independent of the underlying NetCDF-C logging build. It is compiled in by default and can be disabled with -DNEP_ENABLE_LOGGING=OFF. Build-time availability is reported in nep_meta.h:

#include <nep_meta.h>
#if NEP_HAS_LOGGING
/* nxCDF is instrumented for diagnostics. */
#endif

The default runtime verbosity is -1, which suppresses all NEP logging even when logging support is compiled in. Set NEP_LOG_LEVEL=-1 to explicitly select this silent mode; NEP_LOG_LEVEL=0 enables errors only.

To control diagnostics:

  • Set the NEP_LOG_LEVEL environment variable before opening a file. The nxCDF initialization reads this variable and applies it with nep_set_log_level().
  • Call nep_set_log_level(level) directly, where level is 0 (errors only), 1 (major messages), 2 (lifecycle events), 3 (metadata operations), or higher for more detail. nep_set_log_level(-1) disables all output.

Messages are written to stderr. Instrumented nxCDF paths include file creation and open, close/abort, define/end-define mode transitions, dimension and variable definition, and the metadata loader. HDF5 failures additionally dump the HDF5 error stack.

Supported Atomic Types

NetCDF Type HDF5 Type Compatibility Mode
NC_BYTE H5T_STD_I8LE/BE All
NC_UBYTE H5T_STD_U8LE/BE All
NC_CHAR H5T_C_S1 All
NC_SHORT H5T_STD_I16LE/BE All
NC_USHORT H5T_STD_U16LE/BE All
NC_INT H5T_STD_I32LE/BE All
NC_UINT H5T_STD_U32LE/BE All
NC_INT64 H5T_STD_I64LE/BE All
NC_UINT64 H5T_STD_U64LE/BE All
NC_FLOAT H5T_IEEE_F32LE/BE All
NC_DOUBLE H5T_IEEE_F64LE/BE All
NC_FLOAT16 H5T_IEEE_F16LE/BE Native only
NC_BFLOAT16 H5T_FLOAT_BFLOAT16LE/BE Native only
NC_FLOAT8_E4M3 H5T_FLOAT_F8E4M3 Native only
NC_FLOAT8_E5M2 H5T_FLOAT_F8E5M2 Native only
NC_FLOAT6_E2M3 H5T_FLOAT_F6E2M3 Native only
NC_FLOAT6_E3M2 H5T_FLOAT_F6E3M2 Native only
NC_FLOAT4_E2M1 H5T_FLOAT_F4E2M1 Native only
NC_COMPLEX H5T_COMPLEX over H5T_NATIVE_FLOAT Native only
NC_DOUBLECOMPLEX H5T_COMPLEX over H5T_NATIVE_DOUBLE Native only
NC_BITFIELD8/16/32/64 H5T_STD_B*LE/BE Native only
NC_STD_REF H5T_STD_REF Native only

Small floating-point and bitfield types are exchanged as raw bit patterns by the untyped calls; NC_FLOAT16 and NC_BFLOAT16 also accept typed float/double calls (see NXCDF_float16.md). Complex numbers use the memory layout { float r; float i; } or { double r; double i; } but are stored as native HDF5 2.x H5T_COMPLEX datatypes, not as ordinary compounds — a user compound with {r,i} fields remains an ordinary user compound. NC_STD_REF variables store 64-byte H5R_ref_t reference tokens; the nc_ref_*/nc_deref_* functions create and resolve them.

User-Defined Types

nxCDF supports the enhanced-model user types — compounds (with atomic, enum, opaque, array, nested-compound and vlen fields), integer enums, fixed-size opaque types and variable-length types — as variables and attributes in both native and NC_NETCDF4_MODEL files, fully interchangeable with upstream NetCDF-4:

struct sample { int id; float temp; unsigned char flag; };
nc_type cmpid, enumid, opid;
nc_def_compound(ncid, sizeof(struct sample), "sample", &cmpid);
nc_insert_compound(ncid, cmpid, "id", offsetof(struct sample, id), NC_INT);
nc_insert_compound(ncid, cmpid, "temp", offsetof(struct sample, temp), NC_FLOAT);
nc_insert_compound(ncid, cmpid, "flag", offsetof(struct sample, flag), NC_UBYTE);
nc_def_enum(ncid, NC_INT, "cloud_class", &enumid);
nc_insert_enum(ncid, enumid, "clear", &((int){0}));
nc_insert_enum(ncid, enumid, "cumulus",&((int){1}));
nc_def_opaque(ncid, 8, "blob", &opid);
nc_def_var(ncid, "samples", cmpid, 1, &dimid, &varid);
nc_enddef(ncid); /* types commit here */
nc_put_var(ncid, varid, data); /* untyped access copies raw elements */

Semantics:

  • Types are group-scoped and visible to descendant groups; committed datatypes are written at _enddef and member inserts are rejected afterwards (NC_ENOTINDEFINE).
  • I/O is matching-type or untyped (nc_put/get_var, var1, vara, vars); typed calls that request numeric conversion of a user type return NC_EBADTYPE. Memory and file layouts are the type's exact sizes and field offsets — no conversion engine is involved.
  • Attributes of user types work the same way (nc_put_att/nc_get_att with the user typeid or untyped), including zero-length attributes, and preserve their type across close/reopen.
  • Classic-model files reject all user-type calls (NC_ENOTNC4).
  • Compounds nest: fields may be other fixed-size compounds, enums, opaques, or fixed-size multidimensional arrays (nc_insert_array_compound) of any of those. Nested types are committed before their containers and each group tracks link creation order, so upstream netCDF reads the types back in dependency order.
  • For compounds containing nested compound members, nxCDF writes a lossless declared-layout record per element (upstream's own convention under HDF5 2.1.1 overflows dataset storage and loses the last record's tail). Foreign files written with upstream's serialized-compound layout are read through the packed native datatype, matching upstream's own read capability.
  • Variable-length (nc_def_vlen) types work end to end: vlens of atomic and user-defined bases, nested vlens, and compounds (and array fields) containing vlens, for variables and attributes in native and NC_NETCDF4_MODEL modes. Vlen elements are nc_vlen_t {size_t len; void *p} descriptors. nc_put_vlen_element borrows the caller's payload pointer (the data must stay valid until nc_put_var* completes); nc_get_vlen_element copies len * base_size bytes into caller storage. Reads return deep copies owned by the caller — free them with nc_reclaim_data (recursive, frees nested payloads only) or nc_reclaim_data_all (also frees the top-level buffer), and always reclaim before nc_close while file/type context exists. The legacy nc_free_vlen/nc_free_vlens/nc_free_string helpers are flat-only: use nc_reclaim_data* for anything nested. nc_copy_data/nc_copy_data_all deep-copy dynamic values into caller-provided or fresh top-level storage.
  • Strings (Sprint 1P): NC_STRING variables and attributes survive close/reopen in native and NC_NETCDF4_MODEL files. Strings may also be scalar or array compound members, vlen bases, and leaves of recursively nested compound/vlen graphs. NC_STD_REF remains excluded from user-defined type graphs; classic-model files reject strings and user types.

Strings, fills and ownership

Each standalone string element is a char *; an array is a char **. Vlen-of-string payloads are arrays of pointers, not character arrays. Compound string fields use pointer-sized slots at the declared offsets. Write calls borrow application buffers for the duration of the call; attribute and fill caches deep-copy their contents. NULL standalone input elements are normalized to empty strings. String values are NUL-terminated: bytes after the first NUL are not part of the value. New files tag variable-length strings as UTF-8, matching upstream, without validating, normalizing or transcoding payload bytes. Legacy ASCII-tagged variable-length strings remain readable and writable; memory datatypes preserve stored character-set tags recursively.

Successful reads return independently owned payloads. Free standalone arrays with nc_free_string(count, values); this releases the strings, not the pointer array. Use nc_reclaim_data(ncid, xtype, values, count) for nested values, then free an allocated outer buffer yourself, or use nc_reclaim_data_all for both. Reclaim nested values before closing the file because type metadata is needed. Vlen element helpers still borrow/copy descriptors and pointer values; those aliases are not additional owned deep copies.

Standalone string variables default to an allocated empty string on read. Custom nc_def_var_fill/_FillValue strings are owned internally, and fill inquiries return independent copies. nc_def_var_fill(..., NC_NOFILL, ...) returns NC_EINVAL for standalone strings. File-wide fill defaults do not change string/user-type fill rules. Dynamic user-type defaults remain zeroed descriptors (empty vlens/null embedded pointers). Dynamic filled datasets use early HDF5 allocation at creation/extension: this materializes fills while writable, avoiding HDF5 2.1.1's attempt to write a vlen heap when reading an unallocated chunk read-only. Large sparse dynamic datasets can therefore consume more storage when extended.

NC_NETCDF4_MODEL string output is readable by upstream netcdf-c; nxCDF can read and update upstream-created string variables/attributes and nested types. Foreign anonymous compound reads retain the packed-native-layout limitation described above. One-byte fixed-length HDF5 strings remain NC_CHAR; wider fixed-length HDF5 strings are unsupported. This is selected string interoperability, not support for every HDF5 schema or the entire upstream test suite. The three upstream ports tst_strings, tst_strings2 and tst_atts_string_rewrite run in both modes alongside focused regressions under the nxcdf_strings CTest label and the existing normal/sanitizer CI jobs.

Storage and Metadata

nxCDF stores NetCDF dimensions as HDF5 dimension scales and attaches them to variable datasets. It writes the following hidden attributes for fast, reliable round-tripping:

Attribute Location Purpose
_NxcdfBackend root group Identifies the file as created by nxCDF
_NxcdfModel root group Present only in NC_NETCDF4_MODEL mode
_Netcdf4Dimid dimension scale Stores the stable dimension ID
_NxcdfVarid variable dataset Stores the stable variable ID
_Netcdf4Coordinates variable Stores the variable's dimension IDs

These attributes are filtered from user-visible attribute lists.

nxCDF also writes the upstream provenance attributes so h5dump, ncdump -s and third-party tools treat its files exactly like upstream NetCDF-4 files:

Attribute Storage Content
_NCProperties stored on root group version=2,netcdf=<netcdf-c version>,hdf5=<HDF5 version>,nxcdf=<NEP version> — upstream's shape plus a nxcdf key
_nc3_strict stored on root group 1 (int), NC_CLASSIC_MODEL files only
_IsNetcdf4 virtual 1 when the file carries netCDF-4 markers; synthesized on name lookup
_SuperblockVersion virtual the HDF5 FCPL superblock version; synthesized on name lookup

Like upstream, _NCProperties/_nc3_strict are written at _enddef/nc_sync/nc_close, only for files nxCDF created, and never overwrite an existing stamp — an updated file keeps its original creator's provenance, and a file that never had a stamp stays unstamped. _IsNetcdf4 and _SuperblockVersion are never stored: nc_inq_att/nc_get_att answer them by name on the global attribute list (NC_INT, length 1), nc_inq_attid returns NC_EATTMETA, and none of the names appear in nc_inq_natts/nc_inq_attname. _Format is synthesized by tools from nc_inq_format (NC_FORMAT_NETCDF4), not stored. All of these names remain reserved — nc_put_att/nc_rename_att/nc_del_att reject them.

Filters

nxCDF supports the full NetCDF-4 filter model on non-scalar variables. Filters are stored in the shared NC_VAR_INFO_T::filters list — the same structure upstream netcdf-c uses — and are applied to the HDF5 dataset creation property list in pipeline order.

/* Convenience functions for the standard filters */
nc_def_var_deflate(ncid, varid, 1 /* shuffle */, 1 /* deflate */, 4 /* level */);
nc_def_var_fletcher32(ncid, varid, NC_FLETCHER32);
nc_def_var_szip(ncid, varid, NC_SZIP_NN, 16);
/* Generic filter API for plugin filters such as bzip2 (307) or zstd (32015) */
unsigned int level = 3;
nc_def_var_filter(ncid, varid, H5Z_FILTER_ZSTD, 1, &level);
/* Inquiry */
size_t nfilters;
unsigned int ids[8], params[8];
nc_inq_var_filter_ids(ncid, varid, &nfilters, ids);
nc_inq_var_filter_info(ncid, varid, ids[0], &nparams, params);
nc_inq_filter_avail(ncid, H5Z_FILTER_BZIP2); /* NC_ENOFILTER if the plugin is absent */

Semantics match upstream netcdf-c:

  • Defining a filter forces chunked storage; a filtered variable cannot be set contiguous.
  • Scalar variables cannot have filters (NC_EINVAL).
  • Filters cannot be removed once defined; re-defining a filter id replaces its parameters.
  • Fletcher32 is always first in the pipeline, then shuffle, then remaining filters in definition order.
  • Deflate and szip are mutually exclusive; a deflate level of 0 adds no filter.
  • All dataset-property setters — filters, chunking, deflate, fletcher32, fill, endianness, quantization — return NC_ELATEDEF once the variable's dataset is committed (after _enddef commits define mode); calls outside define mode return NC_ENOTINDEFINE.
  • nc_inq_var_filter_ids on a filterless variable returns NC_NOERR with a count of zero.

Plugin filters work through HDF5's plugin mechanism: set HDF5_PLUGIN_PATH (or install plugins in HDF5's default plugin directory) and the filter becomes available to nc_def_var_filter and to data I/O. When a file is opened whose variable uses a filter that is unavailable in the current process, the variable's metadata — including the filter pipeline — remains readable, but reads and writes return NC_ENOFILTER.

NC_CLASSIC_MODEL files permit no filters: every filter and storage setter (nc_def_var_filter, nc_def_var_deflate, nc_def_var_fletcher32, nc_def_var_szip, nc_def_var_chunking) returns NC_EINVAL.

Filters may be applied to coordinate variables. Setter-triggered dataset rebuilds are deferred to _enddef, which detaches and re-attaches dependent variables' dimension-scale references around the recreation, so dependents keep valid attachments in any definition order.

Example: Create a File with a Variable

#include <netcdf.h>
int ncid, dimid, varid;
size_t start[2] = {0, 0}, count[2] = {2, 3};
float data[2][3] = {{1.0f, 2.0f, 3.0f}, {4.0f, 5.0f, 6.0f}};
nc_create("example.nc", NC_NXCDF | NC_CLOBBER, &ncid);
nc_def_dim(ncid, "y", 2, &dimid);
nc_def_dim(ncid, "x", 3, &dimid);
nc_def_var(ncid, "data", NC_FLOAT, 2, (int[]){dimid - 1, dimid}, &varid);
nc_enddef(ncid);
nc_put_vara_float(ncid, varid, start, count, &data[0][0]);
nc_close(ncid);

Example: bfloat16 Storage

bfloat16 (brain floating point) is a 16-bit floating-point format with 1 sign bit, 8 exponent bits and 7 mantissa bits. It has the same range as float32 — the exponent width is identical — but only about 2-3 significant decimal digits of precision, and takes half the storage. NC_BFLOAT16 halves the file size for data where a relative error of up to 2^-8 (about 0.4%) is acceptable.

nxCDF stores bfloat16 as raw bit patterns; conversion between float32 and bfloat16 can be done in user code or by the internal conversion engine through the typed calls. The rules for NC_BFLOAT16 variables:

  • Use nc_put_vara_bfloat16/nc_get_vara_bfloat16 (or the untyped nc_put_vara/nc_get_vara) with a uint16_t buffer, or the typed float/double calls, which the internal engine converts with round-to-nearest-even (finite overflow becomes signed infinity, reported as NC_ERANGE after all values are processed). The nc_*_bfloat16 functions also convert to and from NC_FLOAT/NC_DOUBLE variables with round-to-nearest-even (see NXCDF_float16.md). The same applies to NC_FLOAT16 and the nc_*_float16 functions, with binary16's smaller range (maximum 65504).
  • Fill values work: nc_def_var_fill and a _FillValue attribute both store the raw bfloat16 bits in the dataset's fill message, which survives close/reopen and fills unwritten elements; the _FillValue attribute is visible through nc_inq_att after reopen.

See NXCDF_float16.md for the full float16/bfloat16 user guide, a walkthrough of the examples/nxcdf/bfloat16.c example, and the benchmark results.

Reference API

nxCDF exposes HDF5 2.x unified references (H5T_STD_REF / H5R_ref_t) as NC_STD_REF, with functions for creating and resolving object, region, and attribute references — including references into external files. These are only available in native mode and are declared in nep.h. See refs.md for the full reference.

#include <nep.h>
/* Create an object reference to a variable, group, or user-defined type */
nc_ref_object(ncid, NC_REF_OBJ_VAR, varid, &ref);
/* Resolve a reference back to NetCDF ids */
int obj_type, grpid, id;
nc_deref_object(ncid, &ref, &obj_type, &grpid, &id);
#define NC_REF_OBJ_VAR
Definition nxcdf.h:137
struct nc_std_ref_s nc_std_ref_t
int nc_ref_free(nc_std_ref_t *ref)
Release the resources held by a reference token.
Definition nxcdf_ref.c:1277
int nc_deref_object(int ncid, const nc_std_ref_t *ref, int *obj_typep, int *grpidp, int *idp)
Resolve an object reference token back to NetCDF ids.
Definition nxcdf_ref.c:501
int nc_ref_object(int ncid, int obj_type, int objid, nc_std_ref_t *ref)
Create an HDF5 object reference to a variable, group, or user-defined type in a nxCDF file.
Definition nxcdf_ref.c:398

Region references identify a regular strided hyperslab selection inside a variable; attribute references name an attribute on any object kind:

nc_std_ref_t region, att;
size_t start[2] = {0, 0}, count[2] = {1, 10};
nc_ref_region(ncid, varid, start, count, NULL, &region);
nc_ref_attr(ncid, NC_REF_OBJ_VAR, varid, "units", &att);
int nc_ref_region(int ncid, int varid, const size_t *startp, const size_t *countp, const ptrdiff_t *stridep, nc_std_ref_t *ref)
Create a dataset region reference to a strided hyperslab of a variable in a nxCDF file.
Definition nxcdf_ref.c:565
int nc_ref_attr(int ncid, int obj_type, int objid, const char *att_name, nc_std_ref_t *ref)
Create an HDF5 attribute reference to an attribute on a variable, group, or committed datatype in a n...
Definition nxcdf_ref.c:780

Tokens hold an open reference to their file — release them with nc_ref_free() before closing the file.

Remote references are a separate mechanism: a stored (URL, object path) string convention resolved by nc_ref_open_remote() and nc_ref_resolve() through nc_open() — DAP2, DAP4 or NCZarr byte-range dispatch, returning an ordinary upstream session ncid. They require building with NEP_ENABLE_REMOTE_REFS=ON and a remote-capable NetCDF-C; otherwise the functions return NC_ENOTBUILT. See refs.md.

Known Limitations

  • NC_NXCDF must be passed explicitly; there is no magic-number dispatch for HDF5 files.
  • nc_put_varm and nc_get_varm are not implemented in nxCDF because the mapped I/O API is deprecated.
  • External references require an absolute target filename and a writable target file (HDF5 may reopen it read-write); open the target with nc_ref_open_target and dereference against the returned session — see refs.md.
  • Reference-typed variables cannot be nested inside compound types.
  • Small floating-point types (NC_FLOAT16, NC_BFLOAT16, FP8/FP6/FP4) and NC_STD_REF may be coordinate variables in native mode; the dimension-scale dataset carries the variable's dtype, and ordering/comparison semantics of non-IEEE encodings are the caller's concern.
  • Small floating-point types (NC_FLOAT6_E2M3, NC_FLOAT4_E2M1) have no default fill value because their value spaces are too small for a useful sentinel.
  • Typed calls on FP8/FP6/FP4 variables, and integer/text/string typed calls on any small float, return NC_EBADTYPE (NC_ECHAR for text).
  • Out-of-range conversion results can differ between engines even though both return NC_ERANGE after completing the operation; see the conversion experiment above (issue #381).
  • Def-var setters (nc_def_var_fill, nc_def_var_chunking, nc_def_var_deflate, nc_def_var_endian, nc_def_var_fletcher32, nc_def_var_filter) defer their dataset rebuild to _enddef; each variable is rebuilt at most once and a rebuilt variable leaves one orphaned HDF5 object header (~200-400 bytes) of dead space in the file.

Resources