33 #include <Constructor.h>
35 #include <InternalErr.h>
37 #include "BESStopWatch.h"
39 #include "AggregationUtil.h"
40 #include "GridAggregationBase.h"
42 #include "NCMLDebug.h"
45 using libdap::BaseType;
48 using libdap::D4Group;
49 using libdap::Constructor;
50 using libdap::InternalErr;
55 static const string DEBUG_CHANNEL(
"agg_util");
56 static const bool PRINT_CONSTRAINTS(
false);
60 #undef USE_LOCAL_TIMEOUT_SCHEME
63 GridAggregationBase::GridAggregationBase(
const libdap::Grid& proto,
const AMDList& memberDatasets,
64 const DDSLoader& loaderProto) :
65 Grid(proto), _loader(loaderProto.getDHI()), _pSubGridProto(cloneSubGridProto(proto)), _memberDatasets(
70 GridAggregationBase::GridAggregationBase(
const string& name,
const AMDList& memberDatasets,
72 Grid(name), _loader(loaderProto.getDHI()), _pSubGridProto(0), _memberDatasets(memberDatasets)
77 Grid(proto), _loader(proto._loader.getDHI()), _pSubGridProto(0)
84 GridAggregationBase::~GridAggregationBase()
90 GridAggregationBase::operator=(
const GridAggregationBase& rhs)
102 GridAggregationBase::transform_to_dap4(D4Group *root, Constructor *container)
104 Grid::transform_to_dap4(root,container);
106 #if 0 // I removed this method because I think the parent class implementation should work correctly.
107 BaseType *btp = array_var()->transform_to_dap4(root, container);
108 Array *coverage =
static_cast<Array*
>(btp);
109 if (!coverage)
throw InternalErr(__FILE__, __LINE__,
"Expected an Array while transforming a Grid (coverage)");
111 coverage->set_parent(container);
116 for (Map_iter i = map_begin(), e = map_end(); i != e; ++i) {
117 btp = (*i)->transform_to_dap4(root, container);
118 Array *map =
static_cast<Array*
>(btp);
119 if (!map)
throw InternalErr(__FILE__, __LINE__,
"Expected an Array while transforming a Grid (map)");
126 if (!root->var(map->name())) {
127 map->set_parent(container);
128 container->add_var_nocopy(map);
130 D4Map *dap4_map =
new D4Map(map->name(), map, coverage);
131 coverage->maps()->add_map(dap4_map);
134 throw InternalErr(__FILE__, __LINE__,
135 "transform_to_dap4() returned a null value where there can be no Grid.");
139 container->add_var_nocopy(coverage);
146 Grid& protoSubGrid =
const_cast<Grid&
>(constProtoSubGrid);
150 _pSubGridProto = auto_ptr<Grid>(cloneSubGridProto(protoSubGrid));
153 Array* pDataArrayTemplate = protoSubGrid.get_array();
154 VALID_PTR(pDataArrayTemplate);
155 set_array(
static_cast<Array*
>(pDataArrayTemplate->ptr_duplicate()));
159 Grid::Map_iter endIt = protoSubGrid.map_end();
160 for (Grid::Map_iter it = protoSubGrid.map_begin(); it != endIt; ++it) {
162 Array* pMap =
dynamic_cast<Array*
>(*it);
173 return _memberDatasets;
179 BESDEBUG_FUNC(DEBUG_CHANNEL,
"Function entered..." << endl);
182 BESDEBUG_FUNC(DEBUG_CHANNEL,
"read_p() set, early exit!");
186 if (PRINT_CONSTRAINTS) {
187 printConstraints(*(get_array()));
195 Array* pAggArray = get_array();
196 VALID_PTR(pAggArray);
199 if (pAggArray->send_p() || pAggArray->is_in_selection()) {
235 if (BESISDEBUG(TIMING_LOG)) sw.
start(
"GridAggregationBase::serialize",
"");
240 if (PRINT_CONSTRAINTS) {
241 printConstraints(*(get_array()));
243 #if USE_LOCAL_TIMEOUT_SCHEME
254 Array* pAggArray = get_array();
255 VALID_PTR(pAggArray);
258 if (pAggArray->send_p() || pAggArray->is_in_selection()) {
260 pAggArray->serialize(eval, dds, m, ce_eval);
270 VALID_PTR(pSubGridTemplate);
273 Map_iter mapEndIt = map_end();
274 for (mapIt = map_begin(); mapIt != mapEndIt; ++mapIt) {
275 Array* pOutMap =
static_cast<Array*
>(*mapIt);
279 if (!(pOutMap->send_p() || pOutMap->is_in_selection())) {
286 if (PRINT_CONSTRAINTS) {
287 BESDEBUG_FUNC(DEBUG_CHANNEL,
288 "About to call read() on the map for the new outer dimension name=" <<
getAggregationDimension().name <<
" It's constraints are:" << endl);
289 printConstraints(*pOutMap);
294 pOutMap->serialize(eval, dds, m, ce_eval);
303 NCML_ASSERT_MSG(pProtoGridMap,
"Couldn't find map in prototype grid for map name=" + pOutMap->name());
304 BESDEBUG_FUNC(DEBUG_CHANNEL,
305 "About to call read() on prototype map vector name=" << pOutMap->name() <<
" and calling transfer constraints..." << endl);
308 NCML_ASSERT_MSG(pProtoGridMap->read_p(),
"Expected the prototype map to have been read but it wasn't.");
311 NCML_ASSERT_MSG(pOutMap->length() == pProtoGridMap->length(),
312 "Expected the prototype and output maps to have same length() after transfer of constraints, but they were not so we can't copy the data!");
319 pProtoGridMap->serialize(eval, dds, m, ce_eval);
321 pOutMap->reserve_value_capacity();
322 pOutMap->set_value_slice_from_row_major_vector(*pProtoGridMap, 0);
324 pOutMap->set_read_p(
true);
328 #if USE_LOCAL_TIMEOUT_SCHEME
337 status = libdap::Grid::serialize(eval, dds, m, ce_eval);
341 status = libdap::Grid::serialize(eval, dds, m, ce_eval);
353 return _pSubGridProto.get();
360 std::auto_ptr<Grid> pGridTemplateClone(
361 ((rhs._pSubGridProto.get()) ? (
static_cast<Grid*
>(rhs._pSubGridProto->ptr_duplicate())) : (0)));
362 _pSubGridProto = pGridTemplateClone;
364 _memberDatasets = rhs._memberDatasets;
367 void GridAggregationBase::cleanup()
371 _memberDatasets.clear();
372 _memberDatasets.resize(0);
388 GridAggregationBase::cloneSubGridProto(
const libdap::Grid& proto)
390 return static_cast<Grid*
>(
const_cast<Grid&
>(proto).ptr_duplicate());
393 void GridAggregationBase::printConstraints(
const Array& fromArray)
397 BESDEBUG(
"ncml:2",
"Constraints for Grid: " << name() <<
": " << oss.str() << endl);
403 VALID_PTR(pSubGridTemplate);
409 pSubGridTemplate->set_send_p(send_p());
410 pSubGridTemplate->set_in_selection(is_in_selection());
413 pSubGridTemplate->read();
416 pSubGridTemplate->set_read_p(
true);
422 VALID_PTR(pSubGridTemplate);
425 Map_iter mapEndIt = map_end();
426 for (mapIt = map_begin(); mapIt != mapEndIt; ++mapIt) {
427 Array* pOutMap =
static_cast<Array*
>(*mapIt);
431 if (!(pOutMap->send_p() || pOutMap->is_in_selection())) {
437 if (pOutMap->name() == aggDim.name) {
438 if (PRINT_CONSTRAINTS) {
439 BESDEBUG_FUNC(DEBUG_CHANNEL,
440 "About to call read() on the map for the new outer dimension name=" << aggDim.name <<
" It's constraints are:" << endl);
441 printConstraints(*pOutMap);
451 NCML_ASSERT_MSG(pProtoGridMap,
"Couldn't find map in prototype grid for map name=" + pOutMap->name());
452 BESDEBUG_FUNC(DEBUG_CHANNEL,
453 "About to call read() on prototype map vector name=" << pOutMap->name() <<
" and calling transfer constraints..." << endl);
456 NCML_ASSERT_MSG(pProtoGridMap->read_p(),
"Expected the prototype map to have been read but it wasn't.");
459 NCML_ASSERT_MSG(pOutMap->length() == pProtoGridMap->length(),
460 "Expected the prototype and output maps to have same length() "
461 "after transfer of constraints, but they were not so we can't "
468 pOutMap->reserve_value_capacity();
469 pOutMap->set_value_slice_from_row_major_vector(*pProtoGridMap, 0);
470 pOutMap->set_read_p(
true);
477 THROW_NCML_INTERNAL_ERROR(
"Impl me!");