【问题标题】:how to edit part of an hdf5 file如何编辑hdf5文件的一部分
【发布时间】:2022-01-06 17:10:37
【问题描述】:

我正在尝试编辑现有 hdf5 文件中的降水率值,以便将 >= 10 的值重写为 1,将

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()

【问题讨论】:

    标签: python if-statement hdf5 h5py


    【解决方案1】:

    您似乎只是在更改数组的值,而实际上并未更新文件对象中的任何内容。另外,我会摆脱那个 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()
    

    【讨论】:

      【解决方案2】:

      您似乎没有将新值写入文件,而只是将它们存储在数组中。

      【讨论】:

      • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
      猜你喜欢
      • 1970-01-01
      • 2015-01-24
      • 2017-05-29
      • 2010-11-20
      • 1970-01-01
      • 1970-01-01
      • 2020-03-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多