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