#include #include #include //#include #include "HDFIO.hh" #include int32 HDFIO::DataType2HDF(IObase::DataType nt){ switch(nt){ case Int8: return DFNT_INT8; // means data case Char8: // distinct from INT8.. return DFNT_CHAR8; // means string case Float32: return DFNT_FLOAT32; case Float64: return DFNT_FLOAT64; case Int32: return DFNT_INT32; case Int64: return DFNT_INT64; case Int16: return DFNT_INT16; case uInt8: return DFNT_UINT8; case uInt16: return DFNT_UINT16; case uInt32: return DFNT_UINT32; case uInt64: return DFNT_UINT64; case Char16: // unicode character type return DFNT_CHAR16; } printf("HDFIO::HDF2DataType(): Don't recognize type %d\n",(int)nt); return -1; } IObase::DataType HDFIO::HDF2DataType(int32 nt){ switch(nt){ case DFNT_INT8: return Int8; case DFNT_CHAR8: return Char8; case DFNT_FLOAT32: return Float32; case DFNT_FLOAT64: return Float64; case DFNT_INT32: return Int32; case DFNT_INT64: return Int64; case DFNT_INT16: return Int16; case DFNT_UINT8: case DFNT_UCHAR8: return uInt8; case DFNT_UINT16: return uInt16; case DFNT_UINT32: return uInt32; case DFNT_UINT64: return uInt64; case DFNT_CHAR16: return Char16; } printf("HDFIO::HDF2DataType(): Don't recognize type %d\n",(int)nt); return Error; } void HDFIO::create(int rank,CONST int *dims,IObase::DataType nt){ int32 hdims[5]; if(sid>=0) SDendaccess(sid); nitems++; index=nitems-1; for(int i=0;i=0) return; if(index>=nitems) index=nitems-1; if(index<0) index=0; if(sid>=0) SDendaccess(sid); sid=SDselect(fid,i); index=i; } void HDFIO::endaccess(){ if(sid<0) return; SDendaccess(sid); sid=-1; } HDFIO::HDFIO(CONST char *fname,AccessMode access):IObase(fname,access),sid(-1),fid(-1),hasread(0){ switch(accessmode){ case Read: fid=SDstart(filename,DFACC_RDONLY); break; case Write: fid=SDstart(filename,DFACC_CREATE); break; case Append: fid=SDstart(filename,DFACC_RDWR); break; default: puts("HDFIO: constructor... invalid accessmode"); break; } } HDFIO::~HDFIO(){ // printf("Destroying HDF file fid=%u, sid=%u\n",fid,sid); endaccess(); if(fid>=0) SDend(fid); // SDend(fid); } int HDFIO::isValid() { if(fid>=0) return 1; else return 0; } int HDFIO::write(IObase::DataType typeID,int rank,CONST int *dims,const void *data){ int32 origin[5]={0,0,0,0,0}; int32 stride[5]={1,1,1,1,1}; // kludge... we'll fix later int32 hdims[5]={0,0,0,0,0}; hasread=0; for(int i=0;i(data) ); } int HDFIO::readInfo(char *name,IObase::DataType &typeID,int &rank,int *dims,int maxdims){ int32 nt,nattr,hrank; int32 hdims[5]; if(hasread) select(index+1); else select((int)index); SDgetinfo(sid,name,&hrank,hdims,&nt,&nattr); rank=(int)hrank; for(int i=0;i0) return readAttributeInfo(number,fakename,typeID,nelem); else return -1; } int HDFIO::readAttribute(int number,void *data){ select(index); return (int)SDreadattr(sid,number,data); } int HDFIO::nAttributes(){ int32 nt,nattr,rank,dims[5]; char name[128]; select(index); SDgetinfo(sid,name,&rank,dims,&nt,&nattr); return (int)nattr; } //================Chunking Interface----------------------- int HDFIO::reserveChunk(IObase::DataType typeID,int rank,CONST int *dims){ //int32 origin[5]={0,0,0,0,0}; //int32 stride[5]={1,1,1,1,1}; // kludge... we'll fix later //int32 hdims[5]={0,0,0,0,0}; hasread=0; for(int i=0;i(data) ); } int HDFIO::readChunk(CONST int *dims,CONST int *origin,void *data){ int32 horigin[5]={0,0,0,0,0}; int32 stride[5]={1,1,1,1,1}; // kludge... we'll fix later int32 rank,nt,natt,hdims[5]={0,0,0,0,0}; char name[128]; select(index); SDgetinfo(sid,name,&rank,hdims,&nt,&natt); for(int i=0;i(dim) ); return 1; } int HDFIO::writeDimName(int dimnumber,CONST char *name){ if(sid<0) return -1; int32 dim_id = SDgetdimid(sid,dimnumber); if(dim_id<0) return -1; SDsetdimname(dim_id,(char *)name); return 1; } //===============F77 Interface Long8 f_hdf_open (char *file,char *accessname,int flen,int alen){ // would have used tolower(), but it doesn't exist everywhere.... :( IObase::AccessMode mode; if(*accessname=='R' || *accessname=='r') mode=IObase::Read; else if(*accessname=='W' || *accessname=='w' || *accessname=='C' || *accessname=='c') mode=IObase::Write; else if(*accessname=='A' || *accessname=='a') mode=IObase::Append; else { fprintf(stderr,"IEEEopen(): Error unknown option [%s] to open file %s\n", accessname,file); return 0; } IObase *fid=new HDFIO(file,mode); if(fid->isValid()) return (Long8)fid; else delete fid; // file open failed return 0; } Long8 f_hdf_openr (char *file,int flen){ file[flen]='\0'; // null terminate return (Long8)(new HDFIO(file,IObase::Read)); } Long8 f_hdf_openw (char *file,int flen){ file[flen]='\0'; // null terminate return (Long8)(new HDFIO(file,IObase::Create)); } Long8 f_hdf_opena (char *file,int flen){ file[flen]='\0'; // null terminate return (Long8)(new HDFIO(file,IObase::Append)); } IOFile HDFIOopen (char *file,char *accessname){ // Parse all of the ansi stdio access option strings IObase::AccessMode mode; if(!strcmp(accessname,"read") || !strcmp(accessname,"r") || !strcmp(accessname,"rb")) mode=IObase::Read; else if(*accessname=='a') mode=IObase::Append; else if(!strcmp(accessname,"write") || !strcmp(accessname,"create") || !strcmp(accessname,"wb")) mode = IObase::Write; else if(!strcmp(accessname,"w+") || !strcmp(accessname,"w+b") || !strcmp(accessname,"wb+")) mode=IObase::Append; else{ fprintf(stderr,"IEEEopen(): Error unknown option [%s] to open file %s\n", accessname,file); return 0; } IObase *fid=new HDFIO(file,mode); if(fid->isValid()) return (IOFile)fid; else delete fid; // file open failed return 0; // unknown option } IOFile HDFIOopenRead (char *file){ return (IOFile)(new HDFIO(file,IObase::Read)); } IOFile HDFIOopenWrite (char *file){ return (IOFile)(new HDFIO(file,IObase::Write)); } IOFile HDFIOopenAppend (char *file){ return (IOFile)(new HDFIO(file,IObase::Append)); }