#include <stdio.h>
#include <string.h>
#define FILE_NAME "sfc_pres_temp.nc"
#define NDIMS 2
#define NLAT 6
#define NLON 12
#define LAT_NAME "latitude"
#define LON_NAME "longitude"
#define PRES_NAME "pressure"
#define TEMP_NAME "temperature"
#define SAMPLE_PRESSURE 900
#define SAMPLE_TEMP 9.0
#define START_LAT 25.0
#define START_LON -125.0
#define UNITS "units"
#define PRES_UNITS "hPa"
#define TEMP_UNITS "celsius"
#define LAT_UNITS "degrees_north"
#define LON_UNITS "degrees_east"
#define MAX_ATT_LEN 80
#define ERR(e) {printf("Error: %s\n", nc_strerror(e)); return 2;}
int
main()
{
int ncid, pres_varid, temp_varid;
int lat_varid, lon_varid;
float pres_in[NLAT][NLON];
float temp_in[NLAT][NLON];
float lats_in[NLAT], lons_in[NLON];
char pres_units_in[MAX_ATT_LEN], temp_units_in[MAX_ATT_LEN];
char lat_units_in[MAX_ATT_LEN], lon_units_in[MAX_ATT_LEN];
int ndims_in, nvars_in, ngatts_in, unlimdimid_in;
int lat, lon;
int retval;
ERR(retval);
if ((retval =
nc_inq(ncid, &ndims_in, &nvars_in, &ngatts_in,
&unlimdimid_in)))
ERR(retval);
if (ndims_in != 2 || nvars_in != 4 || ngatts_in != 0 ||
unlimdimid_in != -1) return 2;
ERR(retval);
ERR(retval);
ERR(retval);
ERR(retval);
for (lat = 0; lat < NLAT; lat++)
if (lats_in[lat] != START_LAT + 5.*lat)
return 2;
for (lon = 0; lon < NLON; lon++)
if (lons_in[lon] != START_LON + 5.*lon)
return 2;
ERR(retval);
ERR(retval);
ERR(retval);
ERR(retval);
for (lat = 0; lat < NLAT; lat++)
for (lon = 0; lon < NLON; lon++)
if (pres_in[lat][lon] != SAMPLE_PRESSURE + (lon * NLAT + lat) ||
temp_in[lat][lon] != SAMPLE_TEMP + .25 * (lon * NLAT + lat))
return 2;
ERR(retval);
if (strncmp(lat_units_in, LAT_UNITS, strlen(LAT_UNITS)))
return 2;
ERR(retval);
if (strncmp(lon_units_in, LON_UNITS, strlen(LON_UNITS)))
return 2;
ERR(retval);
if (strncmp(pres_units_in, PRES_UNITS, strlen(PRES_UNITS)))
return 2;
ERR(retval);
if (strncmp(temp_units_in, TEMP_UNITS, strlen(TEMP_UNITS))) return 2;
ERR(retval);
printf("*** SUCCESS reading example file sfc_pres_temp.nc!\n");
return 0;
}
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.
int nc_get_var_float(int ncid, int varid, float *ip)
Read an entire variable in one call.
Main header file for the C API.
#define NC_NOWRITE
Set read-only access for nc_open().