def get_kcorrections(row): maggies = np.array(pow(10, -0.4 * row[1:6])) invar = pow(0.4 * math.log(10) * maggies * np.array(row[6:12]), -2) k_tuple = np.concatenate(([row[0]], maggies, invar)) kcorrect_tuple = kcorrect.fit_coeffs(k_tuple) k_coeff = kcorrect.reconstruct_maggies(kcorrect_tuple) final_input = maggies / np.array(k_coeff[1:]) return [-2.5 * math.log(item, 10) for item in final_input], k_coeff[0]
def looking_at_redshift(z_low,z_high,name): # setting things up cosmo = FlatLambdaCDM(H0=70 * u.km / u.s / u.Mpc, Om0=0.3) kcorrect.load_templates() kcorrect.load_filters() # form of input for fit_coeff # redshift umaggies gmaggies rmaggies imaggies zmaggies uinvvar ginvvar rinvvar iinvvar zinvvar # output: redshift u_rec g_rec r_rec i_rec z_rec x = [] y = [] array = np.load('/home/calum/Documents/MPhysProj/mgs_sample/mgs_kcorrect_array.npy') print('Number of results:',len(array)) print('Starting for loop...') # need to sort out the coefficents so they're in units of maggies! #Note that the conversion to the inverse variances from the maggies and the magnitude errors is (0.4 ln(10) × maggies × magerr)-2 # maggies are simply related to magnitudes by 10−0.4m for row in array: # kcorrect maggies = np.array(pow(10,-0.4*row[2:7])) invar = pow(0.4*math.log(10)*maggies*np.array(row[7:]),-2) k_tuple = np.concatenate(([row[1]],maggies,invar)) kcorrect_tuple = kcorrect.fit_coeffs(k_tuple) k_coeff = kcorrect.reconstruct_maggies(kcorrect_tuple) final_input = maggies/np.array(k_coeff[1:]) kcorrection_array = [-2.5*math.log(item,10) for item in final_input] # put above in a function probs y_val = float(row[2]) - kcorrection_array[1] - float(row[4]) + kcorrection_array[3] z = k_coeff[0] if y_val > 0 and y_val < 6.5: if z > z_low and z < z_high: x_val = float(row[0])-5*(math.log(cosmo.luminosity_distance(z).to(u.pc).value/10 ,10)) if x_val < -15.5 and x_val > -23.5: x.append(x_val) y.append(y_val) contour_plot_array = np.array([x,y]) print('Length of output:',len(x)) np.save('/home/calum/Documents/MPhysProj/data/'+name+'.npy',contour_plot_array) print('Finished:',name)
maggie_i = 10**((22.5 - data['mag_i'][ind]) / 2.5) maggie_z = 10**((22.5 - data['mag_z'][ind]) / 2.5) import kcorrect, numpy kcorrect.load_templates() kcorrect.load_filters() z_arr = data['z'][ind] for i in range(n_source): print(i) a = [ data['z'][ind[i]], maggie_u[i], maggie_g[i], maggie_r[i], maggie_i[i], maggie_z[i], 6.216309e+16, 3.454767e+17, 1.827409e+17, 1.080889e+16, 3163927000000000.0 ] c = kcorrect.fit_coeffs(a) m = kcorrect.reconstruct_maggies(c) maggie_u[i] = m[1] maggie_g[i] = m[2] maggie_r[i] = m[3] maggie_i[i] = m[4] maggie_z[i] = m[5] type_arr = np.zeros(len(ind)) type_arr = type_arr - 999 O2_index = np.log10((data['o21'][ind] + data['o22'][ind]) / data['hb'][ind]) O3_index = np.log10(data['o3'][ind] / data['hb'][ind]) sigma_o3 = np.log10(np.sqrt(data['sigma_o3'][ind]**2)) #-55**2)) sigma_star = np.log10(data['VDISP'][ind]) indt = np.where(sigma_star == 0)
array = np.load( '/home/calum/Documents/Mphys_data/mgs_multiwavelength/final_mgs_array.npy') print('Number of results:', len(array)) print('Starting for loop...') # need to sort out the coefficents so they're in units of maggies! #Note that the conversion to the inverse variances from the maggies and the magnitude errors is (0.4 ln(10) × maggies × magerr)-2 # maggies are simply related to magnitudes by 10−0.4m for row in array: # kcorrect maggies = np.array(pow(10, -0.4 * row[2:7])) invar = pow(0.4 * math.log(10) * maggies * np.array(row[7:12]), -2) k_tuple = np.concatenate(([row[1]], maggies, invar)) kcorrect_tuple = kcorrect.fit_coeffs(k_tuple) k_coeff = kcorrect.reconstruct_maggies(kcorrect_tuple) final_input = maggies / np.array(k_coeff[1:]) kcorrection_array = [-2.5 * math.log(item, 10) for item in final_input] # put above in a function probs y_val = float(row[2]) - kcorrection_array[1] - float( row[4]) + kcorrection_array[3] z = k_coeff[0] if z > 0: if np.isnan(y_val): # why wouldn't y_val be a number? print('do not worry, everything is fine now') else: x_val = float(row[0]) - 5 * (math.log( cosmo.luminosity_distance(z).to(u.pc).value / 10, 10)) obj.append(row[13])
def add_stellar_mass(base, cosmology=WMAP9): """ Calculate stellar mass based only on gi colors and redshift and add to base catalog. Based on GAMA data using Taylor et al (2011). `base` is modified in-place. Parameters ---------- base : astropy.table.Table Returns ------- base : astropy.table.Table """ base['log_sm'] = np.nan global _HAS_KCORRECT if not _HAS_KCORRECT: logging.warn('No kcorrect module. Stellar mass not calculated!') return base if 'OBJID_sdss' in base.colnames: # version 2 base catalog with SDSS postfix = '_sdss' to_calc_query = Query(C.has_spec, 'OBJID_sdss != -1') elif 'EXTINCTION_U' in base.colnames: # version 1 base catalog postfix = '' to_calc_query = C.has_spec for b in get_sdss_bands(): base['{}_mag'.format(b)] = base[b] - base['EXTINCTION_{}'.format( b.upper())] else: # version 2 base catalog without SDSS logging.warn('No SDSS bands! Stellar mass not calculated!') return base to_calc_mask = to_calc_query.mask(base) if not to_calc_mask.any(): logging.warn('Stellar mass not calculated because no valid entry!') return base if _HAS_KCORRECT != 'LOADED': kcorrect.load_templates() kcorrect.load_filters() _HAS_KCORRECT = 'LOADED' mag = view_table_as_2d_array(base, ('{}_mag{}'.format(b, postfix) for b in get_sdss_bands()), to_calc_mask, np.float32) mag_err = view_table_as_2d_array(base, ('{}_err{}'.format(b, postfix) for b in get_sdss_bands()), to_calc_mask, np.float32) redshift = view_table_as_2d_array(base, ['SPEC_Z'], to_calc_mask, np.float32) # CONVERT SDSS MAGNITUDES INTO MAGGIES mgy = 10.0**(-0.4 * mag) mgy_ivar = (0.4 * np.log(10.0) * mgy * mag_err)**-2.0 kcorrect_input = np.hstack((redshift, mgy, mgy_ivar)) # USE ASTROPY TO CALCULATE LUMINOSITY DISTANCE lf_distmod = cosmology.distmod(redshift.ravel()).value # RUN KCORRECT FIT, AND CALCULATE STELLAR MASS tmremain = np.array([0.601525, 0.941511, 0.607033, 0.523732, 0.763937]) sm_not_normed = np.fromiter((np.dot(kcorrect.fit_coeffs(x)[1:], tmremain) for x in kcorrect_input), np.float64, len(kcorrect_input)) base['log_sm'][to_calc_mask] = np.log10(sm_not_normed) + (0.4 * lf_distmod) return base