-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextractdata.py
More file actions
59 lines (48 loc) · 1.37 KB
/
extractdata.py
File metadata and controls
59 lines (48 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#h5 file reader
import h5py
import numpy as np
import matplotlib.pyplot as plt
import math
from math import pi,sqrt
import scipy
import scipy.constants
def gethdf5(filepath):
return h5py.File(filepath,"r")
'''
def hdf5_to_dict(group):
dict = {}
for key, item in group.items():
if isinstance(item, h5py.Group):
dict[key] = hdf5_to_dict(item)
else:
dict[key] = item[()]
return dict
def extract(path):
if path!="":
ex_data = h5py.File(path+'Fields0.h5', 'r')
return hdf5_to_dict(ex_data)
else:
ex_data = h5py.File('Fields0.h5', 'r')
return hdf5_to_dict(ex_data)
'''
'''
def hdf5_to_dict(group):
"""
Recursively convert an HDF5 group and its contents into a dictionary,
including attributes at each level.
"""
result = {'attrs': dict(group.attrs)} # Include attributes for the current group
for key, item in group.items():
if isinstance(item, h5py.Group):
result[key] = hdf5_to_dict(item)
else:
result[key] = {'data': item[()], 'attrs': dict(item.attrs)} # Include attributes for the dataset
return result
def extract(file_path):
"""
Convert an entire HDF5 file into a dictionary.
"""
with h5py.File(file_path, 'r') as file:
return hdf5_to_dict(file)
data_dict = extract("azim_4/Fields0.h5")
'''