NetCDF-C++ 4.3.2
Loading...
Searching...
No Matches
ncGroup.cpp
1#include "ncGroup.h"
2#include "ncVar.h"
3#include "ncDim.h"
4#include "ncVlenType.h"
5#include "ncCompoundType.h"
6#include "ncOpaqueType.h"
7#include "ncGroupAtt.h"
8#include "ncByte.h"
9#include "ncUbyte.h"
10#include "ncChar.h"
11#include "ncShort.h"
12#include "ncUshort.h"
13#include "ncInt.h"
14#include "ncUint.h"
15#include "ncInt64.h"
16#include "ncUint64.h"
17#include "ncFloat.h"
18#include "ncDouble.h"
19#include "ncString.h"
20#include <cstddef>
21#include <ncException.h>
22#include <tuple>
23#include <utility>
24#include "ncCheck.h"
25using namespace std;
26using namespace netCDF::exceptions;
27
28namespace netCDF {
29 // Global comparator operator ==============
30 // comparator operator
31 bool operator<(const NcGroup& lhs,const NcGroup& rhs)
32 {
33 return false;
34 }
35
36 // comparator operator
37 bool operator>(const NcGroup& lhs,const NcGroup& rhs)
38 {
39 return true;
40 }
41}
42
43using namespace netCDF;
44
46
50
51// Constructor generates a null object.
53 nullObject(true),
54 myId(-1)
55{}
56
57
58// constructor
59NcGroup::NcGroup(const int groupId) :
60 nullObject(false),
61 myId(groupId)
62{ }
63
64// assignment operator
66{
68 myId = rhs.myId;
69 return *this;
70}
71
72// The copy constructor.
75 myId(rhs.myId)
76{}
77
78
79// equivalence operator
80bool NcGroup::operator==(const NcGroup & rhs) const
81{
82 if(nullObject)
83 return nullObject == rhs.nullObject;
84 else
85 return myId == rhs.myId;
86}
87
88// != operator
89bool NcGroup::operator!=(const NcGroup & rhs) const
90{
91 return !(*this == rhs);
92}
93
94
95// /////////////
96// NcGroup-related methods
97// /////////////
98
99// Get the group name.
100string NcGroup::getName(bool fullName) const {
101 if(isNull()) throw NcNullGrp("Attempt to invoke NcGroup::getName on a Null group",__FILE__,__LINE__);
102 string groupName;
103 if(fullName){
104 // return full name of group with foward "/" separarating sub-groups.
105 size_t lenp;
106 ncCheck(nc_inq_grpname_len(myId,&lenp),__FILE__,__LINE__);
107 char* charName= new char[lenp+1];
108 ncCheck(nc_inq_grpname_full(myId,&lenp,charName),__FILE__,__LINE__);
109 groupName = charName;
110 delete[] charName;
111 }
112 else {
113 // return the (local) name of this group.
114 char charName[NC_MAX_NAME+1];
115 ncCheck(nc_inq_grpname(myId,charName),__FILE__,__LINE__);
116 groupName = charName;
117 }
118 return groupName;
119}
120
121// returns true if this is the root group.
123 bool result = getName() == "/";
124 return result;
125}
126
127// Get the parent group.
129 if(isNull()) throw NcNullGrp("Attempt to invoke NcGroup::getParentGroup on a Null group",__FILE__,__LINE__);
130 try {
131 int parentId;
132 ncCheck(nc_inq_grp_parent(myId,&parentId),__FILE__,__LINE__);
133 NcGroup ncGroupParent(parentId);
134 return ncGroupParent;
135 }
136 catch (NcEnoGrp& e) {
137 // no group found, so return null group
138 return NcGroup();
139 }
140}
141
142
143// Get the group id.
144int NcGroup::getId() const {
145 if(isNull()) throw NcNullGrp("Attempt to invoke NcGroup::getId on a Null group",__FILE__,__LINE__);
146 return myId;
147}
148
149// Get the number of NcGroup objects.
151 if(isNull()) throw NcNullGrp("Attempt to invoke NcGroup::getGroupCount on a Null group",__FILE__,__LINE__);
152 // initialize group counter
153 int ngroups=0;
154
155 // record this group
156 if(location == ParentsAndCurrentGrps || location == AllGrps) {
157 ngroups ++;
158 }
159
160 // number of children in current group
161 if(location == ChildrenGrps || location == AllChildrenGrps || location == AllGrps ) {
162 int numgrps;
163 int* ncids=NULL;
164 ncCheck(nc_inq_grps(getId(), &numgrps,ncids),__FILE__,__LINE__);
165 ngroups += numgrps;
166 }
167
168 // search in parent groups
169 if(location == ParentsGrps || location == ParentsAndCurrentGrps || location == AllGrps ) {
170 multimap<string,NcGroup> groups(getGroups(ParentsGrps));
171 ngroups += static_cast<int>(groups.size());
172 }
173
174
175 // get the number of all children that are childreof children
176 if(location == ChildrenOfChildrenGrps || location == AllChildrenGrps || location == AllGrps ) {
177 multimap<string,NcGroup> groups(getGroups(ChildrenOfChildrenGrps));
178 ngroups += static_cast<int>(groups.size());
179 }
180
181 return ngroups;
182}
183
184
185// Get the set of child NcGroup objects.
186multimap<std::string,NcGroup> NcGroup::getGroups(NcGroup::GroupLocation location) const {
187 if(isNull()) throw NcNullGrp("Attempt to invoke NcGroup::getGroups on a Null group",__FILE__,__LINE__);
188 // create a container to hold the NcGroup's.
189 multimap<string,NcGroup> ncGroups;
190
191 // record this group
192 if(location == ParentsAndCurrentGrps || location == AllGrps) {
193 ncGroups.emplace(getName(), *this);
194 }
195
196 // the child groups of the current group
197 if(location == ChildrenGrps || location == AllChildrenGrps || location == AllGrps ) {
198 // get the number of groups
199 const auto groupCount = static_cast<std::size_t>(getGroupCount());
200 if (groupCount){
201 vector<int> ncids(groupCount);
202 int* numgrps=NULL;
203 // now get the id of each NcGroup and populate the ncGroups container.
204 ncCheck(nc_inq_grps(myId, numgrps,&ncids[0]),__FILE__,__LINE__);
205 for (auto ncid : ncids) {
206 NcGroup tmpGroup(ncid);
207 ncGroups.emplace(tmpGroup.getName(), tmpGroup);
208 }
209 }
210 }
211
212 // search in parent groups.
213 if(location == ParentsGrps || location == ParentsAndCurrentGrps || location == AllGrps ) {
214 NcGroup tmpGroup(*this);
215 if(!tmpGroup.isRootGroup()) {
216 while(1) {
217 const NcGroup parentGroup(tmpGroup.getParentGroup());
218 if(parentGroup.isNull()) break;
219 ncGroups.insert(pair<const string,NcGroup>(parentGroup.getName(),parentGroup));
220 tmpGroup=parentGroup;
221 }
222 }
223 }
224
225 // search in child groups of the children
226 if(location == ChildrenOfChildrenGrps || location == AllChildrenGrps || location == AllGrps ) {
227 multimap<string,NcGroup>::iterator it;
228 multimap<string,NcGroup> groups(getGroups(ChildrenGrps));
229 for (it=groups.begin();it!=groups.end();it++) {
230 multimap<string,NcGroup> childGroups(it->second.getGroups(AllChildrenGrps));
231 ncGroups.insert(childGroups.begin(),childGroups.end());
232 }
233 }
234
235 return ncGroups;
236}
237
238// Get the named child NcGroup object.
239NcGroup NcGroup::getGroup(const string& name,NcGroup::GroupLocation location) const{
240 if(isNull()) throw NcNullGrp("Attempt to invoke NcGroup::getGroup on a Null group",__FILE__,__LINE__);
241 multimap<string,NcGroup> ncGroups(getGroups(location));
242 pair<multimap<string,NcGroup>::iterator,multimap<string,NcGroup>::iterator> ret;
243 ret = ncGroups.equal_range(name);
244 if(ret.first == ret.second)
245 return NcGroup(); // null group is returned
246 else
247 return ret.first->second;
248}
249
250
251
252// Get all NcGroup objects with a given name.
253set<NcGroup> NcGroup::getGroups(const std::string& name,NcGroup::GroupLocation location) const {
254 if(isNull()) throw NcNullGrp("Attempt to invoke NcGroup::getGroups on a Null group",__FILE__,__LINE__);
255 // get the set of ncGroups in this group and above.
256 multimap<std::string,NcGroup> ncGroups(getGroups(location));
257 pair<multimap<string,NcGroup>::iterator,multimap<string,NcGroup>::iterator> ret;
258 multimap<string,NcGroup>::iterator it;
259 ret = ncGroups.equal_range(name);
260 set<NcGroup> tmpGroup;
261 for (it=ret.first; it!=ret.second; ++it) {
262 tmpGroup.insert(it->second);
263 }
264 return tmpGroup;
265}
266
267// Add a new child NcGroup object.
268NcGroup NcGroup::addGroup(const string& name) const {
269 if(isNull()) throw NcNullGrp("Attempt to invoke NcGroup::addGroup on a Null group",__FILE__,__LINE__);
270 int new_ncid;
271 ncCheck(nc_def_grp(myId, name.c_str(), &new_ncid),__FILE__,__LINE__);
272 return NcGroup(new_ncid);
273}
274
275
276
277// /////////////
278// NcVar-related accessors
279// /////////////
280
281// Get the number of NcVar objects in this group.
283
284 // search in current group.
285 NcGroup tmpGroup(*this);
286 int nvars=0;
287 // search in current group
288 if((location == ParentsAndCurrent || location == ChildrenAndCurrent || location == Current || location ==All) && !tmpGroup.isNull()) {
289 ncCheck(nc_inq_nvars(tmpGroup.getId(), &nvars),__FILE__,__LINE__);
290 }
291
292 // search recursively in all parent groups.
293 if(location == Parents || location == ParentsAndCurrent || location ==All) {
294 tmpGroup=getParentGroup();
295 while(!tmpGroup.isNull()) {
296 int nvarsp;
297 ncCheck(nc_inq_nvars(tmpGroup.getId(), &nvarsp),__FILE__,__LINE__);
298 nvars += nvarsp;
299 // continue loop with the parent.
300 tmpGroup=tmpGroup.getParentGroup();
301 }
302 }
303
304 // search recursively in all child groups
305 if(location == ChildrenAndCurrent || location == Children || location == All) {
306 multimap<string,NcGroup>::iterator it;
307 multimap<string,NcGroup> groups(getGroups());
308 for (it=groups.begin();it!=groups.end();it++) {
309 nvars += it->second.getVarCount(ChildrenAndCurrent);
310 }
311 }
312 return nvars;
313}
314
315// Get the collection of NcVar objects.
316multimap<std::string,NcVar> NcGroup::getVars(NcGroup::Location location) const {
317
318 // create a container to hold the NcVar's.
319 multimap<string,NcVar> ncVars;
320
321 // search in current group.
322 NcGroup tmpGroup(*this);
323 if((location == ParentsAndCurrent || location == ChildrenAndCurrent || location == Current || location ==All) && !tmpGroup.isNull()) {
324 // get the number of variables.
325 const auto varCount = static_cast<std::size_t>(getVarCount());
326 if (varCount){
327 // now get the name of each NcVar object and populate the ncVars container.
328 int* nvars=NULL;
329 vector<int> varids(varCount);
330 ncCheck(nc_inq_varids(myId, nvars,&varids[0]),__FILE__,__LINE__);
331 for (auto varid : varids) {
332 NcVar tmpVar(*this, varid);
333 ncVars.emplace(tmpVar.getName(), tmpVar);
334 }
335 }
336 }
337
338
339 // search recursively in all parent groups.
340 if(location == Parents || location == ParentsAndCurrent || location ==All) {
341 tmpGroup=getParentGroup();
342 while(!tmpGroup.isNull()) {
343 // get the number of variables
344 const auto varCount = static_cast<std::size_t>(tmpGroup.getVarCount());
345 if (varCount){
346 // now get the name of each NcVar object and populate the ncVars container.
347 int* nvars=NULL;
348 vector<int> varids(varCount);
349 ncCheck(nc_inq_varids(tmpGroup.getId(), nvars,&varids[0]),__FILE__,__LINE__);
350 for (auto varid : varids) {
351 NcVar tmpVar(tmpGroup, varid);
352 ncVars.emplace(tmpVar.getName(), tmpVar);
353 }
354 }
355 // continue loop with the parent.
356 tmpGroup=tmpGroup.getParentGroup();
357 }
358 }
359
360 // search recusively in all child groups.
361 if(location == ChildrenAndCurrent || location == Children || location == All ) {
362 multimap<string,NcGroup>::iterator it;
363 multimap<string,NcGroup> groups(getGroups());
364 for (it=groups.begin();it!=groups.end();it++) {
365 multimap<string,NcVar> vars=it->second.getVars(ChildrenAndCurrent);
366 ncVars.insert(vars.begin(),vars.end());
367 }
368 }
369
370 return ncVars;
371}
372
373
374// Get all NcVar objects with a given name.
375set<NcVar> NcGroup::getVars(const string& name,NcGroup::Location location) const {
376 // get the set of ncVars in this group and above.
377 multimap<std::string,NcVar> ncVars(getVars(location));
378 pair<multimap<string,NcVar>::iterator,multimap<string,NcVar>::iterator> ret;
379 multimap<string,NcVar>::iterator it;
380 ret = ncVars.equal_range(name);
381 set<NcVar> tmpVar;
382 for (it=ret.first; it!=ret.second; ++it) {
383 tmpVar.insert(it->second);
384 }
385 return tmpVar;
386}
387
388
389
390// Get the named NcVar object.
391NcVar NcGroup::getVar(const string& name,NcGroup::Location location) const {
392 multimap<std::string,NcVar> ncVars(getVars(location));
393 pair<multimap<string,NcVar>::iterator,multimap<string,NcVar>::iterator> ret;
394 ret = ncVars.equal_range(name);
395 if(ret.first == ret.second)
396 // no matching netCDF variable found so return null object.
397 return NcVar();
398 else
399 return ret.first->second;
400}
401
402// Adds a new netCDF scalar variable.
403NcVar NcGroup::addVar(const std::string& name, const NcType& ncType) const {
404 return NcGroup::addVar(name, ncType, std::vector<NcDim>());
405}
406
407// Add a new netCDF variable.
408NcVar NcGroup::addVar(const string& name, const string& typeName, const string& dimName) const {
409 ncCheckDefineMode(myId);
410
411 // get an NcType object with the given type name.
412 NcType tmpType(getType(typeName,NcGroup::ParentsAndCurrent));
413 if(tmpType.isNull()) throw NcNullType("Attempt to invoke NcGroup::addVar failed: typeName must be defined in either the current group or a parent group",__FILE__,__LINE__);
414
415 // get a NcDim object with the given dimension name
416 NcDim tmpDim(getDim(dimName,NcGroup::ParentsAndCurrent));
417 if(tmpDim.isNull()) throw NcNullDim("Attempt to invoke NcGroup::addVar failed: dimName must be defined in either the current group or a parent group",__FILE__,__LINE__);
418
419 // finally define a new netCDF variable
420 int varId;
421 int dimId(tmpDim.getId());
422 ncCheck(nc_def_var(myId,name.c_str(),tmpType.getId(),1,&dimId,&varId),__FILE__,__LINE__);
423 // return an NcVar object for this new variable
424 return NcVar(*this,varId);
425}
426
427
428// Add a new netCDF variable.
429NcVar NcGroup::addVar(const string& name, const NcType& ncType, const NcDim& ncDim) const {
430 ncCheckDefineMode(myId);
431
432 // check NcType object is valid
433 if(ncType.isNull()) throw NcNullType("Attempt to invoke NcGroup::addVar with a Null NcType object",__FILE__,__LINE__);
435 if(tmpType.isNull()) throw NcNullType("Attempt to invoke NcGroup::addVar failed: NcType must be defined in either the current group or a parent group",__FILE__,__LINE__);
436
437 // check NcDim object is valid
438 if(ncDim.isNull()) throw NcNullDim("Attempt to invoke NcGroup::addVar with a Null NcDim object",__FILE__,__LINE__);
440 if(tmpDim.isNull()) throw NcNullDim("Attempt to invoke NcGroup::addVar failed: NcDim must be defined in either the current group or a parent group",__FILE__,__LINE__);
441
442 // finally define a new netCDF variable
443 int varId;
444 int dimId(tmpDim.getId());
445 ncCheck(nc_def_var(myId,name.c_str(),tmpType.getId(),1,&dimId,&varId),__FILE__,__LINE__);
446 // return an NcVar object for this new variable
447 return NcVar(*this,varId);
448}
449
450
451// Add a new netCDF multi-dimensional variable.
452NcVar NcGroup::addVar(const string& name, const string& typeName, const vector<string>& dimNames) const {
453 ncCheckDefineMode(myId);
454
455 // get an NcType object with the given name.
456 NcType tmpType(getType(typeName,NcGroup::ParentsAndCurrent));
457 if(tmpType.isNull()) throw NcNullType("Attempt to invoke NcGroup::addVar failed: typeName must be defined in either the current group or a parent group",__FILE__,__LINE__);
458
459 // get a set of NcDim objects corresponding to the given dimension names.
460 vector<int> dimIds;
461 dimIds.reserve(dimNames.size());
462 for (const auto& dimName : dimNames) {
463 NcDim tmpDim(getDim(dimName, NcGroup::ParentsAndCurrent));
464 if(tmpDim.isNull()) throw NcNullDim("Attempt to invoke NcGroup::addVar failed: dimNames must be defined in either the current group or a parent group",__FILE__,__LINE__);
465 dimIds.push_back(tmpDim.getId());
466 }
467
468 // finally define a new netCDF variable
469 int varId;
470 int *dimIdsPtr = dimIds.empty() ? 0 : &dimIds[0];
471 ncCheck(nc_def_var(myId,name.c_str(),tmpType.getId(), static_cast<int>(dimIds.size()), dimIdsPtr,&varId),__FILE__,__LINE__);
472 // return an NcVar object for this new variable
473 return NcVar(*this,varId);
474}
475
476// Add a new netCDF multi-dimensional variable.
477NcVar NcGroup::addVar(const string& name, const NcType& ncType, const vector<NcDim>& ncDimVector) const {
478 ncCheckDefineMode(myId);
479
480 // check NcType object is valid
481 if(ncType.isNull()) throw NcNullType("Attempt to invoke NcGroup::addVar with a Null NcType object",__FILE__,__LINE__);
482 NcType tmpType(getType(ncType.getName(),NcGroup::ParentsAndCurrent));
483 if(tmpType.isNull()) throw NcNullType("Attempt to invoke NcGroup::addVar failed: NcType must be defined in either the current group or a parent group",__FILE__,__LINE__);
484
485 // check NcDim objects are valid
486 vector<NcDim>::const_iterator iter;
487 vector<int> dimIds;
488 dimIds.reserve(ncDimVector.size());
489 for (iter=ncDimVector.begin();iter < ncDimVector.end(); iter++) {
490 if(iter->isNull()) throw NcNullDim("Attempt to invoke NcGroup::addVar with a Null NcDim object",__FILE__,__LINE__);
491 NcDim tmpDim(getDim(iter->getName(),NcGroup::ParentsAndCurrent));
492 if(tmpDim.isNull()) throw NcNullDim("Attempt to invoke NcGroup::addVar failed: NcDim must be defined in either the current group or a parent group",__FILE__,__LINE__);
493 dimIds.push_back(tmpDim.getId());
494 }
495
496 // finally define a new netCDF variable
497 int varId;
498 int *dimIdsPtr = dimIds.empty() ? 0 : &dimIds[0];
499 ncCheck(nc_def_var(myId,name.c_str(),tmpType.getId(), static_cast<int>(dimIds.size()), dimIdsPtr,&varId),__FILE__,__LINE__);
500 // return an NcVar object for this new variable
501 return NcVar(*this,varId);
502}
503
504
505// /////////////
506// NcAtt-related methods
507// /////////////
508
509// Get the number of group attributes.
511
512 // search in current group.
513 NcGroup tmpGroup(*this);
514 int ngatts=0;
515 // search in current group
516 if((location == ParentsAndCurrent || location == ChildrenAndCurrent || location == Current || location ==All) && !tmpGroup.isNull()) {
517 ncCheck(nc_inq_natts(tmpGroup.getId(), &ngatts),__FILE__,__LINE__);
518 }
519
520 // search recursively in all parent groups.
521 if(location == Parents || location == ParentsAndCurrent || location ==All) {
522 tmpGroup=getParentGroup();
523 while(!tmpGroup.isNull()) {
524 int ngattsp;
525 ncCheck(nc_inq_natts(tmpGroup.getId(), &ngattsp),__FILE__,__LINE__);
526 ngatts += ngattsp;
527 // continue loop with the parent.
528 tmpGroup=tmpGroup.getParentGroup();
529 }
530 }
531
532 // search recursively in all child groups
533 if(location == ChildrenAndCurrent || location == Children || location == All) {
534 multimap<string,NcGroup>::iterator it;
535 multimap<string,NcGroup> groups(getGroups());
536 for (it=groups.begin();it!=groups.end();it++) {
537 ngatts += it->second.getAttCount(ChildrenAndCurrent);
538 }
539 }
540
541 return ngatts;
542}
543
544// Get the collection of NcGroupAtt objects.
545multimap<std::string,NcGroupAtt> NcGroup::getAtts(NcGroup::Location location) const {
546
547 // create a container to hold the NcAtt's.
548 multimap<string,NcGroupAtt> ncAtts;
549
550 // search in current group.
551 NcGroup tmpGroup(*this);
552 if((location == ParentsAndCurrent || location == ChildrenAndCurrent || location == Current || location ==All) && !tmpGroup.isNull()) {
553 // get the number of attributes
554 int attCount = tmpGroup.getAttCount();
555 // now get the name of each NcAtt and populate the ncAtts container.
556 for(int i=0; i<attCount;i++){
557 char charName[NC_MAX_NAME+1];
558 ncCheck(nc_inq_attname(tmpGroup.getId(),NC_GLOBAL,i,charName),__FILE__,__LINE__);
559 ncAtts.emplace(std::piecewise_construct,
560 std::forward_as_tuple(charName),
561 std::forward_as_tuple(tmpGroup, i));
562 }
563 }
564
565 // search recursively in all parent groups.
566 if(location == Parents || location == ParentsAndCurrent || location ==All) {
567 tmpGroup=getParentGroup();
568 while(!tmpGroup.isNull()) {
569 // get the number of attributes
570 int attCount = tmpGroup.getAttCount();
571 // now get the name of each NcAtt and populate the ncAtts container.
572 for(int i=0; i<attCount;i++){
573 char charName[NC_MAX_NAME+1];
574 ncCheck(nc_inq_attname(tmpGroup.getId(),NC_GLOBAL,i,charName),__FILE__,__LINE__);
575 ncAtts.emplace(std::piecewise_construct,
576 std::forward_as_tuple(charName),
577 std::forward_as_tuple(tmpGroup, i));
578 }
579 // continue loop with the parent.
580 tmpGroup=tmpGroup.getParentGroup();
581 }
582 }
583
584 // search recusively in all child groups.
585 if(location == ChildrenAndCurrent || location == Children || location == All ) {
586 multimap<string,NcGroup>::iterator it;
587 multimap<string,NcGroup> groups(getGroups());
588 for (it=groups.begin();it!=groups.end();it++) {
589 multimap<string,NcGroupAtt> atts=it->second.getAtts(ChildrenAndCurrent);
590 ncAtts.insert(atts.begin(),atts.end());
591 }
592 }
593
594 return ncAtts;
595}
596
597// Get the named NcGroupAtt object.
598NcGroupAtt NcGroup::getAtt(const std::string& name,NcGroup::Location location) const {
599 multimap<std::string,NcGroupAtt> ncAtts(getAtts(location));
600 pair<multimap<string,NcGroupAtt>::iterator,multimap<string,NcGroupAtt>::iterator> ret;
601 ret = ncAtts.equal_range(name);
602 if(ret.first == ret.second)
603 // no matching groupAttribute so return null object.
604 return NcGroupAtt();
605 else
606 return ret.first->second;
607}
608
609// Get all NcGroupAtt objects with a given name.
610set<NcGroupAtt> NcGroup::getAtts(const string& name,NcGroup::Location location) const {
611 // get the set of ncGroupAtts in this group and above.
612 multimap<std::string,NcGroupAtt> ncAtts(getAtts(location));
613 pair<multimap<string,NcGroupAtt>::iterator,multimap<string,NcGroupAtt>::iterator> ret;
614 multimap<string,NcGroupAtt>::iterator it;
615 ret = ncAtts.equal_range(name);
616 set<NcGroupAtt> tmpAtt;
617 for (it=ret.first; it!=ret.second; ++it) {
618 tmpAtt.insert(it->second);
619 }
620 return tmpAtt;
621}
622
623
624
625
626// Creates a new NetCDF group attribute or if already exisiting replaces it.
627NcGroupAtt NcGroup::putAtt(const string& name, const string& dataValues) const {
628 ncCheckDefineMode(myId);
629 ncCheck(nc_put_att_text(myId,NC_GLOBAL,name.c_str(),dataValues.size(),dataValues.c_str()),__FILE__,__LINE__);
630 // finally instantiate this attribute and return
631 return getAtt(name);
632}
633
634// Creates a new NetCDF group attribute or if already exisiting replaces it.
635NcGroupAtt NcGroup::putAtt(const string& name, const NcType& type, size_t len, const unsigned char* dataValues) const {
636 ncCheckDefineMode(myId);
637 if (type.isNonPrimitive())
638 ncCheck(nc_put_att(myId,NC_GLOBAL,name.c_str(),type.getId(),len,dataValues),__FILE__,__LINE__);
639 else
640 ncCheck(nc_put_att_uchar(myId,NC_GLOBAL,name.c_str(),type.getId(),len,dataValues),__FILE__,__LINE__);
641 // finally instantiate this attribute and return
642 return getAtt(name);
643}
644
645
646// Creates a new NetCDF group attribute or if already exisiting replaces it.
647NcGroupAtt NcGroup::putAtt(const string& name, const NcType& type, size_t len, const signed char* dataValues) const {
648 ncCheckDefineMode(myId);
649 if (type.isNonPrimitive())
650 ncCheck(nc_put_att(myId,NC_GLOBAL,name.c_str(),type.getId(),len,dataValues),__FILE__,__LINE__);
651 else
652 ncCheck(nc_put_att_schar(myId,NC_GLOBAL,name.c_str(),type.getId(),len,dataValues),__FILE__,__LINE__);
653 // finally instantiate this attribute and return
654 return getAtt(name);
655}
656
657
658// Creates a new NetCDF group attribute or if already exisiting replaces it.
659NcGroupAtt NcGroup::putAtt(const string& name, const NcType& type, short datumValue) const {
660 ncCheckDefineMode(myId);
661 if (type.isNonPrimitive())
662 ncCheck(nc_put_att(myId,NC_GLOBAL,name.c_str(),type.getId(),1,&datumValue),__FILE__,__LINE__);
663 else
664 ncCheck(nc_put_att_short(myId,NC_GLOBAL,name.c_str(),type.getId(),1,&datumValue),__FILE__,__LINE__);
665 // finally instantiate this attribute and return
666 return getAtt(name);
667}
668
669
670// Creates a new NetCDF group attribute or if already exisiting replaces it.
671NcGroupAtt NcGroup::putAtt(const string& name, const NcType& type, int datumValue) const {
672 ncCheckDefineMode(myId);
673 if (type.isNonPrimitive())
674 ncCheck(nc_put_att(myId,NC_GLOBAL,name.c_str(),type.getId(),1,&datumValue),__FILE__,__LINE__);
675 else
676 ncCheck(nc_put_att_int(myId,NC_GLOBAL,name.c_str(),type.getId(),1,&datumValue),__FILE__,__LINE__);
677 // finally instantiate this attribute and return
678 return getAtt(name);
679}
680
681// Creates a new NetCDF group attribute or if already exisiting replaces it.
682NcGroupAtt NcGroup::putAtt(const string& name, const NcType& type, long datumValue) const {
683 ncCheckDefineMode(myId);
684 if (type.isNonPrimitive())
685 ncCheck(nc_put_att(myId,NC_GLOBAL,name.c_str(),type.getId(),1,&datumValue),__FILE__,__LINE__);
686 else
687 ncCheck(nc_put_att_long(myId,NC_GLOBAL,name.c_str(),type.getId(),1,&datumValue),__FILE__,__LINE__);
688 // finally instantiate this attribute and return
689 return getAtt(name);
690}
691
692// Creates a new NetCDF group attribute or if already exisiting replaces it.
693NcGroupAtt NcGroup::putAtt(const string& name, const NcType& type, float datumValue) const {
694 ncCheckDefineMode(myId);
695 if (type.isNonPrimitive())
696 ncCheck(nc_put_att(myId,NC_GLOBAL,name.c_str(),type.getId(),1,&datumValue),__FILE__,__LINE__);
697 else
698 ncCheck(nc_put_att_float(myId,NC_GLOBAL,name.c_str(),type.getId(),1,&datumValue),__FILE__,__LINE__);
699 // finally instantiate this attribute and return
700 return getAtt(name);
701}
702
703
704// Creates a new NetCDF group attribute or if already exisiting replaces it.
705NcGroupAtt NcGroup::putAtt(const string& name, const NcType& type, double datumValue) const {
706 ncCheckDefineMode(myId);
707 if (type.isNonPrimitive())
708 ncCheck(nc_put_att(myId,NC_GLOBAL,name.c_str(),type.getId(),1,&datumValue),__FILE__,__LINE__);
709 else
710 ncCheck(nc_put_att_double(myId,NC_GLOBAL,name.c_str(),type.getId(),1,&datumValue),__FILE__,__LINE__);
711 // finally instantiate this attribute and return
712 return getAtt(name);
713}
714
715
716// Creates a new NetCDF group attribute or if already exisiting replaces it.
717NcGroupAtt NcGroup::putAtt(const string& name, const NcType& type, unsigned short datumValue) const {
718 ncCheckDefineMode(myId);
719 if (type.isNonPrimitive())
720 ncCheck(nc_put_att(myId,NC_GLOBAL,name.c_str(),type.getId(),1,&datumValue),__FILE__,__LINE__);
721 else
722 ncCheck(nc_put_att_ushort(myId,NC_GLOBAL,name.c_str(),type.getId(),1,&datumValue),__FILE__,__LINE__);
723 // finally instantiate this attribute and return
724 return getAtt(name);
725}
726
727// Creates a new NetCDF group attribute or if already exisiting replaces it.
728NcGroupAtt NcGroup::putAtt(const string& name, const NcType& type, unsigned int datumValue) const {
729 ncCheckDefineMode(myId);
730 if (type.isNonPrimitive())
731 ncCheck(nc_put_att(myId,NC_GLOBAL,name.c_str(),type.getId(),1,&datumValue),__FILE__,__LINE__);
732 else
733 ncCheck(nc_put_att_uint(myId,NC_GLOBAL,name.c_str(),type.getId(),1,&datumValue),__FILE__,__LINE__);
734 // finally instantiate this attribute and return
735 return getAtt(name);
736}
737
738// Creates a new NetCDF group attribute or if already exisiting replaces it.
739NcGroupAtt NcGroup::putAtt(const string& name, const NcType& type, long long datumValue) const {
740 ncCheckDefineMode(myId);
741 if (type.isNonPrimitive())
742 ncCheck(nc_put_att(myId,NC_GLOBAL,name.c_str(),type.getId(),1,&datumValue),__FILE__,__LINE__);
743 else
744 ncCheck(nc_put_att_longlong(myId,NC_GLOBAL,name.c_str(),type.getId(),1,&datumValue),__FILE__,__LINE__);
745 // finally instantiate this attribute and return
746 return getAtt(name);
747}
748
749
750// Creates a new NetCDF group attribute or if already exisiting replaces it.
751NcGroupAtt NcGroup::putAtt(const string& name, const NcType& type, unsigned long long datumValue) const {
752 ncCheckDefineMode(myId);
753 if (type.isNonPrimitive())
754 ncCheck(nc_put_att(myId,NC_GLOBAL,name.c_str(),type.getId(),1,&datumValue),__FILE__,__LINE__);
755 else
756 ncCheck(nc_put_att_ulonglong(myId,NC_GLOBAL,name.c_str(),type.getId(),1,&datumValue),__FILE__,__LINE__);
757 // finally instantiate this attribute and return
758 return getAtt(name);
759}
760
761
762// Creates a new NetCDF group attribute or if already exisiting replaces it.
763NcGroupAtt NcGroup::putAtt(const string& name, const NcType& type, size_t len, const short* dataValues) const {
764 ncCheckDefineMode(myId);
765 if (type.isNonPrimitive())
766 ncCheck(nc_put_att(myId,NC_GLOBAL,name.c_str(),type.getId(),len,dataValues),__FILE__,__LINE__);
767 else
768 ncCheck(nc_put_att_short(myId,NC_GLOBAL,name.c_str(),type.getId(),len,dataValues),__FILE__,__LINE__);
769 // finally instantiate this attribute and return
770 return getAtt(name);
771}
772
773
774// Creates a new NetCDF group attribute or if already exisiting replaces it.
775NcGroupAtt NcGroup::putAtt(const string& name, const NcType& type, size_t len, const int* dataValues) const {
776 ncCheckDefineMode(myId);
777 if (type.isNonPrimitive())
778 ncCheck(nc_put_att(myId,NC_GLOBAL,name.c_str(),type.getId(),len,dataValues),__FILE__,__LINE__);
779 else
780 ncCheck(nc_put_att_int(myId,NC_GLOBAL,name.c_str(),type.getId(),len,dataValues),__FILE__,__LINE__);
781 // finally instantiate this attribute and return
782 return getAtt(name);
783}
784
785// Creates a new NetCDF group attribute or if already exisiting replaces it.
786NcGroupAtt NcGroup::putAtt(const string& name, const NcType& type, size_t len, const long* dataValues) const {
787 ncCheckDefineMode(myId);
788 if (type.isNonPrimitive())
789 ncCheck(nc_put_att(myId,NC_GLOBAL,name.c_str(),type.getId(),len,dataValues),__FILE__,__LINE__);
790 else
791 ncCheck(nc_put_att_long(myId,NC_GLOBAL,name.c_str(),type.getId(),len,dataValues),__FILE__,__LINE__);
792 // finally instantiate this attribute and return
793 return getAtt(name);
794}
795
796// Creates a new NetCDF group attribute or if already exisiting replaces it.
797NcGroupAtt NcGroup::putAtt(const string& name, const NcType& type, size_t len, const float* dataValues) const {
798 ncCheckDefineMode(myId);
799 if (type.isNonPrimitive())
800 ncCheck(nc_put_att(myId,NC_GLOBAL,name.c_str(),type.getId(),len,dataValues),__FILE__,__LINE__);
801 else
802 ncCheck(nc_put_att_float(myId,NC_GLOBAL,name.c_str(),type.getId(),len,dataValues),__FILE__,__LINE__);
803 // finally instantiate this attribute and return
804 return getAtt(name);
805}
806
807
808// Creates a new NetCDF group attribute or if already exisiting replaces it.
809NcGroupAtt NcGroup::putAtt(const string& name, const NcType& type, size_t len, const double* dataValues) const {
810 ncCheckDefineMode(myId);
811 if (type.isNonPrimitive())
812 ncCheck(nc_put_att(myId,NC_GLOBAL,name.c_str(),type.getId(),len,dataValues),__FILE__,__LINE__);
813 else
814 ncCheck(nc_put_att_double(myId,NC_GLOBAL,name.c_str(),type.getId(),len,dataValues),__FILE__,__LINE__);
815 // finally instantiate this attribute and return
816 return getAtt(name);
817}
818
819
820// Creates a new NetCDF group attribute or if already exisiting replaces it.
821NcGroupAtt NcGroup::putAtt(const string& name, const NcType& type, size_t len, const unsigned short* dataValues) const {
822 ncCheckDefineMode(myId);
823 if (type.isNonPrimitive())
824 ncCheck(nc_put_att(myId,NC_GLOBAL,name.c_str(),type.getId(),len,dataValues),__FILE__,__LINE__);
825 else
826 ncCheck(nc_put_att_ushort(myId,NC_GLOBAL,name.c_str(),type.getId(),len,dataValues),__FILE__,__LINE__);
827 // finally instantiate this attribute and return
828 return getAtt(name);
829}
830
831// Creates a new NetCDF group attribute or if already exisiting replaces it.
832NcGroupAtt NcGroup::putAtt(const string& name, const NcType& type, size_t len, const unsigned int* dataValues) const {
833 ncCheckDefineMode(myId);
834 if (type.isNonPrimitive())
835 ncCheck(nc_put_att(myId,NC_GLOBAL,name.c_str(),type.getId(),len,dataValues),__FILE__,__LINE__);
836 else
837 ncCheck(nc_put_att_uint(myId,NC_GLOBAL,name.c_str(),type.getId(),len,dataValues),__FILE__,__LINE__);
838 // finally instantiate this attribute and return
839 return getAtt(name);
840}
841
842// Creates a new NetCDF group attribute or if already exisiting replaces it.
843NcGroupAtt NcGroup::putAtt(const string& name, const NcType& type, size_t len, const long long* dataValues) const {
844 ncCheckDefineMode(myId);
845 if (type.isNonPrimitive())
846 ncCheck(nc_put_att(myId,NC_GLOBAL,name.c_str(),type.getId(),len,dataValues),__FILE__,__LINE__);
847 else
848 ncCheck(nc_put_att_longlong(myId,NC_GLOBAL,name.c_str(),type.getId(),len,dataValues),__FILE__,__LINE__);
849 // finally instantiate this attribute and return
850 return getAtt(name);
851}
852
853
854// Creates a new NetCDF group attribute or if already exisiting replaces it.
855NcGroupAtt NcGroup::putAtt(const string& name, const NcType& type, size_t len, const unsigned long long* dataValues) const {
856 ncCheckDefineMode(myId);
857 if (type.isNonPrimitive())
858 ncCheck(nc_put_att(myId,NC_GLOBAL,name.c_str(),type.getId(),len,dataValues),__FILE__,__LINE__);
859 else
860 ncCheck(nc_put_att_ulonglong(myId,NC_GLOBAL,name.c_str(),type.getId(),len,dataValues),__FILE__,__LINE__);
861 // finally instantiate this attribute and return
862 return getAtt(name);
863}
864
865
866// Creates a new NetCDF group attribute or if already exisiting replaces it.
867NcGroupAtt NcGroup::putAtt(const string& name, size_t len, const char** dataValues) const {
868 ncCheckDefineMode(myId);
869 ncCheck(nc_put_att_string(myId,NC_GLOBAL,name.c_str(),len,dataValues),__FILE__,__LINE__);
870 // finally instantiate this attribute and return
871 return getAtt(name);
872}
873
874// Creates a new NetCDF group attribute or if already exisiting replaces it.
875 NcGroupAtt NcGroup::putAtt(const string& name, const NcType& type, size_t len, const void* dataValues) const {
876 ncCheckDefineMode(myId);
877 ncCheck(nc_put_att(myId,NC_GLOBAL,name.c_str(),type.getId(),len,dataValues),__FILE__,__LINE__);
878 // finally instantiate this attribute and return
879 return getAtt(name);
880}
881
882
883
884// /////////////
885// NcDim-related methods
886// /////////////
887
888// Get the number of NcDim objects.
890 if(isNull()) throw NcNullGrp("Attempt to invoke NcGroup::getDimCount on a Null group",__FILE__,__LINE__);
891
892 // intialize counter
893 int ndims=0;
894
895 // search in current group
896 if(location == Current || location == ParentsAndCurrent || location == ChildrenAndCurrent || location == All ) {
897 int ndimsp;
898 ncCheck(nc_inq_ndims(getId(), &ndimsp),__FILE__,__LINE__);
899 ndims += ndimsp;
900 }
901
902 // search in parent groups.
903 if(location == Parents || location == ParentsAndCurrent || location == All ) {
904 multimap<string,NcGroup>::iterator it;
905 multimap<string,NcGroup> groups(getGroups(ParentsGrps));
906 for (it=groups.begin();it!=groups.end();it++) {
907 ndims += it->second.getDimCount();
908 }
909 }
910
911 // search in child groups.
912 if(location == Children || location == ChildrenAndCurrent || location == All ) {
913 multimap<string,NcGroup>::iterator it;
914 multimap<string,NcGroup> groups(getGroups(AllChildrenGrps));
915 for (it=groups.begin();it!=groups.end();it++) {
916 ndims += it->second.getDimCount();
917 }
918 }
919 return ndims;
920}
921
922
923// Get the set of NcDim objects.
924multimap<string,NcDim> NcGroup::getDims(NcGroup::Location location) const {
925 if(isNull()) throw NcNullGrp("Attempt to invoke NcGroup::getDims on a Null group",__FILE__,__LINE__);
926 // create a container to hold the NcDim's.
927 multimap<string,NcDim> ncDims;
928
929 // search in current group
930 if(location == Current || location == ParentsAndCurrent || location == ChildrenAndCurrent || location == All ) {
931 const auto dimCount = static_cast<std::size_t>(getDimCount());
932 if (dimCount){
933 vector<int> dimids(dimCount);
934 ncCheck(nc_inq_dimids(getId(), nullptr, &dimids[0], 0),__FILE__,__LINE__);
935 // now get the name of each NcDim and populate the nDims container.
936 for (auto dimid : dimids) {
937 NcDim tmpDim(*this,dimid);
938 ncDims.emplace(tmpDim.getName(), tmpDim);
939 }
940 }
941 }
942
943 // search in parent groups.
944 if(location == Parents || location == ParentsAndCurrent || location == All ) {
945 multimap<string,NcGroup>::iterator it;
946 multimap<string,NcGroup> groups(getGroups(ParentsGrps));
947 for (it=groups.begin();it!=groups.end();it++) {
948 multimap<string,NcDim> dimTmp(it->second.getDims());
949 ncDims.insert(dimTmp.begin(),dimTmp.end());
950 }
951 }
952
953 // search in child groups (makes recursive calls).
954 if(location == Children || location == ChildrenAndCurrent || location == All ) {
955 multimap<string,NcGroup>::iterator it;
956 multimap<string,NcGroup> groups(getGroups(AllChildrenGrps));
957 for (it=groups.begin();it!=groups.end();it++) {
958 multimap<string,NcDim> dimTmp(it->second.getDims());
959 ncDims.insert(dimTmp.begin(),dimTmp.end());
960 }
961 }
962
963 return ncDims;
964}
965
966
967
968// Get the named NcDim object.
969NcDim NcGroup::getDim(const string& name,NcGroup::Location location) const {
970 if(isNull()) throw NcNullGrp("Attempt to invoke NcGroup::getDim on a Null group",__FILE__,__LINE__);
971 multimap<string,NcDim> ncDims(getDims(location));
972 pair<multimap<string,NcDim>::iterator,multimap<string,NcDim>::iterator> ret;
973 ret = ncDims.equal_range(name);
974 if(ret.first == ret.second)
975 return NcDim(); // null group is returned
976 else
977 return ret.first->second;
978}
979
980
981// Get all NcDim objects with a given name.
982set<NcDim> NcGroup::getDims(const string& name,NcGroup::Location location) const {
983 if(isNull()) throw NcNullGrp("Attempt to invoke NcGroup::getDims on a Null group",__FILE__,__LINE__);
984 // get the set of ncDims in this group and above.
985 multimap<string,NcDim> ncDims(getDims(location));
986 pair<multimap<string,NcDim>::iterator,multimap<string,NcDim>::iterator> ret;
987 multimap<string,NcDim>::iterator it;
988 ret = ncDims.equal_range(name);
989 set<NcDim> tmpDim;
990 for (it=ret.first; it!=ret.second; ++it) {
991 tmpDim.insert(it->second);
992 }
993 return tmpDim;
994}
995
996// Add a new NcDim object.
997NcDim NcGroup::addDim(const string& name, size_t dimSize) const {
998 ncCheckDefineMode(myId);
999 if(isNull()) throw NcNullGrp("Attempt to invoke NcGroup::addDim on a Null group",__FILE__,__LINE__);
1000 int dimId;
1001 ncCheck(nc_def_dim(myId,name.c_str(),dimSize,&dimId),__FILE__,__LINE__);
1002 // finally return NcDim object for this new variable
1003 return NcDim(*this,dimId);
1004}
1005
1006// Add a new NcDim object with unlimited size..
1007NcDim NcGroup::addDim(const string& name) const {
1008 ncCheckDefineMode(myId);
1009 if(isNull()) throw NcNullGrp("Attempt to invoke NcGroup::addDim on a Null group",__FILE__,__LINE__);
1010 int dimId;
1011 ncCheck(nc_def_dim(myId,name.c_str(),NC_UNLIMITED,&dimId),__FILE__,__LINE__);
1012 // finally return NcDim object for this new variable
1013 return NcDim(*this,dimId);
1014}
1015
1016
1017
1018
1019
1020// /////////////
1021// type-object related methods
1022// /////////////
1023
1024// Gets the number of type objects.
1026
1027 if(isNull()) throw NcNullGrp("Attempt to invoke NcGroup::getTypeCount on a Null group",__FILE__,__LINE__);
1028
1029 // intialize counter
1030 int ntypes=0;
1031
1032 // search in current group
1033 if(location == Current || location == ParentsAndCurrent || location == ChildrenAndCurrent || location == All ) {
1034 int ntypesp;
1035 int* typeidsp=NULL;
1036 ncCheck(nc_inq_typeids(getId(), &ntypesp,typeidsp),__FILE__,__LINE__);
1037 ntypes+= ntypesp;
1038 }
1039
1040 // search in parent groups.
1041 if(location == Parents || location == ParentsAndCurrent || location == All ) {
1042 multimap<string,NcGroup>::iterator it;
1043 multimap<string,NcGroup> groups(getGroups(ParentsGrps));
1044 for (it=groups.begin();it!=groups.end();it++) {
1045 ntypes += it->second.getTypeCount();
1046 }
1047 }
1048
1049 // search in child groups.
1050 if(location == Children || location == ChildrenAndCurrent || location == All ) {
1051 multimap<string,NcGroup>::iterator it;
1052 multimap<string,NcGroup> groups(getGroups(AllChildrenGrps));
1053 for (it=groups.begin();it!=groups.end();it++) {
1054 ntypes += it->second.getTypeCount();
1055 }
1056 }
1057 return ntypes;
1058}
1059
1060
1061
1062// Gets the number of type objects with a given enumeration type.
1064
1065 if(isNull()) throw NcNullGrp("Attempt to invoke NcGroup::getTypeCount on a Null group",__FILE__,__LINE__);
1066
1067 // intialize counter
1068 int ntypes=0;
1069
1070 // search in current group
1071 if(location == Current || location == ParentsAndCurrent || location == ChildrenAndCurrent || location == All ) {
1072 int ntypesp;
1073 int* typeidsp=NULL;
1074 ncCheck(nc_inq_typeids(getId(), &ntypesp,typeidsp),__FILE__,__LINE__);
1075 if (ntypesp){
1076 vector<int> typeids(static_cast<std::size_t>(ntypesp));
1077 ncCheck(nc_inq_typeids(getId(), &ntypesp,&typeids[0]),__FILE__,__LINE__);
1078 for (auto typeid_ : typeids) {
1079 NcType tmpType(*this, typeid_);
1080 if(tmpType.getTypeClass() == enumType) ntypes++;
1081 }
1082 }
1083 }
1084
1085 // search in parent groups.
1086 if(location == Parents || location == ParentsAndCurrent || location == All ) {
1087 multimap<string,NcGroup>::iterator it;
1088 multimap<string,NcGroup> groups(getGroups(ParentsGrps));
1089 for (it=groups.begin();it!=groups.end();it++) {
1090 ntypes += it->second.getTypeCount(enumType);
1091 }
1092 }
1093
1094 // search in child groups.
1095 if(location == Children || location == ChildrenAndCurrent || location == All ) {
1096 multimap<string,NcGroup>::iterator it;
1097 multimap<string,NcGroup> groups(getGroups(AllChildrenGrps));
1098 for (it=groups.begin();it!=groups.end();it++) {
1099 ntypes += it->second.getTypeCount(enumType);
1100 }
1101 }
1102 return ntypes;
1103}
1104
1105
1106// Gets the collection of NcType objects.
1107multimap<string,NcType> NcGroup::getTypes(NcGroup::Location location) const {
1108 if(isNull()) throw NcNullGrp("Attempt to invoke NcGroup::getTypes on a Null group",__FILE__,__LINE__);
1109 // create a container to hold the NcType's.
1110 multimap<string,NcType> ncTypes;
1111
1112 // search in current group
1113 if(location == Current || location == ParentsAndCurrent || location == ChildrenAndCurrent || location == All ) {
1114 const auto typeCount = static_cast<std::size_t>(getTypeCount());
1115 if (typeCount){
1116 vector<int> typeids(typeCount);
1117 ncCheck(nc_inq_typeids(getId(), nullptr, &typeids[0]),__FILE__,__LINE__);
1118 // now get the name of each NcType and populate the nTypes container.
1119 for (auto typeid_ : typeids) {
1120 NcType tmpType(*this, typeid_);
1121 ncTypes.emplace(tmpType.getName(), tmpType);
1122 }
1123 }
1124 }
1125
1126 // search in parent groups.
1127 if(location == Parents || location == ParentsAndCurrent || location == All ) {
1128 multimap<string,NcGroup>::iterator it;
1129 multimap<string,NcGroup> groups(getGroups(ParentsGrps));
1130 for (it=groups.begin();it!=groups.end();it++) {
1131 multimap<string,NcType> typeTmp(it->second.getTypes());
1132 ncTypes.insert(typeTmp.begin(),typeTmp.end());
1133 }
1134 }
1135
1136 // search in child groups (makes recursive calls).
1137 if(location == Children || location == ChildrenAndCurrent || location == All ) {
1138 multimap<string,NcGroup>::iterator it;
1139 multimap<string,NcGroup> groups(getGroups(AllChildrenGrps));
1140 for (it=groups.begin();it!=groups.end();it++) {
1141 multimap<string,NcType> typeTmp(it->second.getTypes());
1142 ncTypes.insert(typeTmp.begin(),typeTmp.end());
1143 }
1144 }
1145
1146 return ncTypes;
1147}
1148
1149
1150// Gets the collection of NcType objects with a given name.
1151set<NcType> NcGroup::getTypes(const string& name, NcGroup::Location location) const {
1152 if(isNull()) throw NcNullGrp("Attempt to invoke NcGroup::getTypes on a Null group",__FILE__,__LINE__);
1153 // iterator for the multimap container.
1154 multimap<string,NcType>::iterator it;
1155 // return argument of equal_range: iterators to lower and upper bounds of the range.
1156 pair<multimap<string,NcType>::iterator,multimap<string,NcType>::iterator> ret;
1157 // get the entire collection of types.
1158 multimap<string,NcType> types(getTypes(location));
1159 // define STL set object to hold the result
1160 set<NcType> tmpType;
1161 // get the set of NcType objects with a given name
1162 ret=types.equal_range(name);
1163 for (it=ret.first;it!=ret.second;it++) {
1164 tmpType.insert(it->second);
1165 }
1166 return tmpType;
1167}
1168
1169
1170// Gets the collection of NcType objects with a given data type.
1171set<NcType> NcGroup::getTypes(NcType::ncType enumType, NcGroup::Location location) const {
1172 if(isNull()) throw NcNullGrp("Attempt to invoke NcGroup::getTypes on a Null group",__FILE__,__LINE__);
1173 // iterator for the multimap container.
1174 multimap<string,NcType>::iterator it;
1175 // get the entire collection of types.
1176 multimap<string,NcType> types(getTypes(location));
1177 // define STL set object to hold the result
1178 set<NcType> tmpType;
1179 // get the set of NcType objects with a given data type
1180 for (it=types.begin();it!=types.end();it++) {
1181 if(it->second.getTypeClass() == enumType) {
1182 tmpType.insert(it->second);
1183 }
1184 }
1185 return(tmpType);
1186}
1187
1188
1189// Gets the collection of NcType objects with a given name and data type.
1190set<NcType> NcGroup::getTypes(const string& name, NcType::ncType enumType, NcGroup::Location location) const {
1191 if(isNull()) throw NcNullGrp("Attempt to invoke NcGroup::getTypes on a Null group",__FILE__,__LINE__);
1192 // iterator for the multimap container.
1193 multimap<string,NcType>::iterator it;
1194 // return argument of equal_range: iterators to lower and upper bounds of the range.
1195 pair<multimap<string,NcType>::iterator,multimap<string,NcType>::iterator> ret;
1196 // get the entire collection of types.
1197 multimap<string,NcType> types(getTypes(location));
1198 // define STL set object to hold the result
1199 set<NcType> tmpType;
1200 // get the set of NcType objects with a given name
1201 ret=types.equal_range(name);
1202 for (it=ret.first;it!=ret.second;it++) {
1203 if((*it).second.getTypeClass() == enumType) {
1204 tmpType.insert(it->second);
1205 }
1206 }
1207 return(tmpType);
1208}
1209
1210
1211// Gets the NcType object with a given name.
1212NcType NcGroup::getType(const string& name, NcGroup::Location location) const {
1213 if(isNull()) throw NcNullGrp("Attempt to invoke NcGroup::getType on a Null group",__FILE__,__LINE__);
1214 if(name == "byte" ) return ncByte;
1215 if(name == "ubyte" ) return ncUbyte;
1216 if(name == "char" ) return ncChar;
1217 if(name == "short" ) return ncShort;
1218 if(name == "ushort" ) return ncUshort;
1219 if(name == "int" ) return ncInt;
1220 if(name == "uint" ) return ncUint;
1221 if(name == "int64" ) return ncInt64;
1222 if(name == "uint64" ) return ncUint64;
1223 if(name == "float" ) return ncFloat;
1224 if(name == "double" ) return ncDouble;
1225 if(name == "string" ) return ncString;
1226
1227 // this is a user defined type
1228 // iterator for the multimap container.
1229 multimap<string,NcType>::iterator it;
1230 // return argument of equal_range: iterators to lower and upper bounds of the range.
1231 pair<multimap<string,NcType>::iterator,multimap<string,NcType>::iterator> ret;
1232 // get the entire collection of types.
1233 multimap<string,NcType> types(getTypes(location));
1234 // define STL set object to hold the result
1235 set<NcType> tmpType;
1236 // get the set of NcType objects with a given name
1237 ret=types.equal_range(name);
1238 if(ret.first == ret.second)
1239 return NcType();
1240 else
1241 return ret.first->second;
1242}
1243
1244
1245// Adds a new netCDF Enum type.
1246NcEnumType NcGroup::addEnumType(const string& name,NcEnumType::ncEnumType baseType) const {
1247 ncCheckDefineMode(myId);
1248 nc_type typeId;
1249 ncCheck(nc_def_enum(myId, baseType, name.c_str(), &typeId),__FILE__,__LINE__);
1250 NcEnumType ncTypeTmp(*this,name);
1251 return ncTypeTmp;
1252}
1253
1254
1255// Adds a new netCDF Vlen type.
1256NcVlenType NcGroup::addVlenType(const string& name,NcType& baseType) const {
1257 ncCheckDefineMode(myId);
1258 nc_type typeId;
1259 ncCheck(nc_def_vlen(myId, name.c_str(), baseType.getId(),&typeId),__FILE__,__LINE__);
1260 NcVlenType ncTypeTmp(*this,name);
1261 return ncTypeTmp;
1262}
1263
1264
1265// Adds a new netCDF Opaque type.
1266NcOpaqueType NcGroup::addOpaqueType(const string& name, size_t size) const {
1267 ncCheckDefineMode(myId);
1268 nc_type typeId;
1269 ncCheck(nc_def_opaque(myId, size, name.c_str(), &typeId),__FILE__,__LINE__);
1270 NcOpaqueType ncTypeTmp(*this,name);
1271 return ncTypeTmp;
1272}
1273
1274// Adds a new netCDF UserDefined type.
1275NcCompoundType NcGroup::addCompoundType(const string& name, size_t size) const {
1276 ncCheckDefineMode(myId);
1277 nc_type typeId;
1278 ncCheck(nc_def_compound(myId, size, name.c_str(), &typeId),__FILE__,__LINE__);
1279 NcCompoundType ncTypeTmp(*this,name);
1280 return ncTypeTmp;
1281}
1282
1283
1284// Get the collection of coordinate variables.
1285map<string,NcGroup> NcGroup::getCoordVars(NcGroup::Location location) const {
1286 map<string,NcGroup> coordVars;
1287
1288 // search in current group and parent groups.
1289 NcGroup tmpGroup(*this);
1290 multimap<string,NcDim>::iterator itD;
1291 multimap<string,NcVar>::iterator itV;
1292 bool check_current_group = !(location == Parents || location == Children);
1293 const bool check_parent_groups = (location == Parents || location == ParentsAndCurrent || location == All);
1294 const bool check_child_groups = (location == Children || location == ChildrenAndCurrent || location == All);
1295
1296 while(1) {
1297 // get the collection of NcDim objects defined in this group.
1298 if (check_current_group) {
1299 multimap<string,NcDim> dimTmp(tmpGroup.getDims());
1300 multimap<string,NcVar> varTmp(tmpGroup.getVars());
1301 for (itD=dimTmp.begin();itD!=dimTmp.end();itD++) {
1302 string coordName(itD->first);
1303 itV = varTmp.find(coordName);
1304 if(itV != varTmp.end()) {
1305 coordVars.insert(pair<const string,NcGroup>(string(coordName),tmpGroup));
1306 }
1307 }
1308 }
1309
1310 if (!check_parent_groups || tmpGroup.isRootGroup()) {
1311 break;
1312 }
1313 check_current_group = true;
1314
1315 // continue loop with the parent.
1316 tmpGroup=tmpGroup.getParentGroup();
1317 }
1318
1319 // search in child groups (makes recursive calls).
1320 if (check_child_groups) {
1321 multimap<string,NcGroup>::iterator it;
1322 multimap<string,NcGroup> groups(getGroups());
1323 for (it=groups.begin();it!=groups.end();it++) {
1324 map<string,NcGroup> coordVarsTmp = it->second.getCoordVars(ChildrenAndCurrent);
1325 coordVars.insert(coordVarsTmp.begin(),coordVarsTmp.end());
1326 }
1327 }
1328
1329 return coordVars;
1330}
1331
1332// Get the NcDim and NcVar object pair for a named coordinate variables.
1333void NcGroup::getCoordVar(const string& coordVarName, NcDim& ncDim, NcVar& ncVar, NcGroup::Location location) const {
1334 // Nullify existing dim/var in case no coordinate variable found
1335 ncDim = NcDim{};
1336 ncVar = NcVar{};
1337
1338 // search in current group and parent groups.
1339 multimap<string,NcDim>::iterator itD;
1340 NcGroup tmpGroup(*this);
1341 multimap<string,NcVar>::iterator itV;
1342 bool check_current_group = !(location == Parents || location == Children);
1343 const bool check_parent_groups = (location == Parents || location == ParentsAndCurrent || location == All);
1344 const bool check_child_groups = (location == Children || location == ChildrenAndCurrent || location == All);
1345
1346 while(1) {
1347 // get the collection of NcDim objects defined in this group.
1348 if (check_current_group) {
1349 multimap<string,NcDim> dimTmp(tmpGroup.getDims());
1350 multimap<string,NcVar> varTmp(tmpGroup.getVars());
1351 itD=dimTmp.find(coordVarName);
1352 itV=varTmp.find(coordVarName);
1353 if(itD != dimTmp.end() && itV != varTmp.end()) {
1354 ncDim=itD->second;
1355 ncVar=itV->second;
1356 return;
1357 }
1358 }
1359 if(!check_parent_groups || tmpGroup.isRootGroup()) {
1360 break;
1361 }
1362 check_current_group = true;
1363 // continue loop with the parent.
1364 tmpGroup=tmpGroup.getParentGroup();
1365 }
1366
1367 // search in child groups (makes recursive calls).
1368 if (check_child_groups) {
1369 multimap<string,NcGroup>::iterator it;
1370 multimap<string,NcGroup> groups(getGroups());
1371 for (it=groups.begin();it!=groups.end();it++) {
1372 it->second.getCoordVar(coordVarName,ncDim,ncVar,ChildrenAndCurrent);
1373 if(!ncDim.isNull()) break;
1374 }
1375 }
1376}
Class represents a netCDF compound type.
Class represents a netCDF dimension.
Definition ncDim.h:13
std::string getName() const
The name of this dimension.
Definition ncDim.cpp:106
bool isNull() const
Returns true if this object is null (i.e.
Definition ncDim.h:62
NcDim()
Constructor generates a null object.
Definition ncDim.cpp:64
Class represents a netCDF enum type.
Definition ncEnumType.h:16
ncEnumType
List of NetCDF-4 Enumeration types.
Definition ncEnumType.h:20
Class represents a netCDF group attribute.
Definition ncGroupAtt.h:13
NcGroupAtt()
Constructor generates a null object.
int getGroupCount(NcGroup::GroupLocation location=ChildrenGrps) const
Gets the number of NcGroup objects.
Definition ncGroup.cpp:150
virtual ~NcGroup()
destructor
Definition ncGroup.cpp:47
friend bool operator>(const NcGroup &lhs, const NcGroup &rhs)
comparator operator
Definition ncGroup.cpp:37
NcGroup & operator=(const NcGroup &rhs)
assignment operator
Definition ncGroup.cpp:65
NcCompoundType addCompoundType(const std::string &name, size_t size) const
Adds a new netCDF UserDefined type.
Definition ncGroup.cpp:1275
bool operator!=(const NcGroup &rhs) const
!= operator
Definition ncGroup.cpp:89
NcVlenType addVlenType(const std::string &name, NcType &basetype) const
Adds a new netCDF Vlen type.
Definition ncGroup.cpp:1256
NcGroup getGroup(const std::string &name, NcGroup::GroupLocation location=ChildrenGrps) const
Gets the named child NcGroup object.
Definition ncGroup.cpp:239
int getTypeCount(NcGroup::Location location=Current) const
Gets the number of type objects.
Definition ncGroup.cpp:1025
int getId() const
Gets the group id.
Definition ncGroup.cpp:144
NcType getType(const std::string &name, NcGroup::Location location=Current) const
Gets the NcType object with a given name.
Definition ncGroup.cpp:1212
int getVarCount(NcGroup::Location location=Current) const
Gets the number of NcVar objects in this group.
Definition ncGroup.cpp:282
std::multimap< std::string, NcGroupAtt > getAtts(NcGroup::Location location=Current) const
Gets the collection of NcGroupAtt objects.
Definition ncGroup.cpp:545
NcGroupAtt putAtt(const std::string &name, size_t len, const char **dataValues) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
NcGroup()
Constructor generates a null object.
Definition ncGroup.cpp:52
NcDim addDim(const std::string &name, size_t dimSize) const
Adds a new netCDF dimension.
NcGroupAtt getAtt(const std::string &name, NcGroup::Location location=Current) const
Gets the named NcGroupAtt object.
Definition ncGroup.cpp:598
bool isRootGroup() const
Returns true if this is the root group, otherwise returns false.
Definition ncGroup.cpp:122
void getCoordVar(const std::string &coordVarName, NcDim &ncDim, NcVar &ncVar, NcGroup::Location location=Current) const
Gets the NcDim and NcVar object pair for a named coordinate variable.
Definition ncGroup.cpp:1333
NcGroup addGroup(const std::string &name) const
Adds a new child netCDF group object.
Definition ncGroup.cpp:268
Location
The enumeration list contains the options for selecting groups.
Definition ncGroup.h:47
@ ParentsAndCurrent
Select from contents of current and parents groups.
Definition ncGroup.h:51
@ Current
Select from contents of current group.
Definition ncGroup.h:48
@ Children
Select from contents of children groups.
Definition ncGroup.h:50
@ ChildrenAndCurrent
Select from contents of current and child groups.
Definition ncGroup.h:52
@ All
Select from contents of current, parents and child groups.
Definition ncGroup.h:53
@ Parents
Select from contents of parents groups.
Definition ncGroup.h:49
bool operator==(const NcGroup &rhs) const
equivalence operator
Definition ncGroup.cpp:80
std::multimap< std::string, NcVar > getVars(NcGroup::Location location=Current) const
Get the collection of NcVar objects.
Definition ncGroup.cpp:316
std::map< std::string, NcGroup > getCoordVars(NcGroup::Location location=Current) const
Gets a collection of coordinate variables.
Definition ncGroup.cpp:1285
GroupLocation
The enumeration list contains the options for selecting groups (used for returned set of NcGroup obje...
Definition ncGroup.h:34
@ ParentsAndCurrentGrps
Select from set of parent groups(includes the current group).
Definition ncGroup.h:39
@ ChildrenOfChildrenGrps
Select from set of all children of children in the current group.
Definition ncGroup.h:37
@ ParentsGrps
Select from set of parent groups (excludes the current group).
Definition ncGroup.h:36
@ AllGrps
Select from set of parent groups, current groups and all the children beneath.
Definition ncGroup.h:40
@ ChildrenGrps
Select from the set of children in the current group.
Definition ncGroup.h:35
@ AllChildrenGrps
Select from set of all children of the current group and beneath.
Definition ncGroup.h:38
NcOpaqueType addOpaqueType(const std::string &name, size_t size) const
Adds a new netCDF Opaque type.
Definition ncGroup.cpp:1266
std::string getName(bool fullName=false) const
Gets the group name.
Definition ncGroup.cpp:100
int getAttCount(NcGroup::Location location=Current) const
Gets the number of group attributes.
Definition ncGroup.cpp:510
NcEnumType addEnumType(const std::string &name, NcEnumType::ncEnumType basetype) const
Adds a new netCDF enum type.
Definition ncGroup.cpp:1246
NcGroup getParentGroup() const
Gets the parent group.
Definition ncGroup.cpp:128
NcVar getVar(const std::string &name, NcGroup::Location location=Current) const
Gets the named NcVar object.
Definition ncGroup.cpp:391
bool nullObject
assignment operator
Definition ncGroup.h:568
NcDim getDim(const std::string &name, NcGroup::Location location=Current) const
Gets the named NcDim object.
Definition ncGroup.cpp:969
NcVar addVar(const std::string &name, const NcType &ncType) const
Adds a new netCDF scalar variable.
Definition ncGroup.cpp:403
bool isNull() const
Returns true if this object is null (i.e.
Definition ncGroup.h:156
std::multimap< std::string, NcGroup > getGroups(NcGroup::GroupLocation location=ChildrenGrps) const
Gets the collection of NcGroup objects.
Definition ncGroup.cpp:186
friend bool operator<(const NcGroup &lhs, const NcGroup &rhs)
comparator operator
Definition ncGroup.cpp:31
std::multimap< std::string, NcType > getTypes(NcGroup::Location location=Current) const
Gets the collection of NcType objects.
Definition ncGroup.cpp:1107
int getDimCount(NcGroup::Location location=Current) const
Gets the number of NcDim objects.
Definition ncGroup.cpp:889
std::multimap< std::string, NcDim > getDims(NcGroup::Location location=Current) const
Gets the collection of NcDim objects.
Definition ncGroup.cpp:924
Class represents a netCDF opaque type.
Base class inherited by NcOpaque, NcVlen, NcCompound and NcEnum classes.
Definition ncType.h:15
ncType getTypeClass() const
The type class returned as enumeration type.
Definition ncType.cpp:111
NcType()
Constructor generates a null object.
Definition ncType.cpp:43
bool isNonPrimitive() const
Return true if this is not a primitive type.
Definition ncType.cpp:163
ncType
List of netCDF types that can be represented.
Definition ncType.h:26
std::string getName() const
The name of this type.
Definition ncType.cpp:93
bool isNull() const
Returns true if this object is null (i.e.
Definition ncType.h:136
nc_type getId() const
The netCDF Id of this type.
Definition ncType.h:88
Class represents a netCDF variable.
Definition ncVar.h:34
std::string getName() const
Name of this NcVar object.
Definition ncVar.cpp:528
NcVar()
Constructor generates a null object.
Definition ncVar.cpp:66
Class represents a netCDF VLEN type.
Definition ncVlenType.h:15
Thrown if cannot return a netCDF group.
Thrown if the requested operation is on a NULL dimension.
Thrown if the requested operation is on a NULL group.
Thrown if the requested operation is on a NULL type.
Exception classes.
Definition ncException.h:16
C++ API for netCDF4.
Definition ncAtt.h:10
NcShort ncShort
A global instance of the NcShort class within the netCDF namespace.
Definition ncShort.cpp:7
NcFloat ncFloat
A global instance of the NcFloat class within the netCDF namespace.
Definition ncFloat.cpp:7
NcInt64 ncInt64
A global instance of the NcInt64 class within the netCDF namespace.
Definition ncInt64.cpp:7
void ncCheckDefineMode(int ncid)
Function checks if the file (group) is in define mode.
Definition ncCheck.cpp:83
NcUint64 ncUint64
A global instance of the NcUint64 class within the netCDF namespace.
Definition ncUint64.cpp:7
NcUint ncUint
A global instance of the NcUint class within the netCDF namespace.
Definition ncUint.cpp:7
NcString ncString
A global instance of the NcString class within the netCDF namespace.
Definition ncString.cpp:7
NcInt ncInt
A global instance of the NcInt class within the netCDF namespace.
Definition ncInt.cpp:7
NcDouble ncDouble
A global instance of the NcDouble class within the netCDF namespace.
Definition ncDouble.cpp:7
void ncCheck(int retCode, const char *file, int line)
Function checks error code and if necessary throws an exception.
Definition ncCheck.cpp:11
NcUbyte ncUbyte
A global instance of the NcUbyte class within the netCDF namespace.
Definition ncUbyte.cpp:7
NcChar ncChar
A global instance of the NcChar class within the netCDF namespace.
Definition ncChar.cpp:7
NcByte ncByte
A global instance of the NcByte class within the netCDF namespace.
Definition ncByte.cpp:7

Return to the Main Unidata NetCDF page.
Generated on for NetCDF-C++. NetCDF is a Unidata library.