NetCDF-C++
4.3.2
Toggle main menu visibility
Loading...
Searching...
No Matches
simple_xy_wr_formats.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_formats.cpp,v 1.1 2010/04/09 02:15:05 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
int
create_file(
string
filename,
NcFile::FileFormat
format) {
33
// This is the data array we will write. It will just be filled
34
// with a progression of numbers for this example.
35
int
dataOut[NX][NY];
36
37
// Create some pretend data. If this wasn't an example program, we
38
// would have some real data to write, for example, model output.
39
for
(
int
i = 0; i < NX; i++)
40
for
(
int
j = 0; j < NY; j++)
41
dataOut[i][j] = i * NY + j;
42
// The default behavior of the C++ API is to throw an exception i
43
// an error occurs. A try catch block is necessary.
44
try
45
{
46
// Create the file. The Replace parameter tells netCDF to overwrite
47
// this file, if it already exists. The classic parameter specifies
48
// that the file ceated should be in classic format, rather than the
49
// default netCDF-4 format used by the cxx4 interface.
50
NcFile
dataFile(filename,
NcFile::replace
, format);
51
52
// Create netCDF dimensions
53
NcDim
xDim = dataFile.addDim(
"x"
, NX);
54
NcDim
yDim = dataFile.addDim(
"y"
, NY);
55
56
// Define the variable. The type of the variable in this case is
57
// ncInt (32-bit integer).
58
vector<NcDim> dims;
59
dims.push_back(xDim);
60
dims.push_back(yDim);
61
NcVar
data = dataFile.addVar(
"data"
,
ncInt
, dims);
62
63
// In the classic model, must explicitly leave define mode
64
// before writing data. Need a method that calls nc_enddef().
65
if
(format !=
NcFile::nc4
)
66
dataFile.enddef();
67
68
// Write the data to the file. Although netCDF supports
69
// reading and writing subsets of data, in this case we write all
70
// the data in one operation.
71
data.
putVar
(dataOut);
72
73
// The file will be automatically closed when the NcFile object goes
74
// out of scope. This frees up any internal netCDF resources
75
// associated with the file, and flushes any buffers.
76
77
//cout << "*** SUCCESS writing example file simple_xy.nc!" << endl;
78
return
0;
79
}
80
catch
(
NcException
& e)
81
{
82
cout<<
"FAILURE**************************\n"
;
83
cout << e.what() << endl;
84
return
NC_ERR;
85
}
86
}
87
88
int
main()
89
{
90
int
ret;
91
if
((ret = create_file(
"simple_xy_nc4.nc"
,
NcFile::nc4
))) {
92
return
ret;
93
}
94
cout <<
"*** SUCCESS creating nc4 file"
<< endl;
95
96
if
((ret = create_file(
"simple_xy_nc4classic.nc"
,
NcFile::nc4classic
))) {
97
return
ret;
98
}
99
cout <<
"*** SUCCESS creating nc4classic file"
<< endl;
100
101
if
((ret = create_file(
"simple_xy_classic.nc"
,
NcFile::classic
))) {
102
return
ret;
103
}
104
cout <<
"*** SUCCESS creating classic file"
<< endl;
105
106
if
((ret = create_file(
"simple_xy_classic64.nc"
,
NcFile::classic64
))) {
107
return
ret;
108
}
109
cout <<
"*** SUCCESS creating classic64 file"
<< endl;
110
111
return
0;
112
}
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::NcFile::FileFormat
FileFormat
Definition
ncFile.h:31
netCDF::NcFile::classic
@ classic
Classic format, classic data model.
Definition
ncFile.h:32
netCDF::NcFile::classic64
@ classic64
64-bit offset format, classic data model
Definition
ncFile.h:33
netCDF::NcFile::nc4
@ nc4
(default) netCDF-4/HDF5 format, enhanced data model
Definition
ncFile.h:34
netCDF::NcFile::nc4classic
@ nc4classic
netCDF-4/HDF5 format, classic data model
Definition
ncFile.h:35
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.