NetCDF-C++  4.3.1-developer
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  http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-tutorial
10 
11  Full documentation of the netCDF C++ API can be found at:
12  http://www.unidata.ucar.edu/software/netcdf/docs/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>
19 using namespace std;
20 using namespace netCDF;
21 using namespace netCDF::exceptions;
22 
23 // We are reading 2D data, a 6 x 12 grid.
24 static const int NX = 6;
25 static const int NY = 12;
26 
27 // Return this in event of a problem.
28 static const int NC_ERR = 2;
29 
30 int 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 }
C++ API for netCDF4.
Definition: ncAtt.h:9
Exception classes.
Definition: ncException.h:15
bool isNull() const
Returns true if this object variable is not defined.
Definition: ncVar.h:112
void getVar(void *dataValues) const
This is an overloaded member function, provided for convenience.
Definition: ncVar.cpp:1439
Base object is thrown if a netCDF exception is encountered.
Definition: ncException.h:24
Class represents a netCDF variable.
Definition: ncVar.h:33
Class represents a netCDF root group.
Definition: ncFile.h:18

Return to the Main Unidata NetCDF page.
Generated on Fri Nov 11 2016 15:28:29 for NetCDF-C++. NetCDF is a Unidata library.