libdap  Updated for version 3.20.6
libdap4 is an implementation of OPeNDAP's DAP protocol.
GridFunction.cc
1 
2 // -*- mode: c++; c-basic-offset:4 -*-
3 
4 // This file is part of libdap, A C++ implementation of the OPeNDAP Data
5 // Access Protocol.
6 
7 // Copyright (c) 2002,2003,2013 OPeNDAP, Inc.
8 // Authors: Nathan Potter <npotter@opendap.org>
9 // James Gallagher <jgallagher@opendap.org>
10 //
11 // This library is free software; you can redistribute it and/or
12 // modify it under the terms of the GNU Lesser General Public
13 // License as published by the Free Software Foundation; either
14 // version 2.1 of the License, or (at your option) any later version.
15 //
16 // This library is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 // Lesser General Public License for more details.
20 //
21 // You should have received a copy of the GNU Lesser General Public
22 // License along with this library; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 //
25 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
26 
27 #include "config.h"
28 
29 #include <BaseType.h>
30 #include <Str.h>
31 #include <Array.h>
32 #include <Grid.h>
33 #include <Error.h>
34 #include <DDS.h>
35 #include <debug.h>
36 #include <util.h>
37 
38 #include <BESDebug.h>
39 
40 #include "GridFunction.h"
41 #include "gse_parser.h"
42 #include "grid_utils.h"
43 
44 using namespace libdap;
45 
46 namespace functions {
47 
77 void
78 function_grid(int argc, BaseType *argv[], DDS &, BaseType **btpp)
79 {
80  DBG(cerr << "Entering function_grid..." << endl);
81 
82  string info =
83  string("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n") +
84  "<function name=\"grid\" version=\"1.0\" href=\"http://docs.opendap.org/index.php/Server_Side_Processing_Functions#grid\">\n" +
85  "</function>\n";
86 
87  if (argc == 0) {
88  Str *response = new Str("info");
89  response->set_value(info);
90  *btpp = response;
91  return;
92  }
93 
94  Grid *original_grid = dynamic_cast < Grid * >(argv[0]);
95  if (!original_grid)
96  throw Error(malformed_expr,"The first argument to grid() must be a Grid variable!");
97 
98  // Duplicate the grid; ResponseBuilder::send_data() will delete the variable
99  // after serializing it.
100  BaseType *btp = original_grid->ptr_duplicate();
101  Grid *l_grid = dynamic_cast < Grid * >(btp);
102  if (!l_grid) {
103  delete btp;
104  throw InternalErr(__FILE__, __LINE__, "Expected a Grid.");
105  }
106 
107  DBG(cerr << "grid: past initialization code" << endl);
108 
109  // Read the maps. Do this before calling parse_gse_expression(). Avoid
110  // reading the array until the constraints have been applied because it
111  // might be large.
112 
113  BESDEBUG("functions", "original_grid: read_p: " << original_grid->read_p() << endl);
114  BESDEBUG("functions", "l_grid: read_p: " << l_grid->read_p() << endl);
115 
116  BESDEBUG("functions", "original_grid->array_(): read_p: " << original_grid->array_var()->read_p() << endl);
117  BESDEBUG("functions", "l_grid->array+var(): read_p: " << l_grid->array_var()->read_p() << endl);
118 
119  // This version makes sure to set the send_p flags which is needed for
120  // the hdf4 handler (and is what should be done in general).
121  Grid::Map_iter i = l_grid->map_begin();
122  while (i != l_grid->map_end())
123  (*i++)->set_send_p(true);
124 
125  l_grid->read();
126 
127  DBG(cerr << "grid: past map read" << endl);
128 
129  // argv[1..n] holds strings; each are little expressions to be parsed.
130  // When each expression is parsed, the parser makes a new instance of
131  // GSEClause. GSEClause checks to make sure the named map really exists
132  // in the Grid and that the range of values given makes sense.
133  vector < GSEClause * > clauses;
134  gse_arg *arg = new gse_arg(l_grid); // unique_ptr here
135  for (int i = 1; i < argc; ++i) {
136  parse_gse_expression(arg, argv[i]);
137  clauses.push_back(arg->get_gsec());
138  }
139  delete arg;
140  arg = 0;
141 
142  apply_grid_selection_expressions(l_grid, clauses);
143 
144  DBG(cerr << "grid: past gse application" << endl);
145 
146  l_grid->get_array()->set_send_p(true);
147 
148  l_grid->read();
149 
150  // Make a new grid here and copy just the parts of the Grid
151  // that are in the current projection - this means reading
152  // the array slicing information, extracting the correct
153  // values and building destination arrays with just those
154  // values.
155 
156  *btpp = l_grid;
157  return;
158 }
159 
165 bool GridFunction::canOperateOn(DDS &dds)
166 {
167  vector<Grid *> grids;
168  get_grids(dds, &grids);
169 
170  return !grids.empty();
171 }
172 
173 } // namesspace functions
virtual bool read_p()
Has this variable been read?
Definition: BaseType.cc:480
Map_iter map_end()
Definition: Grid.cc:537
top level DAP object to house generic methods
Definition: AISConnect.cc:30
A class for software fault reporting.
Definition: InternalErr.h:64
Holds character string data.
Definition: Str.h:62
Map_iter map_begin()
Returns an iterator referencing the first Map vector.
Definition: Grid.cc:525
virtual BaseType * ptr_duplicate()
Definition: Grid.cc:116
virtual bool read()
simple implementation of read that iterates through vars and calls read on them
Definition: Constructor.cc:476
Holds the Grid data type.
Definition: Grid.h:122
virtual bool set_value(const string &value)
Definition: Str.cc:253
Array * get_array()
Returns the Grid Array. This method returns the array using an Array*, so no cast is required...
Definition: Grid.cc:518
virtual void set_send_p(bool state)
Indicates that the data is ready to send.
Definition: Vector.cc:354
The basic data type for the DODS DAP types.
Definition: BaseType.h:117
BaseType * array_var()
Returns the Grid Array.
Definition: Grid.cc:501
A class for error processing.
Definition: Error.h:92