50 #include "config_hdf.h"
68 #ifdef HAVE_SYS_PARAM_H
69 #include <sys/param.h>
73 #include <sys/types.h>
83 #include <libdap/DDS.h>
84 #include <libdap/DAS.h>
85 #include <libdap/escaping.h>
86 #include <libdap/parser.h>
87 #include <libdap/InternalErr.h>
88 #include <libdap/debug.h>
93 #include "HDF4RequestHandler.h"
100 #include "HDFSequence.h"
101 #include "HDFTypeFactory.h"
103 #include "dodsutil.h"
104 #include "hdf-dods.h"
105 #include "hdf-maps.h"
108 #define SIGNED_BYTE_TO_INT32 1
112 #include "HDFInt16.h"
113 #include "HDFUInt16.h"
114 #include "HDFInt32.h"
115 #include "HDFUInt32.h"
116 #include "HDFFloat32.h"
117 #include "HDFFloat64.h"
125 #include "HDFSPArray_RealField.h"
126 #include "HDFSPArrayGeoField.h"
127 #include "HDFSPArrayMissField.h"
128 #include "HDFSPArrayAddCVField.h"
129 #include "HDFSPArray_VDField.h"
130 #include "HDFCFStrField.h"
132 #include "HDFCFUtil.h"
135 #ifdef USE_HDFEOS2_LIB
137 #include "HDFEOS2Array_RealField.h"
138 #include "HDFEOS2ArrayGridGeoField.h"
139 #include "HDFEOS2ArraySwathGeoField.h"
140 #include "HDFEOS2ArrayMissField.h"
141 #include "HDFEOS2ArraySwathDimMapField.h"
142 #include "HDFEOS2ArraySwathGeoMultiDimMapField.h"
143 #include "HDFEOS2ArraySwathGeoDimMapExtraField.h"
146 #include "HDFEOS2HandleType.h"
154 #define ATTR_STRING_QUOTE_FIX
156 template <
class T >
string num2string(T n)
164 void hdfeos_switch_to_buffer(
void *new_buffer);
165 void hdfeos_delete_buffer(
void * buffer);
166 void *hdfeos_string(
const char *yy_str);
168 struct yy_buffer_state;
169 yy_buffer_state *hdfeos_scan_string(
const char *str);
170 extern int hdfeosparse(libdap::parser_arg *arg);
173 void AddHDFAttr(DAS & das,
const string & varname,
174 const vector < hdf_attr > &hav);
175 void AddHDFAttr(DAS & das,
const string & varname,
176 const vector < string > &anv);
178 static void build_descriptions(DDS & dds, DAS & das,
179 const string & filename);
180 static void SDS_descriptions(sds_map & map, DAS & das,
181 const string & filename);
182 static void Vdata_descriptions(vd_map & map, DAS & das,
183 const string & filename);
184 static void Vgroup_descriptions(DDS & dds, DAS & das,
185 const string & filename, sds_map & sdmap,
186 vd_map & vdmap, gr_map & grmap);
187 static void GR_descriptions(gr_map & map, DAS & das,
188 const string & filename);
189 static void FileAnnot_descriptions(DAS & das,
const string & filename);
190 static vector < hdf_attr > Pals2Attrs(
const vector < hdf_palette > palv);
191 static vector < hdf_attr > Dims2Attrs(
const hdf_dim dim);
193 void read_das(DAS & das,
const string & filename);
194 void read_dds(DDS & dds,
const string & filename);
198 bool read_dds_hdfsp(DDS & dds,
const string & filename,int32 sdfd, int32 fileid,
HDFSP::File*h4file);
199 bool read_das_hdfsp(DAS & das,
const string & filename,int32 sdfd, int32 fileid,
HDFSP::File**h4filepptr);
202 bool read_dds_hdfhybrid(DDS & dds,
const string & filename,int32 sdfd, int32 fileid,
HDFSP::File*h4file);
203 bool read_das_hdfhybrid(DAS & das,
const string & filename,int32 sdfd, int32 fileid,
HDFSP::File**h4filepptr);
207 bool read_dds_special_1d_grid(DDS &dds,
HDFSP::File *spf,
const string & filename,int32 sdfd,
bool can_cache);
208 bool read_das_special_eos2(DAS &das,
const string & filename,int32 sdid, int32 fileid,
bool ecs_metadata,
HDFSP::File**h4filepptr);
209 bool read_das_special_eos2_core(DAS &das,
HDFSP::File *spf,
const string & filename,
bool ecs_metadata);
211 void read_das_sds(DAS & das,
const string & filename,int32 sdfd,
bool ecs_metadata,
HDFSP::File**h4fileptr);
212 void read_dds_sds(DDS &dds,
const string & filename,int32 sdfd,
HDFSP::File*h4file,
bool dds_set_cache);
214 void change_das_mod08_scale_offset(DAS & das,
HDFSP::File *spf);
217 void read_dds_spfields(DDS &dds,
const string& filename,
const int sdfd,
HDFSP::SDField *spsds, SPType sptype);
220 void read_dds_spvdfields(DDS &dds,
const string& filename,
const int fileid,int32 vdref, int32 numrec,
HDFSP::VDField *spvd);
223 int check_special_eosfile(
const string&filename,
string&grid_name,int32 sdfd,int32 fileid);
227 #ifdef USE_HDFEOS2_LIB
230 void parse_ecs_metadata(DAS &das,
const string & metaname,
const string &metadata);
248 int read_dds_hdfeos2(DDS & dds,
const string & filename,int32 sdfd,int32 fileid, int32 gridfd, int32 swathfd,
HDFSP::File*h4file,HDFEOS2::File*eosfile);
251 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);
255 void read_dds_hdfeos2_grid_swath(DDS &dds,
const string&filename, HDFEOS2::Dataset *dataset,
int grid_or_swath,
bool ownll, SOType sotype,
bool multi_dmap,
256 int32 sdfd, int32 , int32 gridfd,int32 swathfd)
259 BESDEBUG(
"h4",
"Coming to read_dds_hdfeos2_grid_swath "<<endl);
261 if(grid_or_swath < 0 || grid_or_swath > 1)
262 throw InternalErr(__FILE__, __LINE__,
"The current type should be either grid or swath");
267 vector<struct dimmap_entry> dimmaps;
270 string modis_geofilename=
"";
271 bool geofile_has_dimmap =
false;
275 if(grid_or_swath == 1)
276 HDFCFUtil::obtain_dimmap_info(filename,dataset,dimmaps,modis_geofilename,geofile_has_dimmap);
281 const vector<HDFEOS2::Field*>& fields = (dataset)->getDataFields();
282 vector<HDFEOS2::Field*> all_fields = fields;
283 vector<HDFEOS2::Field*>::const_iterator it_f;
285 if(1 == grid_or_swath) {
286 HDFEOS2::SwathDataset *sw =
static_cast<HDFEOS2::SwathDataset *
>(dataset);
287 const vector<HDFEOS2::Field*>geofields = sw->getGeoFields();
288 for (it_f = geofields.begin(); it_f != geofields.end(); it_f++)
289 all_fields.push_back(*it_f);
294 for(it_f = all_fields.begin(); it_f != all_fields.end(); it_f++)
296 BESDEBUG(
"h4",
"New field Name " <<(*it_f)->getNewName()<<endl);
301 int fieldtype = (*it_f)->getFieldType();
305 bool changedtype =
false;
306 for (vector<string>::const_iterator i = ctype_field_namelist.begin(); i != ctype_field_namelist.end(); ++i){
307 if ((*i) == (*it_f)->getNewName()){
313 switch((*it_f)->getType())
316 #define HANDLE_CASE2(tid, type) \
318 if(true == changedtype && fieldtype==0) \
319 bt = new (HDFFloat32) ((*it_f)->getNewName(), (dataset)->getName()); \
321 bt = new (type)((*it_f)->getNewName(), (dataset)->getName()); \
324 #define HANDLE_CASE(tid, type)\
326 bt = new (type)((*it_f)->getNewName(), (dataset)->getName()); \
330 HANDLE_CASE(DFNT_CHAR8,
HDFStr);
331 #ifndef SIGNED_BYTE_TO_INT32
332 HANDLE_CASE2(DFNT_INT8,
HDFByte);
336 HANDLE_CASE2(DFNT_UINT8,
HDFByte);
341 HANDLE_CASE2(DFNT_UCHAR8,
HDFByte);
343 throw InternalErr(__FILE__,__LINE__,
"unsupported data type.");
350 const vector<HDFEOS2::Dimension*>& dims= (*it_f)->getCorrectedDimensions();
351 vector<HDFEOS2::Dimension*>::const_iterator it_d;
354 if(DFNT_CHAR == (*it_f)->getType()) {
356 if((*it_f)->getRank() >1) {
358 HDFEOS2CFStrField * ar = NULL;
362 ar =
new HDFEOS2CFStrField(
363 (*it_f)->getRank() -1,
364 (grid_or_swath ==0)?gridfd:swathfd,
366 (dataset)->getName(),
369 (*it_f)->getNewName(),
374 throw InternalErr(__FILE__,__LINE__,
"Unable to allocate the HDFCFStr instance.");
376 for(it_d = dims.begin(); it_d != dims.begin()+dims.size()-1; it_d++){
377 ar->append_dim((*it_d)->getSize(), (*it_d)->getName());
388 HDFEOS2CFStr * sca_str = NULL;
391 sca_str =
new HDFEOS2CFStr(
392 (grid_or_swath ==0)?gridfd:swathfd,
394 (dataset)->getName(),
396 (*it_f)->getNewName(),
401 throw InternalErr(__FILE__,__LINE__,
"Unable to allocate the HDFCFStr instance.");
403 dds.add_var(sca_str);
411 else if(fieldtype == 0 || fieldtype == 3 || fieldtype == 5) {
414 if(grid_or_swath==0){
415 HDFEOS2Array_RealField *ar = NULL;
416 ar =
new HDFEOS2Array_RealField(
418 filename,
false,sdfd,gridfd,
419 (dataset)->getName(),
"", (*it_f)->getName(),
421 (*it_f)->getNewName(), bt);
422 for(it_d = dims.begin(); it_d != dims.end(); it_d++)
423 ar->append_dim((*it_d)->getSize(), (*it_d)->getName());
429 else if(grid_or_swath==1){
431 string tempfieldname = (*it_f)->getName();
434 if((*it_f)->UseDimMap() &&
false == multi_dmap) {
437 if (!modis_geofilename.empty()) {
440 if (
true == HDFCFUtil::is_modis_dimmap_nonll_field(tempfieldname)) {
442 if(
false == geofile_has_dimmap) {
449 HDFEOS2Array_RealField *ar = NULL;
450 ar =
new HDFEOS2Array_RealField(
457 "MODIS_Swath_Type_GEO",
460 (*it_f)->getNewName(),
463 for(it_d = dims.begin(); it_d != dims.end(); it_d++)
464 ar->append_dim((*it_d)->getSize(), (*it_d)->getName());
471 HDFEOS2ArraySwathDimMapField * ar = NULL;
478 ar =
new HDFEOS2ArraySwathDimMapField(
485 "MODIS_Swath_Type_GEO",
489 (*it_f)->getNewName(),
491 for(it_d = dims.begin(); it_d != dims.end(); it_d++)
492 ar->append_dim((*it_d)->getSize(), (*it_d)->getName());
500 HDFEOS2ArraySwathDimMapField * ar = NULL;
506 ar =
new HDFEOS2ArraySwathDimMapField(
513 (dataset)->getName(),
517 (*it_f)->getNewName(),
519 for(it_d = dims.begin(); it_d != dims.end(); it_d++)
520 ar->append_dim((*it_d)->getSize(), (*it_d)->getName());
529 HDFEOS2ArraySwathDimMapField * ar = NULL;
530 ar =
new HDFEOS2ArraySwathDimMapField(
537 (dataset)->getName(),
541 (*it_f)->getNewName(),
543 for(it_d = dims.begin(); it_d != dims.end(); it_d++)
544 ar->append_dim((*it_d)->getSize(), (*it_d)->getName());
552 HDFEOS2Array_RealField * ar = NULL;
553 ar =
new HDFEOS2Array_RealField(
560 (dataset)->getName(),
563 (*it_f)->getNewName(),
565 for(it_d = dims.begin(); it_d != dims.end(); it_d++)
566 ar->append_dim((*it_d)->getSize(), (*it_d)->getName());
574 throw InternalErr(__FILE__, __LINE__,
"The current type should be either grid or swath");
579 else if(fieldtype == 1 || fieldtype == 2) {
582 if(grid_or_swath==0) {
584 HDFEOS2ArrayGridGeoField *ar = NULL;
586 bool ydimmajor = (*it_f)->getYDimMajor();
587 bool condenseddim = (*it_f)->getCondensedDim();
588 bool speciallon = (*it_f)->getSpecialLon();
589 int specialformat = (*it_f)->getSpecialLLFormat();
591 ar =
new HDFEOS2ArrayGridGeoField(
602 (dataset)->getName(),
604 (*it_f)->getNewName(),
607 for(it_d = dims.begin(); it_d != dims.end(); it_d++)
608 ar->append_dim((*it_d)->getSize(), (*it_d)->getName());
621 else if(grid_or_swath ==1) {
623 if(
true == multi_dmap) {
624 if((*it_f)->getRank() !=2)
625 throw InternalErr(__FILE__, __LINE__,
"For the multi-dimmap case, the field rank must be 2.");
626 int dim0size = (dims[0])->getSize();
627 int dim1size = (dims[1])->getSize();
628 int dim0offset = (*it_f)->getLLDim0Offset();
629 int dim1offset = (*it_f)->getLLDim1Offset();
630 int dim0inc = (*it_f)->getLLDim0Inc();
631 int dim1inc = (*it_f)->getLLDim1Inc();
634 fieldname =
"Latitude";
636 fieldname =
"Longitude";
638 cerr<<
"hdfdesc: newfieldname is "<<(*it_f)->getNewName() <<endl;
639 cerr<<
"hdfdesc: dim0size "<<dim0size <<endl;
640 cerr<<
"hdfdesc: dim1size "<<dim1size <<endl;
641 cerr<<
"hdfdesc: dim0offset "<<dim0offset <<endl;
642 cerr<<
"hdfdesc: dim1offset "<<dim1offset <<endl;
643 cerr<<
"hdfdesc: dim0inc "<<dim0inc <<endl;
644 cerr<<
"hdfdesc: dim1inc "<<dim1inc <<endl;
647 HDFEOS2ArraySwathGeoMultiDimMapField * ar = NULL;
649 ar =
new HDFEOS2ArraySwathGeoMultiDimMapField(
653 (dataset)->getName(),
661 (*it_f)->getNewName(),
664 for(it_d = dims.begin(); it_d != dims.end(); it_d++)
665 ar->append_dim((*it_d)->getSize(), (*it_d)->getName());
674 if((*it_f)->UseDimMap()) {
677 if(!modis_geofilename.empty()) {
679 if (
false == geofile_has_dimmap) {
680 HDFEOS2ArraySwathGeoDimMapExtraField *ar = NULL;
681 ar =
new HDFEOS2ArraySwathGeoDimMapExtraField(
685 (*it_f)->getNewName(),
687 for(it_d = dims.begin(); it_d != dims.end(); it_d++)
688 ar->append_dim((*it_d)->getSize(), (*it_d)->getName());
695 HDFEOS2ArraySwathDimMapField * ar = NULL;
701 ar =
new HDFEOS2ArraySwathDimMapField(
708 "MODIS_Swath_Type_GEO",
712 (*it_f)->getNewName(),
714 for(it_d = dims.begin(); it_d != dims.end(); it_d++)
715 ar->append_dim((*it_d)->getSize(), (*it_d)->getName());
725 HDFEOS2ArraySwathDimMapField * ar = NULL;
726 ar =
new HDFEOS2ArraySwathDimMapField(
733 (dataset)->getName(),
737 (*it_f)->getNewName(),
739 for(it_d = dims.begin(); it_d != dims.end(); it_d++)
740 ar->append_dim((*it_d)->getSize(), (*it_d)->getName());
749 HDFEOS2ArraySwathGeoField * ar = NULL;
750 ar =
new HDFEOS2ArraySwathGeoField(
754 (dataset)->getName(),
756 (*it_f)->getNewName(),
759 for(it_d = dims.begin(); it_d != dims.end(); it_d++)
760 ar->append_dim((*it_d)->getSize(), (*it_d)->getName());
769 throw InternalErr(__FILE__, __LINE__,
"The current type should be either grid or swath");
775 else if(fieldtype == 4) {
777 if((*it_f)->getRank()!=1){
779 throw InternalErr(__FILE__, __LINE__,
"The rank of missing Z dimension field must be 1");
782 int nelem = ((*it_f)->getCorrectedDimensions()[0])->getSize();
783 HDFEOS2ArrayMissGeoField *ar = NULL;
784 ar =
new HDFEOS2ArrayMissGeoField(
787 (*it_f)->getNewName(),
790 for(it_d = dims.begin(); it_d != dims.end(); it_d++)
791 ar->append_dim((*it_d)->getSize(), (*it_d)->getName());
799 throw InternalErr(__FILE__, __LINE__,
"Encounter unsupported datatype or The field type should be between 0 and 5. ");
809 int read_dds_hdfeos2(DDS & dds,
const string & filename,int32 sdfd,int32 fileid, int32 gridfd, int32 swathfd,
HDFSP::File*spf,HDFEOS2::File*f)
812 BESDEBUG(
"h4",
"Coming to read_dds_hdfeos2 "<<endl);
815 dds.set_dataset_name(basename(filename));
823 if((basename(filename).size() >=5) && ((basename(filename)).compare(0,5,
"MERRA")==0))
828 string check_enable_spec_eos_key=
"H4.EnableSpecialEOS";
829 bool turn_on_enable_spec_eos_key=
false;
830 turn_on_enable_spec_eos_key = HDFCFUtil::check_beskeys(check_enable_spec_eos_key);
834 if(
true == HDF4RequestHandler::get_enable_special_eos()) {
837 int ret_val = check_special_eosfile(filename,grid_name,sdfd,fileid);
848 if(2 == ret_val || 3 == ret_val) {
851 read_dds_special_1d_grid(dds,spf,filename,sdfd,
false);
871 const vector<HDFEOS2::GridDataset *>& grids = f->getGrids();
873 bool onelatlon = f->getOneLatLon();
876 SOType sotype = DEFAULT_CF_EQU;
879 vector<HDFEOS2::GridDataset *>::const_iterator it_g;
880 for(it_g = grids.begin(); it_g != grids.end(); it_g++){
883 ownll = onelatlon?onelatlon:(*it_g)->getLatLonFlag();
886 sotype = (*it_g)->getScaleType();
888 read_dds_hdfeos2_grid_swath(
889 dds, filename,
static_cast<HDFEOS2::Dataset*
>(*it_g), 0,ownll,sotype,
false,sdfd,fileid,gridfd,swathfd);
892 HDFCFUtil::add_cf_grid_cvs(dds,*it_g);
901 bool multi_dmap = f->getMultiDimMaps();
905 const vector<HDFEOS2::SwathDataset *>& swaths= f->getSwaths();
906 vector<HDFEOS2::SwathDataset *>::const_iterator it_s;
907 for(it_s = swaths.begin(); it_s != swaths.end(); it_s++) {
910 sotype = (*it_s)->getScaleType();
913 read_dds_hdfeos2_grid_swath(
914 dds, filename,
static_cast<HDFEOS2::Dataset*
>(*it_s), 1,
false,sotype,multi_dmap,sdfd,fileid,gridfd,swathfd);
926 ctype_field_namelist.clear();
934 bool read_dds_hdfhybrid(DDS & dds,
const string & filename,int32 sdfd, int32 fileid,
HDFSP::File*f)
938 BESDEBUG(
"h4",
"Coming to read_dds_hdfhybrid "<<endl);
941 dds.set_dataset_name(basename(filename));
947 vector<HDFSP::SDField *>::const_iterator it_g;
948 for(it_g = spsds.begin(); it_g != spsds.end(); it_g++){
950 read_dds_spfields(dds,filename,sdfd,(*it_g),f->
getSPType());
965 string check_hybrid_vdata_key=
"H4.EnableHybridVdata";
966 bool turn_on_hybrid_vdata_key =
false;
967 turn_on_hybrid_vdata_key = HDFCFUtil::check_beskeys(check_hybrid_vdata_key);
971 if(
true == HDF4RequestHandler::get_enable_hybrid_vdata()) {
972 for(vector<HDFSP::VDATA *>::const_iterator i = f->
getVDATAs().begin(); i!=f->
getVDATAs().end();i++) {
973 if(
false == (*i)->getTreatAsAttrFlag()){
974 for(vector<HDFSP::VDField *>::const_iterator j=(*i)->getFields().begin();j!=(*i)->getFields().end();j++) {
976 read_dds_spvdfields(dds,filename,fileid, (*i)->getObjRef(),(*j)->getNumRec(),(*j));
991 bool read_das_hdfhybrid(DAS & das,
const string & filename,int32 sdfd, int32 fileid,
HDFSP::File**fpptr)
994 BESDEBUG(
"h4",
"Coming to read_das_hdfhybrid "<<endl);
1005 throw InternalErr(e.
what());
1012 string check_scale_offset_type_key =
"H4.EnableCheckScaleOffsetType";
1013 bool turn_on_enable_check_scale_offset_key=
false;
1014 turn_on_enable_check_scale_offset_key = HDFCFUtil::check_beskeys(check_scale_offset_type_key);
1019 vector<HDFSP::SDField *>::const_iterator it_g;
1020 for(it_g = spsds.begin(); it_g != spsds.end(); it_g++){
1023 AttrTable *at = das.get_table((*it_g)->getNewName());
1025 at = das.add_table((*it_g)->getNewName(),
new AttrTable);
1028 bool long_name_flag =
false;
1030 for(vector<HDFSP::Attribute *>::const_iterator i=(*it_g)->getAttributes().begin();i!=(*it_g)->getAttributes().end();i++) {
1032 if((*i)->getName() ==
"long_name") {
1033 long_name_flag =
true;
1038 if(
false == long_name_flag)
1039 at->append_attr(
"long_name",
"String", (*it_g)->getName());
1042 for(vector<HDFSP::Attribute *>::const_iterator i=(*it_g)->getAttributes().begin();i!=(*it_g)->getAttributes().end();i++) {
1045 if((*i)->getType()==DFNT_UCHAR || (*i)->getType() == DFNT_CHAR){
1048 string tempstring2((*i)->getValue().begin(),(*i)->getValue().end());
1049 string tempfinalstr= string(tempstring2.c_str());
1054 at->append_attr((*i)->getNewName(),
"String" , ((*i)->getNewName()==
"fullpath")?tempfinalstr:
HDFCFUtil::escattr(tempfinalstr));
1057 for (
int loc=0; loc < (*i)->getCount() ; loc++) {
1067 int32 var_type = (*it_g)->getType();
1083 if (
true == HDF4RequestHandler::get_enable_check_scale_offset_type() && at !=NULL)
1090 HDFCFUtil::handle_vdata_attrs_with_desc_key(f,das);
1102 void read_dds_use_eos2lib(DDS & dds,
const string & filename,int32 sdfd,int32 fileid, int32 gridfd, int32 swathfd,
HDFSP::File*h4file,HDFEOS2::File*eosfile)
1105 BESDEBUG(
"h4",
"Coming to read_dds_use_eos2lib" <<endl);
1107 int ret_value = read_dds_hdfeos2(dds,filename,sdfd,fileid,gridfd,swathfd,h4file,eosfile);
1109 BESDEBUG(
"h4",
"ret_value of read_dds_hdfeos2 is "<<ret_value<<endl);
1127 if (0 == ret_value || 5 == ret_value || 4 == ret_value ) {
1128 if(
true == read_dds_hdfsp(dds, filename,sdfd,fileid,h4file))
1132 else if ( 1 == ret_value ) {
1135 if(
true ==read_dds_hdfhybrid(dds,filename,sdfd,fileid,h4file))
1145 if(
true == read_dds_hdfeos2(dds, filename)){
1148 if(
true == read_dds_hdfhybrid(dds,filename))
1153 if(read_dds_hdfsp(dds, filename)){
1160 read_dds(dds, filename);
1165 void write_non_ecsmetadata_attrs(
HE2CF& cf) {
1167 cf.set_non_ecsmetadata_attrs();
1172 void write_ecsmetadata(DAS& das,
HE2CF& cf,
const string& _meta)
1191 bool suffix_is_number =
true;
1192 vector<string> meta_nonum_names;
1193 vector<string> meta_nonum_data;
1195 string meta = cf.
get_metadata(_meta,suffix_is_number,meta_nonum_names, meta_nonum_data);
1197 if(
""==meta &&
true == suffix_is_number){
1201 BESDEBUG(
"h4",meta << endl);
1203 if (
false == suffix_is_number) {
1207 for (
unsigned int i = 0; i <meta_nonum_names.size(); i++)
1208 parse_ecs_metadata(das,meta_nonum_names[i],meta_nonum_data[i]);
1211 parse_ecs_metadata(das,_meta,meta);
1215 void parse_ecs_metadata(DAS &das,
const string & metaname,
const string &metadata) {
1218 AttrTable *at = das.get_table(metaname);
1220 at = das.add_table(metaname,
new AttrTable);
1223 void *buf = hdfeos_string(metadata.c_str());
1226 if (hdfeosparse(&arg) != 0) {
1227 hdfeos_delete_buffer(buf);
1228 throw Error(
"HDF-EOS parse error while processing a " + metadata +
" HDFEOS attribute.");
1231 if (arg.status() ==
false) {
1232 (*BESLog::TheLog())<<
"HDF-EOS parse error while processing a "
1233 << metadata <<
" HDFEOS attribute. (2) " << endl;
1237 hdfeos_delete_buffer(buf);
1241 int read_das_hdfeos2(DAS & das,
const string & filename,int32 sdfd,int32 fileid, int32 gridfd, int32 swathfd,
1242 bool ecs_metadata,
HDFSP::File**spfpptr,HDFEOS2::File **fpptr)
1245 BESDEBUG(
"h4",
"Coming to read_das_hdfeos2 " << endl);
1257 if((basename(filename).size() >=5) && ((basename(filename)).compare(0,5,
"MERRA")==0)) {
1263 string check_enable_spec_eos_key=
"H4.EnableSpecialEOS";
1264 bool turn_on_enable_spec_eos_key=
false;
1265 turn_on_enable_spec_eos_key = HDFCFUtil::check_beskeys(check_enable_spec_eos_key);
1267 if(
true == HDF4RequestHandler::get_enable_special_eos()) {
1270 int ret_val = check_special_eosfile(filename,grid_name,sdfd,fileid);
1276 bool airs_l2_l3_v6 =
false;
1277 bool special_1d_grid =
false;
1280 if(2 == ret_val || 3 == ret_val) {
1290 throw InternalErr(e.
what());
1297 if(spf->Check_update_special(grid_name)==
true){
1299 special_1d_grid =
true;
1302 read_das_special_eos2_core(das,spf,filename,ecs_metadata);
1305 if(grid_name ==
"mod08") {
1306 change_das_mod08_scale_offset(das,spf);
1312 airs_l2_l3_v6 =
true;
1313 spf->Handle_AIRS_L23();
1314 read_das_special_eos2_core(das,spf,filename,ecs_metadata);
1326 if (
true == special_1d_grid ||
true == airs_l2_l3_v6) {
1334 HDFEOS2::File *f = NULL;
1338 f= HDFEOS2::File::Read(filename.c_str(),gridfd,swathfd);
1340 catch (HDFEOS2::Exception &e){
1346 if (!e.getFileType()){
1352 throw InternalErr(e.what());
1359 f->Prepare(filename.c_str());
1362 catch (HDFEOS2:: Exception &e) {
1365 throw InternalErr(e.what());
1374 cf.
open(filename,sdfd,fileid);
1381 SOType sotype = DEFAULT_CF_EQU;
1386 bool tempstrflag =
false;
1390 if (f->getSwaths().size() > 0) {
1391 string temp_fname = basename(filename);
1392 string temp_prod_prefix =
"AMSR_E";
1393 if ((temp_fname.size() > temp_prod_prefix.size()) &&
1394 (0 == (temp_fname.compare(0,temp_prod_prefix.size(),temp_prod_prefix)))) {
1401 bool gridname_change_valid_range =
false;
1402 if(1 == f->getGrids().size()) {
1403 string gridname = f->getGrids()[0]->getName();
1404 if (
"VIP_CMG_GRID" == gridname)
1405 gridname_change_valid_range =
true;
1409 bool is_modis_l1b =
false;
1412 for (
int i = 0; i<(
int) f->getSwaths().size(); i++) {
1413 HDFEOS2::SwathDataset* swath = f->getSwaths()[i];
1414 string sname = swath->getName();
1415 if(
"MODIS_SWATH_Type_L1B" == sname){
1416 is_modis_l1b =
true;
1422 string check_disable_scale_comp_key =
"H4.DisableScaleOffsetComp";
1423 bool turn_on_disable_scale_comp_key=
false;
1424 turn_on_disable_scale_comp_key = HDFCFUtil::check_beskeys(check_disable_scale_comp_key);
1426 string check_scale_offset_type_key =
"H4.EnableCheckScaleOffsetType";
1427 bool turn_on_enable_check_scale_offset_key=
false;
1428 turn_on_enable_check_scale_offset_key = HDFCFUtil::check_beskeys(check_scale_offset_type_key);
1434 for (
int i = 0; i < (
int) f->getGrids().size(); i++) {
1436 HDFEOS2::GridDataset* grid = f->getGrids()[i];
1437 string gname = grid->getName();
1438 sotype = grid->getScaleType();
1440 const vector<HDFEOS2::Field*>gfields = grid->getDataFields();
1441 vector<HDFEOS2::Field*>::const_iterator it_gf;
1443 for (it_gf = gfields.begin();it_gf != gfields.end();++it_gf) {
1445 bool change_fvtype =
false;
1448 string fname = (*it_gf)->getName();
1451 string newfname = (*it_gf)->getNewName();
1453 BESDEBUG(
"h4",
"Original field name: " << fname << endl);
1454 BESDEBUG(
"h4",
"Corrected field name: " << newfname << endl);
1457 int fieldtype = (*it_gf)->getFieldType();
1460 if (fieldtype == 0){
1463 if((*it_gf)->haveAddedFillValue()) {
1464 BESDEBUG(
"h4",
"Has an added fill value." << endl);
1465 float addedfillvalue =
1466 (*it_gf)->getAddedFillValue();
1468 (*it_gf)->getType();
1469 BESDEBUG(
"h4",
"Added fill value = "<<addedfillvalue);
1471 type, addedfillvalue);
1473 string coordinate = (*it_gf)->getCoordinate();
1474 BESDEBUG(
"h4",
"Coordinate attribute: " << coordinate <<endl);
1475 if (coordinate !=
"")
1481 f->getGrids().size(), fieldtype);
1493 if(fieldtype == 1 && ((*it_gf)->getSpecialLLFormat())==3)
1498 if (fieldtype !=3) {
1499 string tempunits = (*it_gf)->getUnits();
1501 "fieldtype " << fieldtype
1502 <<
" units" << tempunits
1509 AttrTable *at = das.get_table(newfname);
1512 if(sotype!=DEFAULT_CF_EQU && at!=NULL)
1514 bool has_Key_attr =
false;
1515 AttrTable::Attr_iter it = at->attr_begin();
1516 while (it!=at->attr_end())
1518 if(at->get_name(it)==
"Key")
1520 has_Key_attr =
true;
1527 if((
false == is_modis_l1b) && (
false == gridname_change_valid_range)&&(
false == has_Key_attr) &&
1528 (
true == HDF4RequestHandler::get_disable_scaleoffset_comp()))
1529 HDFCFUtil::handle_modis_special_attrs_disable_scale_comp(at,basename(filename),
true, newfname,sotype);
1533 bool changedtype = HDFCFUtil::change_data_type(das,sotype,newfname);
1536 if (
true == changedtype)
1537 ctype_field_namelist.push_back(newfname);
1539 HDFCFUtil::handle_modis_special_attrs(at,basename(filename),
true, newfname,sotype,gridname_change_valid_range,changedtype,change_fvtype);
1545 HDFCFUtil::handle_amsr_attrs(at);
1549 if((
false == change_fvtype) && at != NULL) {
1550 int32 var_type = (*it_gf)->getType();
1560 if (
true == HDF4RequestHandler::get_enable_check_scale_offset_type() && at!=NULL)
1567 HDFCFUtil::add_cf_grid_cv_attrs(das,grid);
1578 for (
int i = 0; i < (
int) f->getSwaths().size(); i++) {
1580 HDFEOS2::SwathDataset* swath = f->getSwaths()[i];
1584 const vector<HDFEOS2::Field*> geofields = swath->getGeoFields();
1585 vector<HDFEOS2::Field*> all_fields = geofields;
1586 vector<HDFEOS2::Field*>::const_iterator it_f;
1588 const vector<HDFEOS2::Field*> datafields = swath->getDataFields();
1589 for (it_f = datafields.begin(); it_f != datafields.end(); it_f++)
1590 all_fields.push_back(*it_f);
1592 int total_geofields = geofields.size();
1594 string gname = swath->getName();
1595 BESDEBUG(
"h4",
"Swath name: " << gname << endl);
1597 sotype = swath->getScaleType();
1600 int field_counter = 0;
1602 for(it_f = all_fields.begin(); it_f != all_fields.end(); it_f++)
1604 bool change_fvtype =
false;
1605 string fname = (*it_f)->getName();
1606 string newfname = (*it_f)->getNewName();
1607 BESDEBUG(
"h4",
"Original Field name: " << fname << endl);
1608 BESDEBUG(
"h4",
"Corrected Field name: " << newfname << endl);
1610 int fieldtype = (*it_f)->getFieldType();
1611 if (fieldtype == 0){
1612 string coordinate = (*it_f)->getCoordinate();
1613 BESDEBUG(
"h4",
"Coordinate attribute: " << coordinate <<endl);
1614 if (coordinate !=
"")
1622 if(fieldtype >0 && fieldtype !=3){
1623 string tempunits = (*it_f)->getUnits();
1625 "fieldtype " << fieldtype
1626 <<
" units" << tempunits << endl);
1630 BESDEBUG(
"h4",
"Field Name: " << fname << endl);
1634 if (field_counter >=total_geofields) {
1635 if((*it_f)->haveAddedFillValue()){
1636 float addedfillvalue =
1637 (*it_f)->getAddedFillValue();
1640 BESDEBUG(
"h4",
"Added fill value = "<<addedfillvalue);
1645 f->getSwaths().size(), fieldtype);
1647 AttrTable *at = das.get_table(newfname);
1650 if(sotype!=DEFAULT_CF_EQU && at!=NULL)
1653 bool has_Key_attr =
false;
1654 AttrTable::Attr_iter it = at->attr_begin();
1655 while (it!=at->attr_end())
1657 if(at->get_name(it)==
"Key")
1659 has_Key_attr =
true;
1666 if((
false == is_modis_l1b) && (
false == gridname_change_valid_range) &&(
false == has_Key_attr) &&
1667 (
true == HDF4RequestHandler::get_disable_scaleoffset_comp()))
1668 HDFCFUtil::handle_modis_special_attrs_disable_scale_comp(at,basename(filename),
false,newfname,sotype);
1672 bool changedtype = HDFCFUtil::change_data_type(das,sotype,newfname);
1675 if (
true == changedtype)
1677 ctype_field_namelist.push_back(newfname);
1682 HDFCFUtil::handle_modis_special_attrs(at,basename(filename),
false,newfname,sotype,gridname_change_valid_range,changedtype,change_fvtype);
1688 HDFCFUtil::handle_amsr_attrs(at);
1692 if((
false == change_fvtype) && at != NULL) {
1693 int32 var_type = (*it_f)->getType();
1703 if (
true == HDF4RequestHandler::get_enable_check_scale_offset_type() && at !=NULL)
1718 if(ecs_metadata ==
true) {
1721 write_ecsmetadata(das,cf,
"CoreMetadata");
1723 write_ecsmetadata(das,cf,
"coremetadata");
1725 write_ecsmetadata(das,cf,
"ArchiveMetadata");
1727 write_ecsmetadata(das,cf,
"archivemetadata");
1729 write_ecsmetadata(das,cf,
"ProductMetadata");
1731 write_ecsmetadata(das,cf,
"productmetadata");
1735 if(
false == tempstrflag) {
1738 string check_disable_smetadata_key =
"H4.DisableStructMetaAttr";
1739 bool is_check_disable_smetadata =
false;
1740 is_check_disable_smetadata = HDFCFUtil::check_beskeys(check_disable_smetadata_key);
1743 if (
false == HDF4RequestHandler::get_disable_structmeta() ) {
1744 write_ecsmetadata(das, cf,
"StructMetadata");
1749 write_non_ecsmetadata_attrs(cf);
1762 string check_enable_sg_attr_key=
"H4.EnableSwathGridAttr";
1763 bool turn_on_enable_sg_attr_key=
false;
1764 turn_on_enable_sg_attr_key = HDFCFUtil::check_beskeys(check_enable_sg_attr_key);
1767 if(
true == HDF4RequestHandler::get_enable_swath_grid_attr()) {
1770 for (
int i = 0; i < (
int) f->getGrids().size(); i++) {
1773 HDFEOS2::GridDataset* grid = f->getGrids()[i];
1777 AttrTable*at = NULL;
1780 if(grid->getAttributes().size() != 0){
1781 at = das.get_table(gname);
1783 at = das.add_table(gname,
new AttrTable);
1788 const vector<HDFEOS2::Attribute *> grid_attrs = grid->getAttributes();
1789 vector<HDFEOS2::Attribute*>::const_iterator it_a;
1790 for (it_a = grid_attrs.begin(); it_a != grid_attrs.end(); ++it_a) {
1792 int attr_type = (*it_a)->getType();
1795 if(attr_type==DFNT_UCHAR || attr_type == DFNT_CHAR){
1796 string tempstring2((*it_a)->getValue().begin(),(*it_a)->getValue().end());
1797 string tempfinalstr= string(tempstring2.c_str());
1807 for (
int loc=0; loc < (*it_a)->getCount() ; loc++) {
1808 string print_rep =
HDFCFUtil::print_attr((*it_a)->getType(), loc, (
void*) &((*it_a)->getValue()[0]));
1818 for (
int i = 0; i < (
int) f->getSwaths().size(); i++) {
1820 HDFEOS2::SwathDataset* swath = f->getSwaths()[i];
1821 string sname = swath->getName();
1822 AttrTable*at = NULL;
1825 if(swath->getAttributes().size() != 0) {
1826 at = das.get_table(sname);
1828 at = das.add_table(sname,
new AttrTable);
1832 const vector<HDFEOS2::Attribute *> swath_attrs = swath->getAttributes();
1833 vector<HDFEOS2::Attribute*>::const_iterator it_a;
1834 for (it_a = swath_attrs.begin(); it_a != swath_attrs.end(); ++it_a) {
1836 int attr_type = (*it_a)->getType();
1839 if(attr_type==DFNT_UCHAR || attr_type == DFNT_CHAR){
1840 string tempstring2((*it_a)->getValue().begin(),(*it_a)->getValue().end());
1841 string tempfinalstr= string(tempstring2.c_str());
1849 for (
int loc=0; loc < (*it_a)->getCount() ; loc++) {
1850 string print_rep =
HDFCFUtil::print_attr((*it_a)->getType(), loc, (
void*) &((*it_a)->getValue()[0]));
1868 void read_das_use_eos2lib(DAS & das,
const string & filename,
1869 int32 sdfd,int32 fileid, int32 gridfd, int32 swathfd,
bool ecs_metadata,
1870 HDFSP::File**h4filepptr,HDFEOS2::File**eosfilepptr)
1873 BESDEBUG(
"h4",
"Coming to read_das_use_eos2lib" << endl);
1875 int ret_value = read_das_hdfeos2(das,filename,sdfd,fileid, gridfd, swathfd,ecs_metadata,h4filepptr,eosfilepptr);
1877 BESDEBUG(
"h4",
"ret_value of read_das_hdfeos2 is "<<ret_value <<endl);
1893 if (ret_value == 4) {
1894 if(
true == read_das_special_eos2(das, filename,sdfd,fileid,ecs_metadata,h4filepptr))
1898 else if (ret_value == 2 || ret_value == 3) {
1901 else if (ret_value == 1) {
1904 if(
true == read_das_hdfhybrid(das,filename,sdfd,fileid,h4filepptr))
1908 if(
true == read_das_hdfsp(das, filename,sdfd, fileid,h4filepptr))
1916 if(
true == read_das_hdfeos2(das, filename)){
1919 if (
true == read_das_hdfhybrid(das,filename))
1924 if(
true == read_das_hdfsp(das, filename)){
1931 read_das(das, filename);
1938 bool read_dds_hdfsp(DDS & dds,
const string & filename,int32 sdfd, int32 fileid,
HDFSP::File*f)
1941 BESDEBUG(
"h4",
"Coming to read_dds_sp "<<endl);
1942 dds.set_dataset_name(basename(filename));
1948 vector<HDFSP::SDField *>::const_iterator it_g;
1949 for(it_g = spsds.begin(); it_g != spsds.end(); it_g++){
1955 if (
false == f->
Has_Dim_NoScale_Field() || (0 == (*it_g)->getFieldType()) || (
true == (*it_g)->IsDimScale())){
1957 read_dds_spfields(dds,filename,sdfd,(*it_g),f->
getSPType());
1968 string check_ceres_vdata_key=
"H4.EnableCERESVdata";
1969 bool turn_on_ceres_vdata_key=
false;
1970 turn_on_ceres_vdata_key = HDFCFUtil::check_beskeys(check_ceres_vdata_key);
1973 bool output_vdata_flag =
true;
1975 if (
false == HDF4RequestHandler::get_enable_ceres_vdata() &&
1980 output_vdata_flag =
false;
1982 if(
true == output_vdata_flag) {
1983 for(vector<HDFSP::VDATA *>::const_iterator i=f->
getVDATAs().begin(); i!=f->
getVDATAs().end();i++) {
1984 if(!(*i)->getTreatAsAttrFlag()){
1985 for(vector<HDFSP::VDField *>::const_iterator j=(*i)->getFields().begin();j!=(*i)->getFields().end();j++) {
1987 read_dds_spvdfields(dds,filename,fileid,(*i)->getObjRef(),(*j)->getNumRec(),(*j));
2003 bool read_das_hdfsp(DAS & das,
const string & filename, int32 sdfd, int32 fileid,
HDFSP::File**fpptr)
2006 BESDEBUG(
"h4",
"Coming to read_das_sp "<<endl);
2018 throw InternalErr(e.
what());
2028 throw InternalErr(e.
what());
2035 string check_enable_vg_attr_key=
"H4.EnableVgroupAttr";
2036 bool turn_on_enable_vg_attr_key=
false;
2037 turn_on_enable_vg_attr_key = HDFCFUtil::check_beskeys(check_enable_vg_attr_key);
2042 if(
true == HDF4RequestHandler::get_enable_vgroup_attr()) {
2045 vector<HDFSP::AttrContainer *>vg_container = f->
getVgattrs();
2046 for(vector<HDFSP::AttrContainer *>::const_iterator i=f->
getVgattrs().begin();i!=f->
getVgattrs().end();i++) {
2047 AttrTable *vgattr_at = das.get_table((*i)->getName());
2049 vgattr_at = das.add_table((*i)->getName(),
new AttrTable);
2051 for(vector<HDFSP::Attribute *>::const_iterator j=(*i)->getAttributes().begin();j!=(*i)->getAttributes().end();j++) {
2054 if((*j)->getType()==DFNT_UCHAR || (*j)->getType() == DFNT_CHAR){
2055 string tempstring2((*j)->getValue().begin(),(*j)->getValue().end());
2056 string tempfinalstr= string(tempstring2.c_str());
2059 vgattr_at->append_attr((*j)->getNewName(),
"String" ,
HDFCFUtil::escattr(tempfinalstr));
2062 for (
int loc=0; loc < (*j)->getCount() ; loc++) {
2073 string core_metadata =
"";
2074 string archive_metadata =
"";
2075 string struct_metadata =
"";
2086 if(((*i)->getName().compare(0, 12,
"CoreMetadata" )== 0) ||
2087 ((*i)->getName().compare(0, 12,
"coremetadata" )== 0)){
2092 string tempstring((*i)->getValue().begin(),(*i)->getValue().end());
2099 core_metadata.append(tempstring);
2101 else if(((*i)->getName().compare(0, 15,
"ArchiveMetadata" )== 0) ||
2102 ((*i)->getName().compare(0, 16,
"ArchivedMetadata")==0) ||
2103 ((*i)->getName().compare(0, 15,
"archivemetadata" )== 0)){
2104 string tempstring((*i)->getValue().begin(),(*i)->getValue().end());
2109 archive_metadata.append(tempstring);
2111 else if(((*i)->getName().compare(0, 14,
"StructMetadata" )== 0) ||
2112 ((*i)->getName().compare(0, 14,
"structmetadata" )== 0)){
2115 string check_disable_smetadata_key =
"H4.DisableStructMetaAttr";
2116 bool is_check_disable_smetadata =
false;
2117 is_check_disable_smetadata = HDFCFUtil::check_beskeys(check_disable_smetadata_key);
2121 if (
false == HDF4RequestHandler::get_disable_structmeta()) {
2123 string tempstring((*i)->getValue().begin(),(*i)->getValue().end());
2131 struct_metadata.append(tempstring);
2137 AttrTable *at = das.get_table(
"HDF_GLOBAL");
2139 at = das.add_table(
"HDF_GLOBAL",
new AttrTable);
2142 if((*i)->getType()==DFNT_UCHAR || (*i)->getType() == DFNT_CHAR){
2143 string tempstring2((*i)->getValue().begin(),(*i)->getValue().end());
2144 string tempfinalstr= string(tempstring2.c_str());
2153 for (
int loc=0; loc < (*i)->getCount() ; loc++) {
2167 if(core_metadata.size() > 0){
2168 AttrTable *at = das.get_table(
"CoreMetadata");
2170 at = das.add_table(
"CoreMetadata",
new AttrTable);
2172 void *buf = hdfeos_string(core_metadata.c_str());
2175 if (hdfeosparse(&arg) != 0) {
2176 hdfeos_delete_buffer(buf);
2177 throw Error(
"Parse error while processing a CoreMetadata attribute.");
2181 if (arg.status() ==
false) {
2182 ERROR_LOG(
"Parse error while processing a CoreMetadata attribute. (2) " << endl);
2186 hdfeos_delete_buffer(buf);
2190 if(archive_metadata.size() > 0){
2191 AttrTable *at = das.get_table(
"ArchiveMetadata");
2193 at = das.add_table(
"ArchiveMetadata",
new AttrTable);
2195 void *buf = hdfeos_string(archive_metadata.c_str());
2197 if (hdfeosparse(&arg) != 0){
2199 hdfeos_delete_buffer(buf);
2200 throw Error(
"Parse error while processing an ArchiveMetadata attribute.");
2204 if (arg.status() ==
false) {
2205 ERROR_LOG(
"Parse error while processing an ArchiveMetadata attribute. (2) " << endl);
2209 hdfeos_delete_buffer(buf);
2213 if(struct_metadata.size() > 0){
2214 AttrTable *at = das.get_table(
"StructMetadata");
2216 at = das.add_table(
"StructMetadata",
new AttrTable);
2218 void *buf = hdfeos_string(struct_metadata.c_str());
2220 if (hdfeosparse(&arg) != 0){
2222 hdfeos_delete_buffer(buf);
2223 throw Error(
"Parse error while processing a StructMetadata attribute.");
2226 if (arg.status() ==
false) {
2227 ERROR_LOG(
"Parse error while processing a StructMetadata attribute. (2)" << endl);
2233 if (arg.status() ==
false) {
2234 (*BESLog::TheLog())<<
"Parse error while processing a StructMetadata attribute. (2)" << endl
2235 << arg.error()->get_error_message() << endl;
2239 hdfeos_delete_buffer(buf);
2248 bool global_slope_flag =
false;
2249 float intercept = 0.;
2250 bool global_intercept_flag =
false;
2254 HDFCFUtil::check_obpg_global_attrs(f,scaling,slope,global_slope_flag,intercept,global_intercept_flag);
2258 vector<HDFSP::SDField *>::const_iterator it_g;
2259 for(it_g = spsds.begin(); it_g != spsds.end(); it_g++){
2266 ((*it_g)->getFieldType() !=0)&&
2267 ((*it_g)->IsDimScale() ==
false))
2271 if (OTHERHDF == f->
getSPType() &&
true == (*it_g)->IsDimNoScale())
2274 AttrTable *at = das.get_table((*it_g)->getNewName());
2276 at = das.add_table((*it_g)->getNewName(),
new AttrTable);
2279 bool long_name_flag =
false;
2281 for(vector<HDFSP::Attribute *>::const_iterator i=(*it_g)->getAttributes().begin();
2282 i!=(*it_g)->getAttributes().end();i++) {
2283 if((*i)->getName() ==
"long_name") {
2284 long_name_flag =
true;
2289 if(
false == long_name_flag) {
2291 if((*it_g)->getFieldType() == 1)
2292 at->append_attr(
"standard_name",
"String",
"latitude");
2293 else if ((*it_g)->getFieldType() == 2) {
2294 at->append_attr(
"standard_name",
"String",
"longitude");
2300 if((*it_g)->getFieldType() == 1) {
2301 at->append_attr(
"long_name",
"String",
"latitude");
2302 at->append_attr(
"standard_name",
"String",
"latitude");
2305 else if ((*it_g)->getFieldType() == 2) {
2306 at->append_attr(
"long_name",
"String",
"longitude");
2307 at->append_attr(
"standard_name",
"String",
"longitude");
2312 at->append_attr(
"long_name",
"String", (*it_g)->getName());
2318 HDFCFUtil::add_obpg_special_attrs(f,das,*it_g,scaling,slope,global_slope_flag,intercept,global_intercept_flag);
2321 for(vector<HDFSP::Attribute *>::const_iterator i=(*it_g)->getAttributes().begin();i!=(*it_g)->getAttributes().end();i++) {
2324 if((*i)->getType()==DFNT_UCHAR || (*i)->getType() == DFNT_CHAR){
2325 string tempstring2((*i)->getValue().begin(),(*i)->getValue().end());
2326 string tempfinalstr= string(tempstring2.c_str());
2332 at->append_attr((*i)->getNewName(),
"String" ,((*i)->getNewName()==
"fullpath")?tempfinalstr:
HDFCFUtil::escattr(tempfinalstr));
2335 for (
int loc=0; loc < (*i)->getCount() ; loc++) {
2350 bool has_dim_info =
true;
2351 vector<HDFSP::AttrContainer *>::const_iterator it_end = (*it_g)->getDimInfo().end();
2352 if((*it_g)->getType() == DFNT_CHAR) {
2353 if((*it_g)->getRank() >1 && (*it_g)->getDimInfo().size() >1)
2354 it_end = (*it_g)->getDimInfo().begin()+(*it_g)->getDimInfo().size() -1;
2356 has_dim_info =
false;
2359 if(
true == has_dim_info) {
2361 for(vector<HDFSP::AttrContainer *>::const_iterator i=(*it_g)->getDimInfo().begin();i!=it_end;i++) {
2365 string attr_container_name = (*it_g)->getNewName() + (*i)->getName();
2366 AttrTable *dim_at = das.get_table(attr_container_name);
2368 dim_at = das.add_table(attr_container_name,
new AttrTable);
2370 for(vector<HDFSP::Attribute *>::const_iterator j=(*i)->getAttributes().begin();j!=(*i)->getAttributes().end();j++) {
2373 if((*j)->getType()==DFNT_UCHAR || (*j)->getType() == DFNT_CHAR){
2374 string tempstring2((*j)->getValue().begin(),(*j)->getValue().end());
2375 string tempfinalstr= string(tempstring2.c_str());
2378 dim_at->append_attr((*j)->getNewName(),
"String" ,
HDFCFUtil::escattr(tempfinalstr));
2381 for (
int loc=0; loc < (*j)->getCount() ; loc++) {
2394 if((*it_g)->getFieldType() == 1){
2396 at->del_attr(
"units");
2397 at->append_attr(
"units",
"String",(*it_g)->getUnits());
2399 at->del_attr(
"valid_range");
2404 if((*it_g)->getFieldType() == 2){
2405 at->del_attr(
"units");
2406 at->append_attr(
"units",
"String",(*it_g)->getUnits());
2408 at->del_attr(
"valid_range");
2414 if((*it_g)->getFieldType() == 4){
2415 at->del_attr(
"units");
2416 at->append_attr(
"units",
"String",(*it_g)->getUnits());
2420 if((*it_g)->getFieldType() == 0){
2421 at->del_attr(
"coordinates");
2425 string coordinate = (*it_g)->getCoordinate();
2426 if (coordinate !=
"")
2427 at->append_attr(
"coordinates",
"String", coordinate);
2434 HDFCFUtil::handle_otherhdf_special_attrs(f,das);
2437 HDFCFUtil::add_missing_cf_attrs(f,das);
2440 string check_scale_offset_type_key =
"H4.EnableCheckScaleOffsetType";
2441 bool turn_on_enable_check_scale_offset_key=
false;
2442 turn_on_enable_check_scale_offset_key = HDFCFUtil::check_beskeys(check_scale_offset_type_key);
2447 for(it_g = spsds.begin(); it_g != spsds.end(); it_g++){
2449 AttrTable *at = das.get_table((*it_g)->getNewName());
2451 int32 var_type = (*it_g)->getType();
2466 if (
true == HDF4RequestHandler::get_enable_check_scale_offset_type() && at !=NULL)
2471 HDFCFUtil::handle_merra_ceres_attrs_with_bes_keys(f,das,filename);
2477 HDFCFUtil::handle_vdata_attrs_with_desc_key(f,das);
2494 bool read_das_special_eos2(DAS &das,
const string& filename,int32 sdfd,int32 fileid,
bool ecs_metadata,
HDFSP::File**fpptr) {
2496 BESDEBUG(
"h4",
"Coming to read_das_special_eos2 " << endl);
2501 myfileid = Hopen(
const_cast<char *
>(filename.c_str()), DFACC_READ,0);
2515 throw InternalErr(e.
what());
2525 throw InternalErr(e.
what());
2531 read_das_special_eos2_core(das, f, filename,ecs_metadata);
2543 bool read_das_special_eos2_core(DAS &das,
HDFSP::File* f,
const string& filename,
bool ecs_metadata) {
2545 BESDEBUG(
"h4",
"Coming to read_das_special_eos2_core "<<endl);
2547 string core_metadata =
"";
2548 string archive_metadata =
"";
2549 string struct_metadata =
"";
2558 if(((*i)->getName().compare(0, 12,
"CoreMetadata" )== 0) ||
2559 ((*i)->getName().compare(0, 12,
"coremetadata" )== 0)){
2561 if(ecs_metadata ==
true) {
2565 string tempstring((*i)->getValue().begin(),(*i)->getValue().end());
2566 core_metadata.append(tempstring);
2569 else if(((*i)->getName().compare(0, 15,
"ArchiveMetadata" )== 0) ||
2570 ((*i)->getName().compare(0, 16,
"ArchivedMetadata")==0) ||
2571 ((*i)->getName().compare(0, 15,
"archivemetadata" )== 0)){
2572 if(ecs_metadata ==
true) {
2573 string tempstring((*i)->getValue().begin(),(*i)->getValue().end());
2574 archive_metadata.append(tempstring);
2577 else if(((*i)->getName().compare(0, 14,
"StructMetadata" )== 0) ||
2578 ((*i)->getName().compare(0, 14,
"structmetadata" )== 0))
2582 AttrTable *at = das.get_table(
"HDF_GLOBAL");
2584 at = das.add_table(
"HDF_GLOBAL",
new AttrTable);
2587 if((*i)->getType()==DFNT_UCHAR || (*i)->getType() == DFNT_CHAR){
2588 string tempstring2((*i)->getValue().begin(),(*i)->getValue().end());
2589 string tempfinalstr= string(tempstring2.c_str());
2598 for (
int loc=0; loc < (*i)->getCount() ; loc++) {
2611 if(ecs_metadata ==
true) {
2613 if(core_metadata.size() > 0){
2614 AttrTable *at = das.get_table(
"CoreMetadata");
2616 at = das.add_table(
"CoreMetadata",
new AttrTable);
2618 void *buf = hdfeos_string(core_metadata.c_str());
2621 if (hdfeosparse(&arg) != 0) {
2622 hdfeos_delete_buffer(buf);
2623 throw Error(
"Parse error while processing a CoreMetadata attribute.");
2627 if (arg.status() ==
false) {
2628 ERROR_LOG(
"Parse error while processing a CoreMetadata attribute. (2)" << endl);
2632 hdfeos_delete_buffer(buf);
2637 if(archive_metadata.size() > 0){
2638 AttrTable *at = das.get_table(
"ArchiveMetadata");
2640 at = das.add_table(
"ArchiveMetadata",
new AttrTable);
2642 void *buf = hdfeos_string(archive_metadata.c_str());
2644 if (hdfeosparse(&arg) != 0) {
2645 hdfeos_delete_buffer(buf);
2646 throw Error(
"Parse error while processing an ArchiveMetadata attribute.");
2650 if (arg.status() ==
false) {
2651 ERROR_LOG(
"Parse error while processing an ArchiveMetadata attribute. (2)" << endl);
2655 hdfeos_delete_buffer(buf);
2661 vector<HDFSP::SDField *>::const_iterator it_g;
2662 for(it_g = spsds.begin(); it_g != spsds.end(); it_g++){
2666 if((*it_g)->getFieldType() != 0){
2668 AttrTable *at = das.get_table((*it_g)->getNewName());
2670 at = das.add_table((*it_g)->getNewName(),
new AttrTable);
2672 string tempunits = (*it_g)->getUnits();
2673 if(at->simple_find(
"units")== at->attr_end() && tempunits!=
"")
2674 at->append_attr(
"units",
"String" ,tempunits);
2675 if((*it_g)->getFieldType() == 1){
2676 if(at->simple_find(
"long_name")== at->attr_end())
2677 at->append_attr(
"long_name",
"String",
"Latitude");
2679 else if((*it_g)->getFieldType() == 2) {
2680 if(at->simple_find(
"long_name")== at->attr_end())
2681 at->append_attr(
"long_name",
"String",
"Longitude");
2685 AttrTable *at = das.get_table((*it_g)->getNewName());
2687 at = das.add_table((*it_g)->getNewName(),
new AttrTable);
2688 string tempcoors = (*it_g)->getCoordinate();
2691 at->del_attr(
"coordinates");
2692 at->append_attr(
"coordinates",
"String",tempcoors);
2698 if((*it_g)->getAttributes().size() == 0)
2701 AttrTable *at = das.get_table((*it_g)->getNewName());
2703 at = das.add_table((*it_g)->getNewName(),
new AttrTable);
2706 for(vector<HDFSP::Attribute *>::const_iterator i=(*it_g)->getAttributes().begin();i!=(*it_g)->getAttributes().end();i++) {
2709 if((*i)->getType()==DFNT_UCHAR || (*i)->getType() == DFNT_CHAR){
2710 string tempstring2((*i)->getValue().begin(),(*i)->getValue().end());
2711 string tempfinalstr= string(tempstring2.c_str());
2717 at->append_attr((*i)->getNewName(),
"String" ,((*i)->getNewName()==
"fullpath")?tempfinalstr:
HDFCFUtil::escattr(tempfinalstr));
2720 for (
int loc=0; loc < (*i)->getCount() ; loc++) {
2731 HDFCFUtil::map_eos2_objects_attrs(das,filename);
2739 void change_das_mod08_scale_offset(DAS &das,
HDFSP::File *f) {
2744 vector<HDFSP::SDField *>::const_iterator it_g;
2745 for(it_g = spsds.begin(); it_g != spsds.end(); it_g++){
2746 if((*it_g)->getFieldType() == 0){
2747 AttrTable *at = das.get_table((*it_g)->getNewName());
2749 at = das.add_table((*it_g)->getNewName(),
new AttrTable);
2752 string add_offset_type;
2755 string add_offset_value=
"0";
2756 double orig_offset_value = 0;
2757 bool add_offset_modify =
false;
2763 AttrTable::Attr_iter it = at->attr_begin();
2764 while (it!=at->attr_end())
2766 if(at->get_name(it)==
"add_offset")
2768 add_offset_value = (*at->get_attr_vector(it)->begin());
2769 orig_offset_value = atof(add_offset_value.c_str());
2770 add_offset_type = at->get_type(it);
2771 if(add_offset_value ==
"0.0" || orig_offset_value == 0)
2772 add_offset_modify =
false;
2774 add_offset_modify =
true;
2782 if(
true == add_offset_modify) {
2785 string scale_factor_type;
2788 string scale_factor_value=
"";
2789 double orig_scale_value = 1;
2791 it = at->attr_begin();
2792 while (it!=at->attr_end())
2794 if(at->get_name(it)==
"scale_factor")
2796 scale_factor_value = (*at->get_attr_vector(it)->begin());
2797 orig_scale_value = atof(scale_factor_value.c_str());
2798 scale_factor_type = at->get_type(it);
2803 if(scale_factor_value.length() !=0) {
2804 double new_offset_value = -1 * orig_scale_value*orig_offset_value;
2806 at->del_attr(
"add_offset");
2818 bool read_dds_special_1d_grid(DDS &dds,
HDFSP::File* spf,
const string& filename, int32 sdid,
bool check_cache) {
2822 BESDEBUG(
"h4",
"Coming to read_dds_special_1d_grid "<<endl);
2823 bool dds_cache =
false;
2824 size_t total_bytes_dds_cache = 0;
2827 if(
true == check_cache) {
2829 total_bytes_dds_cache = HDFCFUtil::obtain_dds_cache_size(spf);
2830 BESDEBUG(
"h4",
"Total DDS cache file size is "<< total_bytes_dds_cache<<endl);
2831 if(total_bytes_dds_cache !=0)
2836 SPType sptype = OTHERHDF;
2837 const vector<HDFSP::SDField *>& spsds = spf->
getSD()->
getFields();
2840 vector<HDFSP::SDField *>::const_iterator it_g;
2841 for(it_g = spsds.begin(); it_g != spsds.end(); it_g++){
2844 switch((*it_g)->getType()) {
2845 #define HANDLE_CASE(tid, type) \
2847 bt = new (type)((*it_g)->getNewName(),filename); \
2851 HANDLE_CASE(DFNT_CHAR,
HDFStr);
2852 #ifndef SIGNED_BYTE_TO_INT32
2853 HANDLE_CASE(DFNT_INT8,
HDFByte);
2857 HANDLE_CASE(DFNT_UINT8,
HDFByte);
2862 HANDLE_CASE(DFNT_UCHAR8,
HDFByte);
2864 throw InternalErr(__FILE__,__LINE__,
"unsupported data type.");
2871 const vector<HDFSP::Dimension*>& dims= (*it_g)->getDimensions();
2873 vector<HDFSP::Dimension*>::const_iterator it_d;
2876 if(DFNT_CHAR == (*it_g)->getType()) {
2877 if(1 == (*it_g)->getRank()) {
2882 (*it_g)->getFieldRef(),
2885 (*it_g)->getNewName(),
2891 throw InternalErr(__FILE__,__LINE__,
"Unable to allocate the HDFCFStr instance.");
2893 dds.add_var(sca_str);
2903 (*it_g)->getRank() -1 ,
2907 (*it_g)->getFieldRef(),
2910 (*it_g)->getNewName(),
2916 throw InternalErr(__FILE__,__LINE__,
"Unable to allocate the HDFCFStrField instance.");
2919 for(it_d = dims.begin(); it_d != dims.begin()+dims.size()-1; it_d++)
2920 ar->append_dim((*it_d)->getSize(), (*it_d)->getName());
2931 if((*it_g)->getFieldType()!= 4) {
2936 vector<int32>dimsizes;
2938 dimsizes.resize((*it_g)->getRank());
2939 for(
int i = 0; i <(*it_g)->getRank();i++)
2940 dimsizes[i] = (int32)((dims[i])->getSize());
2945 (*it_g)->getFieldRef(),
2950 (*it_g)->getNewName(),
2955 throw InternalErr(__FILE__,__LINE__,
"Unable to allocate the HDFSPArray_RealField instance.");
2957 for(it_d = dims.begin(); it_d != dims.end(); it_d++)
2958 ar->append_dim((*it_d)->getSize(), (*it_d)->getName());
2964 if((*it_g)->getRank()!=1){
2966 throw InternalErr(__FILE__, __LINE__,
"The rank of missing Z dimension field must be 1");
2968 int nelem = ((*it_g)->getDimensions()[0])->getSize();
2976 (*it_g)->getNewName(),
2981 throw InternalErr(__FILE__,__LINE__,
2982 "Unable to allocate the HDFSPArrayMissGeoField instance.");
2986 for(it_d = dims.begin(); it_d != dims.end(); it_d++)
2987 ar->append_dim((*it_d)->getSize(), (*it_d)->getName());
2998 if(
true == dds_cache) {
3001 string md_cache_dir;
3002 string key =
"H4.Cache.metadata.path";
3009 string base_file_name = basename(filename);
3010 string dds_filename = md_cache_dir +
"/"+base_file_name +
"_dds";
3013 FILE* dds_file =fopen(dds_filename.c_str(),
"wb");
3014 if(NULL == dds_file) {
3015 string msg =
"Cannot create the cache file. " + dds_filename + get_errno();
3016 throw InternalErr(__FILE__,__LINE__,msg);
3018 int fd = fileno(dds_file);
3019 struct flock *l= lock(F_WRLCK);
3020 if (fcntl(fd, F_SETLKW, l) == -1) {
3022 string msg =
"Cannot hold the write lock for dds cached file "+ dds_filename;
3023 throw InternalErr (__FILE__, __LINE__,msg);
3027 HDFCFUtil::write_sp_sds_dds_cache(spf,dds_file,total_bytes_dds_cache,dds_filename);
3030 if (fcntl(fd, F_SETLK, lock(F_UNLCK)) == -1) {
3032 string msg =
"Cannot release the write lock for dds cached file "+ dds_filename;
3033 throw InternalErr (__FILE__, __LINE__,msg);
3037 throw InternalErr(__FILE__,__LINE__,
"Fail to generate a dds cache file.");
3039 if (fcntl(fd, F_SETLK, lock(F_UNLCK)) == -1) {
3041 string msg =
"Cannot release the write lock for dds cached file "+ dds_filename;
3042 throw InternalErr (__FILE__, __LINE__,msg);
3049 throw InternalErr (__FILE__, __LINE__,
3050 "DDS/DAS metadata cache path cannot be found when 'H4.EnableMetaDataCacheFile' key is set to be true.");
3059 void read_dds_spfields(DDS &dds,
const string& filename,
const int sdfd,
HDFSP::SDField *spsds, SPType sptype) {
3061 BESDEBUG(
"h4",
"Coming to read_dds_spfields "<<endl);
3064 if(OTHERHDF == sptype && (
true == spsds->
IsDimNoScale()))
3070 #define HANDLE_CASE(tid, type) \
3072 bt = new (type)(spsds->getNewName(),filename); \
3076 HANDLE_CASE(DFNT_CHAR,
HDFStr);
3077 #ifndef SIGNED_BYTE_TO_INT32
3078 HANDLE_CASE(DFNT_INT8,
HDFByte);
3084 HANDLE_CASE(DFNT_UINT8,
HDFByte);
3089 HANDLE_CASE(DFNT_UCHAR,
HDFByte);
3091 throw InternalErr(__FILE__,__LINE__,
"unsupported data type.");
3094 int fieldtype = spsds->getFieldType();
3100 vector<HDFSP::Dimension*>::const_iterator it_d;
3102 if(DFNT_CHAR == spsds->
getType()) {
3112 spsds->getFieldRef(),
3121 throw InternalErr(__FILE__,__LINE__,
"Unable to allocate the HDFCFStr instance.");
3123 dds.add_var(sca_str);
3136 spsds->getFieldRef(),
3145 throw InternalErr(__FILE__,__LINE__,
"Unable to allocate the HDFCFStrField instance.");
3148 for(it_d = dims.begin(); it_d != dims.begin()+dims.size()-1; it_d++)
3149 ar->append_dim((*it_d)->getSize(), (*it_d)->getName());
3158 else if(fieldtype == 0 || fieldtype == 3 ) {
3163 vector<int32>dimsizes;
3164 dimsizes.resize(spsds->
getRank());
3165 for(
int i = 0; i <spsds->
getRank();i++)
3166 dimsizes[i] = (int32)((dims[i])->getSize());
3172 spsds->getFieldRef(),
3182 throw InternalErr(__FILE__,__LINE__,
"Unable to allocate the HDFSPArray_RealField instance.");
3185 for(it_d = dims.begin(); it_d != dims.end(); it_d++)
3186 ar->append_dim((*it_d)->getSize(), (*it_d)->getName());
3193 else if(fieldtype == 1 || fieldtype == 2) {
3195 if(sptype == MODISARNSS || sptype == TRMML2_V7) {
3201 vector<int32>dimsizes;
3203 dimsizes.resize(spsds->
getRank());
3204 for(
int i = 0; i <spsds->
getRank();i++)
3205 dimsizes[i] = (dims[i])->getSize();
3211 spsds->getFieldRef(),
3221 throw InternalErr(__FILE__,__LINE__,
3222 "Unable to allocate the HDFSPArray_RealField instance.");
3226 for(it_d = dims.begin(); it_d != dims.end(); it_d++)
3227 ar->append_dim((*it_d)->getSize(), (*it_d)->getName());
3242 spsds->getFieldRef(),
3252 throw InternalErr(__FILE__,__LINE__,
3253 "Unable to allocate the HDFSPArray_RealField instance.");
3256 for(it_d = dims.begin(); it_d != dims.end(); it_d++)
3257 ar->append_dim((*it_d)->getSize(), (*it_d)->getName());
3265 else if(fieldtype == 4) {
3268 throw InternalErr(__FILE__, __LINE__,
"The rank of missing Z dimension field must be 1");
3283 throw InternalErr(__FILE__,__LINE__,
3284 "Unable to allocate the HDFSPArrayMissGeoField instance.");
3288 for(it_d = dims.begin(); it_d != dims.end(); it_d++)
3289 ar->append_dim((*it_d)->getSize(), (*it_d)->getName());
3296 else if(fieldtype == 6) {
3300 throw InternalErr(__FILE__, __LINE__,
"The rank of added coordinate variable must be 1");
3316 throw InternalErr(__FILE__,__LINE__,
3317 "Unable to allocate the HDFSPArrayAddCVField instance.");
3321 for(it_d = dims.begin(); it_d != dims.end(); it_d++)
3322 ar->append_dim((*it_d)->getSize(), (*it_d)->getName());
3329 throw InternalErr(__FILE__, __LINE__,
"The field type should be one of 0,1,2,3,4 or 6.");
3337 void read_dds_spvdfields(DDS &dds,
const string & filename,
const int fileid,int32 objref,int32 numrec,
HDFSP::VDField *spvd) {
3339 BESDEBUG(
"h4",
"Coming to read_dds_spvdfields "<<endl);
3344 #define HANDLE_CASE(tid, type) \
3346 bt = new (type)(spvd->getNewName(),filename); \
3350 HANDLE_CASE(DFNT_CHAR8,
HDFStr);
3351 #ifndef SIGNED_BYTE_TO_INT32
3352 HANDLE_CASE(DFNT_INT8,
HDFByte);
3356 HANDLE_CASE(DFNT_UINT8,
HDFByte);
3361 HANDLE_CASE(DFNT_UCHAR8,
HDFByte);
3365 throw InternalErr(__FILE__,__LINE__,
"unsupported data type.");
3372 if(DFNT_CHAR == spvd->
getType()) {
3393 throw InternalErr(__FILE__,__LINE__,
"Unable to allocate the HDFCFStr instance.");
3395 dds.add_var(sca_str);
3419 throw InternalErr(__FILE__,__LINE__,
"Unable to allocate the HDFCFStrField instance.");
3422 string dimname0 =
"VDFDim0_"+spvd->
getNewName();
3423 ar->append_dim(numrec, dimname0);
3448 string dimname1 =
"VDFDim0_"+spvd->
getNewName();
3450 string dimname2 =
"VDFDim1_"+spvd->
getNewName();
3452 ar->append_dim(numrec,dimname1);
3456 ar->append_dim(numrec,dimname1);
3468 int check_special_eosfile(
const string & filename,
string& grid_name,int32 sdfd,int32 ) {
3472 int32 n_sd_attrs = 0;
3473 bool is_eos =
false;
3477 if (SDfileinfo (sdfd, &n_sds, &n_sd_attrs) == FAIL){
3478 throw InternalErr (__FILE__,__LINE__,
"SDfileinfo failed ");
3481 char attr_name[H4_MAX_NC_NAME];
3482 int32 attr_type = -1;
3483 int32 attr_count = -1;
3484 char structmdname[] =
"StructMetadata.0";
3487 for (
int attr_index = 0; attr_index < n_sd_attrs;attr_index++) {
3488 if(SDattrinfo(sdfd,attr_index,attr_name,&attr_type,&attr_count) == FAIL) {
3489 throw InternalErr (__FILE__,__LINE__,
"SDattrinfo failed ");
3492 if(strcmp(attr_name,structmdname)==0) {
3498 if(
true == is_eos) {
3502 int32 dim_sizes[H4_MAX_VAR_DIMS];
3503 int32 sds_dtype = 0;
3504 int32 n_sds_attrs = 0;
3505 char sds_name[H4_MAX_NC_NAME];
3506 char xdim_name[] =
"XDim";
3507 char ydim_name[] =
"YDim";
3509 string temp_grid_name1;
3510 string temp_grid_name2;
3511 bool xdim_is_cv_flag =
false;
3512 bool ydim_is_cv_flag =
false;
3516 for (sds_index = 0; sds_index < (
int)n_sds; sds_index++) {
3518 sds_id = SDselect (sdfd, sds_index);
3519 if (sds_id == FAIL) {
3520 throw InternalErr (__FILE__,__LINE__,
"SDselect failed ");
3524 int status = SDgetinfo (sds_id, sds_name, &sds_rank, dim_sizes,
3525 &sds_dtype, &n_sds_attrs);
3526 if (status == FAIL) {
3527 SDendaccess(sds_id);
3528 throw InternalErr (__FILE__,__LINE__,
"SDgetinfo failed ");
3534 if(strcmp(sds_name,xdim_name) == 0) {
3535 int32 sds_dimid = SDgetdimid(sds_id,0);
3536 if(sds_dimid == FAIL) {
3537 SDendaccess(sds_id);
3538 throw InternalErr (__FILE__,__LINE__,
"SDgetinfo failed ");
3540 char dim_name[H4_MAX_NC_NAME];
3543 int32 num_dim_attrs = 0;
3544 if(SDdiminfo(sds_dimid,dim_name,&dim_size,&dim_type,&num_dim_attrs) == FAIL) {
3545 SDendaccess(sds_id);
3546 throw InternalErr(__FILE__,__LINE__,
"SDdiminfo failed ");
3551 string tempdimname(dim_name);
3552 if(tempdimname.size() >=5) {
3553 if(tempdimname.compare(0,5,
"XDim:") == 0) {
3556 temp_grid_name1 = tempdimname.substr(5);
3557 xdim_is_cv_flag =
true;
3561 else if(
"XDim" == tempdimname)
3562 xdim_is_cv_flag =
true;
3567 if(strcmp(sds_name,ydim_name) == 0) {
3569 int32 sds_dimid = SDgetdimid(sds_id,0);
3570 if(sds_dimid == FAIL) {
3571 SDendaccess (sds_id);
3572 throw InternalErr (__FILE__,__LINE__,
"SDgetinfo failed ");
3574 char dim_name[H4_MAX_NC_NAME];
3577 int32 num_dim_attrs = 0;
3578 if(SDdiminfo(sds_dimid,dim_name,&dim_size,&dim_type,&num_dim_attrs) == FAIL) {
3579 SDendaccess(sds_id);
3580 throw InternalErr(__FILE__,__LINE__,
"SDdiminfo failed ");
3585 string tempdimname(dim_name);
3586 if(tempdimname.size() >=5) {
3587 if(tempdimname.compare(0,5,
"YDim:") == 0) {
3589 temp_grid_name2 = tempdimname.substr(5);
3590 ydim_is_cv_flag =
true;
3593 else if (
"YDim" == tempdimname)
3594 ydim_is_cv_flag =
true;
3599 SDendaccess(sds_id);
3600 if((
true == xdim_is_cv_flag) && (
true == ydim_is_cv_flag ))
3606 if ((temp_grid_name1 == temp_grid_name2) && (
true == xdim_is_cv_flag) && (
true == ydim_is_cv_flag)) {
3607 grid_name = temp_grid_name1;
3615 else if((basename(filename).size() >8) && (basename(filename).compare(0,4,
"AIRS") == 0)
3616 && ((basename(filename).find(
".L3.")!=string::npos) || (basename(filename).find(
".L2.")!=string::npos))){
3618 bool has_dimscale =
false;
3621 for (sds_index = 0; sds_index < n_sds; sds_index++) {
3623 sds_id = SDselect (sdfd, sds_index);
3624 if (sds_id == FAIL) {
3625 throw InternalErr (__FILE__,__LINE__,
"SDselect failed ");
3629 int status = SDgetinfo (sds_id, sds_name, &sds_rank, dim_sizes,
3630 &sds_dtype, &n_sds_attrs);
3631 if (status == FAIL) {
3632 SDendaccess(sds_id);
3633 throw InternalErr (__FILE__,__LINE__,
"SDgetinfo failed ");
3636 for (
int dim_index = 0; dim_index<sds_rank; dim_index++) {
3638 int32 sds_dimid = SDgetdimid(sds_id,dim_index);
3639 if(sds_dimid == FAIL) {
3640 SDendaccess(sds_id);
3641 throw InternalErr (__FILE__,__LINE__,
"SDgetinfo failed ");
3644 char dim_name[H4_MAX_NC_NAME];
3647 int32 num_dim_attrs = 0;
3648 if(SDdiminfo(sds_dimid,dim_name,&dim_size,&dim_type,&num_dim_attrs) == FAIL) {
3649 SDendaccess(sds_id);
3650 throw InternalErr(__FILE__,__LINE__,
"SDdiminfo failed ");
3654 has_dimscale =
true;
3659 SDendaccess(sds_id);
3660 if(
true == has_dimscale)
3665 if (
true == has_dimscale)
3676 bool has_dimscale =
true;
3677 bool is_grid =
false;
3680 for (sds_index = 0; sds_index < n_sds; sds_index++) {
3682 sds_id = SDselect (sdid, sds_index);
3683 if (sds_id == FAIL) {
3685 throw InternalErr (__FILE__,__LINE__,
"SDselect failed ");
3689 int status = SDgetinfo (sds_id, sds_name, &sds_rank, dim_sizes,
3690 &sds_dtype, &n_sds_attrs);
3691 if (status == FAIL) {
3692 SDendaccess(sds_id);
3694 throw InternalErr (__FILE__,__LINE__,
"SDgetinfo failed ");
3698 for (
int dim_index = 0; dim_index<sds_rank; dim_index++) {
3700 int32 sds_dimid = SDgetdimid(sds_id,dim_index);
3701 if(sds_dimid == FAIL) {
3702 SDendaccess(sds_id);
3704 throw InternalErr (__FILE__,__LINE__,
"SDgetinfo failed ");
3706 char dim_name[H4_MAX_NC_NAME];
3709 int32 num_dim_attrs = 0;
3710 if(SDdiminfo(sds_dimid,dim_name,&dim_size,&dim_type,&num_dim_attrs) == FAIL) {
3711 SDendaccess(sds_id);
3713 throw InternalErr(__FILE__,__LINE__,
"SDdiminfo failed ");
3717 has_dimscale =
false;
3721 SDendaccess(sds_id);
3723 if (
true == has_dimscale)
3734 void read_das_sds(DAS & das,
const string & filename,int32 sdfd,
bool ecs_metadata,
HDFSP::File**h4fileptr) {
3739 spf->Handle_AIRS_L23();
3740 read_das_special_eos2_core(das,spf,filename,ecs_metadata);
3746 throw InternalErr(e.
what());
3755 void read_dds_sds(DDS &dds,
const string & filename,int32 sdfd,
HDFSP::File*h4file,
bool dds_setcache) {
3758 dds.set_dataset_name(basename(filename));
3759 read_dds_special_1d_grid(dds,h4file,filename,sdfd,dds_setcache);
3764 void read_dds(DDS & dds,
const string & filename)
3768 dds.set_dataset_name(basename(filename));
3769 build_descriptions(dds, das, filename);
3771 if (!dds.check_semantics()) {
3777 void read_das(DAS & das,
const string & filename)
3781 dds.set_dataset_name(basename(filename));
3783 build_descriptions(dds, das, filename);
3785 if (!dds.check_semantics()) {
3793 static void build_descriptions(DDS & dds, DAS & das,
3794 const string & filename)
3802 SDS_descriptions(sdsmap, das, filename);
3805 FileAnnot_descriptions(das, filename);
3808 Vdata_descriptions(vdatamap, das, filename);
3811 GR_descriptions(grmap, das, filename);
3814 Vgroup_descriptions(dds, das, filename, sdsmap, vdatamap, grmap);
3829 :
public binary_function < hdf_genvec &, hdf_attr, hdf_genvec & > {
3833 accum_attr(
const string & named):d_named(named) {
3839 BESDEBUG(
"h4",
"attr.name: " << attr.name << endl);
3840 if (attr.name.find(d_named) != string::npos) {
3843 stuff.assign(attr.values.data(), attr.values.size());
3844 cerr <<
"Attribute chunk: " << attr.name << endl;
3845 cerr << stuff << endl;
3847 accum.append(attr.values.number_type(), attr.values.data(),
3848 attr.values.size());
3857 struct is_named:
public unary_function < hdf_attr, bool > {
3860 is_named(
const string & named):d_named(named) {
3863 bool operator() (
const hdf_attr & attr) {
3864 return (attr.name.find(d_named) != string::npos);
3869 merge_split_eos_attributes(vector < hdf_attr > &attr_vec,
3870 const string & attr_name)
3873 if (count_if(attr_vec.begin(), attr_vec.end(), is_named(attr_name)) > 1) {
3877 attributes = accumulate(attr_vec.begin(), attr_vec.end(),
3878 attributes, accum_attr(attr_name));
3886 DBG(vector < string > s_m;
3887 attributes.print(s_m);
3888 cerr <<
"Accum struct MD: (" << s_m.size() <<
") "
3892 attr_vec.erase(remove_if(attr_vec.begin(), attr_vec.end(),
3893 is_named(attr_name)), attr_vec.end());
3897 merged_attr.name = attr_name;
3898 merged_attr.values = attributes;
3901 attr_vec.push_back(merged_attr);
3906 static void SDS_descriptions(sds_map & map, DAS & das,
3907 const string & filename)
3911 sdsin.setmeta(
true);
3915 vector < hdf_attr > fileattrs;
3920 while (!sdsin.eos()) {
3923 sdi.in_vgroup =
false;
3924 map[sdi.sds.ref] = sdi;
3931 merge_split_eos_attributes(fileattrs,
"StructMetadata");
3932 merge_split_eos_attributes(fileattrs,
"CoreMetadata");
3933 merge_split_eos_attributes(fileattrs,
"ProductMetadata");
3934 merge_split_eos_attributes(fileattrs,
"ArchiveMetadata");
3935 merge_split_eos_attributes(fileattrs,
"coremetadata");
3936 merge_split_eos_attributes(fileattrs,
"productmetadata");
3939 AddHDFAttr(das,
string(
"HDF_GLOBAL"), fileattrs);
3941 vector < hdf_attr > dattrs;
3945 for (SDSI s = map.begin(); s != map.end(); ++s) {
3946 const hdf_sds *sds = &s->second.sds;
3947 AddHDFAttr(das, sds->name, sds->attrs);
3948 for (
int k = 0; k < (
int) sds->dims.size(); ++k) {
3949 dattrs = Dims2Attrs(sds->dims[k]);
3950 AddHDFAttr(das, sds->name +
"_dim_" + num2string(k), dattrs);
3959 static void Vdata_descriptions(vd_map & map, DAS & das,
3960 const string & filename)
3966 while (!vdin.eos()) {
3969 vdi.in_vgroup =
false;
3970 map[vdi.vdata.ref] = vdi;
3975 vector < hdf_attr > dattrs;
3976 for (VDI s = map.begin(); s != map.end(); ++s) {
3978 AddHDFAttr(das, vd->name, vd->attrs);
3985 static void Vgroup_descriptions(DDS & dds, DAS & das,
3986 const string & filename, sds_map & sdmap,
3987 vd_map & vdmap, gr_map & grmap)
3994 while (!vgin.eos()) {
3997 vgi.toplevel =
true;
3998 vgmap[vgi.vgroup.ref] = vgi;
4002 for (VGI v = vgmap.begin(); v != vgmap.end(); ++v) {
4006 AddHDFAttr(das, vg->name, vg->attrs);
4009 for (uint32 i = 0; i < vg->tags.size(); i++) {
4010 int32 tag = vg->tags[i];
4011 int32 ref = vg->refs[i];
4015 if (grmap.find(ref) != grmap.end())
4016 grmap[ref].in_vgroup =
true;
4018 vgmap[ref].toplevel =
false;
4021 vdmap[ref].in_vgroup =
true;
4024 sdmap[ref].in_vgroup =
true;
4027 ERROR_LOG(
"unknown tag: " << tag <<
" ref: " << ref << endl);
4038 for (VGI v = vgmap.begin(); v != vgmap.end(); ++v) {
4039 if (!v->second.toplevel)
4041 pbt = NewStructureFromVgroup(v->second.vgroup,
4042 vgmap, sdmap, vdmap,
4052 for (SDSI s = sdmap.begin(); s != sdmap.end(); ++s) {
4053 if (s->second.in_vgroup)
4055 if (s->second.sds.has_scale())
4056 pbt = NewGridFromSDS(s->second.sds, filename);
4058 pbt = NewArrayFromSDS(s->second.sds, filename);
4066 for (VDI v = vdmap.begin(); v != vdmap.end(); ++v) {
4067 if (v->second.in_vgroup)
4069 pbt = NewSequenceFromVdata(v->second.vdata, filename);
4076 for (GRI g = grmap.begin(); g != grmap.end(); ++g) {
4077 if (g->second.in_vgroup)
4079 pbt = NewArrayFromGR(g->second.gri, filename);
4087 static void GR_descriptions(gr_map & map, DAS & das,
4088 const string & filename)
4095 vector < hdf_attr > fileattrs;
4100 while (!grin.eos()) {
4103 gri.in_vgroup =
false;
4104 map[gri.gri.ref] = gri;
4110 AddHDFAttr(das,
string(
"HDF_GLOBAL"), fileattrs);
4113 vector < hdf_attr > pattrs;
4114 for (GRI g = map.begin(); g != map.end(); ++g) {
4115 const hdf_gri *gri = &g->second.gri;
4117 AddHDFAttr(das, gri->name, gri->attrs);
4120 pattrs = Pals2Attrs(gri->palettes);
4121 AddHDFAttr(das, gri->name, pattrs);
4129 static void FileAnnot_descriptions(DAS & das,
const string & filename)
4133 vector < string > fileannots;
4135 annotin >> fileannots;
4136 AddHDFAttr(das,
string(
"HDF_GLOBAL"), fileannots);
4143 void AddHDFAttr(DAS & das,
const string & varname,
4144 const vector < hdf_attr > &hav)
4146 if (hav.size() == 0)
4150 string tempname = varname;
4151 AttrTable *atp = das.get_table(tempname);
4153 atp =
new AttrTable;
4154 atp = das.add_table(tempname, atp);
4157 vector < string > attv;
4159 for (
int i = 0; i < (
int) hav.size(); ++i) {
4161 attrtype = DAPTypeName(hav[i].values.number_type());
4163 attv = vector < string > ();
4164 hav[i].values.print(attv);
4167 for (
int j = 0; j < (
int) attv.size(); ++j) {
4169 string container_name = hav[i].name;
4170 if (container_name.find(
"StructMetadata") == 0
4171 || container_name.find(
"CoreMetadata") == 0
4172 || container_name.find(
"ProductMetadata") == 0
4173 || container_name.find(
"ArchiveMetadata") == 0
4174 || container_name.find(
"coremetadata") == 0
4175 || container_name.find(
"productmetadata") == 0) {
4176 string::size_type dotzero = container_name.find(
'.');
4177 if (dotzero != container_name.npos)
4178 container_name.erase(dotzero);
4181 AttrTable *at = das.get_table(container_name);
4183 at = das.add_table(container_name,
new AttrTable);
4186 void *buf = hdfeos_string(attv[j].c_str());
4204 if (hdfeosparse(&arg) != 0){
4205 hdfeos_delete_buffer(buf);
4206 throw Error(
"HDF-EOS parse error while processing a " + container_name +
" HDFEOS attribute.");
4210 if (arg.status() ==
false) {
4211 ERROR_LOG(
"HDF-EOS parse error while processing a "
4212 << container_name <<
" HDFEOS attribute. (2)" << endl);
4216 hdfeos_delete_buffer(buf);
4219 if (attrtype ==
"String")
4220 #ifdef ATTR_STRING_QUOTE_FIX
4221 attv[j] = escattr(attv[j]);
4223 attv[j] =
"\"" + escattr(attv[j]) +
"\"";
4226 if (atp->append_attr(hav[i].name, attrtype, attv[j]) == 0)
4237 void AddHDFAttr(DAS & das,
const string & varname,
4238 const vector < string > &anv)
4240 if (anv.size() == 0)
4245 AttrTable *atp = das.get_table(varname);
4247 atp =
new AttrTable;
4248 atp = das.add_table(varname, atp);
4252 for (
int i = 0; i < (
int) anv.size(); ++i) {
4253 #ifdef ATTR_STRING_QUOTE_FIX
4254 an = escattr(anv[i]);
4256 an =
"\"" + escattr(anv[i]) +
"\"";
4258 if (atp->append_attr(
string(
"HDF_ANNOT"),
"String", an) == 0)
4268 static vector < hdf_attr > Pals2Attrs(
const vector < hdf_palette > palv)
4270 vector < hdf_attr > pattrs;
4272 if (palv.size() != 0) {
4277 for (
int i = 0; i < (
int) palv.size(); ++i) {
4278 palname =
"hdf_palette_" + num2string(i);
4279 pattr.name = palname;
4280 pattr.values = palv[i].table;
4281 pattrs.push_back(pattr);
4282 pattr.name = palname +
"_ncomps";
4285 int32 *
>(&palv[i].ncomp), 1);
4286 pattrs.push_back(pattr);
4287 if (palv[i].name.length() != 0) {
4288 pattr.name = palname +
"_name";
4291 char *
>(palv[i].name.c_str()),
4292 palv[i].name.length());
4293 pattrs.push_back(pattr);
4302 static vector < hdf_attr > Dims2Attrs(
const hdf_dim dim)
4304 vector < hdf_attr > dattrs;
4306 if (dim.name.length() != 0) {
4307 dattr.name =
"name";
4309 hdf_genvec(DFNT_CHAR,
const_cast < char *
>(dim.name.c_str()),
4311 dattrs.push_back(dattr);
4313 if (dim.label.length() != 0) {
4314 dattr.name =
"long_name";
4316 hdf_genvec(DFNT_CHAR,
const_cast < char *
>(dim.label.c_str()),
4317 dim.label.length());
4318 dattrs.push_back(dattr);
4320 if (dim.unit.length() != 0) {
4321 dattr.name =
"units";
4323 hdf_genvec(DFNT_CHAR,
const_cast < char *
>(dim.unit.c_str()),
4325 dattrs.push_back(dattr);
4327 if (dim.format.length() != 0) {
4328 dattr.name =
"format";
4330 hdf_genvec(DFNT_CHAR,
const_cast < char *
>(dim.format.c_str()),
4331 dim.format.length());
4332 dattrs.push_back(dattr);
This class provides a way to map HDF4 1-D character array to DAP Str for the CF option.
This class provides a way to map HDFEOS2 character >1D array to DAP Str array for the CF option.
This class provides a way to map HDFEOS2 1-D character array to DAP Str for the CF option.
virtual const char * what() const
Return exception message.
const std::string & getName() const
Get the name of this field.
int32 getType() const
Get the data type of this field.
const std::string & getNewName() const
Get the CF name(special characters replaced by underscores) of this field.
int32 getRank() const
Get the dimension rank of this field.
static File * Read(const char *path, int32 sdid, int32 fileid)
Retrieve SDS and Vdata information from the HDF4 file.
bool Has_Dim_NoScale_Field() const
This file has a field that is a SDS dimension but no dimension scale.
SD * getSD() const
Public interface to Obtain SD.
const std::vector< VDATA * > & getVDATAs() const
Public interface to Obtain Vdata.
static File * Read_Hybrid(const char *path, int32 sdid, int32 fileid)
SPType getSPType() const
Obtain special HDF4 product type.
const std::vector< AttrContainer * > & getVgattrs() const
Get attributes for all vgroups.
One instance of this class represents one SDS object.
const std::vector< Dimension * > & getCorrectedDimensions() const
Get the list of the corrected dimensions.
const std::vector< Dimension * > & getDimensions() const
Get the list of dimensions.
bool IsDimNoScale() const
Is this field a dimension without dimension scale(or empty[no data]dimension variable)
This class retrieves all SDS objects and SD file attributes.
const std::vector< SDField * > & getFields() const
Redundant member function.
const std::vector< Attribute * > & getAttributes() const
Public interface to obtain the SD(file) attributes.
One instance of this class represents one Vdata field.
int32 getFieldOrder() const
Get the order of this field.
bool open(const std::string &filename, const int sd_id, const int file_id)
openes \afilename HDF4 file.
string get_metadata(const std::string &metadataname, bool &suffix_is_num, std::vector< std::string > &non_num_names, std::vector< std::string > &non_num_data)
retrieves the merged metadata.
bool write_attribute(const std::string &gname, const std::string &fname, const std::string &newfname, int n_groups, int fieldtype)
void set_DAS(libdap::DAS *das)
sets DAS pointer so that we can bulid attribute tables.
bool close()
closes the opened file.
bool write_attribute_FillValue(const std::string &varname, int type, float val)
bool write_attribute_coordinates(const std::string &varname, std::string coord)
bool write_attribute_units(const std::string &varname, std::string units)
void get_value(const std::string &s, std::string &val, bool &found)
Retrieve the value of a given key, if set.
static TheBESKeys * TheKeys()
static std::string print_attr(int32, int, void *)
Print attribute values in string.
static std::string print_type(int32)
Print datatype in string.
static void correct_scale_offset_type(libdap::AttrTable *at)
static std::string get_CF_string(std::string s)
Change special characters to "_".
static std::string escattr(std::string s)
static void correct_fvalue_type(libdap::AttrTable *at, int32 dtype)