-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_yields.py
1439 lines (1234 loc) · 52.1 KB
/
read_yields.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import print_function
'''
Superclass to extract yield data from tables
and from mppnp simulations
Christian Ritter 11/2013
Two classes: One for reading and extracting of
NuGrid table data, the other one for SN1a data.
'''
import numpy as np
import os
color = ['r','k','b','g']
marker_type = ['o','p','s','D']
line_style = ['--','-','-.',':']
# global notebookmode
notebookmode = False
class read_nugrid_parameter():
def __init__(self,nugridtable):
'''
dir : specifing the filename of the table file
'''
table=nugridtable
import os
if '/' in table:
self.label=table.split('/')[-1]
else:
self.label=table
self.path=table
file1=open(nugridtable)
lines=file1.readlines()
file1.close()
header1=[]
table_header=[]
yield_data=[]
header_done=False
ignore=False
col_attrs_data=[]
######read through all lines
for line in lines:
if 'H' in line[0]:
if not 'Table:' in line:
if header_done==False:
header1.append(line.strip())
else:
table_header[-1].append(line.strip())
else:
ignore=False
#print (line,'ignore',ignore)
if ignore==True:
header_done=True
continue
table_header.append([])
table_header[-1].append(line.strip())
yield_data.append([])
#lum_bands.append([])
#m_final.append([])
col_attrs_data.append([])
col_attrs_data[-1].append(line.strip())
header_done=True
continue
if ignore==True:
continue
if header_done==True:
#col_attrs_data.append([])
col_attrs_data[-1].append(float(line.split(':')[1]))
continue
if ignore==True:
continue
if '&Age' in line:
title_line=line.split('&')[1:]
column_titles=[]
for t in title_line:
yield_data[-1].append([])
column_titles.append(t.strip())
#print (column_titles)
continue
#iso ,name and yields
iso_name=line.split('&')[1].strip()
#print (line)
#print (line.split('&'))
yield_data[-1][0].append(float(line.split('&')[1].strip()))
#if len(isotopes)>0:
# if not iso_name in isotopes:
#else:
yield_data[-1][1].append(float(line.split('&')[2].strip()))
# for additional data
for t in range(2,len(yield_data[-1])):
yield_data[-1][t].append(float(line.split('&')[t+1].strip()))
#choose only isotoopes and right order
######reading finished
#In [43]: tablesN.col_attrs
#Out[43]: ['Isotopes', 'Yields', 'X0', 'Z', 'A']
self.yield_data=yield_data
#table header points to element in yield_data
self.table_idx={}
i=0
self.col_attrs=[]
self.table_mz=[]
self.metallicities=[]
#self.col_attrs=table_header
#go through all MZ pairs
for table1 in table_header:
#go through col_attrs
for k in range(len(table1)):
table1[k]=table1[k][2:]
if 'Table' in table1[k]:
self.table_idx[table1[k].split(':')[1].strip()]=i
tablename=table1[k].split(':')[1].strip()
self.table_mz.append(tablename)
metal=tablename.split(',')[1].split('=')[1][:-1]
if float(metal) not in self.metallicities:
self.metallicities.append(float(metal))
if table1 ==table_header[0]:
if 'Table' in table1[k]:
table1[k] = 'Table (M,Z):'
self.col_attrs.append(table1[k].split(':')[0].strip())
i+=1
#define header
self.header_attrs={}
#print ('header1: ',header1)
for h in header1:
self.header_attrs[h.split(':')[0][1:].strip()]=h.split(':')[1].strip()
self.data_cols=column_titles #previous data_attrs
#self.kin_e=kin_e
#self.lum_bands=lum_bands
#self.m_final=m_final
self.col_attrs_data=col_attrs_data
def get(self,M=0,Z=-1,quantity=''):
'''
Allows to extract table data in 2 Modes:
1) For extracting of table data for
star of mass M and metallicity Z.
Returns either table attributes,
given by yield.col_attrs
or table columns,
given by yield.data_cols.
2) For extraction of a table attribute
from all available tables. Can be
directly used in the following way:
get(tableattribute)
M: Stellar mass in Msun
Z: Stellar metallicity (e.g. Z=0.02)
quantity: table attribute or data column/data_cols
'''
all_tattrs=False
if Z ==-1:
if M ==0 and len(quantity)>0:
quantity1=quantity
all_tattrs=True
elif (M in self.col_attrs) and quantity == '':
quantity1=M
all_tattrs=True
else:
print ('Error: Wrong input')
return 0
quantity=quantity1
if (all_tattrs==False) and (not M ==0):
inp='(M='+str(float(M))+',Z='+str(float(Z))+')'
idx=self.table_idx[inp]
if quantity in self.col_attrs:
if all_tattrs==False:
data=self.col_attrs_data[idx][self.col_attrs.index(quantity)]
return data
else:
data=[]
for k in range(len(self.table_idx)):
data.append(self.col_attrs_data[k][self.col_attrs.index(quantity)])
return data
if quantity=='masses':
data_tables=self.table_mz
masses=[]
for table in data_tables:
if str(float(Z)) in table:
masses.append(float(table.split(',')[0].split('=')[1]))
return masses
else:
data=self.yield_data[idx]
idx_col=self.data_cols.index(quantity)
set1=data[idx_col]
return set1
def add_parameter_write_table(self,table_header='',dcols=[],data=[[]],filename='isotope_yield_table_MESA_only_param_new.txt'):
'''
Allows to add more parameter to the parameter table.
dcols=['Test1'], data=[[....]]
'''
import ascii_table as ascii1
tables=self.table_mz
yield_data=self.yield_data
data_cols=self.data_cols
col_attrs=self.col_attrs
col_attrs_data1=self.col_attrs_data
for k in range(len(tables)):
if not tables[k]==table_header:
continue
mass=float(tables[k].split(',')[0].split('=')[1])
metallicity=float(tables[k].split(',')[1].split('=')[1][:-1])
#read out existing data
col_attrs=col_attrs #MZ pairs
col_attrs_data=col_attrs_data1[k]
#over col attrs, first is MZ pair which will be skipped, see special_header
attr_lines=[]
for h in range(1,len(col_attrs)):
attr=col_attrs[h]
idx=col_attrs.index(attr)
# over MZ pairs
attr_data=col_attrs_data[k][idx]
line=attr+': '+'{:.3E}'.format(attr_data)
attr_lines.append(line)
#read in available columns
data_new=yield_data[k]
dcols_new=data_cols[:]
#add more data...
for h in range(len(dcols)):
print ('h :',h)
data_new.append(data[h])
dcols_new.append(dcols[h])
dcols_new=[dcols_new[0]]+dcols_new[2:]+[dcols_new[1]]
print ('dcols: ',dcols_new)
special_header='Table: (M='+str(mass)+',Z='+str(metallicity)+')'
headers=[special_header]+attr_lines
ascii1.writeGCE_table_parameter(filename=filename,headers=headers,data=data_new,dcols=dcols_new)
class read_nugrid_yields():
def __init__(self,nugridtable,isotopes=[],excludemass=[]):
'''
dir : specifing the filename of the table file
'''
table=nugridtable
import os
if '/' in table:
self.label=table.split('/')[-1]
else:
self.label=table
self.path=table
if notebookmode==True:
os.system('sudo python cp.py '+nugridtable)
file1=open('tmp/'+nugridtable)
lines=file1.readlines()
file1.close()
os.system('sudo python delete.py '+nugridtable)
else:
file1=open(nugridtable)
lines=file1.readlines()
file1.close()
header1=[]
table_header=[]
age=[]
yield_data=[]
#kin_e=[]
#lum_bands=[]
#m_final=[]
header_done=False
ignore=False
col_attrs_data=[]
######read through all lines
for line in lines:
if 'H' in line[0]:
if not 'Table' in line:
if header_done==False:
header1.append(line.strip())
else:
table_header[-1].append(line.strip())
else:
ignore=False
for kk in range(len(excludemass)):
if float(excludemass[kk]) == float(line.split(',')[0].split('=')[1]):
ignore=True
#print ('ignore',float(line.split(',')[0].split('=')[1]))
break
#print (line,'ignore',ignore)
if ignore==True:
header_done=True
continue
table_header.append([])
table_header[-1].append(line.strip())
yield_data.append([])
#lum_bands.append([])
#m_final.append([])
col_attrs_data.append([])
col_attrs_data[-1].append(line.strip())
header_done=True
continue
if ignore==True:
continue
if header_done==True:
#col_attrs_data.append([])
col_attrs_data[-1].append(float(line.split(':')[1]))
#age is special col_attrs, used in chem_evol.py
if 'Lifetime' in line:
age.append(float(line.split(':')[1]))
'''
if 'kinetic energy' in line:
kin_e.append(float(line.split(':')[1]))
if 'band' in line:
lum_bands[-1].append(float(line.split(':')[1]))
if 'Mfinal' in line:
m_final[-1].append(float(line.split(':')[1]))
'''
continue
if ignore==True:
continue
if '&Yields' in line:
title_line=line.split('&')[1:]
column_titles=[]
for t in title_line:
yield_data[-1].append([])
column_titles.append(t.strip())
#print (column_titles)
continue
#iso ,name and yields
iso_name=line.split('&')[1].strip()
#print (line)
#print (line.split('&'))
yield_data[-1][0].append(line.split('&')[1].strip())
#if len(isotopes)>0:
# if not iso_name in isotopes:
#else:
yield_data[-1][1].append(float(line.split('&')[2].strip()))
# for additional data
for t in range(2,len(yield_data[-1])):
if column_titles[t] == 'A' or column_titles[t] =='Z':
yield_data[-1][t].append(int(line.split('&')[t+1].strip()))
else:
yield_data[-1][t].append(float(line.split('&')[t+1].strip()))
#choose only isotoopes and right order
######reading finished
#In [43]: tablesN.col_attrs
#Out[43]: ['Isotopes', 'Yields', 'X0', 'Z', 'A']
if len(isotopes)>0:
#print ('correct for isotopes')
data_new=[]
for k in range(len(yield_data)):
#print ('k')
data_new.append([])
#print ('len',len(yield_data[k]))
#print (([[]]*len(yield_data[k]))[0])
for h in range(len(yield_data[k])):
data_new[-1].append([])
#print ('testaa',data_new[-1])
data_all=yield_data[k]
for iso_name in isotopes:
if iso_name in data_all[0]:
#print ('test',data_all[1][data_all[0].index(iso_name)])
for hh in range(1,len(data_all)):
data_new[-1][hh].append(data_all[hh][data_all[0].index(iso_name)])
#data_new[-1][1].append(data_all[2][data_all[0].index(iso_name)])
#data_new[-1][1].append(data_all[2][data_all[0].index(iso_name)])
else:
for hh in range(1,len(data_all)):
data_new[-1][hh].append(0)
#data_new[-1][1].append(0)
#print ('GRID exclude',iso_name)
data_new[-1][0].append(iso_name)
yield_data=data_new
self.yield_data=yield_data
#table header points to element in yield_data
self.table_idx={}
i=0
self.col_attrs=[]
self.table_mz=[]
self.metallicities=[]
#self.col_attrs=table_header
#go through all MZ pairs
for table1 in table_header:
#go through col_attrs
for k in range(len(table1)):
table1[k]=table1[k][2:]
if 'Table' in table1[k]:
self.table_idx[table1[k].split(':')[1].strip()]=i
tablename=table1[k].split(':')[1].strip()
self.table_mz.append(tablename)
metal=tablename.split(',')[1].split('=')[1][:-1]
if float(metal) not in self.metallicities:
self.metallicities.append(float(metal))
if table1 ==table_header[0]:
if 'Table' in table1[k]:
table1[k] = 'Table (M,Z):'
self.col_attrs.append(table1[k].split(':')[0].strip())
#col_attrs_data
#table1.split(':')[1].strip()
i+=1
#define header
self.header_attrs={}
for h in header1:
self.header_attrs[h.split(':')[0][1:].strip()]=h.split(':')[1].strip()
self.data_cols=column_titles #previous data_attrs
self.age=age
#self.kin_e=kin_e
#self.lum_bands=lum_bands
#self.m_final=m_final
self.col_attrs_data=col_attrs_data
def set_col_attrs(self,M=0,Z=-1,quantity='',value=0):
'''
adds quantity with value to header of yield table with mass M and metallicity Z
Note: creates for all tables the same quantity with value 0.
if quantity is already available replace current value with new value
quantites given by col_attrs
'''
inp='(M='+str(float(M))+',Z='+str(float(Z))+')'
idx=self.table_idx[inp]
if quantity in self.col_attrs:
#quantity exists and will be overwritten
idxq=self.col_attrs.index(quantity)
self.col_attrs_data[idx][idxq]=value
else:
#create new entry
self.col_attrs.append(quantity)
#add for each table zero value
for k in range(len(self.col_attrs_data)):
if k==idx:
newval=value
else:
newval=0.
self.col_attrs_data[k].append(newval)
def set(self,M=0,Z=-1,specie='',value=0):
'''
Replace the values in column 3 which
are usually the yields with value.
Use in combination with the write routine
to write out modification into new file.
M: initial mass to be modified
Z: initial Z to
specie: quantity (e.g. yield) of specie will be modified
'''
inp='(M='+str(float(M))+',Z='+str(float(Z))+')'
idx=self.table_idx[inp]
data=self.yield_data[idx]
idx_col=self.data_cols.index('Yields')
set1=self.yield_data[idx][idx_col]
specie_all= data[0]
for k in range(len(set1)):
if specie == specie_all[k]:
#return set1[k]
self.yield_data[idx][idx_col][k] = value
def write_table(self,filename='isotope_yield_table_mod.txt',iolevel=0):
'''
Allows to write out table in NuGrid yield table format.
Note that method has to be generalized for all tables
and lines about NuGrid removed.
fname: Table name
needs ascii_table.py from NuGrid python tools
'''
import getpass
user=getpass.getuser()
import time
date=time.strftime("%d %b %Y", time.localtime())
tables=self.table_mz
#write header attrs
f=open(filename,'w')
self.header_attrs
out=''
l='H Name: '+self.header_attrs['Name']+'\n'
out = out +l
l='H Data prepared by: '+user+'\n'
out=out +l
l='H Data prepared date: '+date+'\n'
out=out +l
l='H Isotopes: '+ self.header_attrs['Isotopes'] +'\n'
out = out +l
l='H Number of metallicities: '+self.header_attrs['Number of metallicities']+'\n'
out = out +l
l='H Units: ' + self.header_attrs['Units'] + '\n'
out = out + l
f.write(out)
f.close()
for k in range(len(tables)):
if iolevel>0:
print ('Write table ',tables[k])
mass=float(self.table_mz[k].split(',')[0].split('=')[1])
metallicity=float(self.table_mz[k].split(',')[1].split('=')[1][:-1])
data=self.yield_data[k]
#search data_cols
idx_y=self.data_cols.index('Yields')
yields=data[idx_y]
idx_x0=self.data_cols.index('X0')
mass_frac_ini=data[idx_x0]
idx_specie=self.data_cols.index(self.data_cols[0])
species=data[idx_specie]
#over col attrs, first is MZ pair which will be skipped, see special_header
attr_lines=[]
for h in range(1,len(self.col_attrs)):
attr=self.col_attrs[h]
idx=self.col_attrs.index(attr)
# over MZ pairs
attr_data=self.col_attrs_data[k][idx]
line=attr+': '+'{:.3E}'.format(attr_data)
attr_lines.append(line)
special_header='Table: (M='+str(mass)+',Z='+str(metallicity)+')'
dcols=[self.data_cols[0],'Yields','X0']
data=[species,list(yields),mass_frac_ini]
headers=[special_header]+attr_lines
write_single_table(filename=filename,headers=headers,data=data,dcols=dcols)
print ('Yields table ',filename,' created.')
def get(self,M=0.,Z=-1.,quantity='',specie=''):
'''
Allows to extract table data in 2 Modes:
1) For extracting of table data for
star of mass M and metallicity Z.
Returns either table attributes,
given by yield.col_attrs
or table columns,
given by yield.data_cols.
2) For extraction of a table attribute
from all available tables. Can be
directly used in the following way:
get(tableattribute)
Parameters
----------
M: float
Stellar mass in Msun
default: 0
Z: float
Stellar metallicity (e.g. 0.02)
quantity: string
table attribute or data column/data_cols
specie: string
optional, return certain specie (e.g. 'H-1')
table1.get(Z=0.02,quantity='masses')
Examples
----------
>>> table1.get(M=2.0,Z=0.02,quantity='Yields')
>>> table1.get(Z=0.02,quantity='masses')
'''
#scale down to Z=0.00001
#print ('get yields ',Z)
#if float(Z) == 0.00001:
# #scale abundance
# if quantity=='Yields':
# return self.get_scaled_Z(M=M,Z=Z,quantity=quantity,specie=specie)
#Take all other parameter from Z=0.0001 case
# else:
# Z=0.0001
all_tattrs=False
if Z ==-1:
if M ==0 and len(quantity)>0:
quantity1=quantity
all_tattrs=True
elif (M in self.col_attrs) and quantity == '':
quantity1=M
all_tattrs=True
else:
print ('Error: Wrong input')
return 0
quantity=quantity1
if (all_tattrs==False) and (not M ==0):
inp='(M='+str(float(M))+',Z='+str(float(Z))+')'
idx=self.table_idx[inp]
#print ('len tableidx:',len(self.table_idx))
#print ('len age',len(self.age))
'''
if quantity=='Lifetime':
if all_tattrs==True:
data=self.age
else:
data=self.age[idx]
return data
if quantity =='Total kinetic energy':
if all_tattrs==True:
data=self.kin_e
else:
data=self.kin_e[idx]
return data
if quantity == 'Lyman-Werner band':
if all_tattrs==True:
data=[list(i) for i in zip(*self.lum_bands)][0]
else:
data=self.lum_bands[idx][0]
return data
if quantity== 'Hydrogen-ionizing band':
if all_tattrs==True:
data=[list(i) for i in zip(*self.lum_bands)][1]
else:
data=self.lum_bands[idx][1]
return data
if quantity == 'High-energy band':
if all_tattrs==True:
data=[list(i) for i in zip(*self.lum_bands)][2]
else:
data=self.lum_bands[idx][2]
return data
if quantity == 'Mfinal':
if all_tattrs==True:
data=self.m_final
else:
data=self.m_final[idx][0]
return data
if quantity== 'Table (M,Z)':
if all_tattrs==True:
data=self.table_mz
else:
data=self.table_mz[idx]
return data
'''
if quantity in self.col_attrs:
if all_tattrs==False:
data=self.col_attrs_data[idx][self.col_attrs.index(quantity)]
return data
else:
data=[]
for k in range(len(self.table_idx)):
data.append(self.col_attrs_data[k][self.col_attrs.index(quantity)])
return data
if quantity=='masses':
data_tables=self.table_mz
masses=[]
for table in data_tables:
if str(float(Z)) in table:
masses.append(float(table.split(',')[0].split('=')[1]))
return masses
else:
data=self.yield_data[idx]
if specie=='':
idx_col=self.data_cols.index(quantity)
set1=data[idx_col]
return set1
else:
idx_col=self.data_cols.index('Yields')
set1=data[idx_col]
specie_all= data[0]
for k in range(len(set1)):
if specie == specie_all[k]: #bug was here
return set1[k]
def get_scaled_Z(self,table, table_yields,iniabu,iniabu_scale,M=0,Z=0,quantity='Yields',specie=''):
'''
Scaled down yields of isotopes 'He','C', 'O', 'Mg', 'Ca', 'Ti', 'Fe', 'Co','Zn','H','N'
down to Z=1e-5 and Z=1e-6 (for Brian). The rest is set to zero.
'''
#print ('####################################')
#print ('Enter routine get_scaled_Z')
elem_prim=['He','C', 'O', 'Mg', 'Ca', 'Ti', 'Fe', 'Co','Zn','H']
elem_sec=['N']
##Scale down
import re
iniiso=[]
iniabu_massfrac=[]
for k in range(len(iniabu.habu)):
iso=iniabu.habu.keys()[k]
iniiso.append(re.split('(\d+)',iso)[0].strip().capitalize()+'-'+re.split('(\d+)',iso)[1])
iniabu_massfrac.append(iniabu.habu.values()[k])
iniiso_scale=[]
iniabu_scale_massfrac=[]
for k in range(len(iniabu_scale.habu)):
iso=iniabu_scale.habu.keys()[k]
iniiso_scale.append(re.split('(\d+)',iso)[0].strip().capitalize()+'-'+re.split('(\d+)',iso)[1])
iniabu_scale_massfrac.append(iniabu_scale.habu.values()[k])
grid_yields=[]
grid_masses=[]
isotope_names=[]
origin_yields=[]
for k in range(len(table.table_mz)):
if 'Z=0.0001' in table.table_mz[k]:
#print (table.table_mz[k])
mini=float(table.table_mz[k].split('=')[1].split(',')[0])
grid_masses.append(mini)
#this is production factor (see file name)
prodfac=table.get(M=mini,Z=0.0001,quantity='Yields')
isotopes=table.get(M=mini,Z=0.0001,quantity='Isotopes')
#this is yields
yields=table_yields.get(M=mini,Z=0.0001,quantity='Yields')
mtot_eject=sum(yields)
origin_yields.append([])
#print ('tot eject',mtot_eject)
mout=[]
sumnonh=0
isotope_names.append([])
for h in range(len(isotopes)):
if not (isotopes[h].split('-')[0] in (elem_prim+elem_sec) ):
#Isotopes/elements not considered/scaled are set to 0
#mout.append(0)
#isotope_names[-1].append(isotopes[h])
continue
isotope_names[-1].append(isotopes[h])
idx=iniiso.index(isotopes[h])
inix=iniabu_massfrac[idx]
idx=iniiso_scale.index(isotopes[h])
inix_scale=iniabu_scale_massfrac[idx]
prodf=prodfac[isotopes.index(isotopes[h])]
origin_yields[-1].append(yields[isotopes.index(isotopes[h])])
if isotopes[h].split('-')[0] in elem_prim:
#primary
mout1=(prodf-1.)*(inix_scale*mtot_eject) + (inix*mtot_eject)
#check if amount destroyed was more than it was initial there
if mout1<0:
#print ('Problem with ',isotopes[h])
#print ('Was more destroyed than evailable')
#Then only what was there can be destroyed
mout1=0
#if isotopes[h] == 'C-13':
# print ('inix',inix)
# print ('inixscale',inix_scale)
# print ('prodf',prodf)
# print ((prodf)*(inix_scale*mtot_eject))
# print ((inix*mtot_eject))
else:
#secondary
mout1=(prodf-1.)*(inix*mtot_eject) + (inix*mtot_eject)
if (not isotopes[h]) == 'H-1' and (mout1>0):
sumnonh+= (mout1 - (inix*mtot_eject))
mout.append(mout1)
#for mass conservation, assume total mass lost is same as in case of Z=0.0001
idx_h=isotope_names[-1].index('H-1')
mout[idx_h]-=sumnonh
for k in range(len(mout)):
mout[k] = float('{:.3E}'.format(mout[k]))
grid_yields.append(mout)
####data
idx=grid_masses.index(M)
all_tattrs=False
if specie=='':
return grid_yields[idx]
else:
set1=data[idx]
names=isotope_names[idx]
for k in range(len(names)):
if specie in names[k]:
return set1[k]
class read_yield_sn1a_tables():
def __init__(self,sn1a_table,isotopes=[]):
'''
Read SN1a tables.
Fills up missing isotope yields
with zeros.
If different Zs are available
do ...
'''
import re
if notebookmode==True:
os.system('sudo python cp.py '+sn1a_table)
f1=open('tmp/'+sn1a_table)
lines=f1.readlines()
f1.close()
os.system('sudo python delete.py '+sn1a_table)
else:
f1=open(sn1a_table)
lines=f1.readlines()
f1.close()
iso=[]
self.header=[]
self.col_attrs=[]
yields=[]
metallicities=[]
isotopes_avail=[]
for line in lines:
#for header
if 'H' in line[0]:
self.header.append(line)
continue
if ('Isotopes' in line) or ('Elements' in line):
l=line.replace('\n','').split('&')[1:]
self.col_attrs=l
metallicities=l[1:]
#print (metallicities)
# metallicity dependent yields
#if len(l)>2:
#else:
for k in l[1:]:
yields.append([])
continue
linesp=line.strip().split('&')[1:]
iso.append(linesp[0].strip())
#print (iso)
for k in range(1,len(linesp)):
yields[k-1].append(float(linesp[k]))
#if isotope list emtpy take all isotopes
if len(isotopes)==0:
isotopes=iso
yields1=[]
#fill up the missing isotope yields with zero
for z in range(len(yields)):
yields1.append([])
for iso1 in isotopes:
#iso1=iso1.split('-')[1]+iso1.split('-')[0]
#ison= iso1+((10-len(iso1))*' ')
if iso1 in iso:
yields1[-1].append(yields[z][iso.index(iso1)])
else:
yields1[-1].append(0.)
self.yields=yields1
self.metallicities=[]
for m in metallicities:
self.metallicities.append(float(m.split('=')[1]))
#self.metallicities=metallicities
#print (yields1)
self.isotopes=iso
def get(self,Z=0,quantity='Yields',specie=''):
'''
Allows to extract SN1a table data.
If metallicity dependent yield tables
were used, data is taken for the closest metallicity available
to reach given Z
quantity: if 'Yields' return yields
if 'Isotopes' return all isotopes available
Default: 'Yields'
specie: specie in yield table. Only with quantity='Yields'
'''
if quantity=='Yields':
if Z not in self.metallicities:
idx = (np.abs(np.array(self.metallicities)-Z)).argmin()
print ('use closest Z=',self.metallicities[idx])
else:
idx = self.metallicities.index(Z)
yields=self.yields[idx]
if len(specie)==0:
return np.array(yields)
else:
if specie in self.isotopes:
idx=self.isotopes.index(specie)
return yields[idx]
else:
print ('Specie not available')
elif quantity=='Isotopes':
return self.isotopes
class read_yield_rawd_tables():
def __init__(self,rawd_table,isotopes):
'''
Read RAWD tables.
Fills up missing isotope yields
with zeros.
If different Zs are available
do ...
'''
import re
if notebookmode==True:
os.system('sudo python cp.py '+rawd_table)
f1=open('tmp/'+rawd_table)
lines=f1.readlines()
f1.close()
os.system('sudo python delete.py '+rawd_table)
else:
f1=open(rawd_table)
lines=f1.readlines()
f1.close()
iso=[]
self.header=[]
self.col_attrs=[]
yields=[]
metallicities=[]
for line in lines:
#for header
if 'H' in line[0]:
self.header.append(line)
continue
if ('Isotopes' in line) or ('Elements' in line):
l=line.replace('\n','').split('&')[1:]
self.col_attrs=l
metallicities=l[1:]
#print (metallicities)
# metallicity dependent yields
#if len(l)>2:
#else:
for k in l[1:]:
yields.append([])
continue
linesp=line.strip().split('&')[1:]
iso.append(linesp[0].strip())
#print (iso)
for k in range(1,len(linesp)):
yields[k-1].append(float(linesp[k]))
yields1=[]
#fill up the missing isotope yields with zero
for z in range(len(yields)):