VAPoR  3.0.0
DCWRF.h
Go to the documentation of this file.
1 #include <vector>
2 #include <algorithm>
3 #include <map>
4 #include <iostream>
5 #include <vapor/MyBase.h>
6 #include <vapor/NetCDFCollection.h>
7 #include <vapor/Proj4API.h>
8 #include <vapor/UDUnitsClass.h>
9 #include <vapor/DC.h>
10 
11 #ifndef _DCWRF_H_
12 #define _DCWRF_H_
13 
14 namespace VAPoR {
15 
16 
27 class VDF_API DCWRF : public VAPoR::DC {
28 public:
29 
30 
34  DCWRF();
35  virtual ~DCWRF();
36 
52  //
53  virtual int Initialize(const vector <string> &paths);
54 
57  virtual bool GetDimension(
58  string dimname, DC::Dimension &dimension
59  ) const;
60 
63  virtual std::vector <string> GetDimensionNames() const;
64 
67  virtual bool GetCoordVarInfo(string varname, DC::CoordVar &cvar) const;
68 
71  virtual bool GetDataVarInfo( string varname, DC::DataVar &datavar) const;
72 
74  //
75  virtual bool GetBaseVarInfo(string varname, DC::BaseVar &var) const;
76 
77 
80  virtual std::vector <string> GetDataVarNames() const;
81 
82 
85  virtual std::vector <string> GetCoordVarNames() const;
86 
87 
90  virtual int GetNumRefLevels(string varname) const { return(1); }
91 
94  virtual int GetMapProjection(
95  string lonname, string latname, string &projstring
96  ) const;
97 
98 
101  virtual int GetAtt(
102  string varname, string attname, vector <double> &values
103  ) const;
104  virtual int GetAtt(
105  string varname, string attname, vector <long> &values
106  ) const;
107  virtual int GetAtt(
108  string varname, string attname, string &values
109  ) const;
110 
113  virtual std::vector <string> GetAttNames(string varname) const;
114 
117  virtual XType GetAttType(string varname, string attname) const;
118 
121  virtual int GetDimLensAtLevel(
122  string varname, int level, std::vector <size_t> &dims_at_level,
123  std::vector <size_t> &bs_at_level
124  ) const;
125 
126 
129  virtual int OpenVariableRead(
130  size_t ts, string varname, int , int
131  ) {
132  return(DCWRF::OpenVariableRead(ts, varname));
133  }
134 
135  virtual int OpenVariableRead(
136  size_t ts, string varname
137  );
138 
139 
142  virtual int CloseVariable();
143 
146  int virtual Read(float *data);
147 
150  virtual int ReadSlice(float *slice);
151 
153  //
154  virtual int ReadRegion(
155  const vector <size_t> &min, const vector <size_t> &max, float *region
156  );
157 
160  virtual int ReadRegionBlock(
161  const vector <size_t> &min, const vector <size_t> &max, float *region
162  );
163 
166  virtual int GetVar(string varname, int, int, float *data) {
167  return(DCWRF::GetVar(varname, data));
168  }
169  virtual int GetVar(string varname, float *data);
170 
173  virtual int GetVar(
174  size_t ts, string varname, int, int, float *data
175  ) {
176  return(DCWRF::GetVar(ts, varname, data));
177  }
178 
179  virtual int GetVar(
180  size_t ts, string varname, float *data
181  );
182 
183 
186  virtual bool VariableExists(
187  size_t ts,
188  string varname,
189  int reflevel = 0,
190  int lod = 0
191  ) const;
192 
193 private:
194  NetCDFCollection *_ncdfc;
195  VAPoR::UDUnits _udunits;
196 
197  //
198  // Various attributes from a WRF data file needed for computing map
199  // projections
200  //
201  float _dx;
202  float _dy;
203  float _cen_lat;
204  float _cen_lon;
205  float _pole_lat;
206  float _pole_lon;
207  float _grav;
208  float _radius;
209  float _p2si;
210  float _mapProj;
211 
212  int _ovr_fd; // File descriptor for currently opened file
213  string _projString;
214  Proj4API _proj4API;
215 
216  class DerivedVarHorizontal;
217  DerivedVarHorizontal *_derivedX;
218  DerivedVarHorizontal *_derivedY;
219  DerivedVarHorizontal *_derivedXU;
220  DerivedVarHorizontal *_derivedYU;
221  DerivedVarHorizontal *_derivedXV;
222  DerivedVarHorizontal *_derivedYV;
223 
224  class DerivedVarElevation;
225  DerivedVarElevation *_derivedElev;
226  DerivedVarElevation *_derivedElevU;
227  DerivedVarElevation *_derivedElevV;
228  DerivedVarElevation *_derivedElevW;
229 
230  class DerivedVarTime;
231  DerivedVarTime *_derivedTime;
232 
233  std::map <string, DC::Dimension> _dimsMap;
234  std::map <string, DC::CoordVar> _coordVarsMap;
235  std::map <string, DC::DataVar> _dataVarsMap;
236  std::vector <size_t> _timeLookup;
237 
238 
239  vector <size_t> _GetSpatialDims(
240  NetCDFCollection *ncdfc, string varname
241  ) const;
242 
243  vector <string> _GetSpatialDimNames(
244  NetCDFCollection *ncdfc, string varname
245  ) const;
246 
247  int _InitAtts(NetCDFCollection *ncdfc);
248 
249  int _GetProj4String(
250  NetCDFCollection *ncdfc, float radius, int map_proj, string &projstring
251  );
252 
253  int _InitProjection(NetCDFCollection *ncdfc, float radius);
254 
255  int _InitHorizontalCoordinates(NetCDFCollection *ncdfc, Proj4API *proj4API);
256 
257  int _InitVerticalCoordinates(NetCDFCollection *ncdfc);
258 
259  int _InitTime(NetCDFCollection *ncdfc);
260 
261  int _InitDimensions(NetCDFCollection *ncdfc);
262 
263  int _GetCoordVars(
264  NetCDFCollection *ncdfc,
265  string varname, vector <string> &cvarnames
266  );
267 
268  bool _GetVarCoordinates(
269  NetCDFCollection *ncdfc, string varname,
270  vector <DC::Dimension> &dimensions,
271  vector <string> &coordvars
272  );
273 
274  int _InitVars(NetCDFCollection *ncdfc);
275 
277  //
278  // Specializations of the NetCDFCollection::DerivedVar class used to
279  // support derived variables - required variables that are not
280  // found in the WRF data.
281  //
283 
284  //
285  // Elevation derived variable. This class computes a vertical coordinate
286  // in meters from the pressure variables found in a WRF data set
287  //
288  class DerivedVarElevation : public VAPoR::NetCDFCollection::DerivedVar {
289  public:
290  DerivedVarElevation(
291  NetCDFCollection *ncdfc, string name,
292  const vector <DC::Dimension> &dims, float grav
293  );
294  virtual ~DerivedVarElevation();
295 
296  virtual int Open(size_t ts);
297  virtual int ReadSlice(float *slice, int );
298  virtual int Read(float *buf, int );
299  virtual int SeekSlice(int offset, int whence, int );
300  virtual int Close(int fd);
301  virtual bool TimeVarying() const {return(true); };
302  virtual std::vector <size_t> GetSpatialDims() const { return(_sdims); }
303  virtual std::vector <string> GetSpatialDimNames() const {return(_sdimnames);}
304  virtual size_t GetTimeDim() const {return(_time_dim); }
305  virtual string GetTimeDimName() const {return(_time_dim_name); }
306  virtual bool GetMissingValue(double &mv) const { return(false); }
307 
308  private:
309  string _name; // name of derived variable
310  size_t _time_dim; // number of time steps
311  string _time_dim_name; // Name of time dimension
312  std::vector <size_t> _sdims; // spatial dimensions
313  std::vector <string> _sdimnames; // spatial dimension names
314  float _grav; // gravitational constant
315  string _PHvar; // name of PH variable
316  string _PHBvar; // name of PHB variable
317  float *_PH; // buffer for PH variable slice
318  float *_PHB; // buffer for PHB variable slice
319  float *_zsliceBuf; // buffer for z starggering interpolation
320  float *_xysliceBuf; // buffer for horizontal extrapolation
321  int _PHfd;
322  int _PHBfd; // file descriptors
323  bool _is_open; // variable open for reading?
324  bool _xextrapolate; // need extrapolation along X?
325  bool _yextrapolate; // need extrapolation along Y?
326  bool _zinterpolate; // need interpolation along Z?
327  std::vector <size_t>_ph_dims; // spatial dims of PH and PHB variables
328  bool _firstSlice; // true if first slice for current open variable
329 
330  int _ReadSlice(float *slice);
331  };
332 
333 
334  //
335  // Horizontal coordinate derived variables. This class computes
336  // horizontal coordinate variables in meters by using a map projection
337  // from geographic to cartographic coordinates
338  //
339  class DerivedVarHorizontal : public NetCDFCollection::DerivedVar {
340  public:
341  DerivedVarHorizontal(
342  NetCDFCollection *ncdfc, string name, const vector <DC::Dimension> &dims,
343  const vector <size_t> latlondims, Proj4API *proj4API
344  );
345  virtual ~DerivedVarHorizontal();
346 
347  virtual int Open(size_t ts);
348  virtual int ReadSlice(float *slice, int );
349  virtual int Read(float *buf, int );
350  virtual int SeekSlice(int offset, int whence, int );
351  virtual int Close(int fd);
352  virtual bool TimeVarying() const {return(true); };
353  virtual std::vector <size_t> GetSpatialDims() const { return(_sdims); }
354  virtual std::vector <string> GetSpatialDimNames() const {return(_sdimnames);}
355  virtual size_t GetTimeDim() const {return(_time_dim); }
356  virtual string GetTimeDimName() const {return(_time_dim_name); }
357  virtual bool GetMissingValue(double &mv) const { return(false); }
358  private:
359  string _name; // name of derived variable
360  size_t _time_dim; // number of time steps
361  string _time_dim_name; // Name of time dimension
362  std::vector <size_t> _sdims; // spatial dimensions
363  std::vector <string> _sdimnames; // spatial dimension names
364  size_t _nx;
365  size_t _ny; // spatial dimensions of XLONG and XLAT variables
366  bool _is_open; // Open for reading?
367  float *_coords; // cached coordinates
368  float *_sliceBuf; // space for reading lat an lon variables
369  float *_lonBdryBuf; // boundary points of lat and lon
370  float *_latBdryBuf; // boundary points of lat and lon
371  size_t _ncoords; // length of coords
372  size_t _cached_ts; // time step of cached coordinate
373  string _lonname;
374  string _latname; // name of lat and lon coordinate variables
375  Proj4API *_proj4API;
376  bool _hasStagCVars; // file has coord vars for staggered dimensions?
377 
378  int _GetCartCoords(size_t ts);
379  };
380 
381  //
382  // Time coordinate derived variables. This class computes
383  // a time coordinate variable with units in seconds.
384  //
385  class DerivedVarTime : public NetCDFCollection::DerivedVar {
386  public:
387  DerivedVarTime(
388  NetCDFCollection *ncdfc, DC::Dimension dims,
389  const std::vector <float> &timecoords
390  );
391  virtual ~DerivedVarTime() {}
392 
393  virtual int Open(size_t ts);
394  virtual int ReadSlice(float *slice, int );
395  virtual int Read(float *buf, int );
396  virtual int SeekSlice(int offset, int whence, int );
397  virtual int Close(int fd);
398  virtual bool TimeVarying() const {return(true); };
399  virtual std::vector <size_t> GetSpatialDims() const { return(_sdims); }
400  virtual std::vector <string> GetSpatialDimNames() const {return(_sdimnames);}
401  virtual size_t GetTimeDim() const {return(_timecoords.size()); }
402  virtual string GetTimeDimName() const {return(_time_dim_name); }
403  virtual bool GetMissingValue(double &mv) const { return(false); }
404  private:
405  string _time_dim_name; // Name of time dimension
406  std::vector <size_t> _sdims; // spatial dimensions
407  std::vector <string> _sdimnames; // spatial dimension names
408  vector <float> _timecoords; // cached coordinates
409  size_t _ts; // current time step
410  };
411 
412 };
413 };
414 
415 #endif
Base class for storing variable metadata.
Definition: DC.h:307
virtual int OpenVariableRead(size_t ts, string varname, int, int)
Definition: DCWRF.h:129
Coordinate variable metadata.
Definition: DC.h:472
Class for reading a WRF data set stored as a series of NetCDF files.
Definition: DCWRF.h:27
virtual int GetVar(string varname, int, int, float *data)
Definition: DCWRF.h:166
Metadata describing a coordinate dimension.
Definition: DC.h:161
virtual int GetNumRefLevels(string varname) const
Definition: DCWRF.h:90
virtual int GetVar(size_t ts, string varname, int, int, float *data)
Definition: DCWRF.h:173
Defines API for reading a collection of data.
Definition: DC.h:150
#define VDF_API
Definition: DC.h:10
Data variable metadata.
Definition: DC.h:557