NetCDF-C++ 4.3.1
simple_xy_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 a very simple example which reads a 2D array of
6 sample data produced by simple_xy_wr.cpp.
7
8 This example 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: simple_xy_rd.cpp,v 1.5 2010/02/11 22:36:43 russ Exp $
15*/
16
17#include <iostream>
18#include <netcdf>
19using namespace std;
20using namespace netCDF;
21using namespace netCDF::exceptions;
22
23// We are reading 2D data, a 6 x 12 grid.
24static const int NX = 6;
25static const int NY = 12;
26
27// Return this in event of a problem.
28static const int NC_ERR = 2;
29
30int main()
31{
32 try
33 {
34 // This is the array we will read.
35 int dataIn[NX][NY];
36
37 // Open the file for read access
38 NcFile dataFile("simple_xy.nc", NcFile::read);
39
40 // Retrieve the variable named "data"
41 NcVar data=dataFile.getVar("data");
42 if(data.isNull()) return NC_ERR;
43 data.getVar(dataIn);
44
45 // Check the values.
46 for (int i = 0; i < NX; i++)
47 for (int j = 0; j < NY; j++)
48 if (dataIn[i][j] != i * NY + j)
49 return NC_ERR;
50
51 // The netCDF file is automatically closed by the NcFile destructor
52 //cout << "*** SUCCESS reading example file simple_xy.nc!" << endl;
53
54 return 0;
55 }catch(NcException& e)
56 {
57 e.what();
58 cout<<"FAILURE*************************************"<<endl;
59 return NC_ERR;
60 }
61}
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.