简体   繁体   English

如何编辑hdf5文件的一部分

[英]how to edit part of an hdf5 file

I'm trying to edit precipitation rate values in an existing hdf5 file such that values >= 10 get rewritten as 1 and values < 10 get rewritten as 0. This is what I have so far.我正在尝试在现有的 hdf5 文件中编辑降水率值,以便将 >= 10 的值重写为 1,将值 < 10 的值重写为 0。这就是我目前所拥有的。 The code runs without errors, but after checking the hdf5 files it appears that the changes to the precipitation rate dataset weren't made.代码运行没有错误,但在检查 hdf5 文件后,似乎没有对降水率数据集进行更改。 I'd appreciate any ideas on how to make it work.我将不胜感激有关如何使其发挥作用的任何想法。

import h5py
import numpy as np
import glob

filenames = []
filenames += glob.glob("/IMERG/Exceedance/2014_E/3B-HHR.MS.MRG.3IMERG.201401*")

for file in filenames:
    f = h5py.File(file,'r+')
    new_value = np.zeros((3600, 1800))
    new_value = new_value.astype(int)
    precip = f['Grid/precipitationCal'][0][:][:]

    for i in precip:
        for j in i:
            if j >= 10.0:
                new_value[...] = 1
            else:
                pass
    precip[...] = new_value
    f.close()

It seems like you are not writing the new values into the file, but only storing them in an array.似乎您没有将新值写入文件,而只是将它们存储在数组中。

It seems like you're only changing the values of the array, not actually updating anything in the file object.似乎您只是在更改数组的值,实际上并未更新文件 object 中的任何内容。 Also, I'd get rid of that for loop - it's slow: Try this:另外,我会摆脱那个 for 循环 - 它很慢:试试这个:

import h5py
import numpy as np
import glob

filenames = []
filenames += glob.glob("/IMERG/Exceedance/2014_E/3B-HHR.MS.MRG.3IMERG.201401*")

for file in filenames:
    f = h5py.File(file,'r+')
    precip = f['Grid/precipitationCal'][0][:][:]

    # Replacing the for loop
    precip[precip>10.0] = 1

    # Assign values
    f['Grid/precipitationCal'][0][:][:] = precip
    f.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM