diff --git a/sdf_helper/HDF5_plotter.py b/sdf_helper/HDF5_plotter.py new file mode 100644 index 0000000..d04dcdc --- /dev/null +++ b/sdf_helper/HDF5_plotter.py @@ -0,0 +1,13 @@ +import h5py +import numpy as np +import matplotlib.pyplot as plt + +f = h5py.File('data.h5','r') #開啟h5檔案 +print(f.keys()) #可以檢視所有的主鍵 +domain = f['Electric_Field_Ey'][:] #取出主鍵為data的所有的鍵值 +f.close() + +plt.contourf(domain) +plt.colorbar() +plt.show() +plt.clf() \ No newline at end of file diff --git a/sdf_helper/SDF2HDF5.py b/sdf_helper/SDF2HDF5.py new file mode 100644 index 0000000..6b8b28f --- /dev/null +++ b/sdf_helper/SDF2HDF5.py @@ -0,0 +1,30 @@ +# -*- coding: utf-8 -*- +""" +Created on Wed Oct 6 22:40:44 2021 + +@author: Jennan Wang +""" + +import sdf_helper as sh +import h5py + +sdf_file_name = '0004.sdf' #sdf file name, assume call it at same folder for sdf +data = sh.getdata(sdf_file_name) #get attribute string +attri_str = str(sh.list_variables(data)).splitlines() #string processing... +variable_str_list = [] #string processing... +h5f = h5py.File(str(sdf_file_name+'.h5'), 'w') #open hdf5 file +h5f_data = [] #container for hdf5 data + +for i in attri_str: + variable_str_list.append((i.split(' '))[0]) #string processing... + exec("h5f_data.append(data.%s.data)" % (variable_str_list[-1])) + #put sdf data to container for hdf5 data by calling it's variables + h5f.create_dataset(variable_str_list[-1], data=h5f_data[-1]) #write to hdf5 file + +h5f.close() #close hdf5 file + + + + + + diff --git a/sdf_helper/sdf_helper.py b/sdf_helper/sdf_helper.py index debbafb..2141e93 100644 --- a/sdf_helper/sdf_helper.py +++ b/sdf_helper/sdf_helper.py @@ -2042,13 +2042,19 @@ def subarray(base, slices): def list_variables(data): dct = data.__dict__ + str_variables = '' for key in sorted(dct): try: val = dct[key] print('{} {} {}'.format(key, type(val), np.array2string(np.array(val.dims), separator=', '))) + str_variables += '{} {} {}'.format(key, type(val), + np.array2string(np.array(val.dims), separator=', ')) + str_variables += '\n' except: + return str_variables pass + return str_variables def escape_latex(string):