#include "h4toh5main.h" /*------------------------------------------------------------------------- * Function: Vdata_h4_to_h5 * * Purpose: translate Vdata object into hdf5 dataset * * Return: FAIL if failed, SUCCEED if successful. * * In : vdata_id: RI identifier group_id: hdf5 group id Out: Modification: *------------------------------------------------------------------------- */ int Vdata_h4_to_h5(int32 file_id,int32 vdata_id, hid_t group_id) { /* define variables for hdf4. */ int32 istat; int32 n_records; int32 vdata_ref; int32 vdata_tag; int32 interlace_mode; int32 vdata_size; int32 vdatamem_size; int32 field_index; int32 fieldorder; int32 fieldtype; int i; int32 nfields; int num_vd_attrs; int num_vd_field_attrs; VOIDP vd_data; char vdlabel[10]; char vdata_name[MAX_NC_NAME]; char fieldname[MAX_NC_NAME]; char vdata_class[VSNAMELENMAX]; char field_name_list[VSFIELDMAX*FIELDNAMELENMAX]; /* define varibles for hdf5. */ hid_t h5d_sid; hid_t h5dset; hid_t h5_ctype; hid_t h5_cmemtype; hid_t* h5memtype = NULL; hid_t* h5type = NULL; size_t* h4memsize = NULL; size_t* h4size = NULL; hsize_t h5_vddims[1]; char* h5cvdata_name; int check_vdname; /* get absolute path of vdata name. */ vdata_ref = VSQueryref(vdata_id); if (vdata_ref == FAIL) { printf("error in getting reference number. \n"); return FAIL; } vdata_tag = VSQuerytag(vdata_id); if (vdata_tag == FAIL) { printf("error in getting object tag number. \n"); return FAIL; } /* get the class name */ if(VSgetclass(vdata_id,vdata_class) == FAIL) { printf("error in obtaining class name. \n"); return FAIL; } /* get number of record,field_name,Size of a record and Name of the vdata*/ if(VSQueryvsize(vdata_id,&vdata_size)==FAIL) { printf("error in getting size of vdata. \n"); return FAIL; } if(vdata_size == 0) {/* empty vdata set. */ return SUCCEED; } /* obtain number of records, field name list, vdata name. */ if(VSinquire(vdata_id,&n_records,&interlace_mode, field_name_list,&vdata_size,vdata_name) == FAIL) { printf("error in inquiring vdata. \n"); return FAIL; } vdatamem_size = 0; nfields = VFnfields(vdata_id); if (nfields == FAIL) { printf("error in obtaining number of vdata fields. \n"); return FAIL; } h5memtype = calloc(nfields,sizeof(hid_t)); h5type = calloc(nfields,sizeof(hid_t)); h4memsize = calloc(nfields,sizeof(size_t)); h4size = calloc(nfields,sizeof(size_t)); 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; }