OPeNDAP client-side subsetting example.
This example demonstrates client-side subsetting: opening the full remote dataset once, then making multiple targeted data requests using start/count arrays. This approach offers more flexibility than server-side constraints.
What this example does:
- Opens the full remote dataset (no constraint expression)
- Queries the variable dimensions to understand the full dataset shape
- Demonstrates four different data access patterns:
- Single time slice: Read all lat/lon for one time step
- Time series: Read all time points at a single lat/lon location
- Regional subset: Read a small 3D cube (3 time steps, 10x10 spatial)
- Multiple scattered requests: Show overhead of many small requests
- Closes the dataset
Client-side vs Server-side subsetting trade-offs:
Client-side (this example):
- Can make multiple different subset requests from one open
- Flexible exploration - change start/count without reopening
- Transfers more data over network for each request
- Server still reads full data then extracts subset
Server-side (constraint expressions):
- Server only sends the requested data
- Most bandwidth-efficient for single known subset
- Must reopen to change the subset
- Dimension queries return constrained sizes only
The 3D array layout is [time][lat][lon] with dimensions approximately [time~200][lat=89][lon=180] depending on the dataset version.
Learning Objectives:
- Open a full remote dataset without constraint expressions
- Use start[] and count[] arrays for multiple targeted data requests
- Implement four different access patterns (time slice, time series, regional, scattered)
- Understand the trade-offs between client-side and server-side subsetting
- Recognize the overhead of many small requests vs fewer large requests
Key Concepts:
- Client-Side Subsetting: Open the full dataset once, then use start/count in nc_get_vara_*() calls to select different subregions on each read
- Time Slice Access: Read all spatial points for one time step (start=[t,0,0], count=[1,nlat,nlon])
- Time Series Access: Read all time points at one spatial location (start=[0,lat,lon], count=[ntime,1,1])
- Regional Subset: Read a small 3D cube from the full dataset (start=[t0,lat0,lon0], count=[nt,nlat_sub,nlon_sub])
- Multiple Requests: One nc_open() supports unlimited nc_get_vara_*() calls with different start/count values — no need to reopen for each subset
Prerequisites:
- opendap_simple.c - Basic OPeNDAP access and metadata query
- simple_2D.c - Understanding start/count subsetting for local files
- A NetCDF-C library built with OPeNDAP support (NC_HAS_DAP2 or NC_HAS_DAP4)
Related Examples:
Compilation:
gcc -o opendap_subset opendap_subset.c -lnetcdf
Usage:
Expected Output:
- Opens remote SST dataset and prints full dimension sizes
- Demonstrates four access patterns with timing and data summaries: single time slice, time series at a point, regional 3D cube, and multiple scattered requests
- 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