VAPoR  0.1
NetCDFCollection.h
Go to the documentation of this file.
1 //
2 // $Id: NetCDFCollection.h,v 1.7 2013/05/16 21:39:38 clynejp Exp $
3 //
4 
5 
6 #ifndef _NetCDFCollection_h_
7 #define _NetCDFCollection_h_
8 
9 #include <vector>
10 #include <map>
11 
12 #include <sstream>
13 #include <vapor/MyBase.h>
14 #include <vapor/NetCDFSimple.h>
15 
16 namespace VAPoR {
17 
18 //
94 
95 
97 public:
98 
100  virtual ~NetCDFCollection();
101 
128  virtual int Initialize(
129  const std::vector <string> &files,
130  const std::vector <string> &time_dimnames,
131  const std::vector <string> &time_coordvar
132  );
133 
150  //
151  virtual bool VariableExists(string varname) const;
152 
172  //
173  virtual bool VariableExists(size_t ts, string varname) const;
174 
184  virtual bool IsDerivedVar(string varname) const {
185  return(_derivedVarsMap.find(varname) != _derivedVarsMap.end());
186  }
187 
188 
189 
201  virtual bool IsStaggeredVar(string varname) const;
202 
212  virtual bool IsStaggeredDim(string dimname) const;
213 
230  //
231  virtual std::vector <string> GetVariableNames(int ndim, bool spatial) const;
232 
245  virtual std::vector <size_t> GetSpatialDims(string varname) const;
246 
258  virtual std::vector <string> GetSpatialDimNames(string varname) const;
259 
269  virtual size_t GetTimeDim(string varname) const;
270 
280  virtual string GetTimeDimName(string varname) const;
281 
296  virtual std::vector <size_t> GetDims(string varname) const ;
297 
311  virtual std::vector <string> GetDimNames(string varname) const;
312 
319  //
320  virtual bool IsTimeVarying(string varname) const;
321 
333  //
334  std::vector <string> GetAttNames(string varname) const;
335 
347  int GetAttType(string varname, string attname) const;
348 
362  //
363  void GetAtt(string varname, string attname, std::vector <double> &values) const;
364  void GetAtt(string varname, string attname, std::vector <long> &values) const;
365  void GetAtt(string varname, string attname, string &values) const;
366 
367 
368 
369 
382  //
383  virtual std::vector <string> GetDimNames() const {return (_dimNames); };
384 
397  //
398  virtual std::vector <size_t> GetDims() const {return (_dimLens); };
399 
412  //
413  virtual size_t GetNumTimeSteps() const {return (_times.size());}
414 
427  //
428  virtual size_t GetNumTimeSteps(string varname) const {
429  return(NetCDFCollection::GetTimeDim(varname));
430  }
431 
432 
433 
448  //
449  virtual int GetTime(size_t ts, double &time) const;
450 
461  //
462  virtual int GetTimes(string varname, std::vector <double> &times) const;
463 
473  //
474  virtual std::vector <double> GetTimes() const {return (_times); };
475 
488  //
489  virtual int GetFile(
490  size_t ts, string varname, string &file, size_t &local_ts
491  ) const;
492 
502  virtual int GetVariableInfo(
503  string varname, NetCDFSimple::Variable &varinfo
504  ) const;
505 
506 
519  virtual bool GetMissingValue(string varname, double &mv) const;
520 
538  virtual int OpenRead(size_t ts, string varname);
539 
569  virtual int Read(size_t start[], size_t count[], float *data, int fd=0);
570  virtual int Read(size_t start[], size_t count[], int *data, int fd=0);
571 
572  virtual int Read(float *data, int fd=0);
573  virtual int Read(char *data, int fd=0);
574 
590  virtual int ReadNative(size_t start[], size_t count[], float *data, int fd=0);
591  virtual int ReadNative(size_t start[], size_t count[], int *data, int fd=0);
592  virtual int ReadNative(size_t start[], size_t count[], char *data, int fd=0);
593 
594  virtual int ReadNative(float *data, int fd=0);
595  virtual int ReadNative(int *data, int fd=0);
596 
616  //
617  virtual int ReadSlice(float *data, int fd=0);
618 
634  //
635  virtual int ReadSliceNative(float *data, int fd=0);
636 
655  //
656  virtual int SeekSlice(int offset, int whence, int fd=0);
657 
662  //
663  virtual int Close(int fd=0);
664 
673  //
674  virtual void SetStaggeredDims(const std::vector <string> dimnames) {
675  _staggeredDims = dimnames;
676  }
677 
687  //
688  virtual void SetMissingValueAttName(string attname) {
689  _missingValAttName = attname;
690  }
691 
698  virtual std::vector <string> GetFailedVars() const {return(_failedVars); };
699 
700  friend std::ostream &operator<<(
701  std::ostream &o, const NetCDFCollection &ncdfc
702  );
703 
705  public:
706  TimeVaryingVar();
707  int Insert(
708  const NetCDFSimple *netcdf,
709  const NetCDFSimple::Variable &variable, string file,
710  const std::vector <string> &time_dimnames,
711  const std::map <string, std::vector <double> > &timesmap,
712  int file_org
713  );
714  std::vector <size_t> GetSpatialDims() const {return(_spatial_dims); };
715  std::vector <string> GetSpatialDimNames() const {return(_spatial_dim_names); };
716  string GetName() const {return(_name); };
717 
718  size_t GetNumTimeSteps() const {return(_time_varying ? _tvmaps.size() : 0); };
719  string GetTimeDimName() const {return (_time_name); };
720  int GetTime(size_t ts, double &time) const;
721  std::vector <double> GetTimes() const;
722  int GetTimeStep(double time, size_t &ts) const;
723  size_t GetLocalTimeStep(size_t ts) const;
724  int GetFile(size_t ts, string &file) const;
725  void GetVariableInfo(NetCDFSimple::Variable &variable) const {
726  variable = _variable;
727  }
728  bool GetTimeVarying() const {return (_time_varying); };
729  bool GetMissingValue(string attname, double &mv) const;
730 
731  friend std::ostream &operator<<(std::ostream &o, const TimeVaryingVar &var);
732 
733 
734  typedef struct {
735  int _fileidx; // Index into _files
736  double _time; // user time for this time step
737  size_t _local_ts; // time step offset within file
738  } tvmap_t;
739  private:
740  NetCDFSimple::Variable _variable;
741  std::vector <string> _files;
742  std::vector <tvmap_t> _tvmaps;
743  std::vector <size_t> _spatial_dims; // **spatial** dimensions
744  std::vector <string> _spatial_dim_names;
745  string _time_name; // name of time dimension
746  string _name; // variable name
747  bool _time_varying; // true if variable's slowest varying dimension
748  // is a time dimension.
749  friend bool tvmap_cmp(
752 
753  };
754 
755  class DerivedVar {
756  public:
758  NetCDFCollection *ncdfc
759  ) {
760  _ncdfc = ncdfc;
761  };
762  virtual ~DerivedVar() {};
763  virtual int Open(size_t ts) = 0;
764  virtual int ReadSlice(float *slice, int fd) = 0;
765  virtual int Read(float *buf, int fd) = 0;
766  virtual int SeekSlice(int offset, int whence, int fd) = 0;
767  virtual int Close(int fd) { return(0); };
768  virtual bool TimeVarying() const = 0;
769  virtual std::vector <size_t> GetSpatialDims() const = 0;
770  virtual std::vector <string> GetSpatialDimNames() const = 0;
771  virtual size_t GetTimeDim() const = 0;
772  virtual string GetTimeDimName() const = 0;
773  virtual bool GetMissingValue(double &mv) const = 0;
774  virtual size_t GetNumTimeSteps() const {return(GetTimeDim());}
775  protected:
777  };
778 
779  void InstallDerivedVar(string varname, DerivedVar *derivedVar) {
780  _derivedVarsMap[varname] = derivedVar;
781  };
782 
783  void RemoveDerivedVar(string varname) {
784  std::map <string,DerivedVar*>::iterator itr = _derivedVarsMap.find(varname);
785  if (itr != _derivedVarsMap.end()) {
786  _derivedVarsMap.erase(varname);
787  }
788  };
789 
790 protected:
791 
792  bool _GetVariableInfo(
793  string varname, NetCDFSimple::Variable &variable
794  ) const;
795 
796 private:
797 
798  std::map <string, TimeVaryingVar > _variableList;
799  std::map <string, NetCDFSimple *> _ncdfmap;
800  std::vector <string> _staggeredDims;
801  std::vector <string> _dimNames; // Names of all dimensions
802  std::vector <size_t> _dimLens; // Names of all dimensions
803  string _missingValAttName;
804  std::map <string, vector <double> > _timesMap; // map variable to time
805  std::vector <double> _times; // all valid time coordinates
806  std::vector <string> _failedVars; // Varibles that could not be added
807  std::map <string, DerivedVar *> _derivedVarsMap;
808  DerivedVar * _derivedVar; // if current opened variable is derived this is it
809 
810  //
811  // file handle for an open variable
812  //
813  class fileHandle {
814  public:
815  fileHandle();
816  DerivedVar *_derived_var;
817  NetCDFSimple *_ncdfptr;
818  int _fd; // NetCDFSimple open file descriptor
819  size_t _local_ts;
820  int _slice;
821  bool _first_slice;
822  unsigned char *_slicebuf;
823  size_t _slicebufsz;
824  unsigned char *_linebuf;
825  size_t _linebufsz;
826  TimeVaryingVar _tvvars;
827  bool _has_missing;
828  double _missing_value;
829  };
830  //
831  // Open file data
832  //
833  std::map<int, NetCDFCollection::fileHandle> _ovr_table;
834 
835  int _InitializeTimesMap(
836  const std::vector <string> &files,
837  const std::vector <string> &time_dimnames,
838  const std::vector <string> &time_coordvars,
839  std::map <string, std::vector <double> > &timesMap,
840  std::vector <double> &times,
841  int &file_org
842  ) const;
843 
844  int _InitializeTimesMapCase1(
845  const std::vector <string> &files,
846  std::map <string, std::vector <double> > &timesMap
847  ) const;
848 
849  int _InitializeTimesMapCase2(
850  const std::vector <string> &files,
851  const std::vector <string> &time_dimnames,
852  std::map <string, std::vector <double> > &timesMap
853  ) const;
854 
855  int _InitializeTimesMapCase3(
856  const std::vector <string> &files,
857  const std::vector <string> &time_dimnames,
858  const std::vector <string> &time_coordvars,
859  std::map <string, std::vector <double> > &timesMap
860  ) const;
861 
862  void _InterpolateLine(
863  const float *src, size_t n, size_t stride,
864  bool has_missing, float mv, float *dst
865  ) const;
866 
867  void _InterpolateSlice(
868  size_t nx, size_t ny, bool xstag, bool ystag,
869  bool has_missing, float mv, float *slice
870  ) const;
871 
872 int _GetTimesMap(
873  NetCDFSimple *netcdf,
874  const std::vector <string> &time_coordvars,
875  const std::vector <string> &time_dimnames,
876  std::map <string, std::vector <double> > &timesmap
877  ) const;
878 
879  float *_Get1DVar(
880  NetCDFSimple *netcdf,
881  const NetCDFSimple::Variable &variable
882  ) const ;
883 
884  int _get_var_index(
885  const vector <NetCDFSimple::Variable> variables, string varname
886  ) const;
887 
888 
889 };
890 };
891 
892 
893 #endif
virtual std::vector< string > GetDimNames() const
virtual void SetMissingValueAttName(string attname)
virtual size_t GetNumTimeSteps() const
#define VDF_API
Definition: common.h:61
COMMON_API double GetTime()
void InstallDerivedVar(string varname, DerivedVar *derivedVar)
void GetVariableInfo(NetCDFSimple::Variable &variable) const
virtual size_t GetNumTimeSteps() const
virtual size_t GetNumTimeSteps(string varname) const
NetCDFSimple API interface.
Definition: NetCDFSimple.h:43
std::vector< size_t > GetSpatialDims() const
virtual bool IsDerivedVar(string varname) const
void RemoveDerivedVar(string varname)
virtual size_t GetTimeDim(string varname) const
std::vector< string > GetSpatialDimNames() const
virtual std::vector< string > GetFailedVars() const
virtual std::vector< size_t > GetDims() const
VetsUtil base class.
Definition: MyBase.h:68
virtual void SetStaggeredDims(const std::vector< string > dimnames)
virtual std::vector< double > GetTimes() const
NetCDFSimple API interface.
Definition: NetCDFSimple.h:33
DerivedVar(NetCDFCollection *ncdfc)
Wrapper for a collection of netCDF files.