workers.py 2.12 KB
Newer Older
1 2 3 4 5 6 7 8 9
# -*- coding: utf-8 -*-
# Copyright (C) 2013 Centre de données Astrophysiques de Marseille
# Copyright (C) 2013-2014 Institute of Astronomy
# Copyright (C) 2014 Yannick Roehlly <yannick@iaora.eu>
# Licensed under the CeCILL-v2 licence - see Licence_CeCILL_V2-en.txt
# Author: Yannick Roehlly & Médéric Boquien

import numpy as np

10
from ...warehouse import SedWarehouse
11

Médéric Boquien's avatar
Médéric Boquien committed
12

13
def init_fluxes(models, counter):
14 15 16 17 18 19
    """Initializer of the pool of processes. It is mostly used to convert
    RawArrays into numpy arrays. The latter are defined as global variables to
    be accessible from the workers.

    Parameters
    ----------
20 21
    models: ModelsManagers
        Manages the storage of the computed models (fluxes and properties).
22 23
    counter: Counter class object
        Counter for the number of models computed
24 25

    """
26
    global gbl_warehouse, gbl_models, gbl_obs, gbl_save, gbl_counter
27

28
    gbl_warehouse = SedWarehouse()
29

30 31 32
    gbl_models = models
    gbl_obs = models.obs
    gbl_save = models.conf['analysis_params']['save_sed']
33
    gbl_counter = counter
34

35

36
def fluxes(idx, midx):
37 38 39 40 41 42
    """Worker process to retrieve a SED and affect the relevant data to shared
    RawArrays.

    Parameters
    ----------
    idx: int
43
        Index of the model within the current block of models.
44 45

    """
46
    sed = gbl_warehouse.get_sed(gbl_models.params.modules,
47
                                gbl_models.params.from_index(midx))
48

49
    if 'sfh.age' in sed.info and sed.info['sfh.age'] > sed.info['universe.age']:
50
        for band in gbl_models.flux:
51
            gbl_models.flux[band][idx] = np.nan
52
        for prop in gbl_models.extprop:
53
            gbl_models.extprop[prop][idx] = np.nan
54
        for prop in gbl_models.intprop:
55
            gbl_models.intprop[prop][idx] = np.nan
56
    else:
57
        for band in gbl_models.flux:
58
            gbl_models.flux[band][idx] = sed.compute_fnu(band)
59
        for prop in gbl_models.extprop:
60
            gbl_models.extprop[prop][idx] = sed.info[prop]
61
        for prop in gbl_models.intprop:
62
            gbl_models.intprop[prop][idx] = sed.info[prop]
63

64
    if gbl_save is True:
65
        sed.to_fits(f"out/{midx}")
66

67
    gbl_counter.inc()