Ejemplo n.º 1
0
        public void H5ScopyTest1()
        {
            hsize_t[] dims  = { 1, 2, 3 };
            hid_t     space = H5S.create_simple(dims.Length, dims, dims);

            Assert.IsTrue(space > 0);
            hid_t clone = H5S.copy(space);

            Assert.IsTrue(clone > 0);
            Assert.IsTrue(H5S.close(clone) >= 0);
            Assert.IsTrue(H5S.close(space) >= 0);
        }
Ejemplo n.º 2
0
        public static T[] Read <T>(long dataPortId, DataContainerType dataContainerType, long dataspaceId = -1)
        {
            long dataspaceId_file   = -1;
            long dataspaceId_memory = -1;
            long typeId             = -1;

            long elementCount;

            int elementTypeSize;
            int byteLength;

            IntPtr bufferPtr;
            Type   elementType;

            T[] returnValue;

            elementTypeSize = 0;
            byteLength      = 0;
            bufferPtr       = IntPtr.Zero;
            elementType     = typeof(T);
            returnValue     = null;

            try
            {
                if (dataspaceId > -1)
                {
                    dataspaceId = H5S.copy(dataspaceId);
                }

                switch (dataContainerType)
                {
                case DataContainerType.Attribute:

                    if (dataspaceId == -1)
                    {
                        dataspaceId = H5A.get_space(dataPortId);
                    }

                    break;

                case DataContainerType.Dataset:

                    if (dataspaceId == -1)
                    {
                        dataspaceId = H5D.get_space(dataPortId);
                    }

                    dataspaceId_file = dataspaceId;

                    break;

                default:
                    throw new NotSupportedException();
                }

                if (elementType == typeof(string))
                {
                    elementTypeSize = Marshal.SizeOf <IntPtr>();
                }
                else if (elementType == typeof(bool))
                {
                    elementTypeSize = Marshal.SizeOf <byte>();
                }
                else
                {
                    elementTypeSize = Marshal.SizeOf(elementType);
                }

                elementCount = H5S.get_select_npoints(dataspaceId);
                byteLength   = (int)elementCount * elementTypeSize;
                bufferPtr    = Marshal.AllocHGlobal(byteLength);
                typeId       = TypeConversionHelper.GetHdfTypeIdFromType(elementType);

                switch (dataContainerType)
                {
                case DataContainerType.Attribute:

                    if (H5A.read(dataPortId, typeId, bufferPtr) < 0)
                    {
                        throw new Exception(ErrorMessage.IOHelper_CouldNotReadAttribute);
                    }

                    break;

                case DataContainerType.Dataset:

                    dataspaceId_memory = H5S.create_simple(1, new ulong[] { (ulong)elementCount }, new ulong[] { (ulong)elementCount });

                    if (H5D.read(dataPortId, typeId, dataspaceId_memory, dataspaceId_file, H5P.DEFAULT, bufferPtr) < 0)
                    {
                        throw new Exception(ErrorMessage.IOHelper_CouldNotReadDataset);
                    }

                    break;

                default:
                    throw new NotSupportedException();
                }

                if (elementType.IsPrimitive)
                {
                    T[]      genericSet;
                    GCHandle gcHandle;
                    byte[]   byteSet;

                    genericSet = new T[(int)elementCount];
                    gcHandle   = GCHandle.Alloc(genericSet, GCHandleType.Pinned);
                    byteSet    = new byte[byteLength];

                    Marshal.Copy(bufferPtr, byteSet, 0, byteLength);
                    Marshal.Copy(byteSet, 0, gcHandle.AddrOfPinnedObject(), byteLength);

                    returnValue = genericSet;

                    gcHandle.Free();
                }
                else if (elementType == typeof(string))
                {
                    IntPtr[] intPtrSet;

                    intPtrSet = new IntPtr[(int)elementCount];

                    Marshal.Copy(bufferPtr, intPtrSet, 0, (int)elementCount);

                    returnValue = intPtrSet.Select(x =>
                    {
                        string result = Marshal.PtrToStringAnsi(x);
                        H5.free_memory(x);
                        return(result);
                    }).Cast <T>().ToArray();
                }
                else if (elementType.IsValueType && !elementType.IsPrimitive && !elementType.IsEnum)
                {
                    T[] structSet;
                    int offset;

                    structSet = new T[(int)elementCount];
                    offset    = 0;

                    Enumerable.Range(0, (int)elementCount).ToList().ForEach(x =>
                    {
                        structSet[x] = Marshal.PtrToStructure <T>(IntPtr.Add(bufferPtr, offset));
                        offset      += elementTypeSize;
                    });

                    returnValue = structSet;
                }
                else
                {
                    throw new NotSupportedException();
                }
            }
            finally
            {
                Marshal.FreeHGlobal(bufferPtr);

                if (H5I.is_valid(typeId) > 0)
                {
                    H5T.close(typeId);
                }
                if (H5I.is_valid(dataspaceId_memory) > 0)
                {
                    H5S.close(dataspaceId_memory);
                }
                if (H5I.is_valid(dataspaceId) > 0)
                {
                    H5S.close(dataspaceId);
                }
            }

            return(returnValue);
        }
Ejemplo n.º 3
0
 public void H5ScopyTest2()
 {
     Assert.IsFalse(H5S.copy(Utilities.RandomInvalidHandle()) >= 0);
 }