models.py 3.26 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
# -*- 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.
"""

12 13
import ctypes

14 15
from astropy.table import Table, Column

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


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]
32
        self.iblock = iblock
33 34
        self.allpropnames, self.allextpropnames = get_info(self)
        self.allintpropnames = set(self.allpropnames) - self.allextpropnames
35

36 37
        props_nolog = set([prop[:-4] if prop.endswith('log') else prop
                           for prop in conf['analysis_params']['variables']])
38
        self.intpropnames = (self.allintpropnames & set(obs.intprops) |
39
                             self.allintpropnames & props_nolog)
40
        self.extpropnames = (self.allextpropnames & set(obs.extprops) |
41
                             self.allextpropnames & props_nolog)
42 43 44 45
        if 'bands' in conf['analysis_params']:
            bandnames = set(obs.bands+conf['analysis_params']['bands'])
        else:
            bandnames = obs.bands
46

47
        size = len(params.blocks[iblock])
48 49 50
        if conf['parameters_file'] is "":
            self.nz = len(conf['sed_modules_params']['redshifting']['redshift'])
            self.nm = size // self.nz
51

52
        self.flux = {band: SharedArray(size) for band in bandnames}
53 54
        self.intprop = {prop: SharedArray(size) for prop in self.intpropnames}
        self.extprop = {prop: SharedArray(size) for prop in self.extpropnames}
55
        self.index = SharedArray(size, ctypes.c_uint32)
56 57 58 59 60 61 62 63 64 65

    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.

        """
66 67 68
        table = Table()
        table.add_column(Column(self.block, name='id'))
        for band in sorted(self.flux.keys()):
69
            if band.startswith('line.') or band.startswith('linefilter.'):
70 71 72
                unit = 'W/m^2'
            else:
                unit = 'mJy'
73
            table.add_column(Column(self.flux[band], name=band,
74
                                    unit=unit))
75
        for prop in sorted(self.extprop.keys()):
76
            table.add_column(Column(self.extprop[prop], name=prop))
77
        for prop in sorted(self.intprop.keys()):
78
            table.add_column(Column(self.intprop[prop], name=prop))
79

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