-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_unit.py
229 lines (173 loc) · 5.94 KB
/
utils_unit.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
'''some of the functions are borrowed from cigale.
'''
import numpy as np
from scipy.constants import c, pi
from astropy import units as u
def lambda_flambda_to_fnu(wavelength, flambda):
"""
Convert a Fλ vs λ spectrum to Fν vs λ
Parameters
----------
wavelength: list-like of floats
The wavelengths in nm.
flambda: list-like of floats
Fλ flux density in W/m²/nm (or Lλ luminosity density in W/nm).
Returns
-------
fnu: array of floats
The Fν flux density in mJy (or the Lν luminosity density in
1.e-29 W/Hz).
"""
wavelength = np.array(wavelength, dtype=float)
flambda = np.array(flambda, dtype=float)
# Factor 1e+29 is to switch from W/m²/Hz to mJy
# Factor 1e-9 is to switch from nm to m (only one because the other nm
# wavelength goes with the Fλ in W/m²/nm).
fnu = 1e+29 * 1e-9 * flambda * wavelength * wavelength / c
return fnu
def lambda_flambda_to_fnu_aa_maggies(wavelength, flambda):
"""
wavelength in angstrom; flambda in erg/cm²/s/A
Returns
-------
fnu: array of floats
Fν flux density in maggies
"""
_flam = flambda*u.erg/u.cm**2/u.second/u.angstrom
fnu = lambda_flambda_to_fnu(wavelength*u.angstrom.to(u.nanometer), _flam.to(u.watt/u.meter**2/u.nanometer))
fnu = fnu * u.millijansky.to(u.jansky)
fnu = fnu/3631
return fnu
def lambda_fnu_to_flambda(wavelength, fnu):
"""
Convert a Fν vs λ spectrum to Fλ vs λ
Parameters
----------
wavelength: list-like of floats
The wavelengths in nm.
fnu: list-like of floats
The Fν flux density in mJy (or the Lν luminosity density in
1.e-29 W/Hz).
Returns
-------
flambda: array of floats
Fλ flux density in W/m²/nm (or Lλ luminosity density in W/nm).
"""
wavelength = np.array(wavelength, dtype=float)
fnu = np.array(fnu, dtype=float)
# Factor 1e-29 is to switch from Jy to W/m²/Hz
# Factor 1e+9 is to switch from m to nm
flambda = 1e-29 * 1e+9 * fnu / (wavelength * wavelength) * c
return flambda
def lambda_fnu_to_flambda_aa_maggies(wavelength, fnu):
"""
wavelength in angstrom; fnu in maggies
Returns
-------
flambda: array of floats
Fλ flux density in erg/cm²/s/A
"""
flambda = lambda_fnu_to_flambda(wavelength*u.angstrom.to(u.nanometer), fnu*3631*u.jansky.to(u.millijansky))
flambda = flambda * u.watt/u.meter**2/u.nanometer
flambda = flambda.to(u.erg/u.cm**2/u.second/u.angstrom)
return flambda.value
def redshift_spectrum(wavelength, flux, redshift, is_fnu=False):
"""Redshit a spectrum
Parameters
----------
wavelength: array like of floats
The wavelength in nm.
flux: array like of floats
The flux or luminosity density.
redshift: float
The redshift.
is_fnu: boolean
If false (default) the flux is a Fλ density in W/m²/nm (or a Lλ
luminosity density in W/nm). If true, the flux is a Fν density in mJy
(or a Lν luminosity density in 1.e-29 W/Hz).
Results
-------
wavelength, flux: tuple of numpy arrays of floats
The redshifted spectrum with the same kind of flux (or luminosity)
density as the input.
"""
wavelength = np.array(wavelength, dtype=float)
flux = np.array(flux, dtype=float)
redshift = float(redshift)
redshift_factor = 1. + redshift
if is_fnu:
# Switch to Fλ
flux = lambda_fnu_to_flambda(wavelength, flux)
wavelength *= redshift_factor
flux /= redshift_factor
if is_fnu:
# Switch back to Fλ
flux = lambda_flambda_to_fnu(wavelength, flux)
return wavelength, flux
def redshift_spectrum_prosp_units(wavelength, flux, redshift, is_fnu=False):
"""Redshit a spectrum
Parameters
----------
wavelength: array like of floats
The wavelength in angstrom.
flux: array like of floats
Flux density.
redshift: float
The redshift.
is_fnu: boolean
If false (default) the flux is a Fλ density in erg/cm²/s/A.
If true, the flux is a Fν density in maggies.
Results
-------
wavelength, flux: tuple of numpy arrays of floats
The redshifted spectrum with the same kind of flux (or luminosity)
density as the input.
"""
wavelength = np.array(wavelength, dtype=float)
flux = np.array(flux, dtype=float)
redshift = float(redshift)
if redshift < 0:
redshift_factor = 1. / (1. - redshift)
else:
redshift_factor = 1. + redshift
if is_fnu:
# Switch to Fλ
flux = lambda_fnu_to_flambda_aa_maggies(wavelength, flux)
wavelength *= redshift_factor
flux /= redshift_factor
if is_fnu:
# Switch back to Fλ
flux = lambda_flambda_to_fnu_aa_maggies(wavelength, flux)
return wavelength, flux
def deredshift_spectrum_prosp_units(wavelength, flux, redshift, is_fnu=False):
"""Redshit a spectrum
Parameters
----------
wavelength: array like of floats
The wavelength in angstrom.
flux: array like of floats
Flux density.
redshift: float
The redshift.
is_fnu: boolean
If false (default) the flux is a Fλ density in erg/cm²/s/A.
If true, the flux is a Fν density in maggies.
Results
-------
wavelength, flux: tuple of numpy arrays of floats
The redshifted spectrum with the same kind of flux (or luminosity)
density as the input.
"""
wavelength = np.array(wavelength, dtype=float)
flux = np.array(flux, dtype=float)
redshift = float(redshift)
redshift_factor = 1. + redshift
if is_fnu:
# Switch to Fλ
flux = lambda_fnu_to_flambda_aa_maggies(wavelength, flux)
wavelength /= redshift_factor
flux *= redshift_factor
if is_fnu:
# Switch back to Fλ
flux = lambda_flambda_to_fnu_aa_maggies(wavelength, flux)
return wavelength, flux