NEP 2.7.0
NetCDF Extension Pack
Loading...
Searching...
No Matches
Macros | Functions
opendap_constraint.c File Reference

OPeNDAP constraint expression example - server-side subsetting. More...

#include <netcdf.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

Macros

#define ERR(e)
 

Functions

int main (void)
 

Detailed Description

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:

  1. 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
  2. Opens the constrained dataset - the server sees only the subset
  3. Demonstrates that dimension sizes reflect the CONSTRAINED subset, not the original full dataset dimensions
  4. Reads the entire (already subsetted) variable data
  5. 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:

Learning Objectives:

Key Concepts:

Prerequisites:

Related Examples:

Compilation:

gcc -o opendap_constraint opendap_constraint.c -lnetcdf

Usage:

./opendap_constraint

Expected Output:

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

Macro Definition Documentation

◆ ERR

#define ERR (   e)
Value:
do { \
if (e) { \
fprintf(stderr, "Error: %s at line %d\n", nc_strerror(e), __LINE__); \
return 1; \
} \
} while (0)

Function Documentation

◆ main()

int main ( void  )