6 #include"HDFSPArray_RealField.h"
7 #include"HDFSPArrayMissField.h"
8 #include"HDFEOS2GeoCFProj.h"
9 #include"HDFEOS2GeoCF1D.h"
13 #define SIGNED_BYTE_TO_INT32 1
18 #include "HDFUInt16.h"
20 #include "HDFUInt32.h"
21 #include "HDFFloat32.h"
22 #include "HDFFloat64.h"
24 #include "HDF4RequestHandler.h"
29 #define ERR_LOC1(x) #x
30 #define ERR_LOC2(x) ERR_LOC1(x)
31 #define ERR_LOC __FILE__ " : " ERR_LOC2(__LINE__)
41 HDFCFUtil::check_beskeys(
const string& key) {
45 const string dosettrue =
"true";
46 const string dosetyes =
"yes";
51 if( dosettrue == doset || dosetyes == doset )
64 split_helper(vector<string> &tokens,
const string &text,
const char sep)
66 string::size_type start = 0, end = 0;
67 while ((end = text.find(sep, start)) != string::npos) {
68 tokens.push_back(text.substr(start, end - start));
71 tokens.push_back(text.substr(start));
80 split_helper(names,
string(s, len), sep);
84 for (
int i = 0, j = 0; j <= len; ++j) {
85 if ((j == len && len) || s[j] == sep) {
86 string elem(s + i, j - i);
87 names.push_back(elem);
103 DBG(std::cerr <<
"HDFCFUtil::Split: sz: <" << sz <<
">, sep: <" << sep <<
">" << std::endl);
105 split_helper(names,
string(sz), sep);
109 Split(sz, (
int)strlen(sz), sep, names);
117 HDFCFUtil::Split(
const char *s,
int len,
char sep, std::vector<std::string> &names)
120 for (
int i = 0, j = 0; j <= len; ++j) {
121 if ((j == len && len) || s[j] == sep) {
122 string elem(s + i, j - i);
123 names.push_back(elem);
134 Split(sz, (
int)strlen(sz), sep, names);
147 pair<map<string,string>::iterator,
bool> ret;
148 ret = m.insert(make_pair(key, val));
149 if(ret.second ==
false){
151 ret = m.insert(make_pair(key, val));
152 if(ret.second ==
false){
153 BESDEBUG(
"h4",
"insert_map():insertion failed on Key=" << key <<
" Val=" << val << endl);
166 string insertString(1,
'_');
169 if (
true == isdigit(s[0]))
170 s.insert(0,insertString);
176 for(
unsigned int i=0; i < s.length(); i++)
177 if((
false == isalnum(s[i])) && (s[i]!=
'_'))
193 pair<set<string>::iterator,
bool> ret;
195 stringstream sclash_index;
196 sclash_index << clash_index;
197 newstr = str + sclash_index.str();
199 ret = namelist.insert(newstr);
200 if (
false == ret.second) {
202 gen_unique_name(str,namelist,clash_index);
215 pair<set<string>::iterator,
bool> setret;
216 set<string>::iterator iss;
218 vector<string> clashnamelist;
219 vector<string>::iterator ivs;
222 map<int,int> cl_to_ol;
226 vector<string>::const_iterator irv;
228 for (irv = newobjnamelist.begin(); irv != newobjnamelist.end(); ++irv) {
229 setret = objnameset.insert(*irv);
230 if (
false == setret.second ) {
231 clashnamelist.insert(clashnamelist.end(),(*irv));
232 cl_to_ol[cl_index] = ol_index;
240 for (ivs=clashnamelist.begin(); ivs!=clashnamelist.end(); ivs++) {
242 string temp_clashname = *ivs +
'_';
244 *ivs = temp_clashname;
248 for (
unsigned int i =0; i <clashnamelist.size(); i++)
249 newobjnamelist[cl_to_ol[i]] = clashnamelist[i];
259 set<string> objnameset;
260 Handle_NameClashing(newobjnamelist,objnameset);
287 gp.ucp = (
unsigned char *) vals;
297 gp.cp = (
char *) vals;
308 return escattr(
static_cast<const char*
>(vals));
313 gp.sp = (
short *) vals;
320 gp.usp = (
unsigned short *) vals;
321 rep << *(gp.usp+loc);
327 gp.lp = (int32 *) vals;
334 gp.ui = (
unsigned int *) vals;
341 float attr_val = *(
float*)vals;
342 bool is_a_fin = isfinite(attr_val);
343 gp.fp = (
float *) vals;
348 rep << setprecision(10);
350 string tmp_rep_str = rep.str();
351 if (tmp_rep_str.find(
'.') == string::npos
352 && tmp_rep_str.find(
'e') == string::npos
353 && tmp_rep_str.find(
'E') == string::npos) {
363 double attr_val = *(
double*)vals;
364 bool is_a_fin = isfinite(attr_val);
365 gp.dp = (
double *) vals;
366 rep << std::showpoint;
367 rep << std::setprecision(17);
369 string tmp_rep_str = rep.str();
370 if (tmp_rep_str.find(
'.') == string::npos
371 && tmp_rep_str.find(
'e') == string::npos
372 && tmp_rep_str.find(
'E') == string::npos) {
379 return string(
"UNKNOWN");
390 static const char UNKNOWN[]=
"Unknown";
391 static const char BYTE[]=
"Byte";
392 static const char INT16[]=
"Int16";
393 static const char UINT16[]=
"UInt16";
394 static const char INT32[]=
"Int32";
395 static const char UINT32[]=
"UInt32";
396 static const char FLOAT32[]=
"Float32";
397 static const char FLOAT64[]=
"Float64";
398 static const char STRING[]=
"String";
415 #ifndef SIGNED_BYTE_TO_INT32
457 return sizeof(
unsigned char);
460 return sizeof(
unsigned char);
465 #ifndef SIGNED_BYTE_TO_INT32
472 return sizeof(
unsigned short);
475 return sizeof(short);
481 return sizeof(
unsigned int);
484 return sizeof(float);
487 return sizeof(double);
496 template <
typename T >
497 void HDFCFUtil::LatLon2DSubset (T * outlatlon,
510 T (*templatlonptr)[majordim][minordim] = (typeof templatlonptr) latlon;
512 T (*templatlonptr)[majordim][minordim] = (T *) latlon;
517 int dim0count = count[0];
518 int dim1count = count[1];
519 int dim0index[dim0count], dim1index[dim1count];
521 for (i = 0; i < count[0]; i++)
522 dim0index[i] = offset[0] + i * step[0];
525 for (j = 0; j < count[1]; j++)
526 dim1index[j] = offset[1] + j * step[1];
531 for (i = 0; i < count[0]; i++) {
532 for (j = 0; j < count[1]; j++) {
533 outlatlon[k] = (*templatlonptr)[dim0index[i]][dim1index[j]];
546 AttrTable::Attr_iter it = at->attr_begin();
547 bool find_fvalue =
false;
548 while (it!=at->attr_end() &&
false==find_fvalue) {
549 if (at->get_name(it) ==
"_FillValue")
552 string fillvalue =
"";
553 string fillvalue_type =
"";
554 if((*at->get_attr_vector(it)).size() !=1)
555 throw InternalErr(__FILE__,__LINE__,
"The number of _FillValue must be 1.");
556 fillvalue = (*at->get_attr_vector(it)->begin());
557 fillvalue_type = at->get_type(it);
560 if(fillvalue_type != var_type){
562 at->del_attr(
"_FillValue");
564 if (fillvalue_type ==
"String") {
568 if(fillvalue.size() >1) {
570 long int fillvalue_int = 0;
571 vector<char> fillvalue_temp(fillvalue.size());
573 fillvalue_int = strtol((fillvalue.substr(1)).c_str(),&pEnd,8);
574 stringstream convert_str;
575 convert_str << fillvalue_int;
576 at->append_attr(
"_FillValue",var_type,convert_str.str());
585 short fillvalue_int = fillvalue.at(0);
587 stringstream convert_str;
588 convert_str << fillvalue_int;
589 if(fillvalue_int <0 || fillvalue_int >128)
590 throw InternalErr(__FILE__,__LINE__,
591 "If the fillvalue is a char type, the value must be between 0 and 128.");
594 at->append_attr(
"_FillValue",var_type,convert_str.str());
599 at->append_attr(
"_FillValue",var_type,fillvalue);
613 AttrTable::Attr_iter it = at->attr_begin();
614 bool find_scale =
false;
615 bool find_offset =
false;
618 string scale_factor_type;
619 string add_offset_type;
620 string scale_factor_value;
621 string add_offset_value;
623 while (it!=at->attr_end() &&((find_scale!=
true) ||(find_offset!=
true))) {
624 if (at->get_name(it) ==
"scale_factor")
627 scale_factor_value = (*at->get_attr_vector(it)->begin());
628 scale_factor_type = at->get_type(it);
631 if(at->get_name(it)==
"add_offset")
634 add_offset_value = (*at->get_attr_vector(it)->begin());
635 add_offset_type = at->get_type(it);
642 if((
true==find_scale) && (
true==find_offset)) {
643 if(scale_factor_type != add_offset_type) {
644 at->del_attr(
"add_offset");
645 at->append_attr(
"add_offset",scale_factor_type,add_offset_value);
651 #ifdef USE_HDFEOS2_LIB
657 bool HDFCFUtil::is_special_value(int32 dtype,
float fillvalue,
float realvalue) {
659 bool ret_value =
false;
661 if (DFNT_UINT16 == dtype) {
663 int fillvalue_int = (int)fillvalue;
664 if (MAX_NON_SCALE_SPECIAL_VALUE == fillvalue_int) {
665 int realvalue_int = (int)realvalue;
666 if (realvalue_int <= MAX_NON_SCALE_SPECIAL_VALUE && realvalue_int >=MIN_NON_SCALE_SPECIAL_VALUE)
681 int HDFCFUtil::check_geofile_dimmap(
const string & geofilename) {
683 int32 fileid = SWopen(
const_cast<char*
>(geofilename.c_str()),DFACC_READ);
686 string swathname =
"MODIS_Swath_Type_GEO";
687 int32 datasetid = SWattach(fileid,
const_cast<char*
>(swathname.c_str()));
697 if ((nummaps = SWnentries(datasetid, HDFE_NENTMAP, &bufsize)) == -1) {
720 bool HDFCFUtil::change_data_type(DAS & das, SOType scaletype,
const string &new_field_name)
723 AttrTable *at = das.get_table(new_field_name);
731 if(scaletype!=DEFAULT_CF_EQU && at!=NULL)
733 AttrTable::Attr_iter it = at->attr_begin();
734 string scale_factor_value=
"";
735 string add_offset_value=
"0";
736 string radiance_scales_value=
"";
737 string radiance_offsets_value=
"";
738 string reflectance_scales_value=
"";
739 string reflectance_offsets_value=
"";
740 string scale_factor_type, add_offset_type;
741 while (it!=at->attr_end())
743 if(at->get_name(it)==
"radiance_scales")
744 radiance_scales_value = *(at->get_attr_vector(it)->begin());
745 if(at->get_name(it)==
"radiance_offsets")
746 radiance_offsets_value = *(at->get_attr_vector(it)->begin());
747 if(at->get_name(it)==
"reflectance_scales")
748 reflectance_scales_value = *(at->get_attr_vector(it)->begin());
749 if(at->get_name(it)==
"reflectance_offsets")
750 reflectance_offsets_value = *(at->get_attr_vector(it)->begin());
755 if(at->get_name(it).find(
"scale_factor")!=string::npos){
756 string temp_attr_name = at->get_name(it);
757 if (temp_attr_name !=
"scale_factor_err") {
758 scale_factor_value = *(at->get_attr_vector(it)->begin());
759 scale_factor_type = at->get_type(it);
762 if(at->get_name(it).find(
"add_offset")!=string::npos)
764 string temp_attr_name = at->get_name(it);
765 if (temp_attr_name !=
"add_offset_err") {
766 add_offset_value = *(at->get_attr_vector(it)->begin());
767 add_offset_type = at->get_type(it);
773 if((radiance_scales_value.length()!=0 && radiance_offsets_value.length()!=0)
774 || (reflectance_scales_value.length()!=0 && reflectance_offsets_value.length()!=0))
777 if(scale_factor_value.length()!=0)
779 if(!(atof(scale_factor_value.c_str())==1 && atof(add_offset_value.c_str())==0))
788 void HDFCFUtil::obtain_dimmap_info(
const string& filename,HDFEOS2::Dataset*dataset,
789 vector<struct dimmap_entry> & dimmaps,
790 string & modis_geofilename,
bool& geofile_has_dimmap) {
793 HDFEOS2::SwathDataset *sw =
static_cast<HDFEOS2::SwathDataset *
>(dataset);
794 const vector<HDFEOS2::SwathDataset::DimensionMap*>& origdimmaps = sw->getDimensionMaps();
795 vector<HDFEOS2::SwathDataset::DimensionMap*>::const_iterator it_dmap;
799 for(
size_t i=0;i<origdimmaps.size();i++){
800 tempdimmap.geodim = origdimmaps[i]->getGeoDimension();
801 tempdimmap.datadim = origdimmaps[i]->getDataDimension();
802 tempdimmap.offset = origdimmaps[i]->getOffset();
803 tempdimmap.inc = origdimmaps[i]->getIncrement();
804 dimmaps.push_back(tempdimmap);
808 string check_modis_geofile_key =
"H4.EnableCheckMODISGeoFile";
809 bool check_geofile_key =
false;
810 check_geofile_key = HDFCFUtil::check_beskeys(check_modis_geofile_key);
815 if((origdimmaps.size() != 0) && (
true == HDF4RequestHandler::get_enable_check_modis_geo_file()) ) {
819 tempcstr =
new char [filename.size()+1];
820 strncpy (tempcstr,filename.c_str(),filename.size());
821 string basefilename = basename(tempcstr);
822 string dirfilename = dirname(tempcstr);
826 bool is_modis_geofile =
false;
827 if(basefilename.size() >5) {
828 if((0 == basefilename.compare(0,5,
"MOD03")) || (0 == basefilename.compare(0,5,
"MYD03")))
829 is_modis_geofile =
true;
848 if ((
false == is_modis_geofile) && (basefilename.size() >3)) {
850 string fnameprefix = basefilename.substr(0,3);
852 if(fnameprefix ==
"MYD" || fnameprefix ==
"MOD") {
853 size_t fnamemidpos = basefilename.find(
".A");
854 if(fnamemidpos != string::npos) {
855 string fnamemiddle = basefilename.substr(fnamemidpos,14);
856 if(fnamemiddle.size()==14) {
857 string geofnameprefix = fnameprefix+
"03";
859 string geofnamefp = geofnameprefix + fnamemiddle;
864 dirp = opendir(dirfilename.c_str());
866 throw InternalErr(__FILE__,__LINE__,
"opendir fails.");
868 while ((dirs = readdir(dirp))!= NULL){
869 if(strncmp(dirs->d_name,geofnamefp.c_str(),geofnamefp.size())==0){
870 modis_geofilename = dirfilename +
"/"+ dirs->d_name;
871 int num_dimmap = HDFCFUtil::check_geofile_dimmap(modis_geofilename);
872 if (num_dimmap < 0) {
874 throw InternalErr(__FILE__,__LINE__,
"this file is not a MODIS geolocation file.");
876 geofile_has_dimmap = (num_dimmap >0)?
true:
false;
891 bool HDFCFUtil::is_modis_dimmap_nonll_field(
string & fieldname) {
893 bool modis_dimmap_nonll_field =
false;
894 vector<string> modis_dimmap_nonll_fieldlist;
896 modis_dimmap_nonll_fieldlist.push_back(
"Height");
897 modis_dimmap_nonll_fieldlist.push_back(
"SensorZenith");
898 modis_dimmap_nonll_fieldlist.push_back(
"SensorAzimuth");
899 modis_dimmap_nonll_fieldlist.push_back(
"Range");
900 modis_dimmap_nonll_fieldlist.push_back(
"SolarZenith");
901 modis_dimmap_nonll_fieldlist.push_back(
"SolarAzimuth");
902 modis_dimmap_nonll_fieldlist.push_back(
"Land/SeaMask");
903 modis_dimmap_nonll_fieldlist.push_back(
"gflags");
904 modis_dimmap_nonll_fieldlist.push_back(
"Solar_Zenith");
905 modis_dimmap_nonll_fieldlist.push_back(
"Solar_Azimuth");
906 modis_dimmap_nonll_fieldlist.push_back(
"Sensor_Azimuth");
907 modis_dimmap_nonll_fieldlist.push_back(
"Sensor_Zenith");
909 map<string,string>modis_field_to_geofile_field;
910 map<string,string>::iterator itmap;
911 modis_field_to_geofile_field[
"Solar_Zenith"] =
"SolarZenith";
912 modis_field_to_geofile_field[
"Solar_Azimuth"] =
"SolarAzimuth";
913 modis_field_to_geofile_field[
"Sensor_Zenith"] =
"SensorZenith";
914 modis_field_to_geofile_field[
"Solar_Azimuth"] =
"SolarAzimuth";
916 for (
unsigned int i = 0; i <modis_dimmap_nonll_fieldlist.size(); i++) {
918 if (fieldname == modis_dimmap_nonll_fieldlist[i]) {
919 itmap = modis_field_to_geofile_field.find(fieldname);
920 if (itmap !=modis_field_to_geofile_field.end())
921 fieldname = itmap->second;
922 modis_dimmap_nonll_field =
true;
927 return modis_dimmap_nonll_field;
930 void HDFCFUtil::add_scale_offset_attrs(AttrTable*at,
const std::string& s_type,
float svalue_f,
double svalue_d,
bool add_offset_found,
931 const std::string& o_type,
float ovalue_f,
double ovalue_d) {
932 at->del_attr(
"scale_factor");
934 if(s_type!=
"Float64") {
936 at->append_attr(
"scale_factor",
"Float32", print_rep);
940 at->append_attr(
"scale_factor",
"Float64", print_rep);
943 if (
true == add_offset_found) {
944 at->del_attr(
"add_offset");
945 if(o_type!=
"Float64") {
947 at->append_attr(
"add_offset",
"Float32", print_rep);
951 at->append_attr(
"add_offset",
"Float64", print_rep);
956 void HDFCFUtil::add_scale_str_offset_attrs(AttrTable*at,
const std::string& s_type,
const std::string& s_value_str,
bool add_offset_found,
957 const std::string& o_type,
float ovalue_f,
double ovalue_d) {
958 at->del_attr(
"scale_factor");
960 if(s_type!=
"Float64")
961 at->append_attr(
"scale_factor",
"Float32", s_value_str);
963 at->append_attr(
"scale_factor",
"Float64", s_value_str);
965 if (
true == add_offset_found) {
966 at->del_attr(
"add_offset");
967 if(o_type!=
"Float64") {
969 at->append_attr(
"add_offset",
"Float32", print_rep);
973 at->append_attr(
"add_offset",
"Float64", print_rep);
978 void HDFCFUtil::handle_modis_special_attrs_disable_scale_comp(AttrTable *at,
979 const string &filename,
981 const string & newfname,
985 string scale_factor_type;
986 string add_offset_type;
989 string scale_factor_value=
"";
990 float orig_scale_value_float = 1;
991 float orig_scale_value_double = 1;
992 string add_offset_value=
"0";
993 float orig_offset_value_float = 0;
994 float orig_offset_value_double = 0;
995 bool add_offset_found =
false;
1000 AttrTable::Attr_iter it = at->attr_begin();
1001 while (it!=at->attr_end())
1003 if(at->get_name(it)==
"scale_factor")
1005 scale_factor_value = (*at->get_attr_vector(it)->begin());
1006 scale_factor_type = at->get_type(it);
1007 if(scale_factor_type ==
"Float64")
1008 orig_scale_value_double=atof(scale_factor_value.c_str());
1010 orig_scale_value_float = atof(scale_factor_value.c_str());
1013 if(at->get_name(it)==
"add_offset")
1015 add_offset_value = (*at->get_attr_vector(it)->begin());
1016 add_offset_type = at->get_type(it);
1018 if(add_offset_type ==
"Float64")
1019 orig_offset_value_double = atof(add_offset_value.c_str());
1021 orig_offset_value_float = atof(add_offset_value.c_str());
1022 add_offset_found =
true;
1048 if(scale_factor_value.length()!=0) {
1049 if (MODIS_EQ_SCALE == sotype || MODIS_MUL_SCALE == sotype) {
1050 if (orig_scale_value_float > 1 || orig_scale_value_double >1) {
1052 bool need_change_scale =
true;
1053 if(
true == is_grid) {
1054 if ((filename.size() >5) && ((filename.compare(0,5,
"MOD09") == 0)|| (filename.compare(0,5,
"MYD09")==0))) {
1055 if ((newfname.size() >5) && newfname.find(
"Range") != string::npos)
1056 need_change_scale =
false;
1058 else if((filename.size() >7)&&((filename.compare(0,7,
"MOD16A2") == 0)|| (filename.compare(0,7,
"MYD16A2")==0) ||
1059 (filename.compare(0,7,
"MOD16A3")==0) || (filename.compare(0,7,
"MYD16A3")==0)))
1060 need_change_scale =
false;
1062 if(
true == need_change_scale) {
1063 sotype = MODIS_DIV_SCALE;
1064 (*BESLog::TheLog())<<
"The field " << newfname <<
" scale factor is "<< scale_factor_value << endl
1065 <<
" But the original scale factor type is MODIS_MUL_SCALE or MODIS_EQ_SCALE. " << endl
1066 <<
" Now change it to MODIS_DIV_SCALE. "<<endl;
1071 if (MODIS_DIV_SCALE == sotype) {
1072 if (orig_scale_value_float < 1 || orig_scale_value_double<1) {
1073 sotype = MODIS_MUL_SCALE;
1074 (*BESLog::TheLog())<<
"The field " << newfname <<
" scale factor is "<< scale_factor_value << endl
1075 <<
" But the original scale factor type is MODIS_DIV_SCALE. " << endl
1076 <<
" Now change it to MODIS_MUL_SCALE. "<<endl;
1081 if ((MODIS_MUL_SCALE == sotype) &&(
true == add_offset_found)) {
1083 float new_offset_value_float=0;
1084 double new_offset_value_double=0;
1085 if(add_offset_type!=
"Float64")
1086 new_offset_value_float = (orig_offset_value_float ==0)?0:(-1 * orig_offset_value_float *orig_scale_value_float);
1088 new_offset_value_double = (orig_offset_value_double ==0)?0:(-1 * orig_offset_value_double *orig_scale_value_double);
1091 add_scale_str_offset_attrs(at,scale_factor_type,scale_factor_value,add_offset_found,
1092 add_offset_type,new_offset_value_float,new_offset_value_double);
1096 if (MODIS_DIV_SCALE == sotype) {
1098 float new_scale_value_float=1;
1099 double new_scale_value_double=1;
1100 float new_offset_value_float=0;
1101 double new_offset_value_double=0;
1104 if(scale_factor_type !=
"Float64") {
1105 new_scale_value_float = 1.0/orig_scale_value_float;
1106 if (
true == add_offset_found) {
1107 if(add_offset_type !=
"Float64")
1108 new_offset_value_float = (orig_offset_value_float==0)?0:(-1 * orig_offset_value_float *new_scale_value_float);
1110 new_offset_value_double = (orig_offset_value_double==0)?0:(-1 * orig_offset_value_double *new_scale_value_float);
1115 new_scale_value_double = 1.0/orig_scale_value_double;
1116 if (
true == add_offset_found) {
1117 if(add_offset_type !=
"Float64")
1118 new_offset_value_float = (orig_offset_value_float==0)?0:(-1 * orig_offset_value_float *new_scale_value_double);
1120 new_offset_value_double = (orig_offset_value_double==0)?0:(-1 * orig_offset_value_double *new_scale_value_double);
1124 add_scale_offset_attrs(at,scale_factor_type,new_scale_value_float,new_scale_value_double,add_offset_found,
1125 add_offset_type,new_offset_value_float,new_offset_value_double);
1149 void HDFCFUtil::handle_modis_special_attrs(AttrTable *at,
const string & filename,
1150 bool is_grid,
const string & newfname,
1151 SOType sotype,
bool gridname_change_valid_range,
1152 bool changedtype,
bool & change_fvtype){
1155 string scale_factor_type;
1156 string add_offset_type;
1157 string fillvalue_type;
1158 string valid_range_type;
1162 string scale_factor_value=
"";
1163 float orig_scale_value = 1;
1164 string add_offset_value=
"0";
1165 float orig_offset_value = 0;
1166 bool add_offset_found =
false;
1169 string fillvalue=
"";
1172 string valid_range_value=
"";
1174 bool has_valid_range =
false;
1177 float orig_valid_min = 0;
1178 float orig_valid_max = 0;
1181 string number_type_value=
"";
1182 string number_type_dap_type=
"";
1186 AttrTable::Attr_iter it = at->attr_begin();
1187 while (it!=at->attr_end())
1189 if(at->get_name(it)==
"scale_factor")
1191 scale_factor_value = (*at->get_attr_vector(it)->begin());
1192 orig_scale_value = atof(scale_factor_value.c_str());
1193 scale_factor_type = at->get_type(it);
1196 if(at->get_name(it)==
"add_offset")
1198 add_offset_value = (*at->get_attr_vector(it)->begin());
1199 orig_offset_value = atof(add_offset_value.c_str());
1200 add_offset_type = at->get_type(it);
1201 add_offset_found =
true;
1204 if(at->get_name(it)==
"_FillValue")
1206 fillvalue = (*at->get_attr_vector(it)->begin());
1207 fillvalue_type = at->get_type(it);
1210 if(at->get_name(it)==
"valid_range")
1212 vector<string> *avalue = at->get_attr_vector(it);
1213 vector<string>::iterator ait = avalue->begin();
1214 while(ait!=avalue->end())
1216 valid_range_value += *ait;
1218 if(ait!=avalue->end())
1219 valid_range_value +=
", ";
1221 valid_range_type = at->get_type(it);
1222 if (
false == gridname_change_valid_range) {
1223 orig_valid_min = (float)(atof((avalue->at(0)).c_str()));
1224 orig_valid_max = (float)(atof((avalue->at(1)).c_str()));
1226 has_valid_range =
true;
1229 if(
true == changedtype && (at->get_name(it)==
"Number_Type"))
1231 number_type_value = (*at->get_attr_vector(it)->begin());
1232 number_type_dap_type= at->get_type(it);
1240 if(scale_factor_value.length()!=0)
1242 if(!(atof(scale_factor_value.c_str())==1 && atof(add_offset_value.c_str())==0))
1244 at->del_attr(
"scale_factor");
1245 at->append_attr(
"orig_scale_factor", scale_factor_type, scale_factor_value);
1246 if(add_offset_found)
1248 at->del_attr(
"add_offset");
1249 at->append_attr(
"orig_add_offset", add_offset_type, add_offset_value);
1255 if(
true == changedtype && fillvalue.length()!=0 && fillvalue_type!=
"Float32" && fillvalue_type!=
"Float64")
1257 change_fvtype =
true;
1258 at->del_attr(
"_FillValue");
1259 at->append_attr(
"_FillValue",
"Float32", fillvalue);
1262 float valid_max = 0;
1263 float valid_min = 0;
1265 it = at->attr_begin();
1266 bool handle_modis_l1b =
false;
1271 if (sotype == MODIS_MUL_SCALE &&
true ==changedtype) {
1274 string emissive_str =
"Emissive";
1275 string RefSB_str =
"RefSB";
1276 bool is_emissive_field =
false;
1277 bool is_refsb_field =
false;
1278 if(newfname.find(emissive_str)!=string::npos) {
1279 if(0 == newfname.compare(newfname.size()-emissive_str.size(),emissive_str.size(),emissive_str))
1280 is_emissive_field =
true;
1283 if(newfname.find(RefSB_str)!=string::npos) {
1284 if(0 == newfname.compare(newfname.size()-RefSB_str.size(),RefSB_str.size(),RefSB_str))
1285 is_refsb_field =
true;
1289 if ((
true == is_emissive_field) || (
true== is_refsb_field)){
1291 float scale_max = 0;
1292 float scale_min = 100000;
1294 float offset_max = 0;
1295 float offset_min = 0;
1297 float temp_var_val = 0;
1299 string orig_long_name_value;
1300 string modify_long_name_value;
1301 string str_removed_from_long_name=
" Scaled Integers";
1302 string radiance_units_value;
1304 while (it!=at->attr_end())
1307 if(
true == is_emissive_field) {
1309 if (
"radiance_scales" == (at->get_name(it))) {
1310 vector<string> *avalue = at->get_attr_vector(it);
1311 for (vector<string>::const_iterator ait = avalue->begin();ait !=avalue->end();++ait) {
1312 temp_var_val = (float)(atof((*ait).c_str()));
1313 if (temp_var_val > scale_max)
1314 scale_max = temp_var_val;
1315 if (temp_var_val < scale_min)
1316 scale_min = temp_var_val;
1320 if (
"radiance_offsets" == (at->get_name(it))) {
1321 vector<string> *avalue = at->get_attr_vector(it);
1322 for (vector<string>::const_iterator ait = avalue->begin();ait !=avalue->end();++ait) {
1323 temp_var_val = (float)(atof((*ait).c_str()));
1324 if (temp_var_val > offset_max)
1325 offset_max = temp_var_val;
1326 if (temp_var_val < scale_min)
1327 offset_min = temp_var_val;
1331 if (
"long_name" == (at->get_name(it))) {
1332 orig_long_name_value = (*at->get_attr_vector(it)->begin());
1333 if (orig_long_name_value.find(str_removed_from_long_name)!=string::npos) {
1334 if(0 == orig_long_name_value.compare(orig_long_name_value.size()-str_removed_from_long_name.size(),
1335 str_removed_from_long_name.size(),str_removed_from_long_name)) {
1337 modify_long_name_value =
1338 orig_long_name_value.substr(0,orig_long_name_value.size()-str_removed_from_long_name.size());
1339 at->del_attr(
"long_name");
1340 at->append_attr(
"long_name",
"String",modify_long_name_value);
1341 at->append_attr(
"orig_long_name",
"String",orig_long_name_value);
1346 if (
"radiance_units" == (at->get_name(it)))
1347 radiance_units_value = (*at->get_attr_vector(it)->begin());
1350 if (
true == is_refsb_field) {
1351 if (
"reflectance_scales" == (at->get_name(it))) {
1352 vector<string> *avalue = at->get_attr_vector(it);
1353 for (vector<string>::const_iterator ait = avalue->begin();ait !=avalue->end();++ait) {
1354 temp_var_val = (float)(atof((*ait).c_str()));
1355 if (temp_var_val > scale_max)
1356 scale_max = temp_var_val;
1357 if (temp_var_val < scale_min)
1358 scale_min = temp_var_val;
1362 if (
"reflectance_offsets" == (at->get_name(it))) {
1364 vector<string> *avalue = at->get_attr_vector(it);
1365 for (vector<string>::const_iterator ait = avalue->begin();ait !=avalue->end();++ait) {
1366 temp_var_val = (float)(atof((*ait).c_str()));
1367 if (temp_var_val > offset_max)
1368 offset_max = temp_var_val;
1369 if (temp_var_val < scale_min)
1370 offset_min = temp_var_val;
1374 if (
"long_name" == (at->get_name(it))) {
1375 orig_long_name_value = (*at->get_attr_vector(it)->begin());
1376 if (orig_long_name_value.find(str_removed_from_long_name)!=string::npos) {
1377 if(0 == orig_long_name_value.compare(orig_long_name_value.size()-str_removed_from_long_name.size(),
1378 str_removed_from_long_name.size(),str_removed_from_long_name)) {
1380 modify_long_name_value =
1381 orig_long_name_value.substr(0,orig_long_name_value.size()-str_removed_from_long_name.size());
1382 at->del_attr(
"long_name");
1383 at->append_attr(
"long_name",
"String",modify_long_name_value);
1384 at->append_attr(
"orig_long_name",
"String",orig_long_name_value);
1394 throw InternalErr(__FILE__,__LINE__,
"the scale factor should always be greater than 0.");
1396 if (orig_valid_max > offset_min)
1397 valid_max = (orig_valid_max-offset_min)*scale_max;
1399 valid_max = (orig_valid_max-offset_min)*scale_min;
1401 if (orig_valid_min > offset_max)
1402 valid_min = (orig_valid_min-offset_max)*scale_min;
1404 valid_min = (orig_valid_min -offset_max)*scale_max;
1411 at->append_attr(
"valid_min",
"Float32",print_rep);
1413 at->append_attr(
"valid_max",
"Float32",print_rep);
1414 at->del_attr(
"valid_range");
1415 handle_modis_l1b =
true;
1418 if (
true == is_emissive_field && radiance_units_value.size() >0) {
1419 at->del_attr(
"units");
1420 at->append_attr(
"units",
"String",radiance_units_value);
1426 if(
true == changedtype &&
true == has_valid_range &&
false == handle_modis_l1b) {
1429 if (
true == gridname_change_valid_range)
1430 HDFCFUtil::handle_modis_vip_special_attrs(valid_range_value,scale_factor_value,valid_min,valid_max);
1431 else if(scale_factor_value.length()!=0) {
1450 if (MODIS_EQ_SCALE == sotype || MODIS_MUL_SCALE == sotype) {
1451 if (orig_scale_value > 1) {
1453 bool need_change_scale =
true;
1454 if(
true == is_grid) {
1455 if ((filename.size() >5) && ((filename.compare(0,5,
"MOD09") == 0)|| (filename.compare(0,5,
"MYD09")==0))) {
1456 if ((newfname.size() >5) && newfname.find(
"Range") != string::npos)
1457 need_change_scale =
false;
1460 else if((filename.size() >7)&&((filename.compare(0,7,
"MOD16A2") == 0)|| (filename.compare(0,7,
"MYD16A2")==0) ||
1461 (filename.compare(0,7,
"MOD16A3")==0) || (filename.compare(0,7,
"MYD16A3")==0)))
1462 need_change_scale =
false;
1464 if(
true == need_change_scale) {
1465 sotype = MODIS_DIV_SCALE;
1466 (*BESLog::TheLog())<<
"The field " << newfname <<
" scale factor is "<< orig_scale_value << endl
1467 <<
" But the original scale factor type is MODIS_MUL_SCALE or MODIS_EQ_SCALE. " << endl
1468 <<
" Now change it to MODIS_DIV_SCALE. "<<endl;
1473 if (MODIS_DIV_SCALE == sotype) {
1474 if (orig_scale_value < 1) {
1475 sotype = MODIS_MUL_SCALE;
1476 (*BESLog::TheLog())<<
"The field " << newfname <<
" scale factor is "<< orig_scale_value << endl
1477 <<
" But the original scale factor type is MODIS_DIV_SCALE. " << endl
1478 <<
" Now change it to MODIS_MUL_SCALE. "<<endl;
1482 if(sotype == MODIS_MUL_SCALE) {
1483 valid_min = (orig_valid_min - orig_offset_value)*orig_scale_value;
1484 valid_max = (orig_valid_max - orig_offset_value)*orig_scale_value;
1486 else if (sotype == MODIS_DIV_SCALE) {
1487 valid_min = (orig_valid_min - orig_offset_value)/orig_scale_value;
1488 valid_max = (orig_valid_max - orig_offset_value)/orig_scale_value;
1490 else if (sotype == MODIS_EQ_SCALE) {
1491 valid_min = orig_valid_min * orig_scale_value + orig_offset_value;
1492 valid_max = orig_valid_max * orig_scale_value + orig_offset_value;
1496 if (MODIS_MUL_SCALE == sotype || MODIS_DIV_SCALE == sotype) {
1497 valid_min = orig_valid_min - orig_offset_value;
1498 valid_max = orig_valid_max - orig_offset_value;
1500 else if (MODIS_EQ_SCALE == sotype) {
1501 valid_min = orig_valid_min + orig_offset_value;
1502 valid_max = orig_valid_max + orig_offset_value;
1508 at->append_attr(
"valid_min",
"Float32",print_rep);
1510 at->append_attr(
"valid_max",
"Float32",print_rep);
1511 at->del_attr(
"valid_range");
1517 if(
true == changedtype && number_type_dap_type !=
"" ) {
1518 at->del_attr(
"Number_Type");
1519 at->append_attr(
"Number_Type_Orig",number_type_dap_type,number_type_value);
1525 void HDFCFUtil::handle_modis_vip_special_attrs(
const std::string& valid_range_value,
1526 const std::string& scale_factor_value,
1527 float& valid_min,
float &valid_max) {
1529 int16 vip_orig_valid_min = 0;
1530 int16 vip_orig_valid_max = 0;
1532 size_t found = valid_range_value.find_first_of(
",");
1533 size_t found_from_end = valid_range_value.find_last_of(
",");
1534 if (string::npos == found)
1535 throw InternalErr(__FILE__,__LINE__,
"should find the separator ,");
1536 if (found != found_from_end)
1537 throw InternalErr(__FILE__,__LINE__,
"There should be only one separator.");
1544 vip_orig_valid_min = atoi((valid_range_value.substr(0,found)).c_str());
1545 vip_orig_valid_max = atoi((valid_range_value.substr(found+1)).c_str());
1547 int16 scale_factor_number = 1;
1549 scale_factor_number = atoi(scale_factor_value.c_str());
1551 if(scale_factor_number !=0) {
1552 valid_min = (float)(vip_orig_valid_min/scale_factor_number);
1553 valid_max = (float)(vip_orig_valid_max/scale_factor_number);
1556 throw InternalErr(__FILE__,__LINE__,
"The scale_factor_number should not be zero.");
1561 void HDFCFUtil::handle_amsr_attrs(AttrTable *at) {
1563 AttrTable::Attr_iter it = at->attr_begin();
1564 string scale_factor_value=
"", add_offset_value=
"0";
1565 string scale_factor_type, add_offset_type;
1566 bool OFFSET_found =
false;
1567 bool Scale_found =
false;
1568 bool SCALE_FACTOR_found =
false;
1570 while (it!=at->attr_end())
1572 if(at->get_name(it)==
"SCALE_FACTOR")
1574 scale_factor_value = (*at->get_attr_vector(it)->begin());
1575 scale_factor_type = at->get_type(it);
1576 SCALE_FACTOR_found =
true;
1579 if(at->get_name(it)==
"Scale")
1581 scale_factor_value = (*at->get_attr_vector(it)->begin());
1582 scale_factor_type = at->get_type(it);
1586 if(at->get_name(it)==
"OFFSET")
1588 add_offset_value = (*at->get_attr_vector(it)->begin());
1589 add_offset_type = at->get_type(it);
1590 OFFSET_found =
true;
1595 if (
true == SCALE_FACTOR_found) {
1596 at->del_attr(
"SCALE_FACTOR");
1597 at->append_attr(
"scale_factor",scale_factor_type,scale_factor_value);
1600 if (
true == Scale_found) {
1601 at->del_attr(
"Scale");
1602 at->append_attr(
"scale_factor",scale_factor_type,scale_factor_value);
1605 if (
true == OFFSET_found) {
1606 at->del_attr(
"OFFSET");
1607 at->append_attr(
"add_offset",add_offset_type,add_offset_value);
1615 void HDFCFUtil::obtain_grid_latlon_dim_info(HDFEOS2::GridDataset* gdset,
1621 const vector<HDFEOS2::Field*>gfields = gdset->getDataFields();
1622 vector<HDFEOS2::Field*>::const_iterator it_gf;
1623 for (it_gf = gfields.begin();it_gf != gfields.end();++it_gf) {
1626 if(1 == (*it_gf)->getFieldType()) {
1627 const vector<HDFEOS2::Dimension*>& dims= (*it_gf)->getCorrectedDimensions();
1630 if(2 == dims.size()) {
1633 if(
true == (*it_gf)->getYDimMajor()) {
1634 dim0name = dims[0]->getName();
1635 dim0size = dims[0]->getSize();
1636 dim1name = dims[1]->getName();
1637 dim1size = dims[1]->getSize();
1640 dim0name = dims[1]->getName();
1641 dim0size = dims[1]->getSize();
1642 dim1name = dims[0]->getName();
1643 dim1size = dims[0]->getSize();
1649 if( 1 == dims.size()) {
1650 dim0name = dims[0]->getName();
1651 dim0size = dims[0]->getSize();
1656 if(2 == (*it_gf)->getFieldType()) {
1657 const vector<HDFEOS2::Dimension*>& dims= (*it_gf)->getCorrectedDimensions();
1658 if(2 == dims.size()) {
1662 if(
true == (*it_gf)->getYDimMajor()) {
1663 dim0name = dims[0]->getName();
1664 dim0size = dims[0]->getSize();
1665 dim1name = dims[1]->getName();
1666 dim1size = dims[1]->getSize();
1669 dim0name = dims[1]->getName();
1670 dim0size = dims[1]->getSize();
1671 dim1name = dims[0]->getName();
1672 dim1size = dims[0]->getSize();
1676 if( 1 == dims.size()) {
1677 dim1name = dims[0]->getName();
1678 dim1size = dims[0]->getSize();
1683 if(
""==dim0name ||
""==dim1name || dim0size<0 || dim1size <0)
1684 throw InternalErr (__FILE__, __LINE__,
"Fail to obtain grid lat/lon dimension info.");
1690 void HDFCFUtil::add_cf_grid_mapping_attr(DAS &das, HDFEOS2::GridDataset*gdset,
const string& cf_projection,
1691 const string & dim0name,int32 dim0size,
const string &dim1name,int32 dim1size) {
1694 const vector<HDFEOS2::Field*>gfields = gdset->getDataFields();
1695 vector<HDFEOS2::Field*>::const_iterator it_gf;
1696 for (it_gf = gfields.begin();it_gf != gfields.end();++it_gf) {
1698 if(0 == (*it_gf)->getFieldType() && (*it_gf)->getRank() >1) {
1699 bool has_dim0 =
false;
1700 bool has_dim1 =
false;
1701 const vector<HDFEOS2::Dimension*>& dims= (*it_gf)->getCorrectedDimensions();
1702 for (vector<HDFEOS2::Dimension *>::const_iterator j =
1703 dims.begin(); j!= dims.end();++j){
1704 if((*j)->getName()== dim0name && (*j)->getSize() == dim0size)
1706 else if((*j)->getName()== dim1name && (*j)->getSize() == dim1size)
1710 if(
true == has_dim0 &&
true == has_dim1) {
1711 AttrTable *at = das.get_table((*it_gf)->getNewName());
1713 at = das.add_table((*it_gf)->getNewName(),
new AttrTable);
1716 at->append_attr(
"grid_mapping",
"String",cf_projection);
1723 void HDFCFUtil::add_cf_grid_cv_attrs(DAS & das, HDFEOS2::GridDataset *gdset) {
1726 if(GCTP_SNSOID == gdset->getProjection().getCode()) {
1729 string dim0name,dim1name;
1730 int32 dim0size = -1,dim1size = -1;
1731 HDFCFUtil::obtain_grid_latlon_dim_info(gdset,dim0name,dim0size,dim1name,dim1size);
1734 AttrTable *at = das.get_table(dim0name);
1736 at = das.add_table(dim0name,
new AttrTable);
1737 at->append_attr(
"standard_name",
"String",
"projection_y_coordinate");
1739 string long_name=
"y coordinate of projection for grid "+ gdset->getName();
1740 at->append_attr(
"long_name",
"String",long_name);
1742 at->append_attr(
"units",
"string",
"meter");
1744 at->append_attr(
"_CoordinateAxisType",
"string",
"GeoY");
1746 at = das.get_table(dim1name);
1748 at = das.add_table(dim1name,
new AttrTable);
1750 at->append_attr(
"standard_name",
"String",
"projection_x_coordinate");
1751 long_name=
"x coordinate of projection for grid "+ gdset->getName();
1752 at->append_attr(
"long_name",
"String",long_name);
1755 at->append_attr(
"units",
"string",
"meter");
1756 at->append_attr(
"_CoordinateAxisType",
"string",
"GeoX");
1759 string cf_projection_base =
"eos_cf_projection";
1761 at = das.get_table(cf_projection);
1763 at = das.add_table(cf_projection,
new AttrTable);
1765 at->append_attr(
"grid_mapping_name",
"String",
"sinusoidal");
1766 at->append_attr(
"longitude_of_central_meridian",
"Float64",
"0.0");
1767 at->append_attr(
"earth_radius",
"Float64",
"6371007.181");
1769 at->append_attr(
"_CoordinateAxisTypes",
"string",
"GeoX GeoY");
1773 HDFCFUtil::add_cf_grid_mapping_attr(das,gdset,cf_projection,dim0name,dim0size,dim1name,dim1size);
1782 void HDFCFUtil::add_cf_grid_cvs(DDS & dds, HDFEOS2::GridDataset *gdset) {
1785 if(GCTP_SNSOID == gdset->getProjection().getCode()) {
1788 string dim0name,dim1name;
1789 int32 dim0size = -1,dim1size = -1;
1790 HDFCFUtil::obtain_grid_latlon_dim_info(gdset,dim0name,dim0size,dim1name,dim1size);
1795 BaseType *bt_dim0 = NULL;
1796 BaseType *bt_dim1 = NULL;
1798 HDFEOS2GeoCF1D * ar_dim0 = NULL;
1799 HDFEOS2GeoCF1D * ar_dim1 = NULL;
1801 float64 *upleft = NULL;
1802 float64 *lowright = NULL;
1806 bt_dim0 =
new(
HDFFloat64)(dim0name,gdset->getName());
1807 bt_dim1 =
new(
HDFFloat64)(dim1name,gdset->getName());
1810 upleft =
const_cast<float64 *
>(gdset->getInfo().getUpLeft());
1811 lowright =
const_cast<float64 *
>(gdset->getInfo().getLowRight());
1814 ar_dim0 =
new HDFEOS2GeoCF1D(GCTP_SNSOID,
1815 upleft[1],lowright[1],dim0size,dim0name,bt_dim0);
1816 ar_dim0->append_dim(dim0size,dim0name);
1818 ar_dim1 =
new HDFEOS2GeoCF1D(GCTP_SNSOID,
1819 upleft[0],lowright[0],dim1size,dim1name,bt_dim1);
1820 ar_dim1->append_dim(dim1size,dim1name);
1821 dds.add_var(ar_dim0);
1822 dds.add_var(ar_dim1);
1834 throw InternalErr(__FILE__,__LINE__,
"Unable to allocate the HDFEOS2GeoCF1D instance.");
1847 string cf_projection_base =
"eos_cf_projection";
1852 HDFEOS2GeoCFProj * dummy_proj_cf =
new HDFEOS2GeoCFProj(cf_projection,gdset->getName());
1853 dds.add_var(dummy_proj_cf);
1855 delete dummy_proj_cf;
1864 void HDFCFUtil::check_obpg_global_attrs(
HDFSP::File *f, std::string & scaling,
1865 float & slope,
bool &global_slope_flag,
1866 float & intercept,
bool & global_intercept_flag){
1879 if((*i)->getName()==
"Scaling")
1881 string tmpstring((*i)->getValue().begin(), (*i)->getValue().end());
1882 scaling = tmpstring;
1884 if((*i)->getName()==
"Slope" || (*i)->getName()==
"slope")
1886 global_slope_flag =
true;
1888 switch((*i)->getType())
1890 #define GET_SLOPE(TYPE, CAST) \
1893 CAST tmpvalue = *(CAST*)&((*i)->getValue()[0]); \
1894 slope = (float)tmpvalue; \
1897 GET_SLOPE(INT16, int16);
1898 GET_SLOPE(INT32, int32);
1899 GET_SLOPE(FLOAT32,
float);
1900 GET_SLOPE(FLOAT64,
double);
1902 throw InternalErr(__FILE__,__LINE__,
"unsupported data type.");
1907 if((*i)->getName()==
"Intercept" || (*i)->getName()==
"intercept")
1909 global_intercept_flag =
true;
1910 switch((*i)->getType())
1912 #define GET_INTERCEPT(TYPE, CAST) \
1915 CAST tmpvalue = *(CAST*)&((*i)->getValue()[0]); \
1916 intercept = (float)tmpvalue; \
1919 GET_INTERCEPT(INT16, int16);
1920 GET_INTERCEPT(INT32, int32);
1921 GET_INTERCEPT(FLOAT32,
float);
1922 GET_INTERCEPT(FLOAT64,
double);
1924 throw InternalErr(__FILE__,__LINE__,
"unsupported data type.");
1925 #undef GET_INTERCEPT
1936 void HDFCFUtil::add_obpg_special_attrs(
HDFSP::File*f,DAS &das,
1938 string& scaling,
float& slope,
1939 bool& global_slope_flag,
1941 bool & global_intercept_flag) {
1943 AttrTable *at = das.get_table(onespsds->
getNewName());
1945 at = das.add_table(onespsds->
getNewName(),
new AttrTable);
1949 bool scale_factor_flag =
false;
1950 bool add_offset_flag =
false;
1951 bool slope_flag =
false;
1952 bool intercept_flag =
false;
1955 for(vector<HDFSP::Attribute *>::const_iterator i=onespsds->
getAttributes().begin();
1957 if(global_slope_flag !=
true && ((*i)->getName()==
"Slope" || (*i)->getName()==
"slope"))
1961 switch((*i)->getType())
1963 #define GET_SLOPE(TYPE, CAST) \
1966 CAST tmpvalue = *(CAST*)&((*i)->getValue()[0]); \
1967 slope = (float)tmpvalue; \
1971 GET_SLOPE(INT16, int16);
1972 GET_SLOPE(INT32, int32);
1973 GET_SLOPE(FLOAT32,
float);
1974 GET_SLOPE(FLOAT64,
double);
1976 throw InternalErr(__FILE__,__LINE__,
"unsupported data type.");
1982 if(global_intercept_flag !=
true && ((*i)->getName()==
"Intercept" || (*i)->getName()==
"intercept"))
1984 intercept_flag =
true;
1985 switch((*i)->getType())
1987 #define GET_INTERCEPT(TYPE, CAST) \
1990 CAST tmpvalue = *(CAST*)&((*i)->getValue()[0]); \
1991 intercept = (float)tmpvalue; \
1994 GET_INTERCEPT(INT16, int16);
1995 GET_INTERCEPT(INT32, int32);
1996 GET_INTERCEPT(FLOAT32,
float);
1997 GET_INTERCEPT(FLOAT64,
double);
1999 throw InternalErr(__FILE__,__LINE__,
"unsupported data type.");
2001 #undef GET_INTERCEPT
2011 if((f->
getSPType()==OBPGL3 || f->
getSPType() == OBPGL2) && (*i)->getName()==
"scale_factor")
2012 scale_factor_flag =
true;
2014 if((f->
getSPType()==OBPGL3 || f->
getSPType() == OBPGL2) && (*i)->getName()==
"add_offset")
2015 add_offset_flag =
true;
2020 if((f->
getSPType() == OBPGL2 || f->
getSPType()==OBPGL3 )&& onespsds->getFieldType()==0)
2023 if((OBPGL3 == f->
getSPType() && (scaling.find(
"linear")!=string::npos)) || OBPGL2 == f->
getSPType() ) {
2025 if(
false == scale_factor_flag && (
true == slope_flag ||
true == global_slope_flag))
2031 if(
false == add_offset_flag && (
true == intercept_flag ||
true == global_intercept_flag))
2038 bool has_fill_value =
false;
2040 if (
"_FillValue" == (*i)->getNewName()){
2041 has_fill_value =
true;
2049 if ((
false == has_fill_value) &&(DFNT_INT16 == onespsds->
getType())) {
2050 short fill_value = -32767;
2055 if ((
false == has_fill_value) &&(DFNT_UINT16 == onespsds->
getType())) {
2056 unsigned short fill_value = 65535;
2067 void HDFCFUtil::handle_otherhdf_special_attrs(
HDFSP::File*f,DAS &das) {
2076 vector<HDFSP::SDField *>::const_iterator it_g;
2080 bool latflag =
false;
2081 bool latunitsflag =
false;
2082 bool lonflag =
false;
2083 bool lonunitsflag =
false;
2084 int llcheckoverflag = 0;
2096 for(it_g = spsds.begin(); it_g != spsds.end(); it_g++){
2100 if (
true == f->
Has_Dim_NoScale_Field() && ((*it_g)->getFieldType() !=0) && ((*it_g)->IsDimScale() ==
false))
2104 if (OTHERHDF == f->
getSPType() &&
true == (*it_g)->IsDimNoScale())
2107 AttrTable *at = das.get_table((*it_g)->getNewName());
2109 at = das.add_table((*it_g)->getNewName(),
new AttrTable);
2111 for(vector<HDFSP::Attribute *>::const_iterator i=(*it_g)->getAttributes().begin();i!=(*it_g)->getAttributes().end();i++) {
2112 if((*i)->getType()==DFNT_UCHAR || (*i)->getType() == DFNT_CHAR){
2114 if((*i)->getName() ==
"long_name") {
2115 string tempstring2((*i)->getValue().begin(),(*i)->getValue().end());
2116 string tempfinalstr= string(tempstring2.c_str());
2117 if(tempfinalstr==
"latitude" || tempfinalstr ==
"Latitude")
2119 if(tempfinalstr==
"longitude" || tempfinalstr ==
"Longitude")
2126 for(vector<HDFSP::Attribute *>::const_iterator i=(*it_g)->getAttributes().begin();i!=(*it_g)->getAttributes().end();i++) {
2127 if((*i)->getName() ==
"units")
2128 latunitsflag =
true;
2133 for(vector<HDFSP::Attribute *>::const_iterator i=(*it_g)->getAttributes().begin();i!=(*it_g)->getAttributes().end();i++) {
2134 if((*i)->getName() ==
"units")
2135 lonunitsflag =
true;
2138 if(latflag && !latunitsflag){
2139 at->append_attr(
"units",
"String",
"degrees_north");
2141 latunitsflag =
false;
2145 if(lonflag && !lonunitsflag){
2146 at->append_attr(
"units",
"String",
"degrees_east");
2148 lonunitsflag =
false;
2151 if(llcheckoverflag ==2)
break;
2161 HDFCFUtil::add_missing_cf_attrs(
HDFSP::File*f,DAS &das) {
2165 vector<HDFSP::SDField *>::const_iterator it_g;
2171 for(it_g = spsds.begin(); it_g != spsds.end(); it_g++){
2172 if((*it_g)->getFieldType() == 0 && (*it_g)->getType()==DFNT_FLOAT32) {
2174 AttrTable *at = das.get_table((*it_g)->getNewName());
2176 at = das.add_table((*it_g)->getNewName(),
new AttrTable);
2177 string print_rep =
"-9999.9";
2183 for(it_g = spsds.begin(); it_g != spsds.end(); it_g++){
2184 if((*it_g)->getFieldType() == 0 && (((*it_g)->getType()==DFNT_INT32) || ((*it_g)->getType()==DFNT_INT16))) {
2186 AttrTable *at = das.get_table((*it_g)->getNewName());
2188 at = das.add_table((*it_g)->getNewName(),
new AttrTable);
2189 string print_rep =
"-9999";
2190 if((*it_g)->getType()==DFNT_INT32)
2200 for(it_g = spsds.begin(); it_g != spsds.end(); it_g++){
2201 if((*it_g)->getFieldType() == 6 && (*it_g)->getNewName()==
"nlayer") {
2203 AttrTable *at = das.get_table((*it_g)->getNewName());
2205 at = das.add_table((*it_g)->getNewName(),
new AttrTable);
2206 at->append_attr(
"units",
"String",
"km");
2209 else if((*it_g)->getFieldType() == 4) {
2211 if ((*it_g)->getNewName()==
"nh3" ||
2212 (*it_g)->getNewName()==
"ncat3" ||
2213 (*it_g)->getNewName()==
"nthrshZO" ||
2214 (*it_g)->getNewName()==
"nthrshHB" ||
2215 (*it_g)->getNewName()==
"nthrshSRT")
2219 "http://pps.gsfc.nasa.gov/Documents/filespec.TRMM.V7.pdf";
2222 AttrTable *at = das.get_table((*it_g)->getNewName());
2224 at = das.add_table((*it_g)->getNewName(),
new AttrTable);
2226 if((*it_g)->getNewName()==
"nh3") {
2227 comment=
"Index number to represent the fixed heights above the earth ellipsoid,";
2228 comment= comment +
" at 2, 4, 6 km plus one for path-average.";
2231 else if((*it_g)->getNewName()==
"ncat3") {
2232 comment=
"Index number to represent catgories for probability distribution functions.";
2233 comment=comment +
"Check more information from the references.";
2236 else if((*it_g)->getNewName()==
"nthrshZO")
2237 comment=
"Q-thresholds for Zero order used for probability distribution functions.";
2239 else if((*it_g)->getNewName()==
"nthrshHB")
2240 comment=
"Q-thresholds for HB used for probability distribution functions.";
2242 else if((*it_g)->getNewName()==
"nthrshSRT")
2243 comment=
"Q-thresholds for SRT used for probability distribution functions.";
2245 at->append_attr(
"comment",
"String",comment);
2246 at->append_attr(
"references",
"String",references);
2258 string base_filename;
2259 size_t last_slash_pos = f->
getPath().find_last_of(
"/");
2260 if(last_slash_pos != string::npos)
2261 base_filename = f->
getPath().substr(last_slash_pos+1);
2262 if(
""==base_filename)
2264 bool t3a26_flag = ((base_filename.find(
"3A26")!=string::npos)?
true:
false);
2266 if(
true == t3a26_flag) {
2268 for(it_g = spsds.begin(); it_g != spsds.end(); it_g++){
2270 if((*it_g)->getFieldType() == 0 && ((*it_g)->getType()==DFNT_FLOAT32)) {
2271 AttrTable *at = das.get_table((*it_g)->getNewName());
2273 at = das.add_table((*it_g)->getNewName(),
new AttrTable);
2274 at->del_attr(
"_FillValue");
2275 at->append_attr(
"_FillValue",
"Float32",
"-999");
2276 at->append_attr(
"valid_min",
"Float32",
"0");
2286 for(it_g = spsds.begin(); it_g != spsds.end(); it_g++){
2288 if((*it_g)->getFieldType() == 4 ) {
2290 string references =
"http://pps.gsfc.nasa.gov/Documents/filespec.TRMM.V7.pdf";
2291 if ((*it_g)->getNewName()==
"nh1") {
2293 AttrTable *at = das.get_table((*it_g)->getNewName());
2295 at = das.add_table((*it_g)->getNewName(),
new AttrTable);
2297 string comment=
"Number of fixed heights above the earth ellipsoid,";
2298 comment= comment +
" at 2, 4, 6, 10, and 15 km plus one for path-average.";
2300 at->append_attr(
"comment",
"String",comment);
2301 at->append_attr(
"references",
"String",references);
2304 if ((*it_g)->getNewName()==
"nh3") {
2306 AttrTable *at = das.get_table((*it_g)->getNewName());
2308 at = das.add_table((*it_g)->getNewName(),
new AttrTable);
2310 string comment=
"Number of fixed heights above the earth ellipsoid,";
2311 comment= comment +
" at 2, 4, 6 km plus one for path-average.";
2313 at->append_attr(
"comment",
"String",comment);
2314 at->append_attr(
"references",
"String",references);
2318 if ((*it_g)->getNewName()==
"nang") {
2320 AttrTable *at = das.get_table((*it_g)->getNewName());
2322 at = das.add_table((*it_g)->getNewName(),
new AttrTable);
2324 string comment=
"Number of fixed incidence angles, at 0, 5, 10 and 15 degree and all angles.";
2325 references =
"http://pps.gsfc.nasa.gov/Documents/ICSVol4.pdf";
2327 at->append_attr(
"comment",
"String",comment);
2328 at->append_attr(
"references",
"String",references);
2332 if ((*it_g)->getNewName()==
"ncat2") {
2334 AttrTable *at = das.get_table((*it_g)->getNewName());
2336 at = das.add_table((*it_g)->getNewName(),
new AttrTable);
2338 string comment=
"Second number of categories for histograms (30). ";
2339 comment=comment +
"Check more information from the references.";
2341 at->append_attr(
"comment",
"String",comment);
2342 at->append_attr(
"references",
"String",references);
2356 string base_filename;
2357 size_t last_slash_pos = f->
getPath().find_last_of(
"/");
2358 if(last_slash_pos != string::npos)
2359 base_filename = f->
getPath().substr(last_slash_pos+1);
2360 if(
""==base_filename)
2362 bool t2b31_flag = ((base_filename.find(
"2B31")!=string::npos)?
true:
false);
2363 bool t2a21_flag = ((base_filename.find(
"2A21")!=string::npos)?
true:
false);
2364 bool t2a12_flag = ((base_filename.find(
"2A12")!=string::npos)?
true:
false);
2367 bool t2a25_flag = ((base_filename.find(
"2A25")!=string::npos)?
true:
false);
2368 bool t1c21_flag = ((base_filename.find(
"1C21")!=string::npos)?
true:
false);
2369 bool t1b21_flag = ((base_filename.find(
"1B21")!=string::npos)?
true:
false);
2370 bool t1b11_flag = ((base_filename.find(
"1B11")!=string::npos)?
true:
false);
2371 bool t1b01_flag = ((base_filename.find(
"1B01")!=string::npos)?
true:
false);
2376 if(t2b31_flag || t2a12_flag || t2a21_flag) {
2381 for(it_g = spsds.begin(); it_g != spsds.end(); it_g++){
2384 if((*it_g)->getFieldType() == 0 && (*it_g)->getType()==DFNT_INT16) {
2386 AttrTable *at = das.get_table((*it_g)->getNewName());
2388 at = das.add_table((*it_g)->getNewName(),
new AttrTable);
2390 AttrTable::Attr_iter it = at->attr_begin();
2391 while (it!=at->attr_end()) {
2392 if(at->get_name(it)==
"scale_factor")
2395 string scale_factor_value=
"";
2396 string scale_factor_type;
2398 scale_factor_value = (*at->get_attr_vector(it)->begin());
2399 scale_factor_type = at->get_type(it);
2401 if(scale_factor_type ==
"Float64") {
2402 double new_scale = 1.0/strtod(scale_factor_value.c_str(),NULL);
2403 at->del_attr(
"scale_factor");
2405 at->append_attr(
"scale_factor", scale_factor_type,print_rep);
2409 if(scale_factor_type ==
"Float32") {
2410 float new_scale = 1.0/strtof(scale_factor_value.c_str(),NULL);
2411 at->del_attr(
"scale_factor");
2413 at->append_attr(
"scale_factor", scale_factor_type,print_rep);
2426 if(t2a12_flag==
true) {
2428 for(it_g = spsds.begin(); it_g != spsds.end(); it_g++){
2430 if((*it_g)->getFieldType() == 6 && (*it_g)->getNewName()==
"nlayer") {
2432 AttrTable *at = das.get_table((*it_g)->getNewName());
2434 at = das.add_table((*it_g)->getNewName(),
new AttrTable);
2435 at->append_attr(
"units",
"String",
"km");
2440 if((*it_g)->getFieldType() == 0 && (*it_g)->getType()==DFNT_INT8) {
2442 AttrTable *at = das.get_table((*it_g)->getNewName());
2444 at = das.add_table((*it_g)->getNewName(),
new AttrTable);
2445 at->append_attr(
"_FillValue",
"Int32",
"-99");
2455 for(it_g = spsds.begin(); it_g != spsds.end(); it_g++){
2456 if((*it_g)->getFieldType() == 0 && (*it_g)->getType()==DFNT_FLOAT32) {
2458 AttrTable *at = das.get_table((*it_g)->getNewName());
2460 at = das.add_table((*it_g)->getNewName(),
new AttrTable);
2461 string print_rep =
"-9999.9";
2467 for(it_g = spsds.begin(); it_g != spsds.end(); it_g++){
2470 if((*it_g)->getFieldType() == 0 && (*it_g)->getType()==DFNT_INT16) {
2472 AttrTable *at = das.get_table((*it_g)->getNewName());
2474 at = das.add_table((*it_g)->getNewName(),
new AttrTable);
2476 string print_rep =
"-9999";
2485 else if(t2a21_flag ==
true || t2a25_flag ==
true) {
2488 if(t2a25_flag ==
true) {
2490 unsigned char handle_scale = 0;
2491 for(it_g = spsds.begin(); it_g != spsds.end(); it_g++){
2493 if((*it_g)->getFieldType() == 0 && (*it_g)->getType()==DFNT_INT16) {
2494 bool has_dBZ =
false;
2495 bool has_rainrate =
false;
2496 bool has_scale =
false;
2497 string scale_factor_value;
2498 string scale_factor_type;
2500 AttrTable *at = das.get_table((*it_g)->getNewName());
2502 at = das.add_table((*it_g)->getNewName(),
new AttrTable);
2503 AttrTable::Attr_iter it = at->attr_begin();
2504 while (it!=at->attr_end()) {
2505 if(at->get_name(it)==
"units"){
2506 string units_value = *at->get_attr_vector(it)->begin();
2507 if(
"dBZ" == units_value) {
2511 else if(
"mm/hr" == units_value){
2512 has_rainrate =
true;
2515 if(at->get_name(it)==
"scale_factor")
2517 scale_factor_value = *at->get_attr_vector(it)->begin();
2518 scale_factor_type = at->get_type(it);
2525 if((
true == has_rainrate ||
true == has_dBZ) &&
true == has_scale) {
2528 short valid_min = 0;
2529 short valid_max = 0;
2532 if(
true == has_rainrate)
2533 valid_max = (short)(300*strtof(scale_factor_value.c_str(),NULL));
2534 else if(
true == has_dBZ)
2535 valid_max = (short)(80*strtof(scale_factor_value.c_str(),NULL));
2538 at->append_attr(
"valid_min",
"Int16",print_rep1);
2540 at->append_attr(
"valid_max",
"Int16",print_rep1);
2542 at->del_attr(
"scale_factor");
2543 if(scale_factor_type ==
"Float64") {
2544 double new_scale = 1.0/strtod(scale_factor_value.c_str(),NULL);
2546 at->append_attr(
"scale_factor", scale_factor_type,print_rep2);
2550 if(scale_factor_type ==
"Float32") {
2551 float new_scale = 1.0/strtof(scale_factor_value.c_str(),NULL);
2553 at->append_attr(
"scale_factor", scale_factor_type,print_rep3);
2560 if(2 == handle_scale)
2569 else if(t1b21_flag || t1c21_flag || t1b11_flag) {
2572 if(t1b21_flag || t1c21_flag) {
2574 for(it_g = spsds.begin(); it_g != spsds.end(); it_g++){
2576 if((*it_g)->getFieldType() == 0 && (*it_g)->getType()==DFNT_INT16) {
2578 bool has_dBm =
false;
2579 bool has_dBZ =
false;
2581 AttrTable *at = das.get_table((*it_g)->getNewName());
2583 at = das.add_table((*it_g)->getNewName(),
new AttrTable);
2584 AttrTable::Attr_iter it = at->attr_begin();
2586 while (it!=at->attr_end()) {
2587 if(at->get_name(it)==
"units"){
2589 string units_value = *at->get_attr_vector(it)->begin();
2590 if(
"dBm" == units_value) {
2595 else if(
"dBZ" == units_value){
2603 if(has_dBm ==
true || has_dBZ ==
true) {
2604 it = at->attr_begin();
2605 while (it!=at->attr_end()) {
2606 if(at->get_name(it)==
"scale_factor")
2609 string scale_value = *at->get_attr_vector(it)->begin();
2611 if(
true == has_dBm) {
2612 short valid_min = (short)(-120 *strtof(scale_value.c_str(),NULL));
2613 short valid_max = (short)(-20 *strtof(scale_value.c_str(),NULL));
2615 at->append_attr(
"valid_min",
"Int16",print_rep);
2617 at->append_attr(
"valid_max",
"Int16",print_rep);
2622 else if(
true == has_dBZ){
2623 short valid_min = (short)(-20 *strtof(scale_value.c_str(),NULL));
2624 short valid_max = (short)(80 *strtof(scale_value.c_str(),NULL));
2626 at->append_attr(
"valid_min",
"Int16",print_rep);
2628 at->append_attr(
"valid_max",
"Int16",print_rep);
2644 for(it_g = spsds.begin(); it_g != spsds.end(); it_g++){
2646 if((*it_g)->getFieldType() == 0 && (*it_g)->getType()==DFNT_INT16) {
2648 AttrTable *at = das.get_table((*it_g)->getNewName());
2650 at = das.add_table((*it_g)->getNewName(),
new AttrTable);
2651 AttrTable::Attr_iter it = at->attr_begin();
2654 while (it!=at->attr_end()) {
2656 if(at->get_name(it)==
"scale_factor")
2659 string scale_factor_value=
"";
2660 string scale_factor_type;
2662 scale_factor_value = (*at->get_attr_vector(it)->begin());
2663 scale_factor_type = at->get_type(it);
2665 if(scale_factor_type ==
"Float64") {
2666 double new_scale = 1.0/strtod(scale_factor_value.c_str(),NULL);
2667 at->del_attr(
"scale_factor");
2669 at->append_attr(
"scale_factor", scale_factor_type,print_rep);
2673 if(scale_factor_type ==
"Float32") {
2674 float new_scale = 1.0/strtof(scale_factor_value.c_str(),NULL);
2675 at->del_attr(
"scale_factor");
2677 at->append_attr(
"scale_factor", scale_factor_type,print_rep);
2688 at->append_attr(
"_FillValue",
"Int16",
"-9999");
2695 else if(t1b01_flag ==
true) {
2696 for(it_g = spsds.begin(); it_g != spsds.end(); it_g++){
2698 if((*it_g)->getFieldType() == 0 && (*it_g)->getType()==DFNT_FLOAT32) {
2699 AttrTable *at = das.get_table((*it_g)->getNewName());
2701 at = das.add_table((*it_g)->getNewName(),
new AttrTable);
2703 at->append_attr(
"_FillValue",
"Float32",
"-9999.9");
2710 AttrTable *at = das.get_table(
"HDF_GLOBAL");
2712 at = das.add_table(
"HDF_GLOBAL",
new AttrTable);
2713 string references =
"http://pps.gsfc.nasa.gov/Documents/filespec.TRMM.V7.pdf";
2714 string comment=
"The HDF4 OPeNDAP handler adds _FillValue, valid_min and valid_max for some TRMM level 1 and level 2 products.";
2715 comment= comment +
" It also changes scale_factor to follow CF conventions. ";
2717 at->append_attr(
"comment",
"String",comment);
2718 at->append_attr(
"references",
"String",references);
2736 void HDFCFUtil::handle_merra_ceres_attrs_with_bes_keys(
HDFSP::File *f, DAS &das,
const string& filename) {
2738 string base_filename = filename.substr(filename.find_last_of(
"/")+1);
2741 string check_ceres_merra_short_name_key=
"H4.EnableCERESMERRAShortName";
2742 bool turn_on_ceres_merra_short_name_key=
false;
2744 turn_on_ceres_merra_short_name_key = HDFCFUtil::check_beskeys(check_ceres_merra_short_name_key);
2747 bool merra_is_eos2 =
false;
2748 if(0== (base_filename.compare(0,5,
"MERRA"))) {
2750 for (vector < HDFSP::Attribute * >::const_iterator i =
2755 if(((*i)->getName().compare(0, 14,
"StructMetadata" )== 0) ||
2756 ((*i)->getName().compare(0, 14,
"structmetadata" )== 0)) {
2757 merra_is_eos2 =
true;
2764 if (
true == HDF4RequestHandler::get_enable_ceres_merra_short_name() && (CER_ES4 == f->
getSPType() || CER_SRB == f->
getSPType()
2767 || CER_AVG == f->
getSPType() || (
true == merra_is_eos2))) {
2770 vector<HDFSP::SDField *>::const_iterator it_g;
2771 for(it_g = spsds.begin(); it_g != spsds.end(); it_g++){
2773 AttrTable *at = das.get_table((*it_g)->getNewName());
2775 at = das.add_table((*it_g)->getNewName(),
new AttrTable);
2777 at->append_attr(
"fullpath",
"String",(*it_g)->getSpecFullPath());
2787 void HDFCFUtil::handle_vdata_attrs_with_desc_key(
HDFSP::File*f,libdap::DAS &das) {
2794 string check_vdata_desc_key=
"H4.EnableVdataDescAttr";
2795 bool turn_on_vdata_desc_key=
false;
2797 turn_on_vdata_desc_key = HDFCFUtil::check_beskeys(check_vdata_desc_key);
2800 string VDdescname =
"hdf4_vd_desc";
2801 string VDdescvalue =
"This is an HDF4 Vdata.";
2802 string VDfieldprefix =
"Vdata_field_";
2803 string VDattrprefix =
"Vdata_attr_";
2804 string VDfieldattrprefix =
"Vdata_field_attr_";
2808 string check_ceres_vdata_key=
"H4.EnableCERESVdata";
2809 bool turn_on_ceres_vdata_key=
false;
2810 turn_on_ceres_vdata_key = HDFCFUtil::check_beskeys(check_ceres_vdata_key);
2813 bool output_vdata_flag =
true;
2814 if (
false == HDF4RequestHandler::get_enable_ceres_vdata() &&
2819 output_vdata_flag =
false;
2821 if (
true == output_vdata_flag) {
2823 for(vector<HDFSP::VDATA *>::const_iterator i=f->
getVDATAs().begin(); i!=f->
getVDATAs().end();i++) {
2825 AttrTable *at = das.get_table((*i)->getNewName());
2827 at = das.add_table((*i)->getNewName(),
new AttrTable);
2829 if (
true == HDF4RequestHandler::get_enable_vdata_desc_attr()) {
2831 bool emptyvddasflag =
true;
2832 if(!((*i)->getAttributes().empty())) emptyvddasflag =
false;
2833 if((*i)->getTreatAsAttrFlag())
2834 emptyvddasflag =
false;
2836 for(vector<HDFSP::VDField *>::const_iterator j=(*i)->getFields().begin();j!=(*i)->getFields().end();j++) {
2837 if(!((*j)->getAttributes().empty())) {
2838 emptyvddasflag =
false;
2846 at->append_attr(VDdescname,
"String" , VDdescvalue);
2848 for(vector<HDFSP::Attribute *>::const_iterator it_va = (*i)->getAttributes().begin();it_va!=(*i)->getAttributes().end();it_va++) {
2850 if((*it_va)->getType()==DFNT_UCHAR || (*it_va)->getType() == DFNT_CHAR){
2852 string tempstring2((*it_va)->getValue().begin(),(*it_va)->getValue().end());
2853 string tempfinalstr= string(tempstring2.c_str());
2854 at->append_attr(VDattrprefix+(*it_va)->getNewName(),
"String" ,
HDFCFUtil::escattr(tempfinalstr));
2857 for (
int loc=0; loc < (*it_va)->getCount() ; loc++) {
2858 string print_rep =
HDFCFUtil::print_attr((*it_va)->getType(), loc, (
void*) &((*it_va)->getValue()[0]));
2859 at->append_attr(VDattrprefix+(*it_va)->getNewName(),
HDFCFUtil::print_type((*it_va)->getType()), print_rep);
2866 if(
false == ((*i)->getTreatAsAttrFlag())){
2868 if (
true == HDF4RequestHandler::get_enable_vdata_desc_attr()) {
2872 for(vector<HDFSP::VDField *>::const_iterator j=(*i)->getFields().begin();j!=(*i)->getFields().end();j++) {
2877 if((*j)->getAttributes().size() !=0) {
2879 AttrTable *at_v = das.get_table((*j)->getNewName());
2881 at_v = das.add_table((*j)->getNewName(),
new AttrTable);
2883 for(vector<HDFSP::Attribute *>::const_iterator it_va = (*j)->getAttributes().begin();it_va!=(*j)->getAttributes().end();it_va++) {
2885 if((*it_va)->getType()==DFNT_UCHAR || (*it_va)->getType() == DFNT_CHAR){
2887 string tempstring2((*it_va)->getValue().begin(),(*it_va)->getValue().end());
2888 string tempfinalstr= string(tempstring2.c_str());
2889 at_v->append_attr((*it_va)->getNewName(),
"String" ,
HDFCFUtil::escattr(tempfinalstr));
2892 for (
int loc=0; loc < (*it_va)->getCount() ; loc++) {
2893 string print_rep =
HDFCFUtil::print_attr((*it_va)->getType(), loc, (
void*) &((*it_va)->getValue()[0]));
2907 for(vector<HDFSP::VDField *>::const_iterator j=(*i)->getFields().begin();j!=(*i)->getFields().end();j++) {
2909 if((*j)->getFieldOrder() == 1) {
2910 if((*j)->getType()==DFNT_UCHAR || (*j)->getType() == DFNT_CHAR){
2911 string tempfinalstr;
2912 tempfinalstr.resize((*j)->getValue().size());
2913 copy((*j)->getValue().begin(),(*j)->getValue().end(),tempfinalstr.begin());
2914 at->append_attr(VDfieldprefix+(*j)->getNewName(),
"String" ,
HDFCFUtil::escattr(tempfinalstr));
2917 for (
int loc=0; loc < (*j)->getNumRec(); loc++) {
2919 at->append_attr(VDfieldprefix+(*j)->getNewName(),
HDFCFUtil::print_type((*j)->getType()), print_rep);
2925 if((*j)->getValue().size() != (
unsigned int)(DFKNTsize((*j)->getType())*((*j)->getFieldOrder())*((*j)->getNumRec()))){
2926 throw InternalErr(__FILE__,__LINE__,
"the vdata field size doesn't match the vector value");
2929 if((*j)->getNumRec()==1){
2930 if((*j)->getType()==DFNT_UCHAR || (*j)->getType() == DFNT_CHAR){
2931 string tempstring2((*j)->getValue().begin(),(*j)->getValue().end());
2932 string tempfinalstr= string(tempstring2.c_str());
2933 at->append_attr(VDfieldprefix+(*j)->getNewName(),
"String",
HDFCFUtil::escattr(tempfinalstr));
2936 for (
int loc=0; loc < (*j)->getFieldOrder(); loc++) {
2938 at->append_attr(VDfieldprefix+(*j)->getNewName(),
HDFCFUtil::print_type((*j)->getType()), print_rep);
2945 if((*j)->getType()==DFNT_UCHAR || (*j)->getType() == DFNT_CHAR){
2947 for(
int tempcount = 0; tempcount < (*j)->getNumRec()*DFKNTsize((*j)->getType());tempcount ++) {
2948 vector<char>::const_iterator tempit;
2949 tempit = (*j)->getValue().begin()+tempcount*((*j)->getFieldOrder());
2950 string tempstring2(tempit,tempit+(*j)->getFieldOrder());
2951 string tempfinalstr= string(tempstring2.c_str());
2952 string tempoutstring =
"'"+tempfinalstr+
"'";
2953 at->append_attr(VDfieldprefix+(*j)->getNewName(),
"String",
HDFCFUtil::escattr(tempoutstring));
2958 for(
int tempcount = 0; tempcount < (*j)->getNumRec();tempcount ++) {
2960 for (
int loc=0; loc < (*j)->getFieldOrder(); loc++) {
2961 string print_rep =
HDFCFUtil::print_attr((*j)->getType(), loc, (
void*) &((*j)->getValue()[tempcount*((*j)->getFieldOrder())]));
2962 at->append_attr(VDfieldprefix+(*j)->getNewName(),
HDFCFUtil::print_type((*j)->getType()), print_rep);
2971 if (
true == HDF4RequestHandler::get_enable_vdata_desc_attr()) {
2972 for(vector<HDFSP::Attribute *>::const_iterator it_va = (*j)->getAttributes().begin();it_va!=(*j)->getAttributes().end();it_va++) {
2974 if((*it_va)->getType()==DFNT_UCHAR || (*it_va)->getType() == DFNT_CHAR){
2976 string tempstring2((*it_va)->getValue().begin(),(*it_va)->getValue().end());
2977 string tempfinalstr= string(tempstring2.c_str());
2978 at->append_attr(VDfieldattrprefix+(*it_va)->getNewName(),
"String" ,
HDFCFUtil::escattr(tempfinalstr));
2981 for (
int loc=0; loc < (*it_va)->getCount() ; loc++) {
2982 string print_rep =
HDFCFUtil::print_attr((*it_va)->getType(), loc, (
void*) &((*it_va)->getValue()[0]));
2983 at->append_attr(VDfieldattrprefix+(*it_va)->getNewName(),
HDFCFUtil::print_type((*it_va)->getType()), print_rep);
2996 void HDFCFUtil::map_eos2_objects_attrs(libdap::DAS &das,
const string &filename) {
2999 int32 status_32 = -1;
3001 int32 vgroup_id = -1;
3002 int32 lone_vg_number = -1;
3003 int32 num_of_lones = 0;
3004 uint16 name_len = 0;
3006 file_id = Hopen (filename.c_str(), DFACC_READ, 0);
3008 throw InternalErr(__FILE__,__LINE__,
"Hopen failed.");
3010 status_n = Vstart (file_id);
3011 if(status_n == FAIL) {
3013 throw InternalErr(__FILE__,__LINE__,
"Vstart failed.");
3017 bool unexpected_fail =
false;
3021 num_of_lones = Vlone (file_id, NULL, num_of_lones );
3025 if (num_of_lones > 0)
3029 vector<int32> ref_array;
3030 ref_array.resize(num_of_lones);
3036 num_of_lones = Vlone (file_id, &ref_array[0], num_of_lones);
3039 for (lone_vg_number = 0; lone_vg_number < num_of_lones;
3046 vgroup_id = Vattach(file_id, ref_array[lone_vg_number],
"r");
3047 if(vgroup_id == FAIL) {
3048 unexpected_fail =
true;
3049 err_msg = string(ERR_LOC) +
" Vattach failed. ";
3053 status_32 = Vgetnamelen(vgroup_id, &name_len);
3054 if(status_32 == FAIL) {
3055 unexpected_fail =
true;
3057 err_msg = string(ERR_LOC) +
" Vgetnamelen failed. ";
3061 vector<char> vgroup_name;
3062 vgroup_name.resize(name_len+1);
3063 status_32 = Vgetname (vgroup_id, &vgroup_name[0]);
3064 if(status_32 == FAIL) {
3065 unexpected_fail =
true;
3067 err_msg = string(ERR_LOC) +
" Vgetname failed. ";
3071 status_32 = Vgetclassnamelen(vgroup_id, &name_len);
3072 if(status_32 == FAIL) {
3073 unexpected_fail =
true;
3075 err_msg = string(ERR_LOC) +
" Vgetclassnamelen failed. ";
3079 vector<char>vgroup_class;
3080 vgroup_class.resize(name_len+1);
3081 status_32 = Vgetclass (vgroup_id, &vgroup_class[0]);
3082 if(status_32 == FAIL) {
3083 unexpected_fail =
true;
3085 err_msg = string(ERR_LOC) +
" Vgetclass failed. ";
3089 string vgroup_name_str(vgroup_name.begin(),vgroup_name.end());
3090 vgroup_name_str = vgroup_name_str.substr(0,vgroup_name_str.size()-1);
3092 string vgroup_class_str(vgroup_class.begin(),vgroup_class.end());
3093 vgroup_class_str = vgroup_class_str.substr(0,vgroup_class_str.size()-1);
3095 if(vgroup_class_str ==
"GRID")
3096 map_eos2_one_object_attrs_wrapper(das,file_id,vgroup_id,vgroup_name_str,
true);
3097 else if(vgroup_class_str ==
"SWATH")
3098 map_eos2_one_object_attrs_wrapper(das,file_id,vgroup_id,vgroup_name_str,
false);
3104 throw InternalErr(__FILE__,__LINE__,
"map_eos2_one_object_attrs_wrapper failed.");
3106 Vdetach (vgroup_id);
3114 if(
true == unexpected_fail)
3115 throw InternalErr(__FILE__,__LINE__,err_msg);
3121 void HDFCFUtil::map_eos2_one_object_attrs_wrapper(libdap:: DAS &das,int32 file_id,int32 vgroup_id,
const string& vgroup_name,
bool is_grid) {
3123 char attr_name[H4_MAX_NC_NAME];
3125 int32 num_gobjects = Vntagrefs (vgroup_id);
3126 if(num_gobjects < 0)
3127 throw InternalErr(__FILE__,__LINE__,
"Cannot obtain the number of objects under a vgroup.");
3129 for(
int i = 0; i<num_gobjects;i++) {
3131 int32 obj_tag, obj_ref;
3132 if (Vgettagref (vgroup_id, i, &obj_tag, &obj_ref) == FAIL)
3133 throw InternalErr(__FILE__,__LINE__,
"Failed to obtain the tag and reference of an object under a vgroup.");
3135 if (Visvg (vgroup_id, obj_ref) == TRUE) {
3137 int32 object_attr_vgroup = Vattach(file_id,obj_ref,
"r");
3138 if(object_attr_vgroup == FAIL)
3139 throw InternalErr(__FILE__,__LINE__,
"Failed to attach an EOS2 vgroup.");
3141 uint16 name_len = 0;
3142 int32 status_32 = Vgetnamelen(object_attr_vgroup, &name_len);
3143 if(status_32 == FAIL) {
3144 Vdetach(object_attr_vgroup);
3145 throw InternalErr(__FILE__,__LINE__,
"Failed to obtain an EOS2 vgroup name length.");
3147 vector<char> attr_vgroup_name;
3148 attr_vgroup_name.resize(name_len+1);
3149 status_32 = Vgetname (object_attr_vgroup, &attr_vgroup_name[0]);
3150 if(status_32 == FAIL) {
3151 Vdetach(object_attr_vgroup);
3152 throw InternalErr(__FILE__,__LINE__,
"Failed to obtain an EOS2 vgroup name. ");
3155 string attr_vgroup_name_str(attr_vgroup_name.begin(),attr_vgroup_name.end());
3156 attr_vgroup_name_str = attr_vgroup_name_str.substr(0,attr_vgroup_name_str.size()-1);
3159 if(
true == is_grid && attr_vgroup_name_str==
"Grid Attributes"){
3160 map_eos2_one_object_attrs(das,file_id,object_attr_vgroup,vgroup_name);
3161 Vdetach(object_attr_vgroup);
3164 else if(
false == is_grid && attr_vgroup_name_str==
"Swath Attributes") {
3165 map_eos2_one_object_attrs(das,file_id,object_attr_vgroup,vgroup_name);
3166 Vdetach(object_attr_vgroup);
3171 Vdetach(object_attr_vgroup);
3172 throw InternalErr(__FILE__,__LINE__,
"Cannot map eos2 object attributes to DAP2.");
3174 Vdetach(object_attr_vgroup);
3181 void HDFCFUtil::map_eos2_one_object_attrs(libdap:: DAS &das,int32 file_id, int32 obj_attr_group_id,
const string& vgroup_name) {
3183 int32 num_gobjects = Vntagrefs(obj_attr_group_id);
3184 if(num_gobjects < 0)
3185 throw InternalErr(__FILE__,__LINE__,
"Cannot obtain the number of objects under a vgroup.");
3188 AttrTable *at = das.get_table(vgroup_cf_name);
3190 at = das.add_table(vgroup_cf_name,
new AttrTable);
3192 for(
int i = 0; i<num_gobjects;i++) {
3194 int32 obj_tag, obj_ref;
3195 if (Vgettagref(obj_attr_group_id, i, &obj_tag, &obj_ref) == FAIL)
3196 throw InternalErr(__FILE__,__LINE__,
"Failed to obtain the tag and reference of an object under a vgroup.");
3198 if(Visvs(obj_attr_group_id,obj_ref)) {
3200 int32 vdata_id = VSattach(file_id,obj_ref,
"r");
3201 if(vdata_id == FAIL)
3202 throw InternalErr(__FILE__,__LINE__,
"Failed to attach a vdata.");
3205 if(VSisattr(vdata_id)) {
3208 int32 num_field = VFnfields(vdata_id);
3211 throw InternalErr(__FILE__,__LINE__,
"Number of vdata field for an EOS2 object must be 1.");
3213 int32 num_record = VSelts(vdata_id);
3216 throw InternalErr(__FILE__,__LINE__,
"Number of vdata record for an EOS2 object must be 1.");
3218 char vdata_name[VSNAMELENMAX];
3219 if(VSQueryname(vdata_id,vdata_name) == FAIL) {
3221 throw InternalErr(__FILE__,__LINE__,
"Failed to obtain EOS2 object vdata name.");
3223 string vdatanamestr(vdata_name);
3225 int32 fieldsize = VFfieldesize(vdata_id,0);
3226 if(fieldsize == FAIL) {
3228 throw InternalErr(__FILE__,__LINE__,
"Failed to obtain EOS2 object vdata field size.");
3231 char* fieldname = VFfieldname(vdata_id,0);
3232 if(fieldname == NULL) {
3234 throw InternalErr(__FILE__,__LINE__,
"Failed to obtain EOS2 object vdata field name.");
3236 int32 fieldtype = VFfieldtype(vdata_id,0);
3237 if(fieldtype == FAIL) {
3239 throw InternalErr(__FILE__,__LINE__,
"Failed to obtain EOS2 object vdata field type.");
3242 if(VSsetfields(vdata_id,fieldname) == FAIL) {
3244 throw InternalErr(__FILE__,__LINE__,
"EOS2 object vdata: VSsetfields failed.");
3247 vector<char> vdata_value;
3248 vdata_value.resize(fieldsize);
3249 if(VSread(vdata_id,(uint8*)&vdata_value[0],1,FULL_INTERLACE) == FAIL) {
3251 throw InternalErr(__FILE__,__LINE__,
"EOS2 object vdata: VSread failed.");
3255 if(fieldtype == DFNT_UCHAR || fieldtype == DFNT_CHAR){
3256 string tempstring(vdata_value.begin(),vdata_value.end());
3258 string tempstring2 = string(tempstring.c_str());
3277 const string printable =
" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789~`!@#$%^&*()_-+={[}]|\\:;<,>.?/'\"\n\t\r";
3278 const string ESC =
"\\";
3279 const string DOUBLE_ESC = ESC + ESC;
3280 const string QUOTE =
"\"";
3281 const string ESCQUOTE = ESC + QUOTE;
3286 while ((ind = s.find(ESC, ind)) != string::npos) {
3287 s.replace(ind, 1, DOUBLE_ESC);
3288 ind += DOUBLE_ESC.length();
3294 while ((ind = s.find_first_not_of(printable, ind)) != string::npos) {
3295 s.replace(ind, 1, ESC + octstring(s[ind]));
3303 while ((ind = s.find_first_not_of(printable, ind)) != s.npos) {
3305 s.replace(ind, 1, ESC + octstring(s[ind]));
3314 size_t temp_ind = 0;
3315 while ((temp_ind = s.find_first_not_of(printable, ind)) != s.npos) {
3319 s.replace(ind, 1, ESC + octstring(s[ind]));
3327 while ((ind = s.find(QUOTE, ind)) != string::npos) {
3329 s.replace(ind, 1, ESCQUOTE);
3330 ind += ESCQUOTE.length();
3337 void HDFCFUtil::parser_trmm_v7_gridheader(
const vector<char>& value,
3338 int& latsize,
int&lonsize,
3339 float& lat_start,
float& lon_start,
3340 float& lat_res,
float& lon_res,
3341 bool check_reg_orig ){
3343 float lat_north = 0.;
3344 float lat_south = 0.;
3345 float lon_east = 0.;
3346 float lon_west = 0.;
3348 vector<string> ind_elems;
3355 if(ind_elems.size()<9)
3356 throw InternalErr(__FILE__,__LINE__,
"The number of elements in the TRMM level 3 GridHeader is not right.");
3358 if(
false == check_reg_orig) {
3359 if (0 != ind_elems[1].find(
"Registration=CENTER"))
3360 throw InternalErr(__FILE__,__LINE__,
"The TRMM grid registration is not center.");
3363 if (0 == ind_elems[2].find(
"LatitudeResolution")){
3365 size_t equal_pos = ind_elems[2].find_first_of(
'=');
3366 if(string::npos == equal_pos)
3367 throw InternalErr(__FILE__,__LINE__,
"Cannot find latitude resolution for TRMM level 3 products");
3369 size_t scolon_pos = ind_elems[2].find_first_of(
';');
3370 if(string::npos == scolon_pos)
3371 throw InternalErr(__FILE__,__LINE__,
"Cannot find latitude resolution for TRMM level 3 products");
3372 if (equal_pos < scolon_pos){
3374 string latres_str = ind_elems[2].substr(equal_pos+1,scolon_pos-equal_pos-1);
3375 lat_res = strtof(latres_str.c_str(),NULL);
3378 throw InternalErr(__FILE__,__LINE__,
"latitude resolution is not right for TRMM level 3 products");
3381 throw InternalErr(__FILE__,__LINE__,
"The TRMM grid LatitudeResolution doesn't exist.");
3383 if (0 == ind_elems[3].find(
"LongitudeResolution")){
3385 size_t equal_pos = ind_elems[3].find_first_of(
'=');
3386 if(string::npos == equal_pos)
3387 throw InternalErr(__FILE__,__LINE__,
"Cannot find longitude resolution for TRMM level 3 products");
3389 size_t scolon_pos = ind_elems[3].find_first_of(
';');
3390 if(string::npos == scolon_pos)
3391 throw InternalErr(__FILE__,__LINE__,
"Cannot find longitude resolution for TRMM level 3 products");
3392 if (equal_pos < scolon_pos){
3393 string lonres_str = ind_elems[3].substr(equal_pos+1,scolon_pos-equal_pos-1);
3394 lon_res = strtof(lonres_str.c_str(),NULL);
3397 throw InternalErr(__FILE__,__LINE__,
"longitude resolution is not right for TRMM level 3 products");
3400 throw InternalErr(__FILE__,__LINE__,
"The TRMM grid LongitudeResolution doesn't exist.");
3402 if (0 == ind_elems[4].find(
"NorthBoundingCoordinate")){
3404 size_t equal_pos = ind_elems[4].find_first_of(
'=');
3405 if(string::npos == equal_pos)
3406 throw InternalErr(__FILE__,__LINE__,
"Cannot find latitude resolution for TRMM level 3 products");
3408 size_t scolon_pos = ind_elems[4].find_first_of(
';');
3409 if(string::npos == scolon_pos)
3410 throw InternalErr(__FILE__,__LINE__,
"Cannot find latitude resolution for TRMM level 3 products");
3411 if (equal_pos < scolon_pos){
3412 string north_bounding_str = ind_elems[4].substr(equal_pos+1,scolon_pos-equal_pos-1);
3413 lat_north = strtof(north_bounding_str.c_str(),NULL);
3416 throw InternalErr(__FILE__,__LINE__,
"NorthBoundingCoordinate is not right for TRMM level 3 products");
3420 throw InternalErr(__FILE__,__LINE__,
"The TRMM grid NorthBoundingCoordinate doesn't exist.");
3422 if (0 == ind_elems[5].find(
"SouthBoundingCoordinate")){
3424 size_t equal_pos = ind_elems[5].find_first_of(
'=');
3425 if(string::npos == equal_pos)
3426 throw InternalErr(__FILE__,__LINE__,
"Cannot find south bound coordinate for TRMM level 3 products");
3428 size_t scolon_pos = ind_elems[5].find_first_of(
';');
3429 if(string::npos == scolon_pos)
3430 throw InternalErr(__FILE__,__LINE__,
"Cannot find south bound coordinate for TRMM level 3 products");
3431 if (equal_pos < scolon_pos){
3432 string lat_south_str = ind_elems[5].substr(equal_pos+1,scolon_pos-equal_pos-1);
3433 lat_south = strtof(lat_south_str.c_str(),NULL);
3436 throw InternalErr(__FILE__,__LINE__,
"south bound coordinate is not right for TRMM level 3 products");
3440 throw InternalErr(__FILE__,__LINE__,
"The TRMM grid SouthBoundingCoordinate doesn't exist.");
3442 if (0 == ind_elems[6].find(
"EastBoundingCoordinate")){
3444 size_t equal_pos = ind_elems[6].find_first_of(
'=');
3445 if(string::npos == equal_pos)
3446 throw InternalErr(__FILE__,__LINE__,
"Cannot find south bound coordinate for TRMM level 3 products");
3448 size_t scolon_pos = ind_elems[6].find_first_of(
';');
3449 if(string::npos == scolon_pos)
3450 throw InternalErr(__FILE__,__LINE__,
"Cannot find south bound coordinate for TRMM level 3 products");
3451 if (equal_pos < scolon_pos){
3452 string lon_east_str = ind_elems[6].substr(equal_pos+1,scolon_pos-equal_pos-1);
3453 lon_east = strtof(lon_east_str.c_str(),NULL);
3456 throw InternalErr(__FILE__,__LINE__,
"south bound coordinate is not right for TRMM level 3 products");
3460 throw InternalErr(__FILE__,__LINE__,
"The TRMM grid EastBoundingCoordinate doesn't exist.");
3462 if (0 == ind_elems[7].find(
"WestBoundingCoordinate")){
3464 size_t equal_pos = ind_elems[7].find_first_of(
'=');
3465 if(string::npos == equal_pos)
3466 throw InternalErr(__FILE__,__LINE__,
"Cannot find south bound coordinate for TRMM level 3 products");
3468 size_t scolon_pos = ind_elems[7].find_first_of(
';');
3469 if(string::npos == scolon_pos)
3470 throw InternalErr(__FILE__,__LINE__,
"Cannot find south bound coordinate for TRMM level 3 products");
3471 if (equal_pos < scolon_pos){
3472 string lon_west_str = ind_elems[7].substr(equal_pos+1,scolon_pos-equal_pos-1);
3473 lon_west = strtof(lon_west_str.c_str(),NULL);
3476 throw InternalErr(__FILE__,__LINE__,
"south bound coordinate is not right for TRMM level 3 products");
3480 throw InternalErr(__FILE__,__LINE__,
"The TRMM grid WestBoundingCoordinate doesn't exist.");
3482 if (
false == check_reg_orig) {
3483 if (0 != ind_elems[8].find(
"Origin=SOUTHWEST"))
3484 throw InternalErr(__FILE__,__LINE__,
"The TRMM grid origin is not SOUTHWEST.");
3489 latsize =(int)((lat_north-lat_south)/lat_res);
3490 lonsize =(int)((lon_east-lon_west)/lon_res);
3491 lat_start = lat_south;
3492 lon_start = lon_west;
3500 void HDFCFUtil::rev_str(
char *str,
int len)
3518 int HDFCFUtil::int_to_str(
int x,
char str[],
int d)
3523 str[i++] = (x%10) +
'0';
3538 void HDFCFUtil::dtoa(
double n,
char *res,
int afterpoint)
3544 double fpart = n - (double)ipart;
3547 int i = int_to_str(ipart, res, 0);
3550 if (afterpoint != 0)
3557 fpart = fpart * pow(10, afterpoint);
3561 int final_fpart = (int)fpart;
3562 if(fpart -(
int)fpart >0.5)
3563 final_fpart = (int)fpart +1;
3564 int_to_str(final_fpart, res + i + 1, afterpoint);
3569 string HDFCFUtil::get_double_str(
double x,
int total_digit,
int after_point) {
3574 res.resize(total_digit);
3575 for(
int i = 0; i<total_digit;i++)
3579 dtoa(-x,&res[0],after_point);
3580 for(
int i = 0; i<total_digit;i++) {
3582 str.push_back(res[i]);
3586 dtoa(x, &res[0], after_point);
3587 for(
int i = 0; i<total_digit;i++) {
3589 str.push_back(res[i]);
3601 string HDFCFUtil::get_int_str(
int x) {
3605 str.push_back(x+
'0');
3607 else if (x >10 && x<100) {
3608 str.push_back(x/10+
'0');
3609 str.push_back(x%10+
'0');
3613 int abs_x = (x<0)?-x:x;
3619 buf.resize(num_digit);
3620 snprintf(&buf[0],num_digit,
"%d",x);
3621 str.assign(&buf[0]);
3630 template<
typename T>
3631 size_t HDFCFUtil::write_vector_to_file(
const string & fname,
const vector<T> &val,
size_t dtypesize) {
3634 size_t HDFCFUtil::write_vector_to_file(
const string & fname,
const vector<double> &val,
size_t dtypesize) {
3638 cerr<<
"Open a file with the name "<<fname<<endl;
3639 pFile = fopen(fname.c_str(),
"wb");
3640 ret_val = fwrite(&val[0],dtypesize,val.size(),pFile);
3641 cerr<<
"ret_val for write is "<<ret_val <<endl;
3647 ssize_t HDFCFUtil::write_vector_to_file2(
const string & fname,
const vector<double> &val,
size_t dtypesize) {
3651 int fd = open(fname.c_str(),O_RDWR|O_CREAT|O_EXCL,0666);
3652 cerr<<
"The first val is "<<val[0] <<endl;
3653 ret_val = write(fd,&val[0],dtypesize*val.size());
3655 cerr<<
"ret_val for write is "<<ret_val <<endl;
3661 ssize_t HDFCFUtil::read_vector_from_file(
int fd, vector<double> &val,
size_t dtypesize) {
3664 ret_val = read(fd,&val[0],val.size()*dtypesize);
3667 cerr<<
"Open a file with the name "<<fname<<endl;
3668 pFile = fopen(fname.c_str(),
"wb");
3669 ret_val = fwrite(&val[0],dtypesize,val.size(),pFile);
3670 cerr<<
"ret_val for write is "<<ret_val <<endl;
3678 ssize_t HDFCFUtil::read_buffer_from_file(
int fd,
void*buf,
size_t total_read) {
3681 ret_val = read(fd,buf,total_read);
3687 if(
false == pass_fileid) {
3692 #ifdef USE_HDFEOS2_LIB
3705 string HDFCFUtil::obtain_cache_fname(
const string & fprefix,
const string &fname,
const string &vname) {
3707 string cache_fname = fprefix;
3708 string base_file_name = basename(fname);
3710 if((base_file_name.size() >12)
3711 && (base_file_name.compare(0,4,
"AIRS") == 0)
3712 && (base_file_name.find(
".L3.")!=string::npos)
3713 && (base_file_name.find(
".v6.")!=string::npos)
3714 && ((vname ==
"Latitude") ||(vname ==
"Longitude")))
3715 cache_fname = cache_fname +
"AIRS"+
".L3."+
".v6."+vname;
3717 cache_fname = cache_fname + base_file_name +
"_"+vname;
3724 size_t HDFCFUtil::obtain_dds_cache_size(
HDFSP::File*spf) {
3726 size_t total_bytes_written = 0;
3727 const vector<HDFSP::SDField *>& spsds = spf->
getSD()->
getFields();
3728 vector<HDFSP::SDField *>::const_iterator it_g;
3729 for(it_g = spsds.begin(); it_g != spsds.end(); it_g++){
3732 if(DFNT_CHAR == (*it_g)->getType()) {
3733 total_bytes_written = 0;
3738 int temp_rank = (*it_g)->getRank();
3739 const vector<HDFSP::Dimension*>& dims= (*it_g)->getDimensions();
3740 vector<HDFSP::Dimension*>::const_iterator it_d;
3741 for(it_d = dims.begin(); it_d != dims.end(); ++it_d)
3742 total_bytes_written += ((*it_d)->getName()).size()+1;
3744 total_bytes_written +=((*it_g)->getName()).size()+1;
3748 if(((*it_g)->getName()) != ((*it_g)->getNewName()))
3749 total_bytes_written +=((*it_g)->getNewName()).size()+1;
3751 total_bytes_written +=1;
3754 total_bytes_written +=(temp_rank+4)*
sizeof(
int);
3758 if(total_bytes_written != 0)
3759 total_bytes_written +=1;
3761 return total_bytes_written;
3766 void HDFCFUtil::write_sp_sds_dds_cache(
HDFSP::File* spf,FILE*dds_file,
size_t total_bytes_dds_cache,
const string &dds_filename) {
3768 BESDEBUG(
"h4",
" Coming to write SDS DDS to a cache" << endl);
3769 char delimiter =
'\0';
3771 size_t total_written_bytes_count = 0;
3774 vector<char>temp_buf;
3775 temp_buf.resize(total_bytes_dds_cache);
3776 char* temp_pointer = &temp_buf[0];
3778 const vector<HDFSP::SDField *>& spsds = spf->
getSD()->
getFields();
3781 vector<HDFSP::SDField *>::const_iterator it_g;
3782 for(it_g = spsds.begin(); it_g != spsds.end(); it_g++){
3785 int sds_rank = (*it_g)->getRank();
3786 int sds_ref = (*it_g)->getFieldRef();
3787 int sds_dtype = (*it_g)->getType();
3788 int sds_ftype = (*it_g)->getFieldType();
3790 vector<int32>dimsizes;
3791 dimsizes.resize(sds_rank);
3794 const vector<HDFSP::Dimension*>& dims= (*it_g)->getDimensions();
3795 for(
int i = 0; i < sds_rank; i++)
3796 dimsizes[i] = dims[i]->getSize();
3798 memcpy((
void*)temp_pointer,(
void*)&sds_rank,
sizeof(
int));
3799 temp_pointer +=
sizeof(int);
3800 memcpy((
void*)temp_pointer,(
void*)&sds_ref,
sizeof(
int));
3801 temp_pointer +=
sizeof(int);
3802 memcpy((
void*)temp_pointer,(
void*)&sds_dtype,
sizeof(
int));
3803 temp_pointer +=
sizeof(int);
3804 memcpy((
void*)temp_pointer,(
void*)&sds_ftype,
sizeof(
int));
3805 temp_pointer +=
sizeof(int);
3807 memcpy((
void*)temp_pointer,(
void*)&dimsizes[0],sds_rank*
sizeof(
int));
3808 temp_pointer +=sds_rank*
sizeof(int);
3811 total_written_bytes_count +=(sds_rank+4)*
sizeof(
int);
3815 string temp_varname = (*it_g)->getName();
3816 vector<char>temp_val1(temp_varname.begin(),temp_varname.end());
3817 memcpy((
void*)temp_pointer,(
void*)&temp_val1[0],temp_varname.size());
3818 temp_pointer +=temp_varname.size();
3819 memcpy((
void*)temp_pointer,&delimiter,1);
3822 total_written_bytes_count =total_written_bytes_count +(1+temp_varname.size());
3828 if((*it_g)->getName() == (*it_g)->getNewName()){
3829 memcpy((
void*)temp_pointer,&delimiter,1);
3831 total_written_bytes_count +=1;
3834 string temp_varnewname = (*it_g)->getNewName();
3835 vector<char>temp_val2(temp_varnewname.begin(),temp_varnewname.end());
3836 memcpy((
void*)temp_pointer,(
void*)&temp_val2[0],temp_varnewname.size());
3837 temp_pointer +=temp_varnewname.size();
3838 memcpy((
void*)temp_pointer,&delimiter,1);
3840 total_written_bytes_count =total_written_bytes_count +(1+temp_varnewname.size());
3844 for(
int i = 0; i < sds_rank; i++) {
3845 string temp_dimname = dims[i]->getName();
3846 vector<char>temp_val3(temp_dimname.begin(),temp_dimname.end());
3847 memcpy((
void*)temp_pointer,(
void*)&temp_val3[0],temp_dimname.size());
3848 temp_pointer +=temp_dimname.size();
3849 memcpy((
void*)temp_pointer,&delimiter,1);
3851 total_written_bytes_count =total_written_bytes_count +(1+temp_dimname.size());
3855 memcpy((
void*)temp_pointer,&cend,1);
3856 total_written_bytes_count +=1;
3858 if(total_written_bytes_count != total_bytes_dds_cache) {
3859 stringstream s_total_written_count;
3860 s_total_written_count << total_written_bytes_count;
3861 stringstream s_total_bytes_dds_cache;
3862 s_total_bytes_dds_cache << total_bytes_dds_cache;
3863 string msg =
"DDs cached file "+ dds_filename +
" buffer size should be " + s_total_bytes_dds_cache.str() ;
3864 msg = msg +
". But the real size written in the buffer is " + s_total_written_count.str();
3865 throw InternalErr (__FILE__, __LINE__,msg);
3868 size_t bytes_really_written = fwrite((
const void*)&temp_buf[0],1,total_bytes_dds_cache,dds_file);
3870 if(bytes_really_written != total_bytes_dds_cache) {
3871 stringstream s_expected_bytes;
3872 s_expected_bytes << total_bytes_dds_cache;
3873 stringstream s_really_written_bytes;
3874 s_really_written_bytes << bytes_really_written;
3875 string msg =
"DDs cached file "+ dds_filename +
" size should be " + s_expected_bytes.str() ;
3876 msg +=
". But the real size written to the file is " + s_really_written_bytes.str();
3877 throw InternalErr (__FILE__, __LINE__,msg);
3883 void HDFCFUtil::read_sp_sds_dds_cache(FILE* dds_file,libdap::DDS * dds_ptr,
3884 const std::string &cache_filename,
const std::string &hdf4_filename) {
3886 BESDEBUG(
"h4",
" Coming to read SDS DDS from a cache" << endl);
3890 if(stat(cache_filename.c_str(),&sb)!=0) {
3891 string err_mesg=
"The DDS cache file " + cache_filename;
3892 err_mesg = err_mesg +
" doesn't exist. ";
3893 throw InternalErr(__FILE__,__LINE__,err_mesg);
3896 size_t bytes_expected_read = (size_t)sb.st_size;
3899 vector<char> temp_buf;
3900 temp_buf.resize(bytes_expected_read);
3901 size_t bytes_really_read = fread((
void*)&temp_buf[0],1,bytes_expected_read,dds_file);
3904 if(bytes_really_read != bytes_expected_read) {
3905 stringstream s_bytes_really_read;
3906 s_bytes_really_read << bytes_really_read ;
3907 stringstream s_bytes_expected_read;
3908 s_bytes_expected_read << bytes_expected_read;
3909 string msg =
"The expected bytes to read from DDS cache file " + cache_filename +
" is " + s_bytes_expected_read.str();
3910 msg = msg +
". But the real read size from the buffer is " + s_bytes_really_read.str();
3911 throw InternalErr (__FILE__, __LINE__,msg);
3913 char* temp_pointer = &temp_buf[0];
3915 char delimiter =
'\0';
3918 bool end_file_flag =
false;
3922 int sds_rank = *((
int *)(temp_pointer));
3923 temp_pointer = temp_pointer +
sizeof(int);
3925 int sds_ref = *((
int *)(temp_pointer));
3926 temp_pointer = temp_pointer +
sizeof(int);
3928 int sds_dtype = *((
int *)(temp_pointer));
3929 temp_pointer = temp_pointer +
sizeof(int);
3931 int sds_ftype = *((
int *)(temp_pointer));
3932 temp_pointer = temp_pointer +
sizeof(int);
3934 vector<int32>dimsizes;
3936 throw InternalErr (__FILE__, __LINE__,
"SDS rank must be >0");
3938 dimsizes.resize(sds_rank);
3939 for (
int i = 0; i <sds_rank;i++) {
3940 dimsizes[i] = *((
int *)(temp_pointer));
3941 temp_pointer = temp_pointer +
sizeof(int);
3944 vector<string>dimnames;
3945 dimnames.resize(sds_rank);
3946 string varname,varnewname;
3947 for (
int i = 0; i <sds_rank+2;i++) {
3948 vector<char> temp_vchar;
3949 char temp_char = *temp_pointer;
3952 if(temp_char == delimiter)
3953 temp_vchar.push_back(temp_char);
3954 while(temp_char !=delimiter) {
3955 temp_vchar.push_back(temp_char);
3957 temp_char = *temp_pointer;
3961 string temp_string(temp_vchar.begin(),temp_vchar.end());
3963 varname = temp_string;
3965 varnewname = temp_string;
3967 dimnames[i-2] = temp_string;
3972 if(varnewname[0] == delimiter)
3973 varnewname = varname;
3976 BaseType *bt = NULL;
3978 #define HANDLE_CASE(tid, type) \
3980 bt = new (type)(varnewname,hdf4_filename); \
3984 HANDLE_CASE(DFNT_CHAR,
HDFStr);
3985 #ifndef SIGNED_BYTE_TO_INT32
3986 HANDLE_CASE(DFNT_INT8,
HDFByte);
3990 HANDLE_CASE(DFNT_UINT8,
HDFByte);
3995 HANDLE_CASE(DFNT_UCHAR8,
HDFByte);
3997 throw InternalErr(__FILE__,__LINE__,
"unsupported data type.");
4002 throw InternalErr(__FILE__,__LINE__,
"Cannot create the basetype when creating DDS from a cache file.");
4004 SPType sptype = OTHERHDF;
4026 throw InternalErr(__FILE__,__LINE__,
"Unable to allocate the HDFSPArray_RealField instance.");
4029 for(
int i = 0; i <sds_rank; i++)
4030 ar->append_dim(dimsizes[i],dimnames[i]);
4031 dds_ptr->add_var(ar);
4047 throw InternalErr(__FILE__,__LINE__,
"Unable to allocate the HDFSPArray_RealField instance.");
4050 ar->append_dim(dimsizes[0],dimnames[0]);
4051 dds_ptr->add_var(ar);
4056 throw InternalErr(__FILE__,__LINE__,
"SDS rank must be 1 for the missing coordinate.");
4059 if(*temp_pointer == cend)
4060 end_file_flag =
true;
4061 if((temp_pointer - &temp_buf[0]) > (
int)bytes_expected_read) {
4062 string msg = cache_filename +
" doesn't have the end-line character at the end. The file may be corrupted.";
4063 throw InternalErr (__FILE__, __LINE__,msg);
4065 }
while(
false == end_file_flag);
4067 dds_ptr->set_dataset_name(basename(hdf4_filename));
4085 void HDFCFUtil::reset_fileid(
int& sdfd,
int& fileid,
int& gridfd,
int& swathfd) {