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