sfhfromfile.py 2.37 KB
Newer Older
1
# -*- coding: utf-8 -*-
2
3
4
# Copyright (C) 2013 Centre de données Astrophysiques de Marseille
# Licensed under the CeCILL-v2 licence - see Licence_CeCILL_V2-en.txt
# Author: Yannick Roehlly <yannick.roehlly@oamp.fr>
5
6
7

import atpy
import numpy as np
8
from collections import OrderedDict
9
10
11
12
13
14
15
from . import common


class Module(common.SEDCreationModule):
    """Module reading the SFH from a file

    This module is used to read the Star Formation Histories from a FITS or
Yannick Roehlly's avatar
Yannick Roehlly committed
16
    VO-Table file. The first column must contain the time values (in Myr) and
17
18
19
20
21
22
    each other column may contain the Star Formation Rates (in solar mass per
    year) corresponding. Each SFR may be cut and normalised to 1 solar mass
    produced at the desired age.

    """

23
24
    parameter_list = OrderedDict([
        ("filename", (
25
26
            "str",
            "Name of the file containing the SFH. The first column must be "
Yannick Roehlly's avatar
Yannick Roehlly committed
27
            "the time [Myr] and the other column must contain the SFR "
28
29
            "[Msun/yr].",
            None
30
31
        )),
        ("sfr_column", (
32
            "integer",
33
34
35
            "List of column numbers where the star formation rates will "
            "be read..",
            None
36
37
        )),
        ("age", (
38
39
            "integer",
            "Age [Myr] where each SFH will be looked at.",
40
            None
41
42
        ))
    ])
43

44
    def process(self, sed):
45
46
47
48
49
50
51
52
        """Add the SFH read from the file.

        Parameters
        ----------
        sed  : pcigale.sed.SED object
        parameters : dictionary containing the parameters

        """
53
        filename = self.parameters['filename']
54
55
56
57
58
59
        table = atpy.Table(filename, verbose=False)

        time_column_name = table.columns.keys[0]
        time_grid = table[time_column_name]

        # -1 because Python indexes start to 0.
60
        sfr_column_number = int(self.parameters['sfr_column']) - 1
61
62
63
        sfr_column_name = table.columns.keys[sfr_column_number]
        sfr = table[sfr_column_name]

64
        age = int(self.parameters['age'])
65
66
67
68
69
70

        # We cut the SFH to the desired age.
        sfr = sfr[time_grid <= age]
        time_grid = time_grid[time_grid <= age]

        # The we normalise it to 1 solar mass produced.
Yannick Roehlly's avatar
Yannick Roehlly committed
71
        sfr = sfr / np.trapz(sfr * 1.e6, time_grid)
72

73
        sed.add_module(self.name, self.parameters)
74
        sed.sfh = (time_grid, sfr)
75
76
        sed.add_info("age", age)
        sed.add_info("sfh_id" + self.postfix, sfr_column_name)