NetCDF-C++ 4.3.1
pres_temp_4D_rd.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 which reads some 4D pressure and temperature
6 values. The data file read by this program is produced by the
7 companion program pres_temp_4D_wr.cpp. It is intended to illustrate
8 the use of the netCDF C++ API.
9
10 This program is part of the netCDF tutorial:
11 https://docs.unidata.ucar.edu/netcdf-c/current/tutorial_8dox.html
12
13 Full documentation of the netCDF C++ API can be found at:
14 https://docs.unidata.ucar.edu/netcdf-cxx
15
16 $Id: pres_temp_4D_rd.cpp,v 1.5 2010/02/11 22:36:42 russ Exp $
17*/
18
19#include <iostream>
20#include <netcdf>
21using namespace std;
22using namespace netCDF;
23using namespace netCDF::exceptions;
24
25// We are writing 4D data, a 2 x 6 x 12 lvl-lat-lon grid, with 2
26// timesteps of data.
27static const int NLVL = 2;
28static const int NLAT = 6;
29static const int NLON = 12;
30static const int NREC = 2;
31
32// These are used to construct some example data.
33static const float SAMPLE_PRESSURE = 900.0;
34static const float SAMPLE_TEMP = 9.0;
35static const float START_LAT = 25.0;
36static const float START_LON = -125.0;
37
38
39// Return this code to the OS in case of failure.
40static const int NC_ERR = 2;
41
42int main()
43{
44 // These arrays will store the latitude and longitude values.
45 float lats[NLAT], lons[NLON];
46
47 // These arrays will hold the data we will read in. We will only
48 // need enough space to hold one timestep of data; one record.
49 float pres_in[NLVL][NLAT][NLON];
50 float temp_in[NLVL][NLAT][NLON];
51
52 try
53 {
54 // Open the file.
55 NcFile dataFile("pres_temp_4D.nc", NcFile::read);
56
57 // Get the latitude and longitude variables and read data.
58 NcVar latVar, lonVar;
59 latVar = dataFile.getVar("latitude");
60 if(latVar.isNull()) return NC_ERR;
61 lonVar = dataFile.getVar("longitude");
62 if(lonVar.isNull()) return NC_ERR;
63 lonVar.getVar(lons);
64 latVar.getVar(lats);
65
66 // Check the coordinate variable data.
67 for (int lat = 0; lat < NLAT; lat++)
68 if (lats[lat] != START_LAT + 5. * lat)
69 return NC_ERR;
70
71 for (int lon = 0; lon < NLON; lon++)
72 if (lons[lon] != START_LON + 5. * lon)
73 return NC_ERR;
74
75 // Get the pressure and temperature variables and read data one time step at a time
76 NcVar presVar, tempVar;
77 presVar = dataFile.getVar("pressure");
78 if(presVar.isNull()) return NC_ERR;
79 tempVar = dataFile.getVar("temperature");
80 if(tempVar.isNull()) return NC_ERR;
81
82 vector<size_t> startp,countp;
83 startp.push_back(0);
84 startp.push_back(0);
85 startp.push_back(0);
86 startp.push_back(0);
87 countp.push_back(1);
88 countp.push_back(NLVL);
89 countp.push_back(NLAT);
90 countp.push_back(NLON);
91 for (size_t rec = 0; rec < NREC; rec++)
92 {
93 // Read the data one record at a time.
94 startp[0]=rec;
95 presVar.getVar(startp,countp,pres_in);
96 tempVar.getVar(startp,countp,temp_in);
97
98 int i=0; //used in the data generation loop
99 for (int lvl = 0; lvl < NLVL; lvl++)
100 for (int lat = 0; lat < NLAT; lat++)
101 for (int lon = 0; lon < NLON; lon++)
102 {
103 if(pres_in[lvl][lat][lon] != (float) (SAMPLE_PRESSURE + i)) return NC_ERR;
104 if(temp_in[lvl][lat][lon] != (float)(SAMPLE_TEMP + i++)) return NC_ERR;
105 }
106
107 } // next record
108
109 // The file is automatically closed by the destructor. This frees
110 // up any internal netCDF resources associated with the file, and
111 // flushes any buffers.
112
113 // cout << "*** SUCCESS reading example file pres_temp_4D.nc!" << endl;
114 return 0;
115
116 }
117 catch(NcException& e)
118 {
119 e.what();
120 cout<<"FAILURE**************************"<<endl;
121 return NC_ERR;
122 }
123
124}
Class represents a netCDF root group.
Definition: ncFile.h:19
Class represents a netCDF variable.
Definition: ncVar.h:34
void getVar(void *dataValues) const
This is an overloaded member function, provided for convenience.
Definition: ncVar.cpp:1467
bool isNull() const
Returns true if this object variable is not defined.
Definition: ncVar.h:126
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.