def write_GCE_input_fxt(self,input_file='maltov1.orig',output_file='maltov1.new',first_change=True,label_masses='set1.1',cycles=20*[[0,-1,500]],exp_dir='',delay=False): ''' Reads input_file of input for GCE code of fxt and replaces data with available data from the simulations. Also creates a file *output_file*_differences to keep track of the replacements. latter not yet ''' import re f1=open(input_file) lines=f1.readlines() f1.close() isotopes=[] isotopes_out=[] iso_line_idx=[] i=0 ##########read input file############## #GCE isotopes for line in lines: if line[0] != ' ' and line[0:3] != 'rem': if line[0].isalpha(): a= line[:6].split()[0] isotopes_out.append(a) match = re.match(r"([a-z]+)([0-9]+)",a, re.I) items = match.groups() isotopes.append(items[0].capitalize()+'-'+items[1]) iso_line_idx.append(i) if line[0:3] == 'rem': isotopes_out.append('rem') isotopes.append('rem') iso_line_idx.append(i) i+=1 bb_massfrac=[0]*len(isotopes) type1a_yields=[0]*len(isotopes) nova_yields=[0]*len(isotopes) metallicities=[0]*len(isotopes) masses=[0]*len(isotopes) yields=[0]*len(isotopes) label=[0]*len(isotopes) i=-1 j=-1 while (True): i+=1 if i>len(lines)-1: break line=lines[i] if i in iso_line_idx: j+=1 #read data if 'big bang' in line: bb_massfrac[j]=float(line[:13]) continue #type 1a input ignore if 'number of type1a' in line: number=int(line[0]) type1a_yields[j]=[0]*number k=0 for sn in range(i+1,i+number+1): type1a_yields[j][k]=(float(lines[sn][:13])) k+=1 i=i+number continue #nova input ignore if 'number of nova' in line: number=int(line[0]) k=0 nova_yields[j]=[0]*number for nova in range(i+1,i+number+1): nova_yields[j][k]=(float(lines[nova][:13])) k+=1 i=i+number continue if 'number of metallicity values' in line: number_metallicities=int(line[0]) metallicities[j]=[0]*(number_metallicities) masses[j]=[0]*(number_metallicities) yields[j]=[0]*(number_metallicities) label[j]=[0]*(number_metallicities) z_counter=-1 continue if '* metallicity' in line: z_counter+=1 metallicities[j][z_counter]=float(line[:13]) continue if '* number of masses @ this metallicity' in line: number_masses=int(line[:2]) masses[j][z_counter]=[0]*(number_masses) yields[j][z_counter]=[0]*(number_masses) label[j][z_counter]=[0]*(number_masses) k=0 for mass in range(i+1,i+number_masses+1): masses[j][z_counter][k]=float(lines[mass][:13]) yields[j][z_counter][k]=float(lines[mass][14:24]) #check for label if len(lines[mass])>27: label1=lines[mass][27:].strip() label[j][z_counter][k] = label1 k+=1 i=i+number_masses continue #print bb_massfrac #print type1a_yields #print nova_yields #print metallicities #print masses #print yields #print label ##############read new simulation input########### #check if GCE isotopes are all available in simulation input sefiles=se(self.runs_H5_surf[0]) isotopes_1=sefiles.se.isotopes isotopes_2=[] no_iso=[] available_iso=[] for j in range(len(isotopes_1)): if is_stable(isotopes_1[j])=='t': isotopes_2.append(isotopes_1[j]) for j in range(len(isotopes)): #if isotope is not available stop if isotopes[j] not in isotopes_2: print 'Isotope ',isotopes[j],'is not available' no_iso.append(j) #print 'STOP' else: available_iso.append(isotopes[j]) #cycles=20*[[0,-1,500]] labelout,tcols,remn_masses_1 = self.write_prod_fact_stellarwinds(cycles=cycles,isotopes=available_iso,ascii_1=False,latex=False,GCE_input_fxt=True,exp_dir=exp_dir,delay=delay) #to include remnants remn_masses=[] #remn_masses_1=20*[3] for i in range(len(remn_masses_1)): #remnant_1='{:.4E}'.format(float(remn_masses_1[i])) remn_masses.append(remn_masses_1[i]) #add values for missing isotopes, value=99 #print 'testteest' #print no_iso for i in no_iso: for j in range(len(cols)): if i == no_iso[-1]: # to add the remnant masses print 'adding normal value for rem' cols[j].insert(i,remn_masses[j]) else: cols[j].insert(i,0) #because the set runs have the same Z: #but first item in label is 'specie', so skip this #trick here, measure z only from first input file #massive stars are therefore not checked and treated as other z metallicity_new=float(labelout[1].split('=')[1]) #metallicity1='{:.4E}'.format(float(metallicity)) masses_new1=[] for i in range(1,len(labelout)): mass=labelout[i].split('Msun')[0] #mass='{:.4E}'.format(float(mass)) masses_new1.append(float(mass)) #print 'Available input masses',masses yields_new=[0]*len(cols[0]) masses_new=[0]*len(cols[0]) label_new=[0]*len(cols[0]) #for every mass for k in range(len(yields_new)): yields_new[k]=[] masses_new[k]=masses_new1 label_new[k]=[label_masses]*len(masses_new1) for w in range(len(cols)): yields_new[k].append(cols[w][k]) #print metallicity_new #print yields_new #print label_new #print masses ##### Merge data############################### metallicities_out=len(isotopes)*[0] for t in range(len(isotopes)): pos_idx=0 #assuming that if this value is still in it, fresh file from fxt without any new z #print metallicities,float('1.9000E-04') if float('1.9000E-04') in metallicities[0]: metallicities_out[t]=metallicities[t][:2]+[metallicity_new] pos_idx=2 else: metal_sum=[] st=0 for metal in metallicities[t]: #metallicities_out[t]=metallicities[t] if (metal>metallicity_new) and (metallicity_new not in metal_sum): metal_sum.append(metallicity_new) metal_sum.append(metallicities[t][st]) else: metal_sum.append(metallicities[t][st]) st+=1 #in the case the added Z is larger than all available Z if len(metal_sum) == len(metallicities[t]): metal_sum.append(metallicity_new) metallicities_out[t]=metal_sum pos_idx=metallicities_out[t].index(metallicity_new) masses_out=len(isotopes)*[0] yields_out=len(isotopes)*[0] label_out=len(isotopes)*[0] print metallicities_out[0] for t in range(len(isotopes)): #print isotopes[t] masses_out[t]=len(metallicities_out)*[0] yields_out[t]=len(metallicities_out)*[0] label_out[t]=len(metallicities_out)*[0] w=0 for wt in range(len(metallicities_out[0])): #if case that it is a fresh file if float('1.9000E-04') in metallicities[0] and wt <2: masses_out[t][wt]=masses[t][w] yields_out[t][wt]=yields[t][w] label_out[t][wt]=label[t][w] w+=1 else: #if we are a t the position to add new metallicity if wt==pos_idx: #print 'add new z' masses_out[t][wt]=masses_new[t] yields_out[t][wt]=yields_new[t] label_out[t][wt]=label_new[t] #print wt,w else: #print metallicities_out #print 'else',wt,w #print masses_out[t],masses[t] masses_out[t][wt]=masses[t][w] yields_out[t][wt]=yields[t][w] label_out[t][wt]=label[t][w] w+=1 ###### Prepare output in right format###################### #isotopes_out defined already above bb_massfrac_out=bb_massfrac type1a_yields_out=type1a_yields nova_yields_out=nova_yields #metallicities_out=metallicities #masses_out=masses #yields_out=yields #label_out=label newline='' for i in range(len(isotopes)): newline+= (isotopes_out[i].ljust(29)+'* symbol name'+'\n') newline+= (' '+'{:.4E}'.format(bb_massfrac_out[i]).ljust(27)+'* big bang mass fraction'+'\n') newline+= (str(len(type1a_yields_out[i])).ljust(29)+'* number of type1a'+'\n') ia_label=['w7 tny86','sub chandra 1','sub chandra 2','sub chandra 7','sub chandra 8','nse 1a'] for k in range(len(type1a_yields_out[i])): newline+= (' '+'{:.4E}'.format(type1a_yields_out[i][k]).ljust(29)+ia_label[k]+'\n') newline+= (str(len(nova_yields_out[i])).ljust(29)+'* number of nova'+'\n') nova_label='* nova yield 1.0 1.25 & 1.35 msun' for k in range(len(nova_yields_out[i])): if k==0: newline+= (' '+'{:.4E}'.format(nova_yields_out[i][k]).ljust(27)+nova_label+'\n') else: newline+= (' '+'{:.4E}'.format(nova_yields_out[i][k]).ljust(27)+'\n') newline+= (str(len(metallicities_out[i])).ljust(29)+'* number of metallicity values'+'\n') for k in range(len(metallicities_out[i])): newline+= (' '+'{:.4E}'.format(metallicities_out[i][k]).ljust(27)+'* metallicity'+'\n') newline+= (str(len(masses_out[i][k])).ljust(29)+'* number of masses @ this metallicity'+'\n') for w in range(len(masses_out[i][k])): #the following is needed for the right label setting if w==0: newline+= (' '+'{:.4E}'.format(masses_out[i][k][w])+' '+'{:.4E}'.format(yields_out[i][k][w])+(7*' ')+label_out[i][k][w]+'\n') label_old=label_out[i][k][w] elif label_out[i][k][w] != label_old: newline+= (' '+'{:.4E}'.format(masses_out[i][k][w])+' '+'{:.4E}'.format(yields_out[i][k][w])+(7*' ')+label_out[i][k][w]+'\n') label_old=label_out[i][k][w] else: newline+= (' '+'{:.4E}'.format(masses_out[i][k][w])+' '+'{:.4E}'.format(yields_out[i][k][w])+'\n') ###write out data f2=open(output_file,'w') f2.write(newline) f2.close()