11 class wreader(object):
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)):
186 with h5py.File(self.
filename,
'r')
as hf:
187 if hdf5_key
in hf.keys():
189 elif os.path.exists(os.path.join(self.
path, ascii_file_path)):
204 if not hasattr(self,
"_wreader__nr_of_snaps"):
210 Read the number of snapshots
217 snapshot_files = [f
for f
in snapshot_files
if f.startswith(
'snapsh_')]
220 with h5py.File(self.
filename,
'r')
as hf:
223 error_msg =
'Failed to read nr of snapshots in "'+str(self.
path)+
'". '+\
224 'Not present as Ascii nor as Hdf5!'
225 raise ValueError(error_msg)
230 Get the tracked nuclei
232 if not hasattr(self,
"_wreader__tracked_nuclei"):
238 Read the tracked nuclei
246 path = os.path.join(self.
path,
"tracked_nuclei.dat")
247 with open(path,
'r')
as f:
248 first_line = f.readline()
250 first_line = first_line.replace(
'Y(',
'')
251 first_line = first_line.replace(
')',
'')
252 first_line = first_line.replace(
'#',
'')
253 track_nuclei_names = first_line.split()[1:]
254 track_nuclei_data = np.loadtxt(path, skiprows=1)
259 Xnuclei = track_nuclei_data[:,1:]*A
266 self.
__tracked_nuclei[
'latex_names'] = [
r"$^{"+str(A[i])+
r"}$"+elnames[i].title()
for i
in range(len(A))]
267 for i, name
in enumerate(track_nuclei_names):
270 with h5py.File(self.
filename,
'r')
as hf:
278 for i, name
in enumerate(nm.names):
283 self.
__tracked_nuclei[
'latex_names'] = [
r"$^{"+str(A[i])+
r"}$"+elnames[i].title()
for i
in range(len(A))]
285 error_msg =
'Failed to read tracked nuclei in "'+str(self.
path)+
'". '+\
286 'Not present as Ascii nor as Hdf5!'
287 raise ValueError(error_msg)
292 Get abundance at snapshot idx
294 if not hasattr(self,
"_wreader__snapshots_time"):
301 Get mass fraction at snapshot idx
303 if not hasattr(self,
"_wreader__snapshots_time"):
310 Get time at snapshot idx
312 if not hasattr(self,
"_wreader__snapshots_time"):
319 Get the timescale of "tau", e.g., "tau_ag"
321 if not hasattr(self,
"_wreader__timescales"):
329 Get the neutrino losses and gains
331 if not hasattr(self,
"_wreader__nuloss"):
338 Read the neutrino losses and gains
344 columns = [
"time",
"temp",
"dens",
"rad",
"nu_total",
"nu_beta",
"nu_nuheat",
"nu_thermal"]
345 data = np.loadtxt(os.path.join(self.
path,
"nu_loss_gain.dat"), unpack=
True)
347 for i, col
in enumerate(columns):
350 with h5py.File(self.
filename,
'r')
as hf:
352 for key
in hf[
'nu_loss_gain'].keys():
353 self.
__nuloss[key] = hf[
'nu_loss_gain'][key][:]
355 error_msg =
'Failed to read nuloss in "'+str(self.
path)+
'". '+\
356 'Not present as Ascii nor as Hdf5!'
357 raise ValueError(error_msg)
369 with open(os.path.join(self.
path,
"timescales.dat"),
'r')
as f:
370 lines = f.readlines()
372 header = header.replace(
'#',
'').replace(
'[s]',
'').replace(
'[GK]',
'')
374 data = np.loadtxt(os.path.join(self.
path,
"timescales.dat"), unpack=
True)
375 for i, k
in enumerate(key):
379 with h5py.File(self.
filename,
'r')
as hf:
381 for key
in hf[
'timescales'].keys():
384 error_msg =
'Failed to read timescales in "'+str(self.
path)+
'". '+\
385 'Not present as Ascii nor as Hdf5!'
386 raise ValueError(error_msg)
392 Get an entry from the mainout
394 if not hasattr(self,
"_wreader__mainout"):
406 columns = [
'iteration',
'time',
'temp',
'dens',
'ye',
407 'rad',
'yn',
'yp',
'ya',
'ylight',
'yheavy',
408 'zbar',
'abar',
'entr']
409 data = np.loadtxt(os.path.join(self.
path,
"mainout.dat"), unpack=
True)
411 for i, col
in enumerate(columns):
414 with h5py.File(self.
filename,
'r')
as hf:
416 for key
in hf[
'mainout'].keys():
417 self.
__mainout[key] = hf[
'mainout'][key][:]
419 error_msg =
'Failed to read mainout in "'+str(self.
path)+
'". '+\
420 'Not present as Ascii nor as Hdf5!'
421 raise ValueError(error_msg)
429 if not hasattr(self,
"_wreader__energy"):
441 columns = [
'time',
'engen_tot',
'S_src',
'engen_beta',
'engen_ng_gn',
'engen_pg_gp',
442 'engen_ag_ga',
'engen_np_pn',
'engen_na_an',
'engen_ap_pa',
'engen_fiss']
443 data = np.loadtxt(os.path.join(self.
path,
"generated_energy.dat"), unpack=
True)
445 for i, col
in enumerate(columns):
448 with h5py.File(self.
filename,
'r')
as hf:
450 for key
in hf[
'energy'].keys():
451 self.
__energy[key] = hf[
'energy'][key][:]
453 error_msg =
'Failed to read energy in "'+str(self.
path)+
'". '+\
454 'Not present as Ascii nor as Hdf5!'
455 raise ValueError(error_msg)
460 Get the final abundances from the finab.dat file
462 if not hasattr(self,
"_wreader__finab"):
473 A,Z,N,Y,X = np.loadtxt(os.path.join(self.
path,
"finab.dat"), unpack=
True)
475 with h5py.File(self.
filename,
'r')
as hf:
476 A = hf[
'finab/finab/A'][:]
477 Z = hf[
'finab/finab/Z'][:]
479 Y = hf[
'finab/finab/Y'][:]
480 X = hf[
'finab/finab/X'][:]
482 error_msg =
'Failed to read finab in "'+str(self.
path)+
'". '+\
483 'Not present as Ascii nor as Hdf5!'
484 raise ValueError(error_msg)
485 self.
__finab = {
"A": A,
"Z": Z,
"N": N,
"Y": Y,
"X": X}
491 Get the final abundances from the finabsum.dat file
493 if not hasattr(self,
"_wreader__finabsum"):
499 Reader of the finabsum
504 A,Y,X = np.loadtxt(os.path.join(self.
path,
"finabsum.dat"), unpack=
True)
506 with h5py.File(self.
filename,
'r')
as hf:
507 A = hf[
'finab/finabsum/A'][:]
508 Y = hf[
'finab/finabsum/Y'][:]
509 X = hf[
'finab/finabsum/X'][:]
511 error_msg =
'Failed to read finabsum in "'+str(self.
path)+
'". '+\
512 'Not present as Ascii nor as Hdf5!'
513 raise ValueError(error_msg)
520 Get the final abundances from the finabelem.dat file
522 if not hasattr(self,
"_wreader__finabelem"):
531 path = os.path.join(self.
path,
"OUT")
535 if os.path.isfile(path):
536 with open(path,
"r")
as f:
537 lines = f.readlines()
539 for ind,l
in enumerate(lines[::-1]):
541 if "iterations" in l:
542 self.
__out_data[
"num_iterations"] =int(l.split()[4])
543 elif "Elapsed time:" in l:
544 self.
__out_data[
"elapsed_time"] = float(l.split()[3])
545 elif "Final time:" in l:
546 self.
__out_data[
"final_time"] = float(l.split()[-1])
547 elif "Expansion velocity" in l:
548 self.
__out_data[
"exp_velocity"] = float(l.split()[-2])
549 elif l.strip() ==
"============================":
552 self.
__out_data[
"final_entropy"] = float(l.split()[-1])
553 self.
__out_data[
"final_density"] = float(l.split()[-2])
554 self.
__out_data[
"final_temperature"] = float(l.split()[-3])
562 Get the data from the OUT file
564 if not hasattr(self,
"_wreader__out_data"):
571 Reader of the finabsum
576 Z, Y = np.loadtxt(os.path.join(self.
path,
"finabelem.dat"), unpack=
True)
578 with h5py.File(self.
filename,
'r')
as hf:
579 Z = hf[
'finab/finabelem/Z'][:]
580 Y = hf[
'finab/finabelem/Y'][:]
582 error_msg =
'Failed to read finabelem in "'+str(self.
path)+
'". '+\
583 'Not present as Ascii nor as Hdf5!'
584 raise ValueError(error_msg)
594 fname = os.path.join(self.
path,
"flow/flow_" + str(iteration).zfill(4) +
".dat")
595 with open(fname,
'r')
as f:
596 lines = f.readlines()
598 time, temp, dens = np.array(header.split()).astype(float)
601 nin,zin,yin,nout,zout,yout,flows = np.loadtxt(fname, unpack=
True, skiprows=3)
602 nin = nin.astype(int)
603 zin = zin.astype(int)
604 nout = nout.astype(int)
605 zout = zout.astype(int)
618 with h5py.File(self.
filename,
'r')
as hf:
620 for key
in hf[flow_group][str(iteration)].keys():
621 flow[key] = hf[flow_group][str(iteration)][key][:]
623 error_msg =
'Failed to read flow in "'+str(self.
path)+
'". '+\
624 'Not present as Ascii nor as Hdf5!'
625 raise ValueError(error_msg)
632 Get the value of a specific key.
636 elif key ==
"timescales":
638 elif key ==
"energy":
640 elif key ==
"tracked_nuclei":
642 elif key ==
"nuloss":
647 if __name__ ==
"__main__":
648 w =
wreader(
'/home/mreichert/data/Networks/comparison_winNet/WinNet-dev/runs/Example_MRSN_r_process_winteler')