5 from nucleus_multiple_class
import nucleus_multiple
13 Minimalistic class to lazily read WinNet data.
19 - path: Path to the WinNet data
28 self.
filename = os.path.join(path,
"WinNet_data.h5")
38 Check if the run has crashed
40 if not hasattr(self,
"_wreader__is_crashed"):
47 Read if the run has crashed
63 if not hasattr(self,
"_wreader__A"):
69 Read the mass number A
75 self.
__N, self.
__Z = np.loadtxt(os.path.join(self.
__snapshot_path,
"snapsh_0001.dat"),unpack=
True, usecols=(0,1),
79 with h5py.File(self.
filename,
'r')
as hf:
80 self.
__A = hf[
'snapshots/A'][:]
81 self.
__Z = hf[
'snapshots/Z'][:]
82 self.
__N = hf[
'snapshots/N'][:]
84 error_msg =
'Failed to read A, Z, and N in "'+str(self.
path)+
'". '+\
85 'Snapshots not present as Ascii nor as Hdf5!'
86 raise ValueError(error_msg)
93 if not hasattr(self,
"_wreader__Z"):
102 if not hasattr(self,
"_wreader__N"):
118 snapshot_files = [f
for f
in snapshot_files
if f.startswith(
'snapsh_')]
122 for i, f
in enumerate(tqdm(snapshot_files, desc=
'Reading snapshots', disable=self.
silent)):
123 fname =
'snapsh_' + str(i+1).zfill(4) +
'.dat'
125 lines = file.readlines()
127 for j, line
in enumerate(lines[3:]):
131 with h5py.File(self.
filename,
'r')
as hf:
136 error_msg =
'Failed to read timescales in "'+str(self.
path)+
'". '+\
137 'Not present as Ascii nor as Hdf5!'
138 raise ValueError(error_msg)
144 Check whether an entry is in the hdf5 format (return 1),
145 or in the ascii format (return 2), or does not exist (return 0)
147 if (entry ==
'mainout'):
149 elif (entry ==
'timescales'):
151 elif (entry ==
'energy'):
153 elif (entry ==
'tracked_nuclei'):
154 value = self.
__check_files(
'tracked_nuclei.dat',
'tracked_nuclei')
155 elif (entry ==
'snapshot'):
156 value = self.
__check_files(
'snaps/snapsh_0001.dat',
'snapshots')
157 elif (entry ==
'flows'):
159 elif (entry ==
"nuloss"):
160 value = self.
__check_files(
'nu_loss_gain.dat',
'nu_loss_gain')
161 elif (entry ==
'finab'):
163 elif (entry ==
'finabsum'):
165 elif (entry ==
'finabelem'):
168 error_msg =
'Checked for unknown entry "'+str(entry)+
'". '
169 raise ValueError(error_msg)
176 Check if something exists in hdf5 or ascii
179 if not os.path.exists(self.
filename):
180 if not os.path.exists(os.path.join(self.
path, ascii_file_path)):
185 with h5py.File(self.
filename,
'r')
as hf:
186 if hdf5_key
in hf.keys():
188 elif os.path.exists(os.path.join(self.
path, ascii_file_path)):
200 if not hasattr(self,
"_wreader__nr_of_snaps"):
206 Read the number of snapshots
213 snapshot_files = [f
for f
in snapshot_files
if f.startswith(
'snapsh_')]
216 with h5py.File(self.
filename,
'r')
as hf:
219 error_msg =
'Failed to read nr of snapshots in "'+str(self.
path)+
'". '+\
220 'Not present as Ascii nor as Hdf5!'
221 raise ValueError(error_msg)
226 Get the tracked nuclei
228 if not hasattr(self,
"_wreader__tracked_nuclei"):
234 Read the tracked nuclei
242 path = os.path.join(self.
path,
"tracked_nuclei.dat")
243 with open(path,
'r')
as f:
244 first_line = f.readline()
246 first_line = first_line.replace(
'Y(',
'')
247 first_line = first_line.replace(
')',
'')
248 first_line = first_line.replace(
'#',
'')
249 track_nuclei_names = first_line.split()[1:]
250 track_nuclei_data = np.loadtxt(path, skiprows=1)
255 Xnuclei = track_nuclei_data[:,1:]*A
262 self.
__tracked_nuclei[
'latex_names'] = [
r"$^{"+str(A[i])+
r"}$"+elnames[i].title()
for i
in range(len(A))]
263 for i, name
in enumerate(track_nuclei_names):
266 with h5py.File(self.
filename,
'r')
as hf:
274 for i, name
in enumerate(nm.names):
279 self.
__tracked_nuclei[
'latex_names'] = [
r"$^{"+str(A[i])+
r"}$"+elnames[i].title()
for i
in range(len(A))]
281 error_msg =
'Failed to read tracked nuclei in "'+str(self.
path)+
'". '+\
282 'Not present as Ascii nor as Hdf5!'
283 raise ValueError(error_msg)
288 Get abundance at snapshot idx
290 if not hasattr(self,
"_wreader__snapshots_time"):
297 Get mass fraction at snapshot idx
299 if not hasattr(self,
"_wreader__snapshots_time"):
306 Get time at snapshot idx
308 if not hasattr(self,
"_wreader__snapshots_time"):
315 Get the timescale of "tau", e.g., "tau_ag"
317 if not hasattr(self,
"_wreader__timescales"):
325 Get the neutrino losses and gains
327 if not hasattr(self,
"_wreader__nuloss"):
334 Read the neutrino losses and gains
340 columns = [
"time",
"temp",
"dens",
"rad",
"nu_total",
"nu_beta",
"nu_nuheat",
"nu_thermal"]
341 data = np.loadtxt(os.path.join(self.
path,
"nu_loss_gain.dat"), unpack=
True)
343 for i, col
in enumerate(columns):
346 with h5py.File(self.
filename,
'r')
as hf:
348 for key
in hf[
'nu_loss_gain'].keys():
349 self.
__nuloss[key] = hf[
'nu_loss_gain'][key][:]
351 error_msg =
'Failed to read nuloss in "'+str(self.
path)+
'". '+\
352 'Not present as Ascii nor as Hdf5!'
353 raise ValueError(error_msg)
365 with open(os.path.join(self.
path,
"timescales.dat"),
'r')
as f:
366 lines = f.readlines()
368 header = header.replace(
'#',
'').replace(
'[s]',
'').replace(
'[GK]',
'')
370 data = np.loadtxt(os.path.join(self.
path,
"timescales.dat"), unpack=
True)
371 for i, k
in enumerate(key):
375 with h5py.File(self.
filename,
'r')
as hf:
377 for key
in hf[
'timescales'].keys():
380 error_msg =
'Failed to read timescales in "'+str(self.
path)+
'". '+\
381 'Not present as Ascii nor as Hdf5!'
382 raise ValueError(error_msg)
388 Get an entry from the mainout
390 if not hasattr(self,
"_wreader__mainout"):
402 columns = [
'iteration',
'time',
'temp',
'dens',
'ye',
403 'rad',
'yn',
'yp',
'ya',
'ylight',
'yheavy',
404 'zbar',
'abar',
'entr']
405 data = np.loadtxt(os.path.join(self.
path,
"mainout.dat"), unpack=
True)
407 for i, col
in enumerate(columns):
410 with h5py.File(self.
filename,
'r')
as hf:
412 for key
in hf[
'mainout'].keys():
413 self.
__mainout[key] = hf[
'mainout'][key][:]
415 error_msg =
'Failed to read mainout in "'+str(self.
path)+
'". '+\
416 'Not present as Ascii nor as Hdf5!'
417 raise ValueError(error_msg)
425 if not hasattr(self,
"_wreader__energy"):
437 columns = [
'time',
'engen_tot',
'S_src',
'engen_beta',
'engen_ng_gn',
'engen_pg_gp',
438 'engen_ag_ga',
'engen_np_pn',
'engen_na_an',
'engen_ap_pa',
'engen_fiss']
439 data = np.loadtxt(os.path.join(self.
path,
"generated_energy.dat"), unpack=
True)
441 for i, col
in enumerate(columns):
444 with h5py.File(self.
filename,
'r')
as hf:
446 for key
in hf[
'energy'].keys():
447 self.
__energy[key] = hf[
'energy'][key][:]
449 error_msg =
'Failed to read energy in "'+str(self.
path)+
'". '+\
450 'Not present as Ascii nor as Hdf5!'
451 raise ValueError(error_msg)
456 Get the final abundances from the finab.dat file
458 if not hasattr(self,
"_wreader__finab"):
469 A,Z,N,Y,X = np.loadtxt(os.path.join(self.
path,
"finab.dat"), unpack=
True)
471 with h5py.File(self.
filename,
'r')
as hf:
472 A = hf[
'finab/finab/A'][:]
473 Z = hf[
'finab/finab/Z'][:]
475 Y = hf[
'finab/finab/Y'][:]
476 X = hf[
'finab/finab/X'][:]
478 error_msg =
'Failed to read finab in "'+str(self.
path)+
'". '+\
479 'Not present as Ascii nor as Hdf5!'
480 raise ValueError(error_msg)
481 self.
__finab = {
"A": A,
"Z": Z,
"N": N,
"Y": Y,
"X": X}
487 Get the final abundances from the finabsum.dat file
489 if not hasattr(self,
"_wreader__finabsum"):
495 Reader of the finabsum
500 A,Y,X = np.loadtxt(os.path.join(self.
path,
"finabsum.dat"), unpack=
True)
502 with h5py.File(self.
filename,
'r')
as hf:
503 A = hf[
'finab/finabsum/A'][:]
504 Y = hf[
'finab/finabsum/Y'][:]
505 X = hf[
'finab/finabsum/X'][:]
507 error_msg =
'Failed to read finabsum in "'+str(self.
path)+
'". '+\
508 'Not present as Ascii nor as Hdf5!'
509 raise ValueError(error_msg)
516 Get the final abundances from the finabelem.dat file
518 if not hasattr(self,
"_wreader__finabelem"):
524 Reader of the finabsum
529 Z, Y = np.loadtxt(os.path.join(self.
path,
"finabelem.dat"), unpack=
True)
531 with h5py.File(self.
filename,
'r')
as hf:
532 Z = hf[
'finab/finabelem/Z'][:]
533 Y = hf[
'finab/finabelem/Y'][:]
535 error_msg =
'Failed to read finabelem in "'+str(self.
path)+
'". '+\
536 'Not present as Ascii nor as Hdf5!'
537 raise ValueError(error_msg)
547 fname = os.path.join(self.
path,
"flow/flow_" + str(iteration).zfill(4) +
".dat")
548 with open(fname,
'r')
as f:
549 lines = f.readlines()
551 time, temp, dens = np.array(header.split()).astype(float)
554 nin,zin,yin,nout,zout,yout,flows = np.loadtxt(fname, unpack=
True, skiprows=3)
555 nin = nin.astype(int)
556 zin = zin.astype(int)
557 nout = nout.astype(int)
558 zout = zout.astype(int)
571 with h5py.File(self.
filename,
'r')
as hf:
573 for key
in hf[flow_group][str(iteration)].keys():
574 flow[key] = hf[flow_group][str(iteration)][key][:]
576 error_msg =
'Failed to read flow in "'+str(self.
path)+
'". '+\
577 'Not present as Ascii nor as Hdf5!'
578 raise ValueError(error_msg)
585 Get the value of a specific key.
589 elif key ==
"timescales":
591 elif key ==
"energy":
593 elif key ==
"tracked_nuclei":
595 elif key ==
"nuloss":
599 if __name__ ==
"__main__":
600 w =
wreader(
'/home/mreichert/data/Networks/comparison_winNet/WinNet-dev/runs/Example_MRSN_r_process_winteler')