20#define FILE_NAME "sfc_pres_temp.nc"
27#define LAT_NAME "latitude"
28#define LON_NAME "longitude"
29#define PRES_NAME "pressure"
30#define TEMP_NAME "temperature"
33#define SAMPLE_PRESSURE 900
34#define SAMPLE_TEMP 9.0
36#define START_LON -125.0
40#define PRES_UNITS "hPa"
41#define TEMP_UNITS "celsius"
42#define LAT_UNITS "degrees_north"
43#define LON_UNITS "degrees_east"
48#define ERR(e) {printf("Error: %s\n", nc_strerror(e)); return 2;}
53 int ncid, pres_varid, temp_varid;
54 int lat_varid, lon_varid;
57 float pres_in[NLAT][NLON];
58 float temp_in[NLAT][NLON];
61 float lats_in[NLAT], lons_in[NLON];
64 char pres_units_in[MAX_ATT_LEN], temp_units_in[MAX_ATT_LEN];
65 char lat_units_in[MAX_ATT_LEN], lon_units_in[MAX_ATT_LEN];
69 int ndims_in, nvars_in, ngatts_in, unlimdimid_in;
86 if ((retval =
nc_inq(ncid, &ndims_in, &nvars_in, &ngatts_in,
93 if (ndims_in != 2 || nvars_in != 4 || ngatts_in != 0 ||
94 unlimdimid_in != -1)
return 2;
100 if ((retval =
nc_inq_varid(ncid, LON_NAME, &lon_varid)))
104 if ((retval = nc_get_var_float(ncid, lat_varid, &lats_in[0])))
106 if ((retval = nc_get_var_float(ncid, lon_varid, &lons_in[0])))
110 for (lat = 0; lat < NLAT; lat++)
111 if (lats_in[lat] != START_LAT + 5.*lat)
113 for (lon = 0; lon < NLON; lon++)
114 if (lons_in[lon] != START_LON + 5.*lon)
119 if ((retval =
nc_inq_varid(ncid, PRES_NAME, &pres_varid)))
121 if ((retval =
nc_inq_varid(ncid, TEMP_NAME, &temp_varid)))
127 if ((retval = nc_get_var_float(ncid, pres_varid, &pres_in[0][0])))
129 if ((retval = nc_get_var_float(ncid, temp_varid, &temp_in[0][0])))
133 for (lat = 0; lat < NLAT; lat++)
134 for (lon = 0; lon < NLON; lon++)
135 if (pres_in[lat][lon] != SAMPLE_PRESSURE + (lon * NLAT + lat) ||
136 temp_in[lat][lon] != SAMPLE_TEMP + .25 * (lon * NLAT + lat))
143 if (strncmp(lat_units_in, LAT_UNITS, strlen(LAT_UNITS)) != 0)
148 if (strncmp(lon_units_in, LON_UNITS, strlen(LON_UNITS)) != 0)
151 if ((retval =
nc_get_att_text(ncid, pres_varid, UNITS, pres_units_in)))
153 if (strncmp(pres_units_in, PRES_UNITS, strlen(PRES_UNITS)) != 0)
156 if ((retval =
nc_get_att_text(ncid, temp_varid, UNITS, temp_units_in)))
158 if (strncmp(temp_units_in, TEMP_UNITS, strlen(TEMP_UNITS)) != 0)
return 2;
164 printf(
"*** SUCCESS reading example file sfc_pres_temp.nc!\n");
EXTERNL int nc_get_att_text(int ncid, int varid, const char *name, char *ip)
Get a text attribute.
EXTERNL int nc_close(int ncid)
Close an open netCDF dataset.
EXTERNL int nc_inq(int ncid, int *ndimsp, int *nvarsp, int *nattsp, int *unlimdimidp)
Inquire about a file or group.
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.
Main header file for the C API.
#define NC_NOWRITE
Set read-only access for nc_open().