Example #1
0
    def create(self, name, data, shape=None, dtype=None):
        """ Create a new attribute, overwriting any existing attribute.

        name
            Name of the new attribute (required)
        data
            An array to initialize the attribute (required)
        shape
            Shape of the attribute.  Overrides data.shape if both are
            given, in which case the total number of points must be unchanged.
        dtype
            Data type of the attribute.  Overrides data.dtype if both
            are given.
        """

        with phil:
            if data is not None:
                data = numpy.asarray(data, order="C", dtype=dtype)
                if shape is None:
                    shape = data.shape
                elif numpy.product(shape) != numpy.product(data.shape):
                    raise ValueError("Shape of new attribute conflicts with shape of data")

                if dtype is None:
                    dtype = data.dtype

            if isinstance(dtype, h5py.Datatype):
                htype = dtype.id
                dtype = htype.dtype
            else:
                if dtype is None:
                    dtype = numpy.dtype("f")
                htype = h5t.py_create(dtype, logical=True)

            if shape is None:
                raise ValueError('At least one of "shape" or "data" must be given')

            data = data.reshape(shape)

            space = h5s.create_simple(shape)

            if name in self:
                h5a.delete(self._id, self._e(name))

            attr = h5a.create(self._id, self._e(name), htype, space)

            if data is not None:
                try:
                    attr.write(data)
                except:
                    attr._close()
                    h5a.delete(self._id, self._e(name))
                    raise
Example #2
0
    def create(self, name, data, shape=None, dtype=None):
        """ Create a new attribute, overwriting any existing attribute.

        name
            Name of the new attribute (required)
        data
            An array to initialize the attribute (required)
        shape
            Shape of the attribute.  Overrides data.shape if both are
            given, in which case the total number of points must be unchanged.
        dtype
            Data type of the attribute.  Overrides data.dtype if both
            are given.
        """

        if data is not None:
            data = numpy.asarray(data, order='C', dtype=dtype)
            if shape is None:
                shape = data.shape
            elif numpy.product(shape) != numpy.product(data.shape):
                raise ValueError(
                    "Shape of new attribute conflicts with shape of data")

            if dtype is None:
                dtype = data.dtype

        if isinstance(dtype, h5py.Datatype):
            htype = dtype.id
            dtype = htype.dtype
        else:
            if dtype is None:
                dtype = numpy.dtype('f')
            htype = h5t.py_create(dtype, logical=True)

        if shape is None:
            raise ValueError('At least one of "shape" or "data" must be given')

        data = data.reshape(shape)

        space = h5s.create_simple(shape)

        if name in self:
            h5a.delete(self._id, self._e(name))

        attr = h5a.create(self._id, self._e(name), htype, space)

        if data is not None:
            try:
                attr.write(data)
            except:
                attr._close()
                h5a.delete(self._id, self._e(name))
                raise
Example #3
0
    def create(self, name, data, shape=None, dtype=None):
        """ Create a new attribute, overwriting any existing attribute.

        name
            Name of the new attribute (required)
        data
            An array to initialize the attribute (required)
        shape
            Shape of the attribute.  Overrides data.shape if both are
            given, in which case the total number of points must be unchanged.
        dtype
            Data type of the attribute.  Overrides data.dtype if both
            are given.
        """
        # TODO: REMOVE WHEN UNICODE VLENS IMPLEMENTED
        # Hack to support Unicode values (scalars only)
        #if isinstance(data, unicode):
        #    unicode_hack = True
        #    data = data.encode('utf8')
        #else:
        #    unicode_hack = False

        if data is not None:
            data = numpy.asarray(data, order='C', dtype=dtype)
            if shape is None:
                shape = data.shape
            elif numpy.product(shape) != numpy.product(data.shape):
                raise ValueError("Shape of new attribute conflicts with shape of data")
                
            if dtype is None:
                dtype = data.dtype

        if dtype is None:
            dtype = numpy.dtype('f')
        if shape is None:
            raise ValueError('At least one of "shape" or "data" must be given')

        data = data.reshape(shape)

        space = h5s.create_simple(shape)
        htype = h5t.py_create(dtype, logical=True)

        # TODO: REMOVE WHEN UNICODE VLENS IMPLEMENTED
        #if unicode_hack:
        #    htype.set_cset(h5t.CSET_UTF8)

        if name in self:
            h5a.delete(self._id, self._e(name))

        attr = h5a.create(self._id, self._e(name), htype, space)
        if data is not None:
            attr.write(data)
Example #4
0
 def __delitem__(self, name):
     """ Delete an attribute (which must already exist). """
     h5a.delete(self._id, self._e(name))
Example #5
0
 def __delitem__(self, name):
     """ Delete an attribute (which must already exist). """
     h5a.delete(self._id, self._e(name))
Example #6
0
def create_attribute(_id, _name, _dims, _value):
  """
  Writes a HDF5 string attribute, ASCII, NULLTERM
 
  _id should be something like dset.id
 
  _dims should be a list.  For a scalar, use an empty list []
 
  """
 
# Make sure we don't have a unicode name
  _name=str_to_h5(_name)

# This routine for string attributes
  _dtype = h5t.FORTRAN_S1
# Create a scalar space (if dims len=0); otherwise a simple space
  if len(_dims) == 0:
    _sid=h5s.create(h5s.SCALAR)
  elif len(_dims) == 1 and _dims[0] == 0 :
    _sid=h5s.create(h5s.SCALAR)
  else:
    _sid=h5s.create_simple(tuple(_dims))
# endif
 
# Create the memory & file datatypes. Adjust if datatype is string.
  _mdtype = _dtype.copy()
  _fdtype = _dtype.copy()
  _classtype = _dtype.get_class()
  if _classtype == h5t.STRING:
    if isinstance(_value, list):
      _strlen=0
      for _part in _value: _strlen=max(_strlen, len(_part))
    else:
      _strlen = len(_value)
#   endif
    if _strlen < 1: return None
    _mdtype.set_size(_strlen)
    _mdtype.set_strpad(h5t.STR_SPACEPAD)
    _fdtype.set_size(_strlen+1)
    _fdtype.set_strpad(h5t.STR_NULLTERM)
# endif
 
## Either add or replace the attribute
#  if h5a.exists(_id, _name):
#    _aid = h5a.open(_id, name=_name)
#  else:
#    _aid=h5a.create(_id, _name, _fdtype, _sid)
# endif
# Either add or replace the attribute
  if h5a.exists(_id, _name):
    _aid = h5a.delete(_id, name=_name)
# endif
  _aid=h5a.create(_id, _name, _fdtype, _sid)

  if _classtype == h5t.STRING:
    if isinstance(_value, list):
      _value = np.array(_value, dtype=np.string_)
    else:
      _value = np.array(str_to_h5(_value))
#   endif
  else:
    _pytype = _fdtype.dtype
    _value = np.array(_value, dtype=_pytype)
# endif
  _aid.write(_value)
  return _aid