bes Updated for version 3.20.10
GridAggregateOnOuterDimension.cc
1
2// 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 <libdap/DataDDS.h> // libdap
35#include <libdap/DDS.h> // libdap
36#include <libdap/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 <libdap/Array.h> // libdap
48#include "ArrayAggregateOnOuterDimension.h" // agg_util
49
50using libdap::Array;
51using libdap::DataDDS;
52using libdap::DDS;
53using libdap::Grid;
54
55namespace 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.
61static const string DEBUG_CHANNEL("ncml:2");
62
63// Copy local data
64void 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
87GridAggregateOnOuterDimension*
88GridAggregateOnOuterDimension::ptr_duplicate()
89{
90 return new GridAggregateOnOuterDimension(*this);
91}
92
93GridAggregateOnOuterDimension&
94GridAggregateOnOuterDimension::operator=(const GridAggregateOnOuterDimension& rhs)
95{
96 if (this != &rhs) {
97 cleanup();
98 GridAggregationBase::operator=(rhs);
99 duplicate(rhs);
100 }
101 return *this;
102}
103
104GridAggregateOnOuterDimension::~GridAggregateOnOuterDimension()
105{
106 BESDEBUG(DEBUG_CHANNEL, "~GridAggregateOnOuterDimension() dtor called!" << endl);
107 cleanup();
108}
109
111// Helpers
112
113void 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
141void GridAggregateOnOuterDimension::cleanup() throw ()
142{
143}
144
145/* virtual */
147{
148 VALID_PTR(pSubGrid);
149 transferConstraintsToSubGridMaps(pSubGrid);
150 transferConstraintsToSubGridArray(pSubGrid);
151}
152
153/* virtual */
154const Dimension&
156{
157 return _newDim;
158}
159
160void 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
180void 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
static void transferArrayConstraints(libdap::Array *pToArray, const libdap::Array &fromArray, bool skipFirstFromDim, bool skipFirstToDim, bool printDebug=false, const std::string &debugChannel="agg_util")
virtual void transferConstraintsToSubGridHook(Grid *pSubGrid)
virtual const Dimension & getAggregationDimension() const
GridAggregateOnOuterDimension(const Grid &proto, const Dimension &newDim, const AMDList &memberDatasets, const DDSLoader &loaderProto)
Helper class for temporarily hijacking an existing dhi to load a DDX response for one particular file...