Read a NetCDF-4 file and print all metadata including user-defined types.
Fortran equivalent of dump_nc4_metadata.c. Reads a filename from the command line, opens the file, and prints all metadata:
- User-defined types (enum, 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)
Note: Fortran does not handle VLEN, String, or Compound types portably, so those type classes are reported by name only without detail.
Learning Objectives:
- Use nf90_inq_typeids() to discover user-defined types
- Use nf90_inq_user_type() to determine type class (enum, opaque, compound, vlen)
- Use nf90_inq_enum(), nf90_inq_enum_member(), nf90_inq_opaque()
- Use nf90_inq_grps() to discover groups and traverse hierarchies recursively
- Build a generic metadata inspection tool for NetCDF-4/HDF5 files
Key Concepts:
- User-Defined Types: NetCDF-4 supports enum, opaque, compound, and vlen types; Fortran handles enum and opaque well but has limited compound/vlen support
- Type Class Discovery: nf90_inq_user_type() returns the type class
- Group Hierarchy: nf90_inq_grps() returns child group IDs; recurse to traverse
- Recursive Traversal: Process root group, then recurse into each child group
- NF90_GLOBAL: Used for file-level and group-level attribute access
Prerequisites:
Related Examples:
Compilation:
Usage:
program f_user_types
Definition f_user_types.f90:52
Expected Output: Prints all metadata from the specified NetCDF-4 file:
- User-defined types with class, name, and member details
- Dimensions with names and lengths (unlimited flagged)
- Global attributes with names, types, and values
- Variables with names, types, dimensions, and per-variable attributes
- Groups (recursively) with their own types, dimensions, variables, and attributes
- 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