我已经在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.
}
Run Code Online (Sandbox Code Playgroud)
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)
Run Code Online (Sandbox Code Playgroud)
文档说它hdf5load需要一个文件名,但也可能需要一个文件句柄。如果是这样,这可能会起作用。