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 demonstrates the netCDF C++ API. This is part of the
10    netCDF tutorial:
11    http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-tutorial
12 
13    Full documentation of the netCDF C++ API can be found at:
14    http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-cxx
15 
16    $Id: sfc_pres_temp_wr.cpp,v 1.6 2010/02/11 22:36:42 russ Exp $
17 */
18 
19 #include <iostream>
20 #include <netcdf>
21 using namespace std;
22 using namespace netCDF;
23 using namespace netCDF::exceptions;
24 
25 // This is the name of the data file we will create.
26 #define FILE_NAME "sfc_pres_temp.nc"
27 
28 // We are writing 2D data, a 6 x 12 lat-lon grid. We will need two
29 // netCDF dimensions.
30 static const int NDIMS = 2;
31 static const int NLAT = 6;
32 static const int NLON = 12;
33 
34 // Names of things.
35 string  PRES_NAME = "pressure";
36 string TEMP_NAME = "temperature";
37 string  UNITS = "units";
38 string  DEGREES_EAST = "degrees_east";
39 string DEGREES_NORTH = "degrees_north";
40 string LAT_NAME = "latitude";
41 string LON_NAME ="longitude";
42 
43 // These are used to construct some example data.
44 #define SAMPLE_PRESSURE 900
45 #define SAMPLE_TEMP     9.0
46 #define START_LAT       25.0
47 #define START_LON       -125.0
48 
49 // Return this to OS if there is a failure.
50 #define NC_ERR 2
51 
main(void)52 int main(void)
53 {
54    // We will write surface temperature and pressure fields.
55    float presOut[NLAT][NLON];
56    float tempOut[NLAT][NLON];
57    float lats[NLAT];
58    float lons[NLON];
59 
60    // In addition to the latitude and longitude dimensions, we will
61    // also create latitude and longitude netCDF variables which will
62    // hold the actual latitudes and longitudes. Since they hold data
63    // about the coordinate system, the netCDF term for these is:
64    // "coordinate variables."
65    for(int lat = 0;lat < NLAT; lat++)
66       lats[lat] = START_LAT + 5.*lat;
67 
68    for(int lon = 0; lon < NLON; lon++)
69       lons[lon] = START_LON + 5.*lon;
70 
71    // Create some pretend data. If this wasn't an example program, we
72    // would have some real data to write, for example, model
73    // output.
74    for (int lat = 0; lat < NLAT; lat++)
75       for(int lon = 0;lon < NLON; lon++)
76       {
77 	 presOut[lat][lon] = SAMPLE_PRESSURE + (lon * NLAT + lat);
78 	 tempOut[lat][lon] = SAMPLE_TEMP + .25 * (lon * NLAT +lat);
79       }
80 
81    try
82    {
83 
84       // Create the file. The Replace parameter tells netCDF to overwrite
85       // this file, if it already exists.
86       NcFile sfc(FILE_NAME, NcFile::replace);
87 
88       // Define the dimensions. NetCDF will hand back an ncDim object for
89       // each.
90       NcDim latDim = sfc.addDim(LAT_NAME, NLAT);
91       NcDim lonDim = sfc.addDim(LON_NAME, NLON);
92 
93       // Define coordinate netCDF variables. They will hold the
94       // coordinate information, that is, the latitudes and
95       // longitudes. An pointer to a NcVar object is returned for
96       // each.
97       NcVar latVar = sfc.addVar(LAT_NAME, ncFloat, latDim);//creates variable
98       NcVar lonVar = sfc.addVar(LON_NAME, ncFloat, lonDim);
99 
100       // Write the coordinate variable data. This will put the latitudes
101       // and longitudes of our data grid into the netCDF file.
102       latVar.putVar(lats);
103       lonVar.putVar(lons);
104 
105       // Define units attributes for coordinate vars. This attaches a
106       // text attribute to each of the coordinate variables, containing
107       // the units. Note that we are not writing a trailing NULL, just
108       // "units", because the reading program may be fortran which does
109       // not use null-terminated strings. In general it is up to the
110       // reading C program to ensure that it puts null-terminators on
111       // strings where necessary.
112       lonVar.putAtt(UNITS,DEGREES_EAST);
113       latVar.putAtt(UNITS,DEGREES_NORTH);
114 
115       // Define the netCDF data variables.
116       vector<NcDim> dims;
117       dims.push_back(latDim);
118       dims.push_back(lonDim);
119       NcVar presVar = sfc.addVar(PRES_NAME, ncFloat, dims);
120       NcVar tempVar = sfc.addVar(TEMP_NAME, ncFloat, dims);
121 
122       // Define units attributes for vars.
123       presVar.putAtt(UNITS,"hPa");
124       tempVar.putAtt(UNITS,"celsius");
125 
126       // Write the pretend data. This will write our surface pressure and
127       // surface temperature data. The arrays of data are the same size
128       // as the netCDF variables we have defined.
129       presVar.putVar(presOut);
130       tempVar.putVar(tempOut);
131 
132       // The file is automatically closed by the destructor. This frees
133       // up any internal netCDF resources associated with the file, and
134       // flushes any buffers.
135 
136       //cout << "*** SUCCESS writing example file " << FILE_NAME << "!" << endl;
137       return 0;
138    }
139    catch(NcException& e)
140      {
141       e.what();
142       return NC_ERR;
143    }
144 }
145