NetCDF-C++ 4.3.1
pres_temp_4D_wr.cpp
1/* This is part of the netCDF package.
2 Copyright 2006 University Corporation for Atmospheric Research/Unidata.
3 See COPYRIGHT file for conditions of use.
4
5 This is an example program which writes some 4D pressure and
6 temperatures. This example demonstrates the netCDF C++ API.
7
8 This is part of the netCDF tutorial:
9 https://docs.unidata.ucar.edu/netcdf-c/current/tutorial_8dox.html
10
11 Full documentation of the netCDF C++ API can be found at:
12 https://docs.unidata.ucar.edu/netcdf-cxx
13
14 $Id: pres_temp_4D_wr.cpp,v 1.6 2010/02/11 22:36:42 russ Exp $
15*/
16
17#include <netcdf>
18#include <iostream>
19#include <string>
20using namespace std;
21using namespace netCDF;
22using namespace netCDF::exceptions;
23
24// This is the name of the data file we will create.
25#define FILE_NAME "pres_temp_4D.nc"
26
27// We are writing 4D data, a 2 x 6 x 12 lvl-lat-lon grid, with 2
28// timesteps of data.
29#define NDIMS 4
30#define NLVL 2
31#define NLAT 6
32#define NLON 12
33#define NREC 2
34
35// Names of things.
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
43// These are used to construct some example data.
44#define SAMPLE_PRESSURE 900
45#define SAMPLE_TEMP 9.0
46#define START_LAT 25.0
47#define START_LON -125.0
48
49
50string UNITS = "units";
51string DEGREES_EAST = "degrees_east";
52string DEGREES_NORTH = "degrees_north";
53
54
55// For the units attributes.
56string PRES_UNITS = "hPa";
57string TEMP_UNITS = "celsius";
58string LAT_UNITS = "degrees_north";
59string LON_UNITS = "degrees_east";
60
61// Return this code to the OS in case of failure.
62#define NC_ERR 2
63
64int main()
65{
66 // We will write latitude and longitude fields.
67 float lats[NLAT],lons[NLON];
68
69 // Program variables to hold the data we will write out. We will
70 // only need enough space to hold one timestep of data; one record.
71 float pres_out[NLVL][NLAT][NLON];
72 float temp_out[NLVL][NLAT][NLON];
73
74 int i=0; //used in the data generation loop
75
76 // create some pretend data. If this wasn't an example program, we
77 // would have some real data to write for example, model output.
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;
82
83 for (int lvl = 0; lvl < NLVL; lvl++)
84 for (int lat = 0; lat < NLAT; lat++)
85 for (int lon = 0; lon < NLON; lon++)
86 {
87 pres_out[lvl][lat][lon] =(float) (SAMPLE_PRESSURE + i);
88 temp_out[lvl][lat][lon] = (float)(SAMPLE_TEMP + i++);
89 }
90
91 try
92 {
93
94
95 // Create the file.
96 NcFile test(FILE_NAME, NcFile::replace);
97
98 // Define the dimensions. NetCDF will hand back an ncDim object for
99 // each.
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); //adds an unlimited dimension
104
105 // Define the coordinate variables.
106 NcVar latVar = test.addVar(LAT_NAME, ncFloat, latDim);
107 NcVar lonVar = test.addVar(LON_NAME, ncFloat, lonDim);
108
109 // Define units attributes for coordinate vars. This attaches a
110 // text attribute to each of the coordinate variables, containing
111 // the units.
112 latVar.putAtt(UNITS, DEGREES_NORTH);
113 lonVar.putAtt(UNITS, DEGREES_EAST);
114
115 // Define the netCDF variables for the pressure and temperature
116 // data.
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);
124
125 // Define units attributes for coordinate vars. This attaches a
126 // text attribute to each of the coordinate variables, containing
127 // the units.
128 pressVar.putAtt(UNITS, PRES_UNITS);
129 tempVar.putAtt(UNITS, TEMP_UNITS);
130
131 // Write the coordinate variable data to the file.
132 latVar.putVar(lats);
133 lonVar.putVar(lons);
134
135 // Write the pretend data. This will write our surface pressure and
136 // surface temperature data. The arrays only hold one timestep
137 // worth of data. We will just rewrite the same data for each
138 // timestep. In a real application, the data would change between
139 // timesteps.
140 vector<size_t> startp,countp;
141 startp.push_back(0);
142 startp.push_back(0);
143 startp.push_back(0);
144 startp.push_back(0);
145 countp.push_back(1);
146 countp.push_back(NLVL);
147 countp.push_back(NLAT);
148 countp.push_back(NLON);
149 for (size_t rec = 0; rec < NREC; rec++)
150 {
151 startp[0]=rec;
152 pressVar.putVar(startp,countp,pres_out);
153 tempVar.putVar(startp,countp,temp_out);
154 }
155
156 // The file is automatically closed by the destructor. This frees
157 // up any internal netCDF resources associated with the file, and
158 // flushes any buffers.
159
160 //cout << "*** SUCCESS writing example file " << FILE_NAME << "!" << endl;
161 return 0;
162 }
163 catch(NcException& e)
164 {
165 e.what();
166 return NC_ERR;
167 }
168}
Class represents a netCDF dimension.
Definition: ncDim.h:13
Class represents a netCDF root group.
Definition: ncFile.h:19
Class represents a netCDF variable.
Definition: ncVar.h:34
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...
void putVar(const void *dataValues) const
This is an overloaded member function, provided for convenience.
Definition: ncVar.cpp:843
Base object is thrown if a netCDF exception is encountered.
Definition: ncException.h:24
Exception classes.
Definition: ncException.h:16
C++ API for netCDF4.
Definition: ncAtt.h:10

Return to the Main Unidata NetCDF page.
Generated on Wed Nov 10 2021 15:25:08 for NetCDF-C++. NetCDF is a Unidata library.