sfhfromfile.py 3.39 KB
Newer Older
1
# -*- coding: utf-8 -*-
2
3
# Copyright (C) 2013 Centre de données Astrophysiques de Marseille
# Licensed under the CeCILL-v2 licence - see Licence_CeCILL_V2-en.txt
Yannick Roehlly's avatar
Yannick Roehlly committed
4
# Author: Yannick Roehlly
5

Yannick Roehlly's avatar
Yannick Roehlly committed
6
7
8
9
10
11
12
13
"""
Read star formation history from file module
============================================

This module reads the star formation history in a file.

"""

14
from collections import OrderedDict
15

16
import numpy as np
17

18
from ..utils.io import read_table
19
from . import SedModule
20
21


22
class SfhFromFile(SedModule):
23
24
25
    """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
26
    VO-Table file. The first column must contain the time values (in Myr) and
27
28
29
30
31
32
    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.

    """

33
    parameter_list = OrderedDict([
34
        ("filename", (
35
            "string()",
36
            "Name of the file containing the SFH. The first column must be "
37
            "the time in Myr, starting from 0 with a step of 1 Myr. The other "
38
            "columns must contain the SFR in Msun/yr."
39
40
            "[Msun/yr].",
            None
41
42
        )),
        ("sfr_column", (
43
            "cigale_list(dtype=int)",
44
45
            "List of column indices of the SFR. The first SFR column has the "
            "index 1.",
46
            None
47
48
        )),
        ("age", (
49
            "cigale_list(dtype=int, minvalue=0.)",
50
            "Age in Myr at which the SFH will be looked at.",
51
            None
52
53
        )),
        ("normalise", (
54
            "boolean()",
55
            "Normalise the SFH to one solar mass produced at the given age.",
56
            True
57
58
        ))
    ])
59

60
    def _init_code(self):
61
        filename = self.parameters['filename']
62
63
        age = int(self.parameters['age'])
        self.sfr_column_number = int(self.parameters['sfr_column'])
64
65
66
67
        if type(self.parameters["normalise"]) is str:
            normalise = self.parameters["normalise"].lower() == 'true'
        else:
            normalise = bool(self.parameters["normalise"])
68

69
70
        table = read_table(filename)
        self.sfr = table.columns[self.sfr_column_number].data.astype(np.float)
71
72
        time_grid = table.columns[0].data.astype(np.int)
        if time_grid[0] != 0:
73
            raise Exception("The time grid must start from 0.")
74
        if np.all(time_grid[1:]-time_grid[:-1] == 1) == False:
75
76
            raise Exception("The time step must be 1 Myr. Computed models will"
                            " be wrong.")
77
78

        # We cut the SFH to the desired age.
79
80
        self.sfr = self.sfr[time_grid <= age]
        time_grid = time_grid[time_grid <= age]
81

82
83
        # Compute the galaxy mass and normalise the SFH to 1 solar mass
        # produced if asked to.
84
        self.sfr_integrated = np.sum(self.sfr) * 1e6
85
        if normalise:
86
87
88
89
90
91
92
93
94
95
96
97
            self.sfr /= self.sfr_integrated
            self.sfr_integrated = 1.

    def process(self, sed):
        """Add the SFH read from the file.

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

        """
98

99
        sed.add_module(self.name, self.parameters)
100
        sed.sfh = self.sfr
101
102
        sed.add_info("sfh.integrated", self.sfr_integrated, True)
        sed.add_info("sfh.index", self.sfr_column_number)
Yannick Roehlly's avatar
Yannick Roehlly committed
103

104

105
# SedModule to be returned by get_module
Yannick Roehlly's avatar
Yannick Roehlly committed
106
Module = SfhFromFile