NetCDF-C++  4.3.1-developer
ncAtt.cpp
1 #include "ncAtt.h"
2 #include "ncGroup.h"
3 #include "ncCheck.h"
4 #include <vector>
5 
6 using namespace std;
7 using namespace netCDF;
8 
9 
10 // destructor (defined even though it is virtual)
11 NcAtt::~NcAtt() {}
12 
13 // assignment operator
14 NcAtt& NcAtt::operator=(const NcAtt& rhs)
15 {
16  nullObject = rhs.nullObject;
17  myName = rhs.myName;
18  groupId = rhs.groupId;
19  varId =rhs.varId;
20  return *this;
21 }
22 
23 // Constructor generates a null object.
24 NcAtt::NcAtt() :
25  nullObject(true)
26 {}
27 
28 // Constructor for non-null instances.
29 NcAtt::NcAtt(bool nullObject):
30  nullObject(nullObject)
31 {}
32 
33 // The copy constructor.
34 NcAtt::NcAtt(const NcAtt& rhs) :
35  nullObject(rhs.nullObject),
36  myName(rhs.myName),
37  groupId(rhs.groupId),
38  varId(rhs.varId)
39 {}
40 
41 
42 // equivalence operator
43 bool NcAtt::operator==(const NcAtt & rhs) const
44 {
45  if(nullObject)
46  return nullObject == rhs.nullObject;
47  else
48  return myName == rhs.myName && groupId == rhs.groupId && varId == rhs.varId;
49 }
50 
51 // != operator
52 bool NcAtt::operator!=(const NcAtt & rhs) const
53 {
54  return !(*this == rhs);
55 }
56 
57 // Gets parent group.
59  return netCDF::NcGroup(groupId);
60 }
61 
62 
63 // Returns the attribute type.
65  // get the identifier for the netCDF type of this attribute.
66  nc_type xtypep;
67  ncCheck(nc_inq_atttype(groupId,varId,myName.c_str(),&xtypep),__FILE__,__LINE__);
68  if(xtypep <= 12)
69  // This is an atomic type
70  return NcType(xtypep);
71  else
72  // this is a user-defined type
73  {
74  // now get the set of NcType objects in this file.
75  multimap<string,NcType> typeMap(getParentGroup().getTypes(NcGroup::ParentsAndCurrent));
76  multimap<string,NcType>::iterator iter;
77  // identify the Nctype object with the same id as this attribute.
78  for (iter=typeMap.begin(); iter!= typeMap.end();iter++) {
79  if(iter->second.getId() == xtypep) return iter->second;
80  }
81  // return a null object, as no type was identified.
82  return NcType();
83  }
84 }
85 
86 // Gets attribute length.
87 size_t NcAtt::getAttLength() const{
88  size_t lenp;
89  ncCheck(nc_inq_attlen(groupId, varId, myName.c_str(), &lenp),__FILE__,__LINE__);
90  return lenp;
91 }
92 
93 // Gets a netCDF variable attribute.
94 void NcAtt::getValues(string& dataValues) const {
95  NcType::ncType typeClass(getType().getTypeClass());
96 
97  size_t att_len=getAttLength();
98  char* tmpValues;
99  tmpValues = (char *) malloc(att_len + 1); /* + 1 for trailing null */
100 
101  if(typeClass == NcType::nc_VLEN || typeClass == NcType::nc_OPAQUE || typeClass == NcType::nc_ENUM || typeClass == NcType::nc_COMPOUND)
102  ncCheck(nc_get_att(groupId,varId,myName.c_str(),tmpValues),__FILE__,__LINE__);
103  else
104  ncCheck(nc_get_att_text(groupId,varId,myName.c_str(),tmpValues),__FILE__,__LINE__);
105  dataValues=string(tmpValues,att_len);
106  free(tmpValues);
107 }
108 
109 // Gets a netCDF variable attribute.
110 void NcAtt::getValues(char* dataValues) const {
111  NcType::ncType typeClass(getType().getTypeClass());
112  if(typeClass == NcType::nc_VLEN || typeClass == NcType::nc_OPAQUE || typeClass == NcType::nc_ENUM || typeClass == NcType::nc_COMPOUND)
113  ncCheck(nc_get_att(groupId,varId,myName.c_str(),dataValues),__FILE__,__LINE__);
114  else
115  ncCheck(nc_get_att_text(groupId,varId,myName.c_str(),dataValues),__FILE__,__LINE__);
116 }
117 
118 
119 // Gets a netCDF variable attribute.
120 void NcAtt::getValues(unsigned char* dataValues) const {
121  NcType::ncType typeClass(getType().getTypeClass());
122  if(typeClass == NcType::nc_VLEN || typeClass == NcType::nc_OPAQUE || typeClass == NcType::nc_ENUM || typeClass == NcType::nc_COMPOUND)
123  ncCheck(nc_get_att(groupId,varId,myName.c_str(),dataValues),__FILE__,__LINE__);
124  else
125  ncCheck(nc_get_att_uchar(groupId,varId,myName.c_str(),dataValues),__FILE__,__LINE__);
126 }
127 
128 // Gets a netCDF variable attribute.
129 void NcAtt::getValues(signed char* dataValues) const {
130  NcType::ncType typeClass(getType().getTypeClass());
131  if(typeClass == NcType::nc_VLEN || typeClass == NcType::nc_OPAQUE || typeClass == NcType::nc_ENUM || typeClass == NcType::nc_COMPOUND)
132  ncCheck(nc_get_att(groupId,varId,myName.c_str(),dataValues),__FILE__,__LINE__);
133  else
134  ncCheck(nc_get_att_schar(groupId,varId,myName.c_str(),dataValues),__FILE__,__LINE__);
135 }
136 
137 // Gets a netCDF variable attribute.
138 void NcAtt::getValues(short* dataValues) const {
139  NcType::ncType typeClass(getType().getTypeClass());
140  if(typeClass == NcType::nc_VLEN || typeClass == NcType::nc_OPAQUE || typeClass == NcType::nc_ENUM || typeClass == NcType::nc_COMPOUND)
141  ncCheck(nc_get_att(groupId,varId,myName.c_str(),dataValues),__FILE__,__LINE__);
142  else
143  ncCheck(nc_get_att_short(groupId,varId,myName.c_str(),dataValues),__FILE__,__LINE__);
144 }
145 
146 // Gets a netCDF variable attribute.
147 void NcAtt::getValues(int* dataValues) const {
148  NcType::ncType typeClass(getType().getTypeClass());
149  if(typeClass == NcType::nc_VLEN || typeClass == NcType::nc_OPAQUE || typeClass == NcType::nc_ENUM || typeClass == NcType::nc_COMPOUND)
150  ncCheck(nc_get_att(groupId,varId,myName.c_str(),dataValues),__FILE__,__LINE__);
151  else
152  ncCheck(nc_get_att_int(groupId,varId,myName.c_str(),dataValues),__FILE__,__LINE__);
153 }
154 
155 // Gets a netCDF variable attribute.
156 void NcAtt::getValues(long* dataValues) const {
157  NcType::ncType typeClass(getType().getTypeClass());
158  if(typeClass == NcType::nc_VLEN || typeClass == NcType::nc_OPAQUE || typeClass == NcType::nc_ENUM || typeClass == NcType::nc_COMPOUND)
159  ncCheck(nc_get_att(groupId,varId,myName.c_str(),dataValues),__FILE__,__LINE__);
160  else
161  ncCheck(nc_get_att_long(groupId,varId,myName.c_str(),dataValues),__FILE__,__LINE__);
162 }
163 
164 // Gets a netCDF variable attribute.
165 void NcAtt::getValues(float* dataValues) const {
166  NcType::ncType typeClass(getType().getTypeClass());
167  if(typeClass == NcType::nc_VLEN || typeClass == NcType::nc_OPAQUE || typeClass == NcType::nc_ENUM || typeClass == NcType::nc_COMPOUND)
168  ncCheck(nc_get_att(groupId,varId,myName.c_str(),dataValues),__FILE__,__LINE__);
169  else
170  ncCheck(nc_get_att_float(groupId,varId,myName.c_str(),dataValues),__FILE__,__LINE__);
171 }
172 
173 // Gets a netCDF variable attribute.
174 void NcAtt::getValues(double* dataValues) const {
175  NcType::ncType typeClass(getType().getTypeClass());
176  if(typeClass == NcType::nc_VLEN || typeClass == NcType::nc_OPAQUE || typeClass == NcType::nc_ENUM || typeClass == NcType::nc_COMPOUND)
177  ncCheck(nc_get_att(groupId,varId,myName.c_str(),dataValues),__FILE__,__LINE__);
178  else
179  ncCheck(nc_get_att_double(groupId,varId,myName.c_str(),dataValues),__FILE__,__LINE__);
180 }
181 
182 // Gets a netCDF variable attribute.
183 void NcAtt::getValues(unsigned short* dataValues) const {
184  NcType::ncType typeClass(getType().getTypeClass());
185  if(typeClass == NcType::nc_VLEN || typeClass == NcType::nc_OPAQUE || typeClass == NcType::nc_ENUM || typeClass == NcType::nc_COMPOUND)
186  ncCheck(nc_get_att(groupId,varId,myName.c_str(),dataValues),__FILE__,__LINE__);
187  else
188  ncCheck(nc_get_att_ushort(groupId,varId,myName.c_str(),dataValues),__FILE__,__LINE__);
189 }
190 
191 // Gets a netCDF variable attribute.
192 void NcAtt::getValues(unsigned int* dataValues) const {
193  NcType::ncType typeClass(getType().getTypeClass());
194  if(typeClass == NcType::nc_VLEN || typeClass == NcType::nc_OPAQUE || typeClass == NcType::nc_ENUM || typeClass == NcType::nc_COMPOUND)
195  ncCheck(nc_get_att(groupId,varId,myName.c_str(),dataValues),__FILE__,__LINE__);
196  else
197  ncCheck(nc_get_att_uint(groupId,varId,myName.c_str(),dataValues),__FILE__,__LINE__);
198 }
199 
200 // Gets a netCDF variable attribute.
201 void NcAtt::getValues(long long* dataValues) const {
202  NcType::ncType typeClass(getType().getTypeClass());
203  if(typeClass == NcType::nc_VLEN || typeClass == NcType::nc_OPAQUE || typeClass == NcType::nc_ENUM || typeClass == NcType::nc_COMPOUND)
204  ncCheck(nc_get_att(groupId,varId,myName.c_str(),dataValues),__FILE__,__LINE__);
205  else
206  ncCheck(nc_get_att_longlong(groupId,varId,myName.c_str(),dataValues),__FILE__,__LINE__);
207 }
208 
209 // Gets a netCDF variable attribute.
210 void NcAtt::getValues(unsigned long long* dataValues) const {
211  NcType::ncType typeClass(getType().getTypeClass());
212  if(typeClass == NcType::nc_VLEN || typeClass == NcType::nc_OPAQUE || typeClass == NcType::nc_ENUM || typeClass == NcType::nc_COMPOUND)
213  ncCheck(nc_get_att(groupId,varId,myName.c_str(),dataValues),__FILE__,__LINE__);
214  else
215  ncCheck(nc_get_att_ulonglong(groupId,varId,myName.c_str(),dataValues),__FILE__,__LINE__);
216 }
217 
218 // Gets a netCDF variable attribute.
219 void NcAtt::getValues(char** dataValues) const {
220  NcType::ncType typeClass(getType().getTypeClass());
221  if(typeClass == NcType::nc_VLEN || typeClass == NcType::nc_OPAQUE || typeClass == NcType::nc_ENUM || typeClass == NcType::nc_COMPOUND)
222  ncCheck(nc_get_att(groupId,varId,myName.c_str(),dataValues),__FILE__,__LINE__);
223  else
224  ncCheck(nc_get_att_string(groupId,varId,myName.c_str(),dataValues),__FILE__,__LINE__);
225 }
226 
227 // Gets a netCDF variable attribute.
228 void NcAtt::getValues(void* dataValues) const {
229  ncCheck(nc_get_att(groupId,varId,myName.c_str(),dataValues),__FILE__,__LINE__);
230 }
231 
NcAtt()
Constructor generates a null object.
Definition: ncAtt.cpp:24
Base class inherited by NcOpaque, NcVlen, NcCompound and NcEnum classes.
Definition: ncType.h:14
void getValues(char *dataValues) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: ncAtt.cpp:110
size_t getAttLength() const
Gets attribute length.
Definition: ncAtt.cpp:87
"NcOpaque type"
Definition: ncType.h:40
C++ API for netCDF4.
Definition: ncAtt.h:9
NcType getType() const
Returns the attribute type.
Definition: ncAtt.cpp:64
"NcCompound type"
Definition: ncType.h:42
NcGroup getParentGroup() const
Gets parent group.
Definition: ncAtt.cpp:58
bool operator==(const NcAtt &rhs) const
equivalence operator
Definition: ncAtt.cpp:43
bool operator!=(const NcAtt &rhs) const
!= operator
Definition: ncAtt.cpp:52
"NcVlen type"
Definition: ncType.h:39
Class represents a netCDF group.
Definition: ncGroup.h:27
ncType
List of netCDF types that can be represented.
Definition: ncType.h:25
Select from contents of current and parents groups.
Definition: ncGroup.h:53
void ncCheck(int retCode, const char *file, int line)
Function checks error code and if necessary throws an exception.
Definition: ncCheck.cpp:11
"NcEnum type"
Definition: ncType.h:41
Abstract base class represents inherited by ncVarAtt and ncGroupAtt.
Definition: ncAtt.h:13

Return to the Main Unidata NetCDF page.
Generated on Fri Nov 11 2016 15:28:29 for NetCDF-C++. NetCDF is a Unidata library.