NetCDF-C++ 4.3.2
Loading...
Searching...
No Matches
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.
29constexpr int NDIMS = 4;
30constexpr int NLVL = 2;
31constexpr int NLAT = 6;
32constexpr int NLON = 12;
33constexpr int NREC = 2;
34
35// Names of things.
36constexpr auto LVL_NAME = "level";
37constexpr auto LAT_NAME = "latitude";
38constexpr auto LON_NAME = "longitude";
39constexpr auto REC_NAME = "time";
40constexpr auto PRES_NAME = "pressure";
41constexpr auto TEMP_NAME = "temperature";
42constexpr auto MAX_ATT_LEN = 80;
43// These are used to construct some example data.
44constexpr float SAMPLE_PRESSURE = 900;
45constexpr float SAMPLE_TEMP = 9.0;
46constexpr float START_LAT = 25.0;
47constexpr float 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];
68 float lons[NLON];
69
70 // Program variables to hold the data we will write out. We will
71 // only need enough space to hold one timestep of data; one record.
72 float pres_out[NLVL][NLAT][NLON];
73 float temp_out[NLVL][NLAT][NLON];
74
75 int i=0; //used in the data generation loop
76
77 // create some pretend data. If this wasn't an example program, we
78 // would have some real data to write for example, model output.
79 for (int lat = 0; lat < NLAT; lat++) {
80 lats[lat] = START_LAT + 5.f * static_cast<float>(lat);
81 }
82 for (int lon = 0; lon < NLON; lon++) {
83 lons[lon] = START_LON + 5.f * static_cast<float>(lon);
84 }
85
86 for (int lvl = 0; lvl < NLVL; lvl++)
87 for (int lat = 0; lat < NLAT; lat++)
88 for (int lon = 0; lon < NLON; lon++)
89 {
90 pres_out[lvl][lat][lon] = SAMPLE_PRESSURE + static_cast<float>(i);
91 temp_out[lvl][lat][lon] = SAMPLE_TEMP + static_cast<float>(i++);
92 }
93
94 try
95 {
96
97
98 // Create the file.
99 NcFile test(FILE_NAME, NcFile::replace);
100
101 // Define the dimensions. NetCDF will hand back an ncDim object for
102 // each.
103 NcDim lvlDim = test.addDim(LVL_NAME, NLVL);
104 NcDim latDim = test.addDim(LAT_NAME, NLAT);
105 NcDim lonDim = test.addDim(LON_NAME, NLON);
106 NcDim recDim = test.addDim(REC_NAME); //adds an unlimited dimension
107
108 // Define the coordinate variables.
109 NcVar latVar = test.addVar(LAT_NAME, ncFloat, latDim);
110 NcVar lonVar = test.addVar(LON_NAME, ncFloat, lonDim);
111
112 // Define units attributes for coordinate vars. This attaches a
113 // text attribute to each of the coordinate variables, containing
114 // the units.
115 latVar.putAtt(UNITS, DEGREES_NORTH);
116 lonVar.putAtt(UNITS, DEGREES_EAST);
117
118 // Define the netCDF variables for the pressure and temperature
119 // data.
120 vector<NcDim> dimVector;
121 dimVector.push_back(recDim);
122 dimVector.push_back(lvlDim);
123 dimVector.push_back(latDim);
124 dimVector.push_back(lonDim);
125 NcVar pressVar = test.addVar(PRES_NAME, ncFloat, dimVector);
126 NcVar tempVar = test.addVar(TEMP_NAME, ncFloat, dimVector);
127
128 // Define units attributes for coordinate vars. This attaches a
129 // text attribute to each of the coordinate variables, containing
130 // the units.
131 pressVar.putAtt(UNITS, PRES_UNITS);
132 tempVar.putAtt(UNITS, TEMP_UNITS);
133
134 // Write the coordinate variable data to the file.
135 latVar.putVar(lats);
136 lonVar.putVar(lons);
137
138 // Write the pretend data. This will write our surface pressure and
139 // surface temperature data. The arrays only hold one timestep
140 // worth of data. We will just rewrite the same data for each
141 // timestep. In a real application, the data would change between
142 // timesteps.
143 vector<size_t> startp,countp;
144 startp.push_back(0);
145 startp.push_back(0);
146 startp.push_back(0);
147 startp.push_back(0);
148 countp.push_back(1);
149 countp.push_back(NLVL);
150 countp.push_back(NLAT);
151 countp.push_back(NLON);
152 for (size_t rec = 0; rec < NREC; rec++)
153 {
154 startp[0]=rec;
155 pressVar.putVar(startp,countp,pres_out);
156 tempVar.putVar(startp,countp,temp_out);
157 }
158
159 // The file is automatically closed by the destructor. This frees
160 // up any internal netCDF resources associated with the file, and
161 // flushes any buffers.
162
163 //cout << "*** SUCCESS writing example file " << FILE_NAME << "!" << endl;
164 return 0;
165 }
166 catch(NcException& e)
167 {
168 cout<<"FAILURE**************************\n";
169 cout << e.what() << endl;
170 return NC_ERR;
171 }
172}
Class represents a netCDF dimension.
Definition ncDim.h:13
Class represents a netCDF root group.
Definition ncFile.h:19
@ replace
Create new file, even if already exists.
Definition ncFile.h:26
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:830
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
NcFloat ncFloat
A global instance of the NcFloat class within the netCDF namespace.
Definition ncFloat.cpp:7

Return to the Main Unidata NetCDF page.
Generated on for NetCDF-C++. NetCDF is a Unidata library.