bes Updated for version 3.21.1
The Backend Server (BES) is the lower two tiers of the Hyrax data server
RoiFunction.cc
1
2// -*- mode: c++; c-basic-offset:4 -*-
3
4// This file is part of bes, A C++ implementation of the OPeNDAP
5// Hyrax data server
6
7// Copyright (c) 2015 OPeNDAP, Inc.
8// Authors: James Gallagher <jgallagher@opendap.org>
9//
10// This library is free software; you can redistribute it and/or
11// modify it under the terms of the GNU Lesser General Public
12// License as published by the Free Software Foundation; either
13// version 2.1 of the License, or (at your option) any later version.
14//
15// This library is distributed in the hope that it will be useful,
16// but WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18// Lesser General Public License for more details.
19//
20// You should have received a copy of the GNU Lesser General Public
21// License along with this library; if not, write to the Free Software
22// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23//
24// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
25
26#include "config.h"
27
28#include <cassert>
29#include <sstream>
30#include <memory>
31
32#include <libdap/BaseType.h>
33#include <libdap/Int32.h>
34#include <libdap/Str.h>
35#include <libdap/Array.h>
36#include <libdap/Structure.h>
37
38#include <libdap/D4RValue.h>
39#include <libdap/Error.h>
40#include <libdap/debug.h>
41#include <libdap/util.h>
42#include <libdap/ServerFunctionsList.h>
43
44#include <BESDebug.h>
45#include <roi_util.h>
46
47#include "RoiFunction.h"
48#include "functions_util.h"
49
50using namespace std;
51using namespace libdap;
52
53namespace functions {
54
76void
77function_dap2_roi(int argc, BaseType *argv[], DDS &, BaseType **btpp)
78{
79 const string wrong_args = "Wrong number of arguments to roi(). Expected one or more Arrays and bounding box";
80
81 // This is the rank of the Array of Slices, not the N-1 arrays to be sliced
82 int rank = 0;
83
84 switch (argc) {
85 case 0:
86 case 1:
87 // Must have 2 or more arguments
88 throw Error(malformed_expr, wrong_args);
89 default:
90 rank = roi_valid_bbox(argv[argc-1]); // throws if slice is not valid
91 }
92
93 unique_ptr<Structure> response(new Structure("roi_subset_unwrap"));
94
95 auto bbox = static_cast<Array*>(argv[argc-1]);
96 // Loop over arguments
97 for (int i = 0; i < argc-1; ++i) {
98 // cast is safe given the above
99 auto the_array = static_cast<Array*>(argv[i]);
100
101 // For each dimension of the array, apply the slice constraint.
102 // Assume Array[]...[][X][Y] where the slice has dims X and Y
103 // So find the last <rank> dimensions and check that their names
104 // match those of the slice. This loops 'walks backward' over
105 // both the N bbox slices and the right-most N dimensions of
106 // the arrays.
107
108 // Loop over dimensions in BBox
109 for (int i = rank-1; i >= 0; --i) {
110 int start, stop;
111 string name;
112 // start, stop, name are value-result parameters
113 roi_bbox_get_slice_data(bbox, i, start, stop, name);
114 // Loop over dimensions in argument
115 for (auto iter = the_array->dim_begin(); iter< the_array->dim_end(); iter++){
116 string cname = the_array->dimension_name(iter);
117 if (the_array->dimension_name(iter) != name) continue;
118 the_array->add_constraint(iter, start, 1 /*stride*/, stop);
119 }
120 }
121
122 // Add the array to the Structure returned by the function
123 the_array->set_send_p(true); // include it
124
125 // TODO Why do we have to force this read? The libdap::BaseType::serialize()
126 // code should take care of it, but in the debugger the read_p property is
127 // showing up as true. jhrg 2/26/15 Hack and move on...
128 //if (!the_array->read_p())
129 the_array->set_read_p(false);
130 the_array->read();
131 the_array->set_read_p(true);
132
133 response->add_var(the_array);
134 }
135
136 response->set_send_p(true);
137 response->set_read_p(true);
138
139 *btpp = response.release();
140}
141
151BaseType *function_dap4_roi(D4RValueList *, DMR &)
152{
153 throw Error(malformed_expr, "Not yet implemented for DAP4 functions.");
154}
155
156} // namesspace functions