/
read.py
163 lines (114 loc) · 4.05 KB
/
read.py
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
'''
Created on 21 Sep 2015
Modified: 9 aug 2017
@author: sm6110
'''
import h5py
from warnings import warn
# from IPython import embed
def adjust_special_structures(data):
'''
Given a class data resulting from reading a h5 file generated by the SaveData
SHARPy solver, this function re-adjust some of the entries of data so as to
match those used in SHARPy workflow.
'''
# ----- dictionaries
data.from_class_to_dict('settings')
# ----- timestep_info
def adjust_timestep_info(H):
H.timestep_info=[]
tt=0
while hasattr(H,'tsinfo%.5d' %tt):
H.timestep_info.append( getattr(H,'tsinfo%.5d' %tt) )
delattr(H, 'tsinfo%.5d' %tt)
tt+=1
if hasattr(data,'aero'):
adjust_timestep_info(data.aero)
if hasattr(data.aero,'beam'): adjust_timestep_info(data.aero.beam)
if hasattr(data,'structure'): adjust_timestep_info(data.structure)
# ----- strct2aero_mapping
if hasattr(data,'aero'):
M=data.aero.struct2aero_mapping
n_nodes=max(M[:,0])+1
rowM=M.shape[0] # rowM > n_nodes
data.aero.struct2aero_mapping=[]
for nn in range(n_nodes):
data.aero.struct2aero_mapping.append([])
for jj in range(rowM):
if M[jj,0]==nn:
data.aero.struct2aero_mapping[nn].append(
{'i_n': M[jj,2], 'i_surf': M[jj,1]})
def h5file(filename,ReadList=None):
'''
Read entries of a HDF5 file and attaches them to class.
Groups are saved as sub-classes, while dataset values are saved as class
attributes
Important: though this method allows to read all the input/output classes
required to run the aeroelastic solutions, ctypes variable are not saved as
such!
If a ReadList=None all groups/attributes are read.
If ReadList is given, the following format is used:
ReadList = ['name']
if name is an attribute, this is read as such.
if name refers to a group, all entries of the group are read
'''
Hinst=ReadInto()
### read and scan file
hdfile=h5py.File(filename,'r')
NamesList=[] # dataset names
hdfile.visit(NamesList.append)
### Identify higher level groups / attributes
if ReadList is None:
MainLev=[]
for name in NamesList:
if '/' not in name: MainLev.append(name)
else:
MainLev=ReadList
### Loop through higher level
for name in MainLev:
# sub-group
if type(hdfile[name]) is h5py._hl.group.Group:
#print('adding group %s as subclass' %name)
Ginst=read_group_as_class(hdfile[name])
Ginst.name=name
setattr(Hinst,name,Ginst)
else:
#print('adding attribute %s' %name)
setattr(Hinst,name,hdfile[name].value)
# close and return
hdfile.close()
return Hinst
def read_group_as_class(Grp):
''' Read an hdf5 group '''
Hinst=ReadInto()
NamesList=[]
Grp.visit(NamesList.append)
### identify higher level
MainLev=[]
for name in NamesList:
if '/' not in name: MainLev.append(name)
### Loop through higher level
for name in MainLev:
# sub-group
if type(Grp[name]) is h5py._hl.group.Group:
#print('adding subclass %s' %name)
Ginst=read_group_as_class(Grp[name])
setattr(Hinst,name,Ginst)
else:
#print('adding attribute %s' %name)
setattr(Hinst,name,Grp[name].value)
return Hinst
def from_class_to_dict(H):
''' From class to dictionary '''
D=H.__dict__
for attr in D:
if hasattr(D[attr],'__dict__'):
D[attr]=from_class_to_dict(D[attr])
return D
class ReadInto:
''' Default class to read hdf5 groups into '''
def from_class_to_dict(self,attr):
'''converts a sub-class into a dictionary'''
setattr(self,attr, from_class_to_dict(getattr(self,attr)) )
if __name__=='__main__':
pass