31#include <libdap/Array.h>
32#include <libdap/D4Group.h>
33#include <libdap/Constructor.h>
34#include <libdap/D4Maps.h>
35#include <libdap/InternalErr.h>
37#include "BESStopWatch.h"
39#include "AggregationUtil.h"
40#include "GridAggregationBase.h"
45using libdap::BaseType;
49using libdap::Constructor;
50using libdap::InternalErr;
55#define DEBUG_CHANNEL "agg_util"
58GridAggregationBase::GridAggregationBase(
const libdap::Grid& proto,
const AMDList& memberDatasets,
const DDSLoader& loaderProto) :
59 Grid(proto), _loader(loaderProto.getDHI()), _pSubGridProto(cloneSubGridProto(proto)), _memberDatasets(memberDatasets)
63GridAggregationBase::GridAggregationBase(
const string& name,
const AMDList& memberDatasets,
const DDSLoader& loaderProto) :
64 Grid(name), _loader(loaderProto.getDHI()), _memberDatasets(memberDatasets)
69 Grid(proto), _loader(proto._loader.getDHI())
75GridAggregationBase::~GridAggregationBase()
81GridAggregationBase::operator=(
const GridAggregationBase& rhs)
93GridAggregationBase::transform_to_dap4(D4Group *root, Constructor *container)
95 Grid::transform_to_dap4(root,container);
98 BaseType *btp = array_var()->transform_to_dap4(root, container);
99 Array *coverage =
static_cast<Array*
>(btp);
100 if (!coverage)
throw InternalErr(__FILE__, __LINE__,
"Expected an Array while transforming a Grid (coverage)");
102 coverage->set_parent(container);
107 for (Map_iter i = map_begin(), e = map_end(); i != e; ++i) {
108 btp = (*i)->transform_to_dap4(root, container);
109 Array *map =
static_cast<Array*
>(btp);
110 if (!map)
throw InternalErr(__FILE__, __LINE__,
"Expected an Array while transforming a Grid (map)");
117 if (!root->var(map->name())) {
118 map->set_parent(container);
119 container->add_var_nocopy(map);
121 D4Map *dap4_map =
new D4Map(map->name(), map, coverage);
122 coverage->maps()->add_map(dap4_map);
125 throw InternalErr(__FILE__, __LINE__,
126 "transform_to_dap4() returned a null value where there can be no Grid.");
130 container->add_var_nocopy(coverage);
137 Grid& protoSubGrid =
const_cast<Grid&
>(constProtoSubGrid);
141 _pSubGridProto = unique_ptr<Grid>(cloneSubGridProto(protoSubGrid));
144 Array* pDataArrayTemplate = protoSubGrid.get_array();
145 VALID_PTR(pDataArrayTemplate);
146 set_array(
static_cast<Array*
>(pDataArrayTemplate->ptr_duplicate()));
150 Grid::Map_iter endIt = protoSubGrid.map_end();
151 for (Grid::Map_iter it = protoSubGrid.map_begin(); it != endIt; ++it) {
153 Array* pMap =
dynamic_cast<Array*
>(*it);
164 return _memberDatasets;
170 BESDEBUG_FUNC(DEBUG_CHANNEL,
"Function entered..." << endl);
173 BESDEBUG_FUNC(DEBUG_CHANNEL,
"read_p() set, early exit!");
182 Array* pAggArray = get_array();
183 VALID_PTR(pAggArray);
186 if (pAggArray->send_p() || pAggArray->is_in_selection()) {
235 Array* pAggArray = get_array();
236 VALID_PTR(pAggArray);
239 if (pAggArray->send_p() || pAggArray->is_in_selection()) {
241 pAggArray->serialize(eval, dds, m, ce_eval);
251 VALID_PTR(pSubGridTemplate);
254 Map_iter mapEndIt = map_end();
255 for (mapIt = map_begin(); mapIt != mapEndIt; ++mapIt) {
256 Array* pOutMap =
static_cast<Array*
>(*mapIt);
260 if (!(pOutMap->send_p() || pOutMap->is_in_selection())) {
269 pOutMap->serialize(eval, dds, m, ce_eval);
278 NCML_ASSERT_MSG(pProtoGridMap,
"Couldn't find map in prototype grid for map name=" + pOutMap->name());
279 BESDEBUG_FUNC(DEBUG_CHANNEL,
280 "About to call read() on prototype map vector name=" << pOutMap->name() <<
" and calling transfer constraints..." << endl);
283 NCML_ASSERT_MSG(pProtoGridMap->read_p(),
"Expected the prototype map to have been read but it wasn't.");
286 NCML_ASSERT_MSG(pOutMap->length() == pProtoGridMap->length(),
287 "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!");
294 pProtoGridMap->serialize(eval, dds, m, ce_eval);
296 pOutMap->reserve_value_capacity();
297 pOutMap->set_value_slice_from_row_major_vector(*pProtoGridMap, 0);
299 pOutMap->set_read_p(
true);
308 status = libdap::Grid::serialize(eval, dds, m, ce_eval);
312 status = libdap::Grid::serialize(eval, dds, m, ce_eval);
324 return _pSubGridProto.get();
331 _pSubGridProto.reset((rhs._pSubGridProto.get()) ? (
static_cast<Grid*
>(rhs._pSubGridProto->ptr_duplicate())) :
nullptr);
333 _memberDatasets = rhs._memberDatasets;
336void GridAggregationBase::cleanup()
340 _memberDatasets.clear();
341 _memberDatasets.resize(0);
357GridAggregationBase::cloneSubGridProto(
const libdap::Grid& proto)
359 return static_cast<Grid*
>(
const_cast<Grid&
>(proto).ptr_duplicate());
362void GridAggregationBase::printConstraints(
const Array& fromArray)
366 BESDEBUG(
"ncml:2",
"Constraints for Grid: " << name() <<
": " << oss.str() << endl);
372 VALID_PTR(pSubGridTemplate);
378 pSubGridTemplate->set_send_p(send_p());
379 pSubGridTemplate->set_in_selection(is_in_selection());
382 pSubGridTemplate->read();
385 pSubGridTemplate->set_read_p(
true);
391 VALID_PTR(pSubGridTemplate);
394 Map_iter mapEndIt = map_end();
395 for (mapIt = map_begin(); mapIt != mapEndIt; ++mapIt) {
396 Array* pOutMap =
static_cast<Array*
>(*mapIt);
400 if (!(pOutMap->send_p() || pOutMap->is_in_selection())) {
406 if (pOutMap->name() == aggDim.name) {
414 NCML_ASSERT_MSG(pProtoGridMap,
"Couldn't find map in prototype grid for map name=" + pOutMap->name());
415 BESDEBUG_FUNC(DEBUG_CHANNEL,
416 "About to call read() on prototype map vector name=" << pOutMap->name() <<
" and calling transfer constraints..." << endl);
419 NCML_ASSERT_MSG(pProtoGridMap->read_p(),
"Expected the prototype map to have been read but it wasn't.");
422 NCML_ASSERT_MSG(pOutMap->length() == pProtoGridMap->length(),
423 "Expected the prototype and output maps to have same length() "
424 "after transfer of constraints, but they were not so we can't "
431 pOutMap->reserve_value_capacity();
432 pOutMap->set_value_slice_from_row_major_vector(*pProtoGridMap, 0);
433 pOutMap->set_read_p(
true);
440 THROW_NCML_INTERNAL_ERROR(
"Impl me!");
static bool IsSet(const std::string &flagName)
see if the debug context flagName is set to true
virtual bool start(std::string name)
static const libdap::Array * findMapByName(const libdap::Grid &inGrid, const std::string &findName)
static void printConstraints(std::ostream &os, const libdap::Array &fromArray)
void cleanup()
restore dhi to clean state
BESDataHandlerInterface & getDHI() const
virtual const Dimension & getAggregationDimension() const =0
bool serialize(libdap::ConstraintEvaluator &eval, libdap::DDS &dds, libdap::Marshaller &m, bool ce_eval) override
virtual void readAndAggregateConstrainedMapsHook()
void copyProtoMapsIntoThisGrid(const Dimension &aggDim)
void setShapeFrom(const libdap::Grid &protoSubGrid, bool addMaps)
Grid * getSubGridTemplate()
virtual const AMDList & getDatasetList() const
virtual void transferConstraintsToSubGridHook(Grid *pSubGrid)
Helper class for temporarily hijacking an existing dhi to load a DDX response for one particular file...