Example #1
0
        public static Hdf5Dataset CreateDataset(
            Hdf5Identifier _fileId,
            Hdf5Path _parentPath,
            string _name,
            Hdf5DataTypes _datatype,
            int _numberOfDimensions,
            List <Hdf5DimensionProperty> _properties)
        {
            Hdf5Path path = _parentPath.Append(_name);

            UInt64[] dimensionSize = new UInt64[_numberOfDimensions];
            UInt64[] maxSize       = null; // new UInt64[_numberOfDimensions];

            int i = 0;

            foreach (var property in _properties)
            {
                dimensionSize[i] = property.CurrentSize;

                //if (property.MaximumSize == UInt64.MaxValue)
                //{
                //    maxSize[i] = H5S.UNLIMITED;
                //}
                //else
                //{
                //    maxSize[i] = property.MaximumSize;
                //}

                i++;
            }

            Hdf5Identifier dataspaceId = H5S.create_simple(_numberOfDimensions, dimensionSize, maxSize).ToId();

            //TODO handle string datasets
            Hdf5Identifier typeId = H5T.copy(TypeHelper.GetNativeType(_datatype).Value).ToId();
            var            status = H5T.set_order(typeId.Value, H5T.order_t.LE);

            Hdf5Identifier datasetId = H5D.create(_fileId.Value, path.FullPath, typeId.Value, dataspaceId.Value).ToId();

            Hdf5Dataset dataset = null;

            if (datasetId.Value > 0)
            {
                dataset = new Hdf5Dataset(_fileId, datasetId, path.FullPath)
                {
                    DataType  = TypeHelper.GetDataTypeFromDataset(datasetId),
                    Dataspace = DataspaceHelper.GetDataspace(datasetId)
                };

                H5D.close(datasetId.Value);
            }

            H5T.close(typeId.Value);

            FileHelper.FlushToFile(_fileId);

            return(dataset);
        }
Example #2
0
        /// <summary>
        /// Currently does not support arrays
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="_objectId"></param>
        /// <param name="_title"></param>
        /// <param name="_value"></param>
        /// <returns></returns>
        public static Hdf5Attribute CreateAttribute <T>(Hdf5Identifier _objectId, string _title, T _value)
        {
            ulong[] sizes = new ulong[1] {
                1
            };
            Hdf5Identifier dataspaceId;
            Hdf5Identifier attributeId;
            Hdf5Identifier typeId;
            Hdf5Attribute  attribute = null;

            Type type     = _value.GetType();
            var  datatype = TypeHelper.GetDataTypesEnum(type);

            if (datatype != Hdf5DataTypes.String)
            {
                var tempType = TypeHelper.GetNativeType(datatype);

                typeId = H5T.copy(tempType.Value).ToId();
                var dataTypeObject = TypeHelper.GetDataTypeByType(typeId);

                var status = H5T.set_order(typeId.Value, H5T.order_t.LE);

                dataspaceId = H5S.create_simple(1, sizes, null).ToId();

                attributeId = H5A.create(_objectId.Value, _title, typeId.Value, dataspaceId.Value).ToId();

                if (attributeId.Value > 0)
                {
                    WriteValue(dataTypeObject, attributeId, _value);
                }
            }
            else
            {
                string tempValue = Convert.ToString(_value);

                dataspaceId = H5S.create(H5S.class_t.SCALAR).ToId();
                typeId      = H5T.copy(H5T.C_S1).ToId();
                int length = tempValue.Length + 1;
                var result = H5T.set_size(typeId.Value, new IntPtr(length));

                attributeId = H5A.create(_objectId.Value, _title, typeId.Value, dataspaceId.Value).ToId();

                IntPtr valueArray = Marshal.StringToHGlobalAnsi(tempValue);

                result = H5A.write(attributeId.Value, typeId.Value, valueArray);

                Marshal.FreeHGlobal(valueArray);
            }

            H5S.close(dataspaceId.Value);
            H5T.close(typeId.Value);
            H5A.close(attributeId.Value);

            if (attributeId.Value > 0)
            {
                attribute = new Hdf5Attribute
                {
                    Value = _value,
                    Name  = _title,
                    Id    = attributeId
                };
            }

            return(attribute);
        }
Example #3
0
        public static Hdf5Dataset CreateDataset(
            Hdf5Identifier _fileId,
            Hdf5Path _parentPath,
            string _name,
            Hdf5DataTypes _datatype,
            int _numberOfDimensions,
            List <Hdf5DimensionProperty> _properties,
            Hdf5CompressionProperty _compressionProperty = null)
        {
            Hdf5Path path = _parentPath.Append(_name);

            UInt64[] dimensionSize = new UInt64[_numberOfDimensions];
            UInt64[] maxSize       = null; // new UInt64[_numberOfDimensions];

            if (_numberOfDimensions != _properties.Count ||
                (_compressionProperty != null && _numberOfDimensions != _compressionProperty.ChunkDimensions.Length))
            {
                throw new Hdf5ArrayDimensionsMismatchException();
            }

            int i = 0;

            foreach (var property in _properties)
            {
                dimensionSize[i] = property.CurrentSize;

                if (_compressionProperty != null && _compressionProperty.ChunkDimensions[i] > property.CurrentSize)
                {
                    throw new Hdf5ArraySizeMismatchException();
                }

                i++;
            }

            Hdf5Identifier dataspaceId = H5S.create_simple(_numberOfDimensions, dimensionSize, maxSize).ToId();

            //TODO handle string datasets
            Hdf5Identifier typeId = H5T.copy(TypeHelper.GetNativeType(_datatype).Value).ToId();
            var            status = H5T.set_order(typeId.Value, H5T.order_t.LE);

            var plist_id = _compressionProperty != null?H5P.create(H5P.DATASET_CREATE) : 0;

            if (plist_id != 0)
            {
                H5P.set_chunk(plist_id, _compressionProperty.ChunkDimensions.Length, _compressionProperty.ChunkDimensions);
                H5P.set_deflate(plist_id, _compressionProperty.CompressionLevel);
            }

            Hdf5Identifier datasetId = H5D.create(_fileId.Value, path.FullPath, typeId.Value, dataspaceId.Value, dcpl_id: plist_id).ToId();

            Hdf5Dataset dataset = null;

            if (datasetId.Value > 0)
            {
                dataset = new Hdf5Dataset(_fileId, datasetId, path.FullPath)
                {
                    DataType  = TypeHelper.GetDataTypeFromDataset(datasetId),
                    Dataspace = DataspaceHelper.GetDataspace(datasetId)
                };

                H5D.close(datasetId.Value);
            }

            H5T.close(typeId.Value);
            if (plist_id != 0)
            {
                H5P.close(plist_id);
            }

            FileHelper.FlushToFile(_fileId);

            return(dataset);
        }