34 #include <libdap/Marshaller.h>
37 #include "BESStopWatch.h"
39 #include "ArrayJoinExistingAggregation.h"
41 #include "AggregationException.h"
42 #include "AggregationUtil.h"
43 #include "NCMLDebug.h"
45 static const string DEBUG_CHANNEL(NCML_MODULE_DBG_CHANNEL_2);
46 static const bool PRINT_CONSTRAINTS =
false;
50 #undef USE_LOCAL_TIMEOUT_SCHEME
55 const AMDList& memberDatasets, std::auto_ptr<ArrayGetterInterface>& arrayGetter,
const Dimension& joinDim) :
58 BESDEBUG_FUNC(DEBUG_CHANNEL,
"Making the aggregated outer dimension be: " + joinDim.
toString() +
"\n");
63 libdap::Array::dimension& rOuterDim = *(dim_begin());
64 NCML_ASSERT_MSG(rOuterDim.name == joinDim.name,
"The outer dimension name of this is not the expected "
65 "outer dimension name! Broken precondition: This ctor cannot be called "
66 "without this being true!");
67 rOuterDim.size = joinDim.size;
73 if (PRINT_CONSTRAINTS) {
77 BESDEBUG_FUNC(DEBUG_CHANNEL,
"Constrained Dims after set are: " + oss.str());
87 ArrayJoinExistingAggregation::~ArrayJoinExistingAggregation()
92 ArrayJoinExistingAggregation&
93 ArrayJoinExistingAggregation::operator=(
const ArrayJoinExistingAggregation& rhs)
97 ArrayAggregationBase::operator=(rhs);
104 ArrayJoinExistingAggregation*
120 bool ArrayJoinExistingAggregation::serialize(libdap::ConstraintEvaluator &eval, libdap::DDS &dds, libdap::Marshaller &m,
131 if (!(send_p() || is_in_selection())) {
132 BESDEBUG_FUNC(DEBUG_CHANNEL,
"Object not in output, skipping... name=" << name() << endl);
144 if (PRINT_CONSTRAINTS) {
145 BESDEBUG_FUNC(DEBUG_CHANNEL,
"Constraints on this Array are:" << endl);
152 if (PRINT_CONSTRAINTS) {
153 BESDEBUG_FUNC(DEBUG_CHANNEL,
"After transfer, constraints on the member template Array are: " << endl);
160 const Array::dimension& outerDim = *(dim_begin());
162 "Aggregating datasets array with outer dimension constraints: " <<
" start=" << outerDim.start <<
" stride=" << outerDim.stride <<
" stop=" << outerDim.stop << endl);
167 m.put_vector_start(length());
169 reserve_value_capacity();
174 NCML_ASSERT(!datasets.empty());
175 int currDatasetIndex = 0;
176 const AggMemberDataset* pCurrDataset = (datasets[currDatasetIndex]).get();
178 int outerDimIndexOfCurrDatasetHead = 0;
179 int currDatasetSize =
int(pCurrDataset->getCachedDimensionSize(_joinDim.name));
180 bool currDatasetWasRead =
false;
183 unsigned int nextOutputBufferElementIndex = 0;
188 for (
int outerDimIndex = outerDim.start; outerDimIndex <= outerDim.stop && outerDimIndex < outerDim.size;
189 outerDimIndex += outerDim.stride) {
191 int localGranuleIndex = outerDimIndex - outerDimIndexOfCurrDatasetHead;
195 while (localGranuleIndex >= currDatasetSize) {
196 localGranuleIndex -= currDatasetSize;
197 outerDimIndexOfCurrDatasetHead += currDatasetSize;
199 NCML_ASSERT(currDatasetIndex <
int(datasets.size()));
200 pCurrDataset = datasets[currDatasetIndex].get();
201 currDatasetSize = pCurrDataset->getCachedDimensionSize(_joinDim.name);
202 currDatasetWasRead =
false;
204 BESDEBUG_FUNC(DEBUG_CHANNEL,
205 "The constraint traversal passed a granule boundary " <<
"on the outer dimension and is stepping forward into " <<
"granule index=" << currDatasetIndex << endl);
210 if (!currDatasetWasRead) {
211 BESDEBUG_FUNC(DEBUG_CHANNEL,
212 " Current granule dataset was traversed but not yet " "read and copied into output. Mapping constraints " "and calling read()..." << endl);
221 Array::Dim_iter outerDimIt = granuleConstraintTemplate.dim_begin();
226 outerDimIt->size = currDatasetSize;
227 outerDimIt->c_size = currDatasetSize;
232 int granuleStopIndex = std::min(outerDim.stop - outerDimIndexOfCurrDatasetHead,
233 currDatasetSize - 1);
238 int clampedStride = std::min(outerDim.stride, currDatasetSize);
240 granuleConstraintTemplate.add_constraint(outerDimIt, localGranuleIndex, clampedStride,
242 #if USE_LOCAL_TIMEOUT_SCHEME
248 nextOutputBufferElementIndex,
251 const_cast<AggMemberDataset&
>(*pCurrDataset),
258 #if USE_LOCAL_TIMEOUT_SCHEME
266 this->set_value_slice_from_row_major_vector(*pDatasetArray, nextOutputBufferElementIndex);
269 pDatasetArray->clear_local_data();
273 currDatasetWasRead =
true;
275 BESDEBUG_FUNC(DEBUG_CHANNEL,
276 " The granule index " << currDatasetIndex <<
" was read with constraints and copied into the aggregation output." << endl);
280 catch (AggregationException& ex) {
281 THROW_NCML_PARSE_ERROR(-1, ex.what());
291 status = libdap::Array::serialize(eval, dds, m, ce_eval);
295 status = libdap::Array::serialize(eval, dds, m, ce_eval);
304 void ArrayJoinExistingAggregation::duplicate(
const ArrayJoinExistingAggregation& rhs)
306 _joinDim = rhs._joinDim;
309 void ArrayJoinExistingAggregation::cleanup() throw ()
331 sw.
start(
"ArrayJoinExistingAggregation::readConstrainedGranuleArraysAndAggregateDataHook",
"");
334 const Array::dimension& outerDim = *(dim_begin());
336 "Aggregating datasets array with outer dimension constraints: " <<
" start=" << outerDim.start <<
" stride=" << outerDim.stride <<
" stop=" << outerDim.stop << endl);
340 reserve_value_capacity();
344 NCML_ASSERT(!datasets.empty());
345 int currDatasetIndex = 0;
348 int outerDimIndexOfCurrDatasetHead = 0;
350 bool currDatasetWasRead =
false;
353 unsigned int nextOutputBufferElementIndex = 0;
358 for (
int outerDimIndex = outerDim.start; outerDimIndex <= outerDim.stop && outerDimIndex < outerDim.size;
359 outerDimIndex += outerDim.stride) {
361 int localGranuleIndex = outerDimIndex - outerDimIndexOfCurrDatasetHead;
365 while (localGranuleIndex >= currDatasetSize) {
366 localGranuleIndex -= currDatasetSize;
367 outerDimIndexOfCurrDatasetHead += currDatasetSize;
369 NCML_ASSERT(currDatasetIndex <
int(datasets.size()));
370 pCurrDataset = datasets[currDatasetIndex].get();
372 currDatasetWasRead =
false;
374 BESDEBUG_FUNC(DEBUG_CHANNEL,
375 "The constraint traversal passed a granule boundary " <<
"on the outer dimension and is stepping forward into " <<
"granule index=" << currDatasetIndex << endl);
380 if (!currDatasetWasRead) {
381 BESDEBUG_FUNC(DEBUG_CHANNEL,
382 " Current granule dataset was traversed but not yet " "read and copied into output. Mapping constraints " "and calling read()..." << endl);
391 Array::Dim_iter outerDimIt = granuleConstraintTemplate.dim_begin();
396 outerDimIt->size = currDatasetSize;
397 outerDimIt->c_size = currDatasetSize;
402 int granuleStopIndex = std::min(outerDim.stop - outerDimIndexOfCurrDatasetHead, currDatasetSize - 1);
407 int clampedStride = std::min(outerDim.stride, currDatasetSize);
409 granuleConstraintTemplate.add_constraint(outerDimIt, localGranuleIndex, clampedStride, granuleStopIndex);
413 nextOutputBufferElementIndex,
421 currDatasetWasRead =
true;
423 BESDEBUG_FUNC(DEBUG_CHANNEL,
424 " The granule index " << currDatasetIndex <<
" was read with constraints and copied into the aggregation output." << endl);
430 THROW_NCML_PARSE_ERROR(-1, ex.what());
static bool IsSet(const std::string &flagName)
see if the debug context flagName is set to true
virtual bool start(std::string name)
virtual unsigned int getCachedDimensionSize(const std::string &dimName) const =0
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 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 printConstraints(std::ostream &os, const libdap::Array &fromArray)
const AMDList & getDatasetList() const
libdap::Array & getGranuleTemplateArray()
void printConstraints(const Array &fromArray)
const ArrayGetterInterface & getArrayGetterInterface() const
ArrayJoinExistingAggregation(const libdap::Array &granuleTemplate, const AMDList &memberDatasets, std::auto_ptr< ArrayGetterInterface > &arrayGetter, const Dimension &joinDim)
virtual void readConstrainedGranuleArraysAndAggregateDataHook()
virtual ArrayJoinExistingAggregation * ptr_duplicate()
virtual void transferOutputConstraintsIntoGranuleTemplateHook()
Helper class for temporarily hijacking an existing dhi to load a DDX response for one particular file...
std::string toString() const