OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
AggregationUtil.h
Go to the documentation of this file.
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 __AGG_UTIL__AGGREGATION_UTIL_H__
30 #define __AGG_UTIL__AGGREGATION_UTIL_H__
31 
32 #include <AttrTable.h>
33 #include <iostream>
34 #include <string>
35 #include <vector>
36 
37 namespace libdap
38 {
39  class Array;
40  class BaseType;
41  class Constructor;
42  class DataDDS;
43  class DDS;
44  class Grid;
45 };
46 
47 namespace agg_util
48 {
49  class AggMemberDataset;
50  struct Dimension;
51  // jhrg 4/16/14 class Dimension;
52 };
53 
54 namespace agg_util
55 {
56 
65  {
66  virtual ~ArrayGetterInterface();
67 
69  virtual ArrayGetterInterface* clone() const = 0;
70 
88  virtual libdap::Array* readAndGetArray(
89  const std::string& name,
90  const libdap::DataDDS& dds,
91  const libdap::Array* const pConstraintTemplate,
92  const std::string& debugChannel
93  ) const = 0;
94  }; // class ArrayGetterInterface
95 
104  {
106  virtual ~TopLevelArrayGetter();
107 
108  virtual TopLevelArrayGetter* clone() const;
109 
116  virtual libdap::Array* readAndGetArray(
117  const std::string& name,
118  const libdap::DataDDS& dds,
119  const libdap::Array* const pConstraintTemplate,
120  const std::string& debugChannel
121  ) const;
122  }; // class TopLevelArrayGetter
123 
125  {
128 
129  virtual TopLevelGridDataArrayGetter* clone() const;
130 
142  virtual libdap::Array* readAndGetArray(
143  const std::string& name,
144  const libdap::DataDDS& dds,
145  const libdap::Array* const pConstraintTemplate,
146  const std::string& debugChannel
147  ) const;
148  }; // class TopLevelGridDataArrayGetter
149 
151  {
152  TopLevelGridMapArrayGetter(const std::string& gridName);
153  virtual ~TopLevelGridMapArrayGetter();
154 
155  virtual TopLevelGridMapArrayGetter* clone() const;
156 
167  virtual libdap::Array* readAndGetArray(
168  const std::string& name,
169  const libdap::DataDDS& dds,
170  const libdap::Array* const pConstraintTemplate,
171  const std::string& debugChannel
172  ) const;
173 
174  // The name of the Grid within which the desired map is contained.
175  const string _gridName;
176  }; // class TopLevelGridMapArrayGetter
177 
186  {
187  private: // This is a static class for now...
188  AggregationUtil() {}
189  ~AggregationUtil() {}
190 
191  static int d_last_added_cv_position;
192 
193  public:
194 
195  // Typedefs
196  typedef std::vector<const libdap::DDS*> ConstDDSList;
197 
204  static void performUnionAggregation(
205  libdap::DDS* pOutputUnion,
206  const ConstDDSList& datasetsInOrder);
207 
214  static void unionAttrsInto(libdap::AttrTable* pOut, const libdap::AttrTable& fromTable);
215 
220  static bool findAttribute(const libdap::AttrTable& inTable, const string& name, libdap::AttrTable::Attr_iter& attr);
221 
227  static void unionAllVariablesInto(libdap::DDS* pOutputUnion, const ConstDDSList& datasetsInOrder);
228 
234  static void resetCVInsertionPosition();
235 
240  static void unionAllVariablesInto(libdap::DDS* pOutputUnion, const libdap::DDS& fromDDS, bool add_at_top = false);
241 
248  static bool addCopyOfVariableIfNameIsAvailable(libdap::DDS* pOutDDS, const libdap::BaseType& varProto, bool add_at_top = false);
249 
257  static void addOrReplaceVariableForName(libdap::DDS* pOutDDS, const libdap::BaseType& varProto);
258 
264  static libdap::BaseType* findVariableAtDDSTopLevel(const libdap::DDS& dds, const string& name);
265 
276  template <class LibdapType> static LibdapType* findTypedVariableAtDDSTopLevel(const libdap::DDS& dds, const string& name);
277 
302  static void produceOuterDimensionJoinedArray(libdap::Array* pJoinedArray,
303  const std::string& joinedArrayName,
304  const std::string& newOuterDimName,
305  const std::vector<libdap::Array*>& fromVars,
306  bool copyData);
307 
314  static bool validateArrayTypesAndShapesMatch(const std::vector<libdap::Array*>& arrays,
315  bool enforceMatchingDimNames);
316 
318  static bool doTypesMatch(const libdap::Array& lhs, const libdap::Array& rhs);
319 
323  static bool doShapesMatch(const libdap::Array& lhs, const libdap::Array& rhs, bool checkDimNames);
324 
333  static unsigned int collectVariableArraysInOrder(
334  std::vector<libdap::Array*>& varArrays,
335  const std::string& collectVarName,
336  const ConstDDSList& datasetsInOrder);
337 
343  static bool couldBeCoordinateVariable(libdap::BaseType* pBT);
344 
366  static void joinArrayData(libdap::Array* pAggArray,
367  const std::vector<libdap::Array*>& varArrays,
368  bool reserveStorage=true,
369  bool clearDataAfterUse=false);
370 
372  static void printDimensions(std::ostream& os, const libdap::Array& fromArray);
373 
379  static void printConstraints(std::ostream& os, const libdap::Array& fromArray);
380 
386  static void printConstraintsToDebugChannel(const std::string& debugChannel, const libdap::Array& fromArray);
387 
408  static void transferArrayConstraints(
409  libdap::Array* pToArray,
410  const libdap::Array& fromArray,
411  bool skipFirstFromDim,
412  bool skipFirstToDim,
413  bool printDebug = false,
414  const std::string& debugChannel = "agg_util");
415 
422  static libdap::BaseType* getVariableNoRecurse(
423  const libdap::DDS& dds,
424  const std::string& name);
425 
432  static libdap::BaseType* getVariableNoRecurse(
433  const libdap::Constructor& varContainer,
434  const std::string& name);
435 
443  static libdap::Array* getAsArrayIfPossible(libdap::BaseType* pBT);
444 
446  static const libdap::Array* findMapByName(const libdap::Grid& inGrid, const std::string& findName);
447 
467  libdap::Array& oOutputArray, // output location
468  unsigned int atIndex, // oOutputArray[atIndex] will be where data put
469  const libdap::Array& constrainedTemplateArray, // for copying constraints
470  const string& varName, // top level var to find in dataset DDS
471  AggMemberDataset& dataset, // Dataset who's DDS should be searched
472  const ArrayGetterInterface& arrayGetter, // alg for getting Array from DDS
473  const string& debugChannel // if !"", debug output goes to this channel.
474  );
475 
484  static void gatherMetadataChangesFrom(
485  libdap::BaseType* pIntoVar,
486  const libdap::BaseType& fromVar);
487 
488  }; // class AggregationUtil
489 
496  template <typename T>
497  void clearVectorAndDeletePointers(std::vector<T*>& vecToClear)
498  {
499  while (!vecToClear.empty())
500  {
501  T* pElt = vecToClear.back();
502  delete pElt;
503  vecToClear.pop_back();
504  }
505  }
506 
508  template <class T>
509  void clearAndUnrefAllElements(std::vector<T*>& vecToClear)
510  {
511  while (!vecToClear.empty())
512  {
513  T* pElt = vecToClear.back();
514  pElt->unref();
515  vecToClear.pop_back();
516  }
517  }
518 
520  template <class T>
521  void appendVectorOfRCObject(std::vector<T*>& intoVec,
522  const std::vector<T*>& fromVec)
523  {
524  class std::vector<T*>::const_iterator it;
525  class std::vector<T*>::const_iterator endIt = fromVec.end();
526  for (it = fromVec.begin(); it != endIt; ++it)
527  {
528  T* pElt = *it;
529  if (pElt)
530  {
531  pElt->ref();
532  }
533  intoVec.push_back(pElt);
534  }
535  }
536 
537 }
538 
539 #endif /* __AGG_UTIL__AGGREGATION_UTIL_H__ */
static void gatherMetadataChangesFrom(libdap::BaseType *pIntoVar, const libdap::BaseType &fromVar)
Union fromVar's AttrTable (initially) with pIntoVar's AttrTable and replace pIntoVar's AttrTable with...
virtual libdap::Array * readAndGetArray(const std::string &name, const libdap::DataDDS &dds, const libdap::Array *const pConstraintTemplate, const std::string &debugChannel) const
Lookup name within the top level of DDS ONLY and return it as an Array* if it is a subclass of Array...
static LibdapType * findTypedVariableAtDDSTopLevel(const libdap::DDS &dds, const string &name)
Template wrapper for findVariableAtDDSTopLevel() which does the find but only return non-NULL if the ...
Abstract helper superclass for allowing lazy access to the DataDDS for an aggregation.
TopLevelGridMapArrayGetter(const std::string &gridName)
virtual libdap::Array * readAndGetArray(const std::string &name, const libdap::DataDDS &dds, const libdap::Array *const pConstraintTemplate, const std::string &debugChannel) const
Find's the Array using name as the name of a map to find within a top level Grid names in the constru...
virtual TopLevelGridDataArrayGetter * clone() const
Virtual constructor idiom.
static libdap::BaseType * findVariableAtDDSTopLevel(const libdap::DDS &dds, const string &name)
Find a variable with name at the top level of the DDS.
std::vector< const libdap::DDS * > ConstDDSList
static const libdap::Array * findMapByName(const libdap::Grid &inGrid, const std::string &findName)
Find the given map name in the given Grid and return it if found, else NULL.
Helper class for temporarily hijacking an existing dhi to load a DDX response for one particular file...
static bool couldBeCoordinateVariable(libdap::BaseType *pBT)
void appendVectorOfRCObject(std::vector< T * > &intoVec, const std::vector< T * > &fromVec)
Assumes T has ref()
static void unionAttrsInto(libdap::AttrTable *pOut, const libdap::AttrTable &fromTable)
Merge any attributes in tableToMerge whose names do not already exist within *pOut into pOut...
static bool validateArrayTypesAndShapesMatch(const std::vector< libdap::Array * > &arrays, bool enforceMatchingDimNames)
Scan all the arrays in arrays using the first as a template and make sure that they all have the same...
static class NCMLUtil overview
static void joinArrayData(libdap::Array *pAggArray, const std::vector< libdap::Array * > &varArrays, bool reserveStorage=true, bool clearDataAfterUse=false)
Copy the simple type data Vector for each Array in varArrays into pAggArray, sequentially, effectively appending all the row major data in each entry in varArray into the row major order of pAggArray.
static void performUnionAggregation(libdap::DDS *pOutputUnion, const ConstDDSList &datasetsInOrder)
Perform an NCML-type union aggregation on the datasets in datasetsInOrder into pOutputUnion.
static void addOrReplaceVariableForName(libdap::DDS *pOutDDS, const libdap::BaseType &varProto)
If a variable with the name varProto.name() doesn't exist, add a copy of varProto to pOutDDS...
Helper class hierarchy for acquiring variable of a certain type from a DDS.
A static class for encapsulating the aggregation functionality on libdap.
Concrete impl that simply finds the Array by looking for a variable of the given name at the top leve...
virtual libdap::Array * readAndGetArray(const std::string &name, const libdap::DataDDS &dds, const libdap::Array *const pConstraintTemplate, const std::string &debugChannel) const =0
Find, constrain, read, and return an Array inside the DDS using the given name information (somehow...
static unsigned int collectVariableArraysInOrder(std::vector< libdap::Array * > &varArrays, const std::string &collectVarName, const ConstDDSList &datasetsInOrder)
Fill in varArrays with Array's named collectVarName from each DDS in datasetsInOrder, in that order.
static libdap::BaseType * getVariableNoRecurse(const libdap::DDS &dds, const std::string &name)
Return the variable in dds top level (no recursing, no fully qualified name dot notation) if it exist...
static void produceOuterDimensionJoinedArray(libdap::Array *pJoinedArray, const std::string &joinedArrayName, const std::string &newOuterDimName, const std::vector< libdap::Array * > &fromVars, bool copyData)
Basic joinNew aggregation into pJoinedArray on the array of inputs fromVars.
static void resetCVInsertionPosition()
Used to reset the class field that tracks where Coordinate Variables (CVs) have been inserted into th...
static bool doTypesMatch(const libdap::Array &lhs, const libdap::Array &rhs)
Do the lhs and rhs have the same data type?
virtual TopLevelGridMapArrayGetter * clone() const
Virtual constructor idiom.
static bool doShapesMatch(const libdap::Array &lhs, const libdap::Array &rhs, bool checkDimNames)
Do the lhs and rhs have the same shapes? Only use size for dimension compares unless checkDimNames...
static void unionAllVariablesInto(libdap::DDS *pOutputUnion, const ConstDDSList &datasetsInOrder)
For each variable within the top level of each dataset in datasetsInOrder (forward iteration) add a c...
static bool findAttribute(const libdap::AttrTable &inTable, const string &name, libdap::AttrTable::Attr_iter &attr)
Lookup the attribute with given name in inTable and place a reference in attr.
virtual TopLevelArrayGetter * clone() const
Virtual constructor idiom.
static void addDatasetArrayDataToAggregationOutputArray(libdap::Array &oOutputArray, unsigned int atIndex, const libdap::Array &constrainedTemplateArray, const string &varName, AggMemberDataset &dataset, const ArrayGetterInterface &arrayGetter, const string &debugChannel)
Load the given dataset's DataDDS.
virtual libdap::Array * readAndGetArray(const std::string &name, const libdap::DataDDS &dds, const libdap::Array *const pConstraintTemplate, const std::string &debugChannel) const
Find the array as the data Array of a TOP-LEVEL Grid with name.
static void transferArrayConstraints(libdap::Array *pToArray, const libdap::Array &fromArray, bool skipFirstFromDim, bool skipFirstToDim, bool printDebug=false, const std::string &debugChannel="agg_util")
Copy the constraints from the from Array into the pToArray in Dim_iter order.
void clearAndUnrefAllElements(std::vector< T * > &vecToClear)
Assumes T has unref()
virtual ArrayGetterInterface * clone() const =0
Virtual constructor idiom.
static void printDimensions(std::ostream &os, const libdap::Array &fromArray)
Print out the dimensions name and size for the given Array into os.
static bool addCopyOfVariableIfNameIsAvailable(libdap::DDS *pOutDDS, const libdap::BaseType &varProto, bool add_at_top=false)
If a variable does not exist within pOutDDS (top level) with the same name as varProto, then place a clone of varProto (using virtual ctor ptr_duplicate) into pOutDDS.
static libdap::Array * getAsArrayIfPossible(libdap::BaseType *pBT)
If pBT is an Array type, cast and return it as the Array.
static void printConstraintsToDebugChannel(const std::string &debugChannel, const libdap::Array &fromArray)
Output using BESDEBUG to the debugChannel channel.
static void printConstraints(std::ostream &os, const libdap::Array &fromArray)
Stream out the current constraints for all the dimensions in the Array.
void clearVectorAndDeletePointers(std::vector< T * > &vecToClear)
For each ptr element pElt in vecToClear, delete pElt and remove it from vecToClear.