Skip to content

Instantly share code, notes, and snippets.

@phaustin
Last active August 29, 2015 14:16
Show Gist options
  • Select an option

  • Save phaustin/254f63d0ff16c4e55c06 to your computer and use it in GitHub Desktop.

Select an option

Save phaustin/254f63d0ff16c4e55c06 to your computer and use it in GitHub Desktop.
hdf5 creation with groups
with h5py.File(output_name, "w") as f:
comments=dict(lat='degrees lat (between -90 and 90)',
lon='degrees lon (betwen 0 and 360)')
for var in ['lat','lon']:
dset = f.create_dataset(var,bigdict[var].shape, dtype=bigdict[var].dtype)
dset[...]=bigdict[var][...]
dset.attrs['comment']=comments[var]
#
# write the 6 temperature arrays in two groups, one for each season
#
for season in ['july','jan']:
group=f.create_group(season)
for var in varnames:
dset = group.create_dataset(var,bigdict[season][var].shape, dtype=bigdict[season][var].dtype)
dset[...]=bigdict[season][var][...]
input output pair:
to write:
output='correct.h5'
with h5py.File(output,'w') as f:
for month in out_dict.keys():
group=f.create_group(month)
for name,field in out_dict[month].items():
dset=group.create_dataset(name,field.shape,dtype=field.dtype)
dset[...]=field[...]
f.attrs['history']='written by full retrieve.py tag noiter with correc={}'.format(correct)
f.attrs['correct_flag']=correct
to read:
input='correct.h5'
data_dict={}
with h5py.File(input,'r') as f:
for month,group in f.items():
data_dict[month]={}
for name,var in group.items():
data_dict[name]=var
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment