// 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.CreateOrOpenGroup(fileId, Hdf5Utils.NormalizedName(_groupName));

            Header       = new Hdf5AcquisitionFile();
            _nrOfRecords = 0;
            _sampleCount = 0;
            //lock_.ExitWriteLock();
        }
 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();
 }
Example #3
0
        public static bool WriteAttributeByPath(string fileName, string xpath, string attributeName, string value)
        {
            long fileId = -1;

            try
            {
                var fileStructure = Hdf5.ReadFlatFileStructure(fileName);
                fileId = Hdf5.OpenFile(fileName, true);
                if (fileId <= 0)
                {
                    LogMessage("Invalid type", Hdf5LogLevel.Error);
                    return(false);
                }

                var group = fileStructure.SingleOrDefault(element => element.Name == xpath);
                if (group == null)
                {
                    LogMessage($"group {xpath} was not found", Hdf5LogLevel.Error);
                    return(false);
                }

                var groupAccessId = H5G.open(fileId, group.Name);
                if (groupAccessId <= 0)
                {
                    LogMessage($"unable to open group", Hdf5LogLevel.Error);
                    return(false);
                }

                var result = Hdf5.WriteAttribute(groupAccessId, attributeName, value);
                return(result.success >= 0);
            }
            catch (Exception e)
            {
                LogMessage($"Error reading Attribute: {e.Message}", Hdf5LogLevel.Error);
                return(false);
            }
            finally
            {
                if (fileId > 0)
                {
                    H5F.close(fileId);
                }
            }
        }
Example #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Hdf5AcquisitionFileReader"/> class.
        /// </summary>
        /// <param name="filename">A filename.</param>
        /// <param name="labels"></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);
        }
Example #5
0
        public (int success, long CreatedId) WriteArray(long groupId, string name, Array collection, Dictionary <string, List <string> > attributes)
        {
            Type     type        = collection.GetType();
            Type     elementType = type.GetElementType();
            TypeCode typeCode    = Type.GetTypeCode(elementType);

            (int success, long CreatedId)result;
            switch (typeCode)
            {
            case TypeCode.Boolean:
                var bls = collection.ConvertArray <bool, ushort>(Convert.ToUInt16);
                result = rw.WriteFromArray <ushort>(groupId, name, bls);
                break;

            case TypeCode.Byte:
                result = rw.WriteFromArray <byte>(groupId, name, collection);
                break;

            case TypeCode.Char:
                var chrs = collection.ConvertArray <char, ushort>(Convert.ToUInt16);
                result = rw.WriteFromArray <ushort>(groupId, name, chrs);
                break;

            case TypeCode.DateTime:
                var dts = collection.ConvertArray <DateTime, long>(dt => Hdf5Conversions.FromDatetime(dt, Hdf5.Settings.DateTimeType));
                result = rw.WriteFromArray <long>(groupId, name, dts);
                break;

            case TypeCode.Decimal:
                var decs = collection.ConvertArray <decimal, double>(Convert.ToDouble);
                result = rw.WriteFromArray <double>(groupId, name, decs);
                break;

            case TypeCode.Double:
                result = rw.WriteFromArray <double>(groupId, name, collection);
                break;

            case TypeCode.Int16:
                result = rw.WriteFromArray <short>(groupId, name, collection);
                break;

            case TypeCode.Int32:
                result = rw.WriteFromArray <int>(groupId, name, collection);
                break;

            case TypeCode.Int64:
                result = rw.WriteFromArray <long>(groupId, name, collection);
                break;

            case TypeCode.SByte:
                result = rw.WriteFromArray <sbyte>(groupId, name, collection);
                break;

            case TypeCode.Single:
                result = rw.WriteFromArray <float>(groupId, name, collection);
                break;

            case TypeCode.UInt16:
                result = rw.WriteFromArray <ushort>(groupId, name, collection);
                break;

            case TypeCode.UInt32:
                result = rw.WriteFromArray <uint>(groupId, name, collection);
                break;

            case TypeCode.UInt64:
                result = rw.WriteFromArray <ulong>(groupId, name, collection);
                break;

            case TypeCode.String:
                if (collection.Rank > 1 && collection.GetLength(1) > 1)
                {
                    throw new Hdf5Exception("Only 1 dimensional string arrays allowed: " + name);
                }

                result = rw.WriteStrings(groupId, name, (string[])collection);
                break;

            default:
                if (elementType == typeof(TimeSpan))
                {
                    var tss = collection.ConvertArray <TimeSpan, long>(dt => dt.Ticks);
                    result = rw.WriteFromArray <long>(groupId, name, tss);
                }
                else
                {
                    string str = "type is not supported: ";
                    throw new NotSupportedException(str + elementType.FullName);
                }
                break;
            }

            if (result.success == 0)//append attributes
            {
                foreach (KeyValuePair <string, List <string> > entry in attributes)
                {
                    Hdf5.WriteStringAttribute(groupId, entry.Key, string.Join("',", entry.Value), name);
                }
            }

            return(result);
        }