1 | #include "inetcdf4.hpp" |
---|
2 | #include "netCdfInterface.hpp" |
---|
3 | #include "netCdf_cf_constant.hpp" |
---|
4 | |
---|
5 | #include <boost/algorithm/string.hpp> |
---|
6 | |
---|
7 | namespace xios |
---|
8 | { |
---|
9 | CINetCDF4::CINetCDF4(const StdString& filename, const MPI_Comm* comm /*= NULL*/, bool multifile /*= true*/, const StdString& timeCounterName /*= "time_counter"*/) |
---|
10 | { |
---|
11 | // Don't use parallel mode if there is only one process |
---|
12 | if (comm) |
---|
13 | { |
---|
14 | int commSize = 0; |
---|
15 | MPI_Comm_size(*comm, &commSize); |
---|
16 | if (commSize <= 1) |
---|
17 | comm = NULL; |
---|
18 | } |
---|
19 | mpi = comm && !multifile; |
---|
20 | |
---|
21 | // The file format will be detected automatically by NetCDF, it is safe to always set NC_MPIIO |
---|
22 | // even if Parallel NetCDF ends up being used. |
---|
23 | if (mpi) |
---|
24 | CNetCdfInterface::openPar(filename, NC_NOWRITE | NC_MPIIO, *comm, MPI_INFO_NULL, this->ncidp); |
---|
25 | else |
---|
26 | CNetCdfInterface::open(filename, NC_NOWRITE, this->ncidp); |
---|
27 | |
---|
28 | this->timeCounterName = timeCounterName; |
---|
29 | if (!CNetCdfInterface::isDimExisted(this->ncidp, this->timeCounterName)) this->timeCounterName=this->getUnlimitedDimensionName() ; |
---|
30 | |
---|
31 | } |
---|
32 | |
---|
33 | CINetCDF4::~CINetCDF4(void) |
---|
34 | { /* Nothing to do */ } |
---|
35 | |
---|
36 | //--------------------------------------------------------------- |
---|
37 | |
---|
38 | void CINetCDF4::close(void) |
---|
39 | { |
---|
40 | CNetCdfInterface::close(this->ncidp); |
---|
41 | } |
---|
42 | |
---|
43 | //--------------------------------------------------------------- |
---|
44 | |
---|
45 | int CINetCDF4::getGroup(const CVarPath* const path) |
---|
46 | { |
---|
47 | int retvalue = this->ncidp; |
---|
48 | if (path == NULL) return retvalue; |
---|
49 | CVarPath::const_iterator it = path->begin(), end = path->end(); |
---|
50 | |
---|
51 | for (; it != end; it++) |
---|
52 | { |
---|
53 | const StdString& groupid = *it; |
---|
54 | CNetCdfInterface::inqNcId(retvalue, groupid, retvalue); |
---|
55 | } |
---|
56 | |
---|
57 | return retvalue; |
---|
58 | } |
---|
59 | |
---|
60 | int CINetCDF4::getVariable(const StdString& varname, |
---|
61 | const CVarPath* const path) |
---|
62 | { |
---|
63 | int varid = 0; |
---|
64 | int grpid = this->getGroup(path); |
---|
65 | if (this->hasVariable(varname, path)) |
---|
66 | CNetCdfInterface::inqVarId(grpid, varname, varid); |
---|
67 | return varid; |
---|
68 | } |
---|
69 | |
---|
70 | int CINetCDF4::getDimension(const StdString& dimname, |
---|
71 | const CVarPath* const path) |
---|
72 | { |
---|
73 | int dimid = 0; |
---|
74 | int grpid = this->getGroup(path); |
---|
75 | CNetCdfInterface::inqDimId(grpid, dimname, dimid); |
---|
76 | return dimid; |
---|
77 | } |
---|
78 | |
---|
79 | std::pair<nc_type, StdSize> CINetCDF4::getAttribute(const StdString& attname, |
---|
80 | const StdString* const var, |
---|
81 | const CVarPath* const path) |
---|
82 | { |
---|
83 | std::pair<nc_type, StdSize> retvalue; |
---|
84 | int grpid = this->getGroup(path); |
---|
85 | int varid = (var != NULL && this->hasVariable(*var, path)) ? this->getVariable(*var, path) : NC_GLOBAL; |
---|
86 | CNetCdfInterface::inqAtt(grpid, varid, attname, retvalue.first, retvalue.second); |
---|
87 | return retvalue; |
---|
88 | } |
---|
89 | |
---|
90 | int CINetCDF4::getUnlimitedDimension(const CVarPath* const path) |
---|
91 | { |
---|
92 | int dimid = 0; |
---|
93 | int grpid = this->getGroup(path); |
---|
94 | CNetCdfInterface::inqUnLimDim(grpid, dimid); |
---|
95 | return dimid; |
---|
96 | } |
---|
97 | |
---|
98 | StdString CINetCDF4::getUnlimitedDimensionName(const CVarPath* const path) |
---|
99 | { |
---|
100 | int grpid = this->getGroup(path); |
---|
101 | int dimid = this->getUnlimitedDimension(path); |
---|
102 | |
---|
103 | StdString dimname; |
---|
104 | if (dimid != -1) |
---|
105 | CNetCdfInterface::inqDimName(grpid, dimid, dimname); |
---|
106 | return dimname; |
---|
107 | } |
---|
108 | |
---|
109 | //--------------------------------------------------------------- |
---|
110 | |
---|
111 | StdSize CINetCDF4::getNbVertex(const StdString& name, |
---|
112 | const CVarPath* const path) |
---|
113 | { |
---|
114 | |
---|
115 | if (this->isRectilinear(name, path) || |
---|
116 | this->isCurvilinear(name, path)) |
---|
117 | { |
---|
118 | if (this->is3Dim(name, path)) return 8; |
---|
119 | else return 4; |
---|
120 | } |
---|
121 | if (this->isUnstructured(name, path)) |
---|
122 | { |
---|
123 | StdString bound = this->getBoundsId |
---|
124 | (this->getCoordinatesIdList(name, path).back(), path); |
---|
125 | StdString dim = this->getDimensionsList(&bound, path).back(); |
---|
126 | return this->getDimensions(&bound, path)[dim]; |
---|
127 | } |
---|
128 | return size_t(-1); |
---|
129 | } |
---|
130 | |
---|
131 | //--------------------------------------------------------------- |
---|
132 | |
---|
133 | std::list<StdString> CINetCDF4::getGroups(const CVarPath* const path) |
---|
134 | { |
---|
135 | int nbgroup = 0, *groupid = NULL; |
---|
136 | int grpid = this->getGroup(path); |
---|
137 | std::list<StdString> retvalue; |
---|
138 | |
---|
139 | CNetCdfInterface::inqGrpIds(grpid, nbgroup, NULL); |
---|
140 | groupid = new int[nbgroup](); |
---|
141 | CNetCdfInterface::inqGrpIds(grpid, nbgroup, groupid); |
---|
142 | |
---|
143 | for (int i = 0; i < nbgroup; i++) |
---|
144 | { |
---|
145 | StdString fullGrpName; |
---|
146 | CNetCdfInterface::inqGrpFullName(groupid[i], fullGrpName); |
---|
147 | retvalue.push_back(fullGrpName); |
---|
148 | } |
---|
149 | |
---|
150 | delete [] groupid; |
---|
151 | return retvalue; |
---|
152 | } |
---|
153 | |
---|
154 | std::list<StdString> CINetCDF4::getVariables(const CVarPath* const path) |
---|
155 | { |
---|
156 | int nbvar = 0, *varid = NULL; |
---|
157 | int grpid = this->getGroup(path); |
---|
158 | std::list<StdString> retvalue; |
---|
159 | |
---|
160 | CNetCdfInterface::inqVarIds(grpid, nbvar, NULL); |
---|
161 | varid = new int[nbvar](); |
---|
162 | CNetCdfInterface::inqVarIds(grpid, nbvar, varid); |
---|
163 | |
---|
164 | for (int i = 0; i < nbvar; i++) |
---|
165 | { |
---|
166 | StdString varName; |
---|
167 | CNetCdfInterface::inqVarName(grpid, varid[i], varName); |
---|
168 | retvalue.push_back(varName); |
---|
169 | } |
---|
170 | |
---|
171 | delete [] varid; |
---|
172 | return retvalue; |
---|
173 | } |
---|
174 | |
---|
175 | StdSize CINetCDF4::getNbOfTimestep(const CVarPath* const path) |
---|
176 | { |
---|
177 | return this->getDimensions(NULL, path)[this->getUnlimitedDimensionName(path)]; |
---|
178 | } |
---|
179 | |
---|
180 | std::set<StdString> CINetCDF4::getBoundVariables(const CVarPath* const path) |
---|
181 | { |
---|
182 | std::set<StdString> retvalue; |
---|
183 | std::list<StdString> variables = this->getVariables(path); |
---|
184 | std::list<StdString>::const_iterator it = variables.begin(), end = variables.end(); |
---|
185 | for (; it != end; it++) |
---|
186 | { |
---|
187 | const StdString& var = *it; |
---|
188 | if (this->hasBounds(var, path)) |
---|
189 | retvalue.insert(retvalue.end(), this->getBoundsId(var, path)); |
---|
190 | } |
---|
191 | return retvalue; |
---|
192 | } |
---|
193 | |
---|
194 | std::set<StdString> CINetCDF4::getCoordVariables(const CVarPath* const path) |
---|
195 | { |
---|
196 | std::set<StdString> retvalue; |
---|
197 | std::list<StdString> variables = this->getVariables(path); |
---|
198 | std::list<StdString>::const_iterator it = variables.begin(), end = variables.end(); |
---|
199 | for (; it != end; it++) |
---|
200 | { |
---|
201 | const StdString& var = *it; |
---|
202 | std::list<StdString> coords = this->getCoordinatesIdList(var, path); |
---|
203 | std::list<StdString>::const_iterator it = coords.begin(), end = coords.end(); |
---|
204 | for (; it != end; it++) |
---|
205 | { |
---|
206 | const StdString& coord = *it; |
---|
207 | if (this->hasVariable(coord, path)) |
---|
208 | retvalue.insert(retvalue.end(), coord); |
---|
209 | } |
---|
210 | } |
---|
211 | return retvalue; |
---|
212 | } |
---|
213 | |
---|
214 | std::list<StdString> CINetCDF4::getDimensionsList(const StdString* const var, const CVarPath* const path) |
---|
215 | { |
---|
216 | int nbdim = 0, *dimid = NULL; |
---|
217 | int grpid = this->getGroup(path); |
---|
218 | int varid = (var != NULL && this->hasVariable(*var, path)) ? this->getVariable(*var, path) : NC_GLOBAL; |
---|
219 | std::list<StdString> retvalue; |
---|
220 | |
---|
221 | if (var != NULL && this->hasVariable(*var, path)) |
---|
222 | { |
---|
223 | CNetCdfInterface::inqVarNDims(grpid, varid, nbdim); |
---|
224 | dimid = new int[nbdim](); |
---|
225 | CNetCdfInterface::inqVarDimId(grpid, varid, dimid); |
---|
226 | } |
---|
227 | else |
---|
228 | { |
---|
229 | CNetCdfInterface::inqDimIds(grpid, nbdim, NULL, 1); |
---|
230 | dimid = new int[nbdim](); |
---|
231 | CNetCdfInterface::inqDimIds(grpid, nbdim, dimid, 1); |
---|
232 | } |
---|
233 | |
---|
234 | for (int i = 0; i < nbdim; i++) |
---|
235 | { |
---|
236 | std::string dimname; |
---|
237 | CNetCdfInterface::inqDimName(grpid, dimid[i], dimname); |
---|
238 | retvalue.push_back(dimname); |
---|
239 | } |
---|
240 | delete [] dimid; |
---|
241 | |
---|
242 | return retvalue; |
---|
243 | } |
---|
244 | |
---|
245 | std::map<StdString, StdSize> CINetCDF4::getDimensions(const StdString* const var, const CVarPath* const path) |
---|
246 | { |
---|
247 | int nbdim = 0, *dimid = NULL; |
---|
248 | int grpid = this->getGroup(path); |
---|
249 | int varid = (var != NULL && this->hasVariable(*var, path)) ? this->getVariable(*var, path) : NC_GLOBAL; |
---|
250 | std::map<StdString, StdSize> retvalue; |
---|
251 | |
---|
252 | if (var != NULL && this->hasVariable(*var, path)) |
---|
253 | { |
---|
254 | CNetCdfInterface::inqVarNDims(grpid, varid, nbdim); |
---|
255 | dimid = new int[nbdim](); |
---|
256 | CNetCdfInterface::inqVarDimId(grpid, varid, dimid); |
---|
257 | } |
---|
258 | else |
---|
259 | { |
---|
260 | CNetCdfInterface::inqDimIds(grpid, nbdim, NULL, 1); |
---|
261 | dimid = new int[nbdim](); |
---|
262 | CNetCdfInterface::inqDimIds(grpid, nbdim, dimid, 1); |
---|
263 | } |
---|
264 | |
---|
265 | for (int i = 0; i < nbdim; i++) |
---|
266 | { |
---|
267 | std::string dimname; |
---|
268 | CNetCdfInterface::inqDimName(grpid, dimid[i], dimname); |
---|
269 | StdSize size = 0; |
---|
270 | CNetCdfInterface::inqDimLen(grpid, dimid[i], size); |
---|
271 | |
---|
272 | retvalue.insert(retvalue.end(), std::make_pair(dimname, size)); |
---|
273 | } |
---|
274 | delete [] dimid; |
---|
275 | |
---|
276 | return retvalue; |
---|
277 | } |
---|
278 | |
---|
279 | std::list<StdString> CINetCDF4::getAttributes(const StdString* const var, const CVarPath* const path) |
---|
280 | { |
---|
281 | int nbatt = 0; |
---|
282 | std::list<StdString> retvalue; |
---|
283 | int grpid = this->getGroup(path); |
---|
284 | int varid = (var != NULL && this->hasVariable(*var, path)) ? this->getVariable(*var, path) : NC_GLOBAL; |
---|
285 | |
---|
286 | if (var != NULL && this->hasVariable(*var, path)) |
---|
287 | CNetCdfInterface::inqVarNAtts(grpid, varid, nbatt); |
---|
288 | else |
---|
289 | CNetCdfInterface::inqNAtts(grpid, nbatt); |
---|
290 | |
---|
291 | for (int i = 0; i < nbatt; i++) |
---|
292 | { |
---|
293 | StdString attname; |
---|
294 | CNetCdfInterface::inqAttName(grpid, varid, i, attname); |
---|
295 | retvalue.push_back(attname); |
---|
296 | } |
---|
297 | return retvalue; |
---|
298 | } |
---|
299 | |
---|
300 | int CINetCDF4::getAttributeId(const StdString& name, |
---|
301 | const StdString* const var, |
---|
302 | const CVarPath* const path) |
---|
303 | { |
---|
304 | int retvalue = 0; |
---|
305 | std::list<StdString> atts = this->getAttributes(var, path); |
---|
306 | std::list<StdString>::const_iterator it = atts.begin(), end = atts.end(); |
---|
307 | for (; it != end; it++) |
---|
308 | { |
---|
309 | const StdString& attname = *it; |
---|
310 | if (attname.compare(0, name.size(), name) == 0) |
---|
311 | return retvalue; |
---|
312 | retvalue++; |
---|
313 | } |
---|
314 | return -1; |
---|
315 | } |
---|
316 | |
---|
317 | //--------------------------------------------------------------- |
---|
318 | |
---|
319 | bool CINetCDF4::hasMissingValue(const StdString& name, |
---|
320 | const CVarPath* const path) |
---|
321 | { |
---|
322 | return (this->hasAttribute("missing_value", &name, path) || this->hasAttribute("_FillValue", &name, path)); |
---|
323 | } |
---|
324 | |
---|
325 | bool CINetCDF4::hasAttribute(const StdString& name, |
---|
326 | const StdString* const var , |
---|
327 | const CVarPath* const path) |
---|
328 | { |
---|
329 | std::list<StdString> atts = this->getAttributes(var, path); |
---|
330 | std::list<StdString>::const_iterator it = atts.begin(), end = atts.end(); |
---|
331 | for (; it != end; it++) |
---|
332 | { |
---|
333 | const StdString& attname = *it; |
---|
334 | if (attname.compare(0, name.size(), name) == 0) return true; |
---|
335 | } |
---|
336 | return false; |
---|
337 | } |
---|
338 | |
---|
339 | bool CINetCDF4::hasVariable(const StdString& name, |
---|
340 | const CVarPath* const path) |
---|
341 | { |
---|
342 | std::list<StdString> variables = this->getVariables(path); |
---|
343 | std::list<StdString>::const_iterator it = variables.begin(), end = variables.end(); |
---|
344 | for (; it != end; it++) |
---|
345 | { |
---|
346 | const StdString& varname = *it; |
---|
347 | if ((varname.compare(0, name.size(), name) == 0) && (0 != name.size())) return true; |
---|
348 | } |
---|
349 | return false; |
---|
350 | } |
---|
351 | |
---|
352 | bool CINetCDF4::hasCoordinates(const StdString& name, |
---|
353 | const CVarPath* const path) |
---|
354 | { |
---|
355 | return this->hasAttribute(CCFKeywords::XIOS_CF_coordinates, &name, path); |
---|
356 | } |
---|
357 | |
---|
358 | bool CINetCDF4::hasBounds(const StdString& name, |
---|
359 | const CVarPath* const path) |
---|
360 | { |
---|
361 | return this->hasAttribute(CCFKeywords::XIOS_CF_bounds, &name, path); |
---|
362 | } |
---|
363 | |
---|
364 | bool CINetCDF4::hasTemporalDim(const CVarPath* const path) |
---|
365 | { |
---|
366 | std::list<StdString> dims = this->getDimensionsList(NULL, path); |
---|
367 | return (std::find(dims.begin(), dims.end(), timeCounterName) != dims.end()); |
---|
368 | } |
---|
369 | |
---|
370 | //--------------------------------------------------------------- |
---|
371 | |
---|
372 | template <class T> |
---|
373 | std::vector<T> CINetCDF4::getAttributeValue(const StdString& name, |
---|
374 | const StdString* const var, |
---|
375 | const CVarPath* const path) |
---|
376 | { |
---|
377 | int grpid = this->getGroup(path); |
---|
378 | int varid = (var != NULL && this->hasVariable(*var, path)) ? this->getVariable(*var, path) : NC_GLOBAL; |
---|
379 | std::pair<nc_type , StdSize> attinfos = this->getAttribute(name, var, path); |
---|
380 | std::vector<T> retvalue(attinfos.second); |
---|
381 | nc_type type = CNetCdfInterface::getNcType<T>(); |
---|
382 | if (attinfos.first != type) |
---|
383 | ERROR("CINetCDF4::getAttributeValue<T>(name, var, path)", |
---|
384 | << "[ name : " << name |
---|
385 | << ", type requested :" << attinfos.first |
---|
386 | << ", type stored : " << type << "]" |
---|
387 | << " Invalid type !"); |
---|
388 | CNetCdfInterface::getAttType(grpid, varid, name.c_str(), &retvalue[0]); |
---|
389 | return retvalue; |
---|
390 | } |
---|
391 | |
---|
392 | template std::vector<double> CINetCDF4::getAttributeValue(const StdString& name, |
---|
393 | const StdString* const var, |
---|
394 | const CVarPath* const path); |
---|
395 | template std::vector<float> CINetCDF4::getAttributeValue(const StdString& name, |
---|
396 | const StdString* const var, |
---|
397 | const CVarPath* const path); |
---|
398 | template std::vector<int> CINetCDF4::getAttributeValue(const StdString& name, |
---|
399 | const StdString* const var, |
---|
400 | const CVarPath* const path); |
---|
401 | template std::vector<char> CINetCDF4::getAttributeValue(const StdString& name, |
---|
402 | const StdString* const var, |
---|
403 | const CVarPath* const path); |
---|
404 | |
---|
405 | StdString CINetCDF4::getAttributeValue(const StdString& name, |
---|
406 | const StdString* const var, |
---|
407 | const CVarPath* const path) |
---|
408 | { |
---|
409 | std::vector<char> data = this->getAttributeValue<char>(name, var, path); |
---|
410 | |
---|
411 | return StdString(data.begin(), data.end()); |
---|
412 | } |
---|
413 | |
---|
414 | template <class T> |
---|
415 | T CINetCDF4::getMissingValue(const StdString& name, const CVarPath* const path) |
---|
416 | { |
---|
417 | if (this->hasAttribute("missing_value", &name, path)) |
---|
418 | return this->getAttributeValue<T>("missing_value", &name, path)[0]; |
---|
419 | if (this->hasAttribute("_FillValue", &name, path)) |
---|
420 | return this->getAttributeValue<T>("_FillValue", &name, path)[0]; |
---|
421 | return 0; |
---|
422 | } |
---|
423 | |
---|
424 | template double CINetCDF4::getMissingValue(const StdString& name, const CVarPath* const path); |
---|
425 | template float CINetCDF4::getMissingValue(const StdString& name, const CVarPath* const path); |
---|
426 | template int CINetCDF4::getMissingValue(const StdString& name, const CVarPath* const path); |
---|
427 | template char CINetCDF4::getMissingValue(const StdString& name, const CVarPath* const path); |
---|
428 | |
---|
429 | //--------------------------------------------------------------- |
---|
430 | |
---|
431 | std::list<StdString> CINetCDF4::getCoordinatesIdList(const StdString& name, const CVarPath* const path) |
---|
432 | { |
---|
433 | std::list<StdString> retvalue; |
---|
434 | StdString value = this->getCoordinatesId(name, path); |
---|
435 | |
---|
436 | boost::split(retvalue, value, boost::is_any_of(" ")); |
---|
437 | |
---|
438 | std::list<StdString>::iterator it = retvalue.begin(), end = retvalue.end(); |
---|
439 | for (; it != end; it++) |
---|
440 | { |
---|
441 | StdString& coord = *it; |
---|
442 | coord.assign(coord.data()); |
---|
443 | } |
---|
444 | return retvalue; |
---|
445 | } |
---|
446 | |
---|
447 | StdString CINetCDF4::getCoordinatesId(const StdString& name, const CVarPath* const path) |
---|
448 | { |
---|
449 | StdString retvalue; |
---|
450 | if (this->hasAttribute(CCFKeywords::XIOS_CF_coordinates, &name, path)) |
---|
451 | { |
---|
452 | return this->getAttributeValue(CCFKeywords::XIOS_CF_coordinates, &name, path); |
---|
453 | } |
---|
454 | else |
---|
455 | { |
---|
456 | std::list<StdString> dims = this->getDimensionsList(&name, path); |
---|
457 | std::list<StdString>::const_iterator it = dims.begin(), end = dims.end(); |
---|
458 | for (; it != end; it++) |
---|
459 | { |
---|
460 | const StdString& value = *it; |
---|
461 | retvalue.append(value).push_back(' '); |
---|
462 | } |
---|
463 | retvalue.erase(retvalue.end() - 1) ; |
---|
464 | } |
---|
465 | |
---|
466 | return retvalue; |
---|
467 | } |
---|
468 | |
---|
469 | StdString CINetCDF4::getBoundsId(const StdString& name, |
---|
470 | const CVarPath* const path) |
---|
471 | { |
---|
472 | StdString retvalue; |
---|
473 | if (this->hasAttribute(CCFKeywords::XIOS_CF_bounds, &name, path)) |
---|
474 | retvalue = this->getAttributeValue(CCFKeywords::XIOS_CF_bounds, &name, path); |
---|
475 | return retvalue; |
---|
476 | } |
---|
477 | |
---|
478 | //--------------------------------------------------------------- |
---|
479 | |
---|
480 | bool CINetCDF4::isBound(const StdString& name, |
---|
481 | const CVarPath* const path) |
---|
482 | { |
---|
483 | std::set<StdString> bounds = this->getBoundVariables(path); |
---|
484 | return (bounds.find(name) != bounds.end()); |
---|
485 | } |
---|
486 | |
---|
487 | bool CINetCDF4::isCoordinate(const StdString& name, |
---|
488 | const CVarPath* const path) |
---|
489 | { |
---|
490 | std::set<StdString> coords = this->getCoordVariables(path); |
---|
491 | return (coords.find(name) != coords.end()); |
---|
492 | } |
---|
493 | |
---|
494 | bool CINetCDF4::isRectilinear(const StdString& name, const CVarPath* const path) |
---|
495 | { |
---|
496 | std::list<StdString> varCoords = this->getCoordinatesIdList(name, path); |
---|
497 | std::list<StdString> varDims = this->getDimensionsList(&name, path); |
---|
498 | std::list<StdString>::const_iterator it = varCoords.begin(), end = varCoords.end(); |
---|
499 | std::set<StdString> varDims1D; |
---|
500 | |
---|
501 | // Firstly, loop over coordinate list |
---|
502 | for (; it != end; it++) |
---|
503 | { |
---|
504 | const StdString& coord = *it; |
---|
505 | if (this->hasVariable(coord, path) && !this->isTemporal(coord, path) ) |
---|
506 | { |
---|
507 | std::map<StdString, StdSize> coordDims = this->getDimensions(&coord, path); |
---|
508 | for (std::map<StdString, StdSize>::const_iterator itTmp = coordDims.begin(); itTmp != coordDims.end(); itTmp++) |
---|
509 | { |
---|
510 | varDims.remove(itTmp->first); |
---|
511 | } |
---|
512 | if (this->isLonOrLat(coord, path) && coordDims.size() == 1) |
---|
513 | { |
---|
514 | varDims1D.insert(coordDims.begin()->first); |
---|
515 | continue; |
---|
516 | } |
---|
517 | } |
---|
518 | } |
---|
519 | // Secondly, loop over remaining dimensions |
---|
520 | for (it= varDims.begin(); it != varDims.end(); it++) |
---|
521 | { |
---|
522 | const StdString& coord = *it; |
---|
523 | std::map<StdString, StdSize> coordDims = this->getDimensions(&coord, path); |
---|
524 | if (this->hasVariable(coord, path) && !this->isTemporal(coord, path) ) |
---|
525 | { |
---|
526 | if (this->isLonOrLat(coord, path) && coordDims.size() == 1) |
---|
527 | { |
---|
528 | varDims1D.insert(coordDims.begin()->first); |
---|
529 | continue; |
---|
530 | } |
---|
531 | } |
---|
532 | } |
---|
533 | |
---|
534 | return (varDims1D.size() == 2); |
---|
535 | } |
---|
536 | |
---|
537 | bool CINetCDF4::isCurvilinear(const StdString& name, const CVarPath* const path) |
---|
538 | { |
---|
539 | if (this->isRectilinear(name, path) || !this->hasCoordinates(name, path)) |
---|
540 | return false; |
---|
541 | |
---|
542 | bool isCurVi = true; |
---|
543 | unsigned int nbLonLat = 0; |
---|
544 | std::list<StdString> coords = this->getCoordinatesIdList(name, path); |
---|
545 | std::list<StdString>::const_iterator it = coords.begin(), end = coords.end(); |
---|
546 | for (; it != end; it++) |
---|
547 | { |
---|
548 | const StdString& coord = *it; |
---|
549 | if (this->hasVariable(coord, path) && !this->isTemporal(coord, path) && this->isLonOrLat(coord, path)) |
---|
550 | { |
---|
551 | std::map<StdString, StdSize> dimvar = this->getDimensions(&coord, path); |
---|
552 | if (2 == dimvar.size()) ++nbLonLat; |
---|
553 | } |
---|
554 | } |
---|
555 | if (2 != nbLonLat) isCurVi = false; |
---|
556 | |
---|
557 | return isCurVi; |
---|
558 | } |
---|
559 | |
---|
560 | bool CINetCDF4::isUnstructured(const StdString& name, const CVarPath* const path) |
---|
561 | { |
---|
562 | if (this->isRectilinear(name, path) || |
---|
563 | this->isCurvilinear(name, path) || |
---|
564 | !this->hasCoordinates(name, path)) |
---|
565 | return false; |
---|
566 | else return true ; |
---|
567 | |
---|
568 | // check this part above |
---|
569 | StdString dimname = this->getDimensionsList(&name, path).back(); |
---|
570 | |
---|
571 | std::list<StdString> coords = this->getCoordinatesIdList(name, path); |
---|
572 | std::list<StdString>::const_iterator it = coords.begin(), end = coords.end(); |
---|
573 | for (; it != end; it++) |
---|
574 | { |
---|
575 | const StdString& coord = *it; |
---|
576 | if (this->hasVariable(coord, path) && !this->isTemporal(coord, path) && this->isLonOrLat(coord, path)) |
---|
577 | { |
---|
578 | std::map<StdString, StdSize> dimvar = this->getDimensions(&coord, path); |
---|
579 | if ((dimvar.size() == 1) && |
---|
580 | (dimvar.find(dimname) != dimvar.end())) |
---|
581 | continue; |
---|
582 | else |
---|
583 | return false; |
---|
584 | } |
---|
585 | } |
---|
586 | |
---|
587 | return true; |
---|
588 | } |
---|
589 | |
---|
590 | bool CINetCDF4::isUnknown(const StdString& name, const CVarPath* const path) |
---|
591 | { |
---|
592 | return !(this->isRectilinear(name, path) || this->isCurvilinear(name, path) || this->isUnstructured(name, path)); |
---|
593 | } |
---|
594 | |
---|
595 | bool CINetCDF4::isTemporal(const StdString& name, const CVarPath* const path) |
---|
596 | { |
---|
597 | std::list<StdString> dims = this->getDimensionsList(&name, path); |
---|
598 | return (std::find(dims.begin(), dims.end(), timeCounterName) != dims.end()); |
---|
599 | } |
---|
600 | |
---|
601 | bool CINetCDF4::is3Dim(const StdString& name, const CVarPath* const path) |
---|
602 | { |
---|
603 | int i = 0; |
---|
604 | std::list<StdString> coords = this->getCoordinatesIdList(name, path); |
---|
605 | std::list<StdString>::const_iterator it = coords.begin(), end = coords.end(); |
---|
606 | for (; it != end; it++) |
---|
607 | { |
---|
608 | const StdString& coord = *it; |
---|
609 | if (this->hasVariable(coord, path)) |
---|
610 | { |
---|
611 | if (this->isTemporal(coord, path)) |
---|
612 | continue; |
---|
613 | i++; |
---|
614 | } |
---|
615 | else |
---|
616 | { |
---|
617 | StdString unlimitedDimName = this->getUnlimitedDimensionName(); |
---|
618 | if (coord.compare(0, unlimitedDimName.size(), unlimitedDimName) == 0) |
---|
619 | continue; |
---|
620 | i++; |
---|
621 | } |
---|
622 | } |
---|
623 | return (i == 3); |
---|
624 | } |
---|
625 | |
---|
626 | bool CINetCDF4::isCellGrid(const StdString& name, const CVarPath* const path) |
---|
627 | { |
---|
628 | if (this->isCoordinate(name, path)) |
---|
629 | { |
---|
630 | return this->hasBounds(name, path); |
---|
631 | } |
---|
632 | else |
---|
633 | { |
---|
634 | std::list<StdString> coords = this->getCoordinatesIdList(name, path); |
---|
635 | std::list<StdString>::const_iterator it = coords.begin(), end = coords.end(); |
---|
636 | for (; it != end; it++) |
---|
637 | { |
---|
638 | const StdString& coord = *it; |
---|
639 | if (this->hasVariable(coord, path)) |
---|
640 | { |
---|
641 | if (this->isTemporal(coord, path)) |
---|
642 | continue; |
---|
643 | if (this->isCellGrid(coord, path)) |
---|
644 | continue; |
---|
645 | return false; |
---|
646 | } |
---|
647 | else |
---|
648 | { |
---|
649 | StdString unlimitedDimName = this->getUnlimitedDimensionName(); |
---|
650 | if (coord.compare(0, unlimitedDimName.size(), unlimitedDimName) == 0) |
---|
651 | continue; |
---|
652 | return false; |
---|
653 | } |
---|
654 | } |
---|
655 | } |
---|
656 | |
---|
657 | return true; |
---|
658 | } |
---|
659 | |
---|
660 | //--------------------------------------------------------------- |
---|
661 | |
---|
662 | std::list<StdString> CINetCDF4::getDataVariables(bool _is3D, bool _isRecti, |
---|
663 | bool _isCurvi, bool _isUnstr, |
---|
664 | bool _isCellData, bool _isTemporal, |
---|
665 | const CVarPath* const path) |
---|
666 | { |
---|
667 | std::list<StdString> retvalue; |
---|
668 | std::list<StdString> allvars = this->getVariables(path); |
---|
669 | std::set<StdString> allcoords = this->getCoordVariables(path); |
---|
670 | |
---|
671 | std::list<StdString>::const_iterator it = allvars.begin(), end = allvars.end(); |
---|
672 | for (; it != end; it++) |
---|
673 | { |
---|
674 | const StdString& var = *it; |
---|
675 | if (this->isCoordinate(var, path)) continue; |
---|
676 | |
---|
677 | if (!_isRecti && this->isRectilinear(var, path)) continue; |
---|
678 | if (!_isCurvi && this->isCurvilinear(var, path)) continue; |
---|
679 | if (!_isUnstr && this->isUnstructured(var, path)) continue; |
---|
680 | |
---|
681 | if (!_isTemporal && this->isTemporal(var, path)) continue; |
---|
682 | if (!_is3D && this->is3Dim(var, path)) continue; |
---|
683 | if (!_isCellData && this->isCellGrid(var, path)) continue; |
---|
684 | |
---|
685 | if (this->isUnknown(var, path)) continue; |
---|
686 | |
---|
687 | retvalue.push_back(var); |
---|
688 | } |
---|
689 | return retvalue; |
---|
690 | } |
---|
691 | |
---|
692 | //--------------------------------------------------------------- |
---|
693 | |
---|
694 | void CINetCDF4::getDataInfo(const StdString& var, const CVarPath* const path, StdSize record, |
---|
695 | std::vector<StdSize>& sstart, std::vector<StdSize>& scount, StdSize& array_size, |
---|
696 | const std::vector<StdSize>* start /*= NULL*/, const std::vector<StdSize>* count /*= NULL*/) |
---|
697 | { |
---|
698 | std::list<StdString> dimlist = this->getDimensionsList(&var, path); |
---|
699 | std::map<StdString, StdSize> dimmap = this->getDimensions(&var, path); |
---|
700 | std::list<StdString>::iterator it = dimlist.begin(); |
---|
701 | if (this->isTemporal(var, path)) |
---|
702 | { |
---|
703 | if (record != UNLIMITED_DIM) |
---|
704 | sstart.push_back(record); |
---|
705 | else |
---|
706 | sstart.push_back(0); |
---|
707 | scount.push_back(1); |
---|
708 | it++; |
---|
709 | } |
---|
710 | for (int i = 0; it != dimlist.end(); it++, i++) |
---|
711 | { |
---|
712 | if (start && count) |
---|
713 | { |
---|
714 | sstart.push_back((*start)[i]); |
---|
715 | scount.push_back((*count)[i]); |
---|
716 | array_size *= (*count)[i]; |
---|
717 | } |
---|
718 | else |
---|
719 | { |
---|
720 | sstart.push_back(0); |
---|
721 | scount.push_back(dimmap[*it]); |
---|
722 | array_size *= dimmap[*it]; |
---|
723 | } |
---|
724 | } |
---|
725 | } |
---|
726 | |
---|
727 | template <class T> |
---|
728 | void CINetCDF4::getData(CArray<T, 1>& data, const StdString& var, |
---|
729 | const CVarPath* const path, StdSize record) |
---|
730 | { |
---|
731 | std::vector<StdSize> start, count; |
---|
732 | int grpid = this->getGroup(path); |
---|
733 | int varid = this->getVariable(var, path); |
---|
734 | StdSize array_size = 1; |
---|
735 | this->getDataInfo(var, path, record, start, count, array_size); |
---|
736 | data.resize(array_size); |
---|
737 | CNetCdfInterface::getVaraType(grpid, varid, &start[0], &count[0], data.dataFirst()); |
---|
738 | } |
---|
739 | |
---|
740 | template <> |
---|
741 | void CINetCDF4::getData(CArray<int, 1>& data, const StdString& var, |
---|
742 | const CVarPath* const path, StdSize record); |
---|
743 | template <> |
---|
744 | void CINetCDF4::getData(CArray<double, 1>& data, const StdString& var, |
---|
745 | const CVarPath* const path, StdSize record); |
---|
746 | template <> |
---|
747 | void CINetCDF4::getData(CArray<float, 1>& data, const StdString& var, |
---|
748 | const CVarPath* const path, StdSize record); |
---|
749 | |
---|
750 | //--------------------------------------------------------------- |
---|
751 | |
---|
752 | StdString CINetCDF4::getLonCoordName(const StdString& varname, |
---|
753 | const CVarPath* const path) |
---|
754 | { |
---|
755 | StdString lonName; |
---|
756 | std::list<StdString>::const_iterator itbList, itList, iteList; |
---|
757 | std::list<StdString> clist = this->getCoordinatesIdList(varname, path); |
---|
758 | itbList = clist.begin(); iteList = clist.end(); |
---|
759 | for (itList = itbList; itList != iteList; ++itList) |
---|
760 | { |
---|
761 | if (this->hasAttribute(CCFKeywords::XIOS_CF_units, &(*itList), path)) |
---|
762 | { |
---|
763 | StdString unit = this->getAttributeValue(CCFKeywords::XIOS_CF_units, &(*itList), path); |
---|
764 | if (CCFConvention::XIOS_CF_Longitude_units.end() != CCFConvention::XIOS_CF_Longitude_units.find(unit)) |
---|
765 | { |
---|
766 | lonName = *itList; |
---|
767 | return lonName; |
---|
768 | } |
---|
769 | } |
---|
770 | } |
---|
771 | return lonName; |
---|
772 | } |
---|
773 | |
---|
774 | StdString CINetCDF4::getLatCoordName(const StdString& varname, |
---|
775 | const CVarPath* const path) |
---|
776 | { |
---|
777 | StdString latName; |
---|
778 | std::list<StdString>::const_iterator itbList, itList, iteList; |
---|
779 | std::list<StdString> clist = this->getCoordinatesIdList(varname, path); |
---|
780 | itbList = clist.begin(); iteList = clist.end(); |
---|
781 | for (itList = itbList; itList != iteList; ++itList) |
---|
782 | { |
---|
783 | if (this->hasAttribute(CCFKeywords::XIOS_CF_units, &(*itList), path)) |
---|
784 | { |
---|
785 | StdString unit = this->getAttributeValue(CCFKeywords::XIOS_CF_units, &(*itList), path); |
---|
786 | if (CCFConvention::XIOS_CF_Latitude_units.end() != CCFConvention::XIOS_CF_Latitude_units.find(unit)) |
---|
787 | { |
---|
788 | latName = *itList; |
---|
789 | return latName; |
---|
790 | } |
---|
791 | } |
---|
792 | } |
---|
793 | return latName; |
---|
794 | } |
---|
795 | |
---|
796 | StdString CINetCDF4::getVertCoordName(const StdString& varname, |
---|
797 | const CVarPath* const path) |
---|
798 | { |
---|
799 | if (!this->is3Dim(varname, path)) return ""; |
---|
800 | std::list<StdString> clist = this->getCoordinatesIdList(varname, path); |
---|
801 | if (this->hasCoordinates(varname, path)) |
---|
802 | return *(++(++clist.begin())); |
---|
803 | else |
---|
804 | return *(++(++clist.rbegin())); |
---|
805 | } |
---|
806 | |
---|
807 | bool CINetCDF4::isLonOrLat(const StdString& varname, const CVarPath* const path) |
---|
808 | { |
---|
809 | if (this->hasAttribute(CCFKeywords::XIOS_CF_units, &varname, path)) |
---|
810 | { |
---|
811 | StdString unit = this->getAttributeValue(CCFKeywords::XIOS_CF_units, &varname, path); |
---|
812 | return (CCFConvention::XIOS_CF_Latitude_units.end() != CCFConvention::XIOS_CF_Latitude_units.find(unit) |
---|
813 | || CCFConvention::XIOS_CF_Longitude_units.end() != CCFConvention::XIOS_CF_Longitude_units.find(unit)); |
---|
814 | } |
---|
815 | } |
---|
816 | |
---|
817 | } // namespace xios |
---|