5 from nucleus_multiple_class
import nucleus_multiple
6 from template_class
import template
14 Minimalistic class to lazily read WinNet data.
20 - path: Path to the WinNet data
29 self.
filename = os.path.join(path,
"WinNet_data.h5")
35 self.
__template_path = os.path.join(path, [f
for f
in os.listdir(path)
if f.endswith(
'.par')][0])
41 Check if the run has crashed
43 if not hasattr(self,
"_wreader__template"):
51 Check if the run has crashed
53 if not hasattr(self,
"_wreader__is_crashed"):
60 Read if the run has crashed
75 if not hasattr(self,
"_wreader__A"):
81 Read the mass number A
87 self.
__N, self.
__Z = np.loadtxt(os.path.join(self.
__snapshot_path,
"snapsh_0001.dat"),unpack=
True, usecols=(0,1),
91 with h5py.File(self.
filename,
'r')
as hf:
92 self.
__A = hf[
'snapshots/A'][:]
93 self.
__Z = hf[
'snapshots/Z'][:]
94 self.
__N = hf[
'snapshots/N'][:]
96 error_msg =
'Failed to read A, Z, and N in "'+str(self.
path)+
'". '+\
97 'Snapshots not present as Ascii nor as Hdf5!'
98 raise ValueError(error_msg)
105 if not hasattr(self,
"_wreader__Z"):
114 if not hasattr(self,
"_wreader__N"):
130 snapshot_files = [f
for f
in snapshot_files
if f.startswith(
'snapsh_')]
134 for i, f
in enumerate(tqdm(snapshot_files, desc=
'Reading snapshots', disable=self.
silent)):
135 fname =
'snapsh_' + str(i+1).zfill(4) +
'.dat'
137 lines = file.readlines()
139 for j, line
in enumerate(lines[3:]):
143 with h5py.File(self.
filename,
'r')
as hf:
148 error_msg =
'Failed to read timescales in "'+str(self.
path)+
'". '+\
149 'Not present as Ascii nor as Hdf5!'
150 raise ValueError(error_msg)
156 Check whether an entry is in the hdf5 format (return 1),
157 or in the ascii format (return 2), or does not exist (return 0)
159 if (entry ==
'mainout'):
161 elif (entry ==
'timescales'):
163 elif (entry ==
'energy'):
165 elif (entry ==
'tracked_nuclei'):
166 value = self.
__check_files(
'tracked_nuclei.dat',
'tracked_nuclei')
167 elif (entry ==
'snapshot'):
168 value = self.
__check_files(
'snaps/snapsh_0001.dat',
'snapshots')
169 elif (entry ==
'flows'):
171 elif (entry ==
"nuloss"):
172 value = self.
__check_files(
'nu_loss_gain.dat',
'nu_loss_gain')
173 elif (entry ==
'finab'):
175 elif (entry ==
'finabsum'):
177 elif (entry ==
'finabelem'):
180 error_msg =
'Checked for unknown entry "'+str(entry)+
'". '
181 raise ValueError(error_msg)
188 Check if something exists in hdf5 or ascii
191 if not os.path.exists(self.
filename):
192 if not os.path.exists(os.path.join(self.
path, ascii_file_path)):
197 with h5py.File(self.
filename,
'r')
as hf:
198 if hdf5_key
in hf.keys():
200 elif os.path.exists(os.path.join(self.
path, ascii_file_path)):
212 if not hasattr(self,
"_wreader__nr_of_snaps"):
218 Read the number of snapshots
225 snapshot_files = [f
for f
in snapshot_files
if f.startswith(
'snapsh_')]
228 with h5py.File(self.
filename,
'r')
as hf:
231 error_msg =
'Failed to read nr of snapshots in "'+str(self.
path)+
'". '+\
232 'Not present as Ascii nor as Hdf5!'
233 raise ValueError(error_msg)
238 Get the tracked nuclei
240 if not hasattr(self,
"_wreader__tracked_nuclei"):
246 Read the tracked nuclei
254 path = os.path.join(self.
path,
"tracked_nuclei.dat")
255 with open(path,
'r')
as f:
256 first_line = f.readline()
258 first_line = first_line.replace(
'Y(',
'')
259 first_line = first_line.replace(
')',
'')
260 first_line = first_line.replace(
'#',
'')
261 track_nuclei_names = first_line.split()[1:]
262 track_nuclei_data = np.loadtxt(path, skiprows=1)
267 Xnuclei = track_nuclei_data[:,1:]*A
274 self.
__tracked_nuclei[
'latex_names'] = [
r"$^{"+str(A[i])+
r"}$"+elnames[i].title()
for i
in range(len(A))]
275 for i, name
in enumerate(track_nuclei_names):
278 with h5py.File(self.
filename,
'r')
as hf:
286 for i, name
in enumerate(nm.names):
291 self.
__tracked_nuclei[
'latex_names'] = [
r"$^{"+str(A[i])+
r"}$"+elnames[i].title()
for i
in range(len(A))]
293 error_msg =
'Failed to read tracked nuclei in "'+str(self.
path)+
'". '+\
294 'Not present as Ascii nor as Hdf5!'
295 raise ValueError(error_msg)
300 Get abundance at snapshot idx
302 if not hasattr(self,
"_wreader__snapshots_time"):
309 Get mass fraction at snapshot idx
311 if not hasattr(self,
"_wreader__snapshots_time"):
318 Get time at snapshot idx
320 if not hasattr(self,
"_wreader__snapshots_time"):
327 Get the timescale of "tau", e.g., "tau_ag"
329 if not hasattr(self,
"_wreader__timescales"):
337 Get the neutrino losses and gains
339 if not hasattr(self,
"_wreader__nuloss"):
346 Read the neutrino losses and gains
352 columns = [
"time",
"temp",
"dens",
"rad",
"nu_total",
"nu_beta",
"nu_nuheat",
"nu_thermal"]
353 data = np.loadtxt(os.path.join(self.
path,
"nu_loss_gain.dat"), unpack=
True)
355 for i, col
in enumerate(columns):
358 with h5py.File(self.
filename,
'r')
as hf:
360 for key
in hf[
'nu_loss_gain'].keys():
361 self.
__nuloss[key] = hf[
'nu_loss_gain'][key][:]
363 error_msg =
'Failed to read nuloss in "'+str(self.
path)+
'". '+\
364 'Not present as Ascii nor as Hdf5!'
365 raise ValueError(error_msg)
377 with open(os.path.join(self.
path,
"timescales.dat"),
'r')
as f:
378 lines = f.readlines()
380 header = header.replace(
'#',
'').replace(
'[s]',
'').replace(
'[GK]',
'')
382 data = np.loadtxt(os.path.join(self.
path,
"timescales.dat"), unpack=
True)
383 for i, k
in enumerate(key):
387 with h5py.File(self.
filename,
'r')
as hf:
389 for key
in hf[
'timescales'].keys():
392 error_msg =
'Failed to read timescales in "'+str(self.
path)+
'". '+\
393 'Not present as Ascii nor as Hdf5!'
394 raise ValueError(error_msg)
400 Get an entry from the mainout
402 if not hasattr(self,
"_wreader__mainout"):
414 columns = [
'iteration',
'time',
'temp',
'dens',
'ye',
415 'rad',
'yn',
'yp',
'ya',
'ylight',
'yheavy',
416 'zbar',
'abar',
'entr']
417 data = np.loadtxt(os.path.join(self.
path,
"mainout.dat"), unpack=
True)
419 for i, col
in enumerate(columns):
422 with h5py.File(self.
filename,
'r')
as hf:
424 for key
in hf[
'mainout'].keys():
425 self.
__mainout[key] = hf[
'mainout'][key][:]
427 error_msg =
'Failed to read mainout in "'+str(self.
path)+
'". '+\
428 'Not present as Ascii nor as Hdf5!'
429 raise ValueError(error_msg)
437 if not hasattr(self,
"_wreader__energy"):
449 columns = [
'time',
'engen_tot',
'S_src',
'engen_beta',
'engen_ng_gn',
'engen_pg_gp',
450 'engen_ag_ga',
'engen_np_pn',
'engen_na_an',
'engen_ap_pa',
'engen_fiss']
451 data = np.loadtxt(os.path.join(self.
path,
"generated_energy.dat"), unpack=
True)
453 for i, col
in enumerate(columns):
456 with h5py.File(self.
filename,
'r')
as hf:
458 for key
in hf[
'energy'].keys():
459 self.
__energy[key] = hf[
'energy'][key][:]
461 error_msg =
'Failed to read energy in "'+str(self.
path)+
'". '+\
462 'Not present as Ascii nor as Hdf5!'
463 raise ValueError(error_msg)
468 Get the final abundances from the finab.dat file
470 if not hasattr(self,
"_wreader__finab"):
481 A,Z,N,Y,X = np.loadtxt(os.path.join(self.
path,
"finab.dat"), unpack=
True)
483 with h5py.File(self.
filename,
'r')
as hf:
484 A = hf[
'finab/finab/A'][:]
485 Z = hf[
'finab/finab/Z'][:]
487 Y = hf[
'finab/finab/Y'][:]
488 X = hf[
'finab/finab/X'][:]
490 error_msg =
'Failed to read finab in "'+str(self.
path)+
'". '+\
491 'Not present as Ascii nor as Hdf5!'
492 raise ValueError(error_msg)
493 self.
__finab = {
"A": A,
"Z": Z,
"N": N,
"Y": Y,
"X": X}
499 Get the final abundances from the finabsum.dat file
501 if not hasattr(self,
"_wreader__finabsum"):
507 Reader of the finabsum
512 A,Y,X = np.loadtxt(os.path.join(self.
path,
"finabsum.dat"), unpack=
True)
514 with h5py.File(self.
filename,
'r')
as hf:
515 A = hf[
'finab/finabsum/A'][:]
516 Y = hf[
'finab/finabsum/Y'][:]
517 X = hf[
'finab/finabsum/X'][:]
519 error_msg =
'Failed to read finabsum in "'+str(self.
path)+
'". '+\
520 'Not present as Ascii nor as Hdf5!'
521 raise ValueError(error_msg)
528 Get the final abundances from the finabelem.dat file
530 if not hasattr(self,
"_wreader__finabelem"):
536 Reader of the finabsum
541 Z, Y = np.loadtxt(os.path.join(self.
path,
"finabelem.dat"), unpack=
True)
543 with h5py.File(self.
filename,
'r')
as hf:
544 Z = hf[
'finab/finabelem/Z'][:]
545 Y = hf[
'finab/finabelem/Y'][:]
547 error_msg =
'Failed to read finabelem in "'+str(self.
path)+
'". '+\
548 'Not present as Ascii nor as Hdf5!'
549 raise ValueError(error_msg)
559 fname = os.path.join(self.
path,
"flow/flow_" + str(iteration).zfill(4) +
".dat")
560 with open(fname,
'r')
as f:
561 lines = f.readlines()
563 time, temp, dens = np.array(header.split()).astype(float)
566 nin,zin,yin,nout,zout,yout,flows = np.loadtxt(fname, unpack=
True, skiprows=3)
567 nin = nin.astype(int)
568 zin = zin.astype(int)
569 nout = nout.astype(int)
570 zout = zout.astype(int)
583 with h5py.File(self.
filename,
'r')
as hf:
585 for key
in hf[flow_group][str(iteration)].keys():
586 flow[key] = hf[flow_group][str(iteration)][key][:]
588 error_msg =
'Failed to read flow in "'+str(self.
path)+
'". '+\
589 'Not present as Ascii nor as Hdf5!'
590 raise ValueError(error_msg)
597 Get the value of a specific key.
601 elif key ==
"timescales":
603 elif key ==
"energy":
605 elif key ==
"tracked_nuclei":
607 elif key ==
"nuloss":
611 if __name__ ==
"__main__":
612 w =
wreader(
'/home/mreichert/data/Networks/comparison_winNet/WinNet-dev/runs/Example_MRSN_r_process_winteler')