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 
331  int GetXType(string varname) const;
332 
344  //
345  std::vector <string> GetAttNames(string varname) const;
346 
358  int GetAttType(string varname, string attname) const;
359 
373  //
374  void GetAtt(string varname, string attname, std::vector <double> &values) const;
375  void GetAtt(string varname, string attname, std::vector <long> &values) const;
376  void GetAtt(string varname, string attname, string &values) const;
377 
378 
379 
380 
393  //
394  virtual std::vector <string> GetDimNames() const {return (_dimNames); };
395 
408  //
409  virtual std::vector <size_t> GetDims() const {return (_dimLens); };
410 
423  //
424  virtual size_t GetNumTimeSteps() const {return (_times.size());}
425 
438  //
439  virtual size_t GetNumTimeSteps(string varname) const {
440  return(NetCDFCollection::GetTimeDim(varname));
441  }
442 
443 
444 
459  //
460  virtual int GetTime(size_t ts, double &time) const;
461 
472  //
473  virtual int GetTimes(string varname, std::vector <double> &times) const;
474 
484  //
485  virtual std::vector <double> GetTimes() const {return (_times); };
486 
499  //
500  virtual int GetFile(
501  size_t ts, string varname, string &file, size_t &local_ts
502  ) const;
503 
513  virtual int GetVariableInfo(
514  string varname, NetCDFSimple::Variable &varinfo
515  ) const;
516 
517 
530  virtual bool GetMissingValue(string varname, double &mv) const;
531 
549  virtual int OpenRead(size_t ts, string varname);
550 
580  virtual int Read(size_t start[], size_t count[], float *data, int fd=0);
581  virtual int Read(size_t start[], size_t count[], int *data, int fd=0);
582  virtual int Read(
583  std::vector <size_t> start, std::vector <size_t> count, float *data, int fd=0);
584  virtual int Read(
585  std::vector <size_t> start, std::vector <size_t> count, int *data, int fd=0
586  );
587 
588  virtual int Read(float *data, int fd=0);
589  virtual int Read(char *data, int fd=0);
590 
606  virtual int ReadNative(size_t start[], size_t count[], float *data, int fd=0);
607  virtual int ReadNative(size_t start[], size_t count[], int *data, int fd=0);
608  virtual int ReadNative(size_t start[], size_t count[], char *data, int fd=0);
609 
610  virtual int ReadNative(float *data, int fd=0);
611  virtual int ReadNative(int *data, int fd=0);
612 
632  //
633  virtual int ReadSlice(float *data, int fd=0);
634 
650  //
651  virtual int ReadSliceNative(float *data, int fd=0);
652 
671  //
672  virtual int SeekSlice(int offset, int whence, int fd=0);
673 
678  //
679  virtual int Close(int fd=0);
680 
689  //
690  virtual void SetStaggeredDims(const std::vector <string> dimnames) {
691  _staggeredDims = dimnames;
692  }
693 
703  //
704  virtual void SetMissingValueAttName(string attname) {
705  _missingValAttName = attname;
706  }
707 
714  virtual std::vector <string> GetFailedVars() const {return(_failedVars); };
715 
716  friend std::ostream &operator<<(
717  std::ostream &o, const NetCDFCollection &ncdfc
718  );
719 
721  public:
722  TimeVaryingVar();
723  int Insert(
724  const NetCDFSimple *netcdf,
725  const NetCDFSimple::Variable &variable, string file,
726  const std::vector <string> &time_dimnames,
727  const std::map <string, std::vector <double> > &timesmap,
728  int file_org
729  );
730  std::vector <size_t> GetSpatialDims() const {return(_spatial_dims); };
731  std::vector <string> GetSpatialDimNames() const {return(_spatial_dim_names); };
732  string GetName() const {return(_name); };
733 
734  size_t GetNumTimeSteps() const {return(_time_varying ? _tvmaps.size() : 0); };
735  string GetTimeDimName() const {return (_time_name); };
736  int GetTime(size_t ts, double &time) const;
737  std::vector <double> GetTimes() const;
738  int GetTimeStep(double time, size_t &ts) const;
739  size_t GetLocalTimeStep(size_t ts) const;
740  int GetFile(size_t ts, string &file) const;
741  void GetVariableInfo(NetCDFSimple::Variable &variable) const {
742  variable = _variable;
743  }
744  bool GetTimeVarying() const {return (_time_varying); };
745  bool GetMissingValue(string attname, double &mv) const;
746 
747  friend std::ostream &operator<<(std::ostream &o, const TimeVaryingVar &var);
748 
749 
750  typedef struct {
751  int _fileidx; // Index into _files
752  double _time; // user time for this time step
753  size_t _local_ts; // time step offset within file
754  } tvmap_t;
755  private:
756  NetCDFSimple::Variable _variable;
757  std::vector <string> _files;
758  std::vector <tvmap_t> _tvmaps;
759  std::vector <size_t> _spatial_dims; // **spatial** dimensions
760  std::vector <string> _spatial_dim_names;
761  string _time_name; // name of time dimension
762  string _name; // variable name
763  bool _time_varying; // true if variable's slowest varying dimension
764  // is a time dimension.
765  friend bool tvmap_cmp(
768 
769  };
770 
771  class DerivedVar {
772  public:
774  NetCDFCollection *ncdfc
775  ) {
776  _ncdfc = ncdfc;
777  };
778  virtual ~DerivedVar() {};
779  virtual int Open(size_t ts) = 0;
780  virtual int ReadSlice(float *slice, int fd) = 0;
781  virtual int Read(float *buf, int fd) = 0;
782  virtual int SeekSlice(int offset, int whence, int fd) = 0;
783  virtual int Close(int fd) { return(0); };
784  virtual bool TimeVarying() const = 0;
785  virtual std::vector <size_t> GetSpatialDims() const = 0;
786  virtual std::vector <string> GetSpatialDimNames() const = 0;
787  virtual size_t GetTimeDim() const = 0;
788  virtual string GetTimeDimName() const = 0;
789  virtual bool GetMissingValue(double &mv) const = 0;
790  virtual size_t GetNumTimeSteps() const {return(GetTimeDim());}
791  protected:
793  };
794 
795  void InstallDerivedVar(string varname, DerivedVar *derivedVar) {
796  _derivedVarsMap[varname] = derivedVar;
797  };
798 
799  void RemoveDerivedVar(string varname) {
800  std::map <string,DerivedVar*>::iterator itr = _derivedVarsMap.find(varname);
801  if (itr != _derivedVarsMap.end()) {
802  _derivedVarsMap.erase(varname);
803  }
804  };
805 
806 protected:
807 
808  bool _GetVariableInfo(
809  string varname, NetCDFSimple::Variable &variable
810  ) const;
811 
812 private:
813 
814  std::map <string, TimeVaryingVar > _variableList;
815  std::map <string, NetCDFSimple *> _ncdfmap;
816  std::vector <string> _staggeredDims;
817  std::vector <string> _dimNames; // Names of all dimensions
818  std::vector <size_t> _dimLens; // Names of all dimensions
819  string _missingValAttName;
820  std::map <string, vector <double> > _timesMap; // map variable to time
821  std::vector <double> _times; // all valid time coordinates
822  std::vector <string> _failedVars; // Varibles that could not be added
823  std::map <string, DerivedVar *> _derivedVarsMap;
824  DerivedVar * _derivedVar; // if current opened variable is derived this is it
825 
826  //
827  // file handle for an open variable
828  //
829  class fileHandle {
830  public:
831  fileHandle();
832  DerivedVar *_derived_var;
833  NetCDFSimple *_ncdfptr;
834  int _fd; // NetCDFSimple open file descriptor
835  size_t _local_ts;
836  int _slice;
837  bool _first_slice;
838  unsigned char *_slicebuf;
839  size_t _slicebufsz;
840  unsigned char *_linebuf;
841  size_t _linebufsz;
842  TimeVaryingVar _tvvars;
843  bool _has_missing;
844  double _missing_value;
845  };
846  //
847  // Open file data
848  //
849  std::map<int, NetCDFCollection::fileHandle> _ovr_table;
850 
851  void ReInitialize();
852 
853  int _InitializeTimesMap(
854  const std::vector <string> &files,
855  const std::vector <string> &time_dimnames,
856  const std::vector <string> &time_coordvars,
857  std::map <string, std::vector <double> > &timesMap,
858  std::vector <double> &times,
859  int &file_org
860  ) const;
861 
862  int _InitializeTimesMapCase1(
863  const std::vector <string> &files,
864  std::map <string, std::vector <double> > &timesMap
865  ) const;
866 
867  int _InitializeTimesMapCase2(
868  const std::vector <string> &files,
869  const std::vector <string> &time_dimnames,
870  std::map <string, std::vector <double> > &timesMap
871  ) const;
872 
873  int _InitializeTimesMapCase3(
874  const std::vector <string> &files,
875  const std::vector <string> &time_dimnames,
876  const std::vector <string> &time_coordvars,
877  std::map <string, std::vector <double> > &timesMap
878  ) const;
879 
880  void _InterpolateLine(
881  const float *src, size_t n, size_t stride,
882  bool has_missing, float mv, float *dst
883  ) const;
884 
885  void _InterpolateSlice(
886  size_t nx, size_t ny, bool xstag, bool ystag,
887  bool has_missing, float mv, float *slice
888  ) const;
889 
890 int _GetTimesMap(
891  NetCDFSimple *netcdf,
892  const std::vector <string> &time_coordvars,
893  const std::vector <string> &time_dimnames,
894  std::map <string, std::vector <double> > &timesmap
895  ) const;
896 
897  float *_Get1DVar(
898  NetCDFSimple *netcdf,
899  const NetCDFSimple::Variable &variable
900  ) const ;
901 
902  int _get_var_index(
903  const vector <NetCDFSimple::Variable> variables, string varname
904  ) const;
905 
906 
907 };
908 };
909 
910 
911 #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.