25 #define FILE_NAME "pres_temp_4D.nc" 36 #define LVL_NAME "level" 37 #define LAT_NAME "latitude" 38 #define LON_NAME "longitude" 39 #define REC_NAME "time" 40 #define PRES_NAME "pressure" 41 #define TEMP_NAME "temperature" 42 #define MAX_ATT_LEN 80 44 #define SAMPLE_PRESSURE 900 45 #define SAMPLE_TEMP 9.0 46 #define START_LAT 25.0 47 #define START_LON -125.0 50 string UNITS =
"units";
51 string DEGREES_EAST =
"degrees_east";
52 string DEGREES_NORTH =
"degrees_north";
56 string PRES_UNITS =
"hPa";
57 string TEMP_UNITS =
"celsius";
58 string LAT_UNITS =
"degrees_north";
59 string LON_UNITS =
"degrees_east";
67 float lats[NLAT],lons[NLON];
71 float pres_out[NLVL][NLAT][NLON];
72 float temp_out[NLVL][NLAT][NLON];
78 for (
int lat = 0; lat < NLAT; lat++)
79 lats[lat] = START_LAT + 5. * lat;
80 for (
int lon = 0; lon < NLON; lon++)
81 lons[lon] = START_LON + 5. * lon;
83 for (
int lvl = 0; lvl < NLVL; lvl++)
84 for (
int lat = 0; lat < NLAT; lat++)
85 for (
int lon = 0; lon < NLON; lon++)
87 pres_out[lvl][lat][lon] =(float) (SAMPLE_PRESSURE + i);
88 temp_out[lvl][lat][lon] = (float)(SAMPLE_TEMP + i++);
96 NcFile test(FILE_NAME, NcFile::replace);
100 NcDim lvlDim = test.addDim(LVL_NAME, NLVL);
101 NcDim latDim = test.addDim(LAT_NAME, NLAT);
102 NcDim lonDim = test.addDim(LON_NAME, NLON);
103 NcDim recDim = test.addDim(REC_NAME);
106 NcVar latVar = test.addVar(LAT_NAME, ncFloat, latDim);
107 NcVar lonVar = test.addVar(LON_NAME, ncFloat, lonDim);
112 latVar.
putAtt(UNITS, DEGREES_NORTH);
113 lonVar.
putAtt(UNITS, DEGREES_EAST);
117 vector<NcDim> dimVector;
118 dimVector.push_back(recDim);
119 dimVector.push_back(lvlDim);
120 dimVector.push_back(latDim);
121 dimVector.push_back(lonDim);
122 NcVar pressVar = test.addVar(PRES_NAME, ncFloat, dimVector);
123 NcVar tempVar = test.addVar(TEMP_NAME, ncFloat, dimVector);
128 pressVar.
putAtt(UNITS, PRES_UNITS);
129 tempVar.
putAtt(UNITS, TEMP_UNITS);
140 vector<size_t> startp,countp;
146 countp.push_back(NLVL);
147 countp.push_back(NLAT);
148 countp.push_back(NLON);
149 for (
size_t rec = 0; rec < NREC; rec++)
152 pressVar.
putVar(startp,countp,pres_out);
153 tempVar.
putVar(startp,countp,temp_out);
NcVarAtt putAtt(const std::string &name, size_t len, const char **dataValues) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Class represents a netCDF dimension.
Base object is thrown if a netCDF exception is encountered.
void putVar(const void *dataValues) const
This is an overloaded member function, provided for convenience.
Class represents a netCDF variable.
Class represents a netCDF root group.