models.py 2.85 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
# -*- coding: utf-8 -*-
# Copyright (C) 2017 Universidad de Antofagasta
# Licensed under the CeCILL-v2 licence - see Licence_CeCILL_V2-en.txt
# Author: Médéric Boquien

"""This class manages the handling of models in the code. It contains the
fluxes and the physical properties and all the necessary information to
compute them, such as the configuration, the observations, and the parameters
of the models.
"""

from astropy.table import Table, Column

14
from .utils import SharedArray, get_info
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29


class ModelsManager(object):
    """A ModelsManager contains the fluxes and the properties of all the
    models. In addition it contains all the information to understand how the
    models have been computed, what block of the grid of models they correspond
    to with the ability to easily recompute a model, what the bands are, what
    are the names of the properties, etc.

    """
    def __init__(self, conf, obs, params, iblock=0):
        self.conf = conf
        self.obs = obs
        self.params = params
        self.block = params.blocks[iblock]
30
        self.iblock = iblock
31
32
        self.allpropnames, self.allextpropnames = get_info(self)
        self.allintpropnames = set(self.allpropnames) - self.allextpropnames
33

34
35
        props_nolog = set([prop[:-4] if prop.endswith('log') else prop
                           for prop in conf['analysis_params']['variables']])
36
        self.intpropnames = (self.allintpropnames & set(obs.intprops) |
37
                             self.allintpropnames & props_nolog)
38
        self.extpropnames = (self.allextpropnames & set(obs.extprops) |
39
                             self.allextpropnames & props_nolog)
40
        size = len(params.blocks[iblock])
41

42
43
44
        self.flux = {band: SharedArray(size) for band in obs.bands}
        self.intprop = {prop: SharedArray(size) for prop in self.intpropnames}
        self.extprop = {prop: SharedArray(size) for prop in self.extpropnames}
45
46
47
48
49
50
51
52
53
54

    def save(self, filename):
        """Save the fluxes and properties of all the models into a table.

        Parameters
        ----------
        filename: str
            Root of the filename where to save the data.

        """
55
56
57
        table = Table()
        table.add_column(Column(self.block, name='id'))
        for band in sorted(self.flux.keys()):
58
59
60
61
            if band.startswith('line.'):
                unit = 'W/m^2'
            else:
                unit = 'mJy'
62
            table.add_column(Column(self.flux[band], name=band,
63
                                    unit=unit))
64
        for prop in sorted(self.extprop.keys()):
65
            table.add_column(Column(self.extprop[prop], name=prop))
66
        for prop in sorted(self.intprop.keys()):
67
            table.add_column(Column(self.intprop[prop], name=prop))
68
69
70
71

        table.write("out/{}.fits".format(filename))
        table.write("out/{}.txt".format(filename), format='ascii.fixed_width',
                    delimiter=None)