bes  Updated for version 3.20.5
FitsRequestHandler.cc
1 // FitsRequestHandler.cc
2 
3 // This file is part of fits_handler, a data handler for the OPeNDAP data
4 // server.
5 
6 // Copyright (c) 2004,2005 University Corporation for Atmospheric Research
7 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
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 University Corporation for Atmospheric Research at
24 // 3080 Center Green Drive, Boulder, CO 80301
25 
26 // (c) COPYRIGHT University Corporation for Atmostpheric Research 2004-2005
27 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
28 //
29 // Authors:
30 // pwest Patrick West <pwest@ucar.edu>
31 // jgarcia Jose Garcia <jgarcia@ucar.edu>
32 
33 #include "config.h"
34 
35 #include <DAS.h>
36 #include <DDS.h>
37 #include <DataDDS.h>
38 #include <DMR.h>
39 #include <D4BaseTypeFactory.h>
40 #include <Ancillary.h>
41 #include <InternalErr.h>
42 #include <mime_util.h>
43 
44 #include <BESResponseHandler.h>
45 #include <BESDapError.h>
46 
47 #include <BESDapNames.h>
48 #include <BESResponseNames.h>
49 #include <BESDASResponse.h>
50 #include <BESDDSResponse.h>
51 #include <BESDataDDSResponse.h>
52 #include <BESDMRResponse.h>
53 
54 #include <BESVersionInfo.h>
55 #include <BESConstraintFuncs.h>
56 #include <BESServiceRegistry.h>
57 #include <BESUtil.h>
58 
59 #include <fitsio.h>
60 
61 #include "FitsRequestHandler.h"
62 #include "fits_read_attributes.h"
63 #include "fits_read_descriptors.h"
64 
65 using namespace libdap;
66 
67 #define FITS_NAME "fits"
68 
69 FitsRequestHandler::FitsRequestHandler(const string &name) :
70  BESRequestHandler(name)
71 {
72  add_method(DAS_RESPONSE, FitsRequestHandler::fits_build_das);
73  add_method(DDS_RESPONSE, FitsRequestHandler::fits_build_dds);
74  add_method(DATA_RESPONSE, FitsRequestHandler::fits_build_data);
75 
76  add_method(DMR_RESPONSE, FitsRequestHandler::fits_build_dmr);
77  add_method(DAP4DATA_RESPONSE, FitsRequestHandler::fits_build_dmr);
78 
79  add_method(VERS_RESPONSE, FitsRequestHandler::fits_build_vers);
80  add_method(HELP_RESPONSE, FitsRequestHandler::fits_build_help);
81 }
82 
83 FitsRequestHandler::~FitsRequestHandler()
84 {
85 }
86 
87 bool FitsRequestHandler::fits_build_das(BESDataHandlerInterface &dhi)
88 {
89  BESResponseObject *response = dhi.response_handler->get_response_object();
90  BESDASResponse *bdas = dynamic_cast<BESDASResponse *>(response);
91  if (!bdas) throw BESInternalError("cast error", __FILE__, __LINE__);
92 
93  try {
95  DAS *das = bdas->get_das();
96  string accessed = dhi.container->access();
97  string fits_error;
98  if (!fits_handler::fits_read_attributes(*das, accessed, fits_error)) {
99  throw BESDapError(fits_error, false, unknown_error, __FILE__, __LINE__);
100  }
101  Ancillary::read_ancillary_das(*das, accessed);
102  bdas->clear_container();
103  }
104  catch( InternalErr &e ) {
105  throw BESDapError(e.get_error_message(), true, e.get_error_code(), __FILE__, __LINE__);
106  }
107  catch( Error &e ) {
108  throw BESDapError(e.get_error_message(), false, e.get_error_code(), __FILE__, __LINE__);
109  }
110  catch (BESError &e) {
111  throw;
112  }
113  catch( ... ) {
114  throw BESDapError("Unknown exception caught building FITS das response", true, unknown_error, __FILE__, __LINE__);
115  }
116  return true;
117 }
118 
119 bool FitsRequestHandler::fits_build_dds(BESDataHandlerInterface &dhi)
120 {
121  BESResponseObject *response = dhi.response_handler->get_response_object();
122  BESDDSResponse *bdds = dynamic_cast<BESDDSResponse *>(response);
123  if (!bdds) throw BESInternalError("cast error", __FILE__, __LINE__);
124 
125  try {
127  DDS *dds = bdds->get_dds();
128  string accessed = dhi.container->access();
129  string fits_error;
130 
131  if (!fits_handler::fits_read_descriptors(*dds, accessed, fits_error)) {
132  throw BESDapError(fits_error, false, unknown_error, __FILE__, __LINE__);
133  }
134 
135  Ancillary::read_ancillary_dds(*dds, accessed);
136  DAS *das = new DAS;
137  BESDASResponse bdas(das);
139  if (!fits_handler::fits_read_attributes(*das, accessed, fits_error)) {
140  throw BESDapError(fits_error, false, unknown_error, __FILE__, __LINE__);
141  }
142  Ancillary::read_ancillary_das(*das, accessed);
143 
144  dds->transfer_attributes(das);
145 
146  bdds->set_constraint(dhi);
147 
148  bdds->clear_container();
149  }
150  catch( InternalErr &e ) {
151  throw BESDapError(e.get_error_message(), true, e.get_error_code(), __FILE__, __LINE__);
152  }
153  catch( Error &e ) {
154  throw BESDapError(e.get_error_message(), false, e.get_error_code(), __FILE__, __LINE__);
155  }
156  catch (BESError &e) {
157  throw;
158  }
159  catch( ... ) {
160  string err = "Unknown exception caught building FITS dds response";
161  throw BESDapError(err, true, unknown_error, __FILE__, __LINE__);
162  }
163 
164  return true;
165 }
166 
167 bool FitsRequestHandler::fits_build_data(BESDataHandlerInterface &dhi)
168 {
169  BESResponseObject *response = dhi.response_handler->get_response_object();
170  BESDataDDSResponse *bdds = dynamic_cast<BESDataDDSResponse *>(response);
171  if (!bdds) throw BESInternalError("cast error", __FILE__, __LINE__);
172 
173  try {
175  DDS *dds = bdds->get_dds();
176  string accessed = dhi.container->access();
177  string fits_error;
178  if (!fits_handler::fits_read_descriptors(*dds, accessed, fits_error)) {
179  throw BESDapError(fits_error, false, unknown_error, __FILE__, __LINE__);
180  }
181  Ancillary::read_ancillary_dds(*dds, accessed);
182 
183  DAS *das = new DAS;
184  BESDASResponse bdas(das);
186  if (!fits_handler::fits_read_attributes(*das, accessed, fits_error)) {
187  throw BESDapError(fits_error, false, unknown_error, __FILE__, __LINE__);
188  }
189  Ancillary::read_ancillary_das(*das, accessed);
190 
191  dds->transfer_attributes(das);
192 
193  bdds->set_constraint(dhi);
194 
195  bdds->clear_container();
196  }
197  catch( InternalErr &e ) {
198  throw BESDapError(e.get_error_message(), true, e.get_error_code(), __FILE__, __LINE__);
199  }
200  catch( Error &e ) {
201  throw BESDapError(e.get_error_message(), false, e.get_error_code(), __FILE__, __LINE__);
202  }
203  catch (BESError &e) {
204  throw;
205  }
206  catch( ... ) {
207  string err = "Unknown exception caught building FITS data response";
208  throw BESDapError(err, true, unknown_error, __FILE__, __LINE__);
209  }
210 
211  return true;
212 }
213 
227 {
228  string data_path = dhi.container->access();
229 
230  BaseTypeFactory factory;
231  DDS dds(&factory, name_path(data_path), "3.2");
232  dds.filename(data_path);
233 
234  try {
235  string fits_error;
236 
237  if (!fits_handler::fits_read_descriptors(dds, data_path, fits_error))
238  throw BESDapError(fits_error, false, unknown_error, __FILE__, __LINE__);
239 
240  DAS das;
241  if (!fits_handler::fits_read_attributes(das, data_path, fits_error))
242  throw BESDapError(fits_error, false, unknown_error, __FILE__, __LINE__);
243  Ancillary::read_ancillary_das(das, data_path);
244 
245  dds.transfer_attributes(&das);
246  }
247  catch( InternalErr &e ) {
248  throw BESDapError(e.get_error_message(), true, e.get_error_code(), __FILE__, __LINE__);
249  }
250  catch( Error &e ) {
251  throw BESDapError(e.get_error_message(), false, e.get_error_code(), __FILE__, __LINE__);
252  }
253  catch (BESError &e) {
254  throw;
255  }
256  catch( ... ) {
257  throw BESDapError("Unknown exception caught building FITS DMR response", true, unknown_error, __FILE__, __LINE__);
258  }
259 
260 
261  // Extract the DMR Response object - this holds the DMR used by the
262  // other parts of the framework.
263  BESResponseObject *response = dhi.response_handler->get_response_object();
264  BESDMRResponse &bdmr = dynamic_cast<BESDMRResponse &>(*response);
265 
266  // Extract the DMR Response object - this holds the DMR used by the
267  // other parts of the framework.
268  DMR *dmr = bdmr.get_dmr();
269  dmr->set_factory(new D4BaseTypeFactory);
270  dmr->build_using_dds(dds);
271 
272  bdmr.set_dap4_constraint(dhi);
273  bdmr.set_dap4_function(dhi);
274 
275  return true;
276 }
277 
278 bool FitsRequestHandler::fits_build_vers(BESDataHandlerInterface &dhi)
279 {
280  BESResponseObject *response = dhi.response_handler->get_response_object();
281  BESVersionInfo *info = dynamic_cast<BESVersionInfo *>(response);
282  if (!info) throw BESInternalError("cast error", __FILE__, __LINE__);
283 
284 #if 0
285  info->add_module(PACKAGE_NAME, PACKAGE_VERSION);
286 #endif
287  info->add_module(MODULE_NAME, MODULE_VERSION);
288 
289  return true;
290 }
291 
292 bool FitsRequestHandler::fits_build_help(BESDataHandlerInterface &dhi)
293 {
294  BESResponseObject *response = dhi.response_handler->get_response_object();
295  BESInfo *info = dynamic_cast<BESInfo *>(response);
296  if (!info) throw BESInternalError("cast error", __FILE__, __LINE__);
297 
298  map<string, string> attrs;
299  attrs["name"] = MODULE_NAME ;
300  attrs["version"] = MODULE_VERSION ;
301 #if 0
302  attrs["name"] = PACKAGE_NAME;
303  attrs["version"] = PACKAGE_VERSION;
304 #endif
305  list<string> services;
306  BESServiceRegistry::TheRegistry()->services_handled(FITS_NAME, services);
307  if (services.size() > 0) {
308  string handles = BESUtil::implode(services, ',');
309  attrs["handles"] = handles;
310  }
311  info->begin_tag("module", &attrs);
312  info->end_tag("module");
313 
314  return true;
315 }
316 
324 void FitsRequestHandler::dump(ostream &strm) const
325 {
326  strm << BESIndent::LMarg << "FitsRequestHandler::dump - (" << (void *) this << ")" << endl;
327  BESIndent::Indent();
329  BESIndent::UnIndent();
330 }
331 
exception thrown if inernal error encountered
Holds a DDS object within the BES.
virtual void clear_container()
clear the container in the DAP response object
libdap::DDS * get_dds()
virtual void set_dap4_constraint(BESDataHandlerInterface &dhi)
set the constraint depending on the context
virtual void set_container(const std::string &cn)
set the container in the DAP response object
virtual 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_dap4_function(BESDataHandlerInterface &dhi)
set the constraint depending on the context
informational response object
Definition: BESInfo.h:68
static string implode(const list< string > &values, char delim)
Definition: BESUtil.cc:635
virtual BESResponseObject * get_response_object()
return the current response object
Abstract exception class for the BES with basic string message.
Definition: BESError.h:58
virtual void dump(ostream &strm) const
dumps information about this object
virtual void set_constraint(BESDataHandlerInterface &dhi)
set the constraint depending on the context
Represents an OPeNDAP DMR DAP4 data object within the BES.
error object created from libdap error objects and can handle those errors
Definition: BESDapError.h:59
Represents an OPeNDAP DataDDS DAP2 data object within the BES.
virtual void clear_container()
clear the container in the DAP response object
Represents a specific data type request handler.
virtual void dump(ostream &strm) const
dumps information about this object
Structure storing information used by the BES to handle the request.
virtual void set_container(const string &cn)
set the container in the DAP response object
Represents an OPeNDAP DAS DAP2 data object within the BES.
virtual void set_container(const string &cn)
set the container in the DAP response object
Abstract base class representing a specific set of information in response to a request to the BES.
BESContainer * container
pointer to current container in this interface
virtual void services_handled(const string &handler, list< string > &services)
returns the list of servies provided by the handler in question
string get_symbolic_name() const
retrieve the symbolic name for this container
Definition: BESContainer.h:224
static bool fits_build_dmr(BESDataHandlerInterface &dhi)