From 9918e0e06e27481a56f90044cac4017d35cdfd6e Mon Sep 17 00:00:00 2001 From: MuQun Yang Date: Thu, 28 Sep 2000 15:51:45 -0500 Subject: [svn-r2614] Purpose: add h4toh5 converter source codes under tools directory. Description: [describe the bug, or describe the new feature, etc] Solution: [details about the changes, algorithm, etc...] [Please as detail as you can since your own explanation is better than others guessing it from the code.] Platforms tested: [machines you have tested the changed version. This is absolute important. Test it out on at least two or three different platforms such as Big-endian-32bit (SUN/IRIX), little-endian-32(LINUX) and 64-bit (IRIX64/UNICOS/DEC-ALPHA) would be good.] --- tools/h4toh5anno.c | 635 ++++++++++++++++++++ tools/h4toh5image.c | 712 ++++++++++++++++++++++ tools/h4toh5main.c | 1538 +++++++++++++++++++++++++++++++++++++++++++++++ tools/h4toh5main.h | 56 ++ tools/h4toh5pal.c | 167 ++++++ tools/h4toh5sds.c | 892 ++++++++++++++++++++++++++++ tools/h4toh5util.c | 1618 ++++++++++++++++++++++++++++++++++++++++++++++++++ tools/h4toh5util.h | 188 ++++++ tools/h4toh5vdata.c | 788 ++++++++++++++++++++++++ tools/h4toh5vgroup.c | 768 ++++++++++++++++++++++++ tools/testh4toh5 | 213 +++++++ 11 files changed, 7575 insertions(+) create mode 100644 tools/h4toh5anno.c create mode 100644 tools/h4toh5image.c create mode 100644 tools/h4toh5main.c create mode 100644 tools/h4toh5main.h create mode 100644 tools/h4toh5pal.c create mode 100644 tools/h4toh5sds.c create mode 100644 tools/h4toh5util.c create mode 100644 tools/h4toh5util.h create mode 100644 tools/h4toh5vdata.c create mode 100644 tools/h4toh5vgroup.c create mode 100644 tools/testh4toh5 diff --git a/tools/h4toh5anno.c b/tools/h4toh5anno.c new file mode 100644 index 0000000..6f11594 --- /dev/null +++ b/tools/h4toh5anno.c @@ -0,0 +1,635 @@ +#include "h4toh5main.h" + + +/*------------------------------------------------------------------------- + * Function: Annofil_h4_to_h5 + * + * Purpose: translate file annotation object into hdf5 dataset + * + * Return: FAIL if failed, SUCCEED if successful. + * + * In : + file_id: file identifier + h5_group: hdf5 group id + + *------------------------------------------------------------------------- + */ + +int Annofil_h4_to_h5(int32 file_id,hid_t h5group){ + + int32 an_id; + int32 ann_id; + int32 i; + + int32 ann_length; + + int32 n_file_label = 0; + int32 n_file_desc = 0; + int32 n_data_label = 0; + int32 n_data_desc = 0; + + int32 istat; + + char* ann_buf; + char anno_labelname[30]; + char anno_descname[30]; + char index_str[5]; + + hid_t h5_sid; + hid_t h5_aid; + hid_t sh5str_type; + hid_t sh5str1_type; + hid_t ret; + + an_id = ANstart(file_id); + + if(an_id < 0) { + printf("error in obtaining an_id. \n"); + return FAIL; + } + + istat = ANfileinfo(an_id,&n_file_label,&n_file_desc, + &n_data_label,&n_data_desc); + + if(istat == FAIL) { + printf("error getting file information.\n"); + ANend(file_id); + return FAIL; + } + + for (i = 0; i < n_file_label; i++) { + + ann_id = ANselect(an_id,i,AN_FILE_LABEL); + if(ann_id == FAIL) { + printf("error in obtaining annotation id. \n"); + ANend(file_id); + return FAIL; + } + + ann_length = ANannlen(ann_id); + if(ann_length == FAIL) { + printf("error in obtaining annotation length. \n"); + ANend(file_id); + ANendaccess(ann_id); + return FAIL; + } + + ann_buf = malloc(ann_length + 1); + if(ann_buf == NULL) { + printf("error in allocating memory. \n"); + return FAIL; + } + bzero(ann_buf,(ann_length+1)*sizeof(char)); + istat = ANreadann(ann_id,ann_buf,ann_length+1); + + if(istat==FAIL) { + printf("fail to read file information. \n"); + ANend(file_id); + ANendaccess(ann_id); + free(ann_buf); + return FAIL; + } + + h5_sid = H5Screate(H5S_SCALAR); + + if (h5_sid < 0) { + printf("failed to create attribute space for"); + printf(" HDF4 FILE ANNOTATION. \n"); + ANend(file_id); + ANendaccess(ann_id); + free(ann_buf); + return FAIL; + } + + if ((sh5str_type = mkstr(ann_length+1,H5T_STR_NULLTERM))<0) { + printf("error in making string at FILE lABEL ANNO. \n"); + ANend(file_id); + ANendaccess(ann_id); + free(ann_buf); + return FAIL; + } + + if(conv_int_str(i,index_str)== FAIL) { + printf("fail to convert integer into character format.\n"); + ANend(file_id); + ANendaccess(ann_id); + free(ann_buf); + return FAIL; + } + + strcpy(anno_labelname,HDF4_FILE_LABEL); + strcat(anno_labelname,"_"); + strcat(anno_labelname,index_str); + + + h5_aid = H5Acreate(h5group,anno_labelname,sh5str_type, + h5_sid,H5P_DEFAULT); + + if (h5_aid <0) { + printf("failed to obtain attribute id for"); + printf(" File annotation. \n"); + ANend(file_id); + ANendaccess(ann_id); + free(ann_buf); + return FAIL; + } + + ret = H5Awrite(h5_aid,sh5str_type,(void *)ann_buf); + + if (ret <0) { + printf("failed to obtain attribute.\n "); + ANend(file_id); + ANendaccess(ann_id); + free(ann_buf); + return FAIL; + } + + ret = H5Sclose(h5_sid); + ret = H5Aclose(h5_aid); + free(ann_buf); + ANendaccess(ann_id); + + } + + for (i = 0; i < n_file_desc; i++) { + + ann_id = ANselect(an_id,i,AN_FILE_DESC); + if(ann_id == FAIL) { + printf("error in obtaining annotation id. \n"); + ANend(an_id); + return FAIL; + } + + ann_length = ANannlen(ann_id); + + if(ann_length == FAIL) { + printf("error in obtaining annotation length. \n"); + ANend(an_id); + ANendaccess(ann_id); + return FAIL; + } + + ann_buf = malloc(ann_length+1); + if(ann_buf == NULL) { + printf("error in allocating memory. \n"); + ANend(an_id); + ANendaccess(ann_id); + return FAIL; + } + bzero(ann_buf,ann_length+1); + + istat = ANreadann(ann_id,ann_buf,ann_length+1); + + if(istat == FAIL) { + printf("error reading file information. \n"); + ANend(an_id); + ANendaccess(ann_id); + free(ann_buf); + return FAIL; + } + + if ((sh5str1_type = mkstr(ann_length+1,H5T_STR_NULLTERM))<0) { + printf("error in making string at FILE DESC. \n"); + ANend(an_id); + ANendaccess(ann_id); + free(ann_buf); + return FAIL; + } + + if(conv_int_str(i,index_str)==FAIL) { + printf("fail to convert integer into character format.\n"); + ANend(an_id); + ANendaccess(ann_id); + free(ann_buf); + return FAIL; + } + + strcpy(anno_descname,HDF4_FILE_DESC); + strcat(anno_descname,"_"); + strcat(anno_descname,index_str); + + h5_sid = H5Screate(H5S_SCALAR); + + if (h5_sid < 0) { + printf("failed to create attribute space for"); + printf(" HDF4 FILE ANNOTATION. \n"); + ANend(an_id); + ANendaccess(ann_id); + free(ann_buf); + return FAIL; + } + + h5_aid = H5Acreate(h5group,anno_descname,sh5str1_type, + h5_sid,H5P_DEFAULT); + + if (h5_aid <0) { + + printf("failed to obtain attribute id for"); + printf(" File annotation. \n"); + ANend(an_id); + ANendaccess(ann_id); + H5Sclose(h5_sid); + free(ann_buf); + return FAIL; + } + + ret = H5Awrite(h5_aid,sh5str1_type,(void *)ann_buf); + + if (ret <0) { + printf("failed to obtain attribute.\n "); + ANend(an_id); + ANendaccess(ann_id); + H5Sclose(h5_sid); + H5Aclose(h5_aid); + free(ann_buf); + return FAIL; + } + + ret = H5Sclose(h5_sid); + ret = H5Aclose(h5_aid); + free(ann_buf); + ANendaccess(ann_id); + } + ANend(an_id); + return SUCCEED; +} + + +/*------------------------------------------------------------------------- + * Function: Annoobj_h4_to_h5 + * + * Purpose: translate annotation object into attribute of hdf5 dataset + * + * Return: FAIL if failed, SUCCEED if successful. + * + * In : + file_id: file identifier + obj_ref: object reference + obj_tag: object tag + h5group: hdf5 group + + *------------------------------------------------------------------------- + */ +int Annoobj_h4_to_h5(int32 file_id,int32 obj_ref, int32 obj_tag, + hid_t h5group){ + + int32 an_id; + int32 ann_id; + int32 i; + int32 status; + int32 ann_length; + + int32 n_file_label =-1; + int32 n_file_desc =-1; + int32 n_data_label =-1; + int32 n_data_desc =-1; + + int num_lab_anno; + int num_des_anno; + int32 istat; + int32* des_anno_list; + int32* lab_anno_list; + + char* ann_buf; + char* ann_obj_name; + char ann_labelname[30]; + + char index_str[5]; + + hid_t h5_sid; + hid_t h5_aid; + hid_t sh5str_type; + hid_t ret; + + an_id = ANstart(file_id); + if(an_id == FAIL) { + printf("fail to start annotation interface.\n"); + return FAIL; + } + + istat = ANfileinfo(an_id,&n_file_label,&n_file_desc, + &n_data_label,&n_data_desc); + + if(istat == FAIL ) { + printf("error getting file information.\n"); + ANend(an_id); + return FAIL; + } + + num_lab_anno = ANnumann(an_id,AN_DATA_LABEL,obj_tag,obj_ref); + num_des_anno = ANnumann(an_id,AN_DATA_DESC,obj_tag,obj_ref); + + if (num_lab_anno == FAIL) { + printf("error getting number of annotation data label.\n"); + ANend(an_id); + return FAIL; + } + + if (num_des_anno == FAIL) { + printf("error getting number of annotation object label.\n"); + ANend(an_id); + return FAIL; + } + + if(num_lab_anno != 0) { + + for(i=0; i0) { + + /* convert reference number into string format. */ + if(conv_int_str(pal_ref,palref_str)==FAIL) { + printf("error in converting palette reference number into string.\n"); + return FAIL; + } + + /* check whether this palette has been looked up already. */ + check_pal = lookup(pal_ref,PAL_HASHSIZE,pal_hashtab); + + if( check_pal < 0) { + printf("error at looking up palette table. \n"); + return FAIL; + } + + /* if check_pal equals to 1, this palette has already been + converted into hdf5 dataset, just obtain the palette name. + if check_pal equals to 0, we will do the converting. */ + + if(check_pal == 1) { + + h5pal_name = get_name(pal_ref,PAL_HASHSIZE,pal_hashtab, + &check_palname); + + if (h5pal_name == NULL && check_palname == 0 ) { + printf("error,cannot find group\n"); + return FAIL; + } + + if (h5pal_name == NULL && check_palname == -1 ) { + printf("error,group name is not defined.\n"); + return FAIL; + } + + } + + if(check_pal == 0) { + /* do converting. */ + strcpy(palg_name,HDF4_PALG); + + /* obtain hdf5 dataset name converted from palette, + no name for hdf4 palette.*/ + h5pal_name = get_obj_aboname(NULL,palref_str,palg_name,HDF4_PALETTE); + if(h5pal_name == NULL) { + printf("error in getting hdf5 palette name.\n"); + return FAIL; + } + + if(set_name(pal_ref,PAL_HASHSIZE,pal_hashtab,h5pal_name)==FAIL) { + printf("error in setting object name.\n"); + free(h5pal_name); + return FAIL; + } + + pal_stat = Palette_h4_to_h5(file_id,pal_id,h5_palgroup,h5pal_name); + + if(pal_stat == FAIL) { + printf("error occurring in transferring palette into dataset. \n"); + free(h5pal_name); + return FAIL; + } + + } + + if(create_pal_objref(h5dset,h5_palgroup,h5pal_name)== FAIL) { + printf("error in creating palette object reference.\n"); + free(h5pal_name); + return FAIL; + } + + if(h5pal_name != NULL) free(h5pal_name); + + strcpy(image_index,HDF4_IMAGE_INDEXED); + if(h4_transpredattrs(h5dset,HDF4_IMAGE_SUBCLASS,image_index)== FAIL) { + printf("failed to transfer hdf4 image indexed.\n"); + return FAIL; + } + } + return SUCCEED; +} +/***** end of palette application. *****/ +/*------------------------------------------------------------------------- + * Function: gr_tranattrs + * + * Purpose: translate attributes of Image object into hdf5 dataset + * + * Return: FAIL if failed, SUCCEED if successful. + * + * In : + sri_id: RI identifier + sh5_dset: hdf5 dataset + snum_grattrs: number of attribute + check_gloflag: flag to check whether this attribute belongs + to gr interface. + + Out: + *------------------------------------------------------------------------- + */ +int gr_tranattrs(int32 sri_id, hid_t sh5_dset,int snum_grattrs, + int check_gloflag) { + + char sgratrr_name[2*MAX_NC_NAME]; + char grglo[MAX_NC_NAME]; + char* grrepattr_name; + int32 count_sgradata; + int32 sgr_atype; + size_t sh4_amemsize; + size_t sh4_asize; + + hid_t sh5a_sid; + hid_t sh5a_id; + hid_t sh5_atype; + hid_t sh5_amemtype; + hid_t sh5str_type; + hid_t sh5str_memtype; + hsize_t sh5dims[MAX_VAR_DIMS]; + void* sgr_adata; + herr_t sret; + int i; + + + for (i =0;i 0) H5Gclose(h5_dimg); + if(num_images >0) H5Gclose(h5_palg); + H5Gclose(h5_root); + H5Fclose(file5_id); + free_allhashmemory(); + return FAIL; + } + +/*convert all objects in group rings into corresponding hdf5 objects. */ + if(h4toh5vgrings(file_id,sd_id,h5_root,h5_dimg,h5_palg) == FAIL){ + printf("error in translating vgroup rings into hdf5 objects.\n"); + SDend(sd_id); + GRend(gr_id); + Vend(file_id); + Hclose(file_id); + if(num_sds >0) H5Gclose(h5_dimg); + if(num_images >0) H5Gclose(h5_palg); + H5Gclose(h5_root); + H5Fclose(file5_id); + free_allhashmemory(); + return FAIL; + } + + /*convert all independent lone vdata into corresponding hdf5 datasets with + compound data type. */ + if(h4toh5lonevds(file_id,h5_root) == FAIL){ + printf("error in translating lone independent vdata into hdf5 objects.\n"); + SDend(sd_id); + GRend(gr_id); + Vend(file_id); + Hclose(file_id); + if(num_sds >0) H5Gclose(h5_dimg); + if(num_images >0) H5Gclose(h5_palg); + H5Gclose(h5_root); + H5Fclose(file5_id); + free_allhashmemory(); + return FAIL; + } + + /*** convert hdf file annotations into hdf5 attributes under the root.***/ + if(Annofil_h4_to_h5(file_id,h5_root) == FAIL) { + printf("error in translating file annotations into root attributes.\n"); + SDend(sd_id); + GRend(gr_id); + Vend(file_id); + Hclose(file_id); + if(num_sds >0) H5Gclose(h5_dimg); + if(num_images >0) H5Gclose(h5_palg); + H5Gclose(h5_root); + H5Fclose(file5_id); + free_allhashmemory(); + return FAIL; + } + + /*** deal with untouched sds objects.convert them into hdf5 datasets under root group.***/ + + if(h4toh5unvisitedsds(file_id,sd_id,h5_root,h5_dimg) == FAIL) { + printf("error in converting unvisited sds objects into hdf5 file.\n"); SDend(sd_id); + GRend(gr_id); + Vend(file_id); + Hclose(file_id); + if(num_sds >0) H5Gclose(h5_dimg); + if(num_images >0) H5Gclose(h5_palg); + H5Gclose(h5_root); + H5Fclose(file5_id); + free_allhashmemory(); + return FAIL; + } + + /*** deal with untouched image objects. convert them into hdf5 datasets under root group. ***/ + + if(h4toh5unvisitedimages(file_id,h5_root,h5_palg) == FAIL) { + printf("error in converting unvisited image objects into hdf5 file.\n"); + SDend(sd_id); + GRend(gr_id); + Vend(file_id); + Hclose(file_id); + if(num_sds >0) H5Gclose(h5_dimg); + if(num_images >0) H5Gclose(h5_palg); + H5Gclose(h5_root); + H5Fclose(file5_id); + free_allhashmemory(); + return FAIL; + } + + free_allhashmemory(); + SDend(sd_id); + GRend(gr_id); + Vend(file_id); + Hclose(file_id); + if(num_sds >0) H5Gclose(h5_dimg); + if(num_images >0) H5Gclose(h5_palg); + H5Gclose(h5_root); + H5Fclose(file5_id); + return SUCCEED; +} + +/*------------------------------------------------------------------------- + * Function: get_numof_hdf4obj + * + * Purpose: get number or estimated number of hdf4 objects + * + * Return: FAIL if failed, SUCCEED if successful. + * + * In : + file_id: hdf file identifier + filename: hdf file name + Out: + Modification: + *------------------------------------------------------------------------- + */ +int get_numof_hdf4obj(char*filename,int32 file_id) { + + int32 sd_id;/* sd interface identifer*/ + int32 gr_id;/* gr interface identifer*/ + int num_lonevd;/* number of lone vdata*/ + int num_lonevg;/* number of lone vgroup.*/ + int32 istat; + + estnum_vg = 0; + estnum_vd = 0; + num_sds = 0; + num_images = 0; + num_objects = 0; + + /* obtain number of sds and number of global sds attribute. */ + + sd_id = SDstart(filename,DFACC_READ); + if(sd_id == FAIL) { + printf("error: cannot start SD interface. \n"); + return FAIL; + } + + if(SDfileinfo(sd_id,&num_sds,&num_glsdsattrs) == FAIL) { + printf("error in obtaining SDS information from the file.\n"); + return FAIL; + } + + + /* obtain number of images and number of global image attributes.*/ + + gr_id = GRstart(file_id); + if(gr_id == FAIL) { + printf("error in obtaining gr id. \n"); + return FAIL; + } + + if(GRfileinfo(gr_id,&num_images,&num_glgrattrs) == FAIL) { + printf("error in obtaining GR information from the file. \n"); + return FAIL; + } + + /* obtain number of lone vgroup and lone vdata. */ + + istat = Vstart(file_id); + if (istat == FAIL) { + fprintf(stderr, "unable to start hdf4 V interface.\n"); + return FAIL; + } + + num_lonevd = VSlone(file_id,NULL,0); + if(num_lonevd == FAIL) { + printf("error in obtaining lone vdata number. \n"); + return FAIL; + } + + num_lonevg = Vlone(file_id,NULL,0); + if(num_lonevg == FAIL) { + printf("error in obtaining lone vgroup number. \n"); + return FAIL; + } + + /* intelligent guess of the total number of vgroups,total number of + independent vdata. */ + + estnum_vg = 6* num_lonevg; + estnum_vd = 4* num_lonevd; + + /* set the size of name hashtable to num_objects. */ + num_objects = estnum_vg + estnum_vd + num_sds + num_images; + + return SUCCEED; +} + + + +/*------------------------------------------------------------------------- + * Function: set_helpgroups + * + * Purpose: get number or estimated number of hdf4 objects + * + * Return: FAIL if failed, SUCCEED if successful. + * + * In : + h5root: hdf5 group identifier + h5dimgptr: h5 dimensional group pointer + h5palgptr: h5 palette group pointer + Modification: + *------------------------------------------------------------------------- + */ + +int set_helpgroups(hid_t h5root,hid_t* h5dimgptr,hid_t* h5palgptr){ + + hid_t h5_dimg;/* hdf5 dimensional scale group identifier. */ + hid_t h5_palg;/* hdf5 palette group identifier. */ + + /*1. dimensional scale group.*/ + + if(num_sds > 0) { + h5_dimg = H5Gcreate(h5root,HDF4_DIMG,0); + if (h5_dimg <0) { + printf("error in creating hdf5 dimensional scale group. \n"); + return FAIL; + } + + *h5dimgptr = h5_dimg; + } + + /*2. palette group.*/ + + if(num_images >0) { + h5_palg = H5Gcreate(h5root,HDF4_PALG,0); + if(h5_palg <0) { + printf("error in creating hdf5 palette group. \n"); + H5Gclose(h5_dimg); + return FAIL; + } + + *h5palgptr = h5_palg; + } + + return SUCCEED; + +} + + +/*------------------------------------------------------------------------- + * Function: set_hashtables + * + * Purpose: set up hashtables + * + * Return: FAIL if failed, SUCCEED if successful. + * + * In : + + Out: + Modification: + *------------------------------------------------------------------------- + */ +int set_hashtables(void) { + + if(num_sds != 0) { + sds_hashtab = malloc(sizeof(struct table)*2*num_sds); + if(init_tab(2*num_sds,sds_hashtab)== FAIL){ + printf("cannot initialize sds hashing table. \n"); + return FAIL; + } + } + + if(num_images != 0) { + gr_hashtab = malloc(sizeof(struct table)*2*num_images); + if(init_tab(2*num_images,gr_hashtab) == FAIL){ + printf("cannot initialize image hashing table. \n"); + return FAIL; + } + } + + /*hashtable is made to be fixed for dimensional scale and palette.*/ + + if(num_sds != 0) { + dim_hashtab = malloc(sizeof(struct name_table)*DIM_HASHSIZE); + if(init_nametab(DIM_HASHSIZE,dim_hashtab) == FAIL) { + printf("can not initialize dimension hashing table.\n"); + return FAIL; + } + } + + /* initialize the palette table */ + if(num_images != 0){ + pal_hashtab = malloc(sizeof(struct table)*PAL_HASHSIZE); + if(init_tab(PAL_HASHSIZE,pal_hashtab) == FAIL) { + printf("can not initialize palette hashing table.\n"); + return FAIL; + } + } + + /* initialize the vgroup table */ + if(estnum_vg > 0) { + vg_hashtab = malloc(sizeof(struct table)*estnum_vg); + } + else { + estnum_vg = VG_DEFHASHSIZE; + vg_hashtab = malloc(sizeof(struct table)*estnum_vg); + } + if(init_tab(estnum_vg,vg_hashtab) == FAIL) { + printf("error in allocating memory for vgroup hashing table.\n"); + return FAIL; + } + + /* initialize the vdata table.*/ + if(estnum_vd > 0) { + vd_hashtab = malloc(sizeof(struct table)*estnum_vd); + } + else { + estnum_vd = VD_DEFHASHSIZE; + vd_hashtab = malloc(sizeof(struct table)*estnum_vd); + } + + if(init_tab(estnum_vd,vd_hashtab)== FAIL) { + printf("cannot initialize vdata hashing table.\n"); + return FAIL; + } + + /* The name hashtable is only for dealing with name clashing, + num_objects is the size of the hash table. */ + + if(num_objects != 0){ + name_hashtab = malloc(sizeof(struct name_table)*num_objects); + if(init_nametab(num_objects,name_hashtab)== FAIL) { + printf("cannot initialize name hashing table. \n"); + return FAIL; + } + } + + return SUCCEED; +} + +/*------------------------------------------------------------------------- + * Function: h4toh5lonevgs + * + * Purpose: Recursively convert hdf4 objects in lone vgroups into + corresponding hdf5 datasets + * + * Return: FAIL if failed, SUCCEED if successful. + * + * In : file_id: hdf file id + sd_id: hdf sd interface id + h5group: hdf5 group id + h5_dimg: hdf5 dimensional scale group id + h5_palg: hdf5 palette group id + + Out: + Modification: + *------------------------------------------------------------------------- + */ +int h4toh5lonevgs(int32 file_id,int32 sd_id,hid_t h5group,hid_t h5_dimg,hid_t h5_palg) { + + int32 vgroup_id; + int num_lonevg; /* number of lone vgroup.*/ + int32 *ref_array; + int32 istat; + char vgroup_name[VGNAMELENMAX]; + char* cor_vgroupname; + char vgroup_class[VGNAMELENMAX]; + char refstr[MAXREF_LENGTH]; + int check_vgroup; + int check_tabst; + int lone_vg_number; + char *h5cgroup_name; + + istat = Vstart(file_id); + if (istat == FAIL) { + fprintf(stderr, "unable to start hdf4 V interface.\n"); + return FAIL; + } + + num_lonevg = Vlone(file_id,NULL,0); + + if (num_lonevg == FAIL) { + printf("error in obtaining lone vgroup number. \n"); + return FAIL; + } + + /* obtain object reference array. */ + + ref_array = (int32 *)malloc(sizeof(int32) *num_lonevg); + + if(ref_array == NULL) { + printf("error in allocating memory for ref_array.\n"); + return FAIL; + } + + num_lonevg = Vlone(file_id,ref_array,num_lonevg); + + /* walk through every lone group in the file */ + + for(lone_vg_number = 0; lone_vg_number < num_lonevg; + lone_vg_number++) { + + vgroup_id = Vattach(file_id,ref_array[lone_vg_number],"r"); + + if(vgroup_id ==FAIL) { + printf("error in attaching lone vgroup.\n"); + free(ref_array); + return FAIL; + } + + /*obtain group name and class name.*/ + bzero(vgroup_class,VGNAMELENMAX); + istat = Vgetclass(vgroup_id,vgroup_class); + if(istat == FAIL) { + printf("error in getting vgroup class.\n"); + free(ref_array); + Vdetach(vgroup_id); + return FAIL; + } + + bzero(vgroup_name,VGNAMELENMAX); + istat = Vgetname(vgroup_id,vgroup_name); + if(istat == FAIL ) { + printf("error in getting vgroup name. \n"); + Vdetach(vgroup_id); + free(ref_array); + return FAIL; + } + + /* check for CDF0.0 and RIG0.0, if yes + don't go into this group.*/ + + if(strcmp(vgroup_class,_HDF_CDF)==0) { + Vdetach(vgroup_id); + continue; + } + if(strcmp(vgroup_class,GR_NAME)==0) { + Vdetach(vgroup_id); + continue; + } + + /* converting integer number into string format. */ + if(conv_int_str(ref_array[lone_vg_number],refstr) == FAIL) { + printf("ref. is negative, error in converting\n"); + Vdetach(vgroup_id); + free(ref_array); + return FAIL; + } + + /* checking whether vgroup name contains ORI_SLASH, changing into CHA_SLASH.*/ + cor_vgroupname = correct_name(vgroup_name); + if(cor_vgroupname == NULL) { + printf("error in generating corrected vgroup name. \n"); + Vdetach(vgroup_id); + free(ref_array); + return FAIL; + } + + /* obtaining group name of the converted lone vgroup. In this call, + we will deal with cases such as name clashing and no available vgroup + name. */ + + h5cgroup_name = get_obj_aboname(cor_vgroupname,refstr,NULL,HDF4_VGROUP); + + if(h5cgroup_name == NULL) { + printf("error in getting group name.\n"); + Vdetach(vgroup_id); + free(ref_array); + free(cor_vgroupname); + return FAIL; + } + + /* printf("h5cgroup_name %s\n",h5cgroup_name);*/ + free(cor_vgroupname); + + /* updating lookup table for vgroups.*/ + + check_vgroup = lookup(ref_array[lone_vg_number],estnum_vg,vg_hashtab); + + if(check_vgroup == 0) { /* adding this vgroup into the list. */ + + check_tabst = set_name(ref_array[lone_vg_number],estnum_vg, + vg_hashtab,h5cgroup_name); + if(check_tabst == FAIL) { + printf("not enough memory to be allocated for vgroup name. \n"); + Vdetach(vgroup_id); + free(h5cgroup_name); + free(ref_array); + return FAIL; + } + } + + /* this line is for debugging. */ + + if(check_vgroup == 1){ + fprintf(stderr,"this vgroup should not be touched. \n"); + Vdetach(vgroup_id); + free(h5cgroup_name); + free(ref_array); + return FAIL; + } + + if(Vgroup_h4_to_h5(file_id,vgroup_id,sd_id,h5group,h5_dimg,h5_palg)==FAIL){ + printf("error in translating vgroup into hdf5 objects.\n"); + Vdetach(vgroup_id); + free(h5cgroup_name); + free(ref_array); + return FAIL; + } + + Vdetach(vgroup_id); + free(h5cgroup_name); + } + free(ref_array); + return SUCCEED; +} + + + +/*------------------------------------------------------------------------- + * Function: h4toh5vgrings + * + * Purpose: Recursively convert objects at special hdf4 vgroups + (vgroup rings) + into objects of corresponding hdf5 groups. The strategy here + is to arbitrily grab any vgroup in the group ring and put it + under hdf5 root. + * + * Return: FAIL if failed, SUCCEED if successful. + * + * In : file_id: hdf file id + sd_id: hdf sds id + h5group: hdf5 group id + h5_dimg: hdf5 dimensional scale group id + h5_palg: hdf5 palette group id + + Out: + Modification: + *------------------------------------------------------------------------- + */ + +int h4toh5vgrings(int32 file_id,int32 sd_id,hid_t h5group,hid_t h5_dimg,hid_t h5_palg){ + + int32 vgroup_id; + int32 ref_num; + char vgroup_name[VGNAMELENMAX]; + char* cor_vgroupname; + char vgroup_class[VGNAMELENMAX]; + char refstr[MAXREF_LENGTH]; + int check_vgroup; + int32 istat; + char *h5cgroup_name; + + ref_num = Vgetid(file_id,-1); + + while (ref_num != -1) { + + /* if we find a group that is not touched, grab it under root group.*/ + + check_vgroup = lookup(ref_num,estnum_vg,vg_hashtab); + + if (check_vgroup == 0){ + + vgroup_id = Vattach(file_id,ref_num,"r"); + if(vgroup_id ==FAIL) { + printf("error in attaching group in a group ring. \n"); + return FAIL; + } + + bzero(vgroup_name,VGNAMELENMAX); + istat = Vgetname(vgroup_id,vgroup_name); + if(istat ==FAIL) { + printf("error in obtaining vgroup names. \n"); + Vdetach(vgroup_id); + return FAIL; + } + + bzero(vgroup_class,VGNAMELENMAX); + if(Vgetclass(vgroup_id,vgroup_class) == FAIL) { + printf("error in obtaining vgroup class name. \n"); + Vdetach(vgroup_id); + return FAIL; + } + + /* do nothing for those predefined attribute.*/ + + if(vgroup_class != NULL) { + + if(strcmp(vgroup_class,_HDF_ATTRIBUTE)==0) { + ref_num = Vgetid(file_id,ref_num); + Vdetach(vgroup_id); + continue; + } + + if(strcmp(vgroup_class,_HDF_VARIABLE)==0) { + ref_num = Vgetid(file_id,ref_num); + Vdetach(vgroup_id); + continue; + } + + if(strcmp(vgroup_class,_HDF_DIMENSION)==0) { + ref_num = Vgetid(file_id,ref_num); + Vdetach(vgroup_id); + continue; + } + + if(strcmp(vgroup_class,_HDF_UDIMENSION)==0) { + ref_num = Vgetid(file_id,ref_num); + Vdetach(vgroup_id); + continue; + } + + if(strcmp(vgroup_class,_HDF_CDF)==0) { + ref_num = Vgetid(file_id,ref_num); + Vdetach(vgroup_id); + continue; + } + + if(strcmp(vgroup_class,GR_NAME)==0) { + ref_num = Vgetid(file_id,ref_num); + Vdetach(vgroup_id); + continue; + } + + if(strcmp(vgroup_class,RI_NAME)==0) { + ref_num = Vgetid(file_id,ref_num); + Vdetach(vgroup_id); + continue; + } + } + + if(vgroup_name != NULL) { + if(strcmp(vgroup_name,GR_NAME)==0) { + ref_num = Vgetid(file_id,ref_num); + Vdetach(vgroup_id); + continue; + } + } + + /* convert reference number into string format. */ + if(conv_int_str(ref_num,refstr) == FAIL) { + printf("ref. is negative, error in converting\n"); + Vdetach(vgroup_id); + return FAIL; + } + + /* checking whether vgroup name contains ORI_SLASH, changing into CHA_SLASH.*/ + cor_vgroupname = correct_name(vgroup_name); + if(cor_vgroupname == NULL) { + printf("error in generating corrected vgroup name. \n"); + Vdetach(vgroup_id); + return FAIL; + } + /* obtain the hdf5 group name. */ + h5cgroup_name = get_obj_aboname(cor_vgroupname,refstr,NULL,HDF4_VGROUP); + + if(h5cgroup_name == NULL) { + printf("error in getting vgroup name.\n"); + Vdetach(vgroup_id); + free(cor_vgroupname); + return FAIL; + } + + free(cor_vgroupname); + if(set_name(ref_num,estnum_vg,vg_hashtab,h5cgroup_name)==FAIL) { + printf("error in setting h5 group name.\n"); + Vdetach(vgroup_id); + free(h5cgroup_name); + return FAIL; + } + + if(Vgroup_h4_to_h5(file_id,vgroup_id,sd_id,h5group,h5_dimg,h5_palg) + ==FAIL){ + + printf("error in translating vgroup into hdf5 group\n"); + Vdetach(vgroup_id); + free(h5cgroup_name); + return FAIL; + } + + Vdetach(vgroup_id); + free(h5cgroup_name); + } + ref_num = Vgetid(file_id,ref_num); + } + return SUCCEED; + +} + + +/*------------------------------------------------------------------------- + * Function: h4toh5lonevds + * + * Purpose: convert hdf4 lone vdata into + the corresponding hdf5 datasets + * + * Return: FAIL if failed, SUCCEED if successful. + * + * In : file_id: hdf file id + h5group: hdf5 group id + + Out: + Modification: + *------------------------------------------------------------------------- + */ +int h4toh5lonevds(int32 file_id, hid_t h5group){ + + int32 vdata_id; + int32 *ref_vdata_array; + int32 vdata_tag; + int32 vdata_ref; + int32 istat; + char vdata_name[VGNAMELENMAX]; + char* cor_vdataname; + char vdata_class[VGNAMELENMAX]; + char refstr[MAXREF_LENGTH]; + int check_vdata; + int lone_vd_number; + int num_lonevd; + char *h5cvdata_name; + + num_lonevd = VSlone(file_id,NULL,0); + + if (num_lonevd == FAIL) { + printf("error in obtaining lone vgroup number. \n"); + return FAIL; + } + + if (num_lonevd > 0) { + + ref_vdata_array = (int32 *)malloc(sizeof(int32) *(num_lonevd)); + + num_lonevd = VSlone(file_id,ref_vdata_array,num_lonevd); + if(num_lonevd == FAIL) { + printf("error in obtaining lone vdata number the second time.\n"); + free(ref_vdata_array); + } + /* walk through all lone vdatas. */ + + for(lone_vd_number = 0; lone_vd_number < num_lonevd;lone_vd_number++) + { + vdata_id = VSattach(file_id,ref_vdata_array[lone_vd_number],"r"); + + if(vdata_id == FAIL) { + printf("error in obtaining vdata id for lone vdata.\n"); + free(ref_vdata_array); + } + + /* Make sure this vdata is not an attribute of other hdf4 objects.*/ + + if(!VSisattr(vdata_id)) { + vdata_ref = VSQueryref(vdata_id); + if(vdata_ref == FAIL) { + printf("error in getting vdata reference number.\n"); + free(ref_vdata_array); + VSdetach(vdata_id); + return FAIL; + } + + vdata_tag = VSQuerytag(vdata_id); + if(vdata_tag == FAIL){ + printf("error in getting vdata tag.\n"); + free(ref_vdata_array); + VSdetach(vdata_id); + return FAIL; + } + + bzero(vdata_class,VGNAMELENMAX); + istat = VSgetclass(vdata_id,vdata_class); + if(istat == FAIL) { + printf("error in getting vdata class name.\n"); + free(ref_vdata_array); + VSdetach(vdata_id); + return FAIL; + } + + bzero(vdata_name,VGNAMELENMAX); + istat = VSQueryname(vdata_id,vdata_name); + if(istat == FAIL) { + printf("error in getting vdata name. \n"); + free(ref_vdata_array); + VSdetach(vdata_id); + return FAIL; + } + + /* converting reference number into string format.*/ + if(conv_int_str(ref_vdata_array[lone_vd_number],refstr)==FAIL) { + printf("error in converting int to string.\n"); + free(ref_vdata_array); + VSdetach(vdata_id); + return FAIL; + } + /* checking whether vdata name contains ORI_SLASH, changing into CHA_SLASH.*/ + cor_vdataname = correct_name(vdata_name); + if(cor_vdataname == NULL) { + printf("error in generating corrected vgroup name. \n"); + VSdetach(vdata_id); + free(ref_vdata_array); + return FAIL; + } + /* obtaining hdf5 dataset name that is converted from hdf4 vdata.*/ + h5cvdata_name = get_obj_aboname(cor_vdataname,refstr,NULL,HDF4_VDATA); + if(h5cvdata_name == NULL) { + printf("error in getting vdata name.\n"); + free(ref_vdata_array); + VSdetach(vdata_id); + free(cor_vdataname); + return FAIL; + } + + free(cor_vdataname); + check_vdata = lookup(ref_vdata_array[lone_vd_number],estnum_vd, + vd_hashtab); + if(check_vdata == 1){ + printf("lone vdata should not be checked before.\n"); + free(h5cvdata_name); + free(ref_vdata_array); + VSdetach(vdata_id); + return FAIL; + } + + if(set_name(ref_vdata_array[lone_vd_number],estnum_vd,vd_hashtab, + h5cvdata_name)==FAIL) { + printf("error in setting lone vdata name. \n"); + free(ref_vdata_array); + free(h5cvdata_name); + VSdetach(vdata_id); + return FAIL; + } + + if(Vdata_h4_to_h5(file_id,vdata_id,h5group)== FAIL) { + printf("error in translating independent vdata into"); + printf(" hdf5 datasets.\n"); + free(h5cvdata_name); + free(ref_vdata_array); + VSdetach(vdata_id); + return FAIL; + } + free(h5cvdata_name); + } + + VSdetach(vdata_id); + } + free(ref_vdata_array); + } + return SUCCEED; +} + + +/*------------------------------------------------------------------------- + * Function: h4toh5unvisitedsds + * + * Purpose: convert unvisited sds objects into hdf5 datasets and put these + datasets under hdf5 root group + This routine will cover old hdf file that doesn't have vgroups. + * + * Return: FAIL if failed, SUCCEED if successful. + * + * In : + sd_id: hdf sds id + h5root: hdf5 root id + h5_dimg: hdf5 dimensional scale group id + + Out: + Modification: + *------------------------------------------------------------------------- + */ + + +int h4toh5unvisitedsds(int32 file_id,int32 sd_id,hid_t h5root,hid_t h5_dimg) { + + int i; + int32 sds_id;/* sd dataset identifer*/ + int32 sds_rank;/* sds dataset dimension rank.*/ + int32 sds_dimsizes[DIM_HASHSIZE];/* array that contains the size of the each dimension in sds dataset.*/ + int32 sds_dtype;/*sds dataset datatype.*/ + int32 num_sdsattrs;/* number of sds attributes. */ + char sds_name[MAX_NC_NAME];/* sds name.*/ + char* cor_sdsname; + int32 obj_ref; /* obj reference number assigned to sds and images.*/ + char refstr[MAXREF_LENGTH];/*object reference number in character string format.*/ + int check_sds;/* flag to check whether this sds is visited. 1 for visited and 0 for non-visited.*/ + char *h5csds_name;/* absolute path name of hdf5 dataset transferred from old sds.*/ + + if(sd_id == FAIL) { + printf("error: cannot start SD interface. \n"); + return FAIL; + } + + /* check all sds objects. */ + for(i=0;ist_mode); +} + +/***************************************************************************** + + Routine: BuildFilename() + + Description: Build a filename with new extension + + Input: filename - present filename + ext - extension to root of filename + + Output: (filename:r).ext + +*****************************************************************************/ + +char *BuildFilename(char *filename, char *ext) +{ + /* build outgoing filename */ + + char *filename_out; + char *lastper_ptr, *lastdir_ptr; + int root_len; + + lastper_ptr = strrchr(filename,'.'); + lastdir_ptr = strrchr(filename,'/'); + + if ( lastper_ptr <= lastdir_ptr ) { /* no extension */ + root_len = strlen(filename); + } else { /* existing extension */ + root_len = (int)(lastper_ptr - filename); + } + + filename_out = (char *)HDmalloc(root_len + strlen(ext) + 2); + filename_out = strncpy(filename_out, filename, (size_t)root_len); + filename_out[root_len] = '\0'; + filename_out = strcat(filename_out,"."); + filename_out = strcat(filename_out,ext); + + return filename_out; +} + + diff --git a/tools/h4toh5main.h b/tools/h4toh5main.h new file mode 100644 index 0000000..8966011 --- /dev/null +++ b/tools/h4toh5main.h @@ -0,0 +1,56 @@ +#include "hdf.h" +#include "mfhdf.h" +#include "hdf5.h" +#include "h4toh5util.h" +#include +#include +/* subroutines adapted from h5toh4 tools and used for h4toh5main.c */ +void PrintOptions_h4toh5(void); +int test_file(char *filename,int oflag,mode_t mode); +int test_dir(char *); +char *BuildFilename(char *filename, char *ext); + +int h4toh5(char*,char*); +int get_numof_hdf4obj(char*,int32); +int set_hashtables(void); +int set_helpgroups(hid_t,hid_t*,hid_t*); +int h4toh5lonevds(int32,hid_t); +int h4toh5lonevgs(int32,int32,hid_t,hid_t,hid_t); +int h4toh5vgrings(int32,int32,hid_t,hid_t,hid_t); +int h4toh5unvisitedimages(int32,hid_t,hid_t); +int h4toh5unvisitedsds(int32,int32,hid_t,hid_t); +void free_allhashmemory(void); + +/*subroutines for h4toh5vgroup.c*/ + +int Vgroup_h4_to_h5(int32,int32,int32,hid_t,hid_t,hid_t); +int convert_vgroup(int32,int32, int32,char* ,hid_t,hid_t,hid_t); +int convert_vdata(int32,int32,char*,hid_t); +int convert_sds(int32,int32,int32,char*,hid_t,hid_t); +int convert_image(int32,int32,char*,hid_t,hid_t); + +/*subroutines for h4toh5vdata.c*/ + +int Vdata_h4_to_h5(int32,int32,hid_t); +int vdata_transattrs(int32,hid_t,int,int,char*); +int gen_h5comptype(int32,int32,size_t *,size_t*,hid_t*,hid_t*,hid_t,hid_t); + +/* subroutines for h4toh5sds.c*/ +int Sds_h4_to_h5(int32,int32,hid_t,hid_t); +int sds_transattrs(int32, hid_t,int,int); +int sdsdim_to_h5dataset(int32,int32,hid_t,hid_t,int32); + + +/*subroutines for h4toh5image.c*/ +int Image_h4_to_h5(int32,int32,hid_t,hid_t); +int gr_tranattrs(int32, hid_t,int,int); +int gr_palette(int32,int32,hid_t,hid_t); +int create_pal_objref(hid_t ,hid_t ,char *); + +/*subroutines for h4toh5anno.c*/ +char* trans_tag_name(int32,ann_type); +int Annofil_h4_to_h5(int32,hid_t); +int Annoobj_h4_to_h5(int32,int32,int32,hid_t); + +/*subroutines for h4toh5pal.c*/ +int Palette_h4_to_h5(int32,int32 ,hid_t,char *); diff --git a/tools/h4toh5pal.c b/tools/h4toh5pal.c new file mode 100644 index 0000000..fc59cce --- /dev/null +++ b/tools/h4toh5pal.c @@ -0,0 +1,167 @@ +#include "h4toh5main.h" + + +/*------------------------------------------------------------------------- + * Function: Palette_h4_to_h5 + * + * Purpose: translate palette into hdf5 dataset + * + * Return: FAIL if failed, SUCCEED if successful. + * + * In : + file_id: file id + pal_id: PALETTE identifier + h5_g: hdf5 group id + pal_name: path name of the group where all palettes are in + + *------------------------------------------------------------------------- + */ + +int Palette_h4_to_h5(int32 file_id,int32 pal_id,hid_t h5g,char*pal_name) { + + int32 ncomp; + int32 pal_ref; + int32 pal_type; + int32 interlace_mode; + int32 num_entries; + void* pal_data; + size_t h4memsize; + size_t h4size; + + char palette_label[MAX_NC_NAME]; + char palette_class[MAX_NC_NAME]; + char palette_type[MAX_NC_NAME]; + + hid_t h5memtype; + hid_t h5type; + hid_t h5d_sid; + hid_t h5dset; + hsize_t h5dims[2]; + + pal_ref = GRluttoref(pal_id); + + if(pal_ref <0) { + printf("error in obtaining palette.\n"); + return FAIL; + } + + /* no palette, just return. */ + if(pal_ref == 0) return SUCCEED; + + if(GRgetlutinfo(pal_id,&ncomp,&pal_type,&interlace_mode,&num_entries)==FAIL) { + printf("error in getting palette information.\n"); + return FAIL; + } + + if(h4type_to_h5type(pal_type,&h5memtype,&h4memsize,&h4size,&h5type)== FAIL) { + fprintf(stderr,"failed to translate image datatype. \n"); + return FAIL; + } + + /* according to mapping document, data type for palette will always be + uint8. */ + + if (h5type == H5T_STRING) { + if(h5string_to_int(DFNT_UCHAR8,&h5memtype,h4memsize,&h5type)==FAIL) { + fprintf(stderr,"failed to translate H5T_STRING to int8."); + return FAIL; + } + } + + h5dims[0] = num_entries; + h5dims[1] = ncomp; + + pal_data = malloc(h4memsize*ncomp*num_entries); + + if (pal_data == NULL) { + printf("error in allocating memory for palette data.\n"); + return FAIL; + } + + if (GRreadlut(pal_id,(VOIDP)pal_data)==FAIL) { + printf("error in reading palette data. \n"); + free(pal_data); + return FAIL; + } + + h5d_sid = H5Screate_simple(2,h5dims,NULL); + + if (h5d_sid <0) { + printf("error in creating space.\n"); + free(pal_data); + return FAIL; + } + + h5dset = H5Dcreate(h5g,pal_name,h5type,h5d_sid,H5P_DEFAULT); + + if (h5dset < 0) { + printf("error in creating dataset. \n"); + free(pal_data); + H5Sclose(h5d_sid); + return FAIL; + } + + if (H5Dwrite(h5dset,h5memtype,h5d_sid,h5d_sid,H5P_DEFAULT, + (void *)pal_data)<0) { + fprintf(stdout,"error writing data for palette data\n"); + free(pal_data); + H5Sclose(h5d_sid); + H5Dclose(h5dset); + return FAIL; + } + free(pal_data); + + + strcpy(palette_label,PALABEL); + strcpy(palette_class,PALETTE); + strcpy(palette_type,PAL_TYPE); + + /* convert palette annotation into attribute of palette dataset. + Since there are no routines to find the exact tag of palette object, + we will check three possible object tags of palette objects, that is: + DFTAG_LUT. If the object tag of palette object is + falling out of this scope, we will not convert annotations into + hdf5 attributes; it is user's responsibility to make sure that object tags + for palette objects are DFTAG_LUT.*/ + + if(Annoobj_h4_to_h5(file_id,pal_ref,DFTAG_LUT,h5dset)== FAIL){ + printf("failed to convert palette annotation into hdf5 attribute.\n"); + H5Sclose(h5d_sid); + H5Dclose(h5dset); + return FAIL; + } + + if(h4_transpredattrs(h5dset,HDF4_OBJECT_TYPE,palette_label)==FAIL) { + printf("unable to transfer palette label to HDF4 OBJECT TYPE.\n"); + H5Sclose(h5d_sid); + H5Dclose(h5dset); + return FAIL; + } + + if(h4_transpredattrs(h5dset,HDF4_PALETTE_CLASS,palette_class)==FAIL){ + printf("unable to transfer palette class to HDF4 PALETTE CLASS.\n"); + H5Sclose(h5d_sid); + H5Dclose(h5dset); + return FAIL; + } + + if(h4_transpredattrs(h5dset,HDF4_PALETTE_TYPE,palette_type)==FAIL){ + printf("unable to transfer palette type to HDF4 PALETTE TYPE.\n"); + H5Sclose(h5d_sid); + H5Dclose(h5dset); + return FAIL; + } + + if(h4_transnumattr(h5dset,HDF4_REF_NUM,pal_ref)==FAIL) { + printf("unable to transfer palette reference number to HDF4 REF. NUM.\n"); + H5Sclose(h5d_sid); + H5Dclose(h5dset); + return FAIL; + } + return SUCCEED; +} + + + + + diff --git a/tools/h4toh5sds.c b/tools/h4toh5sds.c new file mode 100644 index 0000000..6d16938 --- /dev/null +++ b/tools/h4toh5sds.c @@ -0,0 +1,892 @@ +#include "h4toh5main.h" +/*------------------------------------------------------------------------- + * Function: Sds_h4_to_h5 + * + * Purpose: translate SDS object into hdf5 dataset + * + * Return: FAIL if failed, SUCCEED if successful. + * + * In : + sds_id: SDS identifier + h5_group: hdf5 group id + h5_dimgroup: hdf5 dimension group id + dim_pathname: dimensional path name + + *------------------------------------------------------------------------- + */ +int Sds_h4_to_h5(int32 file_id,int32 sds_id,hid_t h5_group,hid_t h5_dimgroup){ + + int32 sds_dtype; + int32 sds_rank; + int32 sds_dimsizes[MAX_VAR_DIMS]; + int32* sds_start; + int32* sds_edge; + int32* sds_stride; + int32 count_sdsdata; + int32 sds_ref; + int32 istat; + int i; + int empty; + int32 num_sdsattrs; + void* sds_data; + + int check_sdsname; + int check_gloattr; + + char sdsname[MAX_NC_NAME]; + char sdslabel[MAX_NC_NAME]; + size_t h4size; + size_t h4memsize; + + /* define varibles for hdf5. */ + + hid_t h5dset; + hid_t h5d_sid; + hid_t h5ty_id; + hid_t h5_memtype; + hid_t h5str_type; + hid_t h5str_memtype; + + hsize_t h5dims[MAX_VAR_DIMS]; + char* h5csds_name; + + herr_t ret; + + /* check whether the sds is empty. */ +#if 0 + if(SDcheckempty(sds_id,&empty)== FAIL) { + printf("error in running SDcheckempty routine. \n"); + return FAIL; + } + printf("empty %d\n",empty); + + if(empty != 0) return SUCCEED; +#endif + + /*check whether the sds is created with unlimited dimension. */ + + if(SDisrecord(sds_id)) { + if (SDgetinfo(sds_id,sdsname,&sds_rank,sds_dimsizes,&sds_dtype, + &num_sdsattrs)==FAIL) { + printf("unable to get information of sds h5dset.\n"); + return FAIL; + } + + } + else { + /*obtain name,rank,dimsizes,datatype and num of attributes of sds */ + if (SDgetinfo(sds_id,sdsname,&sds_rank,sds_dimsizes,&sds_dtype, + &num_sdsattrs)==FAIL) { + printf("unable to get information of sds h5dset.\n"); + return FAIL; + } + } + + /* if(sdsname !=NULL) printf("sdsname %s\n",sdsname);*/ + + /* obtain start,edge, stride and number of sds data. */ + + sds_start = malloc(sizeof(int32)*sds_rank); + if(sds_start == NULL) { + printf("error in allocating memory for sds start.\n"); + return FAIL; + } + + sds_edge = malloc(sizeof(int32)*sds_rank); + if(sds_edge == NULL) { + printf("error in allocating memory for sds edge.\n"); + free(sds_start); + return FAIL; + } + + sds_stride = malloc(sizeof(int32)*sds_rank); + if(sds_stride == NULL) { + printf("error in allocating memory for sds stride. \n"); + free(sds_start); + free(sds_edge); + return FAIL; + } + + count_sdsdata = 1; + for (i=0;inext){ + if (np->ref == objref){ + return 1; + } + } + return 0; +} + +/*------------------------------------------------------------------------- + * Function: init_tab + * + * Purpose: this function will initialize the hash table. + * + + * Return: SUCCEED, table is initialzed. FAIL,otherwise. + * + * In : + SIZE: the hashtable SIZE. + hashtab: pointer to the hash table. + + *------------------------------------------------------------------------- + */ + +int init_tab(int SIZE,struct table *hashtab) { + + int i; + if(hashtab == NULL) { + printf("memory for hashing table is not allocated.\n"); + return FAIL; + } + for (i = 0;i < SIZE; i++) { + (hashtab+i%SIZE)->ref = -1; + (hashtab+i%SIZE)->next = NULL; + (hashtab+i%SIZE)->name = NULL; + } + return SUCCEED; +} + +/*------------------------------------------------------------------------- + * Function: init_nametab + * + * Purpose: this function will initialize the name hash table. + * + + * Return: SUCCEED, table is initialzed. FAIL,otherwise. + * + * In : + SIZE: the hashtable SIZE. + name_hashtab: pointer to the hash table. + + *------------------------------------------------------------------------- + */ +int init_nametab(int SIZE, struct name_table * name_hashtab) { + + int i; + + if(name_hashtab == NULL) { + printf("cannot allocate memory for name hashing table.\n"); + return FAIL; + } + for (i=0;i < SIZE; i++) { + (name_hashtab+i%SIZE)->name = NULL; + (name_hashtab+i%SIZE)->next = NULL; + } + return SUCCEED; +} + +/*------------------------------------------------------------------------- + * Function: get_name + * + * Purpose: obtain the name of the object + * + * Return: the object name + * + * In : objref: reference number of the current object. + SIZE: the hashtable SIZE. + hashtab: pointer to the hash table + pcheck_get: a flag to check errors + + *------------------------------------------------------------------------- + */ + +char* get_name(int objref,int SIZE,struct table*hashtab, int* pcheck_get) { + + struct table *np; + char* tempname; + + np = hashtab+objref%SIZE; + + for (np = hashtab+objref%SIZE; np!=NULL;np=np->next){ + + if (np->ref==objref){ + + if (np->name == NULL) { + *pcheck_get = -1; + return NULL; + } + + else { + tempname = malloc(strlen(np->name)+1); + if(tempname == NULL) { + *pcheck_get = -2; + return NULL; + } + strcpy(tempname,np->name); + return tempname; + } + } + } + + *pcheck_get = 0; + return NULL; +} + + +/*------------------------------------------------------------------------- + * Function: set_name + * + * Purpose: store the name of the object into the hash table + * + * Return: SUCCEED: the name is either set before or set in this routine + * FAIL: the name is not set properly + * + * In : objref: reference number of the current object + SIZE: the hashtable SIZE + hashtab: hash table + namein: object name + + *------------------------------------------------------------------------- + */ + + +int set_name(int objref,int SIZE,struct table*hashtab, char* namein) { + + struct table *np; + struct table *temptr; + + temptr = malloc(sizeof(struct table)); + if(temptr == NULL) { + printf("not enough memory to be allocated. \n"); + return FAIL; + } + + np = hashtab+objref%SIZE; + if(namein == NULL) { + printf("error in inputting name into the table.\n"); + return FAIL; + } + + for (np = hashtab+objref%SIZE; np!= NULL;np = np->next){ + if (np->ref==objref){ + /* the name is set already, don't do anything.*/ + return SUCCEED; + } + if (np->next == NULL) { + np->next = temptr; + temptr->ref = objref; + temptr->next = NULL; + temptr->name = malloc(strlen(namein)+1); + if(temptr->name == NULL) { + printf("error in allocating memory. \n"); + return FAIL; + } + strcpy(temptr->name,namein); + return SUCCEED; + } + } + return SUCCEED; +} + + +/*------------------------------------------------------------------------- + * Function: lookup_name + * + * Purpose: 1. look up whether the same name is used for different objects + 2. then update the table + * + * Return: 1, if the name is in the name hash table. + 0, if the name is to be added into the name table. + -1, otherwise. + * + * In : + size: the hashtable SIZE. + nametab: name hash table + name: the name to be looked up + + *------------------------------------------------------------------------- + */ + +int lookup_name(char* name, int size,struct name_table *nametab) { + + /* temporary pointer of the name table that points to the beginning + address of the current bucket.*/ + struct name_table *np; + + /* temporary pointer of the added name table.*/ + struct name_table *temptr; + + if(name == NULL) { + printf("the name to be looked up is NULL.\n"); + return -1; + } + + temptr = malloc(sizeof(struct name_table)); + if(temptr == NULL) { + printf("not enough memory to be allocated. \n"); + return -1; + } + + np = nametab+hash_fun(name,size); + + temptr->name = malloc(strlen(name)+1); + if(temptr->name == NULL) { + printf("not enough memory to be allocated to table name.\n"); + return -1; + } + + /* look through the linked list starting from the current bucket. + If the name is found, return 1, otherwise, return 0 + after inserting the new bucket. */ + + for(np = nametab+hash_fun(name,size); np!= NULL;np = np->next) { + if(np->name == NULL) { + np->name = malloc(strlen(name)+1); + if(np->name == NULL) { + printf("cannot allocate memory for object name.\n"); + return -1; + } + strcpy(np->name,name); + free(temptr->name); + free(temptr); + return 0; + } + if(strcmp(name,np->name)==0){ + free(temptr->name); + free(temptr); + return 1; + } + if (np->next == NULL) { + np->next = temptr; + temptr->next = NULL; + strcpy(temptr->name,name); + return 0; + } + } + return -1; +} + + +/*------------------------------------------------------------------------- + * Function: hash_fun + * + * Purpose: to get the hash value based on the key + * + * Return: No. of the hashtable + * + * In : name: object name + size: the hashtable size. + + *------------------------------------------------------------------------- + */ +int hash_fun(char *name,int size) { + +int hashval; + + for (hashval = 0;*name !='\0';) + hashval += *name++; + return(hashval%size); + +} + +/*------------------------------------------------------------------------- + * Function: freenametable + * + * Purpose: free the memory of hash table + * + * Return: 0 + * + * In : + SIZE: the hashtable SIZE. + nametab: hash table of the name + + *------------------------------------------------------------------------- + */ +int freenametable(int SIZE,struct name_table *nametab) { + + struct name_table *np,*temptr,*temptr1; + int i; + + if(nametab == NULL) return 0; + /* we first free the additional linked items of the hashtable, + and then free the whole hash table. */ + for (i = 0;i < SIZE; i++) { + np = nametab+i; + temptr1 = np->next; + while(temptr1 != NULL) { + temptr = temptr1; + temptr1 = temptr1->next; + free(temptr->name); + free(temptr); + } + if(np->name !=NULL) free(np->name); + } + free(nametab); + return 0; +} + + +/*------------------------------------------------------------------------- + * Function: freetable + * + * Purpose: free the memory of hash table + * + * Return: 0 + * + * In : + SIZE: the hashtable SIZE. + nametab: hash table + + *------------------------------------------------------------------------- + */ +int freetable(int SIZE,struct table *hashtab) { + + struct table *np,*temptr,*temptr1; + int i; + if(hashtab == NULL) return 0; + + /* we first free the additional linked items of the hashtable, + and then free the whole hash table. */ + for (i =0;i < SIZE; i++) { + np = hashtab+i; + temptr1 = np->next; + while(temptr1 != NULL) { + temptr = temptr1; + temptr1 = temptr1->next; + free(temptr->name); + free(temptr); + } + if(np->name != NULL) free(np->name); + } + + free(hashtab); + return 0; +} + +/*------------------------------------------------------------------------- + * Function: mkstr + * + * Purpose: make hdf5 string type + * + * Return: type + * + * In : + size: String Size + H5T_str_t: pad + + *------------------------------------------------------------------------- + */ + +hid_t mkstr(int size, H5T_str_t pad) { + + hid_t type; + + if((type=H5Tcopy(H5T_C_S1))<0) return -1; + if((H5Tset_size(type,size))<0) return -1; + if((H5Tset_strpad(type,pad))<0) return -1; + + return type; +} + +/*------------------------------------------------------------------------- + * Function: h4_transnumattr + * + * Purpose: translate reference number into hdf5 attribute + * + * Return: FAIL if failed, SUCCEED if successful. + * + * In : + h5g: hdf5 group id + refname: reference name + group_ref: reference number + + *------------------------------------------------------------------------- + */ +int h4_transnumattr(hid_t h5g,const char *refname,uint16 group_ref) { + + hid_t h5memtype; + hid_t h5a_id; + hid_t h5a_sid; + herr_t ret; + + h5a_sid = H5Screate(H5S_SCALAR); + + if (h5a_sid < 0) { + fprintf(stderr,"failed to create attribute space for HDF4_REF_NUM. \n"); + return FAIL; + } + + h5a_id = H5Acreate(h5g,refname,H5T_STD_U16BE,h5a_sid,H5P_DEFAULT); + + if(h5a_id <0) { + fprintf(stderr,"failed to obtain attribute id for HDF4_REF_NUM. \n"); + H5Sclose(h5a_sid); + return FAIL; + } + + if(H5Tget_size(H5T_NATIVE_CHAR)== sizeof(uint16)) + h5memtype = H5T_NATIVE_UCHAR; + else if(H5Tget_size(H5T_NATIVE_SHORT)== sizeof(uint16)) + h5memtype = H5T_NATIVE_USHORT; + else if(H5Tget_size(H5T_NATIVE_INT) == sizeof(uint16)) + h5memtype = H5T_NATIVE_UINT; + else if(H5Tget_size(H5T_NATIVE_LONG)== sizeof(uint16)) + h5memtype = H5T_NATIVE_ULONG; + + ret = H5Awrite(h5a_id,h5memtype,(void *)&group_ref); + + if(ret <0) { + printf("failed to obtain attribute.\n "); + H5Sclose(h5a_sid); + H5Aclose(h5a_id); + return FAIL; + } + + ret = H5Sclose(h5a_sid); + ret = H5Aclose(h5a_id); + return SUCCEED; +} + + +/*------------------------------------------------------------------------- + * Function: h4_transpredattrs + * + * Purpose: translate predefined attributes into hdf5 attribute + * predefined attributes include HDF4 OBJECT TYPE, + HDF4 OBJECT NAME, HDF4 CLASS etc. They are all in + H5T_STRING format. + + * Return: FAIL if failed, SUCCEED if successful. + * + * In : + h5g: group id + attrname: attribute name + data: attribute data + + *------------------------------------------------------------------------- + */ +int h4_transpredattrs(hid_t h5g,const char *attrname,char*data){ + + hsize_t h5str_size; + hid_t h5a_id; + hid_t h5a_sid; + hid_t h5str_type; + herr_t ret; + + if(data == NULL) { + printf("attribute data is not available.\n"); + return FAIL; + } + + h5str_size = strlen(data); + + if ((h5str_type = mkstr(h5str_size,H5T_STR_NULLTERM))<0) { + printf("error in making string for predefined ATTR. \n"); + return FAIL; + } + + h5a_sid = H5Screate(H5S_SCALAR); + + if (h5a_sid < 0) { + printf("failed to create attribute space for HDF4_OBJECT. \n"); + return FAIL; + } + + h5a_id = H5Acreate(h5g,attrname,h5str_type,h5a_sid,H5P_DEFAULT); + + if(h5a_id <0) { + fprintf(stderr,"failed to obtain attribute id for HDF4_OBJECT. \n"); + H5Sclose(h5a_sid); + return FAIL; + } + + ret = H5Awrite(h5a_id,h5str_type,(void *)data); + + if(ret <0) { + fprintf(stderr,"failed to obtain attribute.\n "); + H5Aclose(h5a_id); + H5Sclose(h5a_sid); + return FAIL; + } + ret = H5Sclose(h5a_sid); + ret = H5Aclose(h5a_id); + return SUCCEED; +} + +/*------------------------------------------------------------------------- + * Function: vg_transattrs + * + * Purpose: translate predefined vgroup attributes into hdf5 attribute + * + * Return: FAIL if failed, SUCCEED if successful. + * + * In : + h4vg: hdf4 vgroup id + h5g: hdf5 group id + + *------------------------------------------------------------------------- + */ + +int vg_transattrs(int32 h4vg,hid_t h5g) { + + /* define variables for hdf4. */ + char vgroup_name[VGNAMELENMAX]; + char vgroup_class[VGNAMELENMAX]; + char vgattr_name[MAX_NC_NAME]; + char obtype[MAX_NC_NAME]; + + int32 vgroup_cref; + int32 num_vgattr; + int32 count_vgattr; + int32 vg_atype; + int32 attr_size; + + size_t sh4_size; + size_t sh4_amemsize; + + /* define variables for hdf5. */ + hid_t sh5a_sid; + hid_t sh5a_id; + hid_t sh5_atype; + hid_t sh5_amemtype; + hid_t sh5str_type; + hid_t sh5str_memtype; + hsize_t sh5dims[MAX_VAR_DIMS]; + void* vg_adata; + herr_t sret; + int i; + + num_vgattr = Vnattrs(h4vg); + + for (i = 0;i =0(vdata field).\n"); + return FAIL; + } + + for (i = 0;i < snum_vdattrs; i++) { + + /* if the field_index is 0, no field attribute exists, only + VDATA attributes are converted.*/ + + if (VSattrinfo(vdata_id,field_index,i,svdattr_name,&svd_atype, + &count_svdadata,NULL)== FAIL){ + printf("unable to obtain attribute information. \n"); + return FAIL; + } + + if(svdattr_name[0] == '\0') { + svdrepattr_name = trans_obj_name(DFTAG_VG,i); + strcpy(svdattr_name,svdrepattr_name); + free(svdrepattr_name); + } + + if (field_index == -1); + + else if (field_index != -1 && attr_name != NULL) { + + strcat(svdattr_name,":"); + strcat(svdattr_name,attr_name); + } + + else { + + strcat(svdattr_name,":"); + strcat(svdattr_name,"HDF4_VDATA_ATTR_"); + if(conv_int_str(field_index,refstr)==FAIL) { + printf("error in converting vdata field index to string.\n"); + return FAIL; + } + strcat(svdattr_name,refstr); + + } + + /* converting attribute data type into the corresponding hdf5 data type */ + + if(h4type_to_h5type(svd_atype,&sh5_amemtype,&sh4_amemsize, + &sh4_asize,&sh5_atype)==FAIL){ + printf("fail to translate vdata attribute datatype from H4 to H5.\n"); + return FAIL; + } + + svd_adata = malloc(sh4_amemsize * count_svdadata); + + if(svd_adata == NULL) { + printf("fail to allocate memory for vdata attribute data.\n"); + return FAIL; + } + + if(VSgetattr(vdata_id,field_index,i,(VOIDP)svd_adata)==FAIL){ + printf("error in getting attributes of vdata. \n"); + free(svd_adata); + return FAIL; + } + + /* now do attribute-transferring: + 1. deal with string data type + 2. set attribute space + 3. get attribute name */ + + if (sh5_atype == H5T_STRING) { + + if ((sh5str_type = mkstr(count_svdadata, + H5T_STR_NULLTERM))<0) { + printf("error in making string for vdata attribute. \n"); + free(svd_adata); + return FAIL; + } + + if ((sh5str_memtype = mkstr(count_svdadata*sh4_amemsize, + H5T_STR_NULLTERM))<0) { + printf("error in making memory string for vdata attribute. \n"); + free(svd_adata); + return FAIL; + } + + sh5a_sid = H5Screate(H5S_SCALAR); + + if (sh5a_sid < 0) { + printf("failed to create attribute space for "); + printf("HDF4_OBJECT_TYPE VDATA. \n"); + free(svd_adata); + return FAIL; + } + + + sh5a_id = H5Acreate(h5dset,svdattr_name,sh5str_type, + sh5a_sid,H5P_DEFAULT); + + if (sh5a_id <0) { + printf("failed to obtain attribute id for"); + printf(" HDF4_OBJECT_TYPE VDATA. \n"); + H5Sclose(sh5a_sid); + free(svd_adata); + return FAIL; + } + + sret = H5Awrite(sh5a_id,sh5str_memtype,(void *)svd_adata); + + if (sret <0) { + printf("fail to write vdata attr into hdf5 dataset attr\n "); + H5Sclose(sh5a_sid); + H5Aclose(sh5a_id); + free(svd_adata); + return FAIL; + } + + free(svd_adata); + sret = H5Sclose(sh5a_sid); + sret = H5Aclose(sh5a_id); + } + + else { + + if(count_svdadata == 1) { + sh5a_sid = H5Screate(H5S_SCALAR); + + if (sh5a_sid < 0) { + printf("failed to create scalar space id for hdf5 attribute "); + printf("of dataset converted from attribute of VDATA.\n"); + free(svd_adata); + return FAIL; + } + } + else { + sh5dims[0] = count_svdadata; + sh5a_sid = H5Screate_simple(1,sh5dims,NULL); + + if (sh5a_sid < 0) { + printf("failed to create simple space id for hdf5 attribute "); + printf("of dataset converted from attribute of VDATA.\n"); + free(svd_adata); + return FAIL; + } + } + + sh5a_id = H5Acreate(h5dset,svdattr_name,sh5_atype, + sh5a_sid,H5P_DEFAULT); + + if(sh5a_id <0) { + printf("failed to create attribute id for hdf5 attribute "); + printf("of dataset converted from attribute of VDATA.\n"); + H5Sclose(sh5a_sid); + free(svd_adata); + return FAIL; + } + + sret = H5Awrite(sh5a_id,sh5_amemtype,(void *)svd_adata); + + if(sret <0) { + printf("failed to write attribute data for hdf5 attribute "); + printf("of dataset converted from attribute of VDATA.\n"); + H5Sclose(sh5a_sid); + H5Aclose(sh5a_id); + free(svd_adata); + return FAIL; + } + + sret = H5Aclose(sh5a_id); + sret = H5Sclose(sh5a_sid); + free(svd_adata); + } + } + return SUCCEED; +} +/*------------------------------------------------------------------------- + * Function: gen_h5comptype + * + * Purpose: generate hdf5 compound data type + + * + * Return: FAIL if failed, SUCCEED if successful. + * + * In : + vdata_id: vdata identifier + nfields: number of fields + sh4size: pointer to datatype size in memory + sh4memsize: pointer to datatype size in memory + sh5type: pointer to hdf5 datatype + sh5memtype: pointer to actual hdf5 datatype in memory + h5_ctype: hdf5 compound datatype + h5_cmemtype: hdf5 compound datatype in memory + Out: + Modifications: + + *------------------------------------------------------------------------- + */ + +int gen_h5comptype(int32 vdata_id,int32 nfields, + size_t* sh4size,size_t* sh4memsize, + hid_t* sh5type,hid_t* sh5memtype, + hid_t h5_ctype,hid_t h5_cmemtype) { + + /*char fieldname[MAX_NC_NAME]; + char* temp_fieldname;*/ + char* fieldname; + int32 fieldorder; + int32 fieldsize; + size_t fil_offset; + size_t mem_offset; + size_t fieldsizef; + size_t fielddim[1]; + hid_t h5str_type; + int i; + + fil_offset = 0; + mem_offset = 0; + fieldsizef = 0; + + + + for (i =0;i< nfields;i++) { + + fieldname = NULL; + fieldorder = VFfieldorder(vdata_id,i); + /* printf(" %d fieldorder%d\n",i,fieldorder);*/ + if(fieldorder == FAIL){ + printf("error in obtaining fieldorder.\n"); + return FAIL; + } + /* temp_fieldname = VFfieldname(vdata_id,i); + if(temp_fieldname== NULL) { + printf("fail to obtain Vdata field name. \n"); + return FAIL; + } + + strncpy(fieldname,temp_fieldname,strlen(temp_fieldname)); + + + free(temp_fieldname);*/ + + fieldname = VFfieldname(vdata_id,i); + if(fieldname == NULL){ + printf("fail to obtain Vdata field name. \n"); + return FAIL; + } + + + fieldsize = VFfieldesize(vdata_id,i); + if(fieldsize == FAIL) { + printf("error in obtaining fieldsize of vdata field.\n"); + return FAIL; + } + + if(sh5type[i] == H5T_STRING) { + /* printf("sh4size in the string %d\n",sh4size[i]); + printf("fieldsize in the string %d\n",fieldsize);*/ + if ((h5str_type = mkstr(sh4size[i],H5T_STR_NULLTERM))<0) { + printf("error in making string of hdf5 string. \n"); + return FAIL; + } + sh5type[i] = h5str_type; + } + + if (sh5memtype[i] == H5T_STRING) { + + if((h5str_type = mkstr(sh4memsize[i],H5T_STR_NULLTERM))<0){ + printf("error in making string for VDATA in memory. \n"); + return FAIL; + } + sh5memtype[i] = h5str_type; + + } + + fielddim[0] = fieldorder; + + /* if field type is an array, use H5Tinsert_array.*/ + + if (fielddim[0] == 1) { + + /* printf("i%d,sh5type[i] %d\n",i,sh5type[i]); + printf("i%d,fieldname%s\n",i,fieldname); + printf("i%d,fil_offset%d\n",i,fil_offset);*/ + if(H5Tinsert(h5_ctype,fieldname,fil_offset,sh5type[i])<0) { + printf("error inserting hdf5 compound datatype while "); + printf("converting vdata.\n"); + return FAIL; + } + + /* printf("i%d,sh5memtype[i] %d\n",i,sh5memtype[i]); + printf("i%d,fieldname%s\n",i,fieldname); + printf("i%d,mem_offset%d\n",i,mem_offset);*/ + if(H5Tinsert(h5_cmemtype,fieldname,mem_offset,sh5memtype[i])<0){ + printf("error inserting hdf5 compound datatype of memory"); + printf(" while converting vdata.\n"); + return FAIL; + } + } + + else { + + if(H5Tinsert_array(h5_ctype,fieldname,fil_offset,1,fielddim, + NULL,sh5type[i])<0) { + printf("error inserting array into hdf5 compound datatype. \n"); + return FAIL; + } + /* printf("i%d,sh5memtype[i] %d\n",i,sh5memtype[i]); + printf("i%d,fielddim[0]%d\n",i,fielddim[0]); + printf("i%d,fieldname%s\n",i,fieldname); + printf("i%d,mem_offset%d\n",i,mem_offset);*/ + if(H5Tinsert_array(h5_cmemtype,fieldname,mem_offset,1,fielddim, + NULL,sh5memtype[i])<0) { + printf("error inserting array into hdf5 compound datatype for memory. \n"); + return FAIL; + } + + + } + + /* fieldsizef = (size_t)fieldsize; + fil_offset = fil_offset + fieldsizef; + mem_offset = mem_offset + sh4memsize[i];*/ + + fil_offset = fil_offset + sh4size[i]*fieldorder; + mem_offset = mem_offset + sh4memsize[i]*fieldorder; + /* free(fieldname);*/ + } + + return SUCCEED; +} + + + + + + + + + + + + + + diff --git a/tools/h4toh5vgroup.c b/tools/h4toh5vgroup.c new file mode 100644 index 0000000..14e1c0f --- /dev/null +++ b/tools/h4toh5vgroup.c @@ -0,0 +1,768 @@ +/*** This file is the main program of converter. ***/ + +#include "h4toh5main.h" + + +/*------------------------------------------------------------------------- + * Function: Vgroup_h4_to_h5 + * + * Purpose: translate different Vgroup objects: vgroup,vdata,sds,image + into hdf5 datasets and recursively call the routine + * + * Return: FAIL if failed, SUCCEED if successful. + * + * In : + file_id: hdf4 file identifier + vgroup_id: hdf4 vgroup id + sd_id: sd interface id + h5_group: hdf5 group id + h5_dimgroup: hdf5 dimensional scale group id + h5_palgroup: hdf5 palette group id + Out: + + Modification: + *------------------------------------------------------------------------- + */ + +int Vgroup_h4_to_h5(int32 file_id,int32 vgroup_id,int32 sd_id,hid_t h5_group,hid_t h5_dimgroup,hid_t h5_palgroup) + +{ + + int32 vgroup_tag; + int32 vgroup_ref; + int32 obj_tag; + int32 obj_ref; + int32 num_gobjects; + int i; + + char refstr[5]; + char vgroup_class[VGNAMELENMAX]; + char vgroup_name[VGNAMELENMAX]; + + char* h5pgroup_name; + + int check_vgname; + hid_t h5_pgroup; + + vgroup_tag = VQuerytag(vgroup_id); + if(vgroup_tag == FAIL) { + printf("error in obtaining vgroup tag.\n"); + return FAIL; + } + + vgroup_ref = VQueryref(vgroup_id); + if(vgroup_ref == FAIL) { + printf("error in obtaining vgroup reference.\n"); + return FAIL; + } + + if(Vgetname(vgroup_id,vgroup_name) == FAIL) { + printf("error in obtaining vgroup name.\n"); + return FAIL; + } + + if(Vgetclass(vgroup_id,vgroup_class) == FAIL) { + printf("error in obtaining vgroup class name. \n"); + return FAIL; + } + + /*** ignore reserved HDF group ***/ + + if(vgroup_class != NULL) { + if(strcmp(vgroup_class,_HDF_ATTRIBUTE)==0) return SUCCEED; + if(strcmp(vgroup_class,_HDF_VARIABLE)==0) return SUCCEED; + if(strcmp(vgroup_class,_HDF_DIMENSION)==0) return SUCCEED; + if(strcmp(vgroup_class,_HDF_UDIMENSION)==0) return SUCCEED; + if(strcmp(vgroup_class,_HDF_CDF)==0) return SUCCEED; + if(strcmp(vgroup_class,GR_NAME)==0) return SUCCEED; + if(strcmp(vgroup_class,RI_NAME)==0) return SUCCEED; + } + + if(vgroup_name != NULL) + if(strcmp(vgroup_name,GR_NAME)==0) return SUCCEED; + + h5pgroup_name = get_name(vgroup_ref,estnum_vg,vg_hashtab,&check_vgname); + + if(h5pgroup_name == NULL && check_vgname == 0 ) { + printf("error,cannot find group\n"); + return FAIL; + } + + if(h5pgroup_name == NULL && check_vgname ==-1 ) { + printf("error,group name is not defined.\n"); + return FAIL; + } + + /* create a hdf5 group under h5_group.*/ + + h5_pgroup = H5Gcreate(h5_group,h5pgroup_name,0); + + if(h5_pgroup < 0) { + printf("error in creating group. \n"); + free(h5pgroup_name); + return FAIL; + } + + /* vgroup attributes into corresponding hdf5 group attributes.*/ + if(vg_transattrs(vgroup_id,h5_pgroup)==FAIL) { + printf("error in translating vgroup attributes into hdf5 group attr.\n"); + H5Gclose(h5_pgroup); + free(h5pgroup_name); + return FAIL; + } + + num_gobjects = Vntagrefs(vgroup_id); + + if(num_gobjects == FAIL) { + printf("error in obtaining number of objects in the vgroup. \n"); + H5Gclose(h5_pgroup); + free(h5pgroup_name); + return FAIL; + } + + if(Annoobj_h4_to_h5(file_id,vgroup_ref,vgroup_tag,h5_pgroup)==FAIL) { + printf("error in obtaining annotation of the vgroup.\n"); + H5Gclose(h5_pgroup); + free(h5pgroup_name); + return FAIL; + } + + for( i = 0;i $tmpfile +if [ ! -s $tmpfile ]; then + echo " Could not run the '$H5DUMP' command. The test can still proceed" + echo " but it may fail if '$H5DUMP' is needed to verify the output." + echo " You can make sure '$H5DUMP' is among your shell PATH and run" + echo " the test again. You may also visit http://hdf.ncsa.uiuc.edu" + echo " or email hdfhelp@ncsa.uiuc.edu for more information." + H5DUMP=: +fi +$RM $tmpfile + +# The build (current) directory might be different than the source directory. +if test "X$srcdir" = X; then + srcdir=. +fi +mkdir testfiles >/dev/null 2>&1 + +SRCDIR="$srcdir/testfiles" +OUTDIR="./testfiles" + +nerrors=0 +verbose=yes + +# Print a line-line message left justified in a field of 70 characters +# beginning with the word "Testing". +TESTING() +{ + SPACES=" " + echo "Testing $* $SPACES" |cut -c1-70 |tr -d '\012' +} + +# Run a test and print PASS or *FAIL*. If a test fails then increment +# the `nerrors' global variable and (if $verbose is set) display the +# difference between the actual and the expected hdf4 files. The +# expected hdf5 files are in testfiles/Expected directory. +# The actual hdf5 file is not removed if $HDF5_NOCLEANUP is to a non-null +# value. +CONVERT() +{ + # Run h4toh5 convert. + TESTING $h4toh5 $@ + + # + # Set up arguments to run the conversion test. + # The converter assumes all hdf4 files has the .hdf suffix as in the form + # of foo.hdf. It creates the corresponding hdf5 files with the .h5 suffix + # as in the form of foo.h5. One exception is that if exactly two file + # names are given, it treats the first argument as an hdf4 file and creates + # the corresponding hdf5 file with the name as the second argument, WITOUT + # any consideration of the suffix. (For this test script, in order to + # match the output hdf5 file with the expected hdf5 file, it expects the + # second file of the two-files tests has the .h5 suffix too.) + # + # If SRCDIR != OUTDIR, need to copy the input hdf4 files from the SRCDIR + # to the OUTDIR and transform the input file pathname because of the suffix + # convention mentioned above. This way, the hdf5 files are always created + # in the OUTDIR directory. + + INFILES="" + OUTFILES="" + MULTIRUN="" + + case "$1" in + "-m") # multiple files conversion + MULTIRUN="-m" + shift + for f in $* + do + if [ "$SRCDIR" != "$OUTDIR" ] + then + cp $SRCDIR/$f $OUTDIR/$f + fi + INFILES="$INFILES $f" + OUTFILES="$OUTFILES `basename $f .hdf`.h5" + shift + done + ;; + * ) # Single file conversion + case $# in + 1) if [ "$SRCDIR" != "$OUTDIR" ] + then + cp $SRCDIR/$1 $OUTDIR/$1 + fi + INFILES="$1" + OUTFILES="`basename $1 .hdf`.h5" + ;; + 2) # hdf4 file specified + if [ "$SRCDIR" != "$OUTDIR" ] + then + cp $SRCDIR/$1 $OUTDIR/$1 + fi + INFILES="$1" + OUTFILES="$2" + ;; + *) # Illegal + echo "Illegal arguments" + exit 1 + ;; + esac + ;; + esac + + # run the conversion and remove input files that have been copied over + ( + cd $OUTDIR + ../$h4toh5 $INFILES $OUTFILES 2>/dev/null + if [ "$SRCDIR" != "$OUTDIR" ] + then + $RM $INFILES + fi + ) + + # Verify results + result="passed" + for f in $OUTFILES + do + if $cmp $SRCDIR/Expected/$f $OUTDIR/$f + then + : + else + # Use h5dump to dump the files and verify the output. + outfile=`basename $f .h5` + expect_out=$outfile.expect + actual_out=$outfile.actual + + (cd $SRCDIR/Expected + $H5DUMP $outfile.h5 ) > $expect_out + (cd $OUTDIR + $H5DUMP $outfile.h5 ) > $actual_out + + if [ "passed" = $result -a ! -s $actual_out ] ; then + echo "*FAILED*" + nerrors="`expr $nerrors + 1`" + result=failed + test yes = "$verbose" && + echo " H5DUMP failed to produce valid output" + elif $cmp $expect_out $actual_out; then + : + else + if test "passed" = $result; then + echo "*FAILED*" + nerrors="`expr $nerrors + 1`" + result=failed + fi + test yes = "$verbose" && + echo " Actual result (*.actual) differs from expected result (*.expect)" && + $diff $expect_out $actual_out |sed 's/^/ /' + fi + fi + + # Clean up output file + if [ X = ${HDF5_NOCLEANUP:-X} ]; then + $RM $expect_out $actual_out + $RM $OUTDIR/$f + fi + done + if test "passed" = "$result"; then + echo " PASSED" + fi +} + + + +############################################################################## +############################################################################## +### T H E T E S T S ### +############################################################################## +############################################################################## + +$RM $OUTDIR/*.hdf $OUTDIR/*.tmp + +# +# The HDF5 filenames are created based upon the HDF4 filenames +# without the extension. +# + +# test for converting H5 groups to H4 Vgroups. +#CONVERT vg.hdf + + +# +# The test for conversion are the same as above with the only difference +# being that the HDF5 filenames are given explicitly. +# + +$RM $OUTDIR/*.tmp +CONVERT vg.hdf vg.h5 + +# +# Again, the test for conversion are the same as the first set of test. +# Here, multiple conversion are done on HDF4 files at one time. +# + +$RM $OUTDIR/*.hdf $OUTDIR/*.tmp +#CONVERT -m vg.hdf + +if test $nerrors -eq 0 ; then + echo "All h4toh5 tests passed." +fi + +exit $nerrors -- cgit v0.12