OPeNDAP constraint expression example - server-side subsetting.
This example demonstrates server-side subsetting using OPeNDAP constraint expressions appended to URLs. The server extracts only the requested data, significantly reducing network bandwidth compared to downloading full files.
What this example does:
- Constructs a constrained URL by appending ?sst[0:2][0:88][0:179] to the base URL, requesting only the first 3 time steps of the SST variable
- Opens the constrained dataset - the server sees only the subset
- Demonstrates that dimension sizes reflect the CONSTRAINED subset, not the original full dataset dimensions
- Reads the entire (already subsetted) variable data
- Reopens with a stride constraint [0:2:12] to sample every 2nd time step
Constraint expression syntax: ?variable[start:stop] - Range selection (inclusive) ?variable[start:stride:stop] - Range with stride ?var1[...],var2[...] - Multiple variables
Key concept: With constraints, nc_inq_dimlen() returns the SUBSET size, not the original dataset size. This is different from client-side subsetting where the full dataset is opened and start/count arrays select subsets.
When to use constraints:
- When you know exactly what subset you need before opening
- For large reductions in data size (e.g., one region from global data)
- When making a single request for a specific time/space subset
Learning Objectives:
- Construct OPeNDAP constraint expressions appended to URLs
- Understand that constrained dimensions report subset sizes, not full sizes
- Use range selection [start:stop] and stride [start:stride:stop] syntax
- Compare server-side constraints with client-side subsetting (start/count)
- Recognize when server-side subsetting is more bandwidth-efficient
Key Concepts:
- Constraint Expression: URL suffix (?var[start:stop]) that tells the server to extract and return only the specified subset
- Server-Side Subsetting: The OPeNDAP server processes the constraint and sends only the requested data — reduces network transfer significantly
- Dimension Size After Constraint: nc_inq_dimlen() returns the constrained subset size, not the original full dimension size
- Stride Selection: [start:stride:stop] samples every Nth element along a dimension, useful for quick-look previews of large datasets
- Multiple Variables: Comma-separated constraints (?var1[...],var2[...]) can select subsets of multiple variables in one open
Prerequisites:
- opendap_simple.c - Basic OPeNDAP access pattern
- simple_2D.c - Understanding start/count arrays for local subsetting
- A NetCDF-C library built with OPeNDAP support (NC_HAS_DAP2 or NC_HAS_DAP4)
Related Examples:
Compilation:
gcc -o opendap_constraint opendap_constraint.c -lnetcdf
Usage:
Expected Output:
- Opens a constrained URL requesting first 3 time steps of SST
- Prints constrained dimension sizes (subset, not full dataset)
- Reads the entire constrained variable
- Reopens with a stride constraint to sample every 2nd time step
- 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