def LoadSimFile( data_file, gal_num): print(data_file) gal_file = open(data_file, "r") temp_var = np.zeros(SIZE_GAL_FILE) for index, line in enumerate(gal_file): values = line.split(' ') if local_gal_wl[-1] == 0: local_gal_wl[index] = float(values[0]) temp_var[index] = float(values[FILE_SECOND_COLUMN]) localgalaxy_numbers[gal_num] = int(B.get_number(data_file)) gal_file.close() temp_var = temp_var * (local_gal_wl*local_gal_wl)/speed_of_light #Luminosity per frequency temp_var = temp_var/(4*math.pi*dist_base_2) #Luminosity to Flux #print(new_value) temp_var = temp_var * 1e32 #Flux in microJanksys #print(new_value) #print localgalaxy_file_data.shape #print temp_var.shape #print localgalaxy_file_data[gal_num].shape #print localgalaxy_file_data[gal_num,:].shape localgalaxy_file_data[gal_num,:]= 23.9 - 2.5 * np.log10(temp_var[:])
def LoadSimFiles(): print("Loading Simulation Files") data_files = glob.glob(data_folder+"/"+ "*.txt") for i in range(len(data_files)): print(data_files[i]) gal_file = open(data_files[i], "r") index = 0 for line in gal_file: values = line.split(' ') if i == 0: gal_wl[index] = float(values[0]) galaxy_file_data[i][index] = float(values[1]) index+=1 galaxy_numbers[i] = B.get_number(data_files[i]) gal_file.close()
def LoadFiles(): print("Loading Input Files") doNotAdd = False #For files with problems in the values for i in range(len(input_file_names)): print(input_file_names[i]) data = {"red": -1 } #data dictionary redshift - red #wavelengths -wl ; flux - fl; error - err input_file = open(input_file_names[i], "r") redshift = -1 temp_wl = [] temp_fl = [] temp_err = [] for line in input_file: values = line.split(' ') if data['red'] ==-1: data['red'] = float(values[0]) #Redshift is the first value of the file else: if (len(values) ==3 and float(values[1]) >0 and float(values[2]) > 0): temp_wl.append( float(values[0])/(1+data['red'])) temp_fl.append( float(values[1])) temp_err.append( float(values[2])) elif len(temp_fl)>1 and temp_fl[-1] <= 0: print(values) doNotAdd = True if doNotAdd == False: data['wl'] = np.array(temp_wl) data['fl'] = np.array(temp_fl) data['err'] = np.array(temp_err) data['num'] = B.get_number(input_file_names[i]) input_files_data.append(data) input_file.close()
def Comp(file_name, have_wl): data_comp_file = open(file_name, "r") data_comp = [] for line in data_comp_file: values = line.split(' ') if not have_wl: data_comp_wl.append( float( values[0])) #print("FILE WL Added: " +str(data_comp_wl[-1]) ) data_comp.append( float(values[1])) if have_wl == False: size_comp_wl = len(data_comp_wl) have_wl = True #STAR COMPARISON for k in range(len(input_files_data)): #For each file do a comparison #print(k, " ", input_file_names[k]) chi_2 = 0 index = 0 int_comp = 0 #VECTORIZATION OF DATA # NEEDS WORK (A LOT) file_data_fl = np.zeros(np.shape(input_files_data[k]['wl'])[0]) for i in range(np.shape(input_files_data[k]['wl'])[0]): while input_files_data[k]['wl'][i] < data_comp_wl[0]: i+=1 print("DATA WL TOO SMALL") #we do not have information to calculate these so we skip if input_files_data[k]['wl'][i] > data_comp_wl[-1]: i = np.shape(input_files_data[k]['wl'])[0] print("DATA WL TOO BIG") break # we can stop the loop here since the data points cannot be compared while (index + 1) < size_comp_wl and data_comp_wl[index +1] <= input_files_data[k]['wl'][i]: index+=1 #choose the correct index to make the comparison file_data_fl[i] = (data_comp[index+1]-data_comp[index])/(data_comp_wl[index+1] - data_comp_wl[index])*(input_files_data[k]['wl'][i] - data_comp_wl[index]) + data_comp[index] int_comp +=1 #print(input_files_data[k]['wl'][i], " " , input_files_data[k]['fl'][i], " " , file_data_fl[i]) chi_2 = np.sum( (input_files_data[k]['fl'] - file_data_fl)**2/input_files_data[k]['err']) try: chi_2 /= int_comp #normalizes the chi_2 by the number of comparisons except: print(int_comp) if int_comp != np.shape(input_files_data[k]['fl'])[0]: print("ERRROR NOT ALL COMPARED") factor = np.exp(-chi_2) cumu[k] += factor #print(str(B.get_number(file_name))) #print( "MASS: ",prop[str(B.get_number(file_name))][0]) cumu_mass[k] += factor * prop[str(B.get_number(file_name))][0] #print( "MET: ",prop[str(B.get_number(file_name))][1]) cumu_met[k] += factor * prop[str(B.get_number(file_name))][1] data_comp_file.close()
for i in np.arange(nGal): #range(len(global_prop)): # print i # print 'this string is '+str(int(global_prop[0][i])) prop[str(int(global_prop[0][i]))] = [global_prop[1][i], global_prop[2][i]] print("BEGIN COMPARISON") for i in range(len(input_file_names)): if (i%n_tasks) == rank: res = Comp(input_file_names[i]) if res == -1: ## Error in running continue else: numb = int(B.get_number(input_file_names[i])) local_results[0][i] = numb local_results[1][i] = res[0] local_results[2][i] = res[1] #comm.Barrier() global_results = local_results #comm.Allreduce(local_results, global_results, op=MPI.SUM) if rank == 0: result = open(result_folder + "results.out", "w") for i in np.arange(len(global_results[0])): result.write(str(int(global_results[0][i])) + " " + str(global_results[1][i]) + " " + str(global_results[2][i]) + "\n") print("SUCCESS")
#print( "MASS: ",prop[str(B.get_number(file_name))][0]) cumu_mass += factor * prop[str(int(galaxy_numbers[k]))][0] #print( "MET: ",prop[str(B.get_number(file_name))][1]) cumu_met += factor * prop[str(int(galaxy_numbers[k]))][1] return (cumu_mass/cumu, cumu_met/cumu) ################################################################### # # Run Code # ################################################################### loadProperties() LoadSimFiles() results = open(result_folder + "results.out", 'w') for i in range(len(input_file_names)): p = Comp(input_file_names[i]) if p == -1: continue else: name = B.get_number(input_file_names[i]) results.write(str(name) + " " +str(p[0]) + " " + str(p[1]) + "\n") results.close()
#Redshift is the first value of the file else: if (len(values) ==3 and float(values[1]) >0 and float(values[2]) > 0): temp_wl.append( float(values[0])/(1+data['red'])) temp_fl.append( float(values[1])) temp_err.append( float(values[2])) elif len(temp_fl)>1 and temp_fl[-1] <= 0: print(values) doNotAdd = True if doNotAdd == False: data['wl'] = np.array(temp_wl) data['fl'] = np.array(temp_fl) data['err'] = np.array(temp_err) data['num'] = B.get_number(input_file_names[i]) input_files_data.append(data) input_file.close() ################################################################### # # TRANSFORM INTO ABSOLUTE MAGNITUDES # ################################################################### print("Formating input data to correct units") for k in range(len(input_files_data)): dist = (cosmocalc.cosmocalc( input_files_data[k]['red'], H0=70.4, WM=0.2726, WV=0.7274))['DL_Mpc']