19 #define FILE_NAME "sfc_pres_temp.nc" 26 #define LAT_NAME "latitude" 27 #define LON_NAME "longitude" 28 #define PRES_NAME "pressure" 29 #define TEMP_NAME "temperature" 32 #define SAMPLE_PRESSURE 900 33 #define SAMPLE_TEMP 9.0 34 #define START_LAT 25.0 35 #define START_LON -125.0 39 #define PRES_UNITS "hPa" 40 #define TEMP_UNITS "celsius" 41 #define LAT_UNITS "degrees_north" 42 #define LON_UNITS "degrees_east" 43 #define MAX_ATT_LEN 80 47 #define ERR(e) {printf("Error: %s\n", nc_strerror(e)); return 2;} 52 int ncid, pres_varid, temp_varid;
53 int lat_varid, lon_varid;
56 float pres_in[NLAT][NLON];
57 float temp_in[NLAT][NLON];
60 float lats_in[NLAT], lons_in[NLON];
63 char pres_units_in[MAX_ATT_LEN], temp_units_in[MAX_ATT_LEN];
64 char lat_units_in[MAX_ATT_LEN], lon_units_in[MAX_ATT_LEN];
68 int ndims_in, nvars_in, ngatts_in, unlimdimid_in;
85 if ((retval =
nc_inq(ncid, &ndims_in, &nvars_in, &ngatts_in,
92 if (ndims_in != 2 || nvars_in != 4 || ngatts_in != 0 ||
93 unlimdimid_in != -1)
return 2;
109 for (lat = 0; lat < NLAT; lat++)
110 if (lats_in[lat] != START_LAT + 5.*lat)
112 for (lon = 0; lon < NLON; lon++)
113 if (lons_in[lon] != START_LON + 5.*lon)
118 if ((retval =
nc_inq_varid(ncid, PRES_NAME, &pres_varid)))
120 if ((retval =
nc_inq_varid(ncid, TEMP_NAME, &temp_varid)))
132 for (lat = 0; lat < NLAT; lat++)
133 for (lon = 0; lon < NLON; lon++)
134 if (pres_in[lat][lon] != SAMPLE_PRESSURE + (lon * NLAT + lat) ||
135 temp_in[lat][lon] != SAMPLE_TEMP + .25 * (lon * NLAT + lat))
142 if (strncmp(lat_units_in, LAT_UNITS, strlen(LAT_UNITS)))
147 if (strncmp(lon_units_in, LON_UNITS, strlen(LON_UNITS)))
150 if ((retval =
nc_get_att_text(ncid, pres_varid, UNITS, pres_units_in)))
152 if (strncmp(pres_units_in, PRES_UNITS, strlen(PRES_UNITS)))
155 if ((retval =
nc_get_att_text(ncid, temp_varid, UNITS, temp_units_in)))
157 if (strncmp(temp_units_in, TEMP_UNITS, strlen(TEMP_UNITS)))
return 2;
163 printf(
"*** SUCCESS reading example file sfc_pres_temp.nc!\n");
Main header file for the C API.
EXTERNL int nc_inq(int ncid, int *ndimsp, int *nvarsp, int *nattsp, int *unlimdimidp)
Inquire about a file or group.
#define NC_NOWRITE
Set read-only access for nc_open().
EXTERNL int nc_close(int ncid)
Close an open netCDF dataset.
EXTERNL int nc_get_att_text(int ncid, int varid, const char *name, char *ip)
Get an attribute.
EXTERNL int nc_open(const char *path, int mode, int *ncidp)
Open an existing netCDF file.
EXTERNL int nc_inq_varid(int ncid, const char *name, int *varidp)
Find the ID of a variable, from the name.
int nc_get_var_float(int ncid, int varid, float *ip)
Read an entire variable in one call.