简体   繁体   中英

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. The code runs without errors, but after checking the hdf5 files it appears that the changes to the precipitation rate dataset weren't made. 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. Also, I'd get rid of that for loop - it's slow: Try this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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