Commit 25ea46c1 authored by Maxime Rey's avatar Maxime Rey
Browse files

New name for outflow saves, now split for each output.

parent 7db7adab
......@@ -1339,20 +1339,16 @@ def h5_save_outflows(RamsesDir,timesteps, dist_kpc, prog_bar=True):
if not os.path.exists(filepath):
os.mkdir(filepath)
# Create a file to store data.
filename = '{}{}kpc_{}.h5'.format(filepath, dist_kpc, ''.join(str(tstep) for tstep in timesteps))
if os.path.exists(filename):
f = h5py.File(filename,'r+')
else:
f = h5py.File(filename,'w')
try_write_datasets(f, "time_myr", time_myr)
for i, timestep in enumerate(timesteps):
try_write_datasets(f, "out_rate_array_"+str(timestep), out_rate_array[i])
for i, timestep in enumerate(timesteps):
try_write_datasets(f, "T_array_"+str(timestep), T_array[i])
f.close()
filename = '{}{}kpc_{:05}.h5'.format(filepath, dist_kpc, timestep)
if os.path.exists(filename):
f = h5py.File(filename,'r+')
else:
f = h5py.File(filename,'w')
try_write_datasets(f, "time_myr", [time_myr[i]])
try_write_datasets(f, "out_rate_array", out_rate_array[i])
try_write_datasets(f, "T_array", T_array[i])
f.close()
def h5_get_outflows(RamsesDir,timesteps, dist_kpc, verbose=False, prog_bar=True):
......@@ -1377,31 +1373,15 @@ def h5_get_outflows(RamsesDir,timesteps, dist_kpc, verbose=False, prog_bar=True)
import h5py
filepath = '{}/ratadat/outflows/'.format(RamsesDir)
filename = '{}{}kpc_{}.h5'.format(filepath, dist_kpc, ''.join(str(tstep) for tstep in timesteps))
f = h5py.File(filename,'r')
time_myr = f['time_myr'][:]
out_rate_array = []
for index, timestep in enumerate(timesteps):
try:
out_rate_array.append(f['out_rate_array_'+str(timestep)][:])
except (ValueError, KeyError): # If the outflow for this output was not computed, compute it.
# This should work. If there is an error, it is because there is a problem adding the value to the time_myr list.
if verbose: print('The outflows were probably not saved for the considered timestep ({}), trying to correct it.'.format(timestep))
temp_time_myr, temp_out_rate_array, temp_T_array = get_outflows(RamsesDir,[timestep],dist_kpc, verbose=False, prog_bar=True)
time_myr.insert(index-1, temp_time_myr)
f.close()
f = h5py.File(filename,'r+')
try_write_datasets(f, "out_rate_array_"+str(timestep), temp_out_rate_array)
try_write_datasets(f, "T_array_"+str(timestep), temp_T_array)
f.close()
f = h5py.File(filename,'r')
out_rate_array, T_array, time_myr = [], [], []
T_array = []
for timestep in timesteps:
T_array.append(f['T_array_'+str(timestep)][:])
f.close()
filename = '{}{}kpc_{:05}.h5'.format(filepath, dist_kpc, timestep)
f = h5py.File(filename,'r')
time_myr.append(f['time_myr'][0])
out_rate_array.append(f['out_rate_array'][:])
T_array.append(f['T_array'][:])
f.close()
if verbose: print('Done reading cells from h5 file.')
......@@ -1436,23 +1416,16 @@ def extract_outflows(RamsesDir,timesteps, dist_kpc, saveinfile=True, verbose=Fal
print('------------------------------------ ')
print('COmputing outflows from {} at {} kpc.'.format(RamsesDir.rsplit('/', 2)[-2], dist_kpc))
filepath = '{}/ratadat/outflows/'.format(RamsesDir)
filename = '{}{}kpc_{}.h5'.format(filepath, dist_kpc, ''.join(str(tstep) for tstep in timesteps))
if not os.path.exists(filename):
if saveinfile: # Save data and read from save.
# Reading and saving cells.
h5_save_outflows(RamsesDir,timesteps, dist_kpc, prog_bar=prog_bar)
all_params = h5_get_outflows(RamsesDir,timesteps, dist_kpc, prog_bar=prog_bar)
else: # Read without saving.
# Reading but not saving cells.
for timestep in timesteps:
filename = '{}{}kpc_{:05}.h5'.format(filepath, dist_kpc, timestep)
if not os.path.exists(filename) and saveinfile: # Save data and read from save.
h5_save_outflows(RamsesDir,[timestep], dist_kpc, prog_bar=prog_bar)
elif not os.path.exists(filename) and not saveinfile: # Read without saving. If any timestep is missing, screw using saves and reads all data brutally.
all_params = get_outflows(RamsesDir,timesteps, dist_kpc, verbose=verbose, prog_bar=prog_bar)
else: # Read from save.
if saveinfile:
# Saveinfile set to True but file already exists, reading cells from save.
all_params = h5_get_outflows(RamsesDir,timesteps, dist_kpc, verbose=verbose, prog_bar=prog_bar)
else: # If file existed with wrong params, correct it
# Reading cells from save.
all_params = h5_get_outflows(RamsesDir,timesteps, dist_kpc, verbose=verbose, prog_bar=prog_bar)
return all_params
all_params = h5_get_outflows(RamsesDir,timesteps, dist_kpc, verbose=verbose, prog_bar=prog_bar)
return all_params
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment