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 
87  //
88  int GetVarID() const {return(_varid); };
89 
103  //
104  void GetAtt(string name, std::vector <double> &values) const;
105  void GetAtt(string name, std::vector <long> &values) const;
106  void GetAtt(string name, string &values) const;
107 
112  //
113  void SetAtt(string name, const std::vector <double> &values) {
114  _flt_atts.push_back(make_pair(name, values));
115  }
116  void SetAtt(string name, const std::vector <long> &values) {
117  _int_atts.push_back(make_pair(name, values));
118  }
119  void SetAtt(string name, const string &values) {
120  _str_atts.push_back(make_pair(name, values));
121  }
122 
123  VDF_API friend std::ostream &operator<<(std::ostream &o, const Variable &var);
124  VDF_API friend bool operator==(const Variable &v1, const Variable &v2) {
125  return(
126  (v1._name == v2._name) &&
127  (v1._dimnames == v2._dimnames) &&
128  (v1._flt_atts == v2._flt_atts) &&
129  (v1._int_atts == v2._int_atts) &&
130  (v1._str_atts == v2._str_atts) &&
131  (v1._type == v2._type) &&
132  (v1._varid == v2._varid)
133  );
134  }
135 
136 
137  private:
138  string _name; // variable name
139  std::vector <string> _dimnames; // order list of dimension names
140  std::vector <std::pair <string, std::vector <double> > > _flt_atts;
141  std::vector <std::pair <string, std::vector <long> > > _int_atts;
142  std::vector <std::pair <string, string> > _str_atts;
143  int _type; // netCDF variable type
144  int _varid; // netCDF variable id
145  };
146 
156  int Initialize(string path);
157 
174  int OpenRead(const NetCDFSimple::Variable &variable);
175 
194  int Read(
195  const size_t start[], const size_t count[], float *data, int fd = 0
196  ) const;
197  int Read(
198  const size_t start[], const size_t count[], int *data, int fd = 0
199  ) const;
200  int Read(
201  const size_t start[], const size_t count[], char *data, int fd = 0
202  ) const;
203 
208  //
209  int Close(int fd = 0);
210 
218  const std::vector <NetCDFSimple::Variable> &GetVariables() const {
219  return(_variables);
220  };
221 
233  void GetDimensions(std::vector <string> &names, std::vector <size_t> &dims) const;
234 
245  //
246  string DimName(int id) const;
247 
259  //
260  size_t DimLen(string name) const;
261 
272  //
273  int DimId(string name) const;
274 
283  //
284  std::vector <string> GetAttNames() const;
285 
293  //
294  int GetAttType(string name) const;
295 
309  //
310  void GetAtt(string name, std::vector <double> &values) const;
311  void GetAtt(string name, std::vector <long> &values) const;
312  void GetAtt(string name, string &values) const;
313 
326  static bool IsNCTypeInt(int type);
327 
338  static bool IsNCTypeFloat(int type);
339 
350  static bool IsNCTypeText(int type);
351 
352  VDF_API friend std::ostream &operator<<(std::ostream &o, const NetCDFSimple &nc);
353 
354 private:
355  int _ncid;
356  std::map <int, int> _ovr_table; // open variable map: fd -> varid
357  string _path;
358  size_t _chsz;
359  std::vector <string> _dimnames;
360  std::vector <size_t> _dims;
361  std::vector <string> _unlimited_dimnames;
362  std::vector <std::pair <string, std::vector <double> > > _flt_atts;
363  std::vector <std::pair <string, std::vector <long> > > _int_atts;
364  std::vector <std::pair <string, string> > _str_atts;
365  std::vector <NetCDFSimple::Variable> _variables;
366 
367  int _GetAtts(
368  int ncid, int varid,
369  std::vector <std::pair <string, std::vector <double> > > &flt_atts,
370  std::vector <std::pair <string, std::vector <long> > > &int_atts,
371  std::vector <std::pair <string, string> > &str_atts
372  );
373 
374 };
375 
376 };
377 #endif
#define VDF_API
Definition: common.h:61
void SetAtt(string name, const string &values)
Definition: NetCDFSimple.h:119
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:88
string GetName() const
Return the variable's name.
Definition: NetCDFSimple.h:61
const std::vector< NetCDFSimple::Variable > & GetVariables() const
Definition: NetCDFSimple.h:218
VetsUtil base class.
Definition: MyBase.h:68
VDF_API friend bool operator==(const Variable &v1, const Variable &v2)
Definition: NetCDFSimple.h:124
void SetAtt(string name, const std::vector< long > &values)
Definition: NetCDFSimple.h:116
NetCDFSimple API interface.
Definition: NetCDFSimple.h:33
void SetAtt(string name, const std::vector< double > &values)
Definition: NetCDFSimple.h:113