libdap Updated for version 3.21.1
libdap4 is an implementation of OPeNDAP's DAP protocol.
grid_utils.cc
Go to the documentation of this file.
1// -*- mode: c++; c-basic-offset:4 -*-
2
3// This file is part of libdap, A C++ implementation of the OPeNDAP Data
4// Access Protocol.
5
6// Copyright (c) 2002,2003 OPeNDAP, Inc.
7// Author: Nathan Potter <npotter@opendap.org>
8//
9// This library is free software; you can redistribute it and/or
10// modify it under the terms of the GNU Lesser General Public
11// License as published by the Free Software Foundation; either
12// version 2.1 of the License, or (at your option) any later version.
13//
14// This library is distributed in the hope that it will be useful,
15// but WITHOUT ANY WARRANTY; without even the implied warranty of
16// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17// Lesser General Public License for more details.
18//
19// You should have received a copy of the GNU Lesser General Public
20// License along with this library; if not, write to the Free Software
21// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22//
23// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
24
25#include "config.h"
26
27#include <BaseType.h>
28#include <Grid.h>
29#include <Structure.h>
30#include <util.h>
31
32#include <debug.h>
33
34#include "GSEClause.h"
35#include "grid_utils.h"
36#include "gse_parser.h"
37
38#if 0
39#include "GridGeoConstraint.h"
40#endif
41
42using namespace libdap;
43
45void gse_restart(FILE *in);
46
47// Glue routines declared in gse.lex
48void gse_delete_buffer(void *buffer);
49void *gse_string(const char *yy_str);
50
51namespace functions {
52
60void get_grids(BaseType *bt, vector<Grid *> *grids) {
61 switch (bt->type()) {
62
63 case dods_grid_c: {
64 // Yay! It's a Grid!
65 grids->push_back(static_cast<Grid *>(bt));
66 break;
67 }
68 case dods_structure_c: {
69 // It's an Structure - but of what? Check each variable in the Structure.
70 Structure &s = static_cast<Structure &>(*bt);
71 for (Structure::Vars_iter i = s.var_begin(); i != s.var_begin(); i++) {
72 get_grids(*i, grids);
73 }
74 break;
75 }
76 // Grids cannot be members of Array or Sequence in DAP2. jhrg 6/10/13
77 case dods_array_c:
78 case dods_sequence_c:
79 default:
80 break;
81 }
82}
83
90void get_grids(DDS &dds, vector<Grid *> *grids) {
91 for (DDS::Vars_iter i = dds.var_begin(); i != dds.var_end(); i++) {
92 get_grids(*i, grids);
93 }
94}
95
96#if 0
97
98// Hacked this out of the code to get prototype value-based subsetting
99// into the code. We might add it back later (or maybe there is a better
100// place? jhrg 4/18/19
101
111bool is_geo_grid(Grid *grid)
112{
113 try {
114 GridGeoConstraint gc(grid);
115 }
116 catch (Error *e) {
117 return false;
118 }
119
120 return true;
121}
122#endif
123
125 gse_restart(0); // Restart the scanner.
126 void *cls = gse_string(extract_string_argument(expr).c_str());
127 // gse_switch_to_buffer(cls); // Get set to scan the string.
128 bool status = gse_parse(arg) == 0;
130 if (!status)
131 throw Error(malformed_expr, "Error parsing grid selection.");
132}
133
134static void apply_grid_selection_expr(Grid *grid, GSEClause *clause) {
135 // Basic plan: For each map, look at each clause and set start and stop
136 // to be the intersection of the ranges in those clauses.
137 Grid::Map_iter map_i = grid->map_begin();
138 while (map_i != grid->map_end() && (*map_i)->name() != clause->get_map_name())
139 ++map_i;
140
141 if (map_i == grid->map_end())
142 throw Error(malformed_expr,
143 "The map vector '" + clause->get_map_name() + "' is not in the grid '" + grid->name() + "'.");
144
145 // Use pointer arith & the rule that map order must match array dim order
146 Array::Dim_iter grid_dim = (grid->get_array()->dim_begin() + (map_i - grid->map_begin()));
147
148 Array *map = dynamic_cast<Array *>((*map_i));
149 if (!map)
150 throw InternalErr(__FILE__, __LINE__, "Expected an Array");
151 int start = max(map->dimension_start(map->dim_begin()), clause->get_start());
152 int stop = min(map->dimension_stop(map->dim_begin()), clause->get_stop());
153
154 if (start > stop) {
155 ostringstream msg;
156 msg << "The expressions passed to grid() do not result in an inclusive \n"
157 << "subset of '" << clause->get_map_name() << "'. The map's values range " << "from "
158 << clause->get_map_min_value() << " to " << clause->get_map_max_value() << ".";
159 throw Error(malformed_expr, msg.str());
160 }
161
162 DBG(cerr << "Setting constraint on " << map->name() << "[" << start << ":" << stop << "]" << endl);
163
164 // Stride is always one.
165 map->add_constraint(map->dim_begin(), start, 1, stop);
166 grid->get_array()->add_constraint(grid_dim, start, 1, stop);
167}
168
169void apply_grid_selection_expressions(Grid *grid, vector<GSEClause *> clauses) {
170 vector<GSEClause *>::iterator clause_i = clauses.begin();
171 while (clause_i != clauses.end())
172 apply_grid_selection_expr(grid, *clause_i++);
173
174 grid->set_read_p(false);
175}
176
177} // namespace functions
#define malformed_expr
(400)
Definition Error.h:66
A multidimensional array of identical data types.
Definition Array.h:121
virtual void add_constraint(Dim_iter i, int start, int stride, int stop)
Adds a constraint to an Array dimension.
Definition Array.cc:598
std::vector< dimension >::iterator Dim_iter
Definition Array.h:225
Dim_iter dim_begin()
Definition Array.cc:689
The basic data type for the DODS DAP types.
Definition BaseType.h:118
virtual string name() const
Returns the name of the class instance.
Definition BaseType.cc:296
virtual Type type() const
Returns the type of the class instance.
Definition BaseType.cc:329
void set_read_p(bool state) override
Set the 'read_p' property for the Constructor and its members.
std::vector< BaseType * >::iterator Vars_iter
Definition Constructor.h:60
Vars_iter var_begin()
Vars_iter var_begin()
Definition DDS.h:336
Vars_iter var_end()
Return an iterator.
Definition DDS.h:341
std::vector< BaseType * >::iterator Vars_iter
Definition DDS.h:211
A class for error processing.
Definition Error.h:92
Holds the Grid data type.
Definition Grid.h:121
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 a structure (aggregate) type.
Definition Structure.h:82
STL iterator class.
#define DBG(x)
Definition debug.h:58
void * gse_string(const char *yy_str)
int gse_parse(functions::gse_arg *arg)
void gse_delete_buffer(void *buffer)
void gse_restart(FILE *in)
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
@ dods_sequence_c
Definition Type.h:108
@ dods_grid_c
Definition Type.h:111
@ dods_structure_c
Definition Type.h:106
@ dods_array_c
Definition Type.h:107
string extract_string_argument(BaseType *arg)
Definition util.cc:118