Simple OPeNDAP example - open, read, and close a remote dataset.
This is the most basic OPeNDAP example, demonstrating the core concept: remote datasets are accessed using the same NetCDF API calls as local files.
What this example does:
- Opens a remote sea surface temperature dataset from the public OPeNDAP test server at test.opendap.org
- Queries dataset metadata (dimensions, variables, attributes)
- Reads information about the 'sst' variable and its dimensions
- Reads a small 5x5 spatial subset from the first time step
- Closes the remote connection
Key OPeNDAP concepts demonstrated:
- Using an HTTP URL instead of a local file path in nc_open()
- The NetCDF library handles all HTTP communication transparently
- Data is transferred over the network only when read functions are called
- Subsetting with start/count arrays reduces data transfer
The dataset accessed is sst.mnmean.nc.gz, a classic netCDF-3 file containing NOAA Extended Reconstructed Sea Surface Temperature data. The server transparently decompresses and serves the data via OPeNDAP.
Learning Objectives:
- Open a remote dataset by passing an HTTP URL to nc_open()
- Query remote dataset metadata (dimensions, variables, attributes)
- Read a spatial subset using start/count arrays to minimize data transfer
- Understand that the netCDF API is transport-agnostic (local file vs remote URL)
- Recognize that data transfer occurs only on nc_get_var_*() calls, not on open
Key Concepts:
- OPeNDAP: Open-source protocol for accessing remote scientific data via HTTP
- Transparent Access: nc_open() with a URL triggers the OPeNDAP dispatch layer; all subsequent nc_*() calls work identically to local file access
- Lazy Data Transfer: Opening a URL fetches only metadata (DAS/DDS); actual array data is transferred only when nc_get_var_*() is called
- Client-Side Subsetting: Use start[] and count[] arrays in nc_get_vara_*() to request only needed data, reducing network bandwidth
- NC_NOWRITE: Remote datasets are always read-only; this flag is required
Prerequisites:
- simple_2D.c - Basic nc_open/nc_inq/nc_get_var workflow
- coord_vars.c - Understanding dimension and variable relationships
- A NetCDF-C library built with OPeNDAP support (NC_HAS_DAP2 or NC_HAS_DAP4)
Related Examples:
Compilation:
gcc -o opendap_simple opendap_simple.c -lnetcdf
Usage:
Expected Output:
- Prints dataset metadata (dimensions, variables, attributes)
- Prints dimension names and sizes
- Reads and displays a 5x5 spatial subset of SST from the first 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