예제 #1
0
void
ReadStringsT( hid_t iParent,
              const std::string &iAttrName,
              size_t iNumStrings,
              StringT *oStrings )
{
    ABCA_ASSERT( iParent >= 0, "Invalid parent in ReadStringsT" );

    // Open the attribute.
    hid_t attrId = H5Aopen( iParent, iAttrName.c_str(), H5P_DEFAULT );
    ABCA_ASSERT( attrId >= 0,
                 "Couldn't open attribute named: " << iAttrName );
    AttrCloser attrCloser( attrId );

    // Checking code.
    {
        hid_t attrFtype = H5Aget_type( attrId );
        DtypeCloser dtypeCloser( attrFtype );

        hid_t nativeDtype = GetNativeDtype<CharT>();
        ABCA_ASSERT( H5Tget_class( attrFtype ) ==
                     H5Tget_class( nativeDtype ) &&

                     H5Tget_sign( attrFtype ) ==
                     H5Tget_sign( nativeDtype ),

                     "Invalid datatype for stringT" );
    }

    hid_t attrSpace = H5Aget_space( attrId );
    ABCA_ASSERT( attrSpace >= 0,
                 "Couldn't get dataspace for attribute: " << iAttrName );
    DspaceCloser dspaceCloser( attrSpace );

    hssize_t numPoints = H5Sget_simple_extent_npoints( attrSpace );
    ABCA_ASSERT( numPoints > 0,
                 "Degenerate string dimensions in ReadStringsT" );

    // Create temporary char storage buffer.
    std::vector<CharT> charStorage( ( size_t )( 1 + numPoints ),
                                    ( CharT )0 );

    // Read into it.
    herr_t status = H5Aread( attrId, GetNativeDtype<CharT>(),
                             ( void * )&charStorage.front() );
    ABCA_ASSERT( status >= 0, "Couldn't read from attribute: " << iAttrName );

    // Extract 'em.
    ExtractStrings( oStrings, ( const CharT * )&charStorage.front(),
                    1 + numPoints, iNumStrings );
}
예제 #2
0
파일: rwHDF5.cpp 프로젝트: I2PC/scipion
DataType ImageBase::datatypeH5(hid_t h5datatype)
{
    H5T_sign_t h5sign = H5Tget_sign(h5datatype);

    //    if (h5sign == H5T_SGN_ERROR)
    //        REPORT_ERROR(ERR_IO, "datatypeHDF5: Integer sign error in dataset.");
    bool sign = (h5sign > H5T_SGN_NONE);
    size_t size = H5Tget_size(h5datatype);

    DataType dt;
    switch(H5Tget_class(h5datatype))
    {
    case H5T_FLOAT:
        {
            switch(size)
            {
            case 4:
                dt = DT_Float;
                break;
            case 8:
                dt = DT_Double;
                break;
            default:
                REPORT_ERROR(ERR_IO_SIZE, "datatypeHDF5: bad datatype size");
            }
        }
        break;
    case H5T_INTEGER:
        {
            switch(size)
            {
            case 1:
                dt = (sign)? DT_SChar : DT_UChar;
                break;
            case 2:
                dt = (sign)? DT_Short : DT_UShort;
                break;
            case 4:
                dt = (sign)? DT_Int : DT_UInt;
                break;
            case 8:
                dt = (sign)? DT_Long : DT_ULong;
                break;
            default:
                REPORT_ERROR(ERR_IO_SIZE, "datatypeHDF5: bad datatype size");
            }
        }
        break;
    case H5T_NO_CLASS:
    default:
        dt = DT_Unknown;
        break;
    }
    return dt;
}
예제 #3
0
//--------------------------------------------------------------------------
// Function:	IntType::getSign
///\brief	Retrieves the sign type for an integer type.
///\return	Valid sign type
///\exception	H5::DataTypeIException
// Programmer	Binh-Minh Ribler - 2000
//--------------------------------------------------------------------------
H5T_sign_t IntType::getSign() const
{
   H5T_sign_t type_sign = H5Tget_sign( id );  // C routine

   // Returns a valid sign type if no errors
   if( type_sign == H5T_SGN_ERROR )
   {
      throw DataTypeIException("IntType::getSign",
		"H5Tget_sign failed - returned H5T_SGN_ERROR for the sign type");
   }
   return( type_sign );
}
예제 #4
0
    DCDataType DCDataSet::getDCDataType() throw (DCException)
    {
        if (!opened)
            throw DCException(getExceptionString("getDCDataType: dataset is not opened"));

        DCDataType result = DCDT_UNKNOWN;

        H5T_class_t type_class = H5Tget_class(datatype);
        size_t type_size = H5Tget_size(datatype);
        H5T_sign_t type_signed = H5Tget_sign(datatype);

        if (type_class == H5T_INTEGER)
        {
            if (type_signed == H5T_SGN_NONE)
            {
                if (type_size == sizeof (uint64_t))
                    result = DCDT_UINT64;
                else
                    result = DCDT_UINT32;
            } else
            {
                if (type_size == sizeof (int64_t))
                    result = DCDT_INT64;
                else
                    result = DCDT_INT32;
            }
        } else
            if (type_class == H5T_FLOAT)
        {
            // float or double
            if (type_size == sizeof (float))
                result = DCDT_FLOAT32;
            else
                if (type_size == sizeof (double))
                result = DCDT_FLOAT64;
        }

        return result;
    }
/// Type translation: hdf5->nd
static nd_type_id_t hdf5_to_nd_type(hid_t tid)
{ size_t        p=H5Tget_precision(tid);
  
  switch(H5Tget_class(tid))
  { case H5T_INTEGER:
      { H5T_sign_t sign=H5Tget_sign(tid);
        switch(p)
        { 
#define   CASE(n) case n:  return (sign==H5T_SGN_NONE)?nd_u##n:nd_i##n
          CASE(8);
          CASE(16);
          CASE(32);
          CASE(64);
#undef    CASE
          default:;
        }
      }
      return nd_id_unknown;
    case H5T_FLOAT:
      if     (p==32) return nd_f32;
      else if(p==64) return nd_f64;
    default:         return nd_id_unknown;
  }
}
예제 #6
0
hdstype_t
dau1HdsType( hid_t h5type, int * status ) {

  H5T_class_t tclass = 0;
  size_t dsize = 0;
  hdstype_t thetype = HDSTYPE_NONE;

  if (*status != SAI__OK) return thetype;

  CALLHDFE( H5T_class_t, tclass,
           H5Tget_class( h5type ),
           DAT__HDF5E,
           emsRep("dat1Type_2", "datType: Error obtaining class of data type", status)
           );

  /* Number of bytes representing the type */
  dsize = H5Tget_size( h5type );

  if (*status == SAI__OK) {
    switch (tclass) {
    case H5T_INTEGER:
      {
        /* then need to know signed or unsigned int */
        H5T_sign_t dsign = H5Tget_sign( h5type );
        if (dsign < 0) {
          *status = DAT__HDF5E;
          emsRep("dat1Type_3", "datType: Error obtaining sign of an integer type", status );
        goto CLEANUP;
        }
        if (dsign == H5T_SGN_NONE) {
          if ( dsize == 1 ) {
            thetype = HDSTYPE_UBYTE;
          } else if (dsize == 2) {
            thetype = HDSTYPE_UWORD;
          } else {
            *status = DAT__TYPIN;
            emsRepf("dat1Type_3a",
                    "Unexpected number of bytes (%zu) in unsigned integer type",
                    status, dsize);
          }
        } else {
          /* Signed types */
          switch (dsize) {
          case 1:
            thetype = HDSTYPE_BYTE;
            break;
          case 2:
            thetype = HDSTYPE_WORD;
            break;
          case 4:
            thetype = HDSTYPE_INTEGER;
            break;
          case 8:
            thetype = HDSTYPE_INT64;
            break;
          default:
            *status = DAT__TYPIN;
            emsRepf("dat1Type_3b", "datType: Unexpected number of bytes in integer (%zu)",
                    status, dsize);
          }
        }
      }
      break;
    case H5T_FLOAT:
      if ( dsize == 4 ) {
        thetype = HDSTYPE_REAL;
      } else if (dsize == 8) {
        thetype = HDSTYPE_DOUBLE;
      } else {
        *status = DAT__FATAL;
        emsRepf("datType_5", "Error reading size of float data type. Got %zu bytes"
                " but only understand 4 and 8", status, dsize);
      }
      break;

    case H5T_STRING:
      thetype = HDSTYPE_CHAR;
      break;

    case H5T_BITFIELD:
      if ( dsize == 1 || dsize == 4 ) { /* on disk and in memory version */
        thetype = HDSTYPE_LOGICAL;
      } else {
        *status = DAT__FATAL;
        emsRepf("datType_5", "Error reading size of logical data type. Got %zu bytes"
                " but only understand 1 or 4", status, dsize);
      }
      break;

    default:
      *status = DAT__TYPIN;
      emsRep("datType_4", "dat1Type: Unexpected type class from dataset", status);
    }
  }

 CLEANUP:
  return thetype;

}
예제 #7
0
/* Purpose: retrieve datatype and dataspace information from file 
 * Parameters: H5Dataset *d -- The dataset to be initialized 
 * Return:  Returns a non-negative value if successful; otherwise returns a negative value.
 */
int H5Dataset_init(H5Dataset *d)
{
    int ret_value=0, i=0;
    hsize_t dims[H5S_MAX_RANK];
    hid_t did=-1, sid=-1, tid=-1, ftid=-1;
    unsigned int npoints;

    assert (d);
    H5Eclear();

    if (d->space.rank > 0)
        goto done; /* already called */

    if ( (did = H5Dopen(d->fid, d->fullpath)) < 0)
        THROW_H5LIBRARY_ERROR(d->error,ret_value, done);

    ftid = H5Dget_type(did);
    tid = H5Tget_native_type(ftid, H5T_DIR_ASCEND);
    if ( ftid > 0) H5Tclose(ftid);

    d->tclass = d->type.tclass = (H5Datatype_class_t)H5Tget_class(tid);
    d->type.size = H5Tget_size(tid);

    if  ( d->type.tclass == H5DATATYPE_INTEGER 
         || d->type.tclass == H5DATATYPE_FLOAT 
         || d->type.tclass == H5DATATYPE_BITFIELD 
         || d->type.tclass == H5DATATYPE_REFERENCE 
         || d->type.tclass == H5DATATYPE_ENUM ) 
    {
        d->type.order = (H5Datatype_order_t)H5Tget_order(tid);

        if (d->type.tclass == H5DATATYPE_INTEGER)
        {
            d->type.sign = (H5Datatype_sign_t)H5Tget_sign(tid);

            /* check palette for image */
            H5Dataset_readPalette(d, did);

            H5Dataset_check_image(d, did);
        }
    }
    else if (d->type.tclass == H5DATATYPE_COMPOUND) {
        d->type.nmembers = H5Tget_nmembers(tid );
        d->type.mnames = (char **)malloc(d->type.nmembers*sizeof(char*));
        d->type.mtypes = (int *)malloc(d->type.nmembers*sizeof(int));
        for (i=0; i<d->type.nmembers; i++) {
            hid_t mtid = -1;
            int mtype = 0, mclass, msign, msize;
            d->type.mnames[i] = H5Tget_member_name(tid, i);
            mtid = H5Tget_member_type(tid, i); 
            mclass = H5Tget_class(mtid);
            msign = H5Tget_sign(mtid);
            msize = H5Tget_size(mtid);
            mtype = mclass<<28 | msign<<24 | msize;
            d->type.mtypes[i] = mtype;
            H5Tclose(mtid);
        }
    }

    sid = H5Dget_space(did);
    d->space.rank = H5Sget_simple_extent_ndims(sid);
    if ( H5Sget_simple_extent_dims(sid, dims, NULL) < 0 )
        THROW_H5LIBRARY_ERROR(d->error,ret_value, done);

    if (d->space.rank<=0)
    {
        d->space.rank = 1;
        dims[0] = 1;
    }

    npoints = 1; 
    for (i=0; i<d->space.rank; i++)
    {
        d->space.dims[i] = dims[i];
        d->space.start[i] = 0;
        d->space.stride[i] = 1;
        d->space.count[i] = dims[i];
        npoints *= dims[i];
    }
    d->space.npoints = npoints;

done:
    if (sid > 0 ) H5Sclose(sid);
    if (tid > 0 ) H5Tclose(tid);
    if (did > 0 ) H5Dclose(did);
    return ret_value;
}
예제 #8
0
H5T_sign_t DataType::getSign () const {
    return Exception::check ("H5Tget_sign", H5Tget_sign (handle ()));
}
예제 #9
0
int hdf5read(char *name, struct descriptor_xd *xd)
{
  hid_t obj,type;
  H5G_stat_t statbuf;
  int item_type;
  int idx = 0;
  int status = FindItem(name, &obj, &item_type);
  if (status & 1)
  {
    if (item_type == H5G_DATASET) {
      int size;
      char dtype;
      int htype = 42;
      int is_signed;
      hsize_t ds_dims[64];
      hid_t space = H5Dget_space(obj);
      int n_ds_dims = H5Sget_simple_extent_dims(space,ds_dims,0);
      size_t precision;
      H5Sclose(space);
      type = H5Dget_type(obj);
      switch (H5Tget_class(type))
	{
	case H5T_COMPOUND:
	  {
            printf("Compound data is not supported, skipping\n");
            break;
          }
	case H5T_INTEGER:
	  precision = H5Tget_precision(type);
	  is_signed = (H5Tget_sign(type) != H5T_SGN_NONE);
          size = precision/8;
	  switch (precision)
            {
	    case 8:
              dtype = is_signed ? DTYPE_B : DTYPE_BU;
              htype = is_signed ? H5T_NATIVE_CHAR : H5T_NATIVE_UCHAR;
	      break;
	    case 16: 
              dtype = is_signed ? DTYPE_W : DTYPE_WU;
              htype = is_signed ? H5T_NATIVE_SHORT : H5T_NATIVE_USHORT;
	      break;
	    case 32: 
              dtype = is_signed ? DTYPE_L : DTYPE_LU;
              htype = is_signed ? H5T_NATIVE_INT : H5T_NATIVE_UINT;
	      break;
	    case 64: 
              dtype = is_signed ? DTYPE_Q : DTYPE_QU;
              htype = is_signed ? H5T_NATIVE_LLONG : H5T_NATIVE_ULLONG;
	      break;
	    default: 
	      dtype = 0;
	      break;
            }
	  PutData(obj, dtype, htype, size, n_ds_dims, ds_dims,0,xd);
	  break;
	case H5T_FLOAT:
	  precision = H5Tget_precision(type);
          size = precision/8;
	  switch (precision)
	    {
	    case 32: 
              dtype = DTYPE_NATIVE_FLOAT;
              htype = H5T_NATIVE_FLOAT;
	      break;
	    case 64: 
              dtype = DTYPE_NATIVE_DOUBLE;
              htype = H5T_NATIVE_DOUBLE;
              break;
	    default:
	      dtype = 0;
	      break;
            }
	  PutData(obj, dtype, htype, size, n_ds_dims, ds_dims,0,xd);
	  break;
	case H5T_TIME:
	  printf("dataset is time ---- UNSUPPORTED\n"); break;
	case H5T_STRING:
	  {
	    int slen = H5Tget_size(type);
	    hid_t st_id;
	    if (slen < 0) {
	      printf("Badly formed string attribute\n");
	      return;
	    }
#if H5_VERS_MAJOR>=1&&H5_VERS_MINOR>=6&&H5_VERS_RELEASE>=1
	    if(H5Tis_variable_str(type)) {                    
	      st_id = H5Tcopy (H5T_C_S1);
	      H5Tset_size(st_id, H5T_VARIABLE);
	    } else {
#endif
	      st_id = H5Tcopy (type);
	      H5Tset_cset(st_id, H5T_CSET_ASCII);
#if H5_VERS_MAJOR>=1&&H5_VERS_MINOR>=6&&H5_VERS_RELEASE>=1
	    } 
#endif	  
            if (H5Tget_size(st_id) > slen) {
	      slen = H5Tget_size(st_id);
	    }
	      H5Tset_size (st_id, slen);
	      PutData(obj, DTYPE_T, st_id, slen, n_ds_dims, ds_dims, 0, xd); 
	  }		
/*        printf("dataset is string\n"); */
/* 	  dtype = DTYPE_T; */
/* 	  htype = H5T_STRING; */
/* 	  PutData(obj, dtype, htype, 0, 0, 0, 1, xd); */
	  break;
	case H5T_BITFIELD:
	  printf("dataset is bitfield ---- UNSUPPORTED\n"); break;
	case H5T_OPAQUE:
	  printf("dataset is opaque ---- UNSUPPORTED\n"); break;
        case H5T_ARRAY:
	  printf("dataset is array ---- UNSUPPORTED\n"); break;
        case H5T_VLEN:
	  printf("dataset is vlen ---- UNSUPPORTED\n"); break;
        }
      H5Tclose(type);
    }
    else {
      int size;
      char dtype;
      int htype = 42;
      int is_signed;
      hsize_t ds_dims[64];
      hid_t space = H5Aget_space(obj);
      int n_ds_dims = H5Sget_simple_extent_dims(space,ds_dims,0);
      size_t precision;
      H5Sclose(space);
      type = H5Aget_type(obj);
      switch (H5Tget_class(type))
	{
	case H5T_COMPOUND:
	{
	  printf("Compound data is not supported, skipping\n");
	  break;
	}
	case H5T_INTEGER:
	  precision = H5Tget_precision(type);
	  is_signed = (H5Tget_sign(type) != H5T_SGN_NONE);
	  size = precision/8;
	  switch (precision)
	    {
	    case 8:
	      dtype = is_signed ? DTYPE_B : DTYPE_BU;
	      htype = is_signed ? H5T_NATIVE_CHAR : H5T_NATIVE_UCHAR;
	      break;
	    case 16: 
	      dtype = is_signed ? DTYPE_W : DTYPE_WU;
	      htype = is_signed ? H5T_NATIVE_SHORT : H5T_NATIVE_USHORT;
	      break;
	    case 32: 
	      dtype = is_signed ? DTYPE_L : DTYPE_LU;
	      htype = is_signed ? H5T_NATIVE_INT : H5T_NATIVE_UINT;
	      break;
	    case 64: 
	      dtype = is_signed ? DTYPE_Q : DTYPE_QU;
	      htype = is_signed ? H5T_NATIVE_LLONG : H5T_NATIVE_ULLONG;
	      break;
	    default: 
	      dtype = 0;
	      break;
	    }
	  PutData(obj, dtype, htype, size, n_ds_dims, ds_dims, 1, xd);
	  break;
	case H5T_FLOAT:
	  precision = H5Tget_precision(type);
	  size = precision/8;
	  switch (precision)
	    {
	    case 32: 
	      dtype = DTYPE_NATIVE_FLOAT;
	      htype = H5T_NATIVE_FLOAT;
	      break;
	    case 64: 
	      dtype = DTYPE_NATIVE_DOUBLE;
	      htype = H5T_NATIVE_DOUBLE;
	      break;
	    default:
	      dtype = 0;
	      break;
	    }
	  PutData(obj, dtype, htype, size, n_ds_dims, ds_dims,1, xd);
	  break;
	case H5T_TIME:
	  printf("dataset is time ---- UNSUPPORTED\n"); break;
	case H5T_STRING:
	  {
	    int slen = H5Tget_size(type);
	    hid_t st_id;
	    if (slen < 0) {
	      printf("Badly formed string attribute\n");
	      return;
	    }
#if H5_VERS_MAJOR>=1&&H5_VERS_MINOR>=6&&H5_VERS_RELEASE>=1
	    if(H5Tis_variable_str(type)) {                    
	      st_id = H5Tcopy (H5T_C_S1);
	      H5Tset_size(st_id, H5T_VARIABLE);
	    } else {
#endif
	      st_id = H5Tcopy (type);
	      H5Tset_cset(st_id, H5T_CSET_ASCII);
#if H5_VERS_MAJOR>=1&&H5_VERS_MINOR>=6&&H5_VERS_RELEASE>=1
	    } 
#endif	  
            if (H5Tget_size(st_id) > slen) {
	      slen = H5Tget_size(st_id);
	    }
	      H5Tset_size (st_id, slen);
	      PutData(obj, DTYPE_T, st_id, slen, n_ds_dims, ds_dims, 1, xd); 
	  }		
/* 	  dtype = DTYPE_T; */
/* 	  htype = H5T_STRING; */
/* 	  PutData(obj, dtype, htype, 0, 0, 0, 1, xd); */
	  break;
	case H5T_BITFIELD:
	  printf("dataset is bitfield ---- UNSUPPORTED\n"); break;
	case H5T_OPAQUE:
	  printf("dataset is opaque ---- UNSUPPORTED\n"); break;
	case H5T_ARRAY:
	  printf("dataset is array ---- UNSUPPORTED\n"); break;
	case H5T_VLEN:
	  printf("dataset is vlen ---- UNSUPPORTED\n"); break;
	}
      H5Tclose(type);
    }
  }
  return status;
}
예제 #10
0
파일: Common.cpp 프로젝트: zjnny/hdf
bool getDataType(hid_t hType,int &iDataType,int &iNBytes)
{
	if(hType < 0)
	{
		return false;
	}
	else
	{
		/*�õ�����������־��*/
		H5T_class_t hTypeClass = H5Tget_class(hType);
		if(hTypeClass == H5T_NO_CLASS)/*������ʹ��ï¿*/
		{
			return false;
		}
		if(hTypeClass == H5T_INTEGER) /*����ʱ*/
		{
			/*�õ���������ֽڴ�Ð*/
			size_t hSize = H5Tget_size(hType);
			if(hSize == 0)/*����ֽ�Î?*/
			{
				return false;
			}

			/*�õ�����Ƿ��з��*/
			H5T_sign_t hSign = H5Tget_sign(hType);
			if(hSign == H5T_SGN_ERROR)/*����Ŵ��ï¿*/
			{
				return false;
			}
			if(hSize == 1 && hSign == H5T_SGN_NONE)
			{
				iDataType = UNCHARTYPE;/*1�ֽ��޷�����ï¿*/
				iNBytes = 1;
			}
			if(hSize == 1 && hSign == H5T_SGN_2)
			{
				iDataType = CHARTYPE;/*1�ֽ��з�����ï¿*/
				iNBytes = 1;
			}
			if(hSize == 2 && hSign == H5T_SGN_NONE)
			{
				iDataType = UNSHORTTYPE;/*2�ֽ��޷�����ï¿*/
				iNBytes = 2;
			}
			if(hSize == 2 && hSign == H5T_SGN_2)
			{
				iDataType = SHORTTYPE;/*2�ֽ��з�����ï¿*/
				iNBytes = 2;
			}
			if(hSize == 4 && hSign == H5T_SGN_NONE)
			{
				iDataType = UNINTTYPE;/*4�ֽ��޷�����ï¿*/
				iNBytes = 4;
			}
			if(hSize == 4 && hSign == H5T_SGN_2)
			{
				iDataType = INTTYPE;/*4�ֽ��з�����ï¿*/
				iNBytes = 4;
			}
			if(hSize == 8 && hSign == H5T_SGN_NONE)
			{
				iDataType = UNINT64TYPE;/*8�ֽ��޷�����ï¿*/
				iNBytes = 8;
			}
			if(hSize == 8 && hSign == H5T_SGN_2)
			{
				iDataType = INT64TYPE;/*8�ֽ��з�����ï¿*/
				iNBytes = 8;
			}
		}
		else if(hTypeClass == H5T_FLOAT)/*������ʱ*/
		{
			/*�õ���������ֽڴ�Ð*/
			size_t hSize = H5Tget_size(hType);
			if(hSize == 0)/*����ֽ�Î?*/
			{
				return false;
			}
			if(hSize == 4)
			{
				iDataType = FLOATTYPE;/*4�ֽڸ�����*/
				iNBytes = 4;
			}
			else
			{
				iDataType = DOUBLETYPE;/*8�ֽ�˫������*/
				iNBytes = 8;
			}
		}
		else if(hTypeClass == H5T_STRING)/*������ʱ*/
		{
			iDataType = STRINGTYPE;
			iNBytes = 1;
		}
		else/*���������Ͳ�������*/
		{
			return false;
		}
	}

	return true;
}
예제 #11
0
H5T_sign_t DataType::sign() const {
    H5T_sign_t res = H5Tget_sign(hid);
    return res;
}
예제 #12
0
static AbcA::ArraySamplePtr
ReadStringArrayT( AbcA::ReadArraySampleCachePtr iCache,
                  hid_t iParent,
                  const std::string &iName,
                  const AbcA::DataType &iDataType )
{
    assert( iDataType.getExtent() > 0 );

    // Open the data set.
    hid_t dsetId = H5Dopen( iParent, iName.c_str(), H5P_DEFAULT );
    ABCA_ASSERT( dsetId >= 0, "Cannot open dataset: " << iName );
    DsetCloser dsetCloser( dsetId );

    // Read the digest, if there's a cache.
    AbcA::ArraySample::Key key;
    bool foundDigest = ReadKey( dsetId, "key", key );

    // If we found a digest and there's a cache, see
    // if we're in there, and return it if so.
    if ( foundDigest && iCache )
    {
        AbcA::ReadArraySampleID found = iCache->find( key );
        if ( found )
        {
            AbcA::ArraySamplePtr ret = found.getSample();
            assert( ret );
            if ( ret->getDataType() != iDataType )
            {
                ABCA_THROW( "ERROR: Read data type for dset: " << iName
                            << ": " << ret->getDataType()
                            << " does not match expected data type: "
                            << iDataType );
            }

            // Got it!
            return ret;
        }
    }

    // Okay, we haven't found it in a cache.

    // Read the data type.
    // Checking code.
    {
        hid_t dsetFtype = H5Dget_type( dsetId );
        DtypeCloser dtypeCloser( dsetFtype );

        hid_t nativeDtype = GetNativeDtype<CharT>();
        ABCA_ASSERT( H5Tget_class( dsetFtype ) ==
                     H5Tget_class( nativeDtype ) &&

                     H5Tget_sign( dsetFtype ) ==
                     H5Tget_sign( nativeDtype )

                     // CJH They can now be different
                     // sizes, because wchar_t is sometimes 16-bit,
                     // but we always store 32 bit.
                     // && H5Tget_size( dsetFtype ) ==
                     //H5Tget_size( nativeDtype ),

                     , "Invalid datatype for stringT" );
    }

    // String array datatypes require a "dimensions" to be stored
    // externally, since the strings themselves are stored in a compacted
    // array of rank 1.
    // This is an attribute called "dims" that lives in the dset itself.
    Dimensions realDims;
    ReadDimensions( dsetId, "dims", realDims );
    ABCA_ASSERT( realDims.rank() > 0,
                 "Degenerate rank in Dataset read" );

    // Read the data space.
    hid_t dspaceId = H5Dget_space( dsetId );
    ABCA_ASSERT( dspaceId >= 0, "Could not get dataspace for dataSet: "
                 << iName );
    DspaceCloser dspaceCloser( dspaceId );

    AbcA::ArraySamplePtr ret;

    H5S_class_t dspaceClass = H5Sget_simple_extent_type( dspaceId );

    if ( dspaceClass == H5S_SIMPLE )
    {
        ABCA_ASSERT( realDims.numPoints() > 0,
                     "Degenerate dims in Dataset read" );
        size_t totalNumStrings = realDims.numPoints() * iDataType.getExtent();

        // Get the dimensions
        Dimensions dims;
        int rank = H5Sget_simple_extent_ndims( dspaceId );
        ABCA_ASSERT( rank == realDims.rank(),
                     "H5Sget_simple_extent_ndims() failed." );

        HDimensions hdims;
        hdims.setRank( rank );
        rank = H5Sget_simple_extent_dims( dspaceId, hdims.rootPtr(), NULL );
        ABCA_ASSERT( rank == hdims.rank(),
                     "H5Sget_simple_extent_dims() "
                     "found inconsistent ranks."
                     << std::endl
                     << "Expecting rank: " << hdims.rank()
                     << " instead was: " << rank );
        
        dims = hdims;
        ABCA_ASSERT( dims.numPoints() > 0,
                     "Degenerate dims in Dataset read" );
        

        // Create temporary char storage buffer.
        size_t totalNumChars = dims.numPoints() + 1;
        std::vector<CharT> charStorage( totalNumChars, ( CharT )0 );
        
        // Read into it.
        herr_t status = H5Dread( dsetId, GetNativeDtype<CharT>(),
                                 H5S_ALL, H5S_ALL, H5P_DEFAULT,
                                 ( void * )&charStorage.front() );
        ABCA_ASSERT( status >= 0,
                     "Could not read string array from data set. Weird." );

        // Make an appropriately dimensionalized (and manageable)
        // array of strings using the ArraySamples.
        ret = AbcA::AllocateArraySample( iDataType,
                                         realDims );
        StringT *strings = reinterpret_cast<StringT*>(
            const_cast<void*>( ret->getData() ) );
        assert( strings != NULL );

        // This part is hard. We have to go through the one dimensional
        // array extracting each string.
        ExtractStrings<StringT,CharT>( strings,
                                       ( const CharT * )&charStorage.front(),
                                       totalNumChars,
                                       totalNumStrings );
    }
    else if ( dspaceClass == H5S_NULL )
    {
        // Num points should be zero here.
        ABCA_ASSERT( realDims.numPoints() == 0,
                     "Expecting zero points in dimensions" );

        ret = AbcA::AllocateArraySample( iDataType, realDims );
    }
    else
    {
        ABCA_THROW( "Unexpected scalar dataspace encountered." );
    }

    // Store if there is a cache.
    if ( foundDigest && iCache )
    {
        AbcA::ReadArraySampleID stored = iCache->store( key, ret );
        if ( stored )
        {
            return stored.getSample();
        }
    }

    // Otherwise, just leave! ArraySamplePtr returned by AllocateArraySample
    // already has fancy-dan deleter built in.
    // I REALLY LOVE SMART PTRS.
    return ret;
}
예제 #13
0
/*-------------------------------------------------------------------------
 * Function: diff_datasetid
 *
 * Purpose: check for comparable datasets and read into a compatible
 *  memory type
 *
 * Return: Number of differences found
 *
 * Programmer: Pedro Vicente, [email protected]
 *
 * Date: May 9, 2003
 *
 * Modifications: 
 *
 * October 2006:  Read by hyperslabs for big datasets.
 *
 *  A threshold of H5TOOLS_MALLOCSIZE (128 MB) is the limit upon which I/O hyperslab is done
 *  i.e., if the memory needed to read a dataset is greater than this limit, 
 *  then hyperslab I/O is done instead of one operation I/O 
 *  For each dataset, the memory needed is calculated according to
 *
 *  memory needed = number of elements * size of each element
 *
 *  if the memory needed is lower than H5TOOLS_MALLOCSIZE, then the following operations 
 *  are done
 *
 *  H5Dread( input_dataset1 )
 *  H5Dread( input_dataset2 )
 *
 *  with all elements in the datasets selected. If the memory needed is greater than 
 *  H5TOOLS_MALLOCSIZE, then the following operations are done instead:
 *
 *  a strip mine is defined for each dimension k (a strip mine is defined as a 
 *  hyperslab whose size is memory manageable) according to the formula
 *
 *  (1) strip_mine_size[k ] = MIN(dimension[k ], H5TOOLS_BUFSIZE / size of memory type)
 *
 *  where H5TOOLS_BUFSIZE is a constant currently defined as 1MB. This formula assures 
 *  that for small datasets (small relative to the H5TOOLS_BUFSIZE constant), the strip 
 *  mine size k is simply defined as its dimension k, but for larger datasets the 
 *  hyperslab size is still memory manageable.
 *  a cycle is done until the number of elements in the dataset is reached. In each 
 *  iteration, two parameters are defined for the function H5Sselect_hyperslab, 
 *  the start and size of each hyperslab, according to
 *
 *  (2) hyperslab_size [k] = MIN(dimension[k] - hyperslab_offset[k], strip_mine_size [k])
 *
 *  where hyperslab_offset [k] is initially set to zero, and later incremented in 
 *  hyperslab_size[k] offsets. The reason for the operation 
 *
 *  dimension[k] - hyperslab_offset[k]
 *
 *  in (2) is that, when using the strip mine size, it assures that the "remaining" part 
 *  of the dataset that does not fill an entire strip mine is processed.
 *
 *-------------------------------------------------------------------------
 */
hsize_t diff_datasetid( hid_t did1,
                        hid_t did2,
                        const char *obj1_name,
                        const char *obj2_name,
                        diff_opt_t *options)
{
 hid_t      sid1=-1;
 hid_t      sid2=-1;
 hid_t      f_tid1=-1;
 hid_t      f_tid2=-1;                
 hid_t      m_tid1=-1;
 hid_t      m_tid2=-1;                
 size_t     m_size1;
 size_t     m_size2;               
 H5T_sign_t sign1;
 H5T_sign_t sign2;                 
 int        rank1;
 int        rank2;
 hsize_t    nelmts1;
 hsize_t    nelmts2;
 hsize_t    dims1[H5S_MAX_RANK];
 hsize_t    dims2[H5S_MAX_RANK];
 hsize_t    maxdim1[H5S_MAX_RANK];
 hsize_t    maxdim2[H5S_MAX_RANK];
 const char *name1=NULL;            /* relative names */
 const char *name2=NULL;
 hsize_t    storage_size1;
 hsize_t    storage_size2;
 hsize_t    nfound=0;               /* number of differences found */
 int        cmp=1;                  /* do diff or not */
 void       *buf1=NULL;                  
 void       *buf2=NULL; 
 void       *sm_buf1=NULL;                
 void       *sm_buf2=NULL;
 size_t     need;                   /* bytes needed for malloc */
 int        i;

  /* Get the dataspace handle */
 if ( (sid1 = H5Dget_space(did1)) < 0 )
  goto error;

 /* Get rank */
 if ( (rank1 = H5Sget_simple_extent_ndims(sid1)) < 0 )
  goto error;

 /* Get the dataspace handle */
 if ( (sid2 = H5Dget_space(did2)) < 0 )
  goto error;

 /* Get rank */
 if ( (rank2 = H5Sget_simple_extent_ndims(sid2)) < 0 )
  goto error;

 /* Get dimensions */
 if ( H5Sget_simple_extent_dims(sid1,dims1,maxdim1) < 0 )
  goto error;

 /* Get dimensions */
 if ( H5Sget_simple_extent_dims(sid2,dims2,maxdim2) < 0 )
  goto error;

/*-------------------------------------------------------------------------
 * Get the file data type
 *-------------------------------------------------------------------------
 */

 /* Get the data type */
 if ( (f_tid1 = H5Dget_type(did1)) < 0 )
  goto error;

 /* Get the data type */
 if ( (f_tid2 = H5Dget_type(did2)) < 0 )
  goto error;

/*-------------------------------------------------------------------------
 * check for empty datasets
 *-------------------------------------------------------------------------
 */

 storage_size1=H5Dget_storage_size(did1);
 storage_size2=H5Dget_storage_size(did2);
 if (storage_size1<0 || storage_size2<0)
  goto error;

 if (storage_size1==0 || storage_size2==0)
 {
  if (options->m_verbose && obj1_name && obj2_name)
   printf("<%s> or <%s> are empty datasets\n", obj1_name, obj2_name);
  cmp=0;
  options->not_cmp=1;
 }

/*-------------------------------------------------------------------------
 * check for comparable TYPE and SPACE
 *-------------------------------------------------------------------------
 */

 if (diff_can_type(f_tid1,
                   f_tid2,
                   rank1,
                   rank2,
                   dims1,
                   dims2,
                   maxdim1,
                   maxdim2,
                   obj1_name,
                   obj2_name,
                   options)!=1)
 {
  cmp=0;
  options->not_cmp=1;
 }

/*-------------------------------------------------------------------------
 * memory type and sizes
 *-------------------------------------------------------------------------
 */
 if ((m_tid1=h5tools_get_native_type(f_tid1))<0)
  goto error;

 if ((m_tid2=h5tools_get_native_type(f_tid2))<0)
  goto error;

 m_size1 = H5Tget_size( m_tid1 );
 m_size2 = H5Tget_size( m_tid2 );

/*-------------------------------------------------------------------------
 * check for different signed/unsigned types
 *-------------------------------------------------------------------------
 */

 sign1=H5Tget_sign(m_tid1);
 sign2=H5Tget_sign(m_tid2);
 if ( sign1 != sign2 )
 {
  if (options->m_verbose && obj1_name) {
   printf("Comparison not supported: <%s> has sign %s ", obj1_name, get_sign(sign1));
   printf("and <%s> has sign %s\n", obj2_name, get_sign(sign2));
  }

  cmp=0;
  options->not_cmp=1;
 }

/*-------------------------------------------------------------------------
 * only attempt to compare if possible
 *-------------------------------------------------------------------------
 */
 if (cmp)
 {

/*-------------------------------------------------------------------------
 * get number of elements
 *-------------------------------------------------------------------------
 */
 nelmts1 = 1;
 for (i = 0; i < rank1; i++)
 {
  nelmts1 *= dims1[i];
 }

 nelmts2 = 1;
 for (i = 0; i < rank2; i++)
 {
  nelmts2 *= dims2[i];
 }

 assert(nelmts1==nelmts2);

/*-------------------------------------------------------------------------
 * "upgrade" the smaller memory size
 *-------------------------------------------------------------------------
 */

 if ( m_size1 != m_size2 )
 {
  if ( m_size1 < m_size2 )
  {
   H5Tclose(m_tid1);

   if ((m_tid1=h5tools_get_native_type(f_tid2))<0)
    goto error;

   m_size1 = H5Tget_size( m_tid1 );
  }
  else
  {
   H5Tclose(m_tid2);

   if ((m_tid2=h5tools_get_native_type(f_tid1))<0)
    goto error;

   m_size2 = H5Tget_size( m_tid2 );
  }
 }
 assert(m_size1==m_size2);

 /* print names */
 if (obj1_name) {
  name1=diff_basename(obj1_name);
 }
 if (obj2_name) {
  name2=diff_basename(obj2_name);
 }

 
/*-------------------------------------------------------------------------
 * read/compare
 *-------------------------------------------------------------------------
 */

 need = (size_t)(nelmts1*m_size1);  /* bytes needed */
 if ( need < H5TOOLS_MALLOCSIZE)
 {
  buf1 = HDmalloc(need);
  buf2 = HDmalloc(need);
 }

 if ( buf1!=NULL && buf2!=NULL)
 {
  if ( H5Dread(did1,m_tid1,H5S_ALL,H5S_ALL,H5P_DEFAULT,buf1) < 0 )
   goto error;
  if ( H5Dread(did2,m_tid2,H5S_ALL,H5S_ALL,H5P_DEFAULT,buf2) < 0 )
   goto error;

  /* array diff */
  nfound = diff_array(buf1,
                      buf2,
                      nelmts1,
                      (hsize_t)0,
                      rank1,
                      dims1,
                      options,
                      name1,
                      name2,
                      m_tid1,
                      did1,
                      did2);
 }
 
 else /* possibly not enough memory, read/compare by hyperslabs */
  
 {
  size_t        p_type_nbytes = m_size1; /*size of memory type */
  hsize_t       p_nelmts = nelmts1;      /*total selected elmts */
  hsize_t       elmtno;                  /*counter  */
  int           carry;                   /*counter carry value */
  unsigned int  vl_data = 0;             /*contains VL datatypes */
  
  /* stripmine info */
  hsize_t       sm_size[H5S_MAX_RANK];   /*stripmine size */
  hsize_t       sm_nbytes;               /*bytes per stripmine */
  hsize_t       sm_nelmts;               /*elements per stripmine*/
  hid_t         sm_space;                /*stripmine data space */
  
  /* hyperslab info */
  hsize_t       hs_offset[H5S_MAX_RANK]; /*starting offset */
  hsize_t       hs_size[H5S_MAX_RANK];   /*size this pass */
  hsize_t       hs_nelmts;               /*elements in request */
  hsize_t       zero[8];                 /*vector of zeros */
  
  /* check if we have VL data in the dataset's datatype */
  if (H5Tdetect_class(m_tid1, H5T_VLEN) == TRUE)
   vl_data = TRUE;
  
  /*
   * determine the strip mine size and allocate a buffer. The strip mine is
   * a hyperslab whose size is manageable.
   */
  sm_nbytes = p_type_nbytes;
  
  for (i = rank1; i > 0; --i) {
   sm_size[i - 1] = MIN(dims1[i - 1], H5TOOLS_BUFSIZE / sm_nbytes);
   sm_nbytes *= sm_size[i - 1];
   assert(sm_nbytes > 0);
  }
  
  sm_buf1 = malloc((size_t)sm_nbytes);
  sm_buf2 = malloc((size_t)sm_nbytes);
  
  sm_nelmts = sm_nbytes / p_type_nbytes;
  sm_space = H5Screate_simple(1, &sm_nelmts, NULL);
  
  /* the stripmine loop */
  memset(hs_offset, 0, sizeof hs_offset);
  memset(zero, 0, sizeof zero);

  for (elmtno = 0; elmtno < p_nelmts; elmtno += hs_nelmts) 
  {
   /* calculate the hyperslab size */
   if (rank1 > 0) 
   {
    for (i = 0, hs_nelmts = 1; i < rank1; i++) 
    {
     hs_size[i] = MIN(dims1[i] - hs_offset[i], sm_size[i]);
     hs_nelmts *= hs_size[i];
    }
    if (H5Sselect_hyperslab(sid1, H5S_SELECT_SET, hs_offset, NULL, hs_size, NULL)<0)
     goto error;
    if (H5Sselect_hyperslab(sid2, H5S_SELECT_SET, hs_offset, NULL, hs_size, NULL)<0)
     goto error;
    if (H5Sselect_hyperslab(sm_space, H5S_SELECT_SET, zero, NULL, &hs_nelmts, NULL)<0)
     goto error;
   } 
   else 
   {
    H5Sselect_all(sid1);
    H5Sselect_all(sid2);
    H5Sselect_all(sm_space);
    hs_nelmts = 1;
   } /* rank */

   if ( H5Dread(did1,m_tid1,sm_space,sid1,H5P_DEFAULT,sm_buf1) < 0 )
    goto error;
   if ( H5Dread(did2,m_tid2,sm_space,sid2,H5P_DEFAULT,sm_buf2) < 0 )
    goto error;
   
   /* get array differences. in the case of hyperslab read, increment the number of differences 
      found in each hyperslab and pass the position at the beggining for printing */
   nfound += diff_array(sm_buf1,
                        sm_buf2,
                        hs_nelmts,
                        elmtno,
                        rank1,
                        dims1,
                        options,
                        name1,
                        name2,
                        m_tid1,
                        did1,
                        did2);

   /* reclaim any VL memory, if necessary */
   if(vl_data)
   {
    H5Dvlen_reclaim(m_tid1, sm_space, H5P_DEFAULT, sm_buf1);
    H5Dvlen_reclaim(m_tid1, sm_space, H5P_DEFAULT, sm_buf2);
   }
   
   /* calculate the next hyperslab offset */
   for (i = rank1, carry = 1; i > 0 && carry; --i) 
   {
    hs_offset[i - 1] += hs_size[i - 1];
    if (hs_offset[i - 1] == dims1[i - 1])
     hs_offset[i - 1] = 0;
    else
     carry = 0;
   } /* i */
  } /* elmtno */
  
  H5Sclose(sm_space);
  /* free */
  if (sm_buf1!=NULL)
  {
   free(sm_buf1);
   sm_buf1=NULL;
  }
  if (sm_buf2!=NULL)
  {
   free(sm_buf2);
   sm_buf2=NULL;
  }
  
 } /* hyperslab read */

 }/*cmp*/

/*-------------------------------------------------------------------------
 * compare attributes
 * the if condition refers to cases when the dataset is a referenced object
 *-------------------------------------------------------------------------
 */

 if (obj1_name)
  nfound += diff_attr(did1,did2,obj1_name,obj2_name,options);

/*-------------------------------------------------------------------------
 * close
 *-------------------------------------------------------------------------
 */
 
 /* free */
 if (buf1!=NULL)
 {
  free(buf1);
  buf1=NULL;
 }
 if (buf2!=NULL)
 {
  free(buf2);
  buf2=NULL;
 }
 if (sm_buf1!=NULL)
 {
  free(sm_buf1);
  sm_buf1=NULL;
 }
 if (sm_buf2!=NULL)
 {
  free(sm_buf2);
  sm_buf2=NULL;
 }

 H5E_BEGIN_TRY {
  H5Sclose(sid1);
  H5Sclose(sid2);
  H5Tclose(f_tid1);
  H5Tclose(f_tid2);
  H5Tclose(m_tid1);
  H5Tclose(m_tid2);
 } H5E_END_TRY;

 return nfound;

error:
 options->err_stat=1;
 
 /* free */
 if (buf1!=NULL)
 {
  free(buf1);
  buf1=NULL;
 }
 if (buf2!=NULL)
 {
  free(buf2);
  buf2=NULL;
 }
 if (sm_buf1!=NULL)
 {
  free(sm_buf1);
  sm_buf1=NULL;
 }
 if (sm_buf2!=NULL)
 {
  free(sm_buf2);
  sm_buf2=NULL;
 }

 /* disable error reporting */
 H5E_BEGIN_TRY {
  H5Sclose(sid1);
  H5Sclose(sid2);
  H5Tclose(f_tid1);
  H5Tclose(f_tid2);
  H5Tclose(m_tid1);
  H5Tclose(m_tid2);
   /* enable error reporting */
 } H5E_END_TRY;

 return nfound;
}
예제 #14
0
/*-------------------------------------------------------------------------
* Function: diff_datasetid
*
* Purpose: check for comparable datasets and read into a compatible
*  memory type
*
* Return: Number of differences found
*
* Programmer: Pedro Vicente, [email protected]
*
* Date: May 9, 2003
*
* Modifications:
*
*
* October 2006:  Read by hyperslabs for big datasets.
*
*  A threshold of H5TOOLS_MALLOCSIZE (128 MB) is the limit upon which I/O hyperslab is done
*  i.e., if the memory needed to read a dataset is greater than this limit,
*  then hyperslab I/O is done instead of one operation I/O
*  For each dataset, the memory needed is calculated according to
*
*  memory needed = number of elements * size of each element
*
*  if the memory needed is lower than H5TOOLS_MALLOCSIZE, then the following operations
*  are done
*
*  H5Dread( input_dataset1 )
*  H5Dread( input_dataset2 )
*
*  with all elements in the datasets selected. If the memory needed is greater than
*  H5TOOLS_MALLOCSIZE, then the following operations are done instead:
*
*  a strip mine is defined for each dimension k (a strip mine is defined as a
*  hyperslab whose size is memory manageable) according to the formula
*
*  (1) strip_mine_size[k ] = MIN(dimension[k ], H5TOOLS_BUFSIZE / size of memory type)
*
*  where H5TOOLS_BUFSIZE is a constant currently defined as 1MB. This formula assures
*  that for small datasets (small relative to the H5TOOLS_BUFSIZE constant), the strip
*  mine size k is simply defined as its dimension k, but for larger datasets the
*  hyperslab size is still memory manageable.
*  a cycle is done until the number of elements in the dataset is reached. In each
*  iteration, two parameters are defined for the function H5Sselect_hyperslab,
*  the start and size of each hyperslab, according to
*
*  (2) hyperslab_size [k] = MIN(dimension[k] - hyperslab_offset[k], strip_mine_size [k])
*
*  where hyperslab_offset [k] is initially set to zero, and later incremented in
*  hyperslab_size[k] offsets. The reason for the operation
*
*  dimension[k] - hyperslab_offset[k]
*
*  in (2) is that, when using the strip mine size, it assures that the "remaining" part
*  of the dataset that does not fill an entire strip mine is processed.
*
*-------------------------------------------------------------------------
*/
hsize_t diff_datasetid( hid_t did1,
                        hid_t did2,
                        const char *obj1_name,
                        const char *obj2_name,
                        diff_opt_t *options)
{
    hid_t      sid1=-1;
    hid_t      sid2=-1;
    hid_t      f_tid1=-1;
    hid_t      f_tid2=-1;
    hid_t      m_tid1=-1;
    hid_t      m_tid2=-1;
    size_t     m_size1;
    size_t     m_size2;
    H5T_sign_t sign1;
    H5T_sign_t sign2;
    int        rank1;
    int        rank2;
    hsize_t    nelmts1;
    hsize_t    nelmts2;
    hsize_t    dims1[H5S_MAX_RANK];
    hsize_t    dims2[H5S_MAX_RANK];
    hsize_t    maxdim1[H5S_MAX_RANK];
    hsize_t    maxdim2[H5S_MAX_RANK];
    const char *name1=NULL;            /* relative names */
    const char *name2=NULL;
    hsize_t    storage_size1;
    hsize_t    storage_size2;
    hsize_t    nfound=0;               /* number of differences found */
    int        can_compare=1;          /* do diff or not */
    void       *buf1=NULL;
    void       *buf2=NULL;
    void       *sm_buf1=NULL;
    void       *sm_buf2=NULL;
    hid_t      sm_space;               /*stripmine data space */
    size_t     need;                   /* bytes needed for malloc */
    int        i;
    unsigned int  vl_data = 0;         /*contains VL datatypes */

    /* Get the dataspace handle */
    if ( (sid1 = H5Dget_space(did1)) < 0 )
        goto error;

    /* Get rank */
    if ( (rank1 = H5Sget_simple_extent_ndims(sid1)) < 0 )
        goto error;

    /* Get the dataspace handle */
    if ( (sid2 = H5Dget_space(did2)) < 0 )
        goto error;

    /* Get rank */
    if ( (rank2 = H5Sget_simple_extent_ndims(sid2)) < 0 )
        goto error;

    /* Get dimensions */
    if ( H5Sget_simple_extent_dims(sid1,dims1,maxdim1) < 0 )
        goto error;

    /* Get dimensions */
    if ( H5Sget_simple_extent_dims(sid2,dims2,maxdim2) < 0 )
    {
        goto error;
    }

    /*-------------------------------------------------------------------------
    * get the file data type
    *-------------------------------------------------------------------------
    */

    /* Get the data type */
    if ( (f_tid1 = H5Dget_type(did1)) < 0 )
        goto error;

    /* Get the data type */
    if ( (f_tid2 = H5Dget_type(did2)) < 0 )
    {
        goto error;
    }

    /*-------------------------------------------------------------------------
    * check for empty datasets
    *-------------------------------------------------------------------------
    */

    storage_size1=H5Dget_storage_size(did1);
    storage_size2=H5Dget_storage_size(did2);

    if (storage_size1==0 || storage_size2==0)
    {
        if ( (options->m_verbose||options->m_list_not_cmp) && obj1_name && obj2_name)
            parallel_print("Not comparable: <%s> or <%s> is an empty dataset\n", obj1_name, obj2_name);
        can_compare=0;
        options->not_cmp=1;
    }

    /*-------------------------------------------------------------------------
    * check for comparable TYPE and SPACE
    *-------------------------------------------------------------------------
    */

    if (diff_can_type(f_tid1,
        f_tid2,
        rank1,
        rank2,
        dims1,
        dims2,
        maxdim1,
        maxdim2,
        obj1_name,
        obj2_name,
        options,
        0)!=1)
    {
        can_compare=0;
    }

    /*-------------------------------------------------------------------------
    * memory type and sizes
    *-------------------------------------------------------------------------
    */
    if ((m_tid1=h5tools_get_native_type(f_tid1)) < 0)
        goto error;

    if ((m_tid2=h5tools_get_native_type(f_tid2)) < 0)
        goto error;

    m_size1 = H5Tget_size( m_tid1 );
    m_size2 = H5Tget_size( m_tid2 );

    /*-------------------------------------------------------------------------
    * check for different signed/unsigned types
    *-------------------------------------------------------------------------
    */
    if (can_compare)
    {
        sign1=H5Tget_sign(m_tid1);
        sign2=H5Tget_sign(m_tid2);
        if ( sign1 != sign2 )
        {
            if ((options->m_verbose||options->m_list_not_cmp) && obj1_name && obj2_name)
            {
                parallel_print("Not comparable: <%s> has sign %s ", obj1_name, get_sign(sign1));
                parallel_print("and <%s> has sign %s\n", obj2_name, get_sign(sign2));
            }
    
            can_compare=0;
            options->not_cmp=1;
        }
    }

    /* Check if type is either VLEN-data or VLEN-string to reclaim any
     * VLEN memory buffer later */
    if( TRUE == h5tools_detect_vlen(m_tid1) )
        vl_data = TRUE;

    /*------------------------------------------------------------------------
    * only attempt to compare if possible
    *-------------------------------------------------------------------------
    */
    if(can_compare) /* it is possible to compare */
    {

        /*-----------------------------------------------------------------
        * get number of elements
        *------------------------------------------------------------------
        */
        nelmts1 = 1;
        for(i = 0; i < rank1; i++)
            nelmts1 *= dims1[i];

        nelmts2 = 1;
        for(i = 0; i < rank2; i++)
            nelmts2 *= dims2[i];

        HDassert(nelmts1 == nelmts2);

        /*-----------------------------------------------------------------
        * "upgrade" the smaller memory size
        *------------------------------------------------------------------
        */

        if (FAIL == match_up_memsize (f_tid1, f_tid2,
                                      &m_tid1, &m_tid2, 
                                      &m_size1, &m_size2))
            goto error;

        /* print names */
        if(obj1_name)
            name1 = diff_basename(obj1_name);
        if(obj2_name)
            name2 = diff_basename(obj2_name);


        /*----------------------------------------------------------------
        * read/compare
        *-----------------------------------------------------------------
        */
        need = (size_t)(nelmts1 * m_size1);  /* bytes needed */
        if(need < H5TOOLS_MALLOCSIZE) {
            buf1 = HDmalloc(need);
            buf2 = HDmalloc(need);
        } /* end if */

        if(buf1 != NULL && buf2 != NULL) {
            if(H5Dread(did1, m_tid1, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf1) < 0)
                goto error;
            if(H5Dread(did2, m_tid2, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf2) < 0)
                goto error;

            /* array diff */
            nfound = diff_array(buf1, buf2, nelmts1, (hsize_t)0, rank1, dims1,
                options, name1, name2, m_tid1, did1, did2);

            /* reclaim any VL memory, if necessary */
            if(vl_data) {
                H5Dvlen_reclaim(m_tid1, sid1, H5P_DEFAULT, buf1);
                H5Dvlen_reclaim(m_tid2, sid2, H5P_DEFAULT, buf2);
            } /* end if */
        } /* end if */
        else /* possibly not enough memory, read/compare by hyperslabs */
        {
            size_t        p_type_nbytes = m_size1; /*size of memory type */
            hsize_t       p_nelmts = nelmts1;      /*total selected elmts */
            hsize_t       elmtno;                  /*counter  */
            int           carry;                   /*counter carry value */

            /* stripmine info */
            hsize_t       sm_size[H5S_MAX_RANK];   /*stripmine size */
            hsize_t       sm_nbytes;               /*bytes per stripmine */
            hsize_t       sm_nelmts;               /*elements per stripmine*/

            /* hyperslab info */
            hsize_t       hs_offset[H5S_MAX_RANK]; /*starting offset */
            hsize_t       hs_size[H5S_MAX_RANK];   /*size this pass */
            hsize_t       hs_nelmts;               /*elements in request */
            hsize_t       zero[8];                 /*vector of zeros */

            /*
             * determine the strip mine size and allocate a buffer. The strip mine is
             * a hyperslab whose size is manageable.
             */
            sm_nbytes = p_type_nbytes;

            for(i = rank1; i > 0; --i) {
                hsize_t size = H5TOOLS_BUFSIZE / sm_nbytes;

                if(size == 0) /* datum size > H5TOOLS_BUFSIZE */
                    size = 1;
                sm_size[i - 1] = MIN(dims1[i - 1], size);
                sm_nbytes *= sm_size[i - 1];
                HDassert(sm_nbytes > 0);
            } /* end for */

            /* malloc return code should be verified.
             * If fail, need to handle the error.
             * This else branch should be recoded as a separate function.
             * Note that there are many "goto error" within this branch
             * that fails to address freeing other objects created here.
             * E.g., sm_space.
             */
            sm_buf1 = HDmalloc((size_t)sm_nbytes);
            HDassert(sm_buf1);
            sm_buf2 = HDmalloc((size_t)sm_nbytes);
            HDassert(sm_buf2);

            sm_nelmts = sm_nbytes / p_type_nbytes;
            sm_space = H5Screate_simple(1, &sm_nelmts, NULL);

            /* the stripmine loop */
            HDmemset(hs_offset, 0, sizeof hs_offset);
            HDmemset(zero, 0, sizeof zero);

            for(elmtno = 0; elmtno < p_nelmts; elmtno += hs_nelmts) {
                /* calculate the hyperslab size */
                if(rank1 > 0) {
                    for(i = 0, hs_nelmts = 1; i < rank1; i++) {
                        hs_size[i] = MIN(dims1[i] - hs_offset[i], sm_size[i]);
                        hs_nelmts *= hs_size[i];
                    } /* end for */
                    if(H5Sselect_hyperslab(sid1, H5S_SELECT_SET, hs_offset, NULL, hs_size, NULL) < 0)
                        goto error;
                    if(H5Sselect_hyperslab(sid2, H5S_SELECT_SET, hs_offset, NULL, hs_size, NULL) < 0)
                        goto error;
                    if(H5Sselect_hyperslab(sm_space, H5S_SELECT_SET, zero, NULL, &hs_nelmts, NULL) < 0)
                        goto error;
                } /* end if */
                else
                    hs_nelmts = 1;

                if(H5Dread(did1,m_tid1,sm_space,sid1,H5P_DEFAULT,sm_buf1) < 0)
                    goto error;
                if(H5Dread(did2,m_tid2,sm_space,sid2,H5P_DEFAULT,sm_buf2) < 0)
                    goto error;

                /* get array differences. in the case of hyperslab read, increment the number of differences
                found in each hyperslab and pass the position at the beggining for printing */
                nfound += diff_array(sm_buf1, sm_buf2, hs_nelmts, elmtno, rank1,
                    dims1, options, name1, name2, m_tid1, did1, did2);

                /* reclaim any VL memory, if necessary */
                if(vl_data) {
                    H5Dvlen_reclaim(m_tid1, sm_space, H5P_DEFAULT, sm_buf1);
                    H5Dvlen_reclaim(m_tid1, sm_space, H5P_DEFAULT, sm_buf2);
                } /* end if */

                /* calculate the next hyperslab offset */
                for(i = rank1, carry = 1; i > 0 && carry; --i) {
                    hs_offset[i - 1] += hs_size[i - 1];
                    if(hs_offset[i - 1] == dims1[i - 1])
                        hs_offset[i - 1] = 0;
                    else
                        carry = 0;
                } /* i */
            } /* elmtno */

            H5Sclose(sm_space);
        } /* hyperslab read */
    } /*can_compare*/


    /*-------------------------------------------------------------------------
     * close
     *-------------------------------------------------------------------------
     */

    /* free */
    if(buf1 != NULL) {
        HDfree(buf1);
        buf1 = NULL;
    } /* end if */
    if(buf2 != NULL) {
        HDfree(buf2);
        buf2 = NULL;
    } /* end if */
    if(sm_buf1 != NULL) {
        HDfree(sm_buf1);
        sm_buf1 = NULL;
    } /* end if */
    if(sm_buf2 != NULL) {
        HDfree(sm_buf2);
        sm_buf2 = NULL;
    } /* end if */

    H5E_BEGIN_TRY {
        H5Sclose(sid1);
        H5Sclose(sid2);
        H5Tclose(f_tid1);
        H5Tclose(f_tid2);
        H5Tclose(m_tid1);
        H5Tclose(m_tid2);
    } H5E_END_TRY;

    return nfound;

error:
    options->err_stat=1;

    /* free */
    if (buf1!=NULL)
    {
        /* reclaim any VL memory, if necessary */
        if(vl_data)
            H5Dvlen_reclaim(m_tid1, sid1, H5P_DEFAULT, buf1);
        HDfree(buf1);
        buf1=NULL;
    }
    if (buf2!=NULL)
    {
        /* reclaim any VL memory, if necessary */
        if(vl_data)
            H5Dvlen_reclaim(m_tid2, sid2, H5P_DEFAULT, buf2);
        HDfree(buf2);
        buf2=NULL;
    }
    if (sm_buf1!=NULL)
    {
        /* reclaim any VL memory, if necessary */
        if(vl_data)
            H5Dvlen_reclaim(m_tid1, sm_space, H5P_DEFAULT, sm_buf1);
        HDfree(sm_buf1);
        sm_buf1=NULL;
    }
    if (sm_buf2!=NULL)
    {
        /* reclaim any VL memory, if necessary */
        if(vl_data)
            H5Dvlen_reclaim(m_tid1, sm_space, H5P_DEFAULT, sm_buf2);
        HDfree(sm_buf2);
        sm_buf2=NULL;
    }

    /* disable error reporting */
    H5E_BEGIN_TRY {
        H5Sclose(sid1);
        H5Sclose(sid2);
        H5Tclose(f_tid1);
        H5Tclose(f_tid2);
        H5Tclose(m_tid1);
        H5Tclose(m_tid2);
        /* enable error reporting */
    } H5E_END_TRY;

    return nfound;
}
예제 #15
0
static herr_t H5Z_sz_set_local(hid_t dcpl_id, hid_t type_id, hid_t chunk_space_id)
{
	//printf("start in H5Z_sz_set_local\n");
	size_t r5=0,r4=0,r3=0,r2=0,r1=0, dsize;
	static char const *_funcname_ = "H5Z_sz_set_local";
	int i, ndims, ndims_used = 0;	
	hsize_t dims[H5S_MAX_RANK], dims_used[5] = {0,0,0,0,0};	
	herr_t retval = 0;
	H5T_class_t dclass;
	H5T_sign_t dsign;
	unsigned int flags = 0;
	//conf_params = H5Z_SZ_Init_Default();
	H5Z_SZ_Init(cfgFile);
	
	int dataType = SZ_FLOAT;
	
	if (0 > (dclass = H5Tget_class(type_id)))
		H5Z_SZ_PUSH_AND_GOTO(H5E_ARGS, H5E_BADTYPE, -1, "not a datatype");

	if (0 == (dsize = H5Tget_size(type_id)))
		H5Z_SZ_PUSH_AND_GOTO(H5E_ARGS, H5E_BADTYPE, -1, "size is smaller than 0!");

	if (0 > (ndims = H5Sget_simple_extent_dims(chunk_space_id, dims, 0)))
		H5Z_SZ_PUSH_AND_GOTO(H5E_ARGS, H5E_BADTYPE, -1, "not a data space");
		
	for (i = 0; i < ndims; i++)
	{
		if (dims[i] <= 1) continue;
		dims_used[ndims_used] = dims[i];
		ndims_used++;
	}
	
	//printf("dclass=%d, H5T_FLOAT=%d, H5T_INTEGER=%d\n", dclass, H5T_FLOAT, H5T_INTEGER);
	if (dclass == H5T_FLOAT)
		dataType = dsize==4? SZ_FLOAT: SZ_DOUBLE;
	else if(dclass == H5T_INTEGER)
	{
		if (0 > (dsign = H5Tget_sign(type_id)))
			H5Z_SZ_PUSH_AND_GOTO(H5E_ARGS, H5E_BADTYPE, -1, "Error in calling H5Tget_sign(type_id)....");		
		if(dsign == H5T_SGN_NONE) //unsigned
		{
			switch(dsize)
			{
			case 1:
				dataType = SZ_UINT8;
				break;
			case 2:
				dataType = SZ_UINT16;
				break;
			case 4:
				dataType = SZ_UINT32;
				break;
			case 8:
				dataType = SZ_UINT64;
				break;
			}
		}
		else
		{
			switch(dsize)
			{
			case 1:
				dataType = SZ_INT8;
				break;
			case 2:
				dataType = SZ_INT16;
				break;
			case 4:
				dataType = SZ_INT32;
				break;
			case 8:
				dataType = SZ_INT64;
				break;
			}			
		}
	}
	else
	{
		//printf("Error: dclass...\n");
		H5Z_SZ_PUSH_AND_GOTO(H5E_PLINE, H5E_BADTYPE, 0, "datatype class must be H5T_FLOAT or H5T_INTEGER");
	}
	
	
	switch(ndims_used)
	{
	case 1: 
		r1 = dims_used[0];
		break;
	case 2:
		r1 = dims_used[0];
		r2 = dims_used[1];
		break;
	case 3:
		r1 = dims_used[0];
		r2 = dims_used[1];
		r3 = dims_used[2];		
		break;
	case 4:
		r1 = dims_used[0];
		r2 = dims_used[1];
		r3 = dims_used[2];	
		r4 = dims_used[3];
		break;
	default: 
		H5Z_SZ_PUSH_AND_GOTO(H5E_PLINE, H5E_BADVALUE, 0, "requires chunks w/1,2,3 or 4 non-unity dims");
	}
	
	size_t cd_nelmts = 0;
	unsigned int mem_cd_values[7]; 
	unsigned int* cd_values;

	if (0 > H5Pget_filter_by_id(dcpl_id, H5Z_FILTER_SZ, &flags, &cd_nelmts, mem_cd_values, 0, NULL, NULL))
		H5Z_SZ_PUSH_AND_GOTO(H5E_PLINE, H5E_CANTGET, 0, "unable to get current SZ cd_values");

	SZ_metaDataToCdArray(&cd_nelmts, &cd_values, dataType, r5, r4, r3, r2, r1);
	
	/* Now, update cd_values for the filter */
	if (0 > H5Pmodify_filter(dcpl_id, H5Z_FILTER_SZ, flags, cd_nelmts, cd_values))
		H5Z_SZ_PUSH_AND_GOTO(H5E_PLINE, H5E_BADVALUE, 0, "failed to modify cd_values");	
		
	retval = 1;
done:
	return retval;
}
예제 #16
0
asynStatus hdf5Driver::parseType (hid_t id, NDDataType_t *pNDType)
{
    const char *functionName = "parseType";
    asynStatus status = asynSuccess;

    hid_t type = H5Dget_type(id);
    H5T_class_t dsetTypeClass = H5Tget_class(type);
    H5T_sign_t dsetTypeSign = H5Tget_sign(type);
    size_t dsetTypeSize = H5Tget_size(type);

    *pNDType = NDInt8;   // Default to signed char

    if(dsetTypeClass == H5T_INTEGER)
    {
        if(dsetTypeSign == H5T_SGN_NONE)
        {
            switch(dsetTypeSize)
            {
                case 1: *pNDType = NDUInt8;  break;
                case 2: *pNDType = NDUInt16; break;
                case 4: *pNDType = NDUInt32; break;
                default:
                    asynPrint(pasynUserSelf, ASYN_TRACE_ERROR,
                            "%s:%s invalid dataset type: "
                            "unsigned integer with %lu bytes\n",
                            driverName, functionName, dsetTypeSize);
                    status = asynError;
                    break;
            }
        }
        else if(dsetTypeSign == H5T_SGN_2)
        {
            switch(dsetTypeSize)
            {
                case 1: *pNDType = NDInt8;  break;
                case 2: *pNDType = NDInt16; break;
                case 4: *pNDType = NDInt32; break;
                default:
                    asynPrint(pasynUserSelf, ASYN_TRACE_ERROR,
                            "%s:%s invalid dataset type: "
                            "signed integer with %lu bytes\n",
                            driverName, functionName, dsetTypeSize);
                    status = asynError;
                    break;
            }
        }
        else
        {
            asynPrint(pasynUserSelf, ASYN_TRACE_ERROR,
                    "%s:%s invalid dataset type sign\n",
                    driverName, functionName);
            status = asynError;
        }
    }
    else if(dsetTypeClass == H5T_FLOAT)
    {
        switch(dsetTypeSize)
        {
            case 4: *pNDType = NDFloat32; break;
            case 8: *pNDType = NDFloat64; break;
            default:
                asynPrint(pasynUserSelf, ASYN_TRACE_ERROR,
                        "%s:%s invalid dataset type: "
                        "float with %lu bytes\n",
                        driverName, functionName, dsetTypeSize);
                status = asynError;
                break;
        }
    }
    else
    {
        asynPrint(pasynUserSelf, ASYN_TRACE_ERROR,
                "%s:%s dataset type class not accepted",
                driverName, functionName);
        status = asynError;
    }

    H5Tclose(type);
    return status;
}
예제 #17
0
int H5Attribute_init(H5Attribute *a, hid_t aid) {
    int ret_value=0, i=0;
    hsize_t dims[H5S_MAX_RANK];
    hid_t sid=-1, tid=-1, ftid=-1;
    unsigned int npoints;

    if (NULL == a || aid < 0) {
        ret_value = -1;
        goto done;
    }

    if (a->space.rank > 0)
        goto done; /* already called */

    ftid = H5Aget_type(aid);
    tid = H5Tget_native_type(ftid, H5T_DIR_ASCEND);
    if (ftid > 0) H5Tclose(ftid);

    a->name = (char *)malloc(MAX_NAME_LEN);
    H5Aget_name(aid, MAX_NAME_LEN, a->name);

    a->tclass = a->type.tclass = (H5Datatype_class_t)H5Tget_class(tid);
    a->type.size = H5Tget_size(tid);

    if  ( a->type.tclass == H5DATATYPE_INTEGER
         || a->type.tclass == H5DATATYPE_FLOAT
         || a->type.tclass == H5DATATYPE_BITFIELD
         || a->type.tclass == H5DATATYPE_REFERENCE
         || a->type.tclass == H5DATATYPE_ENUM ) {
        a->type.order = (H5Datatype_order_t)H5Tget_order(tid);
        if (a->type.tclass == H5DATATYPE_INTEGER) {
            a->type.sign = (H5Datatype_sign_t)H5Tget_sign(tid);
        }
    }
    else if (a->type.tclass == H5DATATYPE_COMPOUND) {
        a->type.nmembers = H5Tget_nmembers(tid );
        a->type.mnames = (char **)malloc(a->type.nmembers*sizeof(char*));
        a->type.mtypes = (int *)malloc(a->type.nmembers*sizeof(int));
        for (i=0; i<a->type.nmembers; i++) {
            a->type.mnames[i] = H5Tget_member_name(tid, i);
            a->type.mtypes[i] = H5Tget_class(H5Tget_member_type(tid, i));
        }
    }

    sid = H5Aget_space(aid);
    a->space.rank = H5Sget_simple_extent_ndims(sid);
    if ( H5Sget_simple_extent_dims(sid, dims, NULL) < 0 )
        THROW_H5LIBRARY_ERROR(a->error,ret_value, done);

    if (a->space.rank<=0)
    {
        a->space.rank = 1;
        dims[0] = 1;
    }

    npoints = 1;
    for (i=0; i<a->space.rank; i++)
    {
        a->space.dims[i] = dims[i];
        a->space.start[i] = 0;
        a->space.stride[i] = 1;
        a->space.count[i] = dims[i];
        npoints *= dims[i];
    }
    a->space.npoints = npoints;

done:
    if (sid > 0 ) H5Sclose(sid);
    if (tid > 0 ) H5Tclose(tid);

    return ret_value;
}
예제 #18
0
파일: Object.cpp 프로젝트: mstraubAC/hdf5pp
	Object::TypeName Object::getStlType(hid_t hType) const
	{
		H5T_class_t attrType = H5Tget_class(hType);

		// parsing type info
		stringstream typeName;
		switch(attrType) {
			case H5T_INTEGER:
			{
				size_t precision = H5Tget_precision(hType);
				H5T_sign_t sign = H5Tget_sign(hType);
				switch (sign) {
					case H5T_SGN_NONE:
						typeName << "u";
						break;
					case H5T_SGN_2:
						break;
					case H5T_NSGN:
						break;
					case H5T_SGN_ERROR:
						break;
				}
				typeName << "int" << dec << precision << "_t";
			}
				break;
			case H5T_FLOAT:
			{
				size_t precision = H5Tget_precision(hType);
				if (precision == 32) {
					typeName << "float";
				}
				else if (precision == 64) {
					typeName << "double";
				}
				else {
					stringstream ss;
					ss << "Object::getStlType: H5T_FLOAT with unkown precision " << precision << "bits";
					throw Exception(ss);
				}
			}
				break;
			case H5T_STRING:
			{
				H5T_cset_t charSet = H5Tget_cset(hType);
				if (charSet != 0) {
					stringstream ss;
					ss << "Object::getStlType: Unknown character encoding type '" << charSet << "'. Currently only US-ASCII is supported.";
					throw Exception(ss);
				}
				typeName << "string";
			}
				break;
			case H5T_BITFIELD:
				throw Exception("Object::getStlType: Unknown type H5T_BITFIELD");
				break;
			case H5T_OPAQUE:
				throw Exception("Object::getStlType: Unknown type H5T_OPAQUE");
				break;
			case H5T_COMPOUND:
				throw Exception("Object::getStlType: Unknown type H5T_COMPOUND");
				break;
			case H5T_REFERENCE:
				throw Exception("Object::getStlType: Unknown type H5T_REFERENCE");
				break;
			case H5T_ENUM:
				throw Exception("Object::getStlType: Unknown type H5T_ENUM");
				break;
			case H5T_VLEN:
				throw Exception("Object::getStlType: Unknown type H5T_VLEN");
				break;
			case H5T_ARRAY:
				throw Exception("Object::getStlType: Unknown type H5T_ARRAY");
				break;
			case H5T_NO_CLASS:
				throw Exception("Object::getStlType: Unknown type H5T_NO_CLASS");
				break;
			case H5T_TIME:
				throw Exception("Object::getStlType: Unknown type H5T_TIME");
				break;
			case H5T_NCLASSES:
				throw Exception("Object::getStlType: Unknown type H5T_NCLASSES");
				break;
		}

		return typeName.str();
	}