示例#1
0
        public void TestAppend()
        {
            long[]   longs = Enumerable.Range(0, 500).Select(i => (long)i).ToArray();
            double[] doubles = Enumerable.Range(1000, 20000).Select(i => (double)i).ToArray();
            string   kLong = "long dataset", kDouble = "double dataset";

            TestBeforeAndAfter(
                () =>
            {
                file.CreateDataset(kLong, longs);
                file.Close();
                file = new HDF5File(FullFilename, HDF5FileMode.ReadWrite);
                file.CreateDataset(kDouble, doubles);
            },
                () =>
            {
                Assert.IsTrue(file.DataSets.ContainsKey(kLong));
                var ds = file.DataSets[kLong];
                Assert.AreEqual(new uint[] { 500 }, ds.Shape);
                long[] retrievedLongs = (long[])ds.Get();
                Assert.AreEqual(longs, retrievedLongs);

                Assert.IsTrue(file.DataSets.ContainsKey(kDouble));
                ds = file.DataSets[kDouble];
                Assert.AreEqual(new uint[] { 20000 }, ds.Shape);
                double[] retrievedDoubles = (double[])ds.Get();
                Assert.AreEqual(doubles, retrievedDoubles);
            }
                );
        }
示例#2
0
        public void TestExceptionOnCreateExisting()
        {
            var fn = $"{TestContext.CurrentContext.TestDirectory}\\TestCreateTwice.h5";

            if (File.Exists(fn))
            {
                File.Delete(fn);
            }

            HDF5File f1 = null, f2 = null;
            var      threw = false;

            f1 = new HDF5File(fn, HDF5FileMode.WriteNew);
            try
            {
                f2 = new HDF5File(fn, HDF5FileMode.WriteNew);
            }
            catch (IOException)
            {
                threw = true;
            }

            f1.Close();
            Assert.IsTrue(threw, "Expected an IOException when we tried to create the file the second time round.");
            Assert.IsNull(f2);
        }
示例#3
0
        /// <summary>
        /// 创建 HDF5 文件,并设置可以在采集开始的时候设置的 Attribute
        /// <param name="dataFilePath">HDF5 文件完整路径</param>
        /// <param name="sampleRate">采样率</param>
        /// <param name="channelCount">通道总数</param>
        /// <param name="startTime">采集开始时间</param>
        /// /// </summary>
        public DataFileConfig(string dataFilePath, double sampleRate, int channelCount, double startTime)
        {
            //这个地方,之前的 BinData 不会有问题,因为只是创建了流,流写入文件是最后做的
            //而这里一开始就创建了文件,所以如果没有正常写完并调用 SetAndClose 方法,就会有文件打开而没有关闭的风险
            try
            {
                //创建文件
                myHDF5File  = new HDF5File(dataFilePath);
                myHDF5Group = myHDF5File.CreateGroup("Attribute");

                //设置 Attribute
                myHDF5Group.SetAttribute("StartTime", HDF5AttributeTypes.DOUBLE, startTime);
                myHDF5Group.SetAttribute("SampleRate", HDF5AttributeTypes.DOUBLE, sampleRate);
                myHDF5Group.SetAttribute("ChannelCount", HDF5AttributeTypes.DOUBLE, channelCount);
                myHDF5Group.SetAttribute("CreateTime", HDF5AttributeTypes.DOUBLE, Convert.ToDouble(DateTime.Now.ToLocalTime().ToString("yyyyMMddHHmmss")));

                //设置相应个数的 Dataset
                myHDF5Datasets = new HDF5Dataset[channelCount];
                for (int i = 0; i < channelCount; i++)
                {
                    myHDF5Datasets[i] = new HDF5Dataset();
                    myHDF5Datasets[i] = myHDF5File.CreateChunkDataset("channel" + i.ToString(), HDF5DataTypes.DOUBLE, new ulong[] { 1000 });
                }
            }
            catch (Exception e)
            {
                throw new Exception("Create HDF5 file Failed! Error message: " + e);
            }
        }
示例#4
0
        public void Setup()
        {
            string testDir = TestContext.CurrentContext.TestDirectory;
            string fn      = testDir + @"\..\..\..\TestData\SimpleTest.h5";

            Assert.IsTrue(File.Exists(fn), String.Format("Expected {0} accessible from {1}", fn, testDir));
            file = new HDF5File(fn);
        }
示例#5
0
        public void Setup()
        {
            FullFilename = $"{TestContext.CurrentContext.TestDirectory}\\{BaseFilename}";
            if (File.Exists(FullFilename))
            {
                File.Delete(FullFilename);
            }

            file = new HDF5File(FullFilename, HDF5FileMode.ReadWrite);
            Assert.IsTrue(File.Exists(FullFilename), String.Format("Expected {0} created", FullFilename));
        }
示例#6
0
        /// <summary>
        /// 依照 BinData 签名的构造函数
        /// </summary>
        /// <param name="dataFileDirectory">HDF5 文件所在文件夹</param>
        /// <param name="name">HDF5 文件名不带后缀</param>
        public DataReader(string dataFileDirectory, string name)
        {
            this.name         = name;
            this.dataFilePath = dataFileDirectory + name + ".hdf5";

            myH5File = new HDF5File();
            myH5File.Open(this.dataFilePath);
            myH5Group = myH5File.GetGroup("Attribute");

            // double 转 object 后,object 不能直接转 int,需要先转 double 再转 int
            double d = (double)myH5Group.GetAttribute("ChannelCount");

            channelCount = (int)d;
            d            = (double)myH5Group.GetAttribute("SampleCount");
            sampleCount  = (int)d;
            sampleRate   = (double)myH5Group.GetAttribute("SampleRate");
            startTime    = (double)myH5Group.GetAttribute("StartTime");
            createTime   = (double)myH5Group.GetAttribute("CreateTime");

            myH5Group.Close();
            myH5File.Close();
        }
示例#7
0
 public void ReopenForRead()
 {
     file.Close();
     file = new HDF5File(FullFilename, HDF5FileMode.ReadOnly);
 }