OPeNDAP constraint expression example in Fortran.
This is the Fortran equivalent of opendap_constraint.c, demonstrating server-side subsetting using OPeNDAP constraint expressions.
What this example does:
- Builds a constrained URL: base_url + "?sst[0:2][0:88][0:179]"
- Opens the constrained dataset - only 3 time steps are visible
- Shows that nf90_inquire_dimension returns CONSTRAINED sizes
- Dynamically allocates array based on constrained dimensions
- Reads all constrained data with nf90_get_var
- Reopens with stride constraint [0:2:12] to sample every 2nd step
IMPORTANT indexing note:
- Constraint expressions in the URL use 0-based indexing: [0:2]
- Fortran nf90_get_var uses 1-based indexing for start/count
- These are independent - constraints happen at the server, Fortran indexing happens in the client API
Server-side subsetting reduces network transfer by having the OPeNDAP server extract only requested data. The constrained dataset appears to have only the subset dimensions, not the full original dimensions.
Learning Objectives:
- Build constrained OPeNDAP URLs with server-side subsetting expressions
- Understand that constraint expressions use 0-based indexing (not Fortran-based)
- See how nf90_inquire_dimension() returns constrained sizes, not full sizes
- Use allocatable arrays to handle dynamically-sized constrained data
- Apply stride constraints for temporal sampling
Key Concepts:
- Server-Side Subsetting: Constraint expression appended to URL (e.g., ?sst[0:2][0:88][0:179]) tells the server to send only the requested subset
- Constrained Dimensions: After opening a constrained URL, dimension queries return the constrained sizes (e.g., time=3 instead of full time=1857)
- 0-Based Constraints: URL constraints always use 0-based C-style indexing regardless of the client language; Fortran nf90_get_var still uses 1-based
- Stride Expressions: [start:stride:stop] syntax (e.g., [0:2:12]) samples every Nth element on the server side
- Dynamic Allocation: Fortran allocatable arrays adapt to constrained sizes
Prerequisites:
Related Examples:
Compilation:
program f_opendap_constraint
Definition f_opendap_constraint.f90:86
Usage:
Expected Output:
- Opens constrained SST dataset (3 time steps, full spatial) and prints constrained dimension sizes
- Reads and displays a sample of the constrained data
- Reopens with stride constraint and shows sampled time steps
- 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
- Date
- 6/15/26