#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, lon_dimid, lat_dimid, lvl_dimid, rec_dimid;
int lat_varid, lon_varid, pres_varid, temp_varid;
int dimids[NDIMS];
size_t start[NDIMS], count[NDIMS];
float pres_out[NLVL][NLAT][NLON];
float temp_out[NLVL][NLAT][NLON];
float lats[NLAT], lons[NLON];
int lvl, lat, lon, rec, i = 0;
int retval;
for (lat = 0; lat < NLAT; lat++)
lats[lat] = START_LAT + 5.*lat;
for (lon = 0; lon < NLON; lon++)
lons[lon] = START_LON + 5.*lon;
for (lvl = 0; lvl < NLVL; lvl++)
for (lat = 0; lat < NLAT; lat++)
for (lon = 0; lon < NLON; lon++)
{
pres_out[lvl][lat][lon] = SAMPLE_PRESSURE + i;
temp_out[lvl][lat][lon] = SAMPLE_TEMP + i++;
}
ERR(retval);
if ((retval =
nc_def_dim(ncid, LVL_NAME, NLVL, &lvl_dimid)))
ERR(retval);
if ((retval =
nc_def_dim(ncid, LAT_NAME, NLAT, &lat_dimid)))
ERR(retval);
if ((retval =
nc_def_dim(ncid, LON_NAME, NLON, &lon_dimid)))
ERR(retval);
ERR(retval);
&lat_varid)))
ERR(retval);
&lon_varid)))
ERR(retval);
strlen(DEGREES_NORTH), DEGREES_NORTH)))
ERR(retval);
strlen(DEGREES_EAST), DEGREES_EAST)))
ERR(retval);
dimids[0] = rec_dimid;
dimids[1] = lvl_dimid;
dimids[2] = lat_dimid;
dimids[3] = lon_dimid;
dimids, &pres_varid)))
ERR(retval);
dimids, &temp_varid)))
ERR(retval);
strlen(PRES_UNITS), PRES_UNITS)))
ERR(retval);
strlen(TEMP_UNITS), TEMP_UNITS)))
ERR(retval);
ERR(retval);
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;
&pres_out[0][0][0])))
ERR(retval);
&temp_out[0][0][0])))
ERR(retval);
}
ERR(retval);
printf("*** SUCCESS writing example file %s!\n", FILE_NAME);
return 0;
}