1 """Conversions between fluxes, luminosities and AB magnitudes.
2 """
3 import math
4
5 import numpy
6 import cosmolopy.distance as cd
7 import cosmolopy.constants as cc
8
9 """AB Magnitude zero point."""
10 MAB0 = -2.5 * numpy.log10(3631.e-23)
11
13 """Convert between frequency and wavelength, nu to lambda or
14 lambda to nu.
15
16 Either:
17 given `lambda` returns 'nu' or
18 given `nu` returns `lambda`.
19
20 Units are:
21 `Hz` for nu and `Ang` for `lambda`.
22
23 Works because `nu = c/lambda` and `lambda = c/nu`, and I use `c`
24 in units of `Angs/s`.
25
26 Usage
27 -----
28
29 >>> from cosmolopy import magnitudes
30 >>> nu = magnitudes.nu_lambda(1216.)
31 >>> lam = magnitudes.nu_lambda(nu)
32 >>> lam
33 1216.0
34 """
35 return (cc.c_light_cm_s / cc.angstrom_cm) / coordinate
36
38 """Convert f_nu to f_lambda or f_lambda to f_nu.
39
40 Either:
41 given `f_lambda` and `lambda` returns `f_nu` and 'nu' or
42 given `f_nu` and `nu` returns `f_lambda` and `lambda`.
43
44 Units are:
45 `erg s^-1 cm^-2 Hz^-1` for f_nu and
46 `erg s^-1 cm^-2 Ang^-1` for `f_lambda`.
47
48 Works because `f_nu = f_lambda * lambda**2/c` and `f_lambda = f_nu
49 * nu**2/c`, and I use `c` in units of `Angs/s`.
50
51 Usage
52 -----
53
54 >>> from cosmolopy import magnitudes
55 >>> fnu, nu = magnitudes.f_nu_lambda(2.0, 1216.)
56 >>> flam, lam = magnitudes.f_nu_lambda(fnu, nu)
57 >>> flam, lam
58 (2.0, 1216.0)
59 """
60
61 return (flux * coordinate**2. / (cc.c_light_cm_s / cc.angstrom_cm),
62 (cc.c_light_cm_s / cc.angstrom_cm) / coordinate)
63
64
66 """Convert apparent magnitude into flux (erg s^-1 cm^-2 Hz^-1).
67
68 Usage
69 -----
70
71 Check that the AB magnitude zero point is 3631 Jy:
72
73 >>> from cosmolopy import magnitudes
74 >>> "%.4g" % (magnitudes.f_nu_from_magAB(0.0)/1e-23)
75 '3631'
76
77 """
78 f_nu = 10.**((magAB + MAB0)/(-2.5))
79 return f_nu
80
82 """Convert absolute magnitude into luminosity (erg s^-1 Hz^-1).
83
84 Usage
85 -----
86
87 Check that the AB magnitude zero point is 3631 Jy:
88
89 >>> from cosmolopy import magnitudes
90 >>> import math
91 >>> L_nu = magnitudes.L_nu_from_magAB(0.0)
92 >>> "%.4g" % (L_nu/(1e-23 * 4. * math.pi * (10*cc.pc_cm)**2))
93 '3631'
94
95 """
96 const = 4. * math.pi * (10. * cc.pc_cm)**2.
97 L_nu = const * 10.**((magAB + MAB0)/(-2.5))
98 return L_nu
99
101 """Convert luminosity (erg s^-1 Hz^-1) into absolute magnitude.
102
103 Usage
104 -----
105
106 Check that the AB magnitude zero point is 3631 Jy:
107
108 >>> import numpy, math
109 >>> from cosmolopy import magnitudes, cc
110 >>> L_nu = 3631e-23 * (4. * math.pi * (10*cc.pc_cm)**2)
111 >>> "%.3f" % numpy.abs(magnitudes.magnitude_AB_from_L_nu(L_nu))
112 '0.000'
113
114 """
115
116 const = 4. * math.pi * (10. * cc.pc_cm)**2.
117 magAB = -2.5 * numpy.log10(luminosity_nu/const) - MAB0
118 return magAB
119
121 """Distance modulus mu = m-M.
122
123 The distance modulus is the difference between the apparent and
124 absolute magnitudes,
125
126 mu = 5 log(d/10 pc)
127
128 Usage
129 -----
130
131 >>> from cosmolopy import fidcosmo, magnitudes
132 >>> "mu(z=6) = %.4g" % magnitudes.distance_modulus(6.0, **fidcosmo)
133 'mu(z=6) = 48.86'
134
135 """
136 dl = cd.luminosity_distance(z, **cosmo)
137 mu = 5 * numpy.log10(dl/(10e-6))
138 return mu
139
141 """The apparent and absolute AB magnitude given a flux.
142
143 Inputs
144 ------
145
146 z: array or scalar
147 the redshift of the source. Set to None to get absolute
148 magnitude from a luminosity.
149
150 f_lambda: array or scalar
151 observed flux from the source in units of erg s^-1 cm^-2 Ang^-1
152
153 wavelength: array or scalar
154 the observed wavelength of the flux measurement(s) in Angstroms
155
156 Returns
157 -------
158
159 Returns ab (apparent), and AB (absolute) magnitudes.
160
161 Notes
162 -----
163
164 Note that here you pass fluxes that are per unit wavelength, not
165 per unit frequency. To get the absolute magnitude for a
166 *luminosity* specified in units of erg s^-1 Ang^-1, set z=None.
167
168 Usage
169 -----
170
171 Check that the AB magnitude zero point is 3631 Jy:
172
173 >>> from cosmolopy import fidcosmo, magnitudes, cc, cd
174 >>> import numpy, math
175 >>> L_nu = 3631e-23 * (4. * math.pi * (10*cc.pc_cm)**2)
176 >>> nu = magnitudes.nu_lambda(1216.)
177 >>> L_lambda, lamb = magnitudes.f_nu_lambda(L_nu, nu)
178 >>> mAB, MAB = magnitudes.magnitude_AB(None, L_lambda, 1216., **fidcosmo)
179 >>> "%.3f" % numpy.abs(MAB)
180 '0.000'
181
182 Find the apparent (and absolute, which should be zero) magnitudes
183 of a 3631 Jy source at z=6.0:
184
185 >>> from cosmolopy import fidcosmo, magnitudes, cc, cd
186 >>> import numpy, math
187 >>> L_nu = 3631e-23 * (4. * math.pi * (10*cc.pc_cm)**2)
188 >>> nu = magnitudes.nu_lambda(1216.)
189 >>> L_lambda, lamb = magnitudes.f_nu_lambda(L_nu, nu)
190 >>> dl = cd.luminosity_distance(6.0, **fidcosmo)
191 >>> f_lambda = L_lambda/(4. * math.pi * (dl*cc.Mpc_cm)**2 * (1. + 6.0))
192 >>> mAB, MAB = magnitudes.magnitude_AB(6.0, f_lambda, 7.*1216., **fidcosmo)
193 >>> "%.3f, %.3f" % (mAB, MAB)
194 '48.865, 0.000'
195
196 """
197
198
199 if z is None:
200 mu = 0.0
201 z = 0
202 f_lambda = f_lambda / (4. * math.pi * (10. * cc.pc_cm)**2.)
203 else:
204 mu = distance_modulus(z, **cosmo)
205
206
207 f_rest = f_lambda * (1+z)
208
209
210 lambda_rest = wavelength/(1+z)
211 nu_rest = cc.c_light_cm_s / (lambda_rest * cc.angstrom_cm)
212
213
214 nu_0 = cc.c_light_cm_s / (wavelength * cc.angstrom_cm)
215
216
217 ab_app = -2.5 * numpy.log10(f_rest * (lambda_rest / nu_rest)) - MAB0
218
219
220 ab_abs = ab_app - mu
221
222 return ab_app, ab_abs
223
225 """Extrapolate to the AB magnitude at 1450 Angstroms.
226
227 Inputs
228 ------
229
230 z: array or scalar
231 the redshift of the source
232
233 f_lambda: array or scalar
234 observed flux from the source in units of erg s^-1 cm^-2 Ang^-1
235
236 wavelength: array or scalar
237 the observed wavelength of the flux measurement(s) in Angstroms.
238
239 nu_power:
240 the powerlaw index (f_nu ~ nu^nu_power) used to extrapolate
241 the flux to 1450 Angstroms.
242
243 Returns
244 -------
245
246 Apparent and absolute magnitudes extrapolated to 1450 Angstroms.
247
248
249 Notes
250 -----
251
252 Follows Fan et al. 2003:
253
254 We extrapolate the continuum to rest-frame 1450A, assuming a
255 continuum shape f_nu ~ nu^-0.5 to calculate AB_1450.
256
257 Usage
258 -----
259
260 Find the apparent and absolute rest-frame 1450 Angstrom magnitudes
261 of source with a flux of 3631 Jy at rest-frame 1216 Angstroms at
262 z=6.0:
263
264
265 >>> from cosmolopy import fidcosmo, magnitudes, cc, cd
266 >>> import numpy, math
267 >>> L_nu = 3631e-23 * (4. * math.pi * (10*cc.pc_cm)**2)
268 >>> nu = magnitudes.nu_lambda(1216.)
269 >>> L_lambda, lamb = magnitudes.f_nu_lambda(L_nu, nu)
270 >>> dl = cd.luminosity_distance(6.0, **fidcosmo)
271 >>> f_lambda = L_lambda/(4. * math.pi * (dl*cc.Mpc_cm)**2 * (1. + 6.0))
272 >>> mAB, MAB = magnitudes.magnitude_AB1450(6.0, f_lambda, 7.*1216.,
273 ... **fidcosmo)
274 >>> "%.3f, %.3f" % (mAB, MAB)
275 '48.769, -0.096'
276
277 And is that offset from an absolute magnitude of zero consisten
278 with our assumed powerlaw index?
279
280 >>> "%.3f" %(-2.5 * numpy.log10((1216./1450)**0.5))
281 '0.096'
282
283 """
284
285
286 f_rest = f_lambda * (1+z)
287
288
289 lambda_rest = wavelength/(1+z)
290 nu_rest = cc.c_light_cm_s / (lambda_rest * cc.angstrom_cm)
291
292
293 f_nu_rest = f_rest * (lambda_rest / nu_rest)
294
295 nu_1450 = cc.c_light_cm_s / (1450 * cc.angstrom_cm)
296 f_nu_1450 = f_nu_rest * (nu_1450/nu_rest)**nu_power
297
298
299 ab_app = -2.5 * numpy.log10(f_nu_1450) - MAB0
300
301
302 mu = distance_modulus(z, **cosmo)
303
304
305 ab_abs = ab_app - mu
306
307 return ab_app, ab_abs
308
309 if __name__ == "__main__":
310 import doctest
311 doctest.testmod()
312