NetCDF-C++  4.3.1-developer
ncDim.cpp
1 #include "ncDim.h"
2 #include "ncGroup.h"
3 #include "ncCheck.h"
4 #include <algorithm>
5 using namespace std;
6 
7 
8 namespace 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 
23 using namespace netCDF;
24 
25 // assignment operator
26 NcDim& 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.
35 NcDim::NcDim(const NcDim& rhs):
36  nullObject(rhs.nullObject),
37  myId(rhs.myId),
38  groupId(rhs.groupId)
39 {}
40 
41 
42 // equivalence operator
43 bool 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
52 bool 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.)
69 NcDim::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.
77 size_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.
86 bool NcDim::isUnlimited() const
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.
106 const string 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.
114 void NcDim::rename(const string& name)
115 {
116  ncCheck(nc_rename_dim(groupId, myId, name.c_str()),__FILE__,__LINE__);
117 }
118 
119 
C++ API for netCDF4.
Definition: ncAtt.h:9
NcGroup getParentGroup() const
Gets a NcGroup object of the parent group.
Definition: ncDim.cpp:59
bool operator!=(const NcDim &rhs) const
!= operator
Definition: ncDim.cpp:52
int getId() const
Gets the group id.
Definition: ncGroup.cpp:141
NcDim()
Constructor generates a null object.
Definition: ncDim.cpp:64
Class represents a netCDF group.
Definition: ncGroup.h:27
Class represents a netCDF dimension.
Definition: ncDim.h:13
bool isUnlimited() const
Returns true if this is an unlimited dimension.
Definition: ncDim.cpp:86
void rename(const std::string &newName)
renames the dimension
Definition: ncDim.cpp:114
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 NcDim &rhs) const
equivalence operator
Definition: ncDim.cpp:43
const 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

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