NetCDF-C++  4.3.1-developer
ncCompoundType.cpp
1 #include "ncGroup.h"
2 #include "ncCheck.h"
3 #include "ncCompoundType.h"
4 #include "ncByte.h"
5 #include "ncUbyte.h"
6 #include "ncChar.h"
7 #include "ncShort.h"
8 #include "ncUshort.h"
9 #include "ncInt.h"
10 #include "ncUint.h"
11 #include "ncInt64.h"
12 #include "ncUint64.h"
13 #include "ncFloat.h"
14 #include "ncDouble.h"
15 #include "ncString.h"
16 #include "ncException.h"
17 
18 using namespace std;
19 using namespace netCDF;
20 using namespace netCDF::exceptions;
21 
22 // Class represents a netCDF variable.
23 
24 // assignment operator
25 NcCompoundType& NcCompoundType::operator=(const NcCompoundType& rhs)
26 {
27  NcType::operator=(rhs); // assign base class parts
28  return *this;
29 }
30 
31 // assignment operator
32 NcCompoundType& NcCompoundType::operator=(const NcType& rhs)
33 {
34  if (&rhs != this) {
35  // check the rhs is the base of a Compound type
36  if(getTypeClass() != nc_COMPOUND) throw NcException("The NcType object must be the base of a Compound type.",__FILE__,__LINE__);
37  // assign base class parts
38  NcType::operator=(rhs);
39  }
40  return *this;
41 }
42 
43 // The copy constructor.
44 NcCompoundType::NcCompoundType(const NcCompoundType& rhs):
45  NcType(rhs)
46 {
47 }
48 
49 
50 // equivalence operator
52 {
53  if(nullObject)
54  return nullObject == rhs.nullObject;
55  else
56  return myId ==rhs.myId && groupId == rhs.groupId;
57 }
58 
59 // Constructor generates a null object.
61  NcType() // invoke base class constructor
62 {}
63 
64 // constructor
65 NcCompoundType::NcCompoundType(const NcGroup& grp, const string& name):
66  NcType(grp,name)
67 {
68 }
69 
70 // constructor
71 // The copy constructor.
73  NcType()
74 {
75  // assign base class parts
76  NcType::operator=(rhs);
77 }
78 
79 // Inserts a named field.
80 void NcCompoundType::addMember(const string& memberName, const NcType& newMemberType,size_t offset)
81 {
82  ncCheck(nc_insert_compound(groupId,myId,const_cast<char*>(memberName.c_str()),offset,newMemberType.getId()),__FILE__,__LINE__);
83 }
84 
85 
86 
87 // Inserts a named array field.
88 void NcCompoundType::addMember(const string& memberName, const NcType& newMemberType, size_t offset, const vector<int>& shape)
89 {
90  if (!shape.empty())
91  ncCheck(nc_insert_array_compound(groupId, myId,const_cast<char*>(memberName.c_str()), offset, newMemberType.getId(), shape.size(), const_cast<int*>(&shape[0])),__FILE__,__LINE__);
92  else
93  addMember(memberName, newMemberType, offset);
94 }
95 
96 
97 
98 // Returns number of members in this NcCompoundType object.
100 {
101  size_t nfieldsp;
102  ncCheck(nc_inq_compound_nfields(groupId,myId,&nfieldsp),__FILE__,__LINE__);
103  return nfieldsp;
104 }
105 
106 
107 // Returns a NcType object for a single member. */
108 NcType NcCompoundType::getMember(int memberIndex) const
109 {
110  nc_type fieldtypeidp;
111  ncCheck(nc_inq_compound_fieldtype(groupId,myId,memberIndex,&fieldtypeidp),__FILE__,__LINE__);
112  switch (fieldtypeidp) {
113  case NC_BYTE : return ncByte;
114  case NC_UBYTE : return ncUbyte;
115  case NC_CHAR : return ncChar;
116  case NC_SHORT : return ncShort;
117  case NC_USHORT : return ncUshort;
118  case NC_INT : return ncInt;
119  case NC_UINT : return ncUint;
120  case NC_INT64 : return ncInt64;
121  case NC_UINT64 : return ncUint64;
122  case NC_FLOAT : return ncFloat;
123  case NC_DOUBLE : return ncDouble;
124  case NC_STRING : return ncString;
125  default:
126  // this is a user defined type
127  return NcType(getParentGroup(),fieldtypeidp);
128  }
129 }
130 
131 // Returns name of member field.
132 std::string NcCompoundType::getMemberName(int memberIndex) const
133 {
134  char fieldName[NC_MAX_NAME+1];
135  ncCheck(nc_inq_compound_fieldname(groupId,myId,memberIndex, fieldName),__FILE__,__LINE__);
136  return std::string(fieldName);
137 }
138 
139 // Returns index of named member field.
140 int NcCompoundType::getMemberIndex(const std::string& memberName) const{
141  int memberIndex;
142  ncCheck(nc_inq_compound_fieldindex(groupId,myId, memberName.c_str(),&memberIndex),__FILE__,__LINE__);
143  return memberIndex;
144 }
145 
146 // Returns the number of dimensions of a member with the given index.
147 int NcCompoundType::getMemberDimCount(int memberIndex) const
148 {
149  int ndimsp;
150  ncCheck(nc_inq_compound_fieldndims(groupId,myId,memberIndex, &ndimsp),__FILE__,__LINE__);
151  return ndimsp;
152 }
153 
154 
155 // Returns the shape of the given member.
156 vector<int> NcCompoundType::getMemberShape(int memberIndex) const
157 {
158  vector<int> dim_size;
159  dim_size.resize(getMemberDimCount(memberIndex));
160  if(!dim_size.empty())
161  ncCheck(nc_inq_compound_fielddim_sizes(groupId,myId,memberIndex,&dim_size[0]),__FILE__,__LINE__);
162  return dim_size;
163 }
164 
165 
166 // Returns the offset of the member with given index.
167 size_t NcCompoundType::getMemberOffset(const int index) const
168 {
169  size_t offsetp;
170  ncCheck(nc_inq_compound_fieldoffset(groupId,myId, index,&offsetp),__FILE__,__LINE__);
171  return offsetp;
172 }
Base class inherited by NcOpaque, NcVlen, NcCompound and NcEnum classes.
Definition: ncType.h:14
NcUint64 ncUint64
A global instance of the NcUint64 class within the netCDF namespace.
Definition: ncUint64.cpp:7
int groupId
the group Id
Definition: ncType.h:152
size_t getMemberCount() const
Returns number of members in this NcCompoundType object.
C++ API for netCDF4.
Definition: ncAtt.h:9
std::string getMemberName(int memberIndex) const
Returns name of member field.
NcByte ncByte
A global instance of the NcByte class within the netCDF namespace.
Definition: ncByte.cpp:7
Class represents a netCDF compound type.
NcFloat ncFloat
A global instance of the NcFloat class within the netCDF namespace.
Definition: ncFloat.cpp:7
Class represents a netCDF group.
Definition: ncGroup.h:27
Exception classes.
Definition: ncException.h:15
NcUint ncUint
A global instance of the NcUint class within the netCDF namespace.
Definition: ncUint.cpp:7
NcType getMember(int memberIndex) const
Returns a NcType object for a single member.
NcChar ncChar
A global instance of the NcChar class within the netCDF namespace.
Definition: ncChar.cpp:7
std::vector< int > getMemberShape(int memberIndex) const
Returns the shape of a given member.
nc_type myId
the type Id
Definition: ncType.h:149
nc_type getId() const
The netCDF Id of this type.
Definition: ncType.h:88
NcDouble ncDouble
A global instance of the NcDouble class within the netCDF namespace.
Definition: ncDouble.cpp:7
Base object is thrown if a netCDF exception is encountered.
Definition: ncException.h:24
NcType()
Constructor generates a null object.
Definition: ncType.cpp:44
void ncCheck(int retCode, const char *file, int line)
Function checks error code and if necessary throws an exception.
Definition: ncCheck.cpp:11
bool operator==(const NcCompoundType &rhs)
equivalence operator
size_t getMemberOffset(const int index) const
Returns the offset of the member with given index.
int getMemberDimCount(int memberIndex) const
Returns the number of dimensions of a member with the given index.
NcShort ncShort
A global instance of the NcShort class within the netCDF namespace.
Definition: ncShort.cpp:7
NcCompoundType()
Constructor generates a null object.
NcString ncString
A global instance of the NcString class within the netCDF namespace.
Definition: ncString.cpp:7
netCDF::NcGroup getParentGroup() const
Gets parent group.
Definition: ncType.cpp:89
NcInt ncInt
A global instance of the NcInt class within the netCDF namespace.
Definition: ncInt.cpp:7
int getMemberIndex(const std::string &memberName) const
Returns index of named member field.
void addMember(const std::string &memName, const NcType &newMemberType, size_t offset)
Adds a named field.
NcUbyte ncUbyte
A global instance of the NcUbyte class within the netCDF namespace.
Definition: ncUbyte.cpp:7
NcInt64 ncInt64
A global instance of the NcInt64 class within the netCDF namespace.
Definition: ncInt64.cpp:7
NcType & operator=(const NcType &rhs)
assignment operator
Definition: ncType.cpp:27

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