Created
December 2, 2020 13:12
-
-
Save mraspaud/48fc050fe4990b3e3978809d4f6f3936 to your computer and use it in GitHub Desktop.
Example on exporting hdf5 file to a python dictionary
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# Copyright (c) 2019 Satpy developers | |
# | |
# This file is part of satpy. | |
# | |
# satpy is free software: you can redistribute it and/or modify it under the | |
# terms of the GNU General Public License as published by the Free Software | |
# Foundation, either version 3 of the License, or (at your option) any later | |
# version. | |
# | |
# satpy is distributed in the hope that it will be useful, but WITHOUT ANY | |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR | |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License along with | |
# satpy. If not, see <http://www.gnu.org/licenses/>. | |
import h5py | |
import sys | |
import numpy as np | |
import dpath | |
import json | |
res = dict() | |
def print_item(name, value): | |
assert name not in ['value', 'attrs'] | |
# print(name) | |
if isinstance(value, h5py._hl.dataset.Dataset): | |
if value.size > 300: | |
dpath.util.new(res, name + '/value', 'np.rand.random({}, dtype=np.{})'.format(value.shape, value.dtype)) | |
else: | |
ndvalue = np.array(value) | |
try: | |
ndvalue = ndvalue.item() | |
except ValueError: | |
pass | |
dpath.util.new(res, name + '/value', ndvalue) | |
elif isinstance(value, h5py._hl.group.Group): | |
pass | |
else: | |
if value: | |
dpath.util.new(res, name + '/value', value) | |
if value.attrs: | |
dpath.util.new(res, name + '/attrs', dict(value.attrs)) | |
h5f = h5py.File(sys.argv[1], 'r') | |
h5f.visititems(print_item) | |
res['attrs'] = dict(h5f.attrs) | |
from pprint import pprint | |
pprint(res) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment