OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
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 <Structure.h>
29 #include <Grid.h>
30 #include <util.h>
31 
32 #include <BESDebug.h>
33 
34 #include "grid_utils.h"
35 #include "gse_parser.h"
36 #include "GSEClause.h"
37 #include "GridGeoConstraint.h"
38 
40 void gse_restart(FILE * in);
41 
42 // Glue routines declared in gse.lex
43 void gse_delete_buffer(void *buffer);
44 void *gse_string(const char *yy_str);
45 
46 namespace libdap {
47 
54 void getGrids(BaseType *bt, vector<Grid *> *grids)
55 {
56  switch (bt->type()) {
57 
58  case dods_grid_c: {
59  // Yay! It's a Grid!
60  //Grid &grid = static_cast<Grid&>(*bt);
61  grids->push_back(static_cast<Grid*>(bt));
62  break;
63  }
64  case dods_structure_c: {
65  // It's an Structure - but of what? Check each variable in the Structure.
66  Structure &s = static_cast<Structure&>(*bt);
67  for (Structure::Vars_iter i = s.var_begin(); i != s.var_begin(); i++) {
68  //BaseType *sbt = *i;
69  getGrids(*i, grids);
70  }
71  break;
72  }
73  // Grids cannot be members of Array or Sequence in DAP2. jhrg 6/10/13
74  case dods_array_c:
75  case dods_sequence_c:
76  default:
77  break;
78  }
79 }
80 
87 void getGrids(DDS &dds, vector<Grid *> *grids)
88 {
89  for (DDS::Vars_iter i = dds.var_begin(); i != dds.var_end(); i++) {
90  //BaseType *bt = *i;
91  getGrids(*i, grids);
92  }
93 }
94 
104 bool isGeoGrid(Grid *grid)
105 {
106  try {
107  GridGeoConstraint gc(grid);
108  }
109  catch (Error *e) {
110  return false;
111  }
112 
113  return true;
114 }
115 
116 void parse_gse_expression(gse_arg *arg, BaseType *expr)
117 {
118  gse_restart(0); // Restart the scanner.
119  void *cls = gse_string(extract_string_argument(expr).c_str());
120  // gse_switch_to_buffer(cls); // Get set to scan the string.
121  bool status = gse_parse(arg) == 0;
122  gse_delete_buffer(cls);
123  if (!status)
124  throw Error(malformed_expr, "Error parsing grid selection.");
125 }
126 
127 static void apply_grid_selection_expr(Grid *grid, GSEClause *clause)
128 {
129  // Basic plan: For each map, look at each clause and set start and stop
130  // to be the intersection of the ranges in those clauses.
131  Grid::Map_iter map_i = grid->map_begin();
132  while (map_i != grid->map_end() && (*map_i)->name() != clause->get_map_name())
133  ++map_i;
134 
135  if (map_i == grid->map_end())
136  throw Error(malformed_expr,"The map vector '" + clause->get_map_name()
137  + "' is not in the grid '" + grid->name() + "'.");
138 
139  // Use pointer arith & the rule that map order must match array dim order
140  Array::Dim_iter grid_dim = (grid->get_array()->dim_begin() + (map_i - grid->map_begin()));
141 
142  Array *map = dynamic_cast < Array * >((*map_i));
143  if (!map)
144  throw InternalErr(__FILE__, __LINE__, "Expected an Array");
145  int start = max(map->dimension_start(map->dim_begin()), clause->get_start());
146  int stop = min(map->dimension_stop(map->dim_begin()), clause->get_stop());
147 
148  if (start > stop) {
149  ostringstream msg;
150  msg
151  << "The expressions passed to grid() do not result in an inclusive \n"
152  << "subset of '" << clause->get_map_name()
153  << "'. The map's values range " << "from "
154  << clause->get_map_min_value() << " to "
155  << clause->get_map_max_value() << ".";
156  throw Error(malformed_expr,msg.str());
157  }
158 
159  BESDEBUG("GeoGrid", "Setting constraint on " << map->name() << "[" << start << ":" << stop << "]" << endl);
160 
161  // Stride is always one.
162  map->add_constraint(map->dim_begin(), start, 1, stop);
163  grid->get_array()->add_constraint(grid_dim, start, 1, stop);
164 }
165 
166 void apply_grid_selection_expressions(Grid * grid, vector < GSEClause * >clauses)
167 {
168  vector < GSEClause * >::iterator clause_i = clauses.begin();
169  while (clause_i != clauses.end())
170  apply_grid_selection_expr(grid, *clause_i++);
171 
172  grid->set_read_p(false);
173 }
174 
175 } //namespace libdap
void getGrids(BaseType *bt, vector< Grid * > *grids)
Recursively traverses the BaseType bt (if its a constructor type) and collects pointers to all of the...
Definition: grid_utils.cc:54
void apply_grid_selection_expressions(Grid *grid, vector< GSEClause * >clauses)
Definition: grid_utils.cc:166
void * gse_string(const char *yy_str)
Definition: lex.gse.cc:1854
int gse_parse(libdap::gse_arg *arg)
bool isGeoGrid(Grid *grid)
Evaluates a Grid to see if has suitable semantics for use with function_geogrid.
Definition: grid_utils.cc:104
void gse_delete_buffer(void *buffer)
Definition: lex.gse.cc:1866
Argument to the GSE parser.
Definition: gse_parser.h:43
gse_arg * arg
Definition: gse.tab.cc:714
void parse_gse_expression(gse_arg *arg, BaseType *expr)
Definition: grid_utils.cc:116
void gse_restart(FILE *in)
Immediately switch to a different input stream.
Definition: lex.gse.cc:1290
Geographical constraint applied to a grid.
#define BESDEBUG(x, y)
macro used to send debug information to the debug stream
Definition: BESDebug.h:64