utils.py 6.28 KB
Newer Older
Yannick Roehlly's avatar
Yannick Roehlly committed
1
2
# -*- coding: utf-8 -*-
"""
Yannick Roehlly's avatar
Yannick Roehlly committed
3
Copyright (C) 2012, 2013 Centre de données Astrophysiques de Marseille
Yannick Roehlly's avatar
Yannick Roehlly committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Licensed under the CeCILL-v2 licence - see Licence_CeCILL_V2-en.txt

@author: Yannick Roehlly <yannick.roehlly@oamp.fr>
@author: Médéric Boquien <mederic.boquien@oamp.fr>

"""


import numpy as np
from scipy import integrate
from scipy.constants import c, pi, parsec


def lambda_to_nu(wavelength):
    """Convert wavelength (nm) to frequency (Hz)

Yannick Roehlly's avatar
Yannick Roehlly committed
20
    Parameters
Yannick Roehlly's avatar
Yannick Roehlly committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
    ----------
    wavelength : float or array of floats
        The wavelength(s) in nm.

    Returns
    -------
    nu : float or array of floats
        The frequency(ies) in Hz.

    """
    return c / (wavelength * 1.e-9)


def nu_to_lambda(frequency):
    """Convert frequency (Hz) to wavelength (nm)

Yannick Roehlly's avatar
Yannick Roehlly committed
37
    Parameters
Yannick Roehlly's avatar
Yannick Roehlly committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
    ----------
    frequency : float or numpy.array of floats
        The frequency(ies) in Hz.

    Returns
    -------
    wavelength : float or numpy.array of floats
        The wavelength(s) in nm.

    """
    return 1.e-9 * c / frequency


def best_grid(wavelengths1, wavelengths2):
    """
    Return the best wavelength grid to regrid to arrays

Yannick Roehlly's avatar
Yannick Roehlly committed
55
    Considering the two wavelength grids passed in parameters, this function
Yannick Roehlly's avatar
Yannick Roehlly committed
56
57
58
    compute the best new grid that will be used to regrid the two spectra
    before combining them.

Yannick Roehlly's avatar
Yannick Roehlly committed
59
    Parameters
Yannick Roehlly's avatar
Yannick Roehlly committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
    ----------
    wavelengths1, wavelengths2 : array of floats
        The wavelength grids to be 'regrided'.

    Returns
    -------
    new_grid : array of floats
        Array containing all the wavelengths found in the input arrays.

    """
    new_grid = np.hstack((wavelengths1, wavelengths2))
    new_grid.sort()
    new_grid = np.unique(new_grid)

    return new_grid


def luminosity_distance(z, h0=71., omega_m=0.27, omega_l=0.73):
    """
    Computes luminosity distance at redshift z in Mpc for given Λ cosmology
    (H_0 in (km/s)/Mpc, Ω_M, and Ω_Λ) Ref.: Hogg (1999) astro-ph/9905116

Yannick Roehlly's avatar
Yannick Roehlly committed
82
    Parameters
Yannick Roehlly's avatar
Yannick Roehlly committed
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
    ----------
    z : float
        Redshift
    h0 : float
        Hubble's constant
    omega_m : float
        Omega matter.
    omega_l : float
        Omega vacuum

    Returns
    -------
    luminosity_distance : float
        The luminosity distance in Mpc.

    """

    omega_k = 1. - omega_m - omega_l

    if z > 0.:
        dist, edist = integrate.quad(
            lambda x: (omega_m * (1. + x) ** 3
                       + omega_k * (1 + x) ** 2 + omega_l) ** (-.5),
            0.,
            z,
            epsrel=1e-3)
    else:
        # Bad idea as there is something *wrong* going on
        print('LumDist: z <= 0 -> Assume z = 0!')
        z = 0.
        dist = 0.

    if omega_k > 0.:
        dist = np.sinh(dist * np.sqrt(omega_k)) / np.sqrt(omega_k)
    elif omega_k < 0.:
        dist = np.sin(dist * np.sqrt(-omega_k)) / np.sqrt(-omega_k)

    return c / (h0 * 1.e3) * (1. + z) * dist


def luminosity_to_flux(luminosity, redshift=0):
    """
    Convert a luminosity (or luminosity density) to a flux (or flux density).

    F = L / (4πDl2)

Yannick Roehlly's avatar
Yannick Roehlly committed
129
    Parameters
Yannick Roehlly's avatar
Yannick Roehlly committed
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
    ----------
    luminosity : float or array of floats
        Luminosity (typically in W) or luminosity density (W/nm or W/Hz).
    redshift :
        Redshift. If redshift is 0 (the default) the flux at a luminosity
        distance of 10 pc is returned.

    Returns
    -------
    flux : float or array of floats
        The flux (typically in W/m²) of flux density (W/m²/nm or W/m²/Hz).

    """
    if redshift == 0:
        dist = 10 * parsec
    else:
        dist = luminosity_distance(redshift) * 1.e6 * parsec

    return luminosity / (4 * pi * np.square(dist))


Yannick Roehlly's avatar
Yannick Roehlly committed
151
def lambda_flambda_to_fnu(wavelength, flambda):
Yannick Roehlly's avatar
Yannick Roehlly committed
152
153
154
    """
    Convert a Fλ vs λ spectrum to Fν vs λ

Yannick Roehlly's avatar
Yannick Roehlly committed
155
    Parameters
Yannick Roehlly's avatar
Yannick Roehlly committed
156
    ----------
Yannick Roehlly's avatar
Yannick Roehlly committed
157
158
159
160
    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).
Yannick Roehlly's avatar
Yannick Roehlly committed
161
162
163

    Returns
    -------
Yannick Roehlly's avatar
Yannick Roehlly committed
164
165
166
    fnu : array of floats
        The Fν flux density in mJy (or the Lν luminosity density in
        1.e-29 W/Hz).
Yannick Roehlly's avatar
Yannick Roehlly committed
167
168

    """
Yannick Roehlly's avatar
Yannick Roehlly committed
169
170
171
172
    wavelength = np.array(wavelength, dtype=float)
    flambda = np.array(flambda, dtype=float)

    # Factor 1e+29 is to switch from W/m²/Hz to mJy
Yannick Roehlly's avatar
Yannick Roehlly committed
173
    # Factor 1e-9 is to switch from nm to m (only one because the other nm
Yannick Roehlly's avatar
Yannick Roehlly committed
174
175
    # wavelength goes with the Fλ in W/m²/nm).
    fnu = 1e+29 * 1e-9 * flambda * wavelength * wavelength / c
Yannick Roehlly's avatar
Yannick Roehlly committed
176

Yannick Roehlly's avatar
Yannick Roehlly committed
177
    return fnu
Yannick Roehlly's avatar
Yannick Roehlly committed
178
179


Yannick Roehlly's avatar
Yannick Roehlly committed
180
def lambda_fnu_to_flambda(wavelength, fnu):
Yannick Roehlly's avatar
Yannick Roehlly committed
181
182
183
    """
    Convert a Fν vs λ spectrum to Fλ vs λ

Yannick Roehlly's avatar
Yannick Roehlly committed
184
    Parameters
Yannick Roehlly's avatar
Yannick Roehlly committed
185
    ----------
Yannick Roehlly's avatar
Yannick Roehlly committed
186
187
188
189
190
    wavelength : list-like of floats
        The wavelengths in nm.
    fnu : list-like of floats
        The Fν flux density in mJy (of the  Lν luminosity density in
        1.e-29 W/Hz).
Yannick Roehlly's avatar
Yannick Roehlly committed
191
192
193

    Returns
    -------
Yannick Roehlly's avatar
Yannick Roehlly committed
194
195
    flambda : array of floats
        Fλ flux density in W/m²/nm (or Lλ luminosity density in W/nm).
Yannick Roehlly's avatar
Yannick Roehlly committed
196
197

    """
Yannick Roehlly's avatar
Yannick Roehlly committed
198
199
    wavelength = np.array(wavelength, dtype=float)
    fnu = np.array(fnu, dtype=float)
Yannick Roehlly's avatar
Yannick Roehlly committed
200

Yannick Roehlly's avatar
Yannick Roehlly committed
201
202
203
    # 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
Yannick Roehlly's avatar
Yannick Roehlly committed
204

Yannick Roehlly's avatar
Yannick Roehlly committed
205
    return flambda
Yannick Roehlly's avatar
Yannick Roehlly committed
206
207


Yannick Roehlly's avatar
Yannick Roehlly committed
208
209
def redshift_spectrum(wavelength, flux, redshift, is_fnu=False):
    """Redshit a spectrum
Yannick Roehlly's avatar
Yannick Roehlly committed
210

Yannick Roehlly's avatar
Yannick Roehlly committed
211
    Parameters
Yannick Roehlly's avatar
Yannick Roehlly committed
212
    ----------
Yannick Roehlly's avatar
Yannick Roehlly committed
213
214
215
216
217
218
    wavelength : array like of floats
        The wavelength in nm.
    flux : array like of floats
        The flux or luminosity density.
    redshift : float
        The redshift.
Yannick Roehlly's avatar
Yannick Roehlly committed
219
    is_fnu : boolean
Yannick Roehlly's avatar
Yannick Roehlly committed
220
221
222
        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).
Yannick Roehlly's avatar
Yannick Roehlly committed
223
224
225

    Results
    -------
Yannick Roehlly's avatar
Yannick Roehlly committed
226
227
228
    wavelength, flux : tuple of numpy arrays of floats
        The redshifted spectrum with the same kind of flux (or luminosity)
        density as the input.
Yannick Roehlly's avatar
Yannick Roehlly committed
229
230

    """
Yannick Roehlly's avatar
Yannick Roehlly committed
231
232
233
    wavelength = np.array(wavelength, dtype=float)
    flux = np.array(flux, dtype=float)
    redshift = float(redshift)
Yannick Roehlly's avatar
Yannick Roehlly committed
234

Yannick Roehlly's avatar
Yannick Roehlly committed
235
236
237
238
    if redshift < 0:
        redshift_factor = 1. / (1. - redshift)
    else:
        redshift_factor = 1. + redshift
Yannick Roehlly's avatar
Yannick Roehlly committed
239

Yannick Roehlly's avatar
Yannick Roehlly committed
240
241
242
    if is_fnu:
        # Switch to Fλ
        flux = lambda_fnu_to_flambda(wavelength, flux)
Yannick Roehlly's avatar
Yannick Roehlly committed
243

Yannick Roehlly's avatar
Yannick Roehlly committed
244
245
    wavelength *= redshift_factor
    flux /= redshift_factor
Yannick Roehlly's avatar
Yannick Roehlly committed
246

Yannick Roehlly's avatar
Yannick Roehlly committed
247
248
249
    if is_fnu:
        # Switch back to Fλ
        flux = lambda_flambda_to_fnu(wavelength, flux)
Yannick Roehlly's avatar
Yannick Roehlly committed
250

Yannick Roehlly's avatar
Yannick Roehlly committed
251
    return wavelength, flux