sfhfromfile.py 3.15 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 import read_table
19
from . import CreationModule
20 21


Yannick Roehlly's avatar
Yannick Roehlly committed
22
class SfhFromFile(CreationModule):
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 36
            "str",
            "Name of the file containing the SFH. The first column must be "
37 38
            "the time in Myr, starting from 0 with a step of 1 Myr. The other
            "columns must contain the SFR in Msun/yr."
39 40
            "[Msun/yr].",
            None
41 42
        )),
        ("sfr_column", (
43
            "integer",
44 45
            "List of column indices of the SFR. The first SFR column has the "
            "index 1.",
46
            None
47 48
        )),
        ("age", (
49
            "integer",
50
            "Age in Myr at which the SFH will be looked at.",
51
            None
52 53 54 55 56
        )),
        ("normalise", (
            "boolean",
            "Normalise the SFH to one solar mass produced at the given age.",
            "True"
57 58
        ))
    ])
59

60
    def process(self, sed):
61 62 63 64
        """Add the SFH read from the file.

        Parameters
        ----------
65 66
        sed: pcigale.sed.SED object
        parameters: dictionary containing the parameters
67 68

        """
69
        filename = self.parameters['filename']
70
        table = read_table(filename)
71

72 73 74 75 76 77 78
        time_grid = table.columns[0].data.astype(np.int)

        if time_grid[0] != 0:
            raise Exception("The time grid must start from 0.")
        if np.all(time_grid[1:]-time_grid[:-1] == 1) == False:
            raise Exception("The time step must be 1 Myr. Computed models will "
                            "be wrong.")
79

80
        sfr_column_number = int(self.parameters['sfr_column'])
81
        sfr = table.columns[sfr_column_number].data.astype(np.float)
82

83
        age = int(self.parameters['age'])
84
        normalise = (self.parameters["normalise"].lower() == "true")
85 86 87 88 89

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

90 91
        # Compute the galaxy mass and normalise the SFH to 1 solar mass
        # produced if asked to.
92
        sfr_integrated = np.sum(sfr) * 1e6
93
        if normalise:
94 95
            sfr /= sfr_integrated
            sfr_integrated = 1.
96

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

# CreationModule to be returned by get_module
Module = SfhFromFile