49 #include "config_hdf.h"
67 #ifdef HAVE_SYS_PARAM_H
68 #include <sys/param.h>
72 #include <sys/types.h>
86 #include <InternalErr.h>
92 #include "HDF4RequestHandler.h"
99 #include "HDFSequence.h"
100 #include "HDFTypeFactory.h"
102 #include "dodsutil.h"
103 #include "hdf-dods.h"
104 #include "hdf-maps.h"
107 #define SIGNED_BYTE_TO_INT32 1
111 #include "HDFInt16.h"
112 #include "HDFUInt16.h"
113 #include "HDFInt32.h"
114 #include "HDFUInt32.h"
115 #include "HDFFloat32.h"
116 #include "HDFFloat64.h"
124 #include "HDFSPArray_RealField.h"
125 #include "HDFSPArrayGeoField.h"
126 #include "HDFSPArrayMissField.h"
127 #include "HDFSPArrayAddCVField.h"
128 #include "HDFSPArray_VDField.h"
129 #include "HDFCFStrField.h"
131 #include "HDFCFUtil.h"
134 #ifdef USE_HDFEOS2_LIB
136 #include "HDFEOS2Array_RealField.h"
137 #include "HDFEOS2ArrayGridGeoField.h"
138 #include "HDFEOS2ArraySwathGeoField.h"
139 #include "HDFEOS2ArrayMissField.h"
140 #include "HDFEOS2ArraySwathDimMapField.h"
142 #include "HDFEOS2ArraySwathGeoDimMapExtraField.h"
145 #include "HDFEOS2HandleType.h"
153 #define ATTR_STRING_QUOTE_FIX
155 template <
class T >
string num2string(T n)
163 void hdfeos_switch_to_buffer(
void *new_buffer);
164 void hdfeos_delete_buffer(
void * buffer);
165 void *hdfeos_string(
const char *yy_str);
167 struct yy_buffer_state;
168 yy_buffer_state *hdfeos_scan_string(
const char *str);
169 extern int hdfeosparse(libdap::parser_arg *arg);
172 void AddHDFAttr(DAS & das,
const string & varname,
173 const vector < hdf_attr > &hav);
174 void AddHDFAttr(DAS & das,
const string & varname,
175 const vector < string > &anv);
177 static void build_descriptions(DDS & dds, DAS & das,
178 const string & filename);
179 static void SDS_descriptions(sds_map & map, DAS & das,
180 const string & filename);
181 static void Vdata_descriptions(vd_map & map, DAS & das,
182 const string & filename);
183 static void Vgroup_descriptions(DDS & dds, DAS & das,
184 const string & filename, sds_map & sdmap,
185 vd_map & vdmap, gr_map & grmap);
186 static void GR_descriptions(gr_map & map, DAS & das,
187 const string & filename);
188 static void FileAnnot_descriptions(DAS & das,
const string & filename);
189 static vector < hdf_attr > Pals2Attrs(
const vector < hdf_palette > palv);
190 static vector < hdf_attr > Dims2Attrs(
const hdf_dim dim);
192 void read_das(DAS & das,
const string & filename);
193 void read_dds(DDS & dds,
const string & filename);
197 bool read_dds_hdfsp(DDS & dds,
const string & filename,int32 sdfd, int32 fileid,
HDFSP::File*h4file);
198 bool read_das_hdfsp(DAS & das,
const string & filename,int32 sdfd, int32 fileid,
HDFSP::File**h4filepptr);
201 bool read_dds_hdfhybrid(DDS & dds,
const string & filename,int32 sdfd, int32 fileid,
HDFSP::File*h4file);
202 bool read_das_hdfhybrid(DAS & das,
const string & filename,int32 sdfd, int32 fileid,
HDFSP::File**h4filepptr);
206 bool read_dds_special_1d_grid(DDS &dds,
HDFSP::File *spf,
const string & filename,int32 sdfd,
bool can_cache);
207 bool read_das_special_eos2(DAS &das,
const string & filename,int32 sdid, int32 fileid,
bool ecs_metadata,
HDFSP::File**h4filepptr);
208 bool read_das_special_eos2_core(DAS &das,
HDFSP::File *spf,
const string & filename,
bool ecs_metadata);
210 void read_das_sds(DAS & das,
const string & filename,int32 sdfd,
bool ecs_metadata,
HDFSP::File**h4fileptr);
211 void read_dds_sds(DDS &dds,
const string & filename,int32 sdfd,
HDFSP::File*h4file,
bool dds_set_cache);
213 void change_das_mod08_scale_offset(DAS & das,
HDFSP::File *spf);
216 void read_dds_spfields(DDS &dds,
const string& filename,
const int sdfd,
HDFSP::SDField *spsds, SPType sptype);
219 void read_dds_spvdfields(DDS &dds,
const string& filename,
const int fileid,int32 vdref, int32 numrec,
HDFSP::VDField *spvd);
222 int check_special_eosfile(
const string&filename,
string&grid_name,int32 sdfd,int32 fileid);
226 #ifdef USE_HDFEOS2_LIB
229 void parse_ecs_metadata(DAS &das,
const string & metaname,
const string &metadata);
247 int read_dds_hdfeos2(DDS & dds,
const string & filename,int32 sdfd,int32 fileid, int32 gridfd, int32 swathfd,
HDFSP::File*h4file,HDFEOS2::File*eosfile);
250 int read_das_hdfeos2(DAS & das,
const string & filename,int32 sdfd,int32 fileid, int32 gridfd, int32 swathfd,
bool ecs_metadata,
HDFSP::File**h4filepptr,HDFEOS2::File**eosfilepptr);
254 void read_dds_hdfeos2_grid_swath(DDS &dds,
const string&filename, HDFEOS2::Dataset *dataset,
int grid_or_swath,
bool ownll, SOType sotype,
255 int32 sdfd, int32 fileid, int32 gridfd,int32 swathfd)
258 BESDEBUG(
"h4",
"Coming to read_dds_hdfeos2_grid_swath "<<endl);
260 if(grid_or_swath < 0 || grid_or_swath > 1)
261 throw InternalErr(__FILE__, __LINE__,
"The current type should be either grid or swath");
266 vector<struct dimmap_entry> dimmaps;
269 string modis_geofilename=
"";
270 bool geofile_has_dimmap =
false;
274 if(grid_or_swath == 1)
275 HDFCFUtil::obtain_dimmap_info(filename,dataset,dimmaps,modis_geofilename,geofile_has_dimmap);
280 const vector<HDFEOS2::Field*>& fields = (dataset)->getDataFields();
281 vector<HDFEOS2::Field*> all_fields = fields;
282 vector<HDFEOS2::Field*>::const_iterator it_f;
284 if(1 == grid_or_swath) {
285 HDFEOS2::SwathDataset *sw =
static_cast<HDFEOS2::SwathDataset *
>(dataset);
286 const vector<HDFEOS2::Field*>geofields = sw->getGeoFields();
287 for (it_f = geofields.begin(); it_f != geofields.end(); it_f++)
288 all_fields.push_back(*it_f);
293 for(it_f = all_fields.begin(); it_f != all_fields.end(); it_f++)
295 BESDEBUG(
"h4",
"New field Name " <<(*it_f)->getNewName()<<endl);
300 int fieldtype = (*it_f)->getFieldType();
304 bool changedtype =
false;
305 for (vector<string>::const_iterator i = ctype_field_namelist.begin(); i != ctype_field_namelist.end(); ++i){
306 if ((*i) == (*it_f)->getNewName()){
312 switch((*it_f)->getType())
315 #define HANDLE_CASE2(tid, type) \
317 if(true == changedtype && fieldtype==0) \
318 bt = new (HDFFloat32) ((*it_f)->getNewName(), (dataset)->getName()); \
320 bt = new (type)((*it_f)->getNewName(), (dataset)->getName()); \
323 #define HANDLE_CASE(tid, type)\
325 bt = new (type)((*it_f)->getNewName(), (dataset)->getName()); \
329 HANDLE_CASE(DFNT_CHAR8,
HDFStr);
330 #ifndef SIGNED_BYTE_TO_INT32
331 HANDLE_CASE2(DFNT_INT8,
HDFByte);
335 HANDLE_CASE2(DFNT_UINT8,
HDFByte);
340 HANDLE_CASE2(DFNT_UCHAR8,
HDFByte);
342 throw InternalErr(__FILE__,__LINE__,
"unsupported data type.");
349 const vector<HDFEOS2::Dimension*>& dims= (*it_f)->getCorrectedDimensions();
350 vector<HDFEOS2::Dimension*>::const_iterator it_d;
353 if(DFNT_CHAR == (*it_f)->getType()) {
355 if((*it_f)->getRank() >1) {
357 HDFEOS2CFStrField * ar = NULL;
361 ar =
new HDFEOS2CFStrField(
362 (*it_f)->getRank() -1,
363 (grid_or_swath ==0)?gridfd:swathfd,
365 (dataset)->getName(),
368 (*it_f)->getNewName(),
373 throw InternalErr(__FILE__,__LINE__,
"Unable to allocate the HDFCFStr instance.");
375 for(it_d = dims.begin(); it_d != dims.begin()+dims.size()-1; it_d++){
376 ar->append_dim((*it_d)->getSize(), (*it_d)->getName());
387 HDFEOS2CFStr * sca_str = NULL;
390 sca_str =
new HDFEOS2CFStr(
391 (grid_or_swath ==0)?gridfd:swathfd,
393 (dataset)->getName(),
395 (*it_f)->getNewName(),
400 throw InternalErr(__FILE__,__LINE__,
"Unable to allocate the HDFCFStr instance.");
402 dds.add_var(sca_str);
410 else if(fieldtype == 0 || fieldtype == 3 || fieldtype == 5) {
413 if(grid_or_swath==0){
414 HDFEOS2Array_RealField *ar = NULL;
415 ar =
new HDFEOS2Array_RealField(
417 filename,
false,sdfd,gridfd,
418 (dataset)->getName(),
"", (*it_f)->getName(),
420 (*it_f)->getNewName(), bt);
421 for(it_d = dims.begin(); it_d != dims.end(); it_d++)
422 ar->append_dim((*it_d)->getSize(), (*it_d)->getName());
428 else if(grid_or_swath==1){
430 string tempfieldname = (*it_f)->getName();
433 if((*it_f)->UseDimMap()) {
436 if (!modis_geofilename.empty()) {
439 if (
true == HDFCFUtil::is_modis_dimmap_nonll_field(tempfieldname)) {
441 if(
false == geofile_has_dimmap) {
448 HDFEOS2Array_RealField *ar = NULL;
449 ar =
new HDFEOS2Array_RealField(
456 "MODIS_Swath_Type_GEO",
459 (*it_f)->getNewName(),
462 for(it_d = dims.begin(); it_d != dims.end(); it_d++)
463 ar->append_dim((*it_d)->getSize(), (*it_d)->getName());
470 HDFEOS2ArraySwathDimMapField * ar = NULL;
477 ar =
new HDFEOS2ArraySwathDimMapField(
484 "MODIS_Swath_Type_GEO",
488 (*it_f)->getNewName(),
490 for(it_d = dims.begin(); it_d != dims.end(); it_d++)
491 ar->append_dim((*it_d)->getSize(), (*it_d)->getName());
499 HDFEOS2ArraySwathDimMapField * ar = NULL;
505 ar =
new HDFEOS2ArraySwathDimMapField(
512 (dataset)->getName(),
516 (*it_f)->getNewName(),
518 for(it_d = dims.begin(); it_d != dims.end(); it_d++)
519 ar->append_dim((*it_d)->getSize(), (*it_d)->getName());
528 HDFEOS2ArraySwathDimMapField * ar = NULL;
529 ar =
new HDFEOS2ArraySwathDimMapField(
536 (dataset)->getName(),
540 (*it_f)->getNewName(),
542 for(it_d = dims.begin(); it_d != dims.end(); it_d++)
543 ar->append_dim((*it_d)->getSize(), (*it_d)->getName());
551 HDFEOS2Array_RealField * ar = NULL;
552 ar =
new HDFEOS2Array_RealField(
559 (dataset)->getName(),
562 (*it_f)->getNewName(),
564 for(it_d = dims.begin(); it_d != dims.end(); it_d++)
565 ar->append_dim((*it_d)->getSize(), (*it_d)->getName());
573 throw InternalErr(__FILE__, __LINE__,
"The current type should be either grid or swath");
578 else if(fieldtype == 1 || fieldtype == 2) {
581 if(grid_or_swath==0) {
583 HDFEOS2ArrayGridGeoField *ar = NULL;
585 bool ydimmajor = (*it_f)->getYDimMajor();
586 bool condenseddim = (*it_f)->getCondensedDim();
587 bool speciallon = (*it_f)->getSpecialLon();
588 int specialformat = (*it_f)->getSpecialLLFormat();
590 ar =
new HDFEOS2ArrayGridGeoField(
601 (dataset)->getName(),
603 (*it_f)->getNewName(),
606 for(it_d = dims.begin(); it_d != dims.end(); it_d++)
607 ar->append_dim((*it_d)->getSize(), (*it_d)->getName());
620 else if(grid_or_swath ==1) {
623 if((*it_f)->UseDimMap()) {
626 if(!modis_geofilename.empty()) {
628 if (
false == geofile_has_dimmap) {
629 HDFEOS2ArraySwathGeoDimMapExtraField *ar = NULL;
630 ar =
new HDFEOS2ArraySwathGeoDimMapExtraField(
634 (*it_f)->getNewName(),
636 for(it_d = dims.begin(); it_d != dims.end(); it_d++)
637 ar->append_dim((*it_d)->getSize(), (*it_d)->getName());
644 HDFEOS2ArraySwathDimMapField * ar = NULL;
650 ar =
new HDFEOS2ArraySwathDimMapField(
657 "MODIS_Swath_Type_GEO",
661 (*it_f)->getNewName(),
663 for(it_d = dims.begin(); it_d != dims.end(); it_d++)
664 ar->append_dim((*it_d)->getSize(), (*it_d)->getName());
674 HDFEOS2ArraySwathDimMapField * ar = NULL;
675 ar =
new HDFEOS2ArraySwathDimMapField(
682 (dataset)->getName(),
686 (*it_f)->getNewName(),
688 for(it_d = dims.begin(); it_d != dims.end(); it_d++)
689 ar->append_dim((*it_d)->getSize(), (*it_d)->getName());
698 HDFEOS2ArraySwathGeoField * ar = NULL;
699 ar =
new HDFEOS2ArraySwathGeoField(
703 (dataset)->getName(),
705 (*it_f)->getNewName(),
708 for(it_d = dims.begin(); it_d != dims.end(); it_d++)
709 ar->append_dim((*it_d)->getSize(), (*it_d)->getName());
717 throw InternalErr(__FILE__, __LINE__,
"The current type should be either grid or swath");
723 else if(fieldtype == 4) {
725 if((*it_f)->getRank()!=1){
727 throw InternalErr(__FILE__, __LINE__,
"The rank of missing Z dimension field must be 1");
730 int nelem = ((*it_f)->getCorrectedDimensions()[0])->getSize();
731 HDFEOS2ArrayMissGeoField *ar = NULL;
732 ar =
new HDFEOS2ArrayMissGeoField(
735 (*it_f)->getNewName(),
738 for(it_d = dims.begin(); it_d != dims.end(); it_d++)
739 ar->append_dim((*it_d)->getSize(), (*it_d)->getName());
747 throw InternalErr(__FILE__, __LINE__,
"Encounter unsupported datatype or The field type should be between 0 and 5. ");
757 int read_dds_hdfeos2(DDS & dds,
const string & filename,int32 sdfd,int32 fileid, int32 gridfd, int32 swathfd,
HDFSP::File*spf,HDFEOS2::File*f)
760 BESDEBUG(
"h4",
"Coming to read_dds_hdfeos2 "<<endl);
763 dds.set_dataset_name(basename(filename));
771 if((basename(filename).size() >=5) && ((basename(filename)).compare(0,5,
"MERRA")==0))
776 string check_enable_spec_eos_key=
"H4.EnableSpecialEOS";
777 bool turn_on_enable_spec_eos_key=
false;
778 turn_on_enable_spec_eos_key = HDFCFUtil::check_beskeys(check_enable_spec_eos_key);
782 if(
true == HDF4RequestHandler::get_enable_special_eos()) {
785 int ret_val = check_special_eosfile(filename,grid_name,sdfd,fileid);
796 if(2 == ret_val || 3 == ret_val) {
799 read_dds_special_1d_grid(dds,spf,filename,sdfd,
false);
819 const vector<HDFEOS2::GridDataset *>& grids = f->getGrids();
821 bool onelatlon = f->getOneLatLon();
824 SOType sotype = DEFAULT_CF_EQU;
827 vector<HDFEOS2::GridDataset *>::const_iterator it_g;
828 for(it_g = grids.begin(); it_g != grids.end(); it_g++){
831 ownll = onelatlon?onelatlon:(*it_g)->getLatLonFlag();
834 sotype = (*it_g)->getScaleType();
836 read_dds_hdfeos2_grid_swath(
837 dds, filename,
static_cast<HDFEOS2::Dataset*
>(*it_g), 0,ownll,sotype,sdfd,fileid,gridfd,swathfd);
840 HDFCFUtil::add_cf_grid_cvs(dds,*it_g);
849 const vector<HDFEOS2::SwathDataset *>& swaths= f->getSwaths();
850 vector<HDFEOS2::SwathDataset *>::const_iterator it_s;
851 for(it_s = swaths.begin(); it_s != swaths.end(); it_s++) {
854 sotype = (*it_s)->getScaleType();
856 read_dds_hdfeos2_grid_swath(
857 dds, filename,
static_cast<HDFEOS2::Dataset*
>(*it_s), 1,
false,sotype,sdfd,fileid,gridfd,swathfd);
869 ctype_field_namelist.clear();
877 bool read_dds_hdfhybrid(DDS & dds,
const string & filename,int32 sdfd, int32 fileid,
HDFSP::File*f)
881 BESDEBUG(
"h4",
"Coming to read_dds_hdfhybrid "<<endl);
884 dds.set_dataset_name(basename(filename));
890 vector<HDFSP::SDField *>::const_iterator it_g;
891 for(it_g = spsds.begin(); it_g != spsds.end(); it_g++){
893 read_dds_spfields(dds,filename,sdfd,(*it_g),f->
getSPType());
908 string check_hybrid_vdata_key=
"H4.EnableHybridVdata";
909 bool turn_on_hybrid_vdata_key =
false;
910 turn_on_hybrid_vdata_key = HDFCFUtil::check_beskeys(check_hybrid_vdata_key);
914 if(
true == HDF4RequestHandler::get_enable_hybrid_vdata()) {
915 for(vector<HDFSP::VDATA *>::const_iterator i = f->
getVDATAs().begin(); i!=f->
getVDATAs().end();i++) {
916 if(
false == (*i)->getTreatAsAttrFlag()){
917 for(vector<HDFSP::VDField *>::const_iterator j=(*i)->getFields().begin();j!=(*i)->getFields().end();j++) {
919 read_dds_spvdfields(dds,filename,fileid, (*i)->getObjRef(),(*j)->getNumRec(),(*j));
934 bool read_das_hdfhybrid(DAS & das,
const string & filename,int32 sdfd, int32 fileid,
HDFSP::File**fpptr)
937 BESDEBUG(
"h4",
"Coming to read_das_hdfhybrid "<<endl);
948 throw InternalErr(e.
what());
955 string check_scale_offset_type_key =
"H4.EnableCheckScaleOffsetType";
956 bool turn_on_enable_check_scale_offset_key=
false;
957 turn_on_enable_check_scale_offset_key = HDFCFUtil::check_beskeys(check_scale_offset_type_key);
962 vector<HDFSP::SDField *>::const_iterator it_g;
963 for(it_g = spsds.begin(); it_g != spsds.end(); it_g++){
966 AttrTable *at = das.get_table((*it_g)->getNewName());
968 at = das.add_table((*it_g)->getNewName(),
new AttrTable);
971 bool long_name_flag =
false;
973 for(vector<HDFSP::Attribute *>::const_iterator i=(*it_g)->getAttributes().begin();i!=(*it_g)->getAttributes().end();i++) {
975 if((*i)->getName() ==
"long_name") {
976 long_name_flag =
true;
981 if(
false == long_name_flag)
982 at->append_attr(
"long_name",
"String", (*it_g)->getName());
985 for(vector<HDFSP::Attribute *>::const_iterator i=(*it_g)->getAttributes().begin();i!=(*it_g)->getAttributes().end();i++) {
988 if((*i)->getType()==DFNT_UCHAR || (*i)->getType() == DFNT_CHAR){
991 string tempstring2((*i)->getValue().begin(),(*i)->getValue().end());
992 string tempfinalstr= string(tempstring2.c_str());
997 at->append_attr((*i)->getNewName(),
"String" , ((*i)->getNewName()==
"fullpath")?tempfinalstr:
HDFCFUtil::escattr(tempfinalstr));
1000 for (
int loc=0; loc < (*i)->getCount() ; loc++) {
1010 int32 var_type = (*it_g)->getType();
1026 if (
true == HDF4RequestHandler::get_enable_check_scale_offset_type() && at !=NULL)
1033 HDFCFUtil::handle_vdata_attrs_with_desc_key(f,das);
1045 void read_dds_use_eos2lib(DDS & dds,
const string & filename,int32 sdfd,int32 fileid, int32 gridfd, int32 swathfd,
HDFSP::File*h4file,HDFEOS2::File*eosfile)
1048 BESDEBUG(
"h4",
"Coming to read_dds_use_eos2lib" <<endl);
1050 int ret_value = read_dds_hdfeos2(dds,filename,sdfd,fileid,gridfd,swathfd,h4file,eosfile);
1052 BESDEBUG(
"h4",
"ret_value of read_dds_hdfeos2 is "<<ret_value<<endl);
1070 if (0 == ret_value || 5 == ret_value || 4 == ret_value ) {
1071 if(
true == read_dds_hdfsp(dds, filename,sdfd,fileid,h4file))
1075 else if ( 1 == ret_value ) {
1078 if(
true ==read_dds_hdfhybrid(dds,filename,sdfd,fileid,h4file))
1088 if(
true == read_dds_hdfeos2(dds, filename)){
1091 if(
true == read_dds_hdfhybrid(dds,filename))
1096 if(read_dds_hdfsp(dds, filename)){
1103 read_dds(dds, filename);
1108 void write_non_ecsmetadata_attrs(
HE2CF& cf) {
1110 cf.set_non_ecsmetadata_attrs();
1115 void write_ecsmetadata(DAS& das,
HE2CF& cf,
const string& _meta)
1134 bool suffix_is_number =
true;
1135 vector<string> meta_nonum_names;
1136 vector<string> meta_nonum_data;
1138 string meta = cf.
get_metadata(_meta,suffix_is_number,meta_nonum_names, meta_nonum_data);
1140 if(
""==meta &&
true == suffix_is_number){
1144 BESDEBUG(
"h4",meta << endl);
1146 if (
false == suffix_is_number) {
1150 for (
unsigned int i = 0; i <meta_nonum_names.size(); i++)
1151 parse_ecs_metadata(das,meta_nonum_names[i],meta_nonum_data[i]);
1154 parse_ecs_metadata(das,_meta,meta);
1158 void parse_ecs_metadata(DAS &das,
const string & metaname,
const string &metadata) {
1161 AttrTable *at = das.get_table(metaname);
1163 at = das.add_table(metaname,
new AttrTable);
1166 void *buf = hdfeos_string(metadata.c_str());
1169 if (hdfeosparse(&arg) != 0) {
1170 hdfeos_delete_buffer(buf);
1171 throw Error(
"HDF-EOS parse error while processing a " + metadata +
" HDFEOS attribute.");
1174 if (arg.status() ==
false) {
1175 (*BESLog::TheLog())<<
"HDF-EOS parse error while processing a "
1176 << metadata <<
" HDFEOS attribute. (2) " << endl;
1180 hdfeos_delete_buffer(buf);
1184 int read_das_hdfeos2(DAS & das,
const string & filename,int32 sdfd,int32 fileid, int32 gridfd, int32 swathfd,
1185 bool ecs_metadata,
HDFSP::File**spfpptr,HDFEOS2::File **fpptr)
1188 BESDEBUG(
"h4",
"Coming to read_das_hdfeos2 " << endl);
1200 if((basename(filename).size() >=5) && ((basename(filename)).compare(0,5,
"MERRA")==0)) {
1206 string check_enable_spec_eos_key=
"H4.EnableSpecialEOS";
1207 bool turn_on_enable_spec_eos_key=
false;
1208 turn_on_enable_spec_eos_key = HDFCFUtil::check_beskeys(check_enable_spec_eos_key);
1210 if(
true == HDF4RequestHandler::get_enable_special_eos()) {
1213 int ret_val = check_special_eosfile(filename,grid_name,sdfd,fileid);
1219 bool airs_l2_l3_v6 =
false;
1220 bool special_1d_grid =
false;
1223 if(2 == ret_val || 3 == ret_val) {
1233 throw InternalErr(e.
what());
1240 if(spf->Check_update_special(grid_name)==
true){
1242 special_1d_grid =
true;
1245 read_das_special_eos2_core(das,spf,filename,ecs_metadata);
1248 if(grid_name ==
"mod08") {
1249 change_das_mod08_scale_offset(das,spf);
1255 airs_l2_l3_v6 =
true;
1256 spf->Handle_AIRS_L23();
1257 read_das_special_eos2_core(das,spf,filename,ecs_metadata);
1269 if (
true == special_1d_grid ||
true == airs_l2_l3_v6) {
1277 HDFEOS2::File *f = NULL;
1281 f= HDFEOS2::File::Read(filename.c_str(),gridfd,swathfd);
1283 catch (HDFEOS2::Exception &e){
1289 if (!e.getFileType()){
1295 throw InternalErr(e.what());
1302 f->Prepare(filename.c_str());
1305 catch (HDFEOS2:: Exception &e) {
1308 throw InternalErr(e.what());
1317 cf.
open(filename,sdfd,fileid);
1324 SOType sotype = DEFAULT_CF_EQU;
1329 bool tempstrflag =
false;
1332 bool filename_change_scale =
false;
1333 if (f->getSwaths().size() > 0) {
1334 string temp_fname = basename(filename);
1335 string temp_prod_prefix =
"AMSR_E";
1336 if ((temp_fname.size() > temp_prod_prefix.size()) &&
1337 (0 == (temp_fname.compare(0,temp_prod_prefix.size(),temp_prod_prefix))))
1338 filename_change_scale =
true;
1342 bool gridname_change_valid_range =
false;
1343 if(1 == f->getGrids().size()) {
1344 string gridname = f->getGrids()[0]->getName();
1345 if (
"VIP_CMG_GRID" == gridname)
1346 gridname_change_valid_range =
true;
1350 bool is_modis_l1b =
false;
1353 for (
int i = 0; i<(int) f->getSwaths().size(); i++) {
1354 HDFEOS2::SwathDataset* swath = f->getSwaths()[i];
1355 string sname = swath->getName();
1356 if(
"MODIS_SWATH_Type_L1B" == sname){
1357 is_modis_l1b =
true;
1363 string check_disable_scale_comp_key =
"H4.DisableScaleOffsetComp";
1364 bool turn_on_disable_scale_comp_key=
false;
1365 turn_on_disable_scale_comp_key = HDFCFUtil::check_beskeys(check_disable_scale_comp_key);
1367 string check_scale_offset_type_key =
"H4.EnableCheckScaleOffsetType";
1368 bool turn_on_enable_check_scale_offset_key=
false;
1369 turn_on_enable_check_scale_offset_key = HDFCFUtil::check_beskeys(check_scale_offset_type_key);
1375 for (
int i = 0; i < (int) f->getGrids().size(); i++) {
1377 HDFEOS2::GridDataset* grid = f->getGrids()[i];
1378 string gname = grid->getName();
1379 sotype = grid->getScaleType();
1381 const vector<HDFEOS2::Field*>gfields = grid->getDataFields();
1382 vector<HDFEOS2::Field*>::const_iterator it_gf;
1384 for (it_gf = gfields.begin();it_gf != gfields.end();++it_gf) {
1386 bool change_fvtype =
false;
1389 string fname = (*it_gf)->getName();
1392 string newfname = (*it_gf)->getNewName();
1394 BESDEBUG(
"h4",
"Original field name: " << fname << endl);
1395 BESDEBUG(
"h4",
"Corrected field name: " << newfname << endl);
1398 int fieldtype = (*it_gf)->getFieldType();
1401 if (fieldtype == 0){
1404 if((*it_gf)->haveAddedFillValue()) {
1405 BESDEBUG(
"h4",
"Has an added fill value." << endl);
1406 float addedfillvalue =
1407 (*it_gf)->getAddedFillValue();
1409 (*it_gf)->getType();
1410 BESDEBUG(
"h4",
"Added fill value = "<<addedfillvalue);
1412 type, addedfillvalue);
1414 string coordinate = (*it_gf)->getCoordinate();
1415 BESDEBUG(
"h4",
"Coordinate attribute: " << coordinate <<endl);
1416 if (coordinate !=
"")
1422 f->getGrids().size(), fieldtype);
1434 if(fieldtype == 1 && ((*it_gf)->getSpecialLLFormat())==3)
1439 if (fieldtype !=3) {
1440 string tempunits = (*it_gf)->getUnits();
1442 "fieldtype " << fieldtype
1443 <<
" units" << tempunits
1450 AttrTable *at = das.get_table(newfname);
1453 if(sotype!=DEFAULT_CF_EQU && at!=NULL)
1455 bool has_Key_attr =
false;
1456 AttrTable::Attr_iter it = at->attr_begin();
1457 while (it!=at->attr_end())
1459 if(at->get_name(it)==
"Key")
1461 has_Key_attr =
true;
1468 if((
false == is_modis_l1b) && (
false == gridname_change_valid_range)&&(
false == has_Key_attr) &&
1469 (
true == HDF4RequestHandler::get_disable_scaleoffset_comp()))
1470 HDFCFUtil::handle_modis_special_attrs_disable_scale_comp(at,basename(filename),
true, newfname,sotype);
1474 bool changedtype = HDFCFUtil::change_data_type(das,sotype,newfname);
1477 if (
true == changedtype)
1478 ctype_field_namelist.push_back(newfname);
1480 HDFCFUtil::handle_modis_special_attrs(at,basename(filename),
true, newfname,sotype,gridname_change_valid_range,changedtype,change_fvtype);
1486 HDFCFUtil::handle_amsr_attrs(at);
1490 if((
false == change_fvtype) && at != NULL) {
1491 int32 var_type = (*it_gf)->getType();
1501 if (
true == HDF4RequestHandler::get_enable_check_scale_offset_type() && at!=NULL)
1508 HDFCFUtil::add_cf_grid_cv_attrs(das,grid);
1519 for (
int i = 0; i < (int) f->getSwaths().size(); i++) {
1521 HDFEOS2::SwathDataset* swath = f->getSwaths()[i];
1525 const vector<HDFEOS2::Field*> geofields = swath->getGeoFields();
1526 vector<HDFEOS2::Field*> all_fields = geofields;
1527 vector<HDFEOS2::Field*>::const_iterator it_f;
1529 const vector<HDFEOS2::Field*> datafields = swath->getDataFields();
1530 for (it_f = datafields.begin(); it_f != datafields.end(); it_f++)
1531 all_fields.push_back(*it_f);
1533 int total_geofields = geofields.size();
1535 string gname = swath->getName();
1536 BESDEBUG(
"h4",
"Swath name: " << gname << endl);
1538 sotype = swath->getScaleType();
1541 int field_counter = 0;
1543 for(it_f = all_fields.begin(); it_f != all_fields.end(); it_f++)
1545 bool change_fvtype =
false;
1546 string fname = (*it_f)->getName();
1547 string newfname = (*it_f)->getNewName();
1548 BESDEBUG(
"h4",
"Original Field name: " << fname << endl);
1549 BESDEBUG(
"h4",
"Corrected Field name: " << newfname << endl);
1551 int fieldtype = (*it_f)->getFieldType();
1552 if (fieldtype == 0){
1553 string coordinate = (*it_f)->getCoordinate();
1554 BESDEBUG(
"h4",
"Coordinate attribute: " << coordinate <<endl);
1555 if (coordinate !=
"")
1563 if(fieldtype >0 && fieldtype !=3){
1564 string tempunits = (*it_f)->getUnits();
1566 "fieldtype " << fieldtype
1567 <<
" units" << tempunits << endl);
1571 BESDEBUG(
"h4",
"Field Name: " << fname << endl);
1575 if (field_counter >=total_geofields) {
1576 if((*it_f)->haveAddedFillValue()){
1577 float addedfillvalue =
1578 (*it_f)->getAddedFillValue();
1581 BESDEBUG(
"h4",
"Added fill value = "<<addedfillvalue);
1586 f->getSwaths().size(), fieldtype);
1588 AttrTable *at = das.get_table(newfname);
1591 if(sotype!=DEFAULT_CF_EQU && at!=NULL)
1594 bool has_Key_attr =
false;
1595 AttrTable::Attr_iter it = at->attr_begin();
1596 while (it!=at->attr_end())
1598 if(at->get_name(it)==
"Key")
1600 has_Key_attr =
true;
1607 if((
false == is_modis_l1b) && (
false == gridname_change_valid_range) &&(
false == has_Key_attr) &&
1608 (
true == HDF4RequestHandler::get_disable_scaleoffset_comp()))
1609 HDFCFUtil::handle_modis_special_attrs_disable_scale_comp(at,basename(filename),
false,newfname,sotype);
1613 bool changedtype = HDFCFUtil::change_data_type(das,sotype,newfname);
1616 if (
true == changedtype)
1618 ctype_field_namelist.push_back(newfname);
1623 HDFCFUtil::handle_modis_special_attrs(at,basename(filename),
false,newfname,sotype,gridname_change_valid_range,changedtype,change_fvtype);
1629 HDFCFUtil::handle_amsr_attrs(at);
1633 if((
false == change_fvtype) && at != NULL) {
1634 int32 var_type = (*it_f)->getType();
1644 if (
true == HDF4RequestHandler::get_enable_check_scale_offset_type() && at !=NULL)
1659 if(ecs_metadata ==
true) {
1662 write_ecsmetadata(das,cf,
"CoreMetadata");
1664 write_ecsmetadata(das,cf,
"coremetadata");
1666 write_ecsmetadata(das,cf,
"ArchiveMetadata");
1668 write_ecsmetadata(das,cf,
"archivemetadata");
1670 write_ecsmetadata(das,cf,
"ProductMetadata");
1672 write_ecsmetadata(das,cf,
"productmetadata");
1676 if(
false == tempstrflag) {
1679 string check_disable_smetadata_key =
"H4.DisableStructMetaAttr";
1680 bool is_check_disable_smetadata =
false;
1681 is_check_disable_smetadata = HDFCFUtil::check_beskeys(check_disable_smetadata_key);
1684 if (
false == HDF4RequestHandler::get_disable_structmeta() ) {
1685 write_ecsmetadata(das, cf,
"StructMetadata");
1690 write_non_ecsmetadata_attrs(cf);
1703 string check_enable_sg_attr_key=
"H4.EnableSwathGridAttr";
1704 bool turn_on_enable_sg_attr_key=
false;
1705 turn_on_enable_sg_attr_key = HDFCFUtil::check_beskeys(check_enable_sg_attr_key);
1708 if(
true == HDF4RequestHandler::get_enable_swath_grid_attr()) {
1711 for (
int i = 0; i < (int) f->getGrids().size(); i++) {
1714 HDFEOS2::GridDataset* grid = f->getGrids()[i];
1718 AttrTable*at = NULL;
1721 if(grid->getAttributes().size() != 0){
1722 at = das.get_table(gname);
1724 at = das.add_table(gname,
new AttrTable);
1729 const vector<HDFEOS2::Attribute *> grid_attrs = grid->getAttributes();
1730 vector<HDFEOS2::Attribute*>::const_iterator it_a;
1731 for (it_a = grid_attrs.begin(); it_a != grid_attrs.end(); ++it_a) {
1733 int attr_type = (*it_a)->getType();
1736 if(attr_type==DFNT_UCHAR || attr_type == DFNT_CHAR){
1737 string tempstring2((*it_a)->getValue().begin(),(*it_a)->getValue().end());
1738 string tempfinalstr= string(tempstring2.c_str());
1748 for (
int loc=0; loc < (*it_a)->getCount() ; loc++) {
1749 string print_rep =
HDFCFUtil::print_attr((*it_a)->getType(), loc, (
void*) &((*it_a)->getValue()[0]));
1759 for (
int i = 0; i < (int) f->getSwaths().size(); i++) {
1761 HDFEOS2::SwathDataset* swath = f->getSwaths()[i];
1762 string sname = swath->getName();
1763 AttrTable*at = NULL;
1766 if(swath->getAttributes().size() != 0) {
1767 at = das.get_table(sname);
1769 at = das.add_table(sname,
new AttrTable);
1773 const vector<HDFEOS2::Attribute *> swath_attrs = swath->getAttributes();
1774 vector<HDFEOS2::Attribute*>::const_iterator it_a;
1775 for (it_a = swath_attrs.begin(); it_a != swath_attrs.end(); ++it_a) {
1777 int attr_type = (*it_a)->getType();
1780 if(attr_type==DFNT_UCHAR || attr_type == DFNT_CHAR){
1781 string tempstring2((*it_a)->getValue().begin(),(*it_a)->getValue().end());
1782 string tempfinalstr= string(tempstring2.c_str());
1790 for (
int loc=0; loc < (*it_a)->getCount() ; loc++) {
1791 string print_rep =
HDFCFUtil::print_attr((*it_a)->getType(), loc, (
void*) &((*it_a)->getValue()[0]));
1809 void read_das_use_eos2lib(DAS & das,
const string & filename,
1810 int32 sdfd,int32 fileid, int32 gridfd, int32 swathfd,
bool ecs_metadata,
1811 HDFSP::File**h4filepptr,HDFEOS2::File**eosfilepptr)
1814 BESDEBUG(
"h4",
"Coming to read_das_use_eos2lib" << endl);
1816 int ret_value = read_das_hdfeos2(das,filename,sdfd,fileid, gridfd, swathfd,ecs_metadata,h4filepptr,eosfilepptr);
1818 BESDEBUG(
"h4",
"ret_value of read_das_hdfeos2 is "<<ret_value <<endl);
1834 if (ret_value == 4) {
1835 if(
true == read_das_special_eos2(das, filename,sdfd,fileid,ecs_metadata,h4filepptr))
1839 else if (ret_value == 2 || ret_value == 3) {
1842 else if (ret_value == 1) {
1845 if(
true == read_das_hdfhybrid(das,filename,sdfd,fileid,h4filepptr))
1849 if(
true == read_das_hdfsp(das, filename,sdfd, fileid,h4filepptr))
1857 if(
true == read_das_hdfeos2(das, filename)){
1860 if (
true == read_das_hdfhybrid(das,filename))
1865 if(
true == read_das_hdfsp(das, filename)){
1872 read_das(das, filename);
1875 #endif // #ifdef USE_HDFEOS2_LIB
1879 bool read_dds_hdfsp(DDS & dds,
const string & filename,int32 sdfd, int32 fileid,
HDFSP::File*f)
1882 BESDEBUG(
"h4",
"Coming to read_dds_sp "<<endl);
1883 dds.set_dataset_name(basename(filename));
1889 vector<HDFSP::SDField *>::const_iterator it_g;
1890 for(it_g = spsds.begin(); it_g != spsds.end(); it_g++){
1896 if (
false == f->
Has_Dim_NoScale_Field() || (0 == (*it_g)->getFieldType()) || (
true == (*it_g)->IsDimScale())){
1898 read_dds_spfields(dds,filename,sdfd,(*it_g),f->
getSPType());
1909 string check_ceres_vdata_key=
"H4.EnableCERESVdata";
1910 bool turn_on_ceres_vdata_key=
false;
1911 turn_on_ceres_vdata_key = HDFCFUtil::check_beskeys(check_ceres_vdata_key);
1914 bool output_vdata_flag =
true;
1916 if (
false == HDF4RequestHandler::get_enable_ceres_vdata() &&
1921 output_vdata_flag =
false;
1923 if(
true == output_vdata_flag) {
1924 for(vector<HDFSP::VDATA *>::const_iterator i=f->
getVDATAs().begin(); i!=f->
getVDATAs().end();i++) {
1925 if(!(*i)->getTreatAsAttrFlag()){
1926 for(vector<HDFSP::VDField *>::const_iterator j=(*i)->getFields().begin();j!=(*i)->getFields().end();j++) {
1928 read_dds_spvdfields(dds,filename,fileid,(*i)->getObjRef(),(*j)->getNumRec(),(*j));
1944 bool read_das_hdfsp(DAS & das,
const string & filename, int32 sdfd, int32 fileid,
HDFSP::File**fpptr)
1947 BESDEBUG(
"h4",
"Coming to read_das_sp "<<endl);
1959 throw InternalErr(e.
what());
1969 throw InternalErr(e.
what());
1976 string check_enable_vg_attr_key=
"H4.EnableVgroupAttr";
1977 bool turn_on_enable_vg_attr_key=
false;
1978 turn_on_enable_vg_attr_key = HDFCFUtil::check_beskeys(check_enable_vg_attr_key);
1983 if(
true == HDF4RequestHandler::get_enable_vgroup_attr()) {
1986 vector<HDFSP::AttrContainer *>vg_container = f->
getVgattrs();
1987 for(vector<HDFSP::AttrContainer *>::const_iterator i=f->
getVgattrs().begin();i!=f->
getVgattrs().end();i++) {
1988 AttrTable *vgattr_at = das.get_table((*i)->getName());
1990 vgattr_at = das.add_table((*i)->getName(),
new AttrTable);
1992 for(vector<HDFSP::Attribute *>::const_iterator j=(*i)->getAttributes().begin();j!=(*i)->getAttributes().end();j++) {
1995 if((*j)->getType()==DFNT_UCHAR || (*j)->getType() == DFNT_CHAR){
1996 string tempstring2((*j)->getValue().begin(),(*j)->getValue().end());
1997 string tempfinalstr= string(tempstring2.c_str());
2000 vgattr_at->append_attr((*j)->getNewName(),
"String" ,
HDFCFUtil::escattr(tempfinalstr));
2003 for (
int loc=0; loc < (*j)->getCount() ; loc++) {
2014 string core_metadata =
"";
2015 string archive_metadata =
"";
2016 string struct_metadata =
"";
2027 if(((*i)->getName().compare(0, 12,
"CoreMetadata" )== 0) ||
2028 ((*i)->getName().compare(0, 12,
"coremetadata" )== 0)){
2033 string tempstring((*i)->getValue().begin(),(*i)->getValue().end());
2040 core_metadata.append(tempstring);
2042 else if(((*i)->getName().compare(0, 15,
"ArchiveMetadata" )== 0) ||
2043 ((*i)->getName().compare(0, 16,
"ArchivedMetadata")==0) ||
2044 ((*i)->getName().compare(0, 15,
"archivemetadata" )== 0)){
2045 string tempstring((*i)->getValue().begin(),(*i)->getValue().end());
2050 archive_metadata.append(tempstring);
2052 else if(((*i)->getName().compare(0, 14,
"StructMetadata" )== 0) ||
2053 ((*i)->getName().compare(0, 14,
"structmetadata" )== 0)){
2056 string check_disable_smetadata_key =
"H4.DisableStructMetaAttr";
2057 bool is_check_disable_smetadata =
false;
2058 is_check_disable_smetadata = HDFCFUtil::check_beskeys(check_disable_smetadata_key);
2062 if (
false == HDF4RequestHandler::get_disable_structmeta()) {
2064 string tempstring((*i)->getValue().begin(),(*i)->getValue().end());
2072 struct_metadata.append(tempstring);
2078 AttrTable *at = das.get_table(
"HDF_GLOBAL");
2080 at = das.add_table(
"HDF_GLOBAL",
new AttrTable);
2083 if((*i)->getType()==DFNT_UCHAR || (*i)->getType() == DFNT_CHAR){
2084 string tempstring2((*i)->getValue().begin(),(*i)->getValue().end());
2085 string tempfinalstr= string(tempstring2.c_str());
2094 for (
int loc=0; loc < (*i)->getCount() ; loc++) {
2108 if(core_metadata.size() > 0){
2109 AttrTable *at = das.get_table(
"CoreMetadata");
2111 at = das.add_table(
"CoreMetadata",
new AttrTable);
2113 void *buf = hdfeos_string(core_metadata.c_str());
2116 if (hdfeosparse(&arg) != 0) {
2117 hdfeos_delete_buffer(buf);
2118 throw Error(
"Parse error while processing a CoreMetadata attribute.");
2122 if (arg.status() ==
false) {
2123 (*BESLog::TheLog()) <<
"Parse error while processing a CoreMetadata attribute. (2) " << endl;
2127 hdfeos_delete_buffer(buf);
2131 if(archive_metadata.size() > 0){
2132 AttrTable *at = das.get_table(
"ArchiveMetadata");
2134 at = das.add_table(
"ArchiveMetadata",
new AttrTable);
2136 void *buf = hdfeos_string(archive_metadata.c_str());
2138 if (hdfeosparse(&arg) != 0){
2140 hdfeos_delete_buffer(buf);
2141 throw Error(
"Parse error while processing an ArchiveMetadata attribute.");
2145 if (arg.status() ==
false) {
2146 (*BESLog::TheLog())<<
"Parse error while processing an ArchiveMetadata attribute. (2) " << endl;
2150 hdfeos_delete_buffer(buf);
2154 if(struct_metadata.size() > 0){
2155 AttrTable *at = das.get_table(
"StructMetadata");
2157 at = das.add_table(
"StructMetadata",
new AttrTable);
2159 void *buf = hdfeos_string(struct_metadata.c_str());
2161 if (hdfeosparse(&arg) != 0){
2163 hdfeos_delete_buffer(buf);
2164 throw Error(
"Parse error while processing a StructMetadata attribute.");
2167 if (arg.status() ==
false) {
2168 (*BESLog::TheLog())<<
"Parse error while processing a StructMetadata attribute. (2)" << endl;
2174 if (arg.status() ==
false) {
2175 (*BESLog::TheLog())<<
"Parse error while processing a StructMetadata attribute. (2)" << endl
2176 << arg.error()->get_error_message() << endl;
2180 hdfeos_delete_buffer(buf);
2189 bool global_slope_flag =
false;
2190 float intercept = 0.;
2191 bool global_intercept_flag =
false;
2195 HDFCFUtil::check_obpg_global_attrs(f,scaling,slope,global_slope_flag,intercept,global_intercept_flag);
2199 vector<HDFSP::SDField *>::const_iterator it_g;
2200 for(it_g = spsds.begin(); it_g != spsds.end(); it_g++){
2207 ((*it_g)->getFieldType() !=0)&&
2208 ((*it_g)->IsDimScale() ==
false))
2212 if (OTHERHDF == f->
getSPType() &&
true == (*it_g)->IsDimNoScale())
2215 AttrTable *at = das.get_table((*it_g)->getNewName());
2217 at = das.add_table((*it_g)->getNewName(),
new AttrTable);
2220 bool long_name_flag =
false;
2222 for(vector<HDFSP::Attribute *>::const_iterator i=(*it_g)->getAttributes().begin();
2223 i!=(*it_g)->getAttributes().end();i++) {
2224 if((*i)->getName() ==
"long_name") {
2225 long_name_flag =
true;
2230 if(
false == long_name_flag) {
2232 if((*it_g)->getFieldType() == 1)
2233 at->append_attr(
"standard_name",
"String",
"latitude");
2234 else if ((*it_g)->getFieldType() == 2) {
2235 at->append_attr(
"standard_name",
"String",
"longitude");
2241 if((*it_g)->getFieldType() == 1) {
2242 at->append_attr(
"long_name",
"String",
"latitude");
2243 at->append_attr(
"standard_name",
"String",
"latitude");
2246 else if ((*it_g)->getFieldType() == 2) {
2247 at->append_attr(
"long_name",
"String",
"longitude");
2248 at->append_attr(
"standard_name",
"String",
"longitude");
2253 at->append_attr(
"long_name",
"String", (*it_g)->getName());
2259 HDFCFUtil::add_obpg_special_attrs(f,das,*it_g,scaling,slope,global_slope_flag,intercept,global_intercept_flag);
2262 for(vector<HDFSP::Attribute *>::const_iterator i=(*it_g)->getAttributes().begin();i!=(*it_g)->getAttributes().end();i++) {
2265 if((*i)->getType()==DFNT_UCHAR || (*i)->getType() == DFNT_CHAR){
2266 string tempstring2((*i)->getValue().begin(),(*i)->getValue().end());
2267 string tempfinalstr= string(tempstring2.c_str());
2273 at->append_attr((*i)->getNewName(),
"String" ,((*i)->getNewName()==
"fullpath")?tempfinalstr:
HDFCFUtil::escattr(tempfinalstr));
2276 for (
int loc=0; loc < (*i)->getCount() ; loc++) {
2291 bool has_dim_info =
true;
2292 vector<HDFSP::AttrContainer *>::const_iterator it_end = (*it_g)->getDimInfo().end();
2293 if((*it_g)->getType() == DFNT_CHAR) {
2294 if((*it_g)->getRank() >1 && (*it_g)->getDimInfo().size() >1)
2295 it_end = (*it_g)->getDimInfo().begin()+(*it_g)->getDimInfo().size() -1;
2297 has_dim_info =
false;
2300 if(
true == has_dim_info) {
2302 for(vector<HDFSP::AttrContainer *>::const_iterator i=(*it_g)->getDimInfo().begin();i!=it_end;i++) {
2306 string attr_container_name = (*it_g)->getNewName() + (*i)->getName();
2307 AttrTable *dim_at = das.get_table(attr_container_name);
2309 dim_at = das.add_table(attr_container_name,
new AttrTable);
2311 for(vector<HDFSP::Attribute *>::const_iterator j=(*i)->getAttributes().begin();j!=(*i)->getAttributes().end();j++) {
2314 if((*j)->getType()==DFNT_UCHAR || (*j)->getType() == DFNT_CHAR){
2315 string tempstring2((*j)->getValue().begin(),(*j)->getValue().end());
2316 string tempfinalstr= string(tempstring2.c_str());
2319 dim_at->append_attr((*j)->getNewName(),
"String" ,
HDFCFUtil::escattr(tempfinalstr));
2322 for (
int loc=0; loc < (*j)->getCount() ; loc++) {
2335 if((*it_g)->getFieldType() == 1){
2337 at->del_attr(
"units");
2338 at->append_attr(
"units",
"String",(*it_g)->getUnits());
2340 at->del_attr(
"valid_range");
2345 if((*it_g)->getFieldType() == 2){
2346 at->del_attr(
"units");
2347 at->append_attr(
"units",
"String",(*it_g)->getUnits());
2349 at->del_attr(
"valid_range");
2355 if((*it_g)->getFieldType() == 4){
2356 at->del_attr(
"units");
2357 at->append_attr(
"units",
"String",(*it_g)->getUnits());
2361 if((*it_g)->getFieldType() == 0){
2362 at->del_attr(
"coordinates");
2366 string coordinate = (*it_g)->getCoordinate();
2367 if (coordinate !=
"")
2368 at->append_attr(
"coordinates",
"String", coordinate);
2375 HDFCFUtil::handle_otherhdf_special_attrs(f,das);
2378 HDFCFUtil::add_missing_cf_attrs(f,das);
2381 string check_scale_offset_type_key =
"H4.EnableCheckScaleOffsetType";
2382 bool turn_on_enable_check_scale_offset_key=
false;
2383 turn_on_enable_check_scale_offset_key = HDFCFUtil::check_beskeys(check_scale_offset_type_key);
2388 for(it_g = spsds.begin(); it_g != spsds.end(); it_g++){
2390 AttrTable *at = das.get_table((*it_g)->getNewName());
2392 int32 var_type = (*it_g)->getType();
2407 if (
true == HDF4RequestHandler::get_enable_check_scale_offset_type() && at !=NULL)
2412 HDFCFUtil::handle_merra_ceres_attrs_with_bes_keys(f,das,filename);
2418 HDFCFUtil::handle_vdata_attrs_with_desc_key(f,das);
2435 bool read_das_special_eos2(DAS &das,
const string& filename,int32 sdfd,int32 fileid,
bool ecs_metadata,
HDFSP::File**fpptr) {
2437 BESDEBUG(
"h4",
"Coming to read_das_special_eos2 " << endl);
2442 myfileid = Hopen(
const_cast<char *
>(filename.c_str()), DFACC_READ,0);
2456 throw InternalErr(e.
what());
2466 throw InternalErr(e.
what());
2472 read_das_special_eos2_core(das, f, filename,ecs_metadata);
2484 bool read_das_special_eos2_core(DAS &das,
HDFSP::File* f,
const string& filename,
bool ecs_metadata) {
2486 BESDEBUG(
"h4",
"Coming to read_das_special_eos2_core "<<endl);
2488 string core_metadata =
"";
2489 string archive_metadata =
"";
2490 string struct_metadata =
"";
2499 if(((*i)->getName().compare(0, 12,
"CoreMetadata" )== 0) ||
2500 ((*i)->getName().compare(0, 12,
"coremetadata" )== 0)){
2502 if(ecs_metadata ==
true) {
2506 string tempstring((*i)->getValue().begin(),(*i)->getValue().end());
2507 core_metadata.append(tempstring);
2510 else if(((*i)->getName().compare(0, 15,
"ArchiveMetadata" )== 0) ||
2511 ((*i)->getName().compare(0, 16,
"ArchivedMetadata")==0) ||
2512 ((*i)->getName().compare(0, 15,
"archivemetadata" )== 0)){
2513 if(ecs_metadata ==
true) {
2514 string tempstring((*i)->getValue().begin(),(*i)->getValue().end());
2515 archive_metadata.append(tempstring);
2518 else if(((*i)->getName().compare(0, 14,
"StructMetadata" )== 0) ||
2519 ((*i)->getName().compare(0, 14,
"structmetadata" )== 0))
2523 AttrTable *at = das.get_table(
"HDF_GLOBAL");
2525 at = das.add_table(
"HDF_GLOBAL",
new AttrTable);
2528 if((*i)->getType()==DFNT_UCHAR || (*i)->getType() == DFNT_CHAR){
2529 string tempstring2((*i)->getValue().begin(),(*i)->getValue().end());
2530 string tempfinalstr= string(tempstring2.c_str());
2539 for (
int loc=0; loc < (*i)->getCount() ; loc++) {
2552 if(ecs_metadata ==
true) {
2554 if(core_metadata.size() > 0){
2555 AttrTable *at = das.get_table(
"CoreMetadata");
2557 at = das.add_table(
"CoreMetadata",
new AttrTable);
2559 void *buf = hdfeos_string(core_metadata.c_str());
2562 if (hdfeosparse(&arg) != 0) {
2563 hdfeos_delete_buffer(buf);
2564 throw Error(
"Parse error while processing a CoreMetadata attribute.");
2568 if (arg.status() ==
false) {
2569 (*BESLog::TheLog()) <<
"Parse error while processing a CoreMetadata attribute. (2)" << endl;
2573 hdfeos_delete_buffer(buf);
2578 if(archive_metadata.size() > 0){
2579 AttrTable *at = das.get_table(
"ArchiveMetadata");
2581 at = das.add_table(
"ArchiveMetadata",
new AttrTable);
2583 void *buf = hdfeos_string(archive_metadata.c_str());
2585 if (hdfeosparse(&arg) != 0) {
2586 hdfeos_delete_buffer(buf);
2587 throw Error(
"Parse error while processing an ArchiveMetadata attribute.");
2591 if (arg.status() ==
false) {
2592 (*BESLog::TheLog())<<
"Parse error while processing an ArchiveMetadata attribute. (2)" << endl;
2596 hdfeos_delete_buffer(buf);
2602 vector<HDFSP::SDField *>::const_iterator it_g;
2603 for(it_g = spsds.begin(); it_g != spsds.end(); it_g++){
2607 if((*it_g)->getFieldType() != 0){
2609 AttrTable *at = das.get_table((*it_g)->getNewName());
2611 at = das.add_table((*it_g)->getNewName(),
new AttrTable);
2613 string tempunits = (*it_g)->getUnits();
2614 if(at->simple_find(
"units")== at->attr_end() && tempunits!=
"")
2615 at->append_attr(
"units",
"String" ,tempunits);
2616 if((*it_g)->getFieldType() == 1){
2617 if(at->simple_find(
"long_name")== at->attr_end())
2618 at->append_attr(
"long_name",
"String",
"Latitude");
2620 else if((*it_g)->getFieldType() == 2) {
2621 if(at->simple_find(
"long_name")== at->attr_end())
2622 at->append_attr(
"long_name",
"String",
"Longitude");
2626 AttrTable *at = das.get_table((*it_g)->getNewName());
2628 at = das.add_table((*it_g)->getNewName(),
new AttrTable);
2629 string tempcoors = (*it_g)->getCoordinate();
2632 at->del_attr(
"coordinates");
2633 at->append_attr(
"coordinates",
"String",tempcoors);
2639 if((*it_g)->getAttributes().size() == 0)
2642 AttrTable *at = das.get_table((*it_g)->getNewName());
2644 at = das.add_table((*it_g)->getNewName(),
new AttrTable);
2647 for(vector<HDFSP::Attribute *>::const_iterator i=(*it_g)->getAttributes().begin();i!=(*it_g)->getAttributes().end();i++) {
2650 if((*i)->getType()==DFNT_UCHAR || (*i)->getType() == DFNT_CHAR){
2651 string tempstring2((*i)->getValue().begin(),(*i)->getValue().end());
2652 string tempfinalstr= string(tempstring2.c_str());
2658 at->append_attr((*i)->getNewName(),
"String" ,((*i)->getNewName()==
"fullpath")?tempfinalstr:
HDFCFUtil::escattr(tempfinalstr));
2661 for (
int loc=0; loc < (*i)->getCount() ; loc++) {
2672 HDFCFUtil::map_eos2_objects_attrs(das,filename);
2680 void change_das_mod08_scale_offset(DAS &das,
HDFSP::File *f) {
2685 vector<HDFSP::SDField *>::const_iterator it_g;
2686 for(it_g = spsds.begin(); it_g != spsds.end(); it_g++){
2687 if((*it_g)->getFieldType() == 0){
2688 AttrTable *at = das.get_table((*it_g)->getNewName());
2690 at = das.add_table((*it_g)->getNewName(),
new AttrTable);
2693 string add_offset_type;
2696 string add_offset_value=
"0";
2697 double orig_offset_value = 0;
2698 bool add_offset_modify =
false;
2704 AttrTable::Attr_iter it = at->attr_begin();
2705 while (it!=at->attr_end())
2707 if(at->get_name(it)==
"add_offset")
2709 add_offset_value = (*at->get_attr_vector(it)->begin());
2710 orig_offset_value = atof(add_offset_value.c_str());
2711 add_offset_type = at->get_type(it);
2712 if(add_offset_value ==
"0.0" || orig_offset_value == 0)
2713 add_offset_modify =
false;
2715 add_offset_modify =
true;
2723 if(
true == add_offset_modify) {
2726 string scale_factor_type;
2729 string scale_factor_value=
"";
2730 double orig_scale_value = 1;
2732 it = at->attr_begin();
2733 while (it!=at->attr_end())
2735 if(at->get_name(it)==
"scale_factor")
2737 scale_factor_value = (*at->get_attr_vector(it)->begin());
2738 orig_scale_value = atof(scale_factor_value.c_str());
2739 scale_factor_type = at->get_type(it);
2744 if(scale_factor_value.length() !=0) {
2745 double new_offset_value = -1 * orig_scale_value*orig_offset_value;
2747 at->del_attr(
"add_offset");
2759 bool read_dds_special_1d_grid(DDS &dds,
HDFSP::File* spf,
const string& filename, int32 sdid,
bool check_cache) {
2763 BESDEBUG(
"h4",
"Coming to read_dds_special_1d_grid "<<endl);
2764 bool dds_cache =
false;
2765 size_t total_bytes_dds_cache = 0;
2768 if(
true == check_cache) {
2770 total_bytes_dds_cache = HDFCFUtil::obtain_dds_cache_size(spf);
2771 BESDEBUG(
"h4",
"Total DDS cache file size is "<< total_bytes_dds_cache<<endl);
2772 if(total_bytes_dds_cache !=0)
2777 SPType sptype = OTHERHDF;
2778 const vector<HDFSP::SDField *>& spsds = spf->
getSD()->
getFields();
2781 vector<HDFSP::SDField *>::const_iterator it_g;
2782 for(it_g = spsds.begin(); it_g != spsds.end(); it_g++){
2785 switch((*it_g)->getType()) {
2786 #define HANDLE_CASE(tid, type) \
2788 bt = new (type)((*it_g)->getNewName(),filename); \
2792 HANDLE_CASE(DFNT_CHAR,
HDFStr);
2793 #ifndef SIGNED_BYTE_TO_INT32
2794 HANDLE_CASE(DFNT_INT8,
HDFByte);
2798 HANDLE_CASE(DFNT_UINT8,
HDFByte);
2803 HANDLE_CASE(DFNT_UCHAR8,
HDFByte);
2805 throw InternalErr(__FILE__,__LINE__,
"unsupported data type.");
2812 const vector<HDFSP::Dimension*>& dims= (*it_g)->getDimensions();
2814 vector<HDFSP::Dimension*>::const_iterator it_d;
2817 if(DFNT_CHAR == (*it_g)->getType()) {
2818 if(1 == (*it_g)->getRank()) {
2823 (*it_g)->getFieldRef(),
2826 (*it_g)->getNewName(),
2832 throw InternalErr(__FILE__,__LINE__,
"Unable to allocate the HDFCFStr instance.");
2834 dds.add_var(sca_str);
2844 (*it_g)->getRank() -1 ,
2848 (*it_g)->getFieldRef(),
2851 (*it_g)->getNewName(),
2857 throw InternalErr(__FILE__,__LINE__,
"Unable to allocate the HDFCFStrField instance.");
2860 for(it_d = dims.begin(); it_d != dims.begin()+dims.size()-1; it_d++)
2861 ar->append_dim((*it_d)->getSize(), (*it_d)->getName());
2872 if((*it_g)->getFieldType()!= 4) {
2877 vector<int32>dimsizes;
2879 dimsizes.resize((*it_g)->getRank());
2880 for(
int i = 0; i <(*it_g)->getRank();i++)
2881 dimsizes[i] = (int32)((dims[i])->getSize());
2886 (*it_g)->getFieldRef(),
2891 (*it_g)->getNewName(),
2896 throw InternalErr(__FILE__,__LINE__,
"Unable to allocate the HDFSPArray_RealField instance.");
2898 for(it_d = dims.begin(); it_d != dims.end(); it_d++)
2899 ar->append_dim((*it_d)->getSize(), (*it_d)->getName());
2905 if((*it_g)->getRank()!=1){
2907 throw InternalErr(__FILE__, __LINE__,
"The rank of missing Z dimension field must be 1");
2909 int nelem = ((*it_g)->getDimensions()[0])->getSize();
2917 (*it_g)->getNewName(),
2922 throw InternalErr(__FILE__,__LINE__,
2923 "Unable to allocate the HDFSPArrayMissGeoField instance.");
2927 for(it_d = dims.begin(); it_d != dims.end(); it_d++)
2928 ar->append_dim((*it_d)->getSize(), (*it_d)->getName());
2939 if(
true == dds_cache) {
2942 string md_cache_dir;
2943 string key =
"H4.Cache.metadata.path";
2950 string base_file_name = basename(filename);
2951 string dds_filename = md_cache_dir +
"/"+base_file_name +
"_dds";
2954 FILE* dds_file =fopen(dds_filename.c_str(),
"wb");
2955 if(NULL == dds_file) {
2956 string msg =
"Cannot create the cache file. " + dds_filename + get_errno();
2957 throw InternalErr(__FILE__,__LINE__,msg);
2959 int fd = fileno(dds_file);
2960 struct flock *l= lock(F_WRLCK);
2961 if (fcntl(fd, F_SETLKW, l) == -1) {
2963 string msg =
"Cannot hold the write lock for dds cached file "+ dds_filename;
2964 throw InternalErr (__FILE__, __LINE__,msg);
2968 HDFCFUtil::write_sp_sds_dds_cache(spf,dds_file,total_bytes_dds_cache,dds_filename);
2971 if (fcntl(fd, F_SETLK, lock(F_UNLCK)) == -1) {
2973 string msg =
"Cannot release the write lock for dds cached file "+ dds_filename;
2974 throw InternalErr (__FILE__, __LINE__,msg);
2978 throw InternalErr(__FILE__,__LINE__,
"Fail to generate a dds cache file.");
2980 if (fcntl(fd, F_SETLK, lock(F_UNLCK)) == -1) {
2982 string msg =
"Cannot release the write lock for dds cached file "+ dds_filename;
2983 throw InternalErr (__FILE__, __LINE__,msg);
2990 throw InternalErr (__FILE__, __LINE__,
2991 "DDS/DAS metadata cache path cannot be found when 'H4.EnableMetaDataCacheFile' key is set to be true.");
3000 void read_dds_spfields(DDS &dds,
const string& filename,
const int sdfd,
HDFSP::SDField *spsds, SPType sptype) {
3002 BESDEBUG(
"h4",
"Coming to read_dds_spfields "<<endl);
3005 if(OTHERHDF == sptype && (
true == spsds->
IsDimNoScale()))
3011 #define HANDLE_CASE(tid, type) \
3013 bt = new (type)(spsds->getNewName(),filename); \
3017 HANDLE_CASE(DFNT_CHAR,
HDFStr);
3018 #ifndef SIGNED_BYTE_TO_INT32
3019 HANDLE_CASE(DFNT_INT8,
HDFByte);
3025 HANDLE_CASE(DFNT_UINT8,
HDFByte);
3030 HANDLE_CASE(DFNT_UCHAR,
HDFByte);
3032 throw InternalErr(__FILE__,__LINE__,
"unsupported data type.");
3035 int fieldtype = spsds->getFieldType();
3041 vector<HDFSP::Dimension*>::const_iterator it_d;
3043 if(DFNT_CHAR == spsds->
getType()) {
3053 spsds->getFieldRef(),
3062 throw InternalErr(__FILE__,__LINE__,
"Unable to allocate the HDFCFStr instance.");
3064 dds.add_var(sca_str);
3077 spsds->getFieldRef(),
3086 throw InternalErr(__FILE__,__LINE__,
"Unable to allocate the HDFCFStrField instance.");
3089 for(it_d = dims.begin(); it_d != dims.begin()+dims.size()-1; it_d++)
3090 ar->append_dim((*it_d)->getSize(), (*it_d)->getName());
3099 else if(fieldtype == 0 || fieldtype == 3 ) {
3104 vector<int32>dimsizes;
3105 dimsizes.resize(spsds->
getRank());
3106 for(
int i = 0; i <spsds->
getRank();i++)
3107 dimsizes[i] = (int32)((dims[i])->getSize());
3113 spsds->getFieldRef(),
3123 throw InternalErr(__FILE__,__LINE__,
"Unable to allocate the HDFSPArray_RealField instance.");
3126 for(it_d = dims.begin(); it_d != dims.end(); it_d++)
3127 ar->append_dim((*it_d)->getSize(), (*it_d)->getName());
3134 else if(fieldtype == 1 || fieldtype == 2) {
3136 if(sptype == MODISARNSS || sptype == TRMML2_V7) {
3142 vector<int32>dimsizes;
3144 dimsizes.resize(spsds->
getRank());
3145 for(
int i = 0; i <spsds->
getRank();i++)
3146 dimsizes[i] = (dims[i])->getSize();
3152 spsds->getFieldRef(),
3162 throw InternalErr(__FILE__,__LINE__,
3163 "Unable to allocate the HDFSPArray_RealField instance.");
3167 for(it_d = dims.begin(); it_d != dims.end(); it_d++)
3168 ar->append_dim((*it_d)->getSize(), (*it_d)->getName());
3183 spsds->getFieldRef(),
3193 throw InternalErr(__FILE__,__LINE__,
3194 "Unable to allocate the HDFSPArray_RealField instance.");
3197 for(it_d = dims.begin(); it_d != dims.end(); it_d++)
3198 ar->append_dim((*it_d)->getSize(), (*it_d)->getName());
3206 else if(fieldtype == 4) {
3209 throw InternalErr(__FILE__, __LINE__,
"The rank of missing Z dimension field must be 1");
3224 throw InternalErr(__FILE__,__LINE__,
3225 "Unable to allocate the HDFSPArrayMissGeoField instance.");
3229 for(it_d = dims.begin(); it_d != dims.end(); it_d++)
3230 ar->append_dim((*it_d)->getSize(), (*it_d)->getName());
3237 else if(fieldtype == 6) {
3241 throw InternalErr(__FILE__, __LINE__,
"The rank of added coordinate variable must be 1");
3257 throw InternalErr(__FILE__,__LINE__,
3258 "Unable to allocate the HDFSPArrayAddCVField instance.");
3262 for(it_d = dims.begin(); it_d != dims.end(); it_d++)
3263 ar->append_dim((*it_d)->getSize(), (*it_d)->getName());
3270 throw InternalErr(__FILE__, __LINE__,
"The field type should be one of 0,1,2,3,4 or 6.");
3278 void read_dds_spvdfields(DDS &dds,
const string & filename,
const int fileid,int32 objref,int32 numrec,
HDFSP::VDField *spvd) {
3280 BESDEBUG(
"h4",
"Coming to read_dds_spvdfields "<<endl);
3285 #define HANDLE_CASE(tid, type) \
3287 bt = new (type)(spvd->getNewName(),filename); \
3291 HANDLE_CASE(DFNT_CHAR8,
HDFStr);
3292 #ifndef SIGNED_BYTE_TO_INT32
3293 HANDLE_CASE(DFNT_INT8,
HDFByte);
3297 HANDLE_CASE(DFNT_UINT8,
HDFByte);
3302 HANDLE_CASE(DFNT_UCHAR8,
HDFByte);
3306 throw InternalErr(__FILE__,__LINE__,
"unsupported data type.");
3313 if(DFNT_CHAR == spvd->
getType()) {
3334 throw InternalErr(__FILE__,__LINE__,
"Unable to allocate the HDFCFStr instance.");
3336 dds.add_var(sca_str);
3360 throw InternalErr(__FILE__,__LINE__,
"Unable to allocate the HDFCFStrField instance.");
3363 string dimname0 =
"VDFDim0_"+spvd->
getNewName();
3364 ar->append_dim(numrec, dimname0);
3389 string dimname1 =
"VDFDim0_"+spvd->
getNewName();
3391 string dimname2 =
"VDFDim1_"+spvd->
getNewName();
3393 ar->append_dim(numrec,dimname1);
3397 ar->append_dim(numrec,dimname1);
3409 int check_special_eosfile(
const string & filename,
string& grid_name,int32 sdfd,int32 fileid ) {
3413 int32 n_sd_attrs = 0;
3414 bool is_eos =
false;
3418 if (SDfileinfo (sdfd, &n_sds, &n_sd_attrs) == FAIL){
3419 throw InternalErr (__FILE__,__LINE__,
"SDfileinfo failed ");
3422 char attr_name[H4_MAX_NC_NAME];
3423 int32 attr_type = -1;
3424 int32 attr_count = -1;
3425 char structmdname[] =
"StructMetadata.0";
3428 for (
int attr_index = 0; attr_index < n_sd_attrs;attr_index++) {
3429 if(SDattrinfo(sdfd,attr_index,attr_name,&attr_type,&attr_count) == FAIL) {
3430 throw InternalErr (__FILE__,__LINE__,
"SDattrinfo failed ");
3433 if(strcmp(attr_name,structmdname)==0) {
3439 if(
true == is_eos) {
3443 int32 dim_sizes[H4_MAX_VAR_DIMS];
3444 int32 sds_dtype = 0;
3445 int32 n_sds_attrs = 0;
3446 char sds_name[H4_MAX_NC_NAME];
3447 char xdim_name[] =
"XDim";
3448 char ydim_name[] =
"YDim";
3450 string temp_grid_name1;
3451 string temp_grid_name2;
3452 bool xdim_is_cv_flag =
false;
3453 bool ydim_is_cv_flag =
false;
3457 for (sds_index = 0; sds_index < (int)n_sds; sds_index++) {
3459 sds_id = SDselect (sdfd, sds_index);
3460 if (sds_id == FAIL) {
3461 throw InternalErr (__FILE__,__LINE__,
"SDselect failed ");
3465 int status = SDgetinfo (sds_id, sds_name, &sds_rank, dim_sizes,
3466 &sds_dtype, &n_sds_attrs);
3467 if (status == FAIL) {
3468 SDendaccess(sds_id);
3469 throw InternalErr (__FILE__,__LINE__,
"SDgetinfo failed ");
3475 if(strcmp(sds_name,xdim_name) == 0) {
3476 int32 sds_dimid = SDgetdimid(sds_id,0);
3477 if(sds_dimid == FAIL) {
3478 SDendaccess(sds_id);
3479 throw InternalErr (__FILE__,__LINE__,
"SDgetinfo failed ");
3481 char dim_name[H4_MAX_NC_NAME];
3484 int32 num_dim_attrs = 0;
3485 if(SDdiminfo(sds_dimid,dim_name,&dim_size,&dim_type,&num_dim_attrs) == FAIL) {
3486 SDendaccess(sds_id);
3487 throw InternalErr(__FILE__,__LINE__,
"SDdiminfo failed ");
3492 string tempdimname(dim_name);
3493 if(tempdimname.size() >=5) {
3494 if(tempdimname.compare(0,5,
"XDim:") == 0) {
3497 temp_grid_name1 = tempdimname.substr(5);
3498 xdim_is_cv_flag =
true;
3502 else if(
"XDim" == tempdimname)
3503 xdim_is_cv_flag =
true;
3508 if(strcmp(sds_name,ydim_name) == 0) {
3510 int32 sds_dimid = SDgetdimid(sds_id,0);
3511 if(sds_dimid == FAIL) {
3512 SDendaccess (sds_id);
3513 throw InternalErr (__FILE__,__LINE__,
"SDgetinfo failed ");
3515 char dim_name[H4_MAX_NC_NAME];
3518 int32 num_dim_attrs = 0;
3519 if(SDdiminfo(sds_dimid,dim_name,&dim_size,&dim_type,&num_dim_attrs) == FAIL) {
3520 SDendaccess(sds_id);
3521 throw InternalErr(__FILE__,__LINE__,
"SDdiminfo failed ");
3526 string tempdimname(dim_name);
3527 if(tempdimname.size() >=5) {
3528 if(tempdimname.compare(0,5,
"YDim:") == 0) {
3530 temp_grid_name2 = tempdimname.substr(5);
3531 ydim_is_cv_flag =
true;
3534 else if (
"YDim" == tempdimname)
3535 ydim_is_cv_flag =
true;
3540 SDendaccess(sds_id);
3541 if((
true == xdim_is_cv_flag) && (
true == ydim_is_cv_flag ))
3547 if ((temp_grid_name1 == temp_grid_name2) && (
true == xdim_is_cv_flag) && (
true == ydim_is_cv_flag)) {
3548 grid_name = temp_grid_name1;
3556 else if((basename(filename).size() >8) && (basename(filename).compare(0,4,
"AIRS") == 0)
3557 && ((basename(filename).find(
".L3.")!=string::npos) || (basename(filename).find(
".L2.")!=string::npos))){
3559 bool has_dimscale =
false;
3562 for (sds_index = 0; sds_index < n_sds; sds_index++) {
3564 sds_id = SDselect (sdfd, sds_index);
3565 if (sds_id == FAIL) {
3566 throw InternalErr (__FILE__,__LINE__,
"SDselect failed ");
3570 int status = SDgetinfo (sds_id, sds_name, &sds_rank, dim_sizes,
3571 &sds_dtype, &n_sds_attrs);
3572 if (status == FAIL) {
3573 SDendaccess(sds_id);
3574 throw InternalErr (__FILE__,__LINE__,
"SDgetinfo failed ");
3577 for (
int dim_index = 0; dim_index<sds_rank; dim_index++) {
3579 int32 sds_dimid = SDgetdimid(sds_id,dim_index);
3580 if(sds_dimid == FAIL) {
3581 SDendaccess(sds_id);
3582 throw InternalErr (__FILE__,__LINE__,
"SDgetinfo failed ");
3585 char dim_name[H4_MAX_NC_NAME];
3588 int32 num_dim_attrs = 0;
3589 if(SDdiminfo(sds_dimid,dim_name,&dim_size,&dim_type,&num_dim_attrs) == FAIL) {
3590 SDendaccess(sds_id);
3591 throw InternalErr(__FILE__,__LINE__,
"SDdiminfo failed ");
3595 has_dimscale =
true;
3600 SDendaccess(sds_id);
3601 if(
true == has_dimscale)
3606 if (
true == has_dimscale)
3617 bool has_dimscale =
true;
3618 bool is_grid =
false;
3621 for (sds_index = 0; sds_index < n_sds; sds_index++) {
3623 sds_id = SDselect (sdid, sds_index);
3624 if (sds_id == FAIL) {
3626 throw InternalErr (__FILE__,__LINE__,
"SDselect failed ");
3630 int status = SDgetinfo (sds_id, sds_name, &sds_rank, dim_sizes,
3631 &sds_dtype, &n_sds_attrs);
3632 if (status == FAIL) {
3633 SDendaccess(sds_id);
3635 throw InternalErr (__FILE__,__LINE__,
"SDgetinfo failed ");
3639 for (
int dim_index = 0; dim_index<sds_rank; dim_index++) {
3641 int32 sds_dimid = SDgetdimid(sds_id,dim_index);
3642 if(sds_dimid == FAIL) {
3643 SDendaccess(sds_id);
3645 throw InternalErr (__FILE__,__LINE__,
"SDgetinfo failed ");
3647 char dim_name[H4_MAX_NC_NAME];
3650 int32 num_dim_attrs = 0;
3651 if(SDdiminfo(sds_dimid,dim_name,&dim_size,&dim_type,&num_dim_attrs) == FAIL) {
3652 SDendaccess(sds_id);
3654 throw InternalErr(__FILE__,__LINE__,
"SDdiminfo failed ");
3658 has_dimscale =
false;
3662 SDendaccess(sds_id);
3664 if (
true == has_dimscale)
3675 void read_das_sds(DAS & das,
const string & filename,int32 sdfd,
bool ecs_metadata,
HDFSP::File**h4fileptr) {
3680 spf->Handle_AIRS_L23();
3681 read_das_special_eos2_core(das,spf,filename,ecs_metadata);
3687 throw InternalErr(e.
what());
3696 void read_dds_sds(DDS &dds,
const string & filename,int32 sdfd,
HDFSP::File*h4file,
bool dds_setcache) {
3699 dds.set_dataset_name(basename(filename));
3700 read_dds_special_1d_grid(dds,h4file,filename,sdfd,dds_setcache);
3705 void read_dds(DDS & dds,
const string & filename)
3709 dds.set_dataset_name(basename(filename));
3710 build_descriptions(dds, das, filename);
3712 if (!dds.check_semantics()) {
3718 void read_das(DAS & das,
const string & filename)
3722 dds.set_dataset_name(basename(filename));
3724 build_descriptions(dds, das, filename);
3726 if (!dds.check_semantics()) {
3734 static void build_descriptions(DDS & dds, DAS & das,
3735 const string & filename)
3743 SDS_descriptions(sdsmap, das, filename);
3746 FileAnnot_descriptions(das, filename);
3749 Vdata_descriptions(vdatamap, das, filename);
3752 GR_descriptions(grmap, das, filename);
3755 Vgroup_descriptions(dds, das, filename, sdsmap, vdatamap, grmap);
3770 :
public binary_function < hdf_genvec &, hdf_attr, hdf_genvec & > {
3774 accum_attr(
const string & named):d_named(named) {
3780 BESDEBUG(
"h4",
"attr.name: " << attr.name << endl);
3781 if (attr.name.find(d_named) != string::npos) {
3784 stuff.assign(attr.values.data(), attr.values.size());
3785 cerr <<
"Attribute chunk: " << attr.name << endl;
3786 cerr << stuff << endl;
3788 accum.append(attr.values.number_type(), attr.values.data(),
3789 attr.values.size());
3798 struct is_named:
public unary_function < hdf_attr, bool > {
3801 is_named(
const string & named):d_named(named) {
3804 bool operator() (
const hdf_attr & attr) {
3805 return (attr.name.find(d_named) != string::npos);
3810 merge_split_eos_attributes(vector < hdf_attr > &attr_vec,
3811 const string & attr_name)
3814 if (count_if(attr_vec.begin(), attr_vec.end(), is_named(attr_name)) > 1) {
3818 attributes = accumulate(attr_vec.begin(), attr_vec.end(),
3819 attributes, accum_attr(attr_name));
3827 DBG(vector < string > s_m;
3828 attributes.print(s_m);
3829 cerr <<
"Accum struct MD: (" << s_m.size() <<
") "
3833 attr_vec.erase(remove_if(attr_vec.begin(), attr_vec.end(),
3834 is_named(attr_name)), attr_vec.end());
3838 merged_attr.name = attr_name;
3839 merged_attr.values = attributes;
3842 attr_vec.push_back(merged_attr);
3847 static void SDS_descriptions(sds_map & map, DAS & das,
3848 const string & filename)
3852 sdsin.setmeta(
true);
3856 vector < hdf_attr > fileattrs;
3861 while (!sdsin.eos()) {
3864 sdi.in_vgroup =
false;
3865 map[sdi.sds.ref] = sdi;
3872 merge_split_eos_attributes(fileattrs,
"StructMetadata");
3873 merge_split_eos_attributes(fileattrs,
"CoreMetadata");
3874 merge_split_eos_attributes(fileattrs,
"ProductMetadata");
3875 merge_split_eos_attributes(fileattrs,
"ArchiveMetadata");
3876 merge_split_eos_attributes(fileattrs,
"coremetadata");
3877 merge_split_eos_attributes(fileattrs,
"productmetadata");
3880 AddHDFAttr(das,
string(
"HDF_GLOBAL"), fileattrs);
3882 vector < hdf_attr > dattrs;
3886 for (SDSI s = map.begin(); s != map.end(); ++s) {
3887 const hdf_sds *sds = &s->second.sds;
3888 AddHDFAttr(das, sds->name, sds->attrs);
3889 for (
int k = 0; k < (int) sds->dims.size(); ++k) {
3890 dattrs = Dims2Attrs(sds->dims[k]);
3891 AddHDFAttr(das, sds->name +
"_dim_" + num2string(k), dattrs);
3900 static void Vdata_descriptions(vd_map & map, DAS & das,
3901 const string & filename)
3907 while (!vdin.eos()) {
3910 vdi.in_vgroup =
false;
3911 map[vdi.vdata.ref] = vdi;
3916 vector < hdf_attr > dattrs;
3917 for (VDI s = map.begin(); s != map.end(); ++s) {
3919 AddHDFAttr(das, vd->name, vd->attrs);
3926 static void Vgroup_descriptions(DDS & dds, DAS & das,
3927 const string & filename, sds_map & sdmap,
3928 vd_map & vdmap, gr_map & grmap)
3935 while (!vgin.eos()) {
3938 vgi.toplevel =
true;
3939 vgmap[vgi.vgroup.ref] = vgi;
3943 for (VGI v = vgmap.begin(); v != vgmap.end(); ++v) {
3947 AddHDFAttr(das, vg->name, vg->attrs);
3950 for (uint32 i = 0; i < vg->tags.size(); i++) {
3951 int32 tag = vg->tags[i];
3952 int32 ref = vg->refs[i];
3956 if (grmap.find(ref) != grmap.end())
3957 grmap[ref].in_vgroup =
true;
3959 vgmap[ref].toplevel =
false;
3962 vdmap[ref].in_vgroup =
true;
3965 sdmap[ref].in_vgroup =
true;
3968 (*BESLog::TheLog()) <<
"unknown tag: " << tag <<
" ref: " << ref << endl;
3979 for (VGI v = vgmap.begin(); v != vgmap.end(); ++v) {
3980 if (!v->second.toplevel)
3982 pbt = NewStructureFromVgroup(v->second.vgroup,
3983 vgmap, sdmap, vdmap,
3993 for (SDSI s = sdmap.begin(); s != sdmap.end(); ++s) {
3994 if (s->second.in_vgroup)
3996 if (s->second.sds.has_scale())
3997 pbt = NewGridFromSDS(s->second.sds, filename);
3999 pbt = NewArrayFromSDS(s->second.sds, filename);
4007 for (VDI v = vdmap.begin(); v != vdmap.end(); ++v) {
4008 if (v->second.in_vgroup)
4010 pbt = NewSequenceFromVdata(v->second.vdata, filename);
4017 for (GRI g = grmap.begin(); g != grmap.end(); ++g) {
4018 if (g->second.in_vgroup)
4020 pbt = NewArrayFromGR(g->second.gri, filename);
4028 static void GR_descriptions(gr_map & map, DAS & das,
4029 const string & filename)
4036 vector < hdf_attr > fileattrs;
4041 while (!grin.eos()) {
4044 gri.in_vgroup =
false;
4045 map[gri.gri.ref] = gri;
4051 AddHDFAttr(das,
string(
"HDF_GLOBAL"), fileattrs);
4054 vector < hdf_attr > pattrs;
4055 for (GRI g = map.begin(); g != map.end(); ++g) {
4056 const hdf_gri *gri = &g->second.gri;
4058 AddHDFAttr(das, gri->name, gri->attrs);
4061 pattrs = Pals2Attrs(gri->palettes);
4062 AddHDFAttr(das, gri->name, pattrs);
4070 static void FileAnnot_descriptions(DAS & das,
const string & filename)
4074 vector < string > fileannots;
4076 annotin >> fileannots;
4077 AddHDFAttr(das,
string(
"HDF_GLOBAL"), fileannots);
4084 void AddHDFAttr(DAS & das,
const string & varname,
4085 const vector < hdf_attr > &hav)
4087 if (hav.size() == 0)
4091 string tempname = varname;
4092 AttrTable *atp = das.get_table(tempname);
4094 atp =
new AttrTable;
4095 atp = das.add_table(tempname, atp);
4098 vector < string > attv;
4100 for (
int i = 0; i < (int) hav.size(); ++i) {
4102 attrtype = DAPTypeName(hav[i].values.number_type());
4104 attv = vector < string > ();
4105 hav[i].values.print(attv);
4108 for (
int j = 0; j < (int) attv.size(); ++j) {
4110 string container_name = hav[i].name;
4111 if (container_name.find(
"StructMetadata") == 0
4112 || container_name.find(
"CoreMetadata") == 0
4113 || container_name.find(
"ProductMetadata") == 0
4114 || container_name.find(
"ArchiveMetadata") == 0
4115 || container_name.find(
"coremetadata") == 0
4116 || container_name.find(
"productmetadata") == 0) {
4117 string::size_type dotzero = container_name.find(
'.');
4118 if (dotzero != container_name.npos)
4119 container_name.erase(dotzero);
4122 AttrTable *at = das.get_table(container_name);
4124 at = das.add_table(container_name,
new AttrTable);
4127 void *buf = hdfeos_string(attv[j].c_str());
4145 if (hdfeosparse(&arg) != 0){
4146 hdfeos_delete_buffer(buf);
4147 throw Error(
"HDF-EOS parse error while processing a " + container_name +
" HDFEOS attribute.");
4151 if (arg.status() ==
false) {
4152 (*BESLog::TheLog())<<
"HDF-EOS parse error while processing a "
4153 << container_name <<
" HDFEOS attribute. (2)" << endl;
4157 hdfeos_delete_buffer(buf);
4160 if (attrtype ==
"String")
4161 #ifdef ATTR_STRING_QUOTE_FIX
4162 attv[j] = escattr(attv[j]);
4164 attv[j] =
"\"" + escattr(attv[j]) +
"\"";
4167 if (atp->append_attr(hav[i].name, attrtype, attv[j]) == 0)
4178 void AddHDFAttr(DAS & das,
const string & varname,
4179 const vector < string > &anv)
4181 if (anv.size() == 0)
4186 AttrTable *atp = das.get_table(varname);
4188 atp =
new AttrTable;
4189 atp = das.add_table(varname, atp);
4193 for (
int i = 0; i < (int) anv.size(); ++i) {
4194 #ifdef ATTR_STRING_QUOTE_FIX
4195 an = escattr(anv[i]);
4197 an =
"\"" + escattr(anv[i]) +
"\"";
4199 if (atp->append_attr(
string(
"HDF_ANNOT"),
"String", an) == 0)
4209 static vector < hdf_attr > Pals2Attrs(
const vector < hdf_palette > palv)
4211 vector < hdf_attr > pattrs;
4213 if (palv.size() != 0) {
4218 for (
int i = 0; i < (int) palv.size(); ++i) {
4219 palname =
"hdf_palette_" + num2string(i);
4220 pattr.name = palname;
4221 pattr.values = palv[i].table;
4222 pattrs.push_back(pattr);
4223 pattr.name = palname +
"_ncomps";
4226 int32 *
>(&palv[i].ncomp), 1);
4227 pattrs.push_back(pattr);
4228 if (palv[i].name.length() != 0) {
4229 pattr.name = palname +
"_name";
4232 char *
>(palv[i].name.c_str()),
4233 palv[i].name.length());
4234 pattrs.push_back(pattr);
4243 static vector < hdf_attr > Dims2Attrs(
const hdf_dim dim)
4245 vector < hdf_attr > dattrs;
4247 if (dim.name.length() != 0) {
4248 dattr.name =
"name";
4250 hdf_genvec(DFNT_CHAR,
const_cast < char *
>(dim.name.c_str()),
4252 dattrs.push_back(dattr);
4254 if (dim.label.length() != 0) {
4255 dattr.name =
"long_name";
4257 hdf_genvec(DFNT_CHAR,
const_cast < char *
>(dim.label.c_str()),
4258 dim.label.length());
4259 dattrs.push_back(dattr);
4261 if (dim.unit.length() != 0) {
4262 dattr.name =
"units";
4264 hdf_genvec(DFNT_CHAR,
const_cast < char *
>(dim.unit.c_str()),
4266 dattrs.push_back(dattr);
4268 if (dim.format.length() != 0) {
4269 dattr.name =
"format";
4271 hdf_genvec(DFNT_CHAR,
const_cast < char *
>(dim.format.c_str()),
4272 dim.format.length());
4273 dattrs.push_back(dattr);