NetCDF-C++ 4.3.1
ncDim.cpp
1#include "ncDim.h"
2#include "ncGroup.h"
3#include "ncCheck.h"
4#include <algorithm>
5using namespace std;
6
7
8namespace netCDF {
9 // Global comparator operator ==============
10 // comparator operator
11 bool operator<(const NcDim& lhs,const NcDim& rhs)
12 {
13 return false;
14 }
15
16 // comparator operator
17 bool operator>(const NcDim& lhs,const NcDim& rhs)
18 {
19 return true;
20 }
21}
22
23using namespace netCDF;
24
25// assignment operator
26NcDim& NcDim::operator=(const NcDim & rhs)
27{
28 nullObject = rhs.nullObject;
29 myId = rhs.myId;
30 groupId = rhs.groupId;
31 return *this;
32}
33
34// The copy constructor.
35NcDim::NcDim(const NcDim& rhs):
36 nullObject(rhs.nullObject),
37 myId(rhs.myId),
38 groupId(rhs.groupId)
39{}
40
41
42// equivalence operator
43bool NcDim::operator==(const NcDim& rhs) const
44{
45 if(nullObject)
46 return nullObject == rhs.nullObject;
47 else
48 return myId == rhs.myId && groupId == rhs.groupId;
49}
50
51// != operator
52bool NcDim::operator!=(const NcDim & rhs) const
53{
54 return !(*this == rhs);
55}
56
57
58// Gets parent group.
60 return NcGroup(groupId);
61}
62
63// Constructor generates a null object.
65 nullObject(true)
66{}
67
68// Constructor for a dimension (must already exist in the netCDF file.)
69NcDim::NcDim(const NcGroup& grp, int dimId) :
70 nullObject(false)
71{
72 groupId = grp.getId();
73 myId = dimId;
74}
75
76// gets the size of the dimension, for unlimited, this is the current number of records.
77size_t NcDim::getSize() const
78{
79 size_t dimSize;
80 ncCheck(nc_inq_dimlen(groupId, myId, &dimSize),__FILE__,__LINE__);
81 return dimSize;
82}
83
84
85// returns true if this dimension is unlimited.
87{
88 int numlimdims;
89 int* unlimdimidsp=NULL;
90 // get the number of unlimited dimensions
91 ncCheck(nc_inq_unlimdims(groupId,&numlimdims,unlimdimidsp),__FILE__,__LINE__);
92 if (numlimdims){
93 // get all the unlimited dimension ids in this group
94 vector<int> unlimdimid(numlimdims);
95 ncCheck(nc_inq_unlimdims(groupId,&numlimdims,&unlimdimid[0]),__FILE__,__LINE__);
96 vector<int>::iterator it;
97 // now look to see if this dimension is unlimited
98 it = find(unlimdimid.begin(),unlimdimid.end(),myId);
99 return it != unlimdimid.end();
100 }
101 return false;
102}
103
104
105// gets the name of the dimension.
106string NcDim::getName() const
107{
108 char dimName[NC_MAX_NAME+1];
109 ncCheck(nc_inq_dimname(groupId, myId, dimName),__FILE__,__LINE__);
110 return string(dimName);
111}
112
113// renames this dimension.
114void NcDim::rename(const string& name)
115{
116 ncCheck(nc_rename_dim(groupId, myId, name.c_str()),__FILE__,__LINE__);
117}
Class represents a netCDF dimension.
Definition: ncDim.h:13
std::string getName() const
The name of this dimension.
Definition: ncDim.cpp:106
size_t getSize() const
The size of the dimension; for unlimited, this is the number of records written so far.
Definition: ncDim.cpp:77
bool isUnlimited() const
Returns true if this is an unlimited dimension.
Definition: ncDim.cpp:86
bool operator!=(const NcDim &rhs) const
!= operator
Definition: ncDim.cpp:52
NcGroup getParentGroup() const
Gets a NcGroup object of the parent group.
Definition: ncDim.cpp:59
bool operator==(const NcDim &rhs) const
equivalence operator
Definition: ncDim.cpp:43
void rename(const std::string &newName)
renames the dimension
Definition: ncDim.cpp:114
NcDim()
Constructor generates a null object.
Definition: ncDim.cpp:64
Class represents a netCDF group.
Definition: ncGroup.h:28
int getId() const
Gets the group id.
Definition: ncGroup.cpp:141
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.