NetCDF-C++ 4.3.2
Loading...
Searching...
No Matches
ncType.cpp
1#include <string>
2#include "ncType.h"
3#include "ncGroup.h"
4#include "ncCheck.h"
5using namespace std;
6
7
8namespace netCDF {
9 // Global comparator operator ==============
10 // comparator operator
11 bool operator<(const NcType& lhs,const NcType& rhs)
12 {
13 return false;
14 }
15
16 // comparator operator
17 bool operator>(const NcType& lhs,const NcType& rhs)
18 {
19 return true;
20 }
21}
22
23using namespace netCDF;
24
25// assignment operator
27{
28 nullObject = rhs.nullObject;
29 myId= rhs.myId;
30 groupId = rhs.groupId;
31 return *this;
32}
33
34// The copy constructor.
36 nullObject(rhs.nullObject),
37 myId(rhs.myId),
38 groupId(rhs.groupId)
39{}
40
41
42// Constructor generates a null object.
44 nullObject(true)
45{}
46
47// constructor
48NcType::NcType(const NcGroup& grp, const string& name) :
49 nullObject (false)
50{
51 groupId= grp.getId();
53 myId = typTmp.getId();
54}
55
56// constructor for a global type
57NcType::NcType(nc_type id) :
58 nullObject(false),
59 myId(id),
60 groupId(0)
61{
62}
63
64// Constructor for a non-global type
65NcType::NcType(const netCDF::NcGroup& grp, nc_type id):
66 nullObject(false),
67 myId(id),
68 groupId(grp.getId())
69{}
70
71
72// equivalence operator
73bool NcType::operator==(const NcType & rhs) const
74{
75 if(nullObject)
76 return nullObject == rhs.nullObject;
77 else
78 return groupId == rhs.groupId && myId == rhs.myId;
79}
80
81// != operator
82bool NcType::operator!=(const NcType & rhs) const
83{
84 return !(*this == rhs);
85}
86
87// Gets parent group.
89 if(groupId == 0) return NcGroup(); else return NcGroup(groupId);
90}
91
92// Returns the type name.
93string NcType::getName() const{
94 char charName[NC_MAX_NAME+1];
95 size_t *sizep=NULL;
96
97 ncCheck(nc_inq_type(groupId,myId,charName,sizep),__FILE__,__LINE__);
98 return string(charName);
99
100};
101
102// Returns the size in bytes
103size_t NcType::getSize() const{
104 char* charName=NULL;
105 size_t sizep;
106 ncCheck(nc_inq_type(groupId,myId,charName,&sizep),__FILE__,__LINE__);
107 return sizep;
108};
109
110// The type class returned as an enumeration type.
112 switch (myId) {
113 case NC_BYTE : return nc_BYTE;
114 case NC_UBYTE : return nc_UBYTE;
115 case NC_CHAR : return nc_CHAR;
116 case NC_SHORT : return nc_SHORT;
117 case NC_USHORT : return nc_USHORT;
118 case NC_INT : return nc_INT;
119 case NC_UINT : return nc_UINT;
120 case NC_INT64 : return nc_INT64;
121 case NC_UINT64 : return nc_UINT64;
122 case NC_FLOAT : return nc_FLOAT;
123 case NC_DOUBLE : return nc_DOUBLE;
124 case NC_STRING : return nc_STRING;
125 default:
126 // this is a user defined type
127 // establish its type class, ie whether it is: NC_VLEN, NC_OPAQUE, NC_ENUM, or NC_COMPOUND.
128 char* name=NULL;
129 size_t* sizep=NULL;
130 nc_type* base_nc_typep=NULL;
131 size_t* nfieldsp=NULL;
132 int classp;
133 ncCheck(nc_inq_user_type(groupId,myId,name,sizep,base_nc_typep,nfieldsp,&classp),__FILE__,__LINE__);
134 return static_cast<ncType>(classp);
135 }
136}
137
138// The type class returned as a string.
140 ncType typeClass=getTypeClass();
141 switch (typeClass) {
142 case nc_BYTE : return string("nc_BYTE");
143 case nc_UBYTE : return string("nc_UBYTE");
144 case nc_CHAR : return string("nc_CHAR");
145 case nc_SHORT : return string("nc_SHORT");
146 case nc_USHORT : return string("nc_USHORT");
147 case nc_INT : return string("nc_INT");
148 case nc_UINT : return string("nc_UINT");
149 case nc_INT64 : return string("nc_INT64");
150 case nc_UINT64 : return string("nc_UINT64");
151 case nc_FLOAT : return string("nc_FLOAT");
152 case nc_DOUBLE : return string("nc_DOUBLE");
153 case nc_STRING : return string("nc_STRING");
154 case nc_VLEN : return string("nc_VLEN");
155 case nc_OPAQUE : return string("nc_OPAQUE");
156 case nc_ENUM : return string("nc_ENUM");
157 case nc_COMPOUND: return string("nc_COMPOUND");
158 }
159 // we never get here!
160 return "Dummy";
161}
162
164 NcType::ncType typeClass {getTypeClass()};
165 return (
166 typeClass == NcType::nc_VLEN
167 || typeClass == NcType::nc_OPAQUE
168 || typeClass == NcType::nc_ENUM
169 || typeClass == NcType::nc_COMPOUND
170 );
171}
Class represents a netCDF group.
Definition ncGroup.h:26
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
NcGroup()
Constructor generates a null object.
Definition ncGroup.cpp:52
@ ParentsAndCurrent
Select from contents of current and parents groups.
Definition ncGroup.h:51
Base class inherited by NcOpaque, NcVlen, NcCompound and NcEnum classes.
Definition ncType.h:15
int groupId
the group Id
Definition ncType.h:155
ncType getTypeClass() const
The type class returned as enumeration type.
Definition ncType.cpp:111
netCDF::NcGroup getParentGroup() const
Gets parent group.
Definition ncType.cpp:88
friend bool operator>(const NcType &lhs, const NcType &rhs)
comparator operator
Definition ncType.cpp:17
NcType()
Constructor generates a null object.
Definition ncType.cpp:43
size_t getSize() const
The size in bytes.
Definition ncType.cpp:103
friend bool operator<(const NcType &lhs, const NcType &rhs)
comparator operator
Definition ncType.cpp:11
bool operator==(const NcType &) const
equivalence operator
Definition ncType.cpp:73
nc_type myId
the type Id
Definition ncType.h:152
std::string getTypeClassName() const
Return a string containing the name of the enumerated type.
Definition ncType.cpp:139
bool isNonPrimitive() const
Return true if this is not a primitive type.
Definition ncType.cpp:163
NcType & operator=(const NcType &rhs)
assignment operator
Definition ncType.cpp:26
ncType
List of netCDF types that can be represented.
Definition ncType.h:26
@ nc_DOUBLE
double precision floating point number
Definition ncType.h:32
@ nc_UBYTE
unsigned 1 byte int
Definition ncType.h:33
@ nc_OPAQUE
"NcOpaque type"
Definition ncType.h:40
@ nc_CHAR
ISO/ASCII character.
Definition ncType.h:28
@ nc_SHORT
signed 2 byte integer
Definition ncType.h:29
@ nc_BYTE
signed 1 byte integer
Definition ncType.h:27
@ nc_UINT64
unsigned 8-byte int
Definition ncType.h:37
@ nc_VLEN
"NcVlen type"
Definition ncType.h:39
@ nc_UINT
unsigned 4-byte int
Definition ncType.h:35
@ nc_INT
signed 4 byte integer
Definition ncType.h:30
@ nc_ENUM
"NcEnum type"
Definition ncType.h:41
@ nc_STRING
string
Definition ncType.h:38
@ nc_INT64
signed 8-byte int
Definition ncType.h:36
@ nc_USHORT
unsigned 2-byte int
Definition ncType.h:34
@ nc_FLOAT
single precision floating point number
Definition ncType.h:31
@ nc_COMPOUND
"NcCompound type"
Definition ncType.h:42
std::string getName() const
The name of this type.
Definition ncType.cpp:93
bool operator!=(const NcType &) const
!= operator
Definition ncType.cpp:82
nc_type getId() const
The netCDF Id of this type.
Definition ncType.h:88
C++ API for netCDF4.
Definition ncAtt.h:10
void ncCheck(int retCode, const char *file, int line)
Function checks error code and if necessary throws an exception.
Definition ncCheck.cpp:11

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