redshifting.py 2.79 KB
Newer Older
Yannick Roehlly's avatar
Yannick Roehlly committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# -*- coding: utf-8 -*-
# Copyright (C) 2014 Yannick Roehlly
# Licensed under the CeCILL-v2 licence - see Licence_CeCILL_V2-en.txt
# Author: Yannick Roehlly

"""
Redshifting module
==================

This module implements the redshifting of a SED. The SED must be rest-frame
or the module will raise en exception when processing it.

Note that this module, contrary to the other SED creation modules, actually
changes the individual luminosity contributions as it redshifts everyone.
Also note that doing this, this module does not rely on the SED object
interface but on its inner implementations. That means that if the SED object
is changed, this module may need to be adapted.

"""

from collections import OrderedDict
from ..creation_modules import CreationModule
23
from pcigale.sed.cosmology import cosmology
24
from scipy.constants import parsec
Yannick Roehlly's avatar
Yannick Roehlly committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42


class Redshifting(CreationModule):
    """Redshift a SED

    This module redshift a rest-frame SED. If the SED is already redshifted, an
    exception is raised.

    """

    parameter_list = OrderedDict([
        ("redshift", (
            "float",
            "Redshift to apply to the galaxy.",
            None
        ))
    ])

43
44
45
46
47
    def _init_code(self):
        """Compute the age of the Universe at a given redshift
        """
        self.redshift = float(self.parameters["redshift"])
        self.universe_age = cosmology.age(self.redshift).value * 1000.
48
49
50
51
52
53
        if self.redshift == 0.:
            self.luminosity_distance = 10. * parsec
        else:
            self.luminosity_distance = (
                cosmology.luminosity_distance(self.redshift).value * 1e6 *
                parsec)
54

Yannick Roehlly's avatar
Yannick Roehlly committed
55
56
57
58
59
    def process(self, sed):
        """Redshift the SED

        Parameters
        ----------
60
        sed: pcigale.sed.SED object
Yannick Roehlly's avatar
Yannick Roehlly committed
61
62
63

        """
        # If the SED is already redshifted, raise an error.
64
        if 'redshift' in sed.info.keys() and sed.info['redshift'] > 0.:
65
            raise Exception("The SED is already redshifted <z={}>."
66
                            .format(sed.info['redshift']))
Yannick Roehlly's avatar
Yannick Roehlly committed
67
68
69

        # Raise an error when applying a negative redshift. This module is
        # not for blue-shifting.
70
        if self.redshift < 0:
71
            raise Exception("The redshift provided is negative <{}>."
72
                            .format(self.redshift))
Yannick Roehlly's avatar
Yannick Roehlly committed
73
74

        # We redshift directly the SED wavelength grid
75
        sed.wavelength_grid *= 1. + self.redshift
Yannick Roehlly's avatar
Yannick Roehlly committed
76
77

        # We modify each luminosity contribution to keep energy constant
78
        sed.luminosities /= 1. + self.redshift
Yannick Roehlly's avatar
Yannick Roehlly committed
79

80
        sed.add_info("redshift", self.redshift)
81
        sed.add_info("universe.luminosity_distance", self.luminosity_distance)
82
        sed.add_info("universe.age", self.universe_age)
Yannick Roehlly's avatar
Yannick Roehlly committed
83
84
85
86
        sed.add_module(self.name, self.parameters)

# CreationModule to be returned by get_module
Module = Redshifting