forked from OPENDAP/netcdf_handler
-
Notifications
You must be signed in to change notification settings - Fork 1
/
NCRequestHandler.cc
456 lines (387 loc) · 14.8 KB
/
NCRequestHandler.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
// -*- mode: c++; c-basic-offset:4 -*-
// This file is part of nc_handler, a data handler for the OPeNDAP data
// server.
// Copyright (c) 2002,2003 OPeNDAP, Inc.
// Author: James Gallagher <[email protected]>
//
// This is free software; you can redistribute it and/or modify it under the
// terms of the GNU Lesser General Public License as published by the Free
// Software Foundation; either version 2.1 of the License, or (at your
// option) any later version.
//
// This software is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
// License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
// NCRequestHandler.cc
#include "config_nc.h"
#include <string>
#include <sstream>
#include <exception>
#include <DMR.h>
#include <mime_util.h>
#include <D4BaseTypeFactory.h>
#include <BESResponseHandler.h>
#include <BESResponseNames.h>
#include <BESDapNames.h>
#include <BESDASResponse.h>
#include <BESDDSResponse.h>
#include <BESDataDDSResponse.h>
#include <BESVersionInfo.h>
#include <BESDapError.h>
#include <BESInternalFatalError.h>
#include <BESDataNames.h>
#include <TheBESKeys.h>
#include <BESServiceRegistry.h>
#include <BESUtil.h>
#include <BESDebug.h>
#include <BESContextManager.h>
#include <BESDMRResponse.h>
#include <InternalErr.h>
#include <Ancillary.h>
#include "NCRequestHandler.h"
#if 0
#include "gridfields_functions.h"
#endif
#define NC_NAME "nc"
using namespace libdap;
bool NCRequestHandler::_show_shared_dims = true;
bool NCRequestHandler::_show_shared_dims_set = false;
bool NCRequestHandler::_ignore_unknown_types = false;
bool NCRequestHandler::_ignore_unknown_types_set = false;
bool NCRequestHandler::_promote_byte_to_short = false;
bool NCRequestHandler::_promote_byte_to_short_set = false;
extern void nc_read_dataset_attributes(DAS & das, const string & filename);
extern void nc_read_dataset_variables(DDS & dds, const string & filename);
/** Is the version number string greater than or equal to the value.
* @note Works only for versions with zero or one dot. If the conversion of
* the string to a float fails for any reason, this returns false.
* @param version The string value (e.g., 3.2)
* @param value A floating point value.
*/
static bool version_ge(const string &version, float value)
{
try {
float v;
istringstream iss(version);
iss >> v;
//cerr << "version: " << v << ", value: " << value << endl;
return (v >= value);
}
catch (...) {
return false;
}
return false; // quiet warnings...
}
NCRequestHandler::NCRequestHandler(const string &name) :
BESRequestHandler(name)
{
BESDEBUG("nc", "In NCRequestHandler::NCRequestHandler" << endl);
add_handler(DAS_RESPONSE, NCRequestHandler::nc_build_das);
add_handler(DDS_RESPONSE, NCRequestHandler::nc_build_dds);
add_handler(DATA_RESPONSE, NCRequestHandler::nc_build_data);
add_handler(DMR_RESPONSE, NCRequestHandler::nc_build_dmr);
add_handler(DAP4DATA_RESPONSE, NCRequestHandler::nc_build_dmr);
add_handler(HELP_RESPONSE, NCRequestHandler::nc_build_help);
add_handler(VERS_RESPONSE, NCRequestHandler::nc_build_version);
// Look for the SHowSharedDims property, if it has not been set
if (NCRequestHandler::_show_shared_dims_set == false) {
bool key_found = false;
string doset;
TheBESKeys::TheKeys()->get_value("NC.ShowSharedDimensions", doset, key_found);
if (key_found) {
// It was set in the conf file
NCRequestHandler::_show_shared_dims_set = true;
doset = BESUtil::lowercase(doset);
if (doset == "true" || doset == "yes") {
NCRequestHandler::_show_shared_dims = true;
}
else
NCRequestHandler::_show_shared_dims = false;
}
}
if (NCRequestHandler::_ignore_unknown_types_set == false) {
bool key_found = false;
string doset;
TheBESKeys::TheKeys()->get_value("NC.IgnoreUnknownTypes", doset, key_found);
if (key_found) {
doset = BESUtil::lowercase(doset);
if (doset == "true" || doset == "yes")
NCRequestHandler::_ignore_unknown_types = true;
else
NCRequestHandler::_ignore_unknown_types = false;
NCRequestHandler::_ignore_unknown_types_set = true;
}
}
if (NCRequestHandler::_promote_byte_to_short_set == false) {
bool key_found = false;
string doset;
TheBESKeys::TheKeys()->get_value("NC.PromoteByteToShort", doset, key_found);
if (key_found) {
doset = BESUtil::lowercase(doset);
if (doset == "true" || doset == "yes")
NCRequestHandler::_promote_byte_to_short = true;
else
NCRequestHandler::_promote_byte_to_short = false;
NCRequestHandler::_promote_byte_to_short_set = true;
}
}
BESDEBUG("nc", "Exiting NCRequestHandler::NCRequestHandler" << endl);
}
NCRequestHandler::~NCRequestHandler()
{
}
bool NCRequestHandler::nc_build_das(BESDataHandlerInterface & dhi)
{
BESDEBUG("nc", "In NCRequestHandler::nc_build_das" << endl);
BESResponseObject *response = dhi.response_handler->get_response_object();
BESDASResponse *bdas = dynamic_cast<BESDASResponse *> (response);
if (!bdas)
throw BESInternalError("cast error", __FILE__, __LINE__);
try {
bdas->set_container(dhi.container->get_symbolic_name());
DAS *das = bdas->get_das();
string accessed = dhi.container->access();
nc_read_dataset_attributes(*das, accessed);
Ancillary::read_ancillary_das(*das, accessed);
bdas->clear_container();
}
catch (BESError &e) {
throw;
}
catch (InternalErr & e) {
BESDapError ex(e.get_error_message(), true, e.get_error_code(), __FILE__, __LINE__);
throw ex;
}
catch (Error & e) {
BESDapError ex(e.get_error_message(), false, e.get_error_code(), __FILE__, __LINE__);
throw ex;
}
catch (std::exception &e) {
string s = string("C++ Exception: ") + e.what();
BESInternalFatalError ex(s, __FILE__, __LINE__);
throw ex;
}
catch (...) {
string s = "unknown exception caught building DAS";
BESInternalFatalError ex(s, __FILE__, __LINE__);
throw ex;
}
BESDEBUG("nc", "Exiting NCRequestHandler::nc_build_das" << endl);
return true;
}
bool NCRequestHandler::nc_build_dds(BESDataHandlerInterface & dhi)
{
BESResponseObject *response = dhi.response_handler->get_response_object();
BESDDSResponse *bdds = dynamic_cast<BESDDSResponse *> (response);
if (!bdds)
throw BESInternalError("cast error", __FILE__, __LINE__);
try {
// If there's no value for this set in the conf file, look at the context
// and set the default behavior based on the protocol version clients say
// they will accept.
if (NCRequestHandler::_show_shared_dims_set == false) {
bool context_found = false;
string context_value = BESContextManager::TheManager()->get_context("xdap_accept", context_found);
if (context_found) {
BESDEBUG("nc", "xdap_accept: " << context_value << endl);
if (version_ge(context_value, 3.2))
NCRequestHandler::_show_shared_dims = false;
else
NCRequestHandler::_show_shared_dims = true;
}
}
bdds->set_container(dhi.container->get_symbolic_name());
DDS *dds = bdds->get_dds();
string accessed = dhi.container->access();
dds->filename(accessed);
nc_read_dataset_variables(*dds, accessed);
BESDEBUG("nc", "NCRequestHandler::nc_build_dds, accessed: " << accessed << endl);
#if 0
Ancillary::read_ancillary_dds(*dds, accessed);
#endif
DAS *das = new DAS;
BESDASResponse bdas(das);
bdas.set_container(dhi.container->get_symbolic_name());
nc_read_dataset_attributes(*das, accessed);
Ancillary::read_ancillary_das(*das, accessed);
BESDEBUG("nc", "Prior to dds->transfer_attributes" << endl);
dds->transfer_attributes(das);
#if 0
ConstraintEvaluator & ce = bdds->get_ce();
ce.add_function("ugrid_restrict", function_ugrid_restrict);
#endif
bdds->set_constraint(dhi);
bdds->clear_container();
}
catch (BESError &e) {
throw e;
}
catch (InternalErr & e) {
BESDapError ex(e.get_error_message(), true, e.get_error_code(), __FILE__, __LINE__);
throw ex;
}
catch (Error & e) {
BESDapError ex(e.get_error_message(), false, e.get_error_code(), __FILE__, __LINE__);
throw ex;
}
catch (std::exception &e) {
string s = string("C++ Exception: ") + e.what();
BESInternalFatalError ex(s, __FILE__, __LINE__);
throw ex;
}
catch (...) {
string s = "unknown exception caught building DDS";
BESInternalFatalError ex(s, __FILE__, __LINE__);
throw ex;
}
return true;
}
bool NCRequestHandler::nc_build_data(BESDataHandlerInterface & dhi)
{
BESResponseObject *response = dhi.response_handler->get_response_object();
BESDataDDSResponse *bdds = dynamic_cast<BESDataDDSResponse *> (response);
if (!bdds)
throw BESInternalError("cast error", __FILE__, __LINE__);
try {
if (NCRequestHandler::_show_shared_dims_set == false) {
bool context_found = false;
string context_value = BESContextManager::TheManager()->get_context("xdap_accept", context_found);
if (context_found) {
BESDEBUG("nc", "xdap_accept: " << context_value << endl);
if (version_ge(context_value, 3.2))
NCRequestHandler::_show_shared_dims = false;
else
NCRequestHandler::_show_shared_dims = true;
}
}
bdds->set_container(dhi.container->get_symbolic_name());
DataDDS *dds = bdds->get_dds();
string accessed = dhi.container->access();
dds->filename(accessed);
nc_read_dataset_variables(*dds, accessed);
#if 0
Ancillary::read_ancillary_dds(*dds, accessed);
#endif
DAS *das = new DAS;
BESDASResponse bdas(das);
bdas.set_container(dhi.container->get_symbolic_name());
nc_read_dataset_attributes(*das, accessed);
Ancillary::read_ancillary_das(*das, accessed);
dds->transfer_attributes(das);
#if 0
ConstraintEvaluator & ce = bdds->get_ce();
ce.add_function("ugrid_restrict", function_ugrid_restrict);
#endif
bdds->set_constraint(dhi);
bdds->clear_container();
}
catch (BESError &e) {
throw;
}
catch (InternalErr & e) {
BESDapError ex(e.get_error_message(), true, e.get_error_code(), __FILE__, __LINE__);
throw ex;
}
catch (Error & e) {
BESDapError ex(e.get_error_message(), false, e.get_error_code(), __FILE__, __LINE__);
throw ex;
}
catch (std::exception &e) {
string s = string("C++ Exception: ") + e.what();
BESInternalFatalError ex(s, __FILE__, __LINE__);
throw ex;
}
catch (...) {
string s = "unknown exception caught building DAS";
BESInternalFatalError ex(s, __FILE__, __LINE__);
throw ex;
}
return true;
}
bool NCRequestHandler::nc_build_dmr(BESDataHandlerInterface &dhi)
{
// Because this code does not yet know how to build a DMR directly, use
// the DMR ctor that builds a DMR using a 'full DDS' (a DDS with attributes).
// First step, build the 'full DDS'
string data_path = dhi.container->access();
BaseTypeFactory factory;
DDS dds(&factory, name_path(data_path), "3.2");
dds.filename(data_path);
try {
nc_read_dataset_variables(dds, data_path);
DAS das;
nc_read_dataset_attributes(das, data_path);
Ancillary::read_ancillary_das(das, data_path);
dds.transfer_attributes(&das);
}
catch (InternalErr &e) {
throw BESDapError(e.get_error_message(), true, e.get_error_code(), __FILE__, __LINE__);
}
catch (Error &e) {
throw BESDapError(e.get_error_message(), false, e.get_error_code(), __FILE__, __LINE__);
}
catch (...) {
throw BESDapError("Caught unknown error build NC DMR response", true, unknown_error, __FILE__, __LINE__);
}
// Extract the DMR Response object - this holds the DMR used by the
// other parts of the framework.
BESResponseObject *response = dhi.response_handler->get_response_object();
BESDMRResponse &bdmr = dynamic_cast<BESDMRResponse &>(*response);
// Get the DMR made by the BES in the BES/dap/BESDMRResponseHandler, make sure there's a
// factory we can use and then dump the DAP2 variables and attributes in using the
// BaseType::transform_to_dap4() method that transforms individual variables
DMR *dmr = bdmr.get_dmr();
dmr->set_factory(new D4BaseTypeFactory);
dmr->build_using_dds(dds);
// Instead of fiddling with the internal storage of the DHI object,
// (by setting dhi.data[DAP4_CONSTRAINT], etc., directly) use these
// methods to set the constraints. But, why? Ans: from Patrick is that
// in the 'container' mode of BES each container can have a different
// CE.
bdmr.set_dap4_constraint(dhi);
bdmr.set_dap4_function(dhi);
// What about async and store_result? See BESDapTransmit::send_dap4_data()
return true;
}
bool NCRequestHandler::nc_build_help(BESDataHandlerInterface & dhi)
{
BESResponseObject *response = dhi.response_handler->get_response_object();
BESInfo *info = dynamic_cast<BESInfo *> (response);
if (!info)
throw BESInternalError("cast error", __FILE__, __LINE__);
map < string, string > attrs;
attrs["name"] = MODULE_NAME ;
attrs["version"] = MODULE_VERSION ;
#if 0
attrs["name"] = PACKAGE_NAME;
attrs["version"] = PACKAGE_VERSION;
#endif
list < string > services;
BESServiceRegistry::TheRegistry()->services_handled(NC_NAME, services);
if (services.size() > 0) {
string handles = BESUtil::implode(services, ',');
attrs["handles"] = handles;
}
info->begin_tag("module", &attrs);
info->end_tag("module");
return true;
}
bool NCRequestHandler::nc_build_version(BESDataHandlerInterface & dhi)
{
BESResponseObject *response = dhi.response_handler->get_response_object();
BESVersionInfo *info = dynamic_cast<BESVersionInfo *> (response);
if (!info)
throw BESInternalError("cast error", __FILE__, __LINE__);
#if 0
info->add_module(PACKAGE_NAME, PACKAGE_VERSION);
#endif
info->add_module(MODULE_NAME, MODULE_VERSION);
return true;
}