32#include <libdap/BaseType.h>
33#include <libdap/Array.h>
34#include <libdap/Str.h>
36#include <libdap/Error.h>
37#include <libdap/DDS.h>
38#include <libdap/DMR.h>
39#include <libdap/D4RValue.h>
40#include <libdap/D4Dimensions.h>
41#include <libdap/debug.h>
42#include <libdap/util.h>
46#include "BindNameFunction.h"
53string bind_shape_info =
54 string(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
55 +
"<function name=\"make_array\" version=\"1.0\" href=\"http://docs.opendap.org/index.php/Server_Side_Processing_Functions#bind_shape\">\n"
58vector<int> parse_dims(
const string &shape);
60BaseType *bind_shape_worker(
string shape, BaseType *btp)
63 vector<int> dims = parse_dims(shape);
65 Array *array =
dynamic_cast<Array*
>(btp);
66 if (!array)
throw Error(malformed_expr,
"bind_shape() requires an Array as its second argument.");
68 unsigned long vector_size = array->length();
69 DBG(cerr <<
"bind_shape_worker() - vector_size: " << long_to_string(vector_size) << endl);
71 array->clear_all_dims();
73 unsigned long number_of_elements = 1;
74 vector<int>::iterator i = dims.begin();
75 while (i != dims.end()) {
77 number_of_elements *= dimSize;
78 if (array->is_dap4()) {
79 DBG(cerr <<
"bind_shape_worker() - Adding DAP4 dimension." << endl);
90 D4Dimension *this_dim =
new D4Dimension(
"", dimSize);
91 array->append_dim(this_dim);
94 DBG(cerr <<
"bind_shape_worker() - Adding DAP2 dimension." << endl);
95 array->append_dim(dimSize);
98 } DBG(cerr <<
"bind_shape_worker() - number_of_elements: " << long_to_string(number_of_elements) << endl);
100 if (number_of_elements != vector_size)
101 throw Error(malformed_expr,
102 "bind_shape(): The product of the new dimensions must match the size of the Array's internal storage vector.");
121void function_bind_shape_dap2(
int argc, BaseType * argv[], DDS &, BaseType **btpp)
124 Str *response =
new Str(
"info");
125 response->set_value(bind_shape_info);
131 if (argc != 2)
throw Error(malformed_expr,
"bind_shape(shape,variable) requires two arguments.");
133 string shape = extract_string_argument(argv[0]);
135 BaseType *btp = argv[1];
137 *btpp = bind_shape_worker(shape, btp);
156BaseType *function_bind_shape_dap4(D4RValueList *args, DMR &dmr)
159 if (args == 0 || args->size() == 0) {
160 Str *response =
new Str(
"info");
161 response->set_value(bind_shape_info);
167 DBG(cerr <<
"args.size() = " << args.size() << endl);
168 if (args->size() != 2)
throw Error(malformed_expr,
"bind_shape(shape,variable) requires two arguments.");
170 string shape = extract_string_argument(args->get_rvalue(0)->value(dmr));
172 BaseType *btp = args->get_rvalue(1)->value(dmr);
174 return bind_shape_worker(shape, btp);