HDF5Kit
这是 HDF5 文件格式的 Swift 包装器。HDF5 在科学界用于管理大量数据。目的是使其从 Swift 读取和写入 HDF5 文件变得简单,包括 playground。
用法
此示例展示如何打开现有 HDF5 文件并将数据写入现有数据集。
import HDF5Kit
// Initialize the data
let dataWidth = 6
let dataHeight = 4
var data = [Double](repeating: 0.0, count: dataHeight * dataWidth)
for r in 0..<dataHeight {
for c in 0..<dataWidth {
data[r * dataWidth + c] = Double(r * dataWidth + c + 1)
}
}
// Open an existing file
let path = "file.h5"
guard let file = File.open(path, mode: .readWrite) else {
fatalError("Failed to open \(path)")
}
// Open an existing dataset
let datasetName = "dset"
guard let dataset = file.openDoubleDataset(datasetName) else {
fatalError("Failed to open dataset \(datasetName)")
}
// Write the data
try dataset.write(data)
使用 HDF5Kit 读取数据非常容易
// Open an existing file
let path = "file.h5"
guard let file = File.open(path, mode: .readWrite) else {
fatalError("Failed to open \(path)")
}
// Open an existing dataset
let datasetName = "dset"
guard let dataset = file.openStringDataset(datasetName) else {
fatalError("Failed to open dataset \(datasetName)")
}
let data = dataset[1...3, 2...5]
支持类型: Double
、Float
、Int
和 String
。