OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
GridAggregateOnOuterDimension.cc
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 
31 
32 #include "AggregationException.h"
33 #include "AggregationUtil.h" // agg_util
34 #include "Array.h" // libdap
35 #include "ArrayAggregateOnOuterDimension.h" // agg_util
36 #include "DataDDS.h" // libdap
37 #include "DDS.h" // libdap
38 #include "DDSLoader.h" // agg_util
39 #include "Dimension.h" // agg_util
40 #include "Grid.h" // libdap
41 #include <memory> // auto_ptr
42 #include "NCMLDebug.h" // ncml_module
43 #include "NCMLUtil.h" // ncml_module
44 #include <sstream>
45 
46 using libdap::Array;
47 using libdap::DataDDS;
48 using libdap::DDS;
49 using libdap::Grid;
50 
51 namespace agg_util
52 {
53 
54  // Local flag for whether to print constraints, to help debugging
55 // unused jhrg 4/16/14 static const bool PRINT_CONSTRAINTS = true;
56 
57  // BES Debug output channel for this file.
58  static const string DEBUG_CHANNEL("ncml:2");
59 
60  // Copy local data
61 void
62 GridAggregateOnOuterDimension::duplicate(const GridAggregateOnOuterDimension& rhs)
63 {
64  _newDim = rhs._newDim;
65 }
66 
68  const Grid& proto,
69  const Dimension& newDim,
70  const AMDList& memberDatasets,
71  const DDSLoader& loaderProto)
72 // this should give us map vectors and the member array rank (without new dim).
73 : GridAggregationBase(proto, memberDatasets, loaderProto)
74 , _newDim(newDim)
75 {
76  BESDEBUG(DEBUG_CHANNEL, "GridAggregateOnOuterDimension() ctor called!" << endl);
77 
78  createRep(memberDatasets);
79 }
80 
82 : GridAggregationBase(proto)
83 , _newDim()
84 {
85  BESDEBUG(DEBUG_CHANNEL, "GridAggregateOnOuterDimension() copy ctor called!" << endl);
86  duplicate(proto);
87 }
88 
91 {
92  return new GridAggregateOnOuterDimension(*this);
93 }
94 
97 {
98  if (this != &rhs)
99  {
100  cleanup();
102  duplicate(rhs);
103  }
104  return *this;
105 }
106 
108 {
109  BESDEBUG(DEBUG_CHANNEL, "~GridAggregateOnOuterDimension() dtor called!" << endl);
110  cleanup();
111 }
112 
114 // Helpers
115 
116 void
117 GridAggregateOnOuterDimension::createRep(const AMDList& memberDatasets)
118 {
119  BESDEBUG_FUNC(DEBUG_CHANNEL,
120  "Replacing the Grid's data Array with an ArrayAggregateOnOuterDimension..." << endl);
121 
122  // This is the prototype we need. It will have been set in the ctor.
123  Array* pArr = static_cast<Array*>(array_var());
124  NCML_ASSERT_MSG(pArr,
125  "Expected to find a contained data Array but we did not!");
126 
127  // Create the Grid version of the read getter and make a new AAOOD from our state.
128  std::auto_ptr<ArrayGetterInterface> arrayGetter(new TopLevelGridDataArrayGetter());
129 
130  // Create the subclass that does the work and replace our data array with it.
131  // Note this ctor will prepend the new dimension itself, so we do not.
132  std::auto_ptr<ArrayAggregateOnOuterDimension> aggDataArray(
134  *pArr, // prototype, already should be setup properly _without_ the new dim
135  memberDatasets,
136  arrayGetter,
137  _newDim
138  ));
139 
140  // Make sure null since sink function
141  // called on the auto_ptr
142  NCML_ASSERT(!(arrayGetter.get()));
143 
144  // Replace our data Array with this one. Will delete old one and may throw.
145  set_array(aggDataArray.get());
146 
147  // Release here on successful set since set_array uses raw ptr only.
148  // In case we threw then auto_ptr cleans up itself.
149  aggDataArray.release();
150 }
151 
152 void
153 GridAggregateOnOuterDimension::cleanup() throw()
154 {
155 }
156 
157 /* virtual */
158 void
160 {
161  VALID_PTR(pSubGrid);
162  transferConstraintsToSubGridMaps(pSubGrid);
163  transferConstraintsToSubGridArray(pSubGrid);
164 }
165 
166 /* virtual */
167 const Dimension&
169 {
170  return _newDim;
171 }
172 
173 void
174 GridAggregateOnOuterDimension::transferConstraintsToSubGridMaps(Grid* pSubGrid)
175 {
176  BESDEBUG(DEBUG_CHANNEL, "Transferring constraints to the subgrid maps..." << endl);
177  Map_iter subGridMapIt = pSubGrid->map_begin();
178  for (Map_iter it = map_begin(); it != map_end(); ++it)
179  {
180  // Skip the new outer dimension
181  if (it == map_begin())
182  {
183  continue;
184  }
185  Array* subGridMap = static_cast<Array*>(*subGridMapIt);
186  Array* superGridMap = static_cast<Array*>(*it);
188  *superGridMap,
189  false, // skipFirstDim = false since map sizes consistent
190  false, // same rank, dont skip this one either
191  true, // printDebug
192  DEBUG_CHANNEL); // debugChannel
193  ++subGridMapIt; // keep iterators in sync
194  }
195 }
196 
197 void
198 GridAggregateOnOuterDimension::transferConstraintsToSubGridArray(Grid* pSubGrid)
199 {
200  BESDEBUG(DEBUG_CHANNEL, "Transferring constraints to the subgrid array..." << endl);
201 
202  Array* pSubGridArray = static_cast<Array*>(pSubGrid->get_array());
203  VALID_PTR(pSubGridArray);
204  Array* pThisArray = static_cast<Array*>(array_var());
205  VALID_PTR(pThisArray);
206 
207  // transfer, skipping first dim which is the new one.
209  pSubGridArray, // into the prototype
210  *pThisArray, // from the output array (with valid constraints)
211  true, // skipFirstDim: need to skip since the ranks differ
212  false, // but not into the to array
213  true, // printDebug
214  DEBUG_CHANNEL
215  );
216 }
217 
218 } // namespace agg_util
#define BESDEBUG_FUNC(channel, info)
Definition: NCMLDebug.h:58
#define NCML_ASSERT(cond)
Definition: NCMLDebug.h:80
Helper class for temporarily hijacking an existing dhi to load a DDX response for one particular file...
#define NCML_ASSERT_MSG(cond, msg)
Definition: NCMLDebug.h:83
class GridAggregateOnOuterDimension : public GridAggregationBase
virtual const Dimension & getAggregationDimension() const
Get the contained aggregation dimension info.
Struct for holding information about a dimension of data, minimally a name and a cardinality (size)...
Definition: Dimension.h:50
GridAggregateOnOuterDimension & operator=(const GridAggregateOnOuterDimension &rhs)
GridAggregateOnOuterDimension(const Grid &proto, const Dimension &newDim, const AMDList &memberDatasets, const DDSLoader &loaderProto)
Create the new Grid from the template proto...
virtual GridAggregateOnOuterDimension * ptr_duplicate()
std::vector< RCPtr< AggMemberDataset > > AMDList
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.
#define VALID_PTR(ptr)
Definition: NCMLDebug.h:88
#define BESDEBUG(x, y)
macro used to send debug information to the debug stream
Definition: BESDebug.h:64
GridAggregationBase & operator=(const GridAggregationBase &rhs)
virtual void transferConstraintsToSubGridHook(Grid *pSubGrid)
For the data array and all maps, transfer the constraints from the super grid (ie this) to all the gr...