VAPoR  0.1
NetCDFSimple.h
Go to the documentation of this file.
1 //
2 // $Id$
3 //
4 
5 #ifndef _NetCDFSimple_h_
6 #define _NetCDFSimple_h_
7 
8 #include <vector>
9 #include <map>
10 
11 #include <sstream>
12 #include <vapor/MyBase.h>
13 
14 namespace VAPoR {
15 
16 //
32 //
34 public:
35  NetCDFSimple();
36  virtual ~NetCDFSimple();
37 
42  //
43  class Variable {
44  public:
45  Variable();
46 
54  Variable(
55  string varname, std::vector <string> dimnames,
56  int varid, int type
57  );
58 
60  //
61  string GetName() const {return (_name);};
62 
67  //
68  std::vector <string> GetAttNames() const;
69 
74  std::vector <string> GetDimNames() const {return(_dimnames); };
75 
83  //
84  int GetAttType(string name) const;
85 
88  int GetXType() const {return(_type); };
89 
91  //
92  int GetVarID() const {return(_varid); };
93 
107  //
108  void GetAtt(string name, std::vector <double> &values) const;
109  void GetAtt(string name, std::vector <long> &values) const;
110  void GetAtt(string name, string &values) const;
111 
116  //
117  void SetAtt(string name, const std::vector <double> &values) {
118  _flt_atts.push_back(make_pair(name, values));
119  }
120  void SetAtt(string name, const std::vector <long> &values) {
121  _int_atts.push_back(make_pair(name, values));
122  }
123  void SetAtt(string name, const string &values) {
124  _str_atts.push_back(make_pair(name, values));
125  }
126 
127  VDF_API friend std::ostream &operator<<(std::ostream &o, const Variable &var);
128  VDF_API friend bool operator==(const Variable &v1, const Variable &v2) {
129  return(
130  (v1._name == v2._name) &&
131  (v1._dimnames == v2._dimnames) &&
132  (v1._flt_atts == v2._flt_atts) &&
133  (v1._int_atts == v2._int_atts) &&
134  (v1._str_atts == v2._str_atts) &&
135  (v1._type == v2._type) &&
136  (v1._varid == v2._varid)
137  );
138  }
139 
140 
141  private:
142  string _name; // variable name
143  std::vector <string> _dimnames; // order list of dimension names
144  std::vector <std::pair <string, std::vector <double> > > _flt_atts;
145  std::vector <std::pair <string, std::vector <long> > > _int_atts;
146  std::vector <std::pair <string, string> > _str_atts;
147  int _type; // netCDF variable type
148  int _varid; // netCDF variable id
149  };
150 
160  int Initialize(string path);
161 
178  int OpenRead(const NetCDFSimple::Variable &variable);
179 
198  int Read(
199  const size_t start[], const size_t count[], float *data, int fd = 0
200  ) const;
201  int Read(
202  const size_t start[], const size_t count[], int *data, int fd = 0
203  ) const;
204  int Read(
205  const size_t start[], const size_t count[], char *data, int fd = 0
206  ) const;
207 
212  //
213  int Close(int fd = 0);
214 
222  const std::vector <NetCDFSimple::Variable> &GetVariables() const {
223  return(_variables);
224  };
225 
237  void GetDimensions(std::vector <string> &names, std::vector <size_t> &dims) const;
238 
249  //
250  string DimName(int id) const;
251 
263  //
264  size_t DimLen(string name) const;
265 
276  //
277  int DimId(string name) const;
278 
287  //
288  std::vector <string> GetAttNames() const;
289 
297  //
298  int GetAttType(string name) const;
299 
313  //
314  void GetAtt(string name, std::vector <double> &values) const;
315  void GetAtt(string name, std::vector <long> &values) const;
316  void GetAtt(string name, string &values) const;
317 
330  static bool IsNCTypeInt(int type);
331 
342  static bool IsNCTypeFloat(int type);
343 
354  static bool IsNCTypeText(int type);
355 
356  VDF_API friend std::ostream &operator<<(std::ostream &o, const NetCDFSimple &nc);
357 
358 private:
359  int _ncid;
360  std::map <int, int> _ovr_table; // open variable map: fd -> varid
361  string _path;
362  size_t _chsz;
363  std::vector <string> _dimnames;
364  std::vector <size_t> _dims;
365  std::vector <string> _unlimited_dimnames;
366  std::vector <std::pair <string, std::vector <double> > > _flt_atts;
367  std::vector <std::pair <string, std::vector <long> > > _int_atts;
368  std::vector <std::pair <string, string> > _str_atts;
369  std::vector <NetCDFSimple::Variable> _variables;
370 
371  int _GetAtts(
372  int ncid, int varid,
373  std::vector <std::pair <string, std::vector <double> > > &flt_atts,
374  std::vector <std::pair <string, std::vector <long> > > &int_atts,
375  std::vector <std::pair <string, string> > &str_atts
376  );
377 
378 };
379 
380 };
381 #endif
#define VDF_API
Definition: common.h:61
void SetAtt(string name, const string &values)
Definition: NetCDFSimple.h:123
NetCDFSimple API interface.
Definition: NetCDFSimple.h:43
std::vector< string > GetDimNames() const
Definition: NetCDFSimple.h:74
int GetVarID() const
Return the netCDF variable ID for this variable.
Definition: NetCDFSimple.h:92
string GetName() const
Return the variable's name.
Definition: NetCDFSimple.h:61
const std::vector< NetCDFSimple::Variable > & GetVariables() const
Definition: NetCDFSimple.h:222
VetsUtil base class.
Definition: MyBase.h:68
VDF_API friend bool operator==(const Variable &v1, const Variable &v2)
Definition: NetCDFSimple.h:128
void SetAtt(string name, const std::vector< long > &values)
Definition: NetCDFSimple.h:120
NetCDFSimple API interface.
Definition: NetCDFSimple.h:33
void SetAtt(string name, const std::vector< double > &values)
Definition: NetCDFSimple.h:117