NetCDF-C++  4.3.1-developer
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  http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-tutorial
11 
12  Full documentation of the netCDF C++ API can be found at:
13  http://www.unidata.ucar.edu/software/netcdf/docs/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  {e.what();
82  return NC_ERR;
83  }
84 }
85 
86 int main()
87 {
88  int ret;
89  if(ret = create_file("simple_xy_nc4.nc", NcFile::nc4))
90  return ret;
91  cout << "*** SUCCESS creating nc4 file" << endl;
92 
93  if(ret = create_file("simple_xy_nc4classic.nc", NcFile::nc4classic))
94  return ret;
95  cout << "*** SUCCESS creating nc4classic file" << endl;
96 
97  if(ret = create_file("simple_xy_classic.nc", NcFile::classic))
98  return ret;
99  cout << "*** SUCCESS creating classic file" << endl;
100 
101  if(ret = create_file("simple_xy_classic64.nc", NcFile::classic64))
102  return ret;
103  cout << "*** SUCCESS creating classic64 file" << endl;
104 
105  return 0;
106 }
C++ API for netCDF4.
Definition: ncAtt.h:9
Exception classes.
Definition: ncException.h:15
Class represents a netCDF dimension.
Definition: ncDim.h:13
Base object is thrown if a netCDF exception is encountered.
Definition: ncException.h:24
void putVar(const void *dataValues) const
This is an overloaded member function, provided for convenience.
Definition: ncVar.cpp:815
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.