NetCDF-C++
4.3.2
Toggle main menu visibility
Loading...
Searching...
No Matches
simple_xy_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 a very simple example which writes a 2D array of
6
sample data. To handle this in netCDF we create two shared
7
dimensions, "x" and "y", and a netCDF variable, called "data".
8
9
This example is part of the netCDF tutorial:
10
https://docs.unidata.ucar.edu/netcdf-c/current/tutorial_8dox.html
11
12
Full documentation of the netCDF C++ API can be found at:
13
https://docs.unidata.ucar.edu/netcdf-cxx
14
15
$Id: simple_xy_wr.cpp,v 1.5 2010/02/11 22:36:43 russ Exp $
16
*/
17
18
#include <iostream>
19
#include <netcdf>
20
#include <vector>
21
using namespace
std;
22
using namespace
netCDF
;
23
using namespace
netCDF::exceptions
;
24
25
// We are writing 2D data, a 6 x 12 grid.
26
static
const
int
NX = 6;
27
static
const
int
NY = 12;
28
29
// Return this in event of a problem.
30
static
const
int
NC_ERR = 2;
31
32
33
34
int
main()
35
{
36
// This is the data array we will write. It will just be filled
37
// with a progression of numbers for this example.
38
int
dataOut[NX][NY];
39
40
// Create some pretend data. If this wasn't an example program, we
41
// would have some real data to write, for example, model output.
42
for
(
int
i = 0; i < NX; i++)
43
for
(
int
j = 0; j < NY; j++)
44
dataOut[i][j] = i * NY + j;
45
46
// The default behavior of the C++ API is to throw an exception i
47
// an error occurs. A try catch block is necessary.
48
49
try
50
{
51
// Create the file. The Replace parameter tells netCDF to overwrite
52
// this file, if it already exists.
53
NcFile
dataFile(
"simple_xy.nc"
,
NcFile::replace
);
54
55
// Create netCDF dimensions
56
NcDim
xDim = dataFile.addDim(
"x"
, NX);
57
NcDim
yDim = dataFile.addDim(
"y"
, NY);
58
59
// Define the variable. The type of the variable in this case is
60
// ncInt (32-bit integer).
61
vector<NcDim> dims;
62
dims.push_back(xDim);
63
dims.push_back(yDim);
64
NcVar
data = dataFile.addVar(
"data"
,
ncInt
, dims);
65
66
// Write the data to the file. Although netCDF supports
67
// reading and writing subsets of data, in this case we write all
68
// the data in one operation.
69
data.
putVar
(dataOut);
70
71
// The file will be automatically close when the NcFile object goes
72
// out of scope. This frees up any internal netCDF resources
73
// associated with the file, and flushes any buffers.
74
75
//cout << "*** SUCCESS writing example file simple_xy.nc!" << endl;
76
return
0;
77
}
78
catch
(
NcException
& e)
79
{
80
cout<<
"FAILURE**************************\n"
;
81
cout << e.what() << endl;
82
return
NC_ERR;
83
}
84
}
netCDF::NcDim
Class represents a netCDF dimension.
Definition
ncDim.h:13
netCDF::NcFile
Class represents a netCDF root group.
Definition
ncFile.h:19
netCDF::NcFile::replace
@ replace
Create new file, even if already exists.
Definition
ncFile.h:26
netCDF::NcVar
Class represents a netCDF variable.
Definition
ncVar.h:34
netCDF::NcVar::putVar
void putVar(const void *dataValues) const
This is an overloaded member function, provided for convenience.
Definition
ncVar.cpp:830
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
netCDF::ncInt
NcInt ncInt
A global instance of the NcInt class within the netCDF namespace.
Definition
ncInt.cpp:7
Return to the Main Unidata NetCDF page.
Generated on
for NetCDF-C++. NetCDF is a
Unidata
library.