libdap Updated for version 3.21.1
libdap4 is an implementation of OPeNDAP's DAP protocol.
GridFunction.cc
Go to the documentation of this file.
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 <Array.h>
30#include <BaseType.h>
31#include <DDS.h>
32#include <Error.h>
33#include <Grid.h>
34#include <Str.h>
35#include <debug.h>
36#include <util.h>
37
38#include <BESDebug.h>
39
40#include "GridFunction.h"
41#include "grid_utils.h"
42#include "gse_parser.h"
43
44using namespace libdap;
45
46namespace functions {
47
77void function_grid(int argc, BaseType *argv[], DDS &, BaseType **btpp) {
78 DBG(cerr << "Entering function_grid..." << endl);
79
80 string info = string("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n") +
81 "<function name=\"grid\" version=\"1.0\" "
82 "href=\"http://docs.opendap.org/index.php/Server_Side_Processing_Functions#grid\">\n" +
83 "</function>\n";
84
85 if (argc == 0) {
86 Str *response = new Str("info");
87 response->set_value(info);
88 *btpp = response;
89 return;
90 }
91
92 Grid *original_grid = dynamic_cast<Grid *>(argv[0]);
93 if (!original_grid)
94 throw Error(malformed_expr, "The first argument to grid() must be a Grid variable!");
95
96 // Duplicate the grid; ResponseBuilder::send_data() will delete the variable
97 // after serializing it.
98 BaseType *btp = original_grid->ptr_duplicate();
99 Grid *l_grid = dynamic_cast<Grid *>(btp);
100 if (!l_grid) {
101 delete btp;
102 throw InternalErr(__FILE__, __LINE__, "Expected a Grid.");
103 }
104
105 DBG(cerr << "grid: past initialization code" << endl);
106
107 // Read the maps. Do this before calling parse_gse_expression(). Avoid
108 // reading the array until the constraints have been applied because it
109 // might be large.
110
111 BESDEBUG("functions", "original_grid: read_p: " << original_grid->read_p() << endl);
112 BESDEBUG("functions", "l_grid: read_p: " << l_grid->read_p() << endl);
113
114 BESDEBUG("functions", "original_grid->array_(): read_p: " << original_grid->array_var()->read_p() << endl);
115 BESDEBUG("functions", "l_grid->array+var(): read_p: " << l_grid->array_var()->read_p() << endl);
116
117 // This version makes sure to set the send_p flags which is needed for
118 // the hdf4 handler (and is what should be done in general).
119 Grid::Map_iter i = l_grid->map_begin();
120 while (i != l_grid->map_end())
121 (*i++)->set_send_p(true);
122
123 l_grid->read();
124
125 DBG(cerr << "grid: past map read" << endl);
126
127 // argv[1..n] holds strings; each are little expressions to be parsed.
128 // When each expression is parsed, the parser makes a new instance of
129 // GSEClause. GSEClause checks to make sure the named map really exists
130 // in the Grid and that the range of values given makes sense.
131 vector<GSEClause *> clauses;
132 gse_arg *arg = new gse_arg(l_grid); // unique_ptr here
133 for (int i = 1; i < argc; ++i) {
134 parse_gse_expression(arg, argv[i]);
135 clauses.push_back(arg->get_gsec());
136 }
137 delete arg;
138 arg = 0;
139
140 apply_grid_selection_expressions(l_grid, clauses);
141
142 DBG(cerr << "grid: past gse application" << endl);
143
144 l_grid->get_array()->set_send_p(true);
145
146 l_grid->read();
147
148 // Make a new grid here and copy just the parts of the Grid
149 // that are in the current projection - this means reading
150 // the array slicing information, extracting the correct
151 // values and building destination arrays with just those
152 // values.
153
154 *btpp = l_grid;
155 return;
156}
157
164 vector<Grid *> grids;
165 get_grids(dds, &grids);
166
167 return !grids.empty();
168}
169
170} // namespace functions
#define malformed_expr
(400)
Definition Error.h:66
bool canOperateOn(libdap::DDS &dds)
The basic data type for the DODS DAP types.
Definition BaseType.h:118
virtual bool read_p()
Has this variable been read?
Definition BaseType.cc:410
bool read() override
Read the elements of Constructor marked for transmission.
A class for error processing.
Definition Error.h:92
Holds the Grid data type.
Definition Grid.h:121
virtual BaseType * ptr_duplicate()
Definition Grid.cc:96
BaseType * array_var()
Returns the Grid Array.
Definition Grid.cc:440
Map_iter map_begin()
Returns an iterator referencing the first Map vector.
Definition Grid.cc:448
std::vector< BaseType * >::iterator Map_iter
Definition Grid.h:136
Array * get_array()
Returns the Grid Array. This method returns the array using an Array*, so no cast is required.
Definition Grid.cc:445
Map_iter map_end()
Definition Grid.cc:458
A class for software fault reporting.
Definition InternalErr.h:61
Holds character string data.
Definition Str.h:61
virtual bool set_value(const string &value)
Definition Str.cc:219
void set_send_p(bool state) override
Indicates that the data is ready to send.
Definition Vector.cc:352
#define DBG(x)
Definition debug.h:58
void function_grid(int argc, BaseType *argv[], DDS &, BaseType **btpp)
void get_grids(BaseType *bt, vector< Grid * > *grids)
Definition grid_utils.cc:60
void parse_gse_expression(gse_arg *arg, BaseType *expr)
void apply_grid_selection_expressions(Grid *grid, vector< GSEClause * > clauses)
top level DAP object to house generic methods
Definition AISConnect.cc:30
GSEClause * get_gsec()
Definition gse_parser.h:56