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  void Print2dVars();
48  void _generateWeightTable();
49  string GetGridType() const;
50  virtual std::vector<double> GetTSZCoords(size_t ) const {
51  return(_vertCoordinates);};
53  // DCReader Virtual Functions
54  virtual int OpenVariableRead(size_t timestep, string varname,
55  int reflevel=0, int lod=0);
56  virtual int CloseVariable() {//grib_handle_delete(h);
57  fclose(_inFile); return 0;}
58  virtual int Read(float *values);
59  virtual int ReadSlice(float *slice);
60  virtual bool VariableExists(size_t ts, string varname,
61  int reflevel=0, int lod=0) const; // not pure
62  virtual void GetLatLonExtents(size_t ts, double lon_exts[2],
63  double lat_exts[2]) const;
64  virtual std::vector<std::string> GetVariables2DExcluded() const;
65  virtual std::vector<std::string> GetVariables3DExcluded() const;
66 
67  virtual long GetNumTimeSteps() const; // from Metadata.h
68  virtual void GetGridDim(size_t dim[3]) const; // from Metadata.h
69  virtual std::vector<string> GetVariables3D() const; // from Metadata.h
70  virtual std::vector<string> GetVariables2DXY() const; // from Metadata.h
71  virtual double GetTSUserTime(size_t ts) const {return _gribTimes[ts];} // from Metadata.h
72  virtual std::vector<string> GetVariables2DXZ() const { // from Metadata.h
73  std::vector<string> empty; return(empty);};
74  virtual std::vector<string> GetVariables2DYZ() const { // from Metadata.h
75  std::vector<string> empty; return(empty);};
76  // END DCReader Functions
78 
79 
81  // Metadata Pure Virtual Functions
82  virtual void GetBlockSize(size_t bs[3], int reflevel) const { GetGridDim(bs); }
83  virtual std::vector<double> GetExtents(size_t ts = 0) const {return _cartographicExtents;}//_cartesianExtents;}
84  virtual std::vector<long> GetPeriodicBoundary() const; // Needs implementation!
85  virtual std::vector<long> GetGridPermutation() const; // Needs implementation!
86  virtual void GetTSUserTimeStamp(size_t ts, std::string &s) const;
87  virtual std::string GetMapProjection() const;
88  // END Metadata Virtual Functions
90 
91  vector <double> GetZCoordsInMeters() const {return _vertCoordinates;}
92  void _LinearInterpolation(float* values);
93 
94  private:
96  // Convenience functions
97  //int _Initialize(std::vector<std::map<std::string, std::string> > records);
98  int _Initialize(vector<string> files);
99  int _SetGribEnv();
100  int PrintVar(string var);
101  float GetLevel(int index) const {return _pressureLevels[index];}
102  void PrintLevels();
103  void Print3dVars();
104  void Print1dVars();
105  double BarometricFormula(const double pressure) const;
106  int _InitCartographicExtents(string mapProj);
108 
109  struct MessageLocation {
110  string fileName;
111  int offset;
112  };
113 
114  class Variable {
115  public:
116  Variable();
117  ~Variable();
119  void _AddTime(double t) {_unitTimes.push_back(t);}
120  void _AddMessage(int msg) {_messages.push_back(msg);}
121  void _AddLevel(float lvl) {_pressureLevels.push_back(lvl);}
122  void _AddIndex(double time, float level, string file, int offset);
123 
124  int GetOffset(double time, float level) const;
125  string GetFileName(double time, float level) const;
126  std::vector<int> GetMessages() const {return _messages;}
127  std::vector<double> GetTimes() const {return _unitTimes;}
128  std::vector<float> GetLevels() const {return _pressureLevels;}
129  float GetLevel(int index) const {return _pressureLevels[index];}
130  void PrintTimes();
131  void PrintMessages();
132  void PrintIndex(double time, float level);
133  void _SortLevels() {sort(_pressureLevels.begin(), _pressureLevels.end());}
134  void _SortTimes() {sort(_unitTimes.begin(), _unitTimes.end());}
135  bool _Exists(double time) const;
136  void setScanDirection(bool i, bool j) {iScan = i; jScan = j;}
137  void setOperatingCenter(string oc) {_operatingCenter = oc;}
138  void setParamId(int id) {_paramId = id;}
139  string getOperatingCenter() {return _operatingCenter;}
140  int getParamId() {return _paramId;}
141  bool getiScan() const {return iScan;}
142  bool getjScan() const {return jScan;}
143 
144  private:
145  // time level
146  std::map<double, std::map<float, MessageLocation> > _indices;
147  std::vector<int> _messages;
148  std::vector<float> _pressureLevels;
149 
151  std::vector<size_t> _varTimes;
153  std::vector<double> _unitTimes;
154  string _operatingCenter;
155  int _paramId;
156  bool iScan;
157  bool jScan;
158  bool isGaussian;
159  };
160 
161  bool _ignoreForecastData;
162  static int _sliceNum;
163  int _Ni;
164  int _Nj;
165  double _DxInMetres;
166  double _DyInMetres;
167  double _LaDInDegrees;
168  double _LoVInDegrees;
169  double _minLat;
170  double _minLon;
171  double _maxLat;
172  double _maxLon;
173  bool _iScanNeg;
174  bool _jScanPos;
175  string _gridType;
176  string _openVar;
177  string _Latin1InDegrees;
178  string _Latin2InDegrees;
179  string _orientationOfTheGridInDegrees;
180 
181  static int _openTS;
182  FILE* _inFile;
183  grib_handle* h;
184  std::vector<double> _pressureLevels;
185  std::vector<double> _vertCoordinates;
186  std::vector<double> _cartesianExtents;
187  std::vector<double> _cartographicExtents;
188  std::vector<double> _gribTimes;
189  std::map<std::string, Variable*> _vars1d;
190  std::map<std::string, Variable*> _vars2d;
191  std::map<std::string, Variable*> _vars3d;
192  double* _gaussianLats;
193  std::vector<double> _weights;
194  std::vector<int> _latIndices;
195  float* _iValues;
196  double* _regularLats;
197 
198  UDUnits *_udunit;
199 
200  class GribParser {
201  public:
202  GribParser();
203  ~GribParser();
204  int _LoadRecord(string file, size_t offset);
205  int _LoadRecordKeys(const string file); // loads only the keys that we need for vdc creation
206  int _VerifyKeys(); // verifies that key/values conform to our reqs
207  std::vector<std::map<std::string, std::string> > GetRecords() const {return _recordKeys;}
208  int doWeIgnoreForecastTimes() {return _ignoreForecastTimes;}
209 
210  private:
211  int _err;
212  std::vector<std::map<std::string, std::string> > _recordKeys;
213  std::vector<std::string> _consistentKeys;
214  std::vector<std::string> _varyingKeys;
215  bool _recordKeysVerified;
216  int _ignoreForecastTimes;
217  DCReaderGRIB *_metadata;
218 
219  // vars for key iteration
220  unsigned long _key_iterator_filter_flags;
221  int _grib_count;
222  char* _value;
223  size_t _vlen;
224 
225  // vars for data dump
226  double *_values;
227  double _min,_max,_average;
228  size_t _values_len;
229  string _filename;
230  };
231 
232  GribParser *parser;
233 
234  class Record {
235  public:
236  Record();
237  Record(string file, int recordNum);
238  ~Record();
239  void setTime(string input) {time = input;}
240  void setUnits(string input) {units = input;}
241  void setNi(string input) {Ni = input;}
242  void setNj(string input) {Nj = input;}
243  void setGridType(string input) {gridType = input;}
244  void setVarName(string input) {varName = input;}
245  string getTime() {return time;}
246  string getUnits() {return units;}
247  string getNi() {return Ni;}
248  string getNj() {return Nj;}
249  string getGridType() {return gridType;}
250  string getVarName() {return varName;}
251 
252  private:
253  string time;
254  string units;
255  string Ni;
256  string Nj;
257  string gridType;
258  string varName;
259  };
260  };
261 };
262 #endif // GRIBPARSER_H
#define VDF_API
Definition: common.h:61
virtual double GetTSUserTime(size_t ts) const
Definition: DCReaderGRIB.h:71
virtual int CloseVariable()
Definition: DCReaderGRIB.h:56
virtual std::vector< double > GetExtents(size_t ts=0) const
Definition: DCReaderGRIB.h:83
virtual void GetBlockSize(size_t bs[3], int reflevel) const
Definition: DCReaderGRIB.h:82
vector< double > GetZCoordsInMeters() const
Definition: DCReaderGRIB.h:91
virtual std::vector< string > GetVariables2DYZ() const
Definition: DCReaderGRIB.h:74
virtual std::vector< double > GetTSZCoords(size_t) const
Definition: DCReaderGRIB.h:50
virtual std::vector< string > GetVariables2DXZ() const
Definition: DCReaderGRIB.h:72