18 #define FILE_NAME "pres_temp_4D.nc"
25 #define LAT_NAME "latitude"
26 #define LON_NAME "longitude"
28 #define REC_NAME "time"
29 #define LVL_NAME "level"
33 #define PRES_NAME "pressure"
34 #define TEMP_NAME "temperature"
36 #define DEGREES_EAST "degrees_east"
37 #define DEGREES_NORTH "degrees_north"
40 #define SAMPLE_PRESSURE 900
41 #define SAMPLE_TEMP 9.0
42 #define START_LAT 25.0
43 #define START_LON -125.0
47 #define PRES_UNITS "hPa"
48 #define TEMP_UNITS "celsius"
49 #define LAT_UNITS "degrees_north"
50 #define LON_UNITS "degrees_east"
51 #define MAX_ATT_LEN 80
55 #define ERR(e) {printf("Error: %s\n", nc_strerror(e)); return 2;}
61 int ncid, lon_dimid, lat_dimid, lvl_dimid, rec_dimid;
62 int lat_varid, lon_varid, pres_varid, temp_varid;
67 size_t start[NDIMS], count[NDIMS];
71 float pres_out[NLVL][NLAT][NLON];
72 float temp_out[NLVL][NLAT][NLON];
75 float lats[NLAT], lons[NLON];
78 int lvl, lat, lon, rec, i = 0;
86 for (lat = 0; lat < NLAT; lat++)
87 lats[lat] = START_LAT + 5.*lat;
88 for (lon = 0; lon < NLON; lon++)
89 lons[lon] = START_LON + 5.*lon;
91 for (lvl = 0; lvl < NLVL; lvl++)
92 for (lat = 0; lat < NLAT; lat++)
93 for (lon = 0; lon < NLON; lon++)
95 pres_out[lvl][lat][lon] = SAMPLE_PRESSURE + i;
96 temp_out[lvl][lat][lon] = SAMPLE_TEMP + i++;
106 if ((retval =
nc_def_dim(ncid, LVL_NAME, NLVL, &lvl_dimid)))
108 if ((retval =
nc_def_dim(ncid, LAT_NAME, NLAT, &lat_dimid)))
110 if ((retval =
nc_def_dim(ncid, LON_NAME, NLON, &lon_dimid)))
130 strlen(DEGREES_NORTH), DEGREES_NORTH)))
133 strlen(DEGREES_EAST), DEGREES_EAST)))
140 dimids[0] = rec_dimid;
141 dimids[1] = lvl_dimid;
142 dimids[2] = lat_dimid;
143 dimids[3] = lon_dimid;
148 dimids, &pres_varid)))
151 dimids, &temp_varid)))
156 strlen(PRES_UNITS), PRES_UNITS)))
159 strlen(TEMP_UNITS), TEMP_UNITS)))
188 for (rec = 0; rec < NREC; rec++)
192 &pres_out[0][0][0])))
195 &temp_out[0][0][0])))
203 printf(
"*** SUCCESS writing example file %s!\n", FILE_NAME);
EXTERNL int nc_def_var(int ncid, const char *name, nc_type xtype, int ndims, const int *dimidsp, int *varidp)
Define a new variable.
EXTERNL int nc_def_dim(int ncid, const char *name, size_t len, int *idp)
Define a new dimension.
Main header file for the C API.
int nc_put_vara_float(int ncid, int varid, const size_t *startp, const size_t *countp, const float *op)
Write an array of values to a variable.
EXTERNL int nc_put_att_text(int ncid, int varid, const char *name, size_t len, const char *op)
Write a text attribute.
EXTERNL int nc_close(int ncid)
Close an open netCDF dataset.
#define NC_UNLIMITED
Size argument to nc_def_dim() for an unlimited dimension.
#define NC_CLOBBER
Destroy existing file.
int nc_put_var_float(int ncid, int varid, const float *op)
Write an entire variable with one call.
EXTERNL int nc_enddef(int ncid)
Leave define mode.
#define NC_FLOAT
single precision floating point number
EXTERNL int nc_create(const char *path, int cmode, int *ncidp)
Create a new netCDF file.