NEP 4.3.0
NetCDF Expansion Pack
Loading...
Searching...
No Matches
external_refs.c File Reference

Store references to variables in other files and follow them. More...

#include <netcdf.h>
#include <nep.h>
#include "nxcdf/nxcdf.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

Macros

#define NVALS   4
#define NREFS   2
#define TARGET_A   "external_ref_target_a.h5"
#define TARGET_B   "external_ref_target_b.h5"
#define INDEX_FILE   "external_refs.h5"
#define ERRCODE   2
#define ERR(e)

Functions

static void make_target (const char *name, const char *var_name, const char *units, const int *data)
int main (void)

Detailed Description

Store references to variables in other files and follow them.

This program demonstrates nxCDF external references. It creates two target files, each holding one variable with a little sample data and an attribute, then creates a third file whose refs variable stores one NC_STD_REF token per target variable. After closing everything, it reopens the third file, reads the tokens back, opens each target with nc_ref_open_target(), and resolves the references with nc_deref_object() to read the data and metadata straight through the NetCDF API.

Usage:

NCRCENV_RC=<build>/.ncrc NETCDF_RC=<build> ./external_refs

The nxCDF backend is loaded through the .ncrc UDF9 autoload mechanism, so NCRCENV_RC and NETCDF_RC must name the NEP build directory that contains the generated .ncrc. The program writes and reads three files in the current directory: external_ref_target_a.h5, external_ref_target_b.h5 and external_refs.h5.

Learning Objectives:

  • Create an external object reference with nc_ref_object_ext()
  • Store and reload NC_STD_REF tokens like ordinary variable data
  • Open a token's target file with nc_ref_open_target()
  • Resolve the target to NetCDF ids with nc_deref_object()
  • Understand the ownership contract for tokens and target sessions

Key Concepts:

  • Absolute filenames: external creators require an absolute filesystem path; a stored relative name would be reinterpreted against the reader's working directory.
  • Writable targets: HDF5 may reopen external references read-write, so target files must be writable by the application even though this program only reads them.
  • Object identity: a reference binds the target object, not its path, so the target variable can even be renamed after the token is created.
  • Two lifetimes: a token is released with nc_ref_free(); the target session returned by nc_ref_open_target() is released with nc_close(). Neither affects the other.

Key API functions:

Related Examples:

Author
Edward Hartnett, Intelligent Data Design, Inc.

Macro Definition Documentation

◆ ERR

#define ERR ( e)
Value:
{printf("Error: %s\n", nc_strerror(e)); exit(ERRCODE);}
#define ERRCODE
Definition bfloat16.c:84

◆ ERRCODE

#define ERRCODE   2

◆ INDEX_FILE

#define INDEX_FILE   "external_refs.h5"

File holding the reference tokens.

◆ NREFS

#define NREFS   2

Number of reference tokens stored in the index file.

◆ NVALS

#define NVALS   4

Number of sample values in each target variable.

◆ TARGET_A

#define TARGET_A   "external_ref_target_a.h5"

First target file.

◆ TARGET_B

#define TARGET_B   "external_ref_target_b.h5"

Second target file.

Function Documentation

◆ main()

int main ( void )

◆ make_target()

void make_target ( const char * name,
const char * var_name,
const char * units,
const int * data )
static