Read a NetCDF file and print all metadata (dimensions, variables, attributes)
This example demonstrates how to use the NetCDF 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 nc_inq() to discover file structure
- Iterate over dimensions with nc_inq_dim()
- Iterate over variables with nc_inq_var()
- Iterate over attributes with nc_inq_att() and nc_inq_attname()
- Read attribute values of different types
- Handle unlimited dimensions
Prerequisites: Basic C programming, familiarity with NetCDF concepts
Compilation:
gcc -o dump_classic_metadata dump_classic_metadata.c -lnetcdf
Usage:
./dump_classic_metadata coord_vars.nc
- Author
- Edward Hartnett, Intelligent Data Design, Inc.
- Date
- 2026