public (int success, long CreatedgroupId) WriteArray(long groupId, string name, Array collection, string datasetName, Dictionary <string, List <string> > attributes) { Type type = collection.GetType(); Type elementType = type.GetElementType(); TypeCode typeCode = Type.GetTypeCode(elementType); //Boolean isStruct = type.IsValueType && !type.IsEnum; (int success, long CreatedgroupId)result; switch (typeCode) { case TypeCode.Boolean: var bls = collection.ConvertArray <bool, ushort>(Convert.ToUInt16); result = rw.WriteFromArray <ushort>(groupId, name, bls, datasetName); Hdf5.WriteStringAttribute(groupId, name, "Boolean", name); break; case TypeCode.Byte: result = rw.WriteFromArray <byte>(groupId, name, collection, datasetName); Hdf5.WriteStringAttribute(groupId, name, "Byte", name); break; case TypeCode.Char: var chrs = collection.ConvertArray <char, ushort>(Convert.ToUInt16); result = rw.WriteFromArray <ushort>(groupId, name, chrs, datasetName); Hdf5.WriteStringAttribute(groupId, name, "Char", name); break; case TypeCode.DateTime: var dts = collection.ConvertArray <DateTime, long>(dt => Hdf5Conversions.FromDatetime(dt, Hdf5.Hdf5Settings.DateTimeType)); result = rw.WriteFromArray <long>(groupId, name, dts, datasetName); Hdf5.WriteStringAttribute(groupId, name, "DateTime", name); break; case TypeCode.Decimal: var decs = collection.ConvertArray <decimal, double>(Convert.ToDouble); result = rw.WriteFromArray <double>(groupId, name, decs, datasetName); Hdf5.WriteStringAttribute(groupId, name, "Decimal", name); break; case TypeCode.Double: result = rw.WriteFromArray <double>(groupId, name, collection, datasetName); break; case TypeCode.Int16: result = rw.WriteFromArray <short>(groupId, name, collection, datasetName); break; case TypeCode.Int32: result = rw.WriteFromArray <int>(groupId, name, collection, datasetName); break; case TypeCode.Int64: result = rw.WriteFromArray <long>(groupId, name, collection, datasetName); break; case TypeCode.SByte: result = rw.WriteFromArray <sbyte>(groupId, name, collection, datasetName); Hdf5.WriteStringAttribute(groupId, name, "SByte", name); break; case TypeCode.Single: result = rw.WriteFromArray <float>(groupId, name, collection, datasetName); break; case TypeCode.UInt16: result = rw.WriteFromArray <ushort>(groupId, name, collection, datasetName); break; case TypeCode.UInt32: result = rw.WriteFromArray <uint>(groupId, name, collection, datasetName); break; case TypeCode.UInt64: result = rw.WriteFromArray <ulong>(groupId, name, collection, datasetName); break; case TypeCode.String: if (collection.Rank > 1 && collection.GetLength(1) > 1) { throw new Exception("Only 1 dimensional string arrays allowed: " + name); } result = rw.WriteStrings(groupId, name, (string[])collection, datasetName); break; default: if (elementType == typeof(TimeSpan)) { var tss = collection.ConvertArray <TimeSpan, long>(dt => dt.Ticks); result = rw.WriteFromArray <long>(groupId, name, tss, datasetName); Hdf5.WriteStringAttribute(groupId, name, "TimeSpan", name); } //else if (isStruct) { // rw.WriteStucts(groupId, name, collection); //} else { string str = "type is not supported: "; throw new NotSupportedException(str + elementType.FullName); } break; } //append attributes foreach (KeyValuePair <string, List <string> > entry in attributes) { Hdf5.WriteStringAttribute(groupId, entry.Key, string.Join("',", entry.Value), name); } return(result); }
public void WriteArray(hid_t groupId, string name, Array collection, string datasetName = null) { Type type = collection.GetType(); Type elementType = type.GetElementType(); TypeCode typeCode = Type.GetTypeCode(elementType); //Boolean isStruct = type.IsValueType && !type.IsEnum; switch (typeCode) { case TypeCode.Boolean: var bls = collection.ConvertArray <Boolean, UInt16>(bl => Convert.ToUInt16(bl)); rw.WriteFromArray <UInt16>(groupId, name, bls, datasetName); Hdf5.WriteStringAttribute(groupId, name, "Boolean", name); break; case TypeCode.Byte: rw.WriteFromArray <Byte>(groupId, name, collection, datasetName); Hdf5.WriteStringAttribute(groupId, name, "Byte", name); break; case TypeCode.Char: var chrs = collection.ConvertArray <Char, UInt16>(c => Convert.ToUInt16(c)); rw.WriteFromArray <UInt16>(groupId, name, chrs, datasetName); Hdf5.WriteStringAttribute(groupId, name, "Char", name); break; case TypeCode.DateTime: var dts = collection.ConvertArray <DateTime, long>(dt => dt.Ticks); rw.WriteFromArray <long>(groupId, name, dts, datasetName); Hdf5.WriteStringAttribute(groupId, name, "DateTime", name); break; case TypeCode.Decimal: var decs = collection.ConvertArray <decimal, double>(dc => Convert.ToDouble(dc)); rw.WriteFromArray <double>(groupId, name, decs, datasetName); Hdf5.WriteStringAttribute(groupId, name, "Decimal", name); break; case TypeCode.Double: rw.WriteFromArray <double>(groupId, name, collection, datasetName); break; case TypeCode.Int16: rw.WriteFromArray <short>(groupId, name, collection, datasetName); break; case TypeCode.Int32: rw.WriteFromArray <Int32>(groupId, name, collection, datasetName); break; case TypeCode.Int64: rw.WriteFromArray <Int64>(groupId, name, collection, datasetName); break; case TypeCode.SByte: rw.WriteFromArray <SByte>(groupId, name, collection, datasetName); Hdf5.WriteStringAttribute(groupId, name, "SByte", name); break; case TypeCode.Single: rw.WriteFromArray <Single>(groupId, name, collection, datasetName); break; case TypeCode.UInt16: rw.WriteFromArray <UInt16>(groupId, name, collection, datasetName); break; case TypeCode.UInt32: rw.WriteFromArray <UInt32>(groupId, name, collection, datasetName); break; case TypeCode.UInt64: rw.WriteFromArray <UInt64>(groupId, name, collection, datasetName); break; case TypeCode.String: if (collection.Rank > 1 && collection.GetLength(1) > 1) { throw new Exception("Only 1 dimensional string arrays allowed: " + name); } rw.WriteStrings(groupId, name, (string[])collection, datasetName); break; default: if (elementType == typeof(TimeSpan)) { var tss = collection.ConvertArray <TimeSpan, long>(dt => dt.Ticks); rw.WriteFromArray <Int64>(groupId, name, tss, datasetName); Hdf5.WriteStringAttribute(groupId, name, "TimeSpan", name); } //else if (isStruct) { // rw.WriteStucts(groupId, name, collection); //} else { string str = "type is not supported: "; throw new NotSupportedException(str + elementType.FullName); } break; } //WriteHdf5Attributes(type, groupId, name, name); }