-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathoutput_netcdf.cpp
More file actions
234 lines (175 loc) · 7.03 KB
/
Copy pathoutput_netcdf.cpp
File metadata and controls
234 lines (175 loc) · 7.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Copyright 2020, the Aether Development Team (see doc/dev_team.md for members)
// Full license can be found in License.md
// Added by A. Ridley - Apr. 25, 2023
#include "aether.h"
#ifdef NETCDF
/* ---------------------------------------------------------------------
Output and Input methods for netcdf files
-------------------------------------------------------------------- */
#include <netcdf>
using namespace netCDF;
using namespace netCDF::exceptions;
//----------------------------------------------------------------------
// This is at the top so it doesn't have to go in the include file!
// Output a given variable to the netCDF file. The netCDF system
// doesn't work with Armadillo cubes, so we have to transform the cube
// to a C-array, and then output
// ----------------------------------------------------------------------
void output_netcdf_3d(std::vector<size_t> count_start,
std::vector<size_t> count_end,
arma_cube value,
NcVar variable) {
// Get the size of the cube:
int64_t nX = value.n_rows;
int64_t nY = value.n_cols;
int64_t nZ = value.n_slices;
int64_t iX, iY, iZ, iTotal, index;
iTotal = nX * nY * nZ;
// Create a temporary c-array to use to output the variable
float *tmp_s3gc = static_cast<float*>(malloc(iTotal * sizeof(float)));
// Move the data from the cube to the c-array
for (iX = 0; iX < nX; iX++) {
for (iY = 0; iY < nY; iY++) {
for (iZ = 0; iZ < nZ; iZ++) {
index = iX * nY * nZ + iY * nZ + iZ;
tmp_s3gc[index] = value(iX, iY, iZ);
}
}
}
// Output the data to the netCDF file
variable.putVar(count_start, count_end, tmp_s3gc);
// delete the c-array
free(tmp_s3gc);
}
// -----------------------------------------------------------------------------
// read contents of a netcdf file into an output container
// -----------------------------------------------------------------------------
bool OutputContainer::read_container_netcdf() {
bool didWork = true;
std::string whole_filename = directory + "/" + filename + ".nc";
std::string UNITS = "units";
try {
std::cout << "Reading NetCDF file into container : "
<< whole_filename << "\n";
NcFile ncdf_file_in(whole_filename, NcFile::read);
std::multimap<std::string, NcVar> variables_in_file;
std::multimap<std::string, NcVar>::iterator iter;
// Declare a string to store the variable name.
std::string variable_name;
std::string variable_unit;
// Declare a netCDF variable attribute object.
NcVarAtt attribute;
// Declare a vector of netCDF dimension objects.
std::vector <NcDim> dimensions;
std::string dimension_name;
std::vector<int> nPts(3);
// Assign the variables in the netCDF file to the multimap.
variables_in_file = ncdf_file_in.getVars();
// Use the iterator to loop through the multimap.
for (iter = variables_in_file.begin();
iter != variables_in_file.end(); iter++) {
variable_name = iter->first;
if (variable_name.compare("time") != 0) {
attribute = iter->second.getAtt("units");
attribute.getValues(variable_unit);
dimensions = iter->second.getDims();
int nDims = dimensions.size();
int iTotal = 1;
// For this specific app, we only want the 3d arrays.
if (nDims == 3) {
for (int iDim = 0; iDim < nDims; iDim++) {
dimension_name = dimensions[iDim].getName();
nPts[iDim] = dimensions[iDim].getSize();
iTotal = iTotal * nPts[iDim];
}
float *variable_array = new float[iTotal];
iter->second.getVar(variable_array);
arma_cube value_scgc;
value_scgc.set_size(nPts[0], nPts[1], nPts[2]);
int64_t index;
// NetCDF ordering.
for (int64_t iX = 0; iX < nPts[0]; iX++) {
for (int64_t iY = 0; iY < nPts[1]; iY++) {
for (int64_t iZ = 0; iZ < nPts[2]; iZ++) {
index = iX * nPts[1] * nPts[2] + iY * nPts[2] + iZ;
value_scgc(iX, iY, iZ) = variable_array[index];
}
}
}
// Store in the container:
store_variable(variable_name, variable_unit, value_scgc);
}
} else {
double *time_array = new double[1], t;
iter->second.getVar(time_array);
t = time_array[0];
set_time(t);
}
}
ncdf_file_in.close();
} catch (...) {
std::cout << "Error reading netcdf file : "
<< whole_filename << "\n";
didWork = false;
}
return didWork;
}
// -----------------------------------------------------------------------------
// dump the contents of the container out into a binary file
// -----------------------------------------------------------------------------
bool OutputContainer::write_container_netcdf() {
bool didWork = true;
std::string whole_filename = directory + "/" + filename + ".nc";
std::string UNITS = "units";
std::string LONG_NAME = "long_name";
try {
NcFile ncdf_file(whole_filename, NcFile::replace);
// Add dimensions:
NcDim xDim = ncdf_file.addDim("x", elements[0].value.n_rows);
NcDim yDim = ncdf_file.addDim("y", elements[0].value.n_cols);
NcDim zDim = ncdf_file.addDim("z", elements[0].value.n_slices);
NcDim tDim = ncdf_file.addDim("time", 1);
// Define the netCDF variables for the 3D data.
// First create a vector of dimensions:
std::vector<NcDim> dimVector{xDim, yDim, zDim};
std::vector<size_t> startp{ 0, 0, 0};
std::vector<size_t> countp{elements[0].value.n_rows,
elements[0].value.n_cols,
elements[0].value.n_slices};
// Output time:
NcVar timeVar = ncdf_file.addVar("time", ncDouble, tDim);
double time_array[1];
time_array[0] = time_int_to_real(itime);
timeVar.putVar(time_array);
// Output all objects in the container:
std::vector<NcVar> Var;
int64_t nVars = elements.size();
for (int64_t iVar = 0; iVar < nVars; iVar++) {
Var.push_back(ncdf_file.addVar(elements[iVar].cName, ncFloat, dimVector));
Var[iVar].putAtt(UNITS, elements[iVar].cUnit);
if (elements[iVar].cLongName.length() > 0)
Var[iVar].putAtt(LONG_NAME, elements[iVar].cLongName);
output_netcdf_3d(startp, countp, elements[iVar].value, Var[iVar]);
}
ncdf_file.close();
} catch (...) {
report.error("Error writing netcdf container file : " + whole_filename);
didWork = false;
}
return didWork;
}
#else
/* ---------------------------------------------------------------------
These are dummy functions for compiling without netcdf libraries.
-------------------------------------------------------------------- */
bool OutputContainer::read_container_netcdf() {
bool didWork = false;
std::cout << "read_container_netcdf is not working!\n";
return didWork;
}
bool OutputContainer::write_container_netcdf() {
bool didWork = false;
report.error("write_container_netcdf is not working!");
return didWork;
}
#endif