76 lines
2.8 KiB
Python
76 lines
2.8 KiB
Python
import pandas as pd
|
|
import numpy as np
|
|
import h5py as h5py
|
|
|
|
def get_df_pop(file_path):
|
|
def append_V_m3 (df_meta):
|
|
A_m2 = np.pi*(df_meta.r_max_m**2-df_meta.r_min_m**2)*(df_meta.phi_max-df_meta.phi_min)/2/np.pi
|
|
V_m3 = A_m2*(df_meta.z_max_m-df_meta.z_min_m)
|
|
df_meta.insert(len(df_meta.columns),'V_m3',V_m3)
|
|
return df_meta
|
|
|
|
def get_V_m3(df_meta):
|
|
r_bins = np.unique(df_meta.r_bin)
|
|
V_m3 = np.zeros(r_bins.size)
|
|
for i in np.arange(r_bins.size):
|
|
V_m3[i] = np.mean (df_meta[df_meta.r_bin==r_bins[i]].V_m3)
|
|
return V_m3
|
|
|
|
def append_n_m3 (df, V_m3):
|
|
n_m3 = df.cum_time/df.phys_time_s/V_m3[df.r_bin]
|
|
df['n_m3'] = n_m3
|
|
df['n_m3'].fillna(0., inplace=True)
|
|
return df
|
|
|
|
def append_from_meta(df, df_meta):
|
|
df_length = df.shape[0]
|
|
bin_list = ['z_bin','r_bin','phi_bin','E_bin']
|
|
min_list = ['z_min_m', 'r_min_m', 'phi_min', 'E_min_eV']
|
|
max_list = ['z_max_m', 'r_max_m', 'phi_max', 'E_max_eV']
|
|
for i in np.arange(4):
|
|
i_bin = bin_list[i]
|
|
i_min = min_list[i]
|
|
i_max = max_list[i]
|
|
info_df = df_meta.groupby(i_bin, as_index=False).mean()
|
|
mins = np.zeros(df_length)
|
|
maxs = np.zeros(df_length)
|
|
for row in range(0, info_df.shape[0]):
|
|
mins[df[i_bin] == info_df[i_bin][row]] = info_df[i_min][row]
|
|
maxs[df[i_bin] == info_df[i_bin][row]] = info_df[i_max][row]
|
|
df[i_min] = mins
|
|
df[i_max] = maxs
|
|
return df
|
|
|
|
f = h5py.File(file_path, 'r')
|
|
df = pd.DataFrame()
|
|
species = list(f['Populations'].keys())
|
|
for s in species:
|
|
try: # If has been merged
|
|
pop_key = list(f['Populations'][s].keys())[0]
|
|
df_data = pd.DataFrame(f['Populations'][s][pop_key][s][:])
|
|
df_meta = pd.DataFrame(f['Populations'][s][pop_key][s + '_meta_data'][:])
|
|
except: # If not merged
|
|
df_data = pd.DataFrame(f['Populations'][s][s][:])
|
|
df_meta = pd.DataFrame(f['Populations'][s][s + '_meta_data'][:])
|
|
|
|
df_meta = append_V_m3(df_meta)
|
|
V_m3 = get_V_m3(df_meta)
|
|
df_data = append_n_m3(df_data, V_m3)
|
|
df_data = append_from_meta(df_data, df_meta)
|
|
|
|
a = pd.DataFrame({
|
|
'species' : s,
|
|
't' : df_data['phys_time_s'],
|
|
'zmin' : df_data['z_min_m'],
|
|
'zmax' : df_data['z_max_m'],
|
|
'rmin' : df_data['r_min_m'],
|
|
'rmax' : df_data['r_max_m'],
|
|
'phimin' : df_data['phi_min'],
|
|
'phimax' : df_data['phi_max'],
|
|
'Emin' : df_data['E_min_eV'],
|
|
'Emax' : df_data['E_max_eV'],
|
|
'n': df_data['n_m3']
|
|
})
|
|
df = df.append(a) #append to final
|
|
|
|
return (df) |