bes  Updated for version 3.20.6
NetcdfElement.h
1 // This file is part of the "NcML Module" project, a BES module designed
3 // to allow NcML files to be used to be used as a wrapper to add
4 // AIS to existing datasets of any format.
5 //
6 // Copyright (c) 2009 OPeNDAP, Inc.
7 // Author: Michael Johnson <m.johnson@opendap.org>
8 //
9 // For more information, please also see the main website: http://opendap.org/
10 //
11 // This library is free software; you can redistribute it and/or
12 // modify it under the terms of the GNU Lesser General Public
13 // License as published by the Free Software Foundation; either
14 // version 2.1 of the License, or (at your option) any later version.
15 //
16 // This library is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 // Lesser General Public License for more details.
20 //
21 // You should have received a copy of the GNU Lesser General Public
22 // License along with this library; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 //
25 // Please see the files COPYING and COPYRIGHT for more information on the GLPL.
26 //
27 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
29 #ifndef __NCML_MODULE__NETCDF_ELEMENT_H__
30 #define __NCML_MODULE__NETCDF_ELEMENT_H__
31 
32 #include "AggMemberDataset.h" // agg_util
33 #include "DDSAccessInterface.h"
34 #include "DDSLoader.h"
35 #include "NCMLElement.h"
36 
37 namespace libdap {
38 class BaseType;
39 class DDS;
40 }
41 
42 class BESDapResponse;
43 
44 namespace ncml_module {
45 
46 class AggregationElement;
47 class DimensionElement;
48 class NCMLParser;
49 class VariableElement;
50 
62 class NetcdfElement: public NCMLElement, // superclass
63  public virtual agg_util::DDSAccessRCInterface // interface
64 {
65 private:
66  NetcdfElement& operator=(const NetcdfElement& rhs); //disallow
67 
68 public:
69  static const std::string _sTypeName;
70  static const std::vector<std::string> _sValidAttributes;
71 
72  NetcdfElement();
73  NetcdfElement(const NetcdfElement& proto);
74  virtual ~NetcdfElement();
75  virtual const std::string& getTypeName() const;
76  virtual NetcdfElement* clone() const; // override clone with more specific subclass
77  virtual void setAttributes(const XMLAttributeMap& attrs);
78  virtual void handleBegin();
79  virtual void handleContent(const std::string& content);
80  virtual void handleEnd();
81  virtual std::string toString() const;
82 
83  // Accessors for attributes we deal with.
84  // TODO Add these as we support aggregation attributes
85  const std::string& location() const
86  {
87  return _location;
88  }
89  const std::string& id() const
90  {
91  return _id;
92  }
93  const std::string& title() const
94  {
95  return _title;
96  }
97  const std::string& coordValue() const
98  {
99  return _coordValue;
100  }
101  const std::string& ncoords() const
102  {
103  return _ncoords;
104  }
105 
106  bool hasNcoords() const
107  {
108  return !_ncoords.empty();
109  }
110 
116  unsigned int getNcoordsAsUnsignedInt() const;
117 
122  bool isValid() const;
123 
129  virtual const libdap::DDS* getDDS() const;
130 
134  virtual libdap::DDS* getDDS();
135 
136  bool getProcessedMetadataDirective() const
137  {
138  return _gotMetadataDirective;
139  }
140 
141  void setProcessedMetadataDirective()
142  {
143  _gotMetadataDirective = true;
144  }
145 
150  void borrowResponseObject(BESDapResponse* pResponse);
151 
155  void unborrowResponseObject(BESDapResponse* pResponse);
156 
163 
172 
178  const DimensionElement* getDimensionInLocalScope(const std::string& name) const;
179 
189  const DimensionElement* getDimensionInFullScope(const std::string& name) const;
190 
195  void addDimension(DimensionElement* dim);
196 
198  std::string printDimensions() const;
199 
201  void clearDimensions();
202 
204  const std::vector<DimensionElement*>& getDimensionElements() const;
205 
215  void setChildAggregation(AggregationElement* agg, bool throwIfExists = true);
216 
220 
225 
228 
231 
232 #if 0 // not sure we need this yet
233 
242  template <typename T> int getCoordValueVector(std::vector<T>& values) const;
243 #endif
244 
254  bool getCoordValueAsDouble(double& val) const;
255 
269  void addVariableToValidateOnClose(libdap::BaseType* pNewVar, VariableElement* pVE);
270 
281  void setVariableGotValues(libdap::BaseType* pVarToValidate, bool removeEntry);
282 
289  VariableElement* findVariableElementForLibdapVar(libdap::BaseType* pNewVar);
290 
300  static bool isLocationLexicographicallyLessThan(const NetcdfElement* pLHS, const NetcdfElement* pRHS);
301 
311  static bool isCoordValueLexicographicallyLessThan(const NetcdfElement* pLHS, const NetcdfElement* pRHS);
312 
313 private:
314 
316  void loadLocation();
317 
321  void throwOnUnsupportedAttributes();
322 
328  bool validateAttributeContextOrThrow() const;
329 
330  static std::vector<std::string> getValidAttributes();
331 
332 private:
333  std::string _location;
334  std::string _id;
335  std::string _title;
336  std::string _ncoords;
337  std::string _enhance;
338  std::string _addRecords;
339  std::string _coordValue;
340  std::string _fmrcDefinition;
341 
342  // Whether we got a metadata direction element { readMetadata | explicit } for this node yet.
343  // Just used to check for more than one.
344  bool _gotMetadataDirective;
345 
346  // Whether we own the memory for _response and need to destroy it in dtor or not.
347  bool _weOwnResponse;
348 
349  // true after loadLocation has been called.
350  bool _loaded;
351 
352  // Our response object
353  // We OWN it if we're not the root element,
354  // but the parser owns it if we are the root.
355  // The parser sets _weOwnResponse correctly for our dtor.
356  BESDapResponse* _response;
357 
358  // If non-null, a pointer to the singleton aggregation element for this dataset.
359  // We use an RCPtr to automatically ref() it when we set it
360  // and to unref() it in our dtor.
361  RCPtr<AggregationElement> _aggregation;
362 
363  // If we are nested within an aggregation element,
364  // this is a back ptr we can use to traverse upwards.
365  // If it null if we're a root dataset.
366  AggregationElement* _parentAgg;
367 
368  // A table of strong references to Dimensions valid for this element's lexical scope.
369  // We use raw DimensionElement*, but ref() them upon adding them to the vector
370  // and unref() them in the dtor.
371  // We won't have that many, so a vector is more efficient than a map for this.
372  std::vector<DimensionElement*> _dimensions;
373 
374  // Lazy evaluated, starts unassigned.
375  // When getAggMemberDataset() is called, it is lazy-constructed
376  // to weak ref the returned strong shared ptr (RCPtr).
378 
379 public:
380  // inner classes can't be private?
412  public:
414 
421 
431  void addVariableToValidate(libdap::BaseType* pNewVar, VariableElement* pVE);
432 
437  void removeVariableToValidate(libdap::BaseType* pVarToRemove);
438 
444  void setVariableGotValues(libdap::BaseType* pVarToValidate);
445 
451  bool validate();
452 
459  VariableElement* findVariableElementForLibdapVar(libdap::BaseType* pNewVar);
460 
461  private:
462  // disallow implicit copies
465 
466  public:
467 
468  class VVVEntry {
469  public:
470  VVVEntry() :
471  _pNewVar(0), _pVarElt(0)
472  {
473  }
474  VVVEntry(libdap::BaseType* pBT, VariableElement* pVE) :
475  _pNewVar(pBT), _pVarElt(pVE)
476  {
477  }
478  void clear()
479  {
480  _pNewVar = 0;
481  _pVarElt = 0;
482  }
483  libdap::BaseType* _pNewVar;
484  VariableElement* _pVarElt;
485  };
486 
487  /********************* helper functions *******************/
488 
494  private:
495  VariableValueValidator::VVVEntry* findEntryByLibdapVar(libdap::BaseType* pVarToFind);
496 
497  // We don't expect too many entries, so a simple vector is the best way to go,
498  // avoid overhead of maps, etc.
499  std::vector<VVVEntry> _entries;
500  NetcdfElement* _pParent;
501  }; // class VariableValueValidator
502 
503 private:
504  // The actual instance we will poke from methods in NetcdfElement
505  VariableValueValidator _variableValidator;
506 };
507 
508 }
509 
510 #endif /* __NCML_MODULE__NETCDF_ELEMENT_H__ */
ncml_module::NetcdfElement::getNcoordsAsUnsignedInt
unsigned int getNcoordsAsUnsignedInt() const
Definition: NetcdfElement.cc:237
ncml_module::NetcdfElement::VariableValueValidator::removeVariableToValidate
void removeVariableToValidate(libdap::BaseType *pVarToRemove)
Definition: NetcdfElement.cc:617
ncml_module::NetcdfElement::VariableValueValidator::setVariableGotValues
void setVariableGotValues(libdap::BaseType *pVarToValidate)
Definition: NetcdfElement.cc:629
ncml_module::NetcdfElement::setParentAggregation
void setParentAggregation(AggregationElement *parent)
Definition: NetcdfElement.cc:413
BESDapResponse
Represents an OPeNDAP DAP response object within the BES.
Definition: BESDapResponse.h:41
ncml_module::NetcdfElement::setChildAggregation
void setChildAggregation(AggregationElement *agg, bool throwIfExists=true)
Definition: NetcdfElement.cc:382
ncml_module::NetcdfElement
Concrete class for NcML <netcdf> element.
Definition: NetcdfElement.h:62
ncml_module::NetcdfElement::toString
virtual std::string toString() const
Definition: NetcdfElement.cc:196
ncml_module::NetcdfElement::addDimension
void addDimension(DimensionElement *dim)
Definition: NetcdfElement.cc:340
ncml_module::NetcdfElement::VariableValueValidator::VVVEntry
Definition: NetcdfElement.h:468
ncml_module::NetcdfElement::clone
virtual NetcdfElement * clone() const
Definition: NetcdfElement.cc:115
agg_util::DDSAccessRCInterface
Definition: DDSAccessInterface.h:62
ncml_module::NetcdfElement::isCoordValueLexicographicallyLessThan
static bool isCoordValueLexicographicallyLessThan(const NetcdfElement *pLHS, const NetcdfElement *pRHS)
Definition: NetcdfElement.cc:501
ncml_module::NetcdfElement::clearDimensions
void clearDimensions()
Definition: NetcdfElement.cc:366
ncml_module::NetcdfElement::VariableValueValidator::~VariableValueValidator
~VariableValueValidator()
Definition: NetcdfElement.cc:589
ncml_module::NetcdfElement::isLocationLexicographicallyLessThan
static bool isLocationLexicographicallyLessThan(const NetcdfElement *pLHS, const NetcdfElement *pRHS)
Definition: NetcdfElement.cc:491
ncml_module::NetcdfElement::handleEnd
virtual void handleEnd()
Definition: NetcdfElement.cc:168
agg_util::WeakRCPtr< agg_util::AggMemberDataset >
ncml_module::XMLAttributeMap
Definition: XMLHelpers.h:93
ncml_module::NetcdfElement::handleBegin
virtual void handleBegin()
Definition: NetcdfElement.cc:139
ncml_module::NetcdfElement::getDimensionInFullScope
const DimensionElement * getDimensionInFullScope(const std::string &name) const
Definition: NetcdfElement.cc:326
ncml_module::DimensionElement
Definition: DimensionElement.h:55
libdap
Definition: BESDapFunctionResponseCache.h:35
ncml_module::NetcdfElement::getParentDataset
NetcdfElement * getParentDataset() const
Definition: NetcdfElement.cc:398
ncml_module::NetcdfElement::handleContent
virtual void handleContent(const std::string &content)
Definition: NetcdfElement.cc:159
ncml_module::NetcdfElement::getDimensionElements
const std::vector< DimensionElement * > & getDimensionElements() const
Definition: NetcdfElement.cc:377
ncml_module::VariableElement
Concrete class for NcML <variable> element.
Definition: VariableElement.h:58
agg_util::DDSLoader::ResponseType
ResponseType
Definition: DDSLoader.h:94
ncml_module::NetcdfElement::getTypeName
virtual const std::string & getTypeName() const
Definition: NetcdfElement.cc:109
ncml_module::AggregationElement
Definition: AggregationElement.h:61
ncml_module::NetcdfElement::getChildAggregation
AggregationElement * getChildAggregation() const
Definition: NetcdfElement.cc:419
ncml_module::NetcdfElement::VariableValueValidator::addVariableToValidate
void addVariableToValidate(libdap::BaseType *pNewVar, VariableElement *pVE)
Definition: NetcdfElement.cc:602
ncml_module::NetcdfElement::getDimensionInLocalScope
const DimensionElement * getDimensionInLocalScope(const std::string &name) const
Definition: NetcdfElement.cc:310
ncml_module::NCMLElement
Base class for NcML element concrete classes.
Definition: NCMLElement.h:61
ncml_module::NetcdfElement::VariableValueValidator::validate
bool validate()
Definition: NetcdfElement.cc:645
ncml_module::NetcdfElement::unborrowResponseObject
void unborrowResponseObject(BESDapResponse *pResponse)
Definition: NetcdfElement.cc:256
ncml_module::NetcdfElement::VariableValueValidator
Definition: NetcdfElement.h:411
ncml_module::NetcdfElement::createResponseObject
void createResponseObject(agg_util::DDSLoader::ResponseType type)
Definition: NetcdfElement.cc:263
ncml_module::NetcdfElement::getAggMemberDataset
RCPtr< agg_util::AggMemberDataset > getAggMemberDataset() const
Definition: NetcdfElement.cc:279
ncml_module::NetcdfElement::printDimensions
std::string printDimensions() const
Definition: NetcdfElement.cc:354
ncml_module::NetcdfElement::getParentAggregation
AggregationElement * getParentAggregation() const
Definition: NetcdfElement.cc:408
ncml_module
NcML Parser for adding/modifying/removing metadata (attributes) to existing local datasets using NcML...
Definition: AggregationElement.cc:72
ncml_module::NetcdfElement::findVariableElementForLibdapVar
VariableElement * findVariableElementForLibdapVar(libdap::BaseType *pNewVar)
Definition: NetcdfElement.cc:640
ncml_module::NetcdfElement::setVariableGotValues
void setVariableGotValues(libdap::BaseType *pVarToValidate, bool removeEntry)
Definition: NetcdfElement.cc:481
agg_util::RCPtr
A reference to an RCObject which automatically ref() and deref() on creation and destruction.
Definition: RCObject.h:284
ncml_module::NetcdfElement::VariableValueValidator::findVariableElementForLibdapVar
VariableElement * findVariableElementForLibdapVar(libdap::BaseType *pNewVar)
Definition: NetcdfElement.cc:682
ncml_module::NetcdfElement::getCoordValueAsDouble
bool getCoordValueAsDouble(double &val) const
Definition: NetcdfElement.cc:457
ncml_module::NetcdfElement::getDDS
virtual const libdap::DDS * getDDS() const
Definition: NetcdfElement.cc:207
ncml_module::NetcdfElement::addVariableToValidateOnClose
void addVariableToValidateOnClose(libdap::BaseType *pNewVar, VariableElement *pVE)
Definition: NetcdfElement.cc:476
ncml_module::NetcdfElement::setAttributes
virtual void setAttributes(const XMLAttributeMap &attrs)
Definition: NetcdfElement.cc:120
ncml_module::NetcdfElement::isValid
bool isValid() const
Definition: NetcdfElement.cc:229
ncml_module::NetcdfElement::borrowResponseObject
void borrowResponseObject(BESDapResponse *pResponse)
Definition: NetcdfElement.cc:249