Read a NetCDF file and print all metadata (Fortran)
This example demonstrates how to use the NetCDF Fortran inquiry functions to discover and print all metadata in a NetCDF file without prior knowledge of its contents. It reads a filename from the command line, opens the file, and prints:
- All dimensions (name and length, noting unlimited dimensions)
- All global attributes (name, type, and value)
- All variables (name, type, dimensions, and attributes)
This is a useful pattern for building tools that inspect arbitrary NetCDF files.
Learning Objectives:
- Use nf90_inquire() to discover file structure without prior knowledge
- Iterate over dimensions with nf90_inquire_dimension() and detect unlimited
- Iterate over variables with nf90_inquire_variable() to get type, shape, and attribute count
- Iterate over attributes with nf90_inq_attname() and nf90_inquire_attribute()
- Read attribute values of different types (text, numeric, arrays)
- Handle unlimited dimensions specially in output
- Accept command-line arguments in Fortran with get_command_argument()
Key Concepts:
- Inquiry Functions: nf90_inquire*() family discovers file contents at runtime
- Variable Iteration: Loop from varid=1 to nvars to visit all variables
- Attribute Iteration: Loop from attnum=1 to natts for each variable or NF90_GLOBAL
- Type Discovery: nf90_inquire_attribute() returns xtype for proper interpretation
- NF90_GLOBAL: Special varid constant for accessing global (file-level) attributes
- Command-Line Arguments: get_command_argument() for Fortran 2003+ input
- Generic Tool Pattern: Read structure first, then interpret — enables tools that work on any NetCDF file regardless of contents
Prerequisites:
Related Examples:
Compilation:
Usage:
program f_coord_vars
Definition f_coord_vars.f90:51
Expected Output: Prints all metadata from the specified file:
- Dimensions with names and lengths (unlimited flagged)
- Global attributes with names, types, and values
- Variables with names, types, dimension lists, and per-variable 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