NetCDF4 C++ API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
NcUtil.h
Go to the documentation of this file.
1 
13 #ifndef INC_netcdf_NcUtil_h
14 #define INC_netcdf_NcUtil_h
15 
16 #include "NcType.h"
17 #include <vector>
18 #include <algorithm>
19 
20 namespace netcdf {
21 
22 
23 class NcDim;
24 class NcVar;
25 class NcGroup;
26 class NcFile;
27 
28 
30 inline bool IsAtomicType ( NcType::ncType const typeClass )
31 {
32  return (typeClass < NcType::nc_VLEN);
33 }
34 
36 inline bool IsUserDefinedType ( NcType::ncType const typeClass )
37 {
38  return ( typeClass == NcType::nc_VLEN ||
39  typeClass == NcType::nc_OPAQUE ||
40  typeClass == NcType::nc_ENUM ||
41  typeClass == NcType::nc_COMPOUND );
42 }
43 
45 template
46 <
47  class TNCT // type of the NcVar object - atomic types only!
48 >
49 typename TNCT::value_type* MakeCArray(
50  TNCT const & ncType,
51  int const size,
52  typename TNCT::value_type const initValue = 0)
53 {
54  if ( !IsAtomicType( ncType.getTypeClass() ) )
55  NcEXCEPTION( "Not an atomic NetCDF4 type." );
56 
57  typedef typename TNCT::value_type T;
58  T* ca = new T [ size ];
59  for ( T* pCA = ca; pCA != ca + size; ++pCA )
60  *pCA = initValue;
61  return ca;
62 }
63 
65 template< class TSrc, class TDest >
66 void ToVector(
67  TSrc const srcBegin,
68  TSrc const srcEnd,
69  std::vector<TDest> & dest)
70 {
71  dest.clear();
72  for ( TSrc const * i = srcBegin; i != srcEnd; ++i )
73  dest.push_back( TDest(*i) );
74 }
75 
77 template< class TSrc, class TDest >
78 void ToVector(
79  TSrc const srcBegin,
80  std::vector<TDest> & dest,
81  std::size_t const count)
82 {
83  dest.clear();
84  dest.resize(count);
85  TSrc /* C ptr */ iSrc = srcBegin;
86  typename std::vector<TDest>::iterator iDest = dest.begin();
87  for ( ; iDest != dest.end(); ++iSrc, ++iDest )
88  *iDest = *iSrc;
89 }
90 
92 template< class T >
94  std::vector<T> const & src)
95 {
96  T* dest = new T [ src.size() ];
97  std::copy( src.begin(), src.end(), dest );
98  return dest;
99 }
100 
102 char** MakeCStringArray ( int const numStrings, short const size );
103 char** ToCStringArray ( std::vector< std::string > const & strVec );
104 void FreeCStringArray ( char** & cStrArray, std::size_t const numStrings );
105 
107 void GetNcDims (
108  int const groupID, int const varID,
109  std::vector<NcDim> & ncDims);
110 
111 bool IsRecordVariable ( int const groupID, int const varID );
112 
113 
118 std::size_t GetTotalSize( NcVar const & var );
119 std::size_t GetTotalSize( int const groupID, int const varID );
120 std::size_t GetTotalSize( int const groupID, int const varID,
121  std::vector< std::size_t > const & count ); // edge lengths
122 
123 
125 void GetTypeName (
126  NcGroup const & group,
127  NcType::ncType const typeID,
128  std::string & name );
129 
131 NcGroup GetGroup ( NcFile const & ncFile, int const groupID );
132 NcVar GetVar ( NcFile const & ncFile, int const groupID, int const varID );
133 std::string GetVarName( int const groupID, int const varID );
134 
136 std::size_t GetDimSize (
137  int const groupID,
138  int const varID,
139  int const dimIndex);
140 
141 
142 
144 
145 
146 } // namespace netcdf
147 
148 #endif // INC_netcdf_NcUtil_h
TNCT::value_type * MakeCArray(TNCT const &ncType, int const size, typename TNCT::value_type const initValue=0)
Create an empty C-ptr array from the NcType object and size.
Definition: NcUtil.h:49
ncType
Definition: NcType.h:56
T * FromVector(std::vector< T > const &src)
Create a C-array from a std::vector.
Definition: NcUtil.h:93
void GetNcDims(int const groupID, int const varID, std::vector< NcDim > &ncDims)
Get the dimensions for a variable as vector of NcDim objects.
Definition: NcUtil.cpp:159
NcVar GetVar(NcFile const &ncFile, int const groupID, int const varID)
Definition: NcUtil.cpp:152
bool IsRecordVariable(int const groupID, int const varID)
Definition: NcUtil.cpp:75
char ** MakeCStringArray(int const numStrings, short const size)
Allocates array and copies strings.
Definition: NcUtil.cpp:35
char ** ToCStringArray(std::vector< std::string > const &strVec)
Definition: NcUtil.cpp:47
&quot;NcEnum type&quot;
Definition: NcType.h:73
void FreeCStringArray(char **&cStrArray, std::size_t const numStrings)
Definition: NcUtil.cpp:66
&quot;NcVlen type&quot;
Definition: NcType.h:71
bool IsAtomicType(NcType::ncType const typeClass)
true if is a netCDF atomic type
Definition: NcUtil.h:30
void GetTypeName(NcGroup const &group, NcType::ncType const typeID, std::string &name)
Gets the netCDF name of existing type object from the type ID.
Definition: NcUtil.cpp:125
std::size_t GetDimSize(int const groupID, int const varID, int const dimIndex)
Get the size of a variable&#39;s dimension.
Definition: NcUtil.cpp:177
NcGroup GetGroup(NcFile const &ncFile, int const groupID)
Get item info from netCDF ID numbers.
Definition: NcUtil.cpp:135
#define NcEXCEPTION(a_)
Definition: NcException.h:67
bool IsUserDefinedType(NcType::ncType const typeClass)
true if is a netCDF user-defined type
Definition: NcUtil.h:36
std::string GetVarName(int const groupID, int const varID)
Definition: NcUtil.cpp:143
std::size_t GetTotalSize(NcVar const &var)
Get total number of elements of data from the dimensions of a variable. throws error if netcdf error...
Definition: NcUtil.cpp:82
void ToVector(TSrc const srcBegin, TSrc const srcEnd, std::vector< TDest > &dest)
Copy a C-ptr array to a std::vector.
Definition: NcUtil.h:66
&quot;NcCompound type&quot;
Definition: NcType.h:74
&quot;NcOpaque type&quot;
Definition: NcType.h:72