Exemplo n.º 1
0
        // private readonly ReaderWriterLockSlim lock_ = new ReaderWriterLockSlim();

        public Hdf5AcquisitionFileWriter(string filename, string groupName = "ROOT")
        {
            H5E.set_auto(H5E.DEFAULT, null, IntPtr.Zero);
            //lock_.EnterWriteLock();
            _filename  = filename;
            fileId     = Hdf5.CreateFile(filename);
            _groupName = groupName;
            _groupId   = Hdf5.CreateGroup(fileId, Hdf5Utils.NormalizedName(_groupName));

            Header       = new Hdf5AcquisitionFile();
            _nrOfRecords = 0;
            _sampleCount = 0;
            //lock_.ExitWriteLock();
        }
Exemplo n.º 2
0
 public void SaveHeader()
 {
     //lock_.EnterWriteLock();
     Trace.WriteLine($"saving file {Header.Patient.Name} samples: {_sampleCount}; fileId: {fileId}");
     Header.Recording.EndTime     = Header.Recording.StartTime + TimeSpan.FromSeconds(_sampleCount / Header.Recording.SampleRate);
     Header.Recording.NrOfSamples = _sampleCount;
     Header.EventListToEvents();
     for (int i = 0; i < Header.Channels.Count(); i++)
     {
         Header.Channels[i].NrOfSamples = _sampleCount;
     }
     Trace.WriteLine($"writing file {Header.Patient.Name} groupId: {_groupId}; fileId: {fileId}");
     Hdf5.WriteObject(_groupId, Header);
     //lock_.ExitWriteLock();
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="Hdf5AcquisitionFileReader"/> class.
        /// </summary>
        /// <param name="filename">A filename.</param>
        /// <param name="groupName">a root group. If not specified used "ROOT</param>
        public Hdf5AcquisitionFileReader(string filename, string[] labels = null, string groupName = "ROOT")
        {
            fileId     = Hdf5.OpenFile(filename, readOnly: true);
            _header    = Hdf5.ReadObject <Hdf5AcquisitionFile>(fileId, groupName);
            _groupName = groupName;

            _usedChannels = new Dictionary <string, short>();
            for (short i = 0; i < _header.Recording.NrOfChannels; i++)
            {
                _usedChannels.Add(_header.Channels[i].Label, i);
            }
            if (labels == null)
            {
                _labels = _header.Channels.Select(c => c.Label).ToList();
            }
            else
            {
                _labels = labels;
            }
            _readChannelCnt = _labels.Count;
            _signals        = new List <short[]>(_readChannelCnt);
        }
        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);
        }