Basic example demonstrating 2D array creation and reading in NetCDF (Fortran)
This is the Fortran equivalent of simple_2D.c, demonstrating the fundamental workflow for working with NetCDF files using the Fortran 90 NetCDF API. The program creates a 2D integer array, writes it to a NetCDF file with a global attribute ("title") and a variable attribute ("units"), then reopens the file to verify metadata, attributes, and data correctness using nf90_inquire(), nf90_inquire_dimension(), and nf90_inquire_variable().
Learning Objectives:
- Understand Fortran NetCDF API (nf90_* functions)
- Learn Fortran column-major vs C row-major array ordering
- Add global and variable attributes
- Query file metadata with nf90_inquire(), nf90_inquire_dimension(), nf90_inquire_variable()
- Master error handling with nf90_noerr and nf90_strerror()
- Work with Fortran array indexing (1-based vs C's 0-based)
- Verify equivalence with C version (simple_2D.c)
Key Concepts:
- Fortran Column-Major: Arrays stored column-first [i,j] vs C row-first [j][i]
- Dimension Ordering: Fortran reverses dimension order from C
- 1-Based Indexing: Fortran arrays start at 1, C arrays start at 0
- nf90 Module: Fortran 90 NetCDF interface (use netcdf)
- Error Handling: Check retval against nf90_noerr
Fortran vs C Differences:
- Array Declaration: Fortran data_out(NX, NY) vs C data_out[NY][NX]
- Dimension Order: Fortran dimids(1)=x, dimids(2)=y vs C dimids[0]=y, dimids[1]=x
- Indexing: Fortran 1-based (1 to N) vs C 0-based (0 to N-1)
- API Prefix: Fortran nf90_* vs C nc_*
- Error Handling: Fortran subroutine call vs C macro
Prerequisites:
Related Examples:
Compilation:
program f_simple_2d
Definition f_simple_2D.f90:66
Usage:
Expected Output: Creates f_simple_2D.nc containing:
- 2 dimensions: x(6), y(12)
- 1 variable: data(x, y) of type int
- 1 global attribute: title = "Simple 2D Example"
- 1 variable attribute: units = "m/s"
- Data: sequential integers from 0 to 71
- Output identical to simple_2D.c (verified via ncdump)
- Author
- Edward Hartnett, Intelligent Data Design, Inc.
- Date
- 2026