VAPoR  0.1
NetCDFCFCollection.h
Go to the documentation of this file.
1 //
2 // $Id$
3 //
4 
5 
6 #ifndef _NetCDFCFCollection_h_
7 #define _NetCDFCFCollection_h_
8 
9 #include <vector>
10 #include <map>
11 #include <algorithm>
12 
13 #include <sstream>
14 #include <vapor/MyBase.h>
15 #include <vapor/UDUnitsClass.h>
16 #include <vapor/NetCDFCollection.h>
17 
18 union ut_unit;
19 struct ut_system;
20 
21 namespace VAPoR {
22 
23 //
33 //
34 
36 public:
37 
39  virtual ~NetCDFCFCollection();
40 
41  virtual int Initialize(
42  const std::vector <string> &files
43  );
44 
45 
60  //
61  virtual bool IsCoordVarCF(string var) const {
62  return(std::find( _coordinateVars.begin(), _coordinateVars.end(), var) != _coordinateVars.end());
63  }
64 
80  //
81  virtual bool IsAuxCoordVarCF(string var) const {
82  return(std::find( _auxCoordinateVars.begin(),
83  _auxCoordinateVars.end(), var) != _auxCoordinateVars.end());
84  }
85 
86 
94  //
95  virtual bool IsLatCoordVar(string var) const {
96  return(std::find( _latCoordVars.begin(),
97  _latCoordVars.end(), var) != _latCoordVars.end());
98  }
99 
107  //
108  virtual bool IsLonCoordVar(string var) const {
109  return(std::find( _lonCoordVars.begin(),
110  _lonCoordVars.end(), var) != _lonCoordVars.end());
111  }
112 
120  //
121  virtual bool IsTimeCoordVar(string var) const {
122  return(std::find( _timeCoordVars.begin(),
123  _timeCoordVars.end(), var) != _timeCoordVars.end());
124  }
125 
134  //
135  virtual bool IsVertCoordVar(string var) const {
136  return(std::find( _vertCoordVars.begin(),
137  _vertCoordVars.end(), var) != _vertCoordVars.end());
138  }
139 
155  virtual bool IsVertCoordVarUp(string var) const;
156 
157 
158 
161  virtual std::vector <string> GetLatCoordVars() const {return(_latCoordVars); };
162 
165  virtual std::vector <string> GetLonCoordVars() const {return(_lonCoordVars); };
166 
169  virtual std::vector <string> GetTimeCoordVars() const {return(_timeCoordVars); };
170 
173  virtual std::vector <string> GetVertCoordVars() const {return(_vertCoordVars); };
174 
175 
198  //
199  virtual std::vector <string> GetDataVariableNames(int ndim, bool spatial)const;
200 
233  //
234  virtual int GetVarCoordVarNames(string var, std::vector <string> &cvars) const;
235 
249  virtual int GetVarUnits(string var, string &units) const;
250 
254  const UDUnits *GetUDUnits() const {return(_udunit); };
255 
256 
275  virtual int Convert(
276  const string from,
277  const string to,
278  const double *src,
279  double *dst,
280  size_t n
281  ) const;
282  virtual int Convert(
283  const string from,
284  const string to,
285  const float *src,
286  float *dst,
287  size_t n
288  ) const;
289 
290 
307  virtual bool GetMissingValue(string varname, double &mv) const;
308 
311  virtual int OpenRead(size_t ts, string varname);
312 
320  virtual bool IsVertDimensionless(string cvar) const;
321 
348  virtual int InstallStandardVerticalConverter(
349  string cvar, string newvar, string units = "meters"
350  );
351 
359  //
360  virtual void UninstallStandardVerticalConverter(string cvar) ;
361 
379  virtual int InstallStandardTimeConverter(
380  string cvar, string newvar, string units = "seconds"
381  );
382 
390  //
391  virtual void UninstallStandardTimeConverter(string cvar) ;
392 
405  //
406  void InstallDerivedCoordVar(string varname, DerivedVar *derivedVar, int axis);
407 
410  void RemoveDerivedVar(string varname);
411 
412 
413 
421  //
422  bool GetMapProjectionProj4(
423  string varname, string &proj4string
424  ) const ;
425 
426  void FormatTimeStr(double time, string &str) const;
427 
428  friend std::ostream &operator<<(
429  std::ostream &o, const NetCDFCFCollection &ncdfc
430  );
431 
432 private:
433 
434  class DerivedVar_ocean_s_coordinate_g1 : public NetCDFCollection::DerivedVar {
435  public:
436  DerivedVar_ocean_s_coordinate_g1(
437  NetCDFCFCollection *ncdfcf,
438  const std::map <string, string> &formula_map
439  );
440  virtual ~DerivedVar_ocean_s_coordinate_g1();
441 
442  virtual int Open(size_t ts);
443  virtual int ReadSlice(float *slice, int);
444  virtual int Read(float *buf, int);
445  virtual int SeekSlice(int offset, int whence, int fd);
446  virtual int Close(int) {_is_open = false; return(0); };
447  virtual bool TimeVarying() const {return(false); };
448  virtual std::vector <size_t> GetSpatialDims() const { return(_dims); }
449  virtual std::vector <string> GetSpatialDimNames() const { return(_dimnames); }
450  virtual size_t GetTimeDim() const { return(0); }
451  virtual string GetTimeDimName() const { return(""); };
452  virtual bool GetMissingValue(double &mv) const {mv=0.0; return(false); }
453 
454  private:
455  std::vector <size_t> _dims;
456  std::vector <string> _dimnames;
457  size_t _slice_num;
458  float *_s;
459  float *_C;
460  float *_eta;
461  float *_depth;
462  float _depth_c;
463  string _svar;
464  string _Cvar;
465  string _etavar;
466  string _depthvar;
467  string _depth_cvar;
468  bool _is_open;
469  bool _ok;
470  };
471 
472  class DerivedVar_ocean_s_coordinate_g2 : public NetCDFCollection::DerivedVar {
473 
474  public:
475  DerivedVar_ocean_s_coordinate_g2(
476  NetCDFCFCollection *ncdfcf,
477  const std::map <string, string> &formula_map
478  );
479  virtual ~DerivedVar_ocean_s_coordinate_g2();
480 
481  virtual int Open(size_t ts);
482  virtual int ReadSlice(float *slice, int);
483  virtual int Read(float *buf, int);
484  virtual int SeekSlice(int offset, int whence, int fd);
485  virtual int Close(int) {_is_open = false; return(0); };
486  virtual bool TimeVarying() const {return(false); };
487  virtual std::vector <size_t> GetSpatialDims() const { return(_dims); }
488  virtual std::vector <string> GetSpatialDimNames() const { return(_dimnames); }
489  virtual size_t GetTimeDim() const { return(0); }
490  virtual string GetTimeDimName() const { return(""); };
491  virtual bool GetMissingValue(double &mv) const {mv=0.0; return(false); }
492 
493  private:
494  std::vector <size_t> _dims;
495  std::vector <string> _dimnames;
496  size_t _slice_num;
497  float *_s;
498  float *_C;
499  float *_eta;
500  float *_depth;
501  float _depth_c;
502  string _svar;
503  string _Cvar;
504  string _etavar;
505  string _depthvar;
506  string _depth_cvar;
507  bool _is_open;
508  bool _ok;
509  };
510 
511 
512 class DerivedVar_AHSPC : public NetCDFCollection::DerivedVar {
513 
514 public:
515  DerivedVar_AHSPC(
516  NetCDFCFCollection *ncdfcf,
517  const std::map <string, string> &formula_map
518  );
519  virtual ~DerivedVar_AHSPC();
520  virtual float GetMax() { return _max; }
521  virtual float GetMin() { return _min; }
522  virtual void SetMax(float val) { _max = val; }
523  virtual void SetMin(float val) { _min = val; }
524  virtual int Open(size_t ts);
525  virtual int ReadSlice(float *slice, int);
526  virtual int Read(float *buf, int);
527  virtual int SeekSlice(int offset, int whence, int fd);
528  virtual int Close(int) {_is_open = false; return(0); };
529  virtual bool TimeVarying() const {return(false); };
530  virtual std::vector <size_t> GetSpatialDims() const { return(_dims); }
531  virtual std::vector <string> GetSpatialDimNames() const { return(_dimnames); }
532  virtual size_t GetTimeDim() const { return(0); }
533  virtual string GetTimeDimName() const { return(""); };
534  virtual bool GetMissingValue(double &mv) const {mv=0.0; return(false); }
535 
536  private:
537  virtual int CalculateElevation();
538  virtual int DCZ2(const float* PS1, const float* PHIS1,
539  float** TV2, const int NL,
540  const float P0, float** HYBA, float** HYBB,
541  const int KLEV, const int MLON, const int MLON2,
542  float** HYPDLN, float** HYALPH, float** PTERM,
543  float** ZSLICE);
544  std::vector <size_t> _dims;
545  std::vector <string> _dimnames;
546  size_t _slice_num;
547  float *PS1;
548  float *PHIS1;
549  float *PS;
550  float *PHIS;
551  float *TV;
552  float P0;
553  float *HYAM;
554  float *HYBM;
555  float *HYAI;
556  float *HYBI;
557  float *_Z3;
558  bool _is_open;
559  bool _ok;
560  float _min;
561  float _max;
562  };
563 
564  class DerivedVar_noop : public NetCDFCollection::DerivedVar {
565 
566  public:
567  DerivedVar_noop(
568  NetCDFCFCollection *ncdfcf,
569  const std::map <string, string> &formula_map, string units
570  );
571  virtual ~DerivedVar_noop() {}
572 
573  virtual int Open(size_t ts);
574  virtual int ReadSlice(float *slice, int fd);
575  virtual int Read(float *buf, int fd);
576  virtual int SeekSlice(int offset, int whence, int fd);
577  virtual int Close(int fd) {return(_ncdfc->Close(fd)); };
578  virtual bool TimeVarying() const {return(_ncdfc->IsTimeVarying(_zvar)); };
579  virtual std::vector <size_t> GetSpatialDims() const {
580  return(_ncdfc->GetSpatialDims(_zvar));
581  };
582  virtual std::vector <string> GetSpatialDimNames() const {
583  return(_ncdfc->GetSpatialDimNames(_zvar));
584  };
585  virtual size_t GetTimeDim() const {
586  return(_ncdfc->GetTimeDim(_zvar));
587  };
588  virtual string GetTimeDimName() const {
589  return(_ncdfc->GetTimeDimName(_zvar));
590  };
591 
592  virtual bool GetMissingValue(double &mv) const {
593  return(_ncdfc->GetMissingValue(_zvar, mv));
594  };
595 
596 
597  private:
598  string _zvar;
599  string _native_units;
600  string _derived_units;
601  };
602 
603  class DerivedVarTime : public NetCDFCollection::DerivedVar {
604  public:
605  DerivedVarTime(
606  NetCDFCFCollection *ncdfcf,
607  string native_var, string native_units, string derived_units
608  );
609  virtual ~DerivedVarTime();
610 
611  virtual int Open(size_t ts);
612  virtual int ReadSlice(float *slice, int);
613  virtual int Read(float *buf, int);
614  virtual int SeekSlice(int offset, int whence, int fd);
615  virtual int Close(int) {return(0); };
616  virtual bool TimeVarying() const {return(true); };
617  virtual std::vector <size_t> GetSpatialDims() const { return(_dims); }
618  virtual std::vector <string> GetSpatialDimNames() const { return(_dimnames); }
619  virtual size_t GetTimeDim() const { return(_timedim); }
620  virtual string GetTimeDimName() const { return(_timedimname); };
621  virtual bool GetMissingValue(double &mv) const {mv=0.0; return(false); }
622 
623  private:
624  string _native_var;
625  string _native_units;
626  string _derived_units;
627  std::vector <size_t> _dims;
628  std::vector <string> _dimnames;
629  size_t _timedim;
630  string _timedimname;
631  bool _first;
632  size_t _ts;
633  double *_timecoords;
634  };
635 
636  std::map <string, DerivedVar *> _derivedVarsMap;
637  std::vector <std::string> _coordinateVars;
638  std::vector <std::string> _auxCoordinateVars;
639  std::vector <std::string> _lonCoordVars;
640  std::vector <std::string> _latCoordVars;
641  std::vector <std::string> _vertCoordVars;
642  std::vector <std::string> _timeCoordVars;
643 
644  UDUnits *_udunit;
645 
646  //
647  // Map a variable name to it's missing value (if any)
648  //
649  std::map <string, double> _missingValueMap;
650 
651  std::vector <std::string> _GetCoordAttrs(
652  const NetCDFSimple::Variable &varinfo
653  ) const;
654 
655  int _Initialize(const std::vector <string> &files);
656 
664  //
665  bool _IsCoordinateVar(const NetCDFSimple::Variable &varinfo) const;
666 
683 
684  bool _IsLonCoordVar(
685  const NetCDFSimple::Variable &varinfo
686  ) const;
687 
698  bool _IsLatCoordVar(
699  const NetCDFSimple::Variable &varinfo
700  ) const;
701 
715  bool _IsVertCoordVar(
716  const NetCDFSimple::Variable &varinfo
717  ) const;
718 
729  bool _IsTimeCoordVar(
730  const NetCDFSimple::Variable &varinfo
731  ) const;
732 
733  bool _GetMissingValue(string varname, string &attname, double &mv) const;
734  void _GetMissingValueMap(map <string, double> &missingValueMap) const;
735 
736  int _parse_formula(
737  string formula_terms, map <string, string> &parsed_terms
738  ) const;
739 
740 
741 
742 };
743 };
744 
745 
746 #endif
const UDUnits * GetUDUnits() const
#define VDF_API
Definition: common.h:61
virtual std::vector< string > GetVertCoordVars() const
virtual std::vector< string > GetLonCoordVars() const
Wrapper for a collection of netCDF files.
virtual bool IsAuxCoordVarCF(string var) const
virtual bool IsLonCoordVar(string var) const
virtual bool IsVertCoordVar(string var) const
virtual bool IsTimeCoordVar(string var) const
virtual bool IsLatCoordVar(string var) const
virtual std::vector< string > GetTimeCoordVars() const
virtual std::vector< string > GetLatCoordVars() const
virtual bool IsCoordVarCF(string var) const
Wrapper for a collection of netCDF files.