NetCDF-C++
4.3.2
Toggle main menu visibility
Loading...
Searching...
No Matches
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>
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
cout<<
"FAILURE**************************\n"
;
58
cout << e.what() << endl;
59
return
NC_ERR;
60
}
61
}
netCDF::NcFile
Class represents a netCDF root group.
Definition
ncFile.h:19
netCDF::NcFile::read
@ read
File exists, open read-only.
Definition
ncFile.h:24
netCDF::NcVar
Class represents a netCDF variable.
Definition
ncVar.h:34
netCDF::NcVar::getVar
void getVar(void *dataValues) const
This is an overloaded member function, provided for convenience.
Definition
ncVar.cpp:1389
netCDF::NcVar::isNull
bool isNull() const
Returns true if this object variable is not defined.
Definition
ncVar.h:126
netCDF::exceptions::NcException
Base object is thrown if a netCDF exception is encountered.
Definition
ncException.h:24
netCDF::exceptions
Exception classes.
Definition
ncException.h:16
netCDF
C++ API for netCDF4.
Definition
ncAtt.h:10
Return to the Main Unidata NetCDF page.
Generated on
for NetCDF-C++. NetCDF is a
Unidata
library.