bes  Updated for version 3.20.6
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 <BaseType.h>
33 #include <Int32.h>
34 #include <Str.h>
35 #include <Array.h>
36 #include <Structure.h>
37 
38 #include <D4RValue.h>
39 #include <Error.h>
40 #include <debug.h>
41 #include <util.h>
42 #include <ServerFunctionsList.h>
43 
44 #include <BESDebug.h>
45 #include <roi_util.h>
46 
47 #include "RoiFunction.h"
48 #include "functions_util.h"
49 
50 using namespace std;
51 using namespace libdap;
52 
53 namespace functions {
54 
76 void
77 function_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  auto_ptr<Structure> response(new Structure("roi_subset_unwrap"));
94 
95  Array *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  Array *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 (Array::Dim_iter 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  return;
141 }
142 
152 BaseType *function_dap4_roi(D4RValueList *, DMR &)
153 {
154  throw Error(malformed_expr, "Not yet implemented for DAP4 functions.");
155 
156  return 0;
157 }
158 
159 } // namesspace functions
libdap
Definition: BESDapFunctionResponseCache.h:35
Error