VAPOR3 3.9.4
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#ifndef _NetCDFCollection_h_
6#define _NetCDFCollection_h_
7
8#include <vector>
9#include <map>
10
11#include <sstream>
12#include <netcdf.h>
13#include <vapor/MyBase.h>
14#include <vapor/NetCDFSimple.h>
15
16namespace VAPoR {
17
18
19//
92
94public:
97
124 virtual int Initialize(const std::vector<string> &files, const std::vector<string> &time_dimnames, const std::vector<string> &time_coordvar);
125
142 //
143 virtual bool VariableExists(string varname) const;
144
164 //
165 virtual bool VariableExists(size_t ts, string varname) const;
166
183 //
184 virtual std::vector<string> GetVariableNames(int ndim, bool spatial) const;
185
198 virtual std::vector<size_t> GetSpatialDims(string varname) const;
199
211 virtual std::vector<string> GetSpatialDimNames(string varname) const;
212
222 virtual size_t GetTimeDim(string varname) const;
223
233 virtual string GetTimeDimName(string varname) const;
234
249 virtual std::vector<size_t> GetDims(string varname) const;
250
264 virtual std::vector<string> GetDimNames(string varname) const;
265
272 //
273 virtual bool IsTimeVarying(string varname) const;
274
284 int GetXType(string varname) const;
285
297 //
298 std::vector<string> GetAttNames(string varname) const;
299
311 int GetAttType(string varname, string attname) const;
312
326 //
327 void GetAtt(string varname, string attname, std::vector<double> &values) const;
328 void GetAtt(string varname, string attname, std::vector<long> &values) const;
329 void GetAtt(string varname, string attname, string &values) const;
330
343 //
344 virtual std::vector<string> GetDimNames() const { return (_dimNames); };
345
358 //
359 virtual std::vector<size_t> GetDims() const { return (_dimLens); };
360
361 virtual std::vector<bool> GetDimsAreTimeVarying() const { return _dimIsTimeVarying; }
362
363 long GetDimLengthAtTime(string name, long ts);
364
377 //
378 virtual size_t GetNumTimeSteps() const { return (_times.size()); }
379
392 //
393 virtual size_t GetNumTimeSteps(string varname) const { return (NetCDFCollection::GetTimeDim(varname)); }
394
409 //
410 virtual int GetTime(size_t ts, double &time) const;
411
422 //
423 virtual int GetTimes(string varname, std::vector<double> &times) const;
424
434 //
435 virtual std::vector<double> GetTimes() const { return (_times); };
436
449 //
450 virtual int GetFile(size_t ts, string varname, string &file, size_t &local_ts) const;
451
461 virtual int GetVariableInfo(string varname, NetCDFSimple::Variable &varinfo) const;
462
475 virtual bool GetMissingValue(string varname, double &mv) const;
476
494 virtual int OpenRead(size_t ts, string varname);
495
520 virtual int Read(size_t start[], size_t count[], double *data, int fd = 0);
521 virtual int Read(size_t start[], size_t count[], float *data, int fd = 0);
522 virtual int Read(size_t start[], size_t count[], int *data, int fd = 0);
523 virtual int Read(size_t start[], size_t count[], char *data, int fd = 0);
524 virtual int Read(std::vector<size_t> start, std::vector<size_t> count, double *data, int fd = 0);
525 virtual int Read(std::vector<size_t> start, std::vector<size_t> count, float *data, int fd = 0);
526 virtual int Read(std::vector<size_t> start, std::vector<size_t> count, int *data, int fd = 0);
527
528 virtual int Read(double *data, int fd = 0);
529 virtual int Read(float *data, int fd = 0);
530 virtual int Read(char *data, int fd = 0);
531 virtual int Read(int *data, int fd = 0);
532
551 //
552 virtual int ReadSlice(float *data, int fd = 0);
553
572 //
573 virtual int SeekSlice(int offset, int whence, int fd = 0);
574
579 //
580 virtual int Close(int fd = 0);
581
590 //
591 virtual void SetMissingValueAttName(string attname) { _missingValAttName = attname; }
592
599 virtual std::vector<string> GetFailedVars() const { return (_failedVars); };
600
601 friend std::ostream &operator<<(std::ostream &o, const NetCDFCollection &ncdfc);
602
604 public:
606 int Insert(const NetCDFSimple *netcdf, const NetCDFSimple::Variable &variable, string file, const std::vector<string> &time_dimnames, const std::map<string, std::vector<double>> &timesmap,
607 int file_org);
608 std::vector<size_t> GetSpatialDims() const { return (_spatial_dims); };
609 std::vector<string> GetSpatialDimNames() const { return (_spatial_dim_names); };
610 string GetName() const { return (_name); };
611
612 size_t GetNumTimeSteps() const { return (_time_varying ? _tvmaps.size() : 1); };
613 string GetTimeDimName() const { return (_time_name); };
614 int GetTime(size_t ts, double &time) const;
615 std::vector<double> GetTimes() const;
616 int GetTimeStep(double time, size_t &ts) const;
617 size_t GetLocalTimeStep(size_t ts) const;
618 int GetFile(size_t ts, string &file) const;
619 void GetVariableInfo(NetCDFSimple::Variable &variable) const { variable = _variable; }
620 bool GetTimeVarying() const { return (_time_varying); };
621 bool GetMissingValue(string attname, double &mv) const;
622 void Sort();
623
624 friend std::ostream &operator<<(std::ostream &o, const TimeVaryingVar &var);
625
626 typedef struct {
627 int _fileidx; // Index into _files
628 double _time; // user time for this time step
629 size_t _local_ts; // time step offset within file
630 } tvmap_t;
631
632 private:
633 NetCDFSimple::Variable _variable;
634 std::vector<string> _files;
635 std::vector<tvmap_t> _tvmaps;
636 std::vector<size_t> _spatial_dims; // **spatial** dimensions
637 std::vector<string> _spatial_dim_names;
638 string _time_name; // name of time dimension
639 string _name; // variable name
640 bool _time_varying; // true if variable's slowest varying dimension
641 // is a time dimension.
642 };
643
644protected:
645 bool _GetVariableInfo(string varname, NetCDFSimple::Variable &variable) const;
646
647private:
648 std::map<string, TimeVaryingVar> _variableList;
649 std::map<string, NetCDFSimple *> _ncdfmap;
650 std::vector<string> _dimNames; // Names of all dimensions
651 std::vector<size_t> _dimLens; // Names of all dimensions
652 std::vector<bool> _dimIsTimeVarying;
653 string _missingValAttName;
654 std::map<string, vector<double>> _timesMap; // map variable to time
655 std::vector<double> _times; // all valid time coordinates
656 std::vector<string> _failedVars; // Varibles that could not be added
657
658 //
659 // file handle for an open variable
660 //
661 class fileHandle {
662 public:
663 fileHandle();
664 NetCDFSimple * _ncdfptr;
665 int _fd; // NetCDFSimple open file descriptor
666 size_t _local_ts;
667 int _slice;
668 bool _first_slice;
669 unsigned char *_slicebuf;
670 size_t _slicebufsz;
671 unsigned char *_linebuf;
672 size_t _linebufsz;
673 TimeVaryingVar _tvvars;
674 bool _has_missing;
675 double _missing_value;
676 };
677 //
678 // Open file data
679 //
680 std::map<int, NetCDFCollection::fileHandle> _ovr_table;
681
682 void ReInitialize();
683
684 int _InitializeTimesMap(const std::vector<string> &files, const std::vector<string> &time_dimnames, const std::vector<string> &time_coordvars, std::map<string, std::vector<double>> &timesMap,
685 std::map<string, size_t> &timeDimLens, std::vector<double> &times, int &file_org) const;
686
687 int _InitializeTimesMapCase1(const std::vector<string> &files, std::map<string, std::vector<double>> &timesMap, std::map<string, size_t> &timeDimLens) const;
688
689 int _InitializeTimesMapCase2(const std::vector<string> &files, const std::vector<string> &time_dimnames, std::map<string, std::vector<double>> &timesMap,
690 std::map<string, size_t> &timeDimLens) const;
691
692 int _InitializeTimesMapCase3(const std::vector<string> &files, const std::vector<string> &time_dimnames, const std::vector<string> &time_coordvars, std::map<string, std::vector<double>> &timesMap,
693 map<string, size_t> &timeDimLens) const;
694
695 int _GetTimesMap(NetCDFSimple *netcdf, const std::vector<string> &time_coordvars, const std::vector<string> &time_dimnames, std::map<string, std::vector<double>> &timesmap) const;
696
697 double *_Get1DVar(NetCDFSimple *netcdf, const NetCDFSimple::Variable &variable) const;
698
699 int _get_var_index(const vector<NetCDFSimple::Variable> variables, string varname) const;
700
701 template<typename T> int _read_template(T *data, int fd);
702 template<typename T> int _read_template(size_t start[], size_t count[], T *data, int fd);
703 template<typename T> int _read_slice_template(T *data, int fd);
704};
705}; // namespace VAPoR
706
707#endif
std::vector< string > GetSpatialDimNames() const
std::vector< double > GetTimes() const
int GetFile(size_t ts, string &file) const
int Insert(const NetCDFSimple *netcdf, const NetCDFSimple::Variable &variable, string file, const std::vector< string > &time_dimnames, const std::map< string, std::vector< double > > &timesmap, int file_org)
friend std::ostream & operator<<(std::ostream &o, const TimeVaryingVar &var)
void GetVariableInfo(NetCDFSimple::Variable &variable) const
int GetTimeStep(double time, size_t &ts) const
bool GetMissingValue(string attname, double &mv) const
int GetTime(size_t ts, double &time) const
size_t GetLocalTimeStep(size_t ts) const
std::vector< size_t > GetSpatialDims() const
Wrapper for a collection of netCDF files.
virtual int Read(size_t start[], size_t count[], float *data, int fd=0)
virtual int ReadSlice(float *data, int fd=0)
int GetXType(string varname) const
virtual string GetTimeDimName(string varname) const
virtual int Read(std::vector< size_t > start, std::vector< size_t > count, float *data, int fd=0)
virtual int Read(std::vector< size_t > start, std::vector< size_t > count, double *data, int fd=0)
virtual bool GetMissingValue(string varname, double &mv) const
virtual size_t GetNumTimeSteps(string varname) const
virtual int GetVariableInfo(string varname, NetCDFSimple::Variable &varinfo) const
virtual std::vector< string > GetSpatialDimNames(string varname) const
virtual std::vector< size_t > GetDims(string varname) const
virtual bool IsTimeVarying(string varname) const
std::vector< string > GetAttNames(string varname) const
virtual bool VariableExists(string varname) const
virtual int Read(size_t start[], size_t count[], int *data, int fd=0)
virtual int Read(char *data, int fd=0)
void GetAtt(string varname, string attname, std::vector< long > &values) const
virtual std::vector< string > GetVariableNames(int ndim, bool spatial) const
void GetAtt(string varname, string attname, string &values) const
virtual int GetTime(size_t ts, double &time) const
virtual std::vector< double > GetTimes() const
void GetAtt(string varname, string attname, std::vector< double > &values) const
virtual size_t GetNumTimeSteps() const
virtual int Read(int *data, int fd=0)
long GetDimLengthAtTime(string name, long ts)
friend std::ostream & operator<<(std::ostream &o, const NetCDFCollection &ncdfc)
virtual int SeekSlice(int offset, int whence, int fd=0)
virtual std::vector< string > GetDimNames(string varname) const
virtual int Read(size_t start[], size_t count[], char *data, int fd=0)
virtual int Read(double *data, int fd=0)
virtual int GetFile(size_t ts, string varname, string &file, size_t &local_ts) const
virtual int OpenRead(size_t ts, string varname)
virtual bool VariableExists(size_t ts, string varname) const
virtual std::vector< size_t > GetDims() const
bool _GetVariableInfo(string varname, NetCDFSimple::Variable &variable) const
virtual int Close(int fd=0)
virtual std::vector< string > GetDimNames() const
virtual int Read(size_t start[], size_t count[], double *data, int fd=0)
virtual void SetMissingValueAttName(string attname)
virtual int Initialize(const std::vector< string > &files, const std::vector< string > &time_dimnames, const std::vector< string > &time_coordvar)
int GetAttType(string varname, string attname) const
virtual size_t GetTimeDim(string varname) const
virtual int Read(std::vector< size_t > start, std::vector< size_t > count, int *data, int fd=0)
virtual int GetTimes(string varname, std::vector< double > &times) const
virtual std::vector< string > GetFailedVars() const
virtual std::vector< size_t > GetSpatialDims(string varname) const
virtual std::vector< bool > GetDimsAreTimeVarying() const
virtual int Read(float *data, int fd=0)
NetCDFSimple API interface.
Definition: NetCDFSimple.h:43
NetCDFSimple API interface.
Definition: NetCDFSimple.h:33
Wasp base class.
Definition: MyBase.h:67
#define VDF_API
Definition: common.h:73