VAPoR  3.0.0
DC.h
Go to the documentation of this file.
1 #include <vector>
2 #include <algorithm>
3 #include <map>
4 #include <iostream>
5 #include <vapor/MyBase.h>
6 
7 #ifndef _DC_H_
8 #define _DC_H_
9 
10 namespace VAPoR {
11 
150 class VDF_API DC : public VetsUtil::MyBase {
151 public:
152 
154  //
155  enum XType {INVALID = -1, FLOAT, DOUBLE, INT32, INT64, TEXT};
156 
161  class Dimension {
162  public:
163 
167  _name.clear();
168  _length = 0;
169  _axis = 0;
170  };
171 
180  Dimension(std::string name, size_t length, int axis) {
181  _name = name;
182  _length = length;
183  _axis = axis;
184  };
185  virtual ~Dimension() {};
186 
187  void Clear() {
188  _name.clear();
189  _length = 0;
190  _axis = 0;
191  }
192 
194  //
195  string GetName() const {return (_name); };
196 
198  //
199  void SetName(string name) {_name = name;};
200 
202  //
203  size_t GetLength() const {return (_length); };
204 
206  //
207  void SetLength(size_t length) {_length = length;};
208 
210  //
211  int GetAxis() const {return (_axis); };
212 
214  //
215  void SetAxis(int axis) {_axis = axis;};
216 
217  bool operator==(const Dimension &rhs) const {
218  return(_name == rhs._name && _length==rhs._length && _axis==rhs._axis);
219  }
220  bool operator!=(const Dimension &rhs) const {
221  return(! (*this==rhs));
222  }
223  friend std::ostream &operator<<(
224  std::ostream &o, const Dimension &dimension
225  );
226 
227  private:
228  string _name;
229  size_t _length;
230  int _axis;
231  };
232 
237  class Attribute {
238  public:
239  Attribute() {_name = ""; _type = FLOAT; _values.clear(); };
240 
246  //
247  Attribute(string name, XType type, const std::vector <float> &values);
248  Attribute(string name, XType type, const std::vector <double> &values);
249  Attribute(string name, XType type, const std::vector <int> &values);
250  Attribute(string name, XType type, const std::vector <long> &values);
251  Attribute(string name, XType type, const string &values);
252  Attribute(string name, XType type) {
253  _name = name; _type = type; _values.clear();
254  };
255  virtual ~Attribute() {};
256 
258  //
259  string GetName() const {return (_name); };
260 
262  //
263  XType GetXType() const {return (_type);};
264 
270  void GetValues(std::vector <float> &values) const;
271  void GetValues(std::vector <double> &values) const;
272  void GetValues(std::vector <int> &values) const;
273  void GetValues(std::vector <long> &values) const;
274  void GetValues(string &values) const;
275 
281  void SetValues(const std::vector <float> &values);
282  void SetValues(const std::vector <double> &values);
283  void SetValues(const std::vector <int> &values);
284  void SetValues(const std::vector <long> &values);
285  void SetValues(const string &values);
286 
287  friend std::ostream &operator<<(std::ostream &o, const Attribute &attr);
288 
289  private:
290  string _name;
291  XType _type;
292  union podunion {
293  float f;
294  double d;
295  int i;
296  long l;
297  char c;
298  };
299  std::vector <podunion> _values;
300 
301  };
302 
306  //
307  class BaseVar {
308  public:
309 
313  _name.clear();
314  _dimensions.clear();
315  _units = "";
316  _type = FLOAT;
317  _wname = "";
318  _cratios.clear();
319  _bs.clear();
320  _periodic.clear();
321  _atts.clear();
322  }
323 
350  string name, std::vector <DC::Dimension> dimensions,
351  string units, XType type,
352  std::vector <size_t> bs, string wname,
353  std::vector <size_t> cratios, std::vector <bool> periodic
354  ) :
355  _name(name),
356  _dimensions(dimensions),
357  _units(units),
358  _type(type),
359  _wname(wname),
360  _cratios(cratios),
361  _bs(bs),
362  _periodic(periodic)
363  {
364  if (_cratios.size()==0) _cratios.push_back(1);
365  for (int i=_bs.size(); i<_dimensions.size(); i++) _bs.push_back(1);
366  };
367 
384  BaseVar(
385  string name, std::vector <DC::Dimension> dimensions,
386  string units, XType type, std::vector <bool> periodic
387  );
388 
389  virtual ~BaseVar() {};
390 
392  //
393  string GetName() const {return (_name); };
394  void SetName(string name) {_name = name; };
395 
397  //
398  std::vector <DC::Dimension> GetDimensions() const {return (_dimensions); };
399  void SetDimensions(std::vector <DC::Dimension> dimensions) {
400  _dimensions = dimensions;
401  };
402 
404  //
405  string GetUnits() const {return (_units); };
406  void SetUnits(string units) {_units = units; };
407 
409  //
410  XType GetXType() const {return (_type); };
411  void SetXType(XType type) {_type = type; };
412 
413 
415  //
416  std::vector <size_t> GetBS() const {return (_bs); };
417  void SetBS(std::vector <size_t> bs) {_bs = bs; };
418 
420  //
421  string GetWName() const {return (_wname); };
422  void SetWName(string wname) {_wname = wname; };
423 
425  //
426  std::vector <size_t> GetCRatios() const {return (_cratios); };
427  void SetCRatios(std::vector <size_t> cratios) {
428  _cratios = cratios;
429  if (_cratios.size()==0) _cratios.push_back(1);
430  };
431 
433  //
434  std::vector <bool> GetPeriodic() const {return (_periodic); };
435  void SetPeriodic(std::vector <bool> periodic) { _periodic = periodic; };
436 
438  //
439  std::map <string, Attribute> GetAttributes() const {return (_atts); };
440  void SetAttributes(std::map <string, Attribute> &atts) {_atts = atts; };
441 
443  //
444  bool IsCompressed() const { return (! _wname.empty()); };
445 
447  //
448  bool IsTimeVarying() const {
449  for (int i=0; i<_dimensions.size(); i++) {
450  if (_dimensions[i].GetAxis() == 3) return(true);
451  }
452  return(false);
453  };
454 
455  friend std::ostream &operator<<(std::ostream &o, const BaseVar &var);
456 
457  private:
458  string _name;
459  std::vector <DC::Dimension> _dimensions;
460  string _units;
461  XType _type;
462  string _wname;
463  std::vector <size_t> _cratios;
464  vector <size_t> _bs;
465  std::vector <bool> _periodic;
466  std::map <string, Attribute> _atts;
467  };
468 
471  //
472  class CoordVar : public BaseVar {
473  public:
474 
476  //
478  _axis = 0;
479  _uniform = false;
480  }
481 
482 
495  //
497  string name, std::vector <DC::Dimension> dimensions,
498  string units, XType type,
499  std::vector <size_t> bs, string wname,
500  std::vector <size_t> cratios, std::vector <bool> periodic,
501  int axis, bool uniform
502  ) :
503  BaseVar(
504  name, dimensions, units, type, bs,
505  wname, cratios,
506  periodic
507  ),
508  _axis(axis),
509  _uniform(uniform)
510  {}
511 
521  //
523  string name, std::vector <DC::Dimension> dimensions,
524  string units, XType type,
525  std::vector <bool> periodic,
526  int axis, bool uniform
527  ) :
528  BaseVar(name, dimensions, units, type, periodic),
529  _axis(axis),
530  _uniform(uniform)
531  {}
532 
533  virtual ~CoordVar() {};
534 
536  //
537  int GetAxis() const {return (_axis); };
538  void SetAxis(int axis) {_axis = axis; };
539 
541  //
542  bool GetUniform() const {return (_uniform); };
543  void SetUniform(bool uniform) {_uniform = uniform; };
544 
545  friend std::ostream &operator<<(std::ostream &o, const CoordVar &var);
546 
547  private:
548  int _axis;
549  bool _uniform;
550  };
551 
557  class DataVar : public BaseVar {
558  public:
559 
561  //
562  DataVar() : BaseVar() {
563  _coordvars.clear();
564  _maskvar = "";
565  _has_missing = false;
566  _missing_value = 0.0;
567  }
568 
585  string name, std::vector <DC::Dimension> dimensions,
586  string units, XType type,
587  std::vector <size_t> bs, string wname,
588  std::vector <size_t> cratios,
589  std::vector <bool> periodic, std::vector <string> coordvars,
590  double missing_value
591  ) :
592  BaseVar(
593  name, dimensions, units, type,
594  bs, wname, cratios, periodic
595  ),
596  _coordvars(coordvars),
597  _maskvar(""),
598  _has_missing(true),
599  _missing_value(missing_value)
600  {}
601 
623  string name, std::vector <DC::Dimension> dimensions,
624  string units, XType type,
625  std::vector <size_t> bs, string wname,
626  std::vector <size_t> cratios,
627  std::vector <bool> periodic, std::vector <string> coordvars,
628  double missing_value, string maskvar
629  ) :
630  BaseVar(
631  name, dimensions, units, type,
632  bs, wname, cratios, periodic
633  ),
634  _coordvars(coordvars),
635  _maskvar(maskvar),
636  _has_missing(true),
637  _missing_value(missing_value)
638  {}
639 
652  string name, std::vector <DC::Dimension> dimensions,
653  string units, XType type,
654  std::vector <size_t> bs, string wname,
655  std::vector <size_t> cratios,
656  std::vector <bool> periodic, std::vector <string> coordvars
657  ) :
658  BaseVar(
659  name, dimensions, units, type,
660  bs, wname, cratios, periodic
661  ),
662  _coordvars(coordvars),
663  _maskvar(""),
664  _has_missing(false),
665  _missing_value(0.0)
666  {}
667 
679  string name, std::vector <DC::Dimension> dimensions,
680  string units, XType type,
681  std::vector <bool> periodic, std::vector <string> coordvars,
682  double missing_value
683  ) :
684  BaseVar(
685  name, dimensions, units, type, periodic
686  ),
687  _coordvars(coordvars),
688  _maskvar(""),
689  _has_missing(true),
690  _missing_value(missing_value)
691  {}
692 
712  string name, std::vector <DC::Dimension> dimensions,
713  string units, XType type,
714  std::vector <bool> periodic, std::vector <string> coordvars,
715  double missing_value, string maskvar
716  ) :
717  BaseVar(
718  name, dimensions, units, type, periodic
719  ),
720  _coordvars(coordvars),
721  _maskvar(maskvar),
722  _has_missing(true),
723  _missing_value(missing_value)
724  {}
725 
736  string name, std::vector <DC::Dimension> dimensions,
737  string units, XType type,
738  std::vector <bool> periodic, std::vector <string> coordvars
739  ) :
740  BaseVar(
741  name, dimensions, units, type, periodic
742  ),
743  _coordvars(coordvars),
744  _maskvar(""),
745  _has_missing(false),
746  _missing_value(0.0)
747  {}
748 
749 
750  virtual ~DataVar() {};
751 
753  //
754  std::vector <string> GetCoordvars() const {return (_coordvars); };
755  void SetCoordvars(std::vector <string> coordvars) {_coordvars = coordvars; };
756 
758  //
759  string GetMaskvar() const {return (_maskvar); };
760  void SetMaskvar(string maskvar) {_maskvar = maskvar; };
761 
763  //
764  bool GetHasMissing() const {return (_has_missing); };
765  void SetHasMissing(bool has_missing) {_has_missing = has_missing; };
766 
768  //
769  double GetMissingValue() const {return (_missing_value); };
770  void SetMissingValue(double missing_value) {_missing_value = missing_value; };
771 
772  VDF_API friend std::ostream &operator<<(std::ostream &o, const DataVar &var);
773 
774  private:
775  std::vector <string> _coordvars;
776  string _maskvar;
777  bool _has_missing;
778  double _missing_value;
779  };
780 
781 
782 
786  DC() {};
787  virtual ~DC() {}
788 
802  //
803  virtual int Initialize(const vector <string> &paths) = 0;
804 
805 
817  virtual bool GetDimension(
818  string dimname, DC::Dimension &dimension
819  ) const = 0;
820 
826  virtual std::vector <string> GetDimensionNames() const = 0;
827 
840  virtual bool GetCoordVarInfo(string varname, DC::CoordVar &cvar) const = 0;
841 
854  virtual bool GetDataVarInfo( string varname, DC::DataVar &datavar) const = 0;
855 
866  //
867  virtual bool GetBaseVarInfo(string varname, DC::BaseVar &var) const = 0;
868 
869 
874  //
875  virtual std::vector <string> GetDataVarNames() const = 0;
876 
877 
882  //
883  virtual std::vector <string> GetCoordVarNames() const = 0;
884 
885 
900  //
901  virtual int GetNumRefLevels(string varname) const = 0;
902 
903 
921  //
922  virtual int GetAtt(
923  string varname, string attname, vector <double> &values
924  ) const = 0;
925  virtual int GetAtt(
926  string varname, string attname, vector <long> &values
927  ) const = 0;
928  virtual int GetAtt(
929  string varname, string attname, string &values
930  ) const = 0;
931 
944  //
945  virtual std::vector <string> GetAttNames(string varname) const = 0;
946 
958  virtual XType GetAttType(string varname, string attname) const = 0;
959 
982  //
983  virtual int GetDimLensAtLevel(
984  string varname, int level, std::vector <size_t> &dims_at_level,
985  std::vector <size_t> &bs_at_level
986  ) const = 0;
987 
1002  //
1003  virtual int GetMapProjection(
1004  string lonname, string latname, string &projstring
1005  ) const = 0;
1006 
1007 
1039  //
1040  virtual int OpenVariableRead(
1041  size_t ts, string varname, int level=0, int lod=0
1042  ) = 0;
1043 
1044 
1050  //
1051  virtual int CloseVariable() = 0;
1052 
1068  //
1069  int virtual Read(float *data) = 0;
1070 
1095  virtual int ReadSlice(float *slice) = 0;
1096 
1119  //
1120  virtual int ReadRegion(
1121  const vector <size_t> &min, const vector <size_t> &max, float *region
1122  ) = 0;
1123 
1137  virtual int ReadRegionBlock(
1138  const vector <size_t> &min, const vector <size_t> &max, float *region
1139  ) = 0;
1140 
1164  //
1165  virtual int GetVar(string varname, int level, int lod, float *data) = 0;
1166 
1193  //
1194  virtual int GetVar(
1195  size_t ts, string varname, int level, int lod, float *data
1196  ) = 0;
1197 
1198 
1211  //
1212  virtual bool VariableExists(
1213  size_t ts,
1214  string varname,
1215  int reflevel = 0,
1216  int lod = 0
1217  ) const = 0;
1218 
1219 
1221  //
1222  // The following are convenience methods provided by the DC base
1223  // class. In general they should NOT need to be reimplimented by
1224  // derived classes.
1225  //
1227 
1241  virtual bool GetDimension(
1242  string dimname, size_t &length, int &axis
1243  ) const;
1244 
1254  //
1255  virtual std::vector <string> GetDataVarNames(int ndim, bool spatial) const;
1256 
1257 
1258  //
1268  //
1269  virtual std::vector <string> GetCoordVarNames(int ndim, bool spatial) const;
1270 
1281  virtual bool IsTimeVarying(string varname) const;
1282 
1293  //
1294  virtual bool IsCompressed(string varname) const;
1295 
1308  //
1309  virtual int GetNumTimeSteps(string varname) const;
1310 
1323  //
1324  virtual int GetCRatios(string varname, vector <size_t> &cratios) const;
1325 
1326 
1334  virtual bool IsDataVar(string varname) const {
1335  vector <string> names = GetDataVarNames();
1336  return(find(names.begin(), names.end(), varname) != names.end());
1337  }
1338 
1347  virtual bool IsCoordVar(string varname) const {
1348  vector <string> names = GetDataVarNames();
1349  return(find(names.begin(), names.end(), varname) != names.end());
1350  }
1351 
1367  //
1368  virtual int GetMapProjection(string varname, string &projstring) const;
1369 
1385  //
1386  static bool ParseDimensions(
1387  const vector <DC::Dimension> &dimensions,
1388  vector <size_t> &sdims, size_t &numts
1389  );
1390 
1391 
1392 };
1393 };
1394 
1395 #endif
Attribute(string name, XType type)
Definition: DC.h:252
virtual ~BaseVar()
Definition: DC.h:389
virtual ~DC()
Definition: DC.h:787
bool operator!=(const Dimension &rhs) const
Definition: DC.h:220
bool GetUniform() const
Access coordinate variable uniform sampling flag.
Definition: DC.h:542
void SetPeriodic(std::vector< bool > periodic)
Definition: DC.h:435
std::vector< string > GetCoordvars() const
Access data variable's coordinate variable names.
Definition: DC.h:754
void SetXType(XType type)
Definition: DC.h:411
std::vector< size_t > GetBS() const
Access variable's block size.
Definition: DC.h:416
virtual ~CoordVar()
Definition: DC.h:533
void SetAttributes(std::map< string, Attribute > &atts)
Definition: DC.h:440
bool IsCompressed() const
Return true if no wavelet is defined.
Definition: DC.h:444
void Clear()
Definition: DC.h:187
void SetCRatios(std::vector< size_t > cratios)
Definition: DC.h:427
void SetUniform(bool uniform)
Definition: DC.h:543
string GetUnits() const
Access variable units.
Definition: DC.h:405
Base class for storing variable metadata.
Definition: DC.h:307
virtual bool IsDataVar(string varname) const
Definition: DC.h:1334
void SetName(string name)
Set dimension name.
Definition: DC.h:199
DataVar(string name, std::vector< DC::Dimension > dimensions, string units, XType type, std::vector< size_t > bs, string wname, std::vector< size_t > cratios, std::vector< bool > periodic, std::vector< string > coordvars, double missing_value, string maskvar)
Definition: DC.h:622
DataVar()
constructor for default Data variable definition
Definition: DC.h:562
std::vector< size_t > GetCRatios() const
Access variable's compression ratios.
Definition: DC.h:426
void SetMissingValue(double missing_value)
Definition: DC.h:770
int GetAxis() const
Access coordinate variable axis.
Definition: DC.h:537
bool IsTimeVarying() const
Return true if a time dimension is present.
Definition: DC.h:448
virtual ~Attribute()
Definition: DC.h:255
CoordVar(string name, std::vector< DC::Dimension > dimensions, string units, XType type, std::vector< bool > periodic, int axis, bool uniform)
Definition: DC.h:522
void SetDimensions(std::vector< DC::Dimension > dimensions)
Definition: DC.h:399
DataVar(string name, std::vector< DC::Dimension > dimensions, string units, XType type, std::vector< bool > periodic, std::vector< string > coordvars, double missing_value)
Definition: DC.h:678
virtual ~Dimension()
Definition: DC.h:185
Coordinate variable metadata.
Definition: DC.h:472
virtual ~DataVar()
Definition: DC.h:750
std::vector< bool > GetPeriodic() const
Access variable bounary periodic.
Definition: DC.h:434
void SetCoordvars(std::vector< string > coordvars)
Definition: DC.h:755
int GetAxis() const
Access dimension axis.
Definition: DC.h:211
CoordVar()
Default Coordinate Variable metadata constructor.
Definition: DC.h:477
std::vector< DC::Dimension > GetDimensions() const
Access variable's dimension names.
Definition: DC.h:398
DataVar(string name, std::vector< DC::Dimension > dimensions, string units, XType type, std::vector< bool > periodic, std::vector< string > coordvars)
Definition: DC.h:735
void SetAxis(int axis)
Definition: DC.h:538
void SetName(string name)
Definition: DC.h:394
Metadata describing a coordinate dimension.
Definition: DC.h:161
void SetUnits(string units)
Definition: DC.h:406
DataVar(string name, std::vector< DC::Dimension > dimensions, string units, XType type, std::vector< size_t > bs, string wname, std::vector< size_t > cratios, std::vector< bool > periodic, std::vector< string > coordvars)
Definition: DC.h:651
Dimension(std::string name, size_t length, int axis)
Definition: DC.h:180
void SetBS(std::vector< size_t > bs)
Definition: DC.h:417
CoordVar(string name, std::vector< DC::Dimension > dimensions, string units, XType type, std::vector< size_t > bs, string wname, std::vector< size_t > cratios, std::vector< bool > periodic, int axis, bool uniform)
Definition: DC.h:496
size_t GetLength() const
Access dimension length.
Definition: DC.h:203
virtual bool IsCoordVar(string varname) const
Definition: DC.h:1347
BaseVar(string name, std::vector< DC::Dimension > dimensions, string units, XType type, std::vector< size_t > bs, string wname, std::vector< size_t > cratios, std::vector< bool > periodic)
Definition: DC.h:349
string GetMaskvar() const
Access data variable's mask variable names.
Definition: DC.h:759
XType
External storage types for primitive data.
Definition: DC.h:155
DataVar(string name, std::vector< DC::Dimension > dimensions, string units, XType type, std::vector< bool > periodic, std::vector< string > coordvars, double missing_value, string maskvar)
Definition: DC.h:711
Defines API for reading a collection of data.
Definition: DC.h:150
VetsUtil base class.
Definition: MyBase.h:68
string GetName() const
Get variable name.
Definition: DC.h:393
void SetAxis(int axis)
Set dimension axis.
Definition: DC.h:215
#define VDF_API
std::map< string, Attribute > GetAttributes() const
Access variable attributes.
Definition: DC.h:439
DC()
Definition: DC.h:786
string GetName() const
Get attribute name.
Definition: DC.h:259
Definition: DC.h:10
void SetLength(size_t length)
Set dimension length.
Definition: DC.h:207
string GetName() const
Get dimension name.
Definition: DC.h:195
XType GetXType() const
Access variable external storage type.
Definition: DC.h:410
double GetMissingValue() const
Access data variable's missing data value.
Definition: DC.h:769
XType GetXType() const
Get an attribute's external representation type.
Definition: DC.h:263
void SetMaskvar(string maskvar)
Definition: DC.h:760
Data variable metadata.
Definition: DC.h:557
Variable or global metadata.
Definition: DC.h:237
void SetHasMissing(bool has_missing)
Definition: DC.h:765
string GetWName() const
Access variable's wavelet family name.
Definition: DC.h:421
bool operator==(const Dimension &rhs) const
Definition: DC.h:217
DataVar(string name, std::vector< DC::Dimension > dimensions, string units, XType type, std::vector< size_t > bs, string wname, std::vector< size_t > cratios, std::vector< bool > periodic, std::vector< string > coordvars, double missing_value)
Definition: DC.h:584
void SetWName(string wname)
Definition: DC.h:422
bool GetHasMissing() const
Access data variable's missing data flag.
Definition: DC.h:764