|
| static const char * | type_name_str (int ncid, nc_type xtype, char *buf, size_t buflen) |
| |
| static int | print_att_value (int ncid, int varid, const char *att_name, nc_type xtype, size_t len) |
| |
| static int | print_attributes (int ncid, int varid, int natts, const char *indent) |
| |
| static int | print_user_types (int ncid, const char *indent) |
| |
| static int | print_group (int ncid, const char *group_name, const char *indent) |
| |
| int | main (int argc, char *argv[]) |
| |
Read a NetCDF-4 file and print all metadata including user-defined types.
This example demonstrates how to use the NetCDF-4 inquiry functions to discover and print all metadata in a NetCDF-4 file without prior knowledge of its contents. It reads a filename from the command line, opens the file, and prints:
- User-defined types (compound, enum, vlen, opaque)
- All dimensions (name and length, noting unlimited dimensions)
- All global attributes (name, type, and value)
- All variables (name, type, dimensions, and attributes)
- Groups (recursively)
This extends the classic dump_classic_metadata example with NetCDF-4 features.
Learning Objectives:
- Use nc_inq_typeids() to discover user-defined types in a group
- Use nc_inq_user_type() to determine type class (compound, enum, vlen, opaque)
- Use nc_inq_compound(), nc_inq_enum(), nc_inq_vlen(), nc_inq_opaque() for details
- Use nc_inq_grps() to discover child groups
- Recursively traverse the group hierarchy
- Build generic inspection tools for NetCDF-4 files with complex structure
Key Concepts:
- User-Defined Types: Types beyond atomic (compound, enum, vlen, opaque)
- Type Discovery: nc_inq_typeids() returns all type IDs in a group
- Type Classes: NC_COMPOUND, NC_ENUM, NC_VLEN, NC_OPAQUE identified by nc_inq_user_type()
- Group Hierarchy: nc_inq_grps() returns child group IDs for recursive traversal
- Recursive Pattern: Process root group, then recurse into each child group
- NC_GLOBAL in Groups: Each group has its own global attributes, accessed via group ncid
- Type Scope: User-defined types are visible in their defining group and descendants
Prerequisites:
Related Examples:
Compilation:
gcc -o dump_nc4_metadata dump_nc4_metadata.c -lnetcdf
Usage:
./dump_nc4_metadata user_types.nc
Expected Output: Prints all metadata from the specified NetCDF-4 file:
- User-defined types with class, size, and member details
- Dimensions (with unlimited flagged) per group
- Global attributes per group
- Variables with types, dimensions, and per-variable attributes
- Recursive output for all child groups
- 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