bes  Updated for version 3.20.10
AggregationUtil.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 __AGG_UTIL__AGGREGATION_UTIL_H__
30 #define __AGG_UTIL__AGGREGATION_UTIL_H__
31 
32 #include <libdap/AttrTable.h>
33 #include <iostream>
34 #include <string>
35 #include <vector>
36 
37 namespace libdap {
38 class Array;
39 class BaseType;
40 class Constructor;
41 class DDS;
42 class Grid;
43 }
44 
45 namespace agg_util {
46 class AggMemberDataset;
47 struct Dimension; // jhrg 4/16/14 class Dimension;
48 }
49 
50 namespace agg_util {
51 
60  virtual ~ArrayGetterInterface();
61 
63  virtual ArrayGetterInterface* clone() const = 0;
64 
82  virtual libdap::Array* readAndGetArray(const std::string& name, const libdap::DDS& dds,
83  const libdap::Array* const pConstraintTemplate, const std::string& debugChannel) const = 0;
84 };
85 // class ArrayGetterInterface
86 
96  virtual ~TopLevelArrayGetter();
97 
98  virtual TopLevelArrayGetter* clone() const;
99 
106  virtual libdap::Array* readAndGetArray(const std::string& name, const libdap::DDS& dds,
107  const libdap::Array* const pConstraintTemplate, const std::string& debugChannel) const;
108 };
109 // class TopLevelArrayGetter
110 
113  virtual ~TopLevelGridDataArrayGetter();
114 
115  virtual TopLevelGridDataArrayGetter* clone() const;
116 
128  virtual libdap::Array* readAndGetArray(const std::string& name, const libdap::DDS& dds,
129  const libdap::Array* const pConstraintTemplate, const std::string& debugChannel) const;
130 };
131 // class TopLevelGridDataArrayGetter
132 
134  TopLevelGridMapArrayGetter(const std::string& gridName);
135  virtual ~TopLevelGridMapArrayGetter();
136 
137  virtual TopLevelGridMapArrayGetter* clone() const;
138 
149  virtual libdap::Array* readAndGetArray(const std::string& name, const libdap::DDS& dds,
150  const libdap::Array* const pConstraintTemplate, const std::string& debugChannel) const;
151 
152  // The name of the Grid within which the desired map is contained.
153  const string _gridName;
154 };
155 // class TopLevelGridMapArrayGetter
156 
165 private:
166  // This is a static class for now...
168  {
169  }
170  ~AggregationUtil()
171  {
172  }
173 
174  static int d_last_added_cv_position;
175 
176 public:
177 
178  // Typedefs
179  typedef std::vector<const libdap::DDS*> ConstDDSList;
180 
187  static void performUnionAggregation(libdap::DDS* pOutputUnion, const ConstDDSList& datasetsInOrder);
188 
195  static void unionAttrsInto(libdap::AttrTable* pOut, const libdap::AttrTable& fromTable);
196 
201  static bool findAttribute(const libdap::AttrTable& inTable, const string& name, libdap::AttrTable::Attr_iter& attr);
202 
208  static void unionAllVariablesInto(libdap::DDS* pOutputUnion, const ConstDDSList& datasetsInOrder);
209 
215  static void resetCVInsertionPosition();
216 
221  static void unionAllVariablesInto(libdap::DDS* pOutputUnion, const libdap::DDS& fromDDS, bool add_at_top = false);
222 
229  static bool addCopyOfVariableIfNameIsAvailable(libdap::DDS* pOutDDS, const libdap::BaseType& varProto,
230  bool add_at_top = false);
231 
239  static void addOrReplaceVariableForName(libdap::DDS* pOutDDS, const libdap::BaseType& varProto);
240 
246  static libdap::BaseType* findVariableAtDDSTopLevel(const libdap::DDS& dds, const string& name);
247 
258  template<class LibdapType> static LibdapType* findTypedVariableAtDDSTopLevel(const libdap::DDS& dds,
259  const string& name);
260 
261 #if 0
286  static void produceOuterDimensionJoinedArray(libdap::Array* pJoinedArray, const std::string& joinedArrayName,
287  const std::string& newOuterDimName, const std::vector<libdap::Array*>& fromVars, bool copyData);
288 #endif
289 
296  static bool validateArrayTypesAndShapesMatch(const std::vector<libdap::Array*>& arrays,
297  bool enforceMatchingDimNames);
298 
300  static bool doTypesMatch(const libdap::Array& lhs, const libdap::Array& rhs);
301 
305  static bool doShapesMatch(const libdap::Array& lhs, const libdap::Array& rhs, bool checkDimNames);
306 
315  static unsigned int collectVariableArraysInOrder(std::vector<libdap::Array*>& varArrays,
316  const std::string& collectVarName, const ConstDDSList& datasetsInOrder);
317 
323  static bool couldBeCoordinateVariable(libdap::BaseType* pBT);
324 
325 #if 0
347  static void joinArrayData(libdap::Array* pAggArray, const std::vector<libdap::Array*>& varArrays,
348  bool reserveStorage = true, bool clearDataAfterUse = false);
349 #endif
350 
352  static void printDimensions(std::ostream& os, const libdap::Array& fromArray);
353 
359  static void printConstraints(std::ostream& os, const libdap::Array& fromArray);
360 
366  static void printConstraintsToDebugChannel(const std::string& debugChannel, const libdap::Array& fromArray);
367 
388  static void transferArrayConstraints(libdap::Array* pToArray, const libdap::Array& fromArray, bool skipFirstFromDim,
389  bool skipFirstToDim, bool printDebug = false, const std::string& debugChannel = "agg_util");
390 
397  static libdap::BaseType* getVariableNoRecurse(const libdap::DDS& dds, const std::string& name);
398 
405  static libdap::BaseType* getVariableNoRecurse(const libdap::Constructor& varContainer, const std::string& name);
406 
414  static libdap::Array* getAsArrayIfPossible(libdap::BaseType* pBT);
415 
417  static const libdap::Array* findMapByName(const libdap::Grid& inGrid, const std::string& findName);
418 
437  static libdap::Array* readDatasetArrayDataForAggregation(const libdap::Array& constrainedTemplateArray,
438  const std::string& varName, AggMemberDataset& dataset, const ArrayGetterInterface& arrayGetter,
439  const std::string& debugChannel);
440 
459  static void addDatasetArrayDataToAggregationOutputArray(libdap::Array& oOutputArray, // output location
460  unsigned int atIndex, // oOutputArray[atIndex] will be where data put
461  const libdap::Array& constrainedTemplateArray, // for copying constraints
462  const string& varName, // top level var to find in dataset DDS
463  AggMemberDataset& dataset, // Dataset who's DDS should be searched
464  const ArrayGetterInterface& arrayGetter, // alg for getting Array from DDS
465  const string& debugChannel // if !"", debug output goes to this channel.
466  );
467 
476  static void gatherMetadataChangesFrom(libdap::BaseType* pIntoVar, const libdap::BaseType& fromVar);
477 
478 };
479 // class AggregationUtil
480 
487 template<typename T>
488 void clearVectorAndDeletePointers(std::vector<T*>& vecToClear)
489 {
490  while (!vecToClear.empty()) {
491  T* pElt = vecToClear.back();
492  delete pElt;
493  vecToClear.pop_back();
494  }
495 }
496 
498 template<class T>
499 void clearAndUnrefAllElements(std::vector<T*>& vecToClear)
500 {
501  while (!vecToClear.empty()) {
502  T* pElt = vecToClear.back();
503  pElt->unref();
504  vecToClear.pop_back();
505  }
506 }
507 
509 template<class T>
510 void appendVectorOfRCObject(std::vector<T*>& intoVec, const std::vector<T*>& fromVec)
511 {
512  class std::vector<T*>::const_iterator it;
513  class std::vector<T*>::const_iterator endIt = fromVec.end();
514  for (it = fromVec.begin(); it != endIt; ++it) {
515  T* pElt = *it;
516  if (pElt) {
517  pElt->ref();
518  }
519  intoVec.push_back(pElt);
520  }
521 }
522 
523 }
524 
525 #endif /* __AGG_UTIL__AGGREGATION_UTIL_H__ */
static void addDatasetArrayDataToAggregationOutputArray(libdap::Array &oOutputArray, unsigned int atIndex, const libdap::Array &constrainedTemplateArray, const string &varName, AggMemberDataset &dataset, const ArrayGetterInterface &arrayGetter, const string &debugChannel)
static void printConstraintsToDebugChannel(const std::string &debugChannel, const libdap::Array &fromArray)
static void gatherMetadataChangesFrom(libdap::BaseType *pIntoVar, const libdap::BaseType &fromVar)
static bool doTypesMatch(const libdap::Array &lhs, const libdap::Array &rhs)
static unsigned int collectVariableArraysInOrder(std::vector< libdap::Array * > &varArrays, const std::string &collectVarName, const ConstDDSList &datasetsInOrder)
static LibdapType * findTypedVariableAtDDSTopLevel(const libdap::DDS &dds, const string &name)
static void transferArrayConstraints(libdap::Array *pToArray, const libdap::Array &fromArray, bool skipFirstFromDim, bool skipFirstToDim, bool printDebug=false, const std::string &debugChannel="agg_util")
static void printDimensions(std::ostream &os, const libdap::Array &fromArray)
static libdap::Array * readDatasetArrayDataForAggregation(const libdap::Array &constrainedTemplateArray, const std::string &varName, AggMemberDataset &dataset, const ArrayGetterInterface &arrayGetter, const std::string &debugChannel)
static void performUnionAggregation(libdap::DDS *pOutputUnion, const ConstDDSList &datasetsInOrder)
static libdap::BaseType * getVariableNoRecurse(const libdap::DDS &dds, const std::string &name)
static void unionAllVariablesInto(libdap::DDS *pOutputUnion, const ConstDDSList &datasetsInOrder)
static libdap::Array * getAsArrayIfPossible(libdap::BaseType *pBT)
static bool couldBeCoordinateVariable(libdap::BaseType *pBT)
static const libdap::Array * findMapByName(const libdap::Grid &inGrid, const std::string &findName)
static bool addCopyOfVariableIfNameIsAvailable(libdap::DDS *pOutDDS, const libdap::BaseType &varProto, bool add_at_top=false)
static void addOrReplaceVariableForName(libdap::DDS *pOutDDS, const libdap::BaseType &varProto)
static void unionAttrsInto(libdap::AttrTable *pOut, const libdap::AttrTable &fromTable)
static bool doShapesMatch(const libdap::Array &lhs, const libdap::Array &rhs, bool checkDimNames)
static bool findAttribute(const libdap::AttrTable &inTable, const string &name, libdap::AttrTable::Attr_iter &attr)
static void resetCVInsertionPosition()
static bool validateArrayTypesAndShapesMatch(const std::vector< libdap::Array * > &arrays, bool enforceMatchingDimNames)
static void printConstraints(std::ostream &os, const libdap::Array &fromArray)
static libdap::BaseType * findVariableAtDDSTopLevel(const libdap::DDS &dds, const string &name)
Helper class for temporarily hijacking an existing dhi to load a DDX response for one particular file...
void clearAndUnrefAllElements(std::vector< T * > &vecToClear)
void clearVectorAndDeletePointers(std::vector< T * > &vecToClear)
void appendVectorOfRCObject(std::vector< T * > &intoVec, const std::vector< T * > &fromVec)
virtual libdap::Array * readAndGetArray(const std::string &name, const libdap::DDS &dds, const libdap::Array *const pConstraintTemplate, const std::string &debugChannel) const =0
virtual ArrayGetterInterface * clone() const =0
virtual libdap::Array * readAndGetArray(const std::string &name, const libdap::DDS &dds, const libdap::Array *const pConstraintTemplate, const std::string &debugChannel) const
virtual TopLevelArrayGetter * clone() const
virtual libdap::Array * readAndGetArray(const std::string &name, const libdap::DDS &dds, const libdap::Array *const pConstraintTemplate, const std::string &debugChannel) const
virtual TopLevelGridDataArrayGetter * clone() const
virtual TopLevelGridMapArrayGetter * clone() const
virtual libdap::Array * readAndGetArray(const std::string &name, const libdap::DDS &dds, const libdap::Array *const pConstraintTemplate, const std::string &debugChannel) const