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