20#define FILE_NAME "sfc_pres_temp.nc"
27#define LAT_NAME "latitude"
28#define LON_NAME "longitude"
31#define PRES_NAME "pressure"
32#define TEMP_NAME "temperature"
34#define DEGREES_EAST "degrees_east"
35#define DEGREES_NORTH "degrees_north"
38#define SAMPLE_PRESSURE 900.0f
39#define SAMPLE_TEMP 9.0f
40#define START_LAT 25.0f
41#define START_LON -125.0f
45#define ERR(e) {printf("Error: %s\n", nc_strerror(e)); return 2;}
50 int ncid, lon_dimid, lat_dimid, pres_varid, temp_varid;
57 int lat_varid, lon_varid;
62 float pres_out[NLAT][NLON];
63 float temp_out[NLAT][NLON];
64 float lats[NLAT], lons[NLON];
68 char pres_units[] =
"hPa";
69 char temp_units[] =
"celsius";
80 for (lat = 0; lat < NLAT; lat++)
81 lats[lat] = START_LAT + 5.F*(
float)lat;
82 for (lon = 0; lon < NLON; lon++)
83 lons[lon] = START_LON + 5.F*(
float)lon;
85 for (lat = 0; lat < NLAT; lat++)
86 for (lon = 0; lon < NLON; lon++)
88 pres_out[lat][lon] = SAMPLE_PRESSURE + (float)(lon * NLAT + lat);
89 temp_out[lat][lon] = SAMPLE_TEMP + .25f * (float)(lon * NLAT + lat);
97 if ((retval =
nc_def_dim(ncid, LAT_NAME, NLAT, &lat_dimid)))
99 if ((retval =
nc_def_dim(ncid, LON_NAME, NLON, &lon_dimid)))
120 strlen(DEGREES_NORTH), DEGREES_NORTH)))
123 strlen(DEGREES_EAST), DEGREES_EAST)))
128 dimids[0] = lat_dimid;
129 dimids[1] = lon_dimid;
131 dimids, &pres_varid)))
134 dimids, &temp_varid)))
139 strlen(pres_units), pres_units)))
142 strlen(temp_units), temp_units)))
151 if ((retval = nc_put_var_float(ncid, lat_varid, &lats[0])))
153 if ((retval = nc_put_var_float(ncid, lon_varid, &lons[0])))
159 if ((retval = nc_put_var_float(ncid, pres_varid, &pres_out[0][0])))
161 if ((retval = nc_put_var_float(ncid, temp_varid, &temp_out[0][0])))
168 printf(
"*** SUCCESS writing example file sfc_pres_temp.nc!\n");
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.
EXTERNL int nc_create(const char *path, int cmode, int *ncidp)
Create a new netCDF file.
EXTERNL int nc_enddef(int ncid)
Leave define mode.
EXTERNL int nc_def_dim(int ncid, const char *name, size_t len, int *idp)
Define a new dimension.
EXTERNL int nc_def_var(int ncid, const char *name, nc_type xtype, int ndims, const int *dimidsp, int *varidp)
Define a new variable.
Main header file for the C API.
#define NC_CLOBBER
Destroy existing file.
#define NC_FLOAT
single precision floating point number