36#include <libdap/DMR.h>
37#include <libdap/mime_util.h>
38#include <libdap/D4BaseTypeFactory.h>
39#include <libdap/InternalErr.h>
40#include <libdap/Ancillary.h>
42#include <dispatch/BESResponseHandler.h>
43#include <dispatch/BESServiceRegistry.h>
44#include <dispatch/BESResponseNames.h>
45#include <dispatch/BESVersionInfo.h>
46#include <dispatch/BESUtil.h>
47#include <dispatch/TheBESKeys.h>
49#include <dap/BESDapNames.h>
51#include <dap/BESDASResponse.h>
52#include <dap/BESDDSResponse.h>
53#include <dap/BESDataDDSResponse.h>
54#include <dap/BESDMRResponse.h>
56#include <dap/BESDapError.h>
57#include <dispatch/BESInternalFatalError.h>
62#include "GDALRequestHandler.h"
63#include "reader/gdal_utils.h"
65#define GDAL_NAME "gdal"
69GDALRequestHandler::GDALRequestHandler(
const string &name) :
72 add_method(DAS_RESPONSE, GDALRequestHandler::gdal_build_das);
73 add_method(DDS_RESPONSE, GDALRequestHandler::gdal_build_dds);
74 add_method(DATA_RESPONSE, GDALRequestHandler::gdal_build_data);
76 add_method(DMR_RESPONSE, GDALRequestHandler::gdal_build_dmr);
77 add_method(DAP4DATA_RESPONSE, GDALRequestHandler::gdal_build_dmr);
79 add_method(HELP_RESPONSE, GDALRequestHandler::gdal_build_help);
80 add_method(VERS_RESPONSE, GDALRequestHandler::gdal_build_version);
83 CPLSetErrorHandler(CPLQuietErrorHandler);
86GDALRequestHandler::~GDALRequestHandler()
93 BESDASResponse *bdas =
dynamic_cast<BESDASResponse *
> (response);
95 throw BESInternalError(
"cast error", __FILE__, __LINE__);
100 DAS *das = bdas->get_das();
103 hDS = GDALOpen(filename.c_str(), GA_ReadOnly);
106 throw Error(
string(CPLGetLastErrorMsg()));
108 BESDEBUG(
"gdal",
"Data ACCESS in gdal_build_das: "<<filename << endl);
109 gdal_read_dataset_attributes(*das, hDS);
114 Ancillary::read_ancillary_das(*das, filename);
118 catch (BESError &e) {
119 if (hDS) GDALClose(hDS);
122 catch (InternalErr & e) {
123 if (hDS) GDALClose(hDS);
124 throw BESDapError(e.get_error_message(),
true, e.get_error_code(), __FILE__, __LINE__);
127 if (hDS) GDALClose(hDS);
128 throw BESDapError(e.get_error_message(),
false, e.get_error_code(), __FILE__, __LINE__);
131 if (hDS) GDALClose(hDS);
132 throw BESInternalFatalError(
"unknown exception caught building DAS", __FILE__, __LINE__);
141 BESDDSResponse *bdds =
dynamic_cast<BESDDSResponse *
> (response);
143 throw BESInternalError(
"cast error", __FILE__, __LINE__);
145 GDALDatasetH hDS = 0;
151 dds->filename(filename);
152 dds->set_dataset_name(name_path(filename));
154 hDS = GDALOpen(filename.c_str(), GA_ReadOnly);
157 throw Error(
string(CPLGetLastErrorMsg()));
159 gdal_read_dataset_variables(dds, hDS, filename,
true);
167 catch (BESError &e) {
168 if (hDS) GDALClose(hDS);
171 catch (InternalErr & e) {
172 if (hDS) GDALClose(hDS);
173 throw BESDapError(e.get_error_message(),
true, e.get_error_code(), __FILE__, __LINE__);
176 if (hDS) GDALClose(hDS);
177 throw BESDapError(e.get_error_message(),
false, e.get_error_code(), __FILE__, __LINE__);
180 if (hDS) GDALClose(hDS);
181 throw BESInternalFatalError(
"unknown exception caught building DDS", __FILE__, __LINE__);
192 BESDataDDSResponse *bdds =
dynamic_cast<BESDataDDSResponse *
> (response);
194 throw BESInternalError(
"cast error", __FILE__, __LINE__);
196 GDALDatasetH hDS = 0;
199 DDS *dds = bdds->get_dds();
202 dds->filename(filename);
203 dds->set_dataset_name(name_path(filename));
205 hDS = GDALOpen(filename.c_str(), GA_ReadOnly);
208 throw Error(
string(CPLGetLastErrorMsg()));
211 gdal_read_dataset_variables(dds, hDS, filename,
false);
217 BESDEBUG(
"gdal",
"Data ACCESS build_data(): set the including attribute flag to false: "<<filename << endl);
218 bdds->set_ia_flag(
false);
221 catch (BESError &e) {
222 if (hDS) GDALClose(hDS);
225 catch (InternalErr & e) {
226 if (hDS) GDALClose(hDS);
227 throw BESDapError(e.get_error_message(),
true, e.get_error_code(), __FILE__, __LINE__);
230 if (hDS) GDALClose(hDS);
231 throw BESDapError(e.get_error_message(),
false, e.get_error_code(), __FILE__, __LINE__);
234 if (hDS) GDALClose(hDS);
235 throw BESInternalFatalError(
"unknown exception caught building DAS", __FILE__, __LINE__);
253 BaseTypeFactory factory;
254 DDS dds(&factory, name_path(filename),
"3.2");
255 dds.filename(filename);
257 GDALDatasetH hDS = GDALOpen(filename.c_str(), GA_ReadOnly);
260 throw Error(
string(CPLGetLastErrorMsg()));
263 gdal_read_dataset_variables(&dds, hDS, filename,
true);
268 catch (InternalErr &e) {
269 if (hDS) GDALClose(hDS);
270 throw BESDapError(e.get_error_message(),
true, e.get_error_code(), __FILE__, __LINE__);
273 if (hDS) GDALClose(hDS);
274 throw BESDapError(e.get_error_message(),
false, e.get_error_code(), __FILE__, __LINE__);
277 if (hDS) GDALClose(hDS);
278 throw BESDapError(
"Caught unknown error building GDAL DMR response",
true, unknown_error, __FILE__, __LINE__);
286 DMR *dmr = bes_dmr.get_dmr();
287 D4BaseTypeFactory d4_factory;
288 dmr->set_factory(&d4_factory);
289 dmr->build_using_dds(dds);
311 DMR *dmr = bes_dmr.get_dmr();
312 D4BaseTypeFactory d4_factory;
313 dmr->set_factory(&d4_factory);
314 dmr->set_filename(filename);
315 dmr->set_name(name_path(filename));
317 GDALDatasetH hDS = 0;
320 hDS = GDALOpen(filename.c_str(), GA_ReadOnly);
321 if (hDS == NULL)
throw Error(
string(CPLGetLastErrorMsg()));
323 gdal_read_dataset_variables(dmr, hDS, filename);
328 catch (InternalErr &e) {
329 if (hDS) GDALClose(hDS);
330 throw BESDapError(e.get_error_message(),
true, e.get_error_code(), __FILE__, __LINE__);
333 if (hDS) GDALClose(hDS);
334 throw BESDapError(e.get_error_message(),
false, e.get_error_code(), __FILE__, __LINE__);
337 if (hDS) GDALClose(hDS);
338 throw BESDapError(
"Caught unknown error building GDAL DMR response",
true, unknown_error, __FILE__, __LINE__);
355 BESInfo *info =
dynamic_cast<BESInfo *
> (response);
357 throw BESInternalError(
"cast error", __FILE__, __LINE__);
359 map < string, string, std::less<> > attrs;
360 attrs[
"name"] = MODULE_NAME ;
361 attrs[
"version"] = MODULE_VERSION ;
362 list < string > services;
364 if (services.size() > 0) {
366 attrs[
"handles"] = handles;
368 info->begin_tag(
"module", &attrs);
369 info->end_tag(
"module");
377 BESVersionInfo *info =
dynamic_cast<BESVersionInfo *
> (response);
379 throw BESInternalError(
"cast error", __FILE__, __LINE__);
381 info->add_module(MODULE_NAME, MODULE_VERSION);
389 BESDataDDSResponse *bdds =
dynamic_cast<BESDataDDSResponse *
>(response);
391 throw BESInternalError(
"cast error", __FILE__, __LINE__);
392 DDS *dds = bdds->get_dds();
397 GDALDatasetH hDS = 0;
404 if (!container_name.empty()) das->container_name(container_name);
406 hDS = GDALOpen(filename.c_str(), GA_ReadOnly);
408 throw Error(
string(CPLGetLastErrorMsg()));
410 gdal_read_dataset_attributes(*das,hDS);
411 Ancillary::read_ancillary_das(*das, filename);
413 dds->transfer_attributes(das);
418 BESDEBUG(
"gdal",
"Data ACCESS in add_attributes(): set the including attribute flag to true: "<<filename << endl);
419 bdds->set_ia_flag(
true);
423 catch (BESError &e) {
424 if (hDS) GDALClose(hDS);
428 catch (InternalErr & e) {
429 if (hDS) GDALClose(hDS);
431 throw BESDapError(e.get_error_message(),
true, e.get_error_code(), __FILE__, __LINE__);
434 if (hDS) GDALClose(hDS);
436 throw BESDapError(e.get_error_message(),
false, e.get_error_code(), __FILE__, __LINE__);
439 if (hDS) GDALClose(hDS);
441 throw BESInternalFatalError(
"unknown exception caught building DDS", __FILE__, __LINE__);
std::string get_symbolic_name() const
retrieve the symbolic name for this container
virtual std::string access()=0
returns the true name of this container
virtual void clear_container()
clear the container in the DAP response object
virtual void set_container(const std::string &cn)
set the container in the DAP response object
virtual void set_container(const std::string &cn)
set the container in the DAP response object
virtual void clear_container()
clear the container in the DAP response object
Represents an OPeNDAP DMR DAP4 data object within the BES.
error object created from libdap error objects and can handle those errors
virtual void set_dap4_function(BESDataHandlerInterface &dhi)
set the constraint depending on the context
virtual void set_dap4_constraint(BESDataHandlerInterface &dhi)
set the constraint depending on the context
virtual void set_constraint(BESDataHandlerInterface &dhi)
set the constraint depending on the context
bool get_explicit_containers() const
Should containers be explicitly represented in the DD* responses?
virtual void set_container(const std::string &cn)
set the container in the DAP response object
virtual void clear_container()
clear the container in the DAP response object
Structure storing information used by the BES to handle the request.
BESContainer * container
pointer to current container in this interface
Represents a specific data type request handler.
virtual BESResponseObject * get_response_object()
return the current response object
Abstract base class representing a specific set of information in response to a request to the BES.
virtual void services_handled(const std::string &handler, std::list< std::string > &services)
returns the list of servies provided by the handler in question
static std::string implode(const std::list< std::string > &values, char delim)
static bool gdal_build_dmr_using_dds(BESDataHandlerInterface &dhi)
Unused.