def get_kcorrect2(data, mag, magerr, bands, aps, filter, redshift): import kcorrect N = len(redshift) zshift = np.array(redshift, dtype='float32') kcorrect.load_templates() print('It takes a while to load the filters...') #<---- it's this kcorrect.load_filters(f=filter) #magnitudes in different bands kc = [] for p in range(0, N): i = data[bands[2] + mag + aps][p] ierr = data[bands[2] + mag + aps + magerr][p] r = data[bands[1] + mag + aps][p] rerr = data[bands[1] + mag + aps + magerr][p] g = data[bands[0] + mag + aps][p] gerr = data[bands[0] + mag + aps + magerr][p] z = data[bands[3] + mag + aps][p] zerr = data[bands[3] + mag + aps + magerr][p] y = data[bands[4] + mag + aps][p] yerr = data[bands[4] + mag + aps + magerr][p] #print(i, r, g, z) maggiesi = 10**(-0.4 * i) maggiesr = 10**(-0.4 * r) maggiesg = 10**(-0.4 * g) maggiesz = 10**(-0.4 * z) maggiesy = 10**(-0.4 * y) magiivar = 1. / ( (10**(-0.4 * (ierr)))**2) / (0.4 * math.log(10.))**2 / maggiesi**2 maggivar = 1. / ( (10**(-0.4 * (gerr)))**2) / (0.4 * math.log(10.))**2 / maggiesg**2 magrivar = 1. / ( (10**(-0.4 * (rerr)))**2) / (0.4 * math.log(10.))**2 / maggiesr**2 magzivar = 1. / ( (10**(-0.4 * (zerr)))**2) / (0.4 * math.log(10.))**2 / maggiesz**2 magyivar = 1. / ( (10**(-0.4 * (yerr)))**2) / (0.4 * math.log(10.))**2 / maggiesy**2 maggies = np.array([maggiesg, maggiesr, maggiesi, maggiesz, maggiesy], dtype='float32') maggies_ivar = np.array( [maggivar, magrivar, magiivar, magzivar, magyivar], dtype='float32') coeffs = kcorrect.fit_nonneg(redshift[p], maggies, maggies_ivar) rm = kcorrect.reconstruct_maggies(coeffs) #reconstructed maggies rm0 = kcorrect.reconstruct_maggies(coeffs, redshift=0.) kcs = -2.5 * np.log10(rm[1:] / rm0[1:]) #print('kcorrect in g, r, i, z, y= ',kcs) kc.append(kcs) #print(kc) return kc
def get_kcorrect(data, mag, magerr, bands, filter, redshift): import kcorrect #magnitudes in different bands i = data[bands[2] + mag + aps] ierr = data[bands[2] + mag + aps + magerr] r = data[bands[1] + mag + aps] rerr = data[bands[1] + mag + aps + magerr] g = data[bands[0] + mag + aps] gerr = data[bands[0] + mag + aps + magerr] z = data[bands[3] + mag + aps] zerr = data[bands[3] + mag + aps + magerr] y = data[bands[4] + mag + aps] yerr = data[bands[4] + mag + aps + magerr] #print(i, r, g, z) maggiesi = 10**(-0.4 * i) maggiesr = 10**(-0.4 * r) maggiesg = 10**(-0.4 * g) maggiesz = 10**(-0.4 * z) maggiesy = 10**(-0.4 * y) magiivar = 1. / ( (10**(-0.4 * (ierr)))**2) / (0.4 * math.log(10.))**2 / maggiesi**2 maggivar = 1. / ( (10**(-0.4 * (gerr)))**2) / (0.4 * math.log(10.))**2 / maggiesg**2 magrivar = 1. / ( (10**(-0.4 * (rerr)))**2) / (0.4 * math.log(10.))**2 / maggiesr**2 magzivar = 1. / ( (10**(-0.4 * (zerr)))**2) / (0.4 * math.log(10.))**2 / maggiesz**2 magyivar = 1. / ( (10**(-0.4 * (yerr)))**2) / (0.4 * math.log(10.))**2 / maggiesy**2 print('is loading the templates taking a while') kcorrect.load_templates() print('or is loading the filters?') #<---- it's this kcorrect.load_filters(f=filter) maggies = np.array([maggiesg, maggiesr, maggiesi, maggiesz, maggiesy], dtype='float32') maggies_ivar = np.array([maggivar, magrivar, magiivar, magzivar, magyivar], dtype='float32') coeffs = kcorrect.fit_nonneg(redshift, maggies, maggies_ivar) rm = kcorrect.reconstruct_maggies(coeffs) #reconstructed maggies rm0 = kcorrect.reconstruct_maggies(coeffs, redshift=0.) kc = -2.5 * np.log10(rm[1:] / rm0[1:]) print('kcorrect in g, r, i, z, y= ', kc) #in order g, r, i, z, y return kc
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)
def kcorrect(mags, magerrs, redshifts, bandshift, filters="des_filters.dat"): if kc is None: return None try: kc.load_templates() kc.load_filters(f=filters) except AttributeError: raise RuntimeError("No kcorrect available") maggies = 10.0**(-0.4 * mags) maggies_ivars = 1.0 / (maggies * 0.4 * np.log(10) * magerrs)**2 kcorrect_arr = np.zeros_like(magerrs) for i, (maggie, z, maggies_ivar) in enumerate(zip(maggies, redshifts, maggies_ivars)): coffs = kc.fit_nonneg(z, maggie, maggies_ivar) # Reconstruct the magnitudes as observed and in the rest frame rmaggies = kc.reconstruct_maggies(coffs, redshift=z) reconstruct_maggies = kc.reconstruct_maggies(coffs, redshift=bandshift) reconstruct_maggies /= 1.0 + bandshift kcorrect_res_this = 2.5 * np.log10(reconstruct_maggies / rmaggies) kcorrect_arr[i] = kcorrect_res_this[1:] return kcorrect_arr
ind = np.array( list( set(ind1[0]) & set(ind2[0]) & set(ind3[0]) & set(ind4[0]) & set(ind5[0]) & set(ind6[0]) & set(ind7[0]) & set(ind8[0]) & set(ind9[0]) & set(ind10[0]) & set(ind11[0]) & set(ind12[0]) & set(ind13[0]) & set(ind14[0]))) n_source = len(ind) maggie_u = 10**((22.5 - data['mag_u'][ind]) / 2.5) maggie_g = 10**((22.5 - data['mag_g'][ind]) / 2.5) maggie_r = 10**((22.5 - data['mag_r'][ind]) / 2.5) 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]
import kcorrect import numpy import os test_file = os.path.join(os.environ['KCORRECT_DIR'], 'test', 'sample.dat') kcorrect.load_templates() kcorrect.load_filters() kcorrect.fit_coeffs_from_file(test_file) kcorrect.reconstruct_maggies_from_file('coeffs.dat')
def load_SDSS(filename, colors, SDSS_kcorrection_z): """ Compute the CDF of SDSS colors for some redshift range. Parameters ---------- colors : list of string, required list of colors to be tested e.g ['u-g','g-r','r-i','i-z'] zlo : float, requred minimum redshift of the validation catalog zhi : float, requred maximum redshift of the validation catalog """ translate = {'u': 'M_u', 'g': 'M_g', 'r': 'M_r', 'i': 'M_i', 'z': 'M_z'} data_dir = os.path.dirname(filename) kcorrect_magnitudes_path = os.path.join( data_dir, 'sdss_k_corrected_magnitudes_z_0.06_0.09_z_{:.3f}.fits'.format( SDSS_kcorrection_z)) if not os.path.exists(kcorrect_magnitudes_path): kcorrect_maggies_path = os.path.join( data_dir, 'sdss_k_corrected_maggies_z_0.06_0.09_z_{:.3f}.dat'.format( SDSS_kcorrection_z)) # Load kcorrect templates and filters kcorrect.load_templates() kcorrect.load_filters() kcorrect.reconstruct_maggies_from_file(filename, redshift=SDSS_kcorrection_z, outfile=kcorrect_maggies_path) #----------Convert kcorrected maggies to magnitudes---------------- cat = Table.read(os.path.join(data_dir, kcorrect_maggies_path), format='ascii.no_header', names=('redshift', 'maggies_u', 'maggies_g', 'maggies_r', 'maggies_i', 'maggies_z')) cat0 = Table.read(filename, format='ascii.no_header') redshifts = cat0['col1'] u = -2.5 * np.log10(cat['maggies_u']) g = -2.5 * np.log10(cat['maggies_g']) r = -2.5 * np.log10(cat['maggies_r']) i = -2.5 * np.log10(cat['maggies_i']) z = -2.5 * np.log10(cat['maggies_z']) cat1 = Table() cat1['redshift'] = redshifts cat1['u'] = u cat1['g'] = g cat1['r'] = r cat1['i'] = i cat1['z'] = z cat1.write(kcorrect_magnitudes_path) cat = cat1.copy() else: cat = Table.read(kcorrect_magnitudes_path) # distance modulus ########################################## cosmo = FlatLambdaCDM(H0=70.2, Om0=0.275) ########################################## dm = np.array(cosmo.distmod(cat['redshift'])) cat['M_u'] = cat['u'] - dm cat['M_g'] = cat['g'] - dm cat['M_r'] = cat['r'] - dm cat['M_i'] = cat['i'] - dm cat['M_z'] = cat['z'] - dm # Calculate the aboluste magnitude cut mask = (cat['redshift'] > 0.089) & (cat['redshift'] < 0.090) mr = cat['M_r'][mask] mr_sort = np.sort(mr) mrmax = mr_sort[int(len(mr) * 0.85)] # Apply r-band absolute magnitude mask = (cat['M_r'] < mrmax) cat = cat[mask] vsummary = [] # Histogram with small bins (for calculating CDF) for index in range(len(colors)): color = colors[index] band1 = translate[color[0]] band2 = translate[color[2]] bins = np.linspace(-1, 4, 2000) hist, bin_edges = np.histogram((cat[band1] - cat[band2]), bins=bins) hist = hist / np.sum(hist) binctr = (bin_edges[1:] + bin_edges[:-1]) / 2. # Convert PDF to CDF cdf = np.zeros(len(hist)) cdf[0] = hist[0] for cdf_index in range(1, len(hist)): cdf[cdf_index] = cdf[cdf_index - 1] + hist[cdf_index] vsummary.append((len(cat), binctr, hist, cdf)) return vsummary, mrmax
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