37#include <libdap/DMR.h>
38#include <libdap/D4BaseTypeFactory.h>
39#include <BESDMRResponse.h>
40#include <libdap/mime_util.h>
41#include <libdap/InternalErr.h>
42#include <libdap/Ancillary.h>
43#include <libdap/debug.h>
45#include "HDF4RequestHandler.h"
46#include <BESResponseNames.h>
47#include <BESDapNames.h>
48#include <BESDASResponse.h>
49#include <BESDDSResponse.h>
50#include <BESDataDDSResponse.h>
52#include <BESResponseHandler.h>
53#include <BESVersionInfo.h>
54#include <BESServiceRegistry.h>
56#include <TheBESKeys.h>
57#include <libdap/InternalErr.h>
58#include <BESInternalError.h>
59#include <BESDapError.h>
60#include <BESStopWatch.h>
62#include "BESDataNames.h"
64#include <libdap/Ancillary.h>
65#include "config_hdf.h"
74#include "HDFFloat32.h"
75#include "HDFSPArray_RealField.h"
85bool check_beskeys(
const string);
86bool get_beskeys(
const string,
string &);
88extern void read_das(DAS & das,
const string & filename);
89extern void read_dds(DDS & dds,
const string & filename);
91extern bool read_dds_hdfsp(DDS & dds,
const string & filename,int32 sdfd, int32 fileid,
HDFSP::File*h4file);
93extern bool read_das_hdfsp(DAS & das,
const string & filename,int32 sdfd, int32 fileid,
HDFSP::File**h4fileptr);
95extern void read_das_sds(DAS & das,
const string & filename,int32 sdfd,
bool ecs_metadata,
HDFSP::File**h4fileptr);
96extern void read_dds_sds(DDS &dds,
const string & filename,int32 sdfd,
HDFSP::File*h4file,
bool dds_set_cache);
100void read_das_use_eos2lib(DAS & das,
const string & filename,int32 sdfd,int32 fileid, int32 gridfd, int32 swathfd,
bool ecs_metadata,
HDFSP::File**h4file,HDFEOS2::File**eosfile);
101void read_dds_use_eos2lib(DDS & dds,
const string & filename,int32 sdfd,int32 fileid, int32 gridfd, int32 swathfd,
HDFSP::File*h4file,HDFEOS2::File*eosfile);
102void close_fileid(
const int sdfd,
const int fileid,
const int gridfd,
const int swathfd,
HDFSP::File*h4file,HDFEOS2::File*eosfile);
106void close_hdf4_fileid(
const int sdfd,
const int fileid,
HDFSP::File*h4file);
107bool rw_das_cache_file(
const string & filename, DAS *das_ptr,
bool rw_flag);
108bool r_dds_cache_file(
const string & cache_filename, DDS *dds_ptr,
const string & hdf4_filename);
111bool HDF4RequestHandler::_usecf =
false;
114bool HDF4RequestHandler::_pass_fileid =
false;
115bool HDF4RequestHandler::_disable_structmeta =
false;
116bool HDF4RequestHandler::_enable_special_eos =
false;
117bool HDF4RequestHandler::_disable_scaleoffset_comp =
false;
118bool HDF4RequestHandler::_disable_ecsmetadata_min =
false;
119bool HDF4RequestHandler::_disable_ecsmetadata_all =
false;
123bool HDF4RequestHandler::_enable_eosgeo_cachefile =
false;
124bool HDF4RequestHandler::_enable_data_cachefile =
false;
125bool HDF4RequestHandler::_enable_metadata_cachefile=
false;
128bool HDF4RequestHandler::_enable_hybrid_vdata =
false;
129bool HDF4RequestHandler::_enable_ceres_vdata =
false;
130bool HDF4RequestHandler::_enable_vdata_attr =
false;
131bool HDF4RequestHandler::_enable_vdata_desc_attr =
false;
132bool HDF4RequestHandler::_disable_vdata_nameclashing_check =
false;
133bool HDF4RequestHandler::_enable_vgroup_attr =
false;
136bool HDF4RequestHandler::_enable_check_modis_geo_file =
false;
137bool HDF4RequestHandler::_enable_swath_grid_attr =
false;
138bool HDF4RequestHandler::_enable_ceres_merra_short_name =
false;
139bool HDF4RequestHandler::_enable_check_scale_offset_type =
false;
140bool HDF4RequestHandler::_disable_swath_dim_map =
false;
143bool HDF4RequestHandler::_cache_latlon_path_exist =
false;
144string HDF4RequestHandler::_cache_latlon_path =
"";
145bool HDF4RequestHandler::_cache_latlon_prefix_exist =
false;
146string HDF4RequestHandler::_cache_latlon_prefix =
"";
147bool HDF4RequestHandler::_cache_latlon_size_exist =
false;
148long HDF4RequestHandler::_cache_latlon_size =0;
149bool HDF4RequestHandler::_cache_metadata_path_exist =
false;
150string HDF4RequestHandler::_cache_metadata_path =
"";
152HDF4RequestHandler::HDF4RequestHandler(
const string & name) :
154 add_handler(DAS_RESPONSE, HDF4RequestHandler::hdf4_build_das);
155 add_handler(DDS_RESPONSE, HDF4RequestHandler::hdf4_build_dds);
156 add_handler(DATA_RESPONSE, HDF4RequestHandler::hdf4_build_data);
157 add_handler(DMR_RESPONSE, HDF4RequestHandler::hdf4_build_dmr);
158 add_handler(DAP4DATA_RESPONSE, HDF4RequestHandler::hdf4_build_dmr);
159 add_handler(HELP_RESPONSE, HDF4RequestHandler::hdf4_build_help);
160 add_handler(VERS_RESPONSE, HDF4RequestHandler::hdf4_build_version);
162 _usecf = check_beskeys(
"H4.EnableCF");
166 _pass_fileid = check_beskeys(
"H4.EnablePassFileID");
167 _disable_structmeta = check_beskeys(
"H4.DisableStructMetaAttr");
168 _enable_special_eos = check_beskeys(
"H4.EnableSpecialEOS");
169 _disable_scaleoffset_comp = check_beskeys(
"H4.DisableScaleOffsetComp");
170 _disable_ecsmetadata_min = check_beskeys(
"H4.DisableECSMetaDataMin");
171 _disable_ecsmetadata_all = check_beskeys(
"H4.DisableECSMetaDataAll");
175 _enable_eosgeo_cachefile = check_beskeys(
"H4.EnableEOSGeoCacheFile");
176 _enable_data_cachefile = check_beskeys(
"H4.EnableDataCacheFile");
177 _enable_metadata_cachefile = check_beskeys(
"H4.EnableMetaDataCacheFile");
180 _enable_hybrid_vdata = check_beskeys(
"H4.EnableHybridVdata");
181 _enable_ceres_vdata = check_beskeys(
"H4.EnableCERESVdata");
182 _enable_vdata_attr = check_beskeys(
"H4.EnableVdata_to_Attr");
183 _enable_vdata_desc_attr = check_beskeys(
"H4.EnableVdataDescAttr");
184 _disable_vdata_nameclashing_check = check_beskeys(
"H4.DisableVdataNameclashingCheck");
185 _enable_vgroup_attr = check_beskeys(
"H4.EnableVgroupAttr");
188 _enable_check_modis_geo_file = check_beskeys(
"H4.EnableCheckMODISGeoFile");
189 _enable_swath_grid_attr = check_beskeys(
"H4.EnableSwathGridAttr");
190 _enable_ceres_merra_short_name = check_beskeys(
"H4.EnableCERESMERRAShortName");
191 _enable_check_scale_offset_type = check_beskeys(
"H4.EnableCheckScaleOffsetType");
193 _disable_swath_dim_map = check_beskeys(
"H4.DisableSwathDimMap");
196 _cache_latlon_path_exist =get_beskeys(
"HDF4.Cache.latlon.path",_cache_latlon_path);
197 _cache_latlon_prefix_exist =get_beskeys(
"HDF4.Cache.latlon.prefix",_cache_latlon_prefix);
198 string temp_cache_latlon_size;
199 _cache_latlon_size_exist =get_beskeys(
"HDF4.Cache.latlon.size",temp_cache_latlon_size);
200 if(_cache_latlon_size_exist ==
true) {
201 istringstream iss(temp_cache_latlon_size);
202 iss >> _cache_latlon_size;
205 _cache_metadata_path_exist =get_beskeys(
"H4.Cache.metadata.path",_cache_metadata_path);
209HDF4RequestHandler::~HDF4RequestHandler() {
217 sw.
start(
"HDF4RequestHandler::hdf4_build_das", dhi.
data[REQUEST_ID]);
235 if((base_file_name.size() >12) && (base_file_name.compare(0,4,
"AIRS") == 0)
236 && ((base_file_name.find(
".L3.")!=string::npos) || (base_file_name.find(
".L2.")!=string::npos))
237 && (base_file_name.find(
".v6.")!=string::npos)) {
238 return hdf4_build_das_cf_sds(dhi);
250 DAS *das = bdas->get_das();
256 if (
true == _usecf) {
265 sdfd = SDstart (
const_cast < char *
>(accessed.c_str()), DFACC_READ);
267 string invalid_file_msg=
"HDF4 SDstart error for the file ";
268 invalid_file_msg +=accessed;
269 invalid_file_msg +=
". It is very possible that this file is not an HDF4 file. ";
274 fileid = Hopen(
const_cast<char *
>(accessed.c_str()), DFACC_READ,0);
277 string invalid_file_msg=
"HDF4 Hopen error for the file ";
278 invalid_file_msg +=accessed;
279 invalid_file_msg +=
". It is very possible that this file is not an HDF4 file. ";
283#ifdef USE_HDFEOS2_LIB
287 HDFEOS2::File *eosfile = NULL;
291 gridfd = GDopen(
const_cast < char *
>(accessed.c_str()), DFACC_READ);
295 string invalid_file_msg=
"HDF-EOS GDopen error for the file ";
296 invalid_file_msg +=accessed;
297 invalid_file_msg +=
". It is very possible that this file is not an HDF-EOS2 file. ";
302 swathfd = SWopen(
const_cast < char *
>(accessed.c_str()), DFACC_READ);
307 string invalid_file_msg=
"HDF-EOS SWopen error for the file ";
308 invalid_file_msg +=accessed;
309 invalid_file_msg +=
". It is very possible that this file is not an HDF-EOS2 file. ";
314 bool ecs_metadata = !(_disable_ecsmetadata_all);
316if(ecs_metadata ==
true)
317cerr<<
"output ecs metadata "<<endl;
319cerr<<
"Don't output ecs metadata "<<endl;
321 read_das_use_eos2lib(*das, accessed,sdfd,fileid,gridfd,swathfd,ecs_metadata,&h4file,&eosfile);
324 close_fileid(sdfd,fileid,gridfd,swathfd,h4file,eosfile);
337 read_das_hdfsp(*das,accessed,sdfd,fileid,&h4file);
340 close_hdf4_fileid(sdfd,fileid,h4file);
345 close_hdf4_fileid(sdfd,fileid,h4file);
348 read_das(*das,accessed);
350 Ancillary::read_ancillary_das(*das, accessed);
357 catch (InternalErr & e) {
358 throw BESDapError(e.get_error_message(),
true, e.get_error_code(),
362 throw BESDapError(e.get_error_message(),
false, e.get_error_code(),
366 string s =
"unknown exception caught building HDF4 DAS";
367 throw BESDapError(s,
true, unknown_error, __FILE__, __LINE__);
379 sw.
start(
"HDF4RequestHandler::hdf4_build_das", dhi.
data[REQUEST_ID]);
397 if((base_file_name.size() >12) && (base_file_name.compare(0,4,
"AIRS") == 0)
398 && ((base_file_name.find(
".L3.")!=string::npos) || (base_file_name.find(
".L2.")!=string::npos))
399 && (base_file_name.find(
".v6.")!=string::npos)) {
400 return hdf4_build_dds_cf_sds(dhi);
407struct timeval start_time,end_time;
408gettimeofday(&start_time,NULL);
424 dds->filename(accessed);
430 if (
true == _usecf) {
438 sdfd = SDstart (
const_cast < char *
>(accessed.c_str()), DFACC_READ);
440 string invalid_file_msg=
"HDF4 SDstart error for the file ";
441 invalid_file_msg +=accessed;
442 invalid_file_msg +=
". It is very possible that this file is not an HDF4 file. ";
447 fileid = Hopen(
const_cast<char *
>(accessed.c_str()), DFACC_READ,0);
450 string invalid_file_msg=
"HDF4 Hopen error for the file ";
451 invalid_file_msg +=accessed;
452 invalid_file_msg +=
". It is very possible that this file is not an HDF4 file. ";
456#ifdef USE_HDFEOS2_LIB
461 HDFEOS2::File *eosfile = NULL;
465 gridfd = GDopen(
const_cast < char *
>(accessed.c_str()), DFACC_READ);
469 string invalid_file_msg=
"HDF-EOS GDopen error for the file ";
470 invalid_file_msg +=accessed;
471 invalid_file_msg +=
". It is very possible that this file is not an HDF-EOS2 file. ";
476 swathfd = SWopen(
const_cast < char *
>(accessed.c_str()), DFACC_READ);
481 string invalid_file_msg=
"HDF-EOS SWopen error for the file ";
482 invalid_file_msg +=accessed;
483 invalid_file_msg +=
". It is very possible that this file is not an HDF-EOS2 file. ";
488 bool ecs_metadata = !(_disable_ecsmetadata_all);
489 read_das_use_eos2lib(*das, accessed,sdfd,fileid,gridfd,swathfd,ecs_metadata,&h4file,&eosfile);
490 Ancillary::read_ancillary_das(*das, accessed);
493 read_dds_use_eos2lib(*dds, accessed,sdfd,fileid,gridfd,swathfd,h4file,eosfile);
496 close_fileid(sdfd,fileid,gridfd,swathfd,h4file,eosfile);
508 read_das_hdfsp(*das, accessed,sdfd,fileid,&h4file);
509 Ancillary::read_ancillary_das(*das, accessed);
512 read_dds_hdfsp(*dds, accessed,sdfd,fileid,h4file);
515 close_hdf4_fileid(sdfd,fileid,h4file);
520 close_hdf4_fileid(sdfd,fileid,h4file);
523 read_das(*das, accessed);
524 Ancillary::read_ancillary_das(*das, accessed);
525 read_dds(*dds, accessed);
530gettimeofday(&end_time,NULL);
531int total_time_spent = (end_time.tv_sec - start_time.tv_sec)*1000000 +end_time.tv_usec-start_time.tv_usec;
532cerr<<
"total time spent for DDS buld is "<<total_time_spent<<
"micro seconds "<<endl;
535 Ancillary::read_ancillary_dds(*dds, accessed);
537 dds->transfer_attributes(das);
546 catch (InternalErr & e) {
547 throw BESDapError(e.get_error_message(),
true, e.get_error_code(),
551 throw BESDapError(e.get_error_message(),
false, e.get_error_code(),
555 string s =
"unknown exception caught building HDF4 DDS";
556 throw BESDapError(s,
true, unknown_error, __FILE__, __LINE__);
567 sw.
start(
"HDF4RequestHandler::hdf4_build_data", dhi.
data[REQUEST_ID]);
590 if((base_file_name.size() >12) && (base_file_name.compare(0,4,
"AIRS") == 0)
591 && ((base_file_name.find(
".L3.")!=string::npos) || (base_file_name.find(
".L2.")!=string::npos))
592 && (base_file_name.find(
".v6.")!=string::npos)) {
594 BESDEBUG(
"h4",
"Coming to read the data of AIRS level 3 or level 2 products." << endl);
596 if(
true == _pass_fileid)
597 return hdf4_build_data_cf_sds_with_IDs(dhi);
599 return hdf4_build_data_cf_sds(dhi);
603 if(
true == _pass_fileid)
604 return hdf4_build_data_with_IDs(dhi);
617 DDS *dds = bdds->get_dds();
623 dds->filename(accessed);
629 if (
true == _usecf) {
635 sdfd = SDstart (
const_cast < char *
>(accessed.c_str()), DFACC_READ);
637 string invalid_file_msg=
"HDF4 SDstart error for the file ";
638 invalid_file_msg +=accessed;
639 invalid_file_msg +=
". It is very possible that this file is not an HDF4 file. ";
644 fileid = Hopen(
const_cast<char *
>(accessed.c_str()), DFACC_READ,0);
647 string invalid_file_msg=
"HDF4 Hopen error for the file ";
648 invalid_file_msg +=accessed;
649 invalid_file_msg +=
". It is very possible that this file is not an HDF4 file. ";
654#ifdef USE_HDFEOS2_LIB
658 HDFEOS2::File *eosfile = NULL;
662 gridfd = GDopen(
const_cast < char *
>(accessed.c_str()), DFACC_READ);
666 string invalid_file_msg=
"HDF-EOS GDopen error for the file ";
667 invalid_file_msg +=accessed;
668 invalid_file_msg +=
". It is very possible that this file is not an HDF-EOS2 file. ";
673 swathfd = SWopen(
const_cast < char *
>(accessed.c_str()), DFACC_READ);
678 string invalid_file_msg=
"HDF-EOS SWopen error for the file ";
679 invalid_file_msg +=accessed;
680 invalid_file_msg +=
". It is very possible that this file is not an HDF-EOS2 file. ";
690 bool ecs_metadata =
true;
692 if((
true == HDFCFUtil::check_beskeys(
"H4.DisableECSMetaDataMin"))
693 || (
true == HDFCFUtil::check_beskeys(
"H4.DisableECSMetaDataAll")))
694 ecs_metadata =
false;
696 if((
true == _disable_ecsmetadata_min)
697 || (
true == _disable_ecsmetadata_all))
698 ecs_metadata =
false;
700 read_das_use_eos2lib(*das, accessed,sdfd,fileid,gridfd,swathfd,ecs_metadata,&h4file,&eosfile);
701 Ancillary::read_ancillary_das(*das, accessed);
704 read_dds_use_eos2lib(*dds, accessed,sdfd,fileid,gridfd,swathfd,h4file,eosfile);
707 close_fileid(sdfd,fileid,gridfd,swathfd,h4file,eosfile);
718 read_das_hdfsp(*das, accessed,sdfd,fileid,&h4file);
719 Ancillary::read_ancillary_das(*das, accessed);
722 read_dds_hdfsp(*dds, accessed,sdfd,fileid,h4file);
725 close_hdf4_fileid(sdfd,fileid,h4file);
729 close_hdf4_fileid(sdfd,fileid,h4file);
732 read_das(*das, accessed);
733 Ancillary::read_ancillary_das(*das, accessed);
734 read_dds(*dds, accessed);
737 Ancillary::read_ancillary_dds(*dds, accessed);
738 dds->transfer_attributes(das);
747 catch (InternalErr & e) {
748 throw BESDapError(e.get_error_message(),
true, e.get_error_code(),
752 throw BESDapError(e.get_error_message(),
false, e.get_error_code(),
756 string s =
"unknown exception caught building DAP2 Data Response from an HDF4 data resource";
757 throw BESDapError(s,
true, unknown_error, __FILE__, __LINE__);
767 sw.
start(
"HDF4RequestHandler::hdf4_build_data_with_IDs", dhi.
data[REQUEST_ID]);
772#ifdef USE_HDFEOS2_LIB
775 HDFEOS2::File *eosfile = NULL;
791 delete bdds->get_dds();
800 hdds->filename(accessed);
809 sdfd = SDstart (
const_cast < char *
>(accessed.c_str()), DFACC_READ);
811 string invalid_file_msg=
"HDF4 SDstart error for the file ";
812 invalid_file_msg +=accessed;
813 invalid_file_msg +=
". It is very possible that this file is not an HDF4 file. ";
817 fileid = Hopen(
const_cast<char *
>(accessed.c_str()), DFACC_READ,0);
820 string invalid_file_msg=
"HDF4 Hopen error for the file ";
821 invalid_file_msg +=accessed;
822 invalid_file_msg +=
". It is very possible that this file is not an HDF4 file. ";
826#ifdef USE_HDFEOS2_LIB
832 gridfd = GDopen(
const_cast < char *
>(accessed.c_str()), DFACC_READ);
836 string invalid_file_msg=
"HDF-EOS GDopen error for the file ";
837 invalid_file_msg +=accessed;
838 invalid_file_msg +=
". It is very possible that this file is not an HDF-EOS2 file. ";
843 swathfd = SWopen(
const_cast < char *
>(accessed.c_str()), DFACC_READ);
848 string invalid_file_msg=
"HDF-EOS SWopen error for the file ";
849 invalid_file_msg +=accessed;
850 invalid_file_msg +=
". It is very possible that this file is not an HDF-EOS2 file. ";
854 hdds->setHDF4Dataset(sdfd,fileid,gridfd,swathfd);
859 bool ecs_metadata =
true;
861 if((
true == HDFCFUtil::check_beskeys(
"H4.DisableECSMetaDataMin"))
862 || (
true == HDFCFUtil::check_beskeys(
"H4.DisableECSMetaDataAll")))
864 if((
true == _disable_ecsmetadata_min)
865 || (
true == _disable_ecsmetadata_all))
866 ecs_metadata =
false;
868 read_das_use_eos2lib(*das, accessed,sdfd,fileid,gridfd,swathfd,ecs_metadata,&h4file,&eosfile);
874 Ancillary::read_ancillary_das(*das, accessed);
877 read_dds_use_eos2lib(*hdds, accessed,sdfd,fileid,gridfd,swathfd,h4file,eosfile);
883 hdds->setHDF4Dataset(sdfd,fileid);
884 read_das_hdfsp(*das, accessed,sdfd,fileid,&h4file);
885 Ancillary::read_ancillary_das(*das, accessed);
888 read_dds_hdfsp(*hdds, accessed,sdfd,fileid,h4file);
893 Ancillary::read_ancillary_dds(*hdds, accessed);
895 hdds->transfer_attributes(das);
905#ifdef USE_HDFEOS2_LIB
916 catch (InternalErr & e) {
917 throw BESDapError(e.get_error_message(),
true, e.get_error_code(),
921 throw BESDapError(e.get_error_message(),
false, e.get_error_code(),
925#ifdef USE_HDFEOS2_LIB
926 close_fileid(sdfd,fileid,gridfd,swathfd,h4file,eosfile);
928 close_hdf4_fileid(sdfd,fileid,h4file);
930 string s =
"unknown exception caught building DAP2 data response from an HDF4 data resource";
931 throw BESDapError(s,
true, unknown_error, __FILE__, __LINE__);
961 dds->filename(accessed);
968 bool das_set_cache =
false;
969 bool dds_set_cache =
false;
970 bool dds_das_get_cache =
false;
976 string check_enable_mcache_key=
"H4.EnableMetaDataCacheFile";
977 bool turn_on_enable_mcache_key=
false;
978 turn_on_enable_mcache_key = HDFCFUtil::check_beskeys(check_enable_mcache_key);
979 if(
true == turn_on_enable_mcache_key) {
981 if(
true == _enable_metadata_cachefile) {
987 string key =
"H4.Cache.metadata.path";
990 if(
true == _cache_metadata_path_exist) {
993 das_filename = _cache_metadata_path +
"/" + base_file_name +
"_das";
994 dds_filename = _cache_metadata_path +
"/" + base_file_name +
"_dds";
998 das_set_cache = rw_das_cache_file(das_filename,das,
false);
1002 dds_set_cache = r_dds_cache_file(dds_filename,dds,accessed);
1007 if((
false == das_set_cache)&&(
false == dds_set_cache))
1008 dds_das_get_cache =
true;
1014 if(
false == dds_das_get_cache) {
1017 sdfd = SDstart (
const_cast < char *
>(accessed.c_str()), DFACC_READ);
1020 string invalid_file_msg=
"HDF4 SDstart error for the file ";
1021 invalid_file_msg +=accessed;
1022 invalid_file_msg +=
". It is very possible that this file is not an HDF4 file. ";
1029 bool ecs_metadata = !(_disable_ecsmetadata_all);
1031 bool ecs_metadata =
true;
1032 if((
true == HDFCFUtil::check_beskeys(
"H4.DisableECSMetaDataMin"))
1033 || (
true == HDFCFUtil::check_beskeys(
"H4.DisableECSMetaDataAll")))
1034 ecs_metadata =
false;
1037 read_das_sds(*das, accessed,sdfd,ecs_metadata,&h4file);
1039 Ancillary::read_ancillary_das(*das, accessed);
1043 read_dds_sds(*dds, accessed,sdfd,h4file,dds_set_cache);
1046 if(
true == das_set_cache) {
1047 if(das_filename ==
"")
1048 throw InternalErr(__FILE__,__LINE__,
"DAS cache file name should be set ");
1049 rw_das_cache_file(das_filename,das,
true);
1054 Ancillary::read_ancillary_dds(*dds, accessed);
1055 dds->transfer_attributes(das);
1076 catch (InternalErr & e) {
1077 throw BESDapError(e.get_error_message(),
true, e.get_error_code(),
1078 __FILE__, __LINE__);
1081 throw BESDapError(e.get_error_message(),
false, e.get_error_code(),
1082 __FILE__, __LINE__);
1089 string s =
"unknown exception caught building HDF4 DataDDS";
1090 throw BESDapError(s,
true, unknown_error, __FILE__, __LINE__);
1112 DAS *das = bdas->get_das();
1118 bool das_set_cache =
false;
1119 bool das_get_cache =
false;
1120 string das_filename;
1122 string check_enable_mcache_key=
"H4.EnableMetaDataCacheFile";
1123 bool turn_on_enable_mcache_key=
false;
1124 turn_on_enable_mcache_key = HDFCFUtil::check_beskeys(check_enable_mcache_key);
1125 if(
true == turn_on_enable_mcache_key) {
1127 if(
true == _enable_metadata_cachefile) {
1129 string md_cache_dir;
1130 string key =
"H4.Cache.metadata.path";
1135 if(
true == _cache_metadata_path_exist) {
1138 das_filename = _cache_metadata_path +
"/" + base_file_name +
"_das";
1142 das_set_cache = rw_das_cache_file(das_filename,das,
false);
1145 if(
false == das_set_cache)
1146 das_get_cache =
true;
1151 if(
false == das_get_cache) {
1153 sdfd = SDstart (
const_cast < char *
>(accessed.c_str()), DFACC_READ);
1155 string invalid_file_msg=
"HDF4 SDstart error for the file ";
1156 invalid_file_msg +=accessed;
1157 invalid_file_msg +=
". It is very possible that this file is not an HDF4 file. ";
1163 bool ecs_metadata =
true;
1164 if((
true == HDFCFUtil::check_beskeys(
"H4.DisableECSMetaDataMin"))
1165 || (
true == HDFCFUtil::check_beskeys(
"H4.DisableECSMetaDataAll")))
1166 ecs_metadata =
false;
1170 bool ecs_metadata = !(_disable_ecsmetadata_all);
1172 read_das_sds(*das, accessed,sdfd,ecs_metadata,&h4file);
1173 Ancillary::read_ancillary_das(*das, accessed);
1176 if(
true == das_set_cache)
1177 rw_das_cache_file(das_filename,das,
true);
1194 catch (InternalErr & e) {
1195 throw BESDapError(e.get_error_message(),
true, e.get_error_code(),
1196 __FILE__, __LINE__);
1199 throw BESDapError(e.get_error_message(),
false, e.get_error_code(),
1200 __FILE__, __LINE__);
1207 string s =
"unknown exception caught building HDF4 DataDDS";
1208 throw BESDapError(s,
true, unknown_error, __FILE__, __LINE__);
1228 DDS *dds = bdds->get_dds();
1231 dds->filename(accessed);
1244 bool das_set_cache =
false;
1245 bool dds_set_cache =
false;
1246 bool dds_das_get_cache =
false;
1247 string das_filename;
1248 string dds_filename;
1251 string check_enable_mcache_key=
"H4.EnableMetaDataCacheFile";
1252 bool _disable_ecsmetadata_allturn_on_enable_mcache_key=
false;
1253 turn_on_enable_mcache_key = HDFCFUtil::check_beskeys(check_enable_mcache_key);
1254 if(
true == turn_on_enable_mcache_key) {
1256 if(
true == _enable_metadata_cachefile) {
1258 if(
true == _cache_metadata_path_exist) {
1259 BESDEBUG(
"h4",
"H4.Cache.metadata.path key is set and metadata cache key is set." << endl);
1264 das_filename = _cache_metadata_path +
"/" + base_file_name +
"_das_dd";
1265 dds_filename = _cache_metadata_path +
"/" + base_file_name +
"_dds";
1268 das_set_cache = rw_das_cache_file(das_filename,das,
false);
1271 dds_set_cache = r_dds_cache_file(dds_filename,dds,accessed);
1273 if((
false == das_set_cache)&&(
false == dds_set_cache))
1274 dds_das_get_cache =
true;
1277 if(
false == dds_das_get_cache) {
1280 sdfd = SDstart (
const_cast < char *
>(accessed.c_str()), DFACC_READ);
1282 string invalid_file_msg=
"HDF4 SDstart error for the file ";
1283 invalid_file_msg +=accessed;
1284 invalid_file_msg +=
". It is very possible that this file is not an HDF4 file. ";
1292 bool ecs_metadata =
true;
1294 if((
true == HDFCFUtil::check_beskeys(
"H4.DisableECSMetaDataMin"))
1295 || (
true == HDFCFUtil::check_beskeys(
"H4.DisableECSMetaDataAll")))
1297 if((
true == _disable_ecsmetadata_min)
1298 || (
true == _disable_ecsmetadata_all))
1299 ecs_metadata =
false;
1301 read_das_sds(*das, accessed,sdfd,ecs_metadata,&h4file);
1303 Ancillary::read_ancillary_das(*das, accessed);
1306 if(
true == das_set_cache) {
1307 rw_das_cache_file(das_filename,das,
true);
1312 read_dds_sds(*dds, accessed,sdfd,h4file,dds_set_cache);
1316 Ancillary::read_ancillary_dds(*dds, accessed);
1318 dds->transfer_attributes(das);
1334 catch (InternalErr & e) {
1335 throw BESDapError(e.get_error_message(),
true, e.get_error_code(),
1336 __FILE__, __LINE__);
1339 throw BESDapError(e.get_error_message(),
false, e.get_error_code(),
1340 __FILE__, __LINE__);
1347 string s =
"unknown exception caught building HDF4 DataDDS";
1348 throw BESDapError(s,
true, unknown_error, __FILE__, __LINE__);
1376 delete bdds->get_dds();
1382 hdds->filename(accessed);
1389 sdfd = SDstart (
const_cast < char *
>(accessed.c_str()), DFACC_READ);
1391 string invalid_file_msg=
"HDF4 SDstart error for the file ";
1392 invalid_file_msg +=accessed;
1393 invalid_file_msg +=
". It is very possible that this file is not an HDF4 file. ";
1397 hdds->setHDF4Dataset(sdfd,-1);
1402 bool ecs_metadata =
true;
1404 if((
true == HDFCFUtil::check_beskeys(
"H4.DisableECSMetaDataMin"))
1405 || (
true == HDFCFUtil::check_beskeys(
"H4.DisableECSMetaDataAll")))
1407 if((
true == _disable_ecsmetadata_min)
1408 || (
true == _disable_ecsmetadata_all))
1409 ecs_metadata =
false;
1411 read_das_sds(*das, accessed,sdfd,ecs_metadata,&h4file);
1416 Ancillary::read_ancillary_das(*das, accessed);
1419 read_dds_sds(*hdds, accessed,sdfd,h4file,
false);
1424 Ancillary::read_ancillary_dds(*hdds, accessed);
1426 hdds->transfer_attributes(das);
1437 catch (InternalErr & e) {
1438 throw BESDapError(e.get_error_message(),
true, e.get_error_code(),
1439 __FILE__, __LINE__);
1442 throw BESDapError(e.get_error_message(),
false, e.get_error_code(),
1443 __FILE__, __LINE__);
1450 string s =
"unknown exception caught building HDF4 DataDDS";
1451 throw BESDapError(s,
true, unknown_error, __FILE__, __LINE__);
1462 sw.
start(
"HDF4RequestHandler::hdf4_build_dmr", dhi.
data[REQUEST_ID]);
1469 BaseTypeFactory factory;
1470 DDS dds(&factory, name_path(data_path),
"3.2");
1471 dds.filename(data_path);
1481 if(
true == _usecf) {
1483 if(
true == _pass_fileid)
1484 return hdf4_build_dmr_with_IDs(dhi);
1491 if (
true == _usecf) {
1497 sdfd = SDstart (
const_cast < char *
>(data_path.c_str()), DFACC_READ);
1499 string invalid_file_msg=
"HDF4 SDstart error for the file ";
1500 invalid_file_msg +=data_path;
1501 invalid_file_msg +=
". It is very possible that this file is not an HDF4 file. ";
1506 fileid = Hopen(
const_cast<char *
>(data_path.c_str()), DFACC_READ,0);
1509 string invalid_file_msg=
"HDF4 Hopen error for the file ";
1510 invalid_file_msg +=data_path;
1511 invalid_file_msg +=
". It is very possible that this file is not an HDF4 file. ";
1515#ifdef USE_HDFEOS2_LIB
1520 HDFEOS2::File *eosfile = NULL;
1524 gridfd = GDopen(
const_cast < char *
>(data_path.c_str()), DFACC_READ);
1528 string invalid_file_msg=
"HDF-EOS GDopen error for the file ";
1529 invalid_file_msg +=data_path;
1530 invalid_file_msg +=
". It is very possible that this file is not an HDF-EOS2 file. ";
1535 swathfd = SWopen(
const_cast < char *
>(data_path.c_str()), DFACC_READ);
1536 if (-1 == swathfd) {
1540 string invalid_file_msg=
"HDF-EOS SWopen error for the file ";
1541 invalid_file_msg +=data_path;
1542 invalid_file_msg +=
". It is very possible that this file is not an HDF-EOS2 file. ";
1553 bool ecs_metadata =
true;
1555 if((
true == HDFCFUtil::check_beskeys(
"H4.DisableECSMetaDataMin"))
1556 || (
true == HDFCFUtil::check_beskeys(
"H4.DisableECSMetaDataAll")))
1558 if((
true == _disable_ecsmetadata_min)
1559 || (
true == _disable_ecsmetadata_all))
1560 ecs_metadata =
false;
1564 read_das_use_eos2lib(das, data_path,sdfd,fileid,gridfd,swathfd,ecs_metadata,&h4file,&eosfile);
1568 Ancillary::read_ancillary_das(das, data_path);
1571 read_dds_use_eos2lib(dds, data_path,sdfd,fileid,gridfd,swathfd,h4file,eosfile);
1575 close_fileid(sdfd,fileid,gridfd,swathfd,h4file,eosfile);
1585 read_das_hdfsp(das, data_path,sdfd,fileid,&h4file);
1586 Ancillary::read_ancillary_das(das, data_path);
1589 read_dds_hdfsp(dds, data_path,sdfd,fileid,h4file);
1592 close_hdf4_fileid(sdfd,fileid,h4file);
1597 close_hdf4_fileid(sdfd,fileid,h4file);
1601 read_das(das, data_path);
1602 Ancillary::read_ancillary_das(das, data_path);
1603 read_dds(dds, data_path);
1607 Ancillary::read_ancillary_dds(dds, data_path);
1609 dds.transfer_attributes(&das);
1613 catch (InternalErr & e) {
1614 throw BESDapError(e.get_error_message(),
true, e.get_error_code(),
1615 __FILE__, __LINE__);
1618 throw BESDapError(e.get_error_message(),
false, e.get_error_code(),
1619 __FILE__, __LINE__);
1622 string s =
"unknown exception caught building HDF4 DataDDS";
1623 throw BESDapError(s,
true, unknown_error, __FILE__, __LINE__);
1636 DMR *dmr = bes_dmr.get_dmr();
1638 D4BaseTypeFactory MyD4TypeFactory;
1639 dmr->set_factory(&MyD4TypeFactory);
1643 dmr->build_using_dds(dds);
1656 dmr->set_factory(0);
1665 sw.
start(
"HDF4RequestHandler::hdf4_build_dmr_with_IDs", dhi.
data[REQUEST_ID]);
1672 BaseTypeFactory factory;
1673 DDS dds(&factory, name_path(data_path),
"3.2");
1674 dds.filename(data_path);
1685 sdfd = SDstart (
const_cast < char *
>(data_path.c_str()), DFACC_READ);
1687 string invalid_file_msg=
"HDF4 SDstart error for the file ";
1688 invalid_file_msg +=data_path;
1689 invalid_file_msg +=
". It is very possible that this file is not an HDF4 file. ";
1694 fileid = Hopen(
const_cast<char *
>(data_path.c_str()), DFACC_READ,0);
1697 string invalid_file_msg=
"HDF4 SDstart error for the file ";
1698 invalid_file_msg +=data_path;
1699 invalid_file_msg +=
". It is very possible that this file is not an HDF4 file. ";
1704#ifdef USE_HDFEOS2_LIB
1709 HDFEOS2::File *eosfile = NULL;
1712 gridfd = GDopen(
const_cast < char *
>(data_path.c_str()), DFACC_READ);
1716 string invalid_file_msg=
"HDF4 SDstart error for the file ";
1717 invalid_file_msg +=data_path;
1718 invalid_file_msg +=
". It is very possible that this file is not an HDF4 file. ";
1724 swathfd = SWopen(
const_cast < char *
>(data_path.c_str()), DFACC_READ);
1725 if (-1 == swathfd) {
1729 string invalid_file_msg=
"HDF-EOS SWopen error for the file ";
1730 invalid_file_msg +=data_path;
1731 invalid_file_msg +=
". It is very possible that this file is not an HDF-EOS2 file. ";
1742 bool ecs_metadata =
true;
1744 if((
true == HDFCFUtil::check_beskeys(
"H4.DisableECSMetaDataMin"))
1745 || (
true == HDFCFUtil::check_beskeys(
"H4.DisableECSMetaDataAll")))
1747 if((
true == _disable_ecsmetadata_min)
1748 || (
true == _disable_ecsmetadata_all))
1749 ecs_metadata =
false;
1753 read_das_use_eos2lib(das, data_path,sdfd,fileid,gridfd,swathfd,ecs_metadata,&h4file,&eosfile);
1757 Ancillary::read_ancillary_das(das, data_path);
1760 read_dds_use_eos2lib(dds, data_path,sdfd,fileid,gridfd,swathfd,h4file,eosfile);
1764 close_fileid(sdfd,fileid,gridfd,swathfd,h4file,eosfile);
1772 read_das_hdfsp(das, data_path,sdfd,fileid,&h4file);
1773 Ancillary::read_ancillary_das(das, data_path);
1776 read_dds_hdfsp(dds, data_path,sdfd,fileid,h4file);
1779 close_hdf4_fileid(sdfd,fileid,h4file);
1786 Ancillary::read_ancillary_dds(dds, data_path);
1788 dds.transfer_attributes(&das);
1794#ifdef USE_HDFEOS2_LIB
1815 DMR *dmr = bes_dmr.get_dmr();
1816 D4BaseTypeFactory MyD4TypeFactory;
1817 dmr->set_factory(&MyD4TypeFactory);
1818 dmr->build_using_dds(dds);
1820#ifdef USE_HDFEOS2_LIB
1821 hdf4_dmr->setHDF4Dataset(sdfd,fileid,gridfd,swathfd);
1823 hdf4_dmr->setHDF4Dataset(sdfd,fileid);
1826 bes_dmr.set_dmr(hdf4_dmr);
1835 hdf4_dmr->set_factory(0);
1847 map < string, string > attrs;
1848 attrs[
"name"] = MODULE_NAME ;
1849 attrs[
"version"] = MODULE_VERSION ;
1850 list < string > services;
1852 if (services.size() > 0) {
1854 attrs[
"handles"] = handles;
1856 info->begin_tag(
"module", &attrs);
1857 info->end_tag(
"module");
1868 info->add_module(MODULE_NAME, MODULE_VERSION);
1873#ifdef USE_HDFEOS2_LIB
1892void close_hdf4_fileid(
int sdfd,
int fileid,
HDFSP::File*h4file) {
1906bool rw_das_cache_file(
const string & filename, DAS *das_ptr,
bool w_flag) {
1908 bool das_set_cache =
false;
1909 FILE *das_file = NULL;
1912 das_file = fopen(filename.c_str(),
"r");
1914 das_file = fopen(filename.c_str(),
"w");
1916 if(NULL == das_file) {
1917 if(ENOENT == errno) {
1920 if(
false == w_flag) {
1921 BESDEBUG(
"h4",
"DAS set cache key is true." << endl);
1922 das_set_cache =
true;
1926 throw BESInternalError(
"An error occurred trying to open a das cache file " + get_errno(), __FILE__, __LINE__);
1930 int fd_das = fileno(das_file);
1933 struct flock *l_das;
1935 l_das = lock(F_RDLCK);
1937 l_das = lock(F_WRLCK);
1940 if(fcntl(fd_das,F_SETLKW,l_das) == -1) {
1943 oss <<
"cache process: " << l_das->l_pid <<
" triggered a locking error: " << get_errno();
1947 if(
false == w_flag){
1949 BESDEBUG(
"h4",
"Obtaining DAS from the cache file" << endl);
1951 das_ptr->parse(das_file);
1954 if(fcntl(fd_das,F_SETLK,lock(F_UNLCK)) == -1) {
1956 throw BESInternalError(
"An error occurred trying to unlock the file" + get_errno(), __FILE__, __LINE__);
1959 throw InternalErr(__FILE__,__LINE__,
"Fail to parse the das from a das file.");
1964 BESDEBUG(
"h4",
"write DAS to a cache file" << endl);
1966 das_ptr->print(das_file);
1969 if(fcntl(fd_das,F_SETLK,lock(F_UNLCK)) == -1) {
1971 throw BESInternalError(
"An error occurred trying to unlock the file" + get_errno(), __FILE__, __LINE__);
1974 throw InternalErr(__FILE__,__LINE__,
"Fail to generate a das cache file.");
1980 if(fcntl(fd_das,F_SETLK,lock(F_UNLCK)) == -1) {
1982 throw BESInternalError(
"An error occurred trying to unlock the file" + get_errno(), __FILE__, __LINE__);
1988 return das_set_cache;
1993bool r_dds_cache_file(
const string & cache_filename, DDS *dds_ptr,
const string & hdf4_filename) {
1995 bool dds_set_cache =
false;
1996 FILE *dds_file = NULL;
1997 dds_file = fopen(cache_filename.c_str(),
"rb");
1999 if(NULL == dds_file) {
2000 if(ENOENT == errno) {
2004 dds_set_cache =
true;
2007 throw BESInternalError(
"An error occurred trying to open a dds cache file " + get_errno(), __FILE__, __LINE__);
2011 int fd_dds = fileno(dds_file);
2012 struct flock *l_dds;
2013 l_dds = lock(F_RDLCK);
2016 if(fcntl(fd_dds,F_SETLKW,l_dds) == -1) {
2019 oss <<
"cache process: " << l_dds->l_pid <<
" triggered a locking error: " << get_errno();
2024 HDFCFUtil::read_sp_sds_dds_cache(dds_file,dds_ptr,cache_filename,hdf4_filename);
2027 if(fcntl(fd_dds,F_SETLK,lock(F_UNLCK)) == -1) {
2029 throw BESInternalError(
"An error occurred trying to unlock the file" + get_errno(), __FILE__, __LINE__);
2033 throw InternalErr(__FILE__,__LINE__,
"Fail to generate a dds cache file.");
2036 if(fcntl(fd_dds,F_SETLK,lock(F_UNLCK)) == -1) {
2038 throw BESInternalError(
"An error occurred trying to unlock the file" + get_errno(), __FILE__, __LINE__);
2045 return dds_set_cache;
2049bool check_beskeys(
const string key) {
2053 const string dosettrue =
"true";
2054 const string dosetyes =
"yes";
2057 if(
true == found ) {
2059 if( dosettrue == doset || dosetyes == doset )
2066bool get_beskeys(
const string key,
string &key_value) {
2076int get_cachekey_int(
const string key) {
2083 if(
true == found ) {
2084 istringstream iss(doset);
2093cerr<<
"OK to pass pointer of a NULL pointer "<<endl;
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
Represents an OPeNDAP DAS DAP2 data object within the BES.
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
Holds a DDS object within the BES.
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
Represents an OPeNDAP DataDDS DAP2 data object within the BES.
void set_dds(libdap::DDS *ddsIn)
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.
std::map< std::string, std::string > data
the map of string data that will be required for the current request.
BESContainer * container
pointer to current container in this interface
static bool IsSet(const std::string &flagName)
see if the debug context flagName is set to true
Abstract exception class for the BES with basic string message.
informational response object
exception thrown if internal error encountered
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
virtual bool start(std::string name)
static std::string lowercase(const std::string &s)
static std::string implode(const std::list< std::string > &values, char delim)
void get_value(const std::string &s, std::string &val, bool &found)
Retrieve the value of a given key, if set.
static TheBESKeys * TheKeys()
void close_fileid(hid_t fid)