#include <stdio.h>
#include <string.h>
#define FILE_NAME "pres_temp_4D.nc"
#define NDIMS 4
#define NLAT 6
#define NLON 12
#define LAT_NAME "latitude"
#define LON_NAME "longitude"
#define NREC 2
#define REC_NAME "time"
#define LVL_NAME "level"
#define NLVL 2
#define PRES_NAME "pressure"
#define TEMP_NAME "temperature"
#define UNITS "units"
#define DEGREES_EAST "degrees_east"
#define DEGREES_NORTH "degrees_north"
#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;
size_t start[NDIMS], count[NDIMS];
float pres_in[NLVL][NLAT][NLON];
float temp_in[NLVL][NLAT][NLON];
float lats[NLAT], lons[NLON];
int lvl, lat, lon, rec, i = 0;
int retval;
ERR(retval);
ERR(retval);
ERR(retval);
ERR(retval);
ERR(retval);
for (lat = 0; lat < NLAT; lat++)
if (lats[lat] != START_LAT + 5.*lat)
return 2;
for (lon = 0; lon < NLON; lon++)
if (lons[lon] != START_LON + 5.*lon)
return 2;
ERR(retval);
ERR(retval);
count[0] = 1;
count[1] = NLVL;
count[2] = NLAT;
count[3] = NLON;
start[1] = 0;
start[2] = 0;
start[3] = 0;
for (rec = 0; rec < NREC; rec++)
{
start[0] = rec;
count, &pres_in[0][0][0])))
ERR(retval);
count, &temp_in[0][0][0])))
ERR(retval);
i = 0;
for (lvl = 0; lvl < NLVL; lvl++)
for (lat = 0; lat < NLAT; lat++)
for (lon = 0; lon < NLON; lon++)
{
if (pres_in[lvl][lat][lon] != SAMPLE_PRESSURE + i)
return 2;
if (temp_in[lvl][lat][lon] != SAMPLE_TEMP + i)
return 2;
i++;
}
}
ERR(retval);
printf("*** SUCCESS reading example file pres_temp_4D.nc!\n");
return 0;
}
EXTERNL int nc_close(int ncid)
Close an open netCDF dataset.
EXTERNL int nc_open(const char *path, int mode, int *ncidp)
Open an existing netCDF file.
int nc_get_vara_float(int ncid, int varid, const size_t *startp, const size_t *countp, float *ip)
Read an array of values from a variable.
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().