VAPoR  0.1
DCReaderGRIB.h
Go to the documentation of this file.
1 //************************************************************************
2 // *
3 // Copyright (C) 2004 *
4 // University Corporation for Atmospheric Research *
5 // All Rights Reserved *
6 // *
7 //************************************************************************/
8 //
9 // File: DCReaderGRIB.h
10 //
11 // Author: Scott Pearse
12 // National Center for Atmospheric Research
13 // PO 3000, Boulder, Colorado
14 //
15 // Date: June 2014
16 //
17 // Description: TBD
18 //
19 //
20 
21 #ifndef DCREADERGRIB_H
22 #define DCREADERGRIB_H
23 #include "grib_api.h"
24 #include <vapor/DCReader.h>
25 #include <iostream>
26 #include <string>
27 #include <vector>
28 #include <map>
29 
30 #include <vapor/UDUnitsClass.h>
31 #ifdef _WINDOWS
32 #include "vapor/udunits2.h"
33 #else
34 #include <udunits2.h>
35 #endif
36 
37 namespace VAPoR {
38 
39 class UDUnits;
40 
41 class VDF_API DCReaderGRIB : public DCReader {
42  public:
43  DCReaderGRIB();
44  DCReaderGRIB(const vector<string> files);
45  ~DCReaderGRIB();
46 
47  int HydrostaticPressureEqn();
48  int LoadHydroData(float* T, float* Q, float* Sp);
49  int CalcVirtualTemperature(float* T, float* Q, float* Sum, float* Tv);
50  int DoHydroVarsExist();
51  void Print2dVars();
52  void _generateWeightTable();
53  string GetGridType() const;
54  virtual std::vector<double> GetTSZCoords(size_t ) const {
55  return(_vertCoordinates);};
57  // DCReader Virtual Functions
58  virtual int OpenVariableRead(size_t timestep, string varname,
59  int reflevel=0, int lod=0);
60  virtual int CloseVariable() {//grib_handle_delete(h);
61  fclose(_inFile); return 0;}
62  virtual int Read(float *values);
63  virtual int ReadSlice(float *slice);
64  virtual bool VariableExists(size_t ts, string varname,
65  int reflevel=0, int lod=0) const; // not pure
66  virtual void GetLatLonExtents(size_t ts, double lon_exts[2],
67  double lat_exts[2]) const;
68  virtual std::vector<std::string> GetVariables2DExcluded() const;
69  virtual std::vector<std::string> GetVariables3DExcluded() const;
70 
71  virtual long GetNumTimeSteps() const; // from Metadata.h
72  virtual void GetGridDim(size_t dim[3]) const; // from Metadata.h
73  virtual std::vector<string> GetVariables3D() const; // from Metadata.h
74  virtual std::vector<string> GetVariables2DXY() const; // from Metadata.h
75  virtual double GetTSUserTime(size_t ts) const {return _gribTimes[ts];} // from Metadata.h
76  virtual std::vector<string> GetVariables2DXZ() const { // from Metadata.h
77  std::vector<string> empty; return(empty);};
78  virtual std::vector<string> GetVariables2DYZ() const { // from Metadata.h
79  std::vector<string> empty; return(empty);};
80  // END DCReader Functions
82 
83 
85  // Metadata Pure Virtual Functions
86  virtual void GetBlockSize(size_t bs[3], int reflevel) const { GetGridDim(bs); }
87  virtual std::vector<double> GetExtents(size_t ts = 0) const {return _cartographicExtents;}//_cartesianExtents;}
88  virtual std::vector<long> GetPeriodicBoundary() const; // Needs implementation!
89  virtual std::vector<long> GetGridPermutation() const; // Needs implementation!
90  virtual void GetTSUserTimeStamp(size_t ts, std::string &s) const;
91  virtual std::string GetMapProjection() const;
92  // END Metadata Virtual Functions
94 
95  vector <double> GetZCoordsInMeters() const {return _vertCoordinates;}
96  void _LinearInterpolation(float* values);
97 
98  private:
100  // Convenience functions
101  //int _Initialize(std::vector<std::map<std::string, std::string> > records);
102  int _Initialize(vector<string> files);
103  int _SetGribEnv();
104  int PrintVar(string var);
105  float GetLevel(int index) const {return _pressureLevels[index];}
106  void PrintLevels();
107  void Print3dVars();
108  void Print1dVars();
109  double BarometricFormula(const double pressure) const;
110  int _InitCartographicExtents(string mapProj);
112 
113  struct MessageLocation {
114  string fileName;
115  int offset;
116  };
117 
118  class Variable {
119  public:
120  Variable();
121  ~Variable();
123  void _AddTime(double t) {_unitTimes.push_back(t);}
124  void _AddMessage(int msg) {_messages.push_back(msg);}
125  void _AddLevel(float lvl) {_pressureLevels.push_back(lvl);}
126  void _AddIndex(double time, float level, string file, int offset);
127 
128  int GetOffset(double time, float level) const;
129  string GetFileName(double time, float level) const;
130  std::vector<int> GetMessages() const {return _messages;}
131  std::vector<double> GetTimes() const {return _unitTimes;}
132  std::vector<float> GetLevels() const {return _pressureLevels;}
133  float GetLevel(int index) const {return _pressureLevels[index];}
134  void PrintTimes();
135  void PrintMessages();
136  void PrintIndex(double time, float level);
137  void _SortLevels() {sort(_pressureLevels.begin(), _pressureLevels.end());}
138  void _SortTimes() {sort(_unitTimes.begin(), _unitTimes.end());}
139  bool _Exists(double time) const;
140  void setScanDirection(bool i, bool j) {iScan = i; jScan = j;}
141  void setOperatingCenter(string oc) {_operatingCenter = oc;}
142  void setParamId(int id) {_paramId = id;}
143  string getOperatingCenter() {return _operatingCenter;}
144  int getParamId() {return _paramId;}
145  bool getiScan() const {return iScan;}
146  bool getjScan() const {return jScan;}
147 
148  private:
149  // time level
150  std::map<double, std::map<float, MessageLocation> > _indices;
151  std::vector<int> _messages;
152  std::vector<float> _pressureLevels;
153 
155  std::vector<size_t> _varTimes;
157  std::vector<double> _unitTimes;
158  string _operatingCenter;
159  int _paramId;
160  bool iScan;
161  bool jScan;
162  bool isGaussian;
163  };
164 
165  bool _ignoreForecastData;
166  static int _sliceNum;
167  int _Ni;
168  int _Nj;
169  double _DxInMetres;
170  double _DyInMetres;
171  double _LaDInDegrees;
172  double _LoVInDegrees;
173  double _minLat;
174  double _minLon;
175  double _maxLat;
176  double _maxLon;
177  bool _iScanNeg;
178  bool _jScanPos;
179  string _gridType;
180  string _openVar;
181  string _Latin1InDegrees;
182  string _Latin2InDegrees;
183  string _orientationOfTheGridInDegrees;
184 
185  static int _openTS;
186  FILE* _inFile;
187  grib_handle* h;
188  std::vector<double> _pressureLevels;
189  std::vector<double> _vertCoordinates;
190  std::vector<double> _cartesianExtents;
191  std::vector<double> _cartographicExtents;
192  std::vector<double> _gribTimes;
193  std::map<std::string, Variable*> _vars1d;
194  std::map<std::string, Variable*> _vars2d;
195  std::map<std::string, Variable*> _vars3d;
196  double* _gaussianLats;
197  std::vector<double> _weights;
198  std::vector<int> _latIndices;
199  float* _iValues;
200  double* _regularLats;
201  float* _hydrostaticElevation;
202 
203  UDUnits *_udunit;
204 
205  class GribParser {
206  public:
207  GribParser();
208  ~GribParser();
209  int _LoadRecord(string file, size_t offset);
210  int _LoadRecordKeys(const string file); // loads only the keys that we need for vdc creation
211  int _VerifyKeys(); // verifies that key/values conform to our reqs
212  std::vector<std::map<std::string, std::string> > GetRecords() const {return _recordKeys;}
213  int doWeIgnoreForecastTimes() {return _ignoreForecastTimes;}
214 
215  private:
216  int _err;
217  std::vector<std::map<std::string, std::string> > _recordKeys;
218  std::vector<std::string> _consistentKeys;
219  std::vector<std::string> _varyingKeys;
220  bool _recordKeysVerified;
221  int _ignoreForecastTimes;
222  DCReaderGRIB *_metadata;
223 
224  // vars for key iteration
225  unsigned long _key_iterator_filter_flags;
226  int _grib_count;
227  char* _value;
228  size_t _vlen;
229 
230  // vars for data dump
231  double *_values;
232  double _min,_max,_average;
233  size_t _values_len;
234  string _filename;
235  };
236 
237  GribParser *parser;
238 
239  class Record {
240  public:
241  Record();
242  Record(string file, int recordNum);
243  ~Record();
244  void setTime(string input) {time = input;}
245  void setUnits(string input) {units = input;}
246  void setNi(string input) {Ni = input;}
247  void setNj(string input) {Nj = input;}
248  void setGridType(string input) {gridType = input;}
249  void setVarName(string input) {varName = input;}
250  string getTime() {return time;}
251  string getUnits() {return units;}
252  string getNi() {return Ni;}
253  string getNj() {return Nj;}
254  string getGridType() {return gridType;}
255  string getVarName() {return varName;}
256 
257  private:
258  string time;
259  string units;
260  string Ni;
261  string Nj;
262  string gridType;
263  string varName;
264  };
265  };
266 };
267 #endif // GRIBPARSER_H
#define VDF_API
Definition: common.h:61
virtual double GetTSUserTime(size_t ts) const
Definition: DCReaderGRIB.h:75
virtual int CloseVariable()
Definition: DCReaderGRIB.h:60
virtual std::vector< double > GetExtents(size_t ts=0) const
Definition: DCReaderGRIB.h:87
virtual void GetBlockSize(size_t bs[3], int reflevel) const
Definition: DCReaderGRIB.h:86
vector< double > GetZCoordsInMeters() const
Definition: DCReaderGRIB.h:95
virtual std::vector< string > GetVariables2DYZ() const
Definition: DCReaderGRIB.h:78
virtual std::vector< double > GetTSZCoords(size_t) const
Definition: DCReaderGRIB.h:54
virtual std::vector< string > GetVariables2DXZ() const
Definition: DCReaderGRIB.h:76