|
NEP 4.3.0
NetCDF Expansion Pack
|
nxCDF stores HDF5 references through the atomic nc_type NC_STD_REF, which maps to the HDF5 2.x unified reference datatype H5T_STD_REF. Each element of an NC_STD_REF variable or attribute is one opaque 64-byte nc_std_ref_t token — layout-compatible with HDF5 H5R_ref_t. A single token format covers all three HDF5 reference kinds: object references, dataset region references, and attribute references, including references to objects in other files.
The older HDF5 reference datatypes (H5T_STD_REF_OBJ, H5T_STD_REF_DSETREG and their hobj_ref_t/hdset_reg_ref_t tokens) are deprecated in HDF5 2.x and are not supported. A file containing a dataset of either deprecated type fails nc_open with NC_EBADTYPE.
Reference functions are declared in nep.h and operate only on files opened or created with NC_NXCDF.
NC_STD_REF variables and attributes are defined and accessed like any other atomic type: nc_def_var, nc_put_att, nc_put_var, nc_put_vara, nc_put_vars, and the matching nc_get_* calls move whole tokens. nc_inq_type reports a size of 64. Each element is an independent token; strided access reads or writes independent references.
All create functions take an open ncid, fill a caller-supplied nc_std_ref_t, and require native mode (NC_ENOTNC4 otherwise, including on a non-NXCDF ncid).
Creates an object reference. obj_type selects what objid is: NC_REF_OBJ_VAR — a varid in group ncid; NC_REF_OBJ_GRP — a group ncid (ncid ignored); NC_REF_OBJ_TYPE — a typeid in group ncid.
Creates a dataset region reference to a hyperslab selection in variable varid. start/count/stride use normal NetCDF indexing; stride may be NULL for unit stride. Any selection expressible as start/count/stride is allowed.
Creates an attribute reference. obj_type/objid dispatch exactly as in nc_ref_object, so attribute references work on every attribute-bearing object kind HDF5 supports: variables, groups (the group's attributes, i.e. what nc_put_att attaches to NC_GLOBAL), and committed datatypes. Note the NetCDF API cannot put attributes on types — a datatype attribute exists only if created through raw HDF5 — but a reference can point at it regardless. att_name must name an existing attribute on the object (NC_EINVAL otherwise).
Dereference functions resolve a token back to NetCDF ids in the file identified by ncid. They accept any NXCDF mode and return NC_EINVAL when the token kind does not match the function, the session is not the file the token points into, or the target no longer exists. For a token pointing into another file, pass the session returned by nc_ref_open_target as ncid (see below); the ids it returns belong to that target session and remain valid until it is closed.
*obj_typep receives NC_REF_OBJ_VAR/NC_REF_OBJ_GRP/NC_REF_OBJ_TYPE. For a variable, *grpidp is the containing group and *idp the varid; for a group, both receive the group ncid; for a type, *grpidp is the containing group and *idp the typeid.
Recovers the target variable and its selection. Only regular strided hyperslabs are representable: an irregular selection, or one built with non-unit blocks, returns NC_EINVAL. Output pointers may be NULL to skip a result.
Returns the attribute-bearing object's kind and ids as in nc_deref_object, plus the attribute name in att_name, which must be a caller buffer of at least NC_MAX_NAME + 1 bytes. For NC_REF_OBJ_TYPE owners the attribute value is read through the HDF5 API (H5Aopen/H5Aread, or H5Ropen_attr on the token), since the NetCDF API has no datatype-attribute calls.
The nc_ref_*_ext functions create tokens that reference objects in a different file. ncid is still validated as a native-mode NXCDF file (the token can then be stored in its NC_STD_REF data), but the target is named by filename plus path, the object's HDF5 path in that file (for example "grp/dset" or "/grp/dset").
path may name any attribute-bearing object kind, so nc_ref_attr_ext covers the same owner set as nc_ref_attr. The resulting token records filename and HDF5 resolves it at open time.
Two requirements apply to filename:
nc_ref_open_target opens the file an external token points into as a new nxCDF session (NC_NXCDF|NC_WRITE) and returns its NetCDF file ID. The caller owns the returned session and closes it with nc_close; each call returns an independent session even for tokens into the same file. Pass the returned target_ncid to nc_deref_object, nc_deref_region, or nc_deref_attr to resolve the token — the returned group, variable and type IDs belong to the target session and remain valid until it is closed. Token lifetime is separate: nc_ref_free the token when done; freeing it does not close the target session, and closing the session does not affect the token.
Errors: NC_EINVAL for an in-file token, a stored filename that is not absolute, or a deref session that is not the token's target; NC_ENOTNC4 when the target is not a nxCDF-readable NetCDF-4 file; NC_EPERM-family errors when the target cannot be opened read-write; nc_open's codes (for example NC_ENOTNC) for a missing or unreadable target. On failure *target_ncidp is -1.
One HDF5 caveat: while any token references a target file, HDF5 holds that file open internally with its default file-access properties. A plain nc_open on the same file can therefore fail with an HDF error until the tokens are released with nc_ref_free — sessions obtained through nc_ref_open_target use a compatible open mode and are not affected.
Remote references are not NC_STD_REF tokens — HDF5 tokens store filesystem filenames and cannot name a remote resource. A remote reference is a stored convention of two strings, a URL and an object path, carried in NC_STRING variables or attributes. No names are reserved: any parallel pair of string holders works — for example an NC_STRING variable ref_url whose values are URLs and a sibling ref_path of the same shape holding object paths, or two NC_STRING attributes.
The remote session is opened with nc_open(url, NC_NOWRITE, ...) — an ordinary upstream NetCDF file ID, not a nxCDF session — so any remote-capable dispatch in the installed NetCDF-C answers: DAP2, DAP4, or NCZarr file:///byte-range (https://...#mode=bytes) stores. s3:// URLs require NetCDF-C built with NC_HAS_S3 and are untested. Remote access is read-only; there is no constraint-expression or subsetting API beyond the stored object path.
nc_ref_open_remote validates ncid as a nxCDF file (context only), opens url, and resolves path in one call, returning a caller-owned session the caller must nc_close. Each call returns an independent session. If the open succeeds but the path fails to resolve, the session is closed and *target_ncidp is -1. nc_ref_resolve resolves additional paths on the same session without paying another remote open.
The path grammar is a leading /, /-separated group components, and a final component naming a variable, group, or committed datatype, with an optional #attname suffix selecting an attribute of that object. / alone resolves to the root group. The final component resolves as variable, then group, then committed type. kindp receives NC_REF_OBJ_VAR, NC_REF_OBJ_GRP, NC_REF_OBJ_TYPE, or NC_REF_OBJ_ATT for a #attname path (then att_name receives the attribute name and objidp the owner's ID). Attribute existence is verified for variable and group owners; committed-datatype attributes are returned unchecked because the NetCDF API cannot inquire them.
The feature requires building NEP with NEP_ENABLE_REMOTE_REFS=ON (reported as NEP_HAS_REMOTE_REFS in nep_meta.h), which needs nxCDF and a NetCDF-C with NC_HAS_DAP2, NC_HAS_DAP4 or NC_HAS_BYTERANGE. Without it both functions return NC_ENOTBUILT.
Errors: NC_EINVAL for bad arguments or malformed paths, NC_ENOTVAR for an unresolvable object, NC_ENOTATT for a missing attribute, NC_ENOTBUILT when the feature is off, and the propagated nc_open/inquiry error for unreachable or non-NetCDF endpoints.
See examples/nxcdf/remote_refs.c for a complete program.
Reports the token kind: NC_STD_REF_OBJECT, NC_STD_REF_REGION, or NC_STD_REF_ATTR.
Copy the stored file name (of the file the token points into, which differs from the containing file only for external references) and the object's path within that file into caller buffers of size bytes. Both return NC_EINVAL when size is too small. For an attribute reference, nc_ref_get_obj_name returns the name of the object that carries the attribute; nc_deref_attr recovers the attribute name itself.
A reference token holds an open reference to the file it was created in; until it is released the HDF5 file cannot fully close, and a later H5Fopen on the same file fails. Call nc_ref_free on every token obtained from an nc_ref_* creator — and on tokens read back from a variable or attribute — when it is no longer needed, and always before nc_close. Releasing an uninitialized or already-released token is safe.
External variant (absolute filename required):