gpt4 book ai didi

r - 在 R 中加载后如何关闭 hdf5 文件?

转载 作者:行者123 更新时间:2023-12-04 21:20:31 25 4
gpt4 key购买 nike

我已经在 linux 机器上成功安装了 hdf5 包。我现在希望从大量 hdf5 文件中循环读取数据并创建一个时间序列。每个 hdf5 文件对应不同的时间。在阅读了许多文件(刚刚超过 1000 个)后,R 说打开了太多文件。我想找到一种关闭它们的方法,以便循环可以继续。这是代码:

    fdate <- "200605312355" # the first date for my test loop
fmax <- 1400
arr <- vector()

for (i in 1:fmax){
fname <- paste(fdate,"_.h5") # path of h5 file
fid <- hdf5load(fname) # fid = NULL
arr[i] <- somevariable$data_array[lon,lat]
# would like to close the file here
fdate <- newdate(fdate,60*5) # a function that increments the date by seconds.
}

hdf5 包包含 hdf5cleanup 函数,它看起来可以清理东西,但它需要一个文件标识符。我上面代码中的 fid 返回 NULL。尝试插入文件名,即 hdf5cleanup(fname) 导致 R 中止。也许 hdf5load 应该关闭文件并失败。如果是这种情况,有没有办法通过发出 system() 命令或其他方式来关闭连接?

顺便说一句,showConnections() 什么都不返回,嗯,实际上只是标题名称“描述类模式文本 isopen 可以读可以写”。

我的问题简而言之:
使用 hdf5load() 加载后,如何关闭与 R 中的 hdf5 文件的连接?

最佳答案

注意:根据评论,以下答案不起作用。留下它,至少现在,标记一条不成功的追求路线。

我没有 hdf5安装,所以我无法检查这是否有效,所以这有点暗中拍摄:

fname <- paste(fdate,"_.h5") # path of h5 file
# fhandle <- open(fname) # Comment pointed out this was not the intended function
fhandle <- file(description = fname, open = "rb")
hdf5load(fhandle) # hdf5load returns nothing when load=TRUE (the default)
arr[i] <- somevariable$data_array[lon,lat]
close(fhandle)

文档说 hdf5load需要一个文件名,但它也可能需要一个文件句柄。如果是这样,这可能会奏效。

关于r - 在 R 中加载后如何关闭 hdf5 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12695318/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com