/// <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); } }
/// <summary> /// 从文件中读出指定通道的数据片段,支持复杂读取 /// </summary> /// <param name="channelNo">指定的通道,从 0 开始</param> /// <param name="start">开始点</param> /// <param name="stride">每次向后多少点</param> /// <param name="count">读多少次</param> /// <param name="block">每次读多少个连续点</param> /// <returns></returns> public double[] LoadDataFromFileComplex(int channelNo, ulong start, ulong stride, ulong count, ulong block = 1) { if (!IsParamsRight(channelNo, start, stride, count, block)) { return(null); } try { myH5File.Open(dataFilePath); myH5Dataset = myH5File.GetDataset("channel" + channelNo.ToString()); double[] data = (double[])myH5Dataset.ComplexRead1DimChunkDataset(start, stride, count, block); myH5Dataset.Close(); myH5File.Close(); return(data); } catch { throw new Exception("Something wrong with Data Read, may be you can adjust your params and try again."); } }