forked from amjsmith/hodpy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathk_correction.py
233 lines (177 loc) · 7.83 KB
/
k_correction.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
#! /usr/bin/env python
from __future__ import print_function
import numpy as np
import parameters as par
from scipy.interpolate import RegularGridInterpolator
from cosmology import Cosmology
class KCorrection(object):
def apparent_magnitude(self, absolute_magnitude, redshift):
pass
def absoulte_magnitude(self, apparent_magnitude, redshift):
pass
def magnitude_faint(self, redshift):
pass
class GAMA_KCorrection(KCorrection):
"""
Colour-dependent polynomial fit to the GAMA K-correction
(Fig. 13 of Smith+17), used to convert between SDSS r-band
Petrosian apparent magnitudes, and rest frame absolute manigutues
at z_ref = 0.1
"""
def __init__(self):
# read file of parameters of polynomial fit to k-correction
cmin, cmax, A, B, C, D, E, cmed = \
np.loadtxt(par.k_corr_file, unpack=True)
self.z0 = 0.1 # reference redshift
# Polynomial fit parameters
self.__A_interpolator = self.__initialize_parameter_interpolator(A,cmed)
self.__B_interpolator = self.__initialize_parameter_interpolator(B,cmed)
self.__C_interpolator = self.__initialize_parameter_interpolator(C,cmed)
self.__D_interpolator = self.__initialize_parameter_interpolator(D,cmed)
self.__E = E[0]
self.colour_min = np.min(cmed)
self.colour_max = np.max(cmed)
self.colour_med = cmed
self.cosmo = Cosmology(par.h0, par.OmegaM, par.OmegaL)
# Linear extrapolation
self.__X_interpolator = lambda x: None
self.__Y_interpolator = lambda x: None
self.__X_interpolator, self.__Y_interpolator = \
self.__initialize_line_interpolators()
def __initialize_parameter_interpolator(self, parameter, median_colour):
# interpolated polynomial coefficient as a function of colour
return RegularGridInterpolator((median_colour,), parameter,
bounds_error=False, fill_value=None)
def __initialize_line_interpolators(self):
# linear coefficients for z>0.5
X = np.zeros(7)
Y = np.zeros(7)
# find X, Y at each colour
redshift = np.array([0.4,0.5])
arr_ones = np.ones(len(redshift))
for i in range(7):
k = self.k(redshift, arr_ones*self.colour_med[i])
X[i] = (k[1]-k[0]) / (redshift[1]-redshift[0])
Y[i] = k[0] - X[i]*redshift[0]
X_interpolator = RegularGridInterpolator((self.colour_med,), X,
bounds_error=False, fill_value=None)
Y_interpolator = RegularGridInterpolator((self.colour_med,), Y,
bounds_error=False, fill_value=None)
return X_interpolator, Y_interpolator
def __A(self, colour):
# coefficient of the z**4 term
colour_clipped = np.clip(colour, self.colour_min, self.colour_max)
return self.__A_interpolator(colour_clipped)
def __B(self, colour):
# coefficient of the z**3 term
colour_clipped = np.clip(colour, self.colour_min, self.colour_max)
return self.__B_interpolator(colour_clipped)
def __C(self, colour):
# coefficient of the z**2 term
colour_clipped = np.clip(colour, self.colour_min, self.colour_max)
return self.__C_interpolator(colour_clipped)
def __D(self, colour):
# coefficient of the z**1 term
colour_clipped = np.clip(colour, self.colour_min, self.colour_max)
return self.__D_interpolator(colour_clipped)
def __X(self, colour):
colour_clipped = np.clip(colour, self.colour_min, self.colour_max)
return self.__X_interpolator(colour_clipped)
def __Y(self, colour):
colour_clipped = np.clip(colour, self.colour_min, self.colour_max)
return self.__Y_interpolator(colour_clipped)
def k(self, redshift, colour):
"""
Polynomial fit to the GAMA K-correction for z<0.5
The K-correction is extrapolated linearly for z>0.5
Args:
redshift: array of redshifts
colour: array of ^0.1(g-r) colour
Returns:
array of K-corrections
"""
K = np.zeros(len(redshift))
idx = redshift <= 0.5
K[idx] = self.__A(colour[idx])*(redshift[idx]-self.z0)**4 + \
self.__B(colour[idx])*(redshift[idx]-self.z0)**3 + \
self.__C(colour[idx])*(redshift[idx]-self.z0)**2 + \
self.__D(colour[idx])*(redshift[idx]-self.z0) + self.__E
idx = redshift > 0.5
K[idx] = self.__X(colour[idx])*redshift[idx] + self.__Y(colour[idx])
return K
def apparent_magnitude(self, absolute_magnitude, redshift, colour):
"""
Convert absolute magnitude to apparent magnitude
Args:
absolute_magnitude: array of absolute magnitudes (with h=1)
redshift: array of redshifts
colour: array of ^0.1(g-r) colour
Returns:
array of apparent magnitudes
"""
# Luminosity distance
D_L = (1.+redshift) * self.cosmo.comoving_distance(redshift)
return absolute_magnitude + 5*np.log10(D_L) + 25 + \
self.k(redshift,colour)
def absolute_magnitude(self, apparent_magnitude, redshift, colour):
"""
Convert apparent magnitude to absolute magnitude
Args:
apparent_magnitude: array of apparent magnitudes
redshift: array of redshifts
colour: array of ^0.1(g-r) colour
Returns:
array of absolute magnitudes (with h=1)
"""
# Luminosity distance
D_L = (1.+redshift) * self.cosmo.comoving_distance(redshift)
return apparent_magnitude - 5*np.log10(D_L) - 25 - \
self.k(redshift,colour)
def magnitude_faint(self, redshift):
"""
Convert faintest apparent magnitude (set in parameters.py)
to faintest absolute magnitude
Args:
redshift: array of redshifts
Returns:
array of absolute magnitudes (with h=1)
"""
# convert faint apparent magnitude to absolute magnitude
# for bluest and reddest galaxies
arr_ones = np.ones(len(redshift))
abs_mag_blue = self.absolute_magnitude(arr_ones*par.mag_faint,
redshift, arr_ones*0)
abs_mag_red = self.absolute_magnitude(arr_ones*par.mag_faint,
redshift, arr_ones*2)
# find faintest absolute magnitude, add small amount to be safe
mag_faint = np.maximum(abs_mag_red, abs_mag_blue) + 0.01
# avoid infinity
mag_faint = np.minimum(mag_faint, -10)
return mag_faint
def test():
import matplotlib.pyplot as plt
kcorr = GAMA_KCorrection()
redshifts = np.arange(0, 1, 0.01)
arr_ones = np.ones(len(redshifts))
for i in range(7):
k = kcorr.k(redshifts, arr_ones*kcorr.colour_med[i])
plt.plot(redshifts, k)
plt.show()
app_mag = np.ones(len(redshifts)) * 20.0
colour = np.ones(len(app_mag))
abs_mag = kcorr.absolute_magnitude(app_mag, redshifts, colour*2)
plt.plot(redshifts, abs_mag, c="r", label="red")
abs_mag = kcorr.absolute_magnitude(app_mag, redshifts, colour*0)
plt.plot(redshifts, abs_mag, c="c", label="blue")
mag_faint = kcorr.magnitude_faint(redshifts)
plt.plot(redshifts, mag_faint, c="k", ls=":", label="mag faint")
print(mag_faint)
plt.legend(loc="upper right")
plt.xlim(0,1)
plt.ylim(-12, -24)
plt.xlabel("redshift")
plt.ylabel("Absolute magnitude")
plt.title("r=20.0")
plt.show()
if __name__ == "__main__":
test()