bes  Updated for version 3.20.6
ScaleGrid.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) 2016 OPeNDAP, Inc.
8 // Author: 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 //
24 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
25 
26 #include "ScaleGrid.h"
27 
28 #include "config.h"
29 
30 #include <memory>
31 #include <string>
32 
33 #include <BaseType.h>
34 #include <Array.h>
35 #include <Grid.h>
36 #include <Str.h>
37 #include <Error.h>
38 #include <util.h>
39 
40 #include <BESDebug.h>
41 
42 #include "functions_util.h"
43 #include "GridGeoConstraint.h"
44 
45 #define DEBUG_KEY "geo"
46 
47 using namespace std;
48 using namespace libdap;
49 
50 namespace functions {
51 
65 void function_scale_grid(int argc, BaseType *argv[], DDS &, BaseType **btpp)
66 {
67  // scale_grid(argv[0], y, x)
68  string info =
69  string("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n") +
70  "<function name=\"scale_grid\" version=\"1.0\" href=\"http://docs.opendap.org/index.php/Server_Side_Processing_Functions#scale_grid\">\n" +
71  "</function>\n";
72 
73  if (argc == 0) {
74  unique_ptr<Str> response(new Str("info"));
75  response->set_value(info);
76  *btpp = response.release();
77  return;
78  }
79 
80  if (argc < 3 || argc > 5) {
81  throw Error("The scale_grid() function requires three arguments: a Grid and the new lon, lat extents (got " + long_to_string(argc) + " args).\n\
82  See http://docs.opendap.org/index.php/Server_Side_Processing_Functions#scale_grid");
83  }
84 
85  Grid *src = dynamic_cast < Grid * >(argv[0]);
86  if (!src)
87  throw Error(malformed_expr,"The first argument to scale_grid() must be a Grid variable!");
88 
89  GridGeoConstraint gc(src);
90  if(!gc.is_longitude_rightmost()){
91  throw Error(malformed_expr,"The last argument to scale_grid() must be a longitude variable!");
92  }
93 
94  BESDEBUG(DEBUG_KEY,"function_scale_grid() - Evaluating grid '"<< src->name() << "'" << endl);
95  unsigned long y = extract_uint_value(argv[1]);
96  unsigned long x = extract_uint_value(argv[2]);
97 
98  string crs = "WGS84";
99  string interp = "nearest";
100  if (argc > 3) {
101  crs = extract_string_argument(argv[3]);
102  }
103  if (argc > 4) {
104  interp = extract_string_argument(argv[4]);
105  }
106 
107  BESDEBUG(DEBUG_KEY,"function_scale_grid() - CRS '"<< crs << "'" << endl);
108  BESDEBUG(DEBUG_KEY,"function_scale_grid() - Interpolation Method '"<< interp << "'" << endl);
109 
110  SizeBox size(x, y);
111  *btpp = scale_dap_grid(src, size, crs, interp);
112 }
113 
114 //Grid *scale_dap_array(const Array *data, const Array *lon, const Array *lat, const SizeBox &size,
115 // const string &crs, const string &interp)
130 void function_scale_array(int argc, BaseType *argv[], DDS &, BaseType **btpp)
131 {
132  // scale_array(argv[0], Y, X, y, x)
133  string info =
134  string("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n") +
135  "<function name=\"scale_array\" version=\"1.0\" href=\"http://docs.opendap.org/index.php/Server_Side_Processing_Functions#scale_array\">\n" +
136  "</function>\n";
137 
138  if (argc == 0) {
139  unique_ptr<Str> response(new Str("info"));
140  response->set_value(info);
141  *btpp = response.release();
142  return;
143  }
144 
145  if (!(argc > 4 && argc < 8)) {
146  throw Error("The scale_array() function requires five arguments: three Arrays and the new lat and lon extents.\n\
147  See http://docs.opendap.org/index.php/Server_Side_Processing_Functions#scale_array");
148  }
149 
150  Array *data = dynamic_cast <Array *>(argv[0]);
151  if (!data)
152  throw Error(malformed_expr,"The first argument to scale_array() must be an Array variable!");
153  Array *x = dynamic_cast <Array *>(argv[2]);
154  if (!x)
155  throw Error(malformed_expr,"The second argument to scale_array() must be an Array variable!");
156  Array *y = dynamic_cast <Array *>(argv[1]);
157  if (!y)
158  throw Error(malformed_expr,"The third argument to scale_array() must be an Array variable!");
159 
160  unsigned long new_x = extract_uint_value(argv[4]);
161  unsigned long new_y = extract_uint_value(argv[3]);
162 
163  string crs = "WGS84"; // FIXME WGS84 assumes a certain order for lat and lon
164  string interp = "nearest";
165  if (argc > 5) {
166  crs = extract_string_argument(argv[5]);
167  }
168 
169  if (argc > 6) {
170  interp = extract_string_argument(argv[6]);
171  }
172 
173  SizeBox size(new_x, new_y);
174  *btpp = scale_dap_array(data, x, y, size, crs, interp);
175 }
176 
177 //Grid *scale_dap_3D_array(const Array *data, const Array *time const Array *lon, const Array *lat, const SizeBox &size,
178 // const string &crs, const string &interp)
193 void function_scale_array_3D(int argc, BaseType *argv[], DDS &, BaseType **btpp)
194 {
195  // scale_3D_array(arg[0], T, Y, X, y, x)
196  string info =
197  string("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
198  + "<function name=\"scale_array\" version=\"1.0\" href=\"http://docs.opendap.org/index.php/Server_Side_Processing_Functions#scale_3D_array\">\n"
199  + "</function>\n";
200 
201  BESDEBUG(DEBUG_KEY,"function_scale_array_3D() - argc = " << argc << endl);
202 
203  if (argc == 0) {
204  unique_ptr<Str> response(new Str("info"));
205  response->set_value(info);
206  *btpp = response.release();
207  return;
208  }
209 
210  if (!(argc > 5 && argc < 9)) {
211  throw Error(
212  "The scale_array_3D() function requires six arguments: four Arrays and the new lat and lon extents.\n\
213  See http://docs.opendap.org/index.php/Server_Side_Processing_Functions#scale_array");
214  }
215 
216  BESDEBUG(DEBUG_KEY,"function_scale_array_3D() - arg[0] name: " << argv[0]->name() << endl);
217 
218  Array *data = dynamic_cast<Array *>(argv[0]);
219  if (!data) throw Error(malformed_expr, "The first argument to scale_array_3D() must be an Array variable!");
220  Array *t = dynamic_cast<Array *>(argv[1]);
221  if (!t) throw Error(malformed_expr, "The second argument to scale_array_3D() must be an Array variable!");
222  Array *y = dynamic_cast<Array *>(argv[2]);
223  if (!y) throw Error(malformed_expr, "The third argument to scale_array_3D() must be an Array variable!");
224  Array *x = dynamic_cast<Array *>(argv[3]);
225  if (!x) throw Error(malformed_expr, "The fourth argument to scale_array_3D() must be an Array variable!");
226 
227  unsigned long new_x = extract_uint_value(argv[5]); // lon should be last argument
228  unsigned long new_y = extract_uint_value(argv[4]);
229 
230  string crs = "WGS84"; // FIXME WGS84 assumes a certain order for lat and lon
231  string interp = "nearest";
232  if (argc > 6) {
233  crs = extract_string_argument(argv[6]);
234  }
235 
236  if (argc > 7) {
237  interp = extract_string_argument(argv[7]);
238  }
239 
240  SizeBox size(new_x, new_y);
241  *btpp = scale_dap_array_3D(data, t, x, y, size, crs, interp);
242 }
243 
244 
245 }
libdap
Definition: BESDapFunctionResponseCache.h:35
Error