Commit 581f0980 authored by Médéric Boquien's avatar Médéric Boquien

Add an option to save the generated SEDs with savefluxes.

parent 2dafd027
......@@ -51,6 +51,11 @@ class SaveFluxes(AnalysisModule):
"Name of the output file.",
"computed_fluxes.txt"
)),
("save_sed", (
"boolean",
"If True, save the generated SED.",
"False"
)),
("output_format", (
"string",
"Format of the output file. Any format supported by astropy.table "
......@@ -90,7 +95,7 @@ class SaveFluxes(AnalysisModule):
out_file = parameters["output_file"]
out_format = parameters["output_format"]
save_sed = parameters["save_sed"].lower() == "true"
# Get the needed filters in the pcigale database. We use an ordered
# dictionary because we need the keys to always be returned in the
......@@ -132,8 +137,8 @@ class SaveFluxes(AnalysisModule):
n_params * n_info),
(n_params, n_info))
initargs = (params, filters, model_fluxes, model_parameters,
time.time(), mp.Value('i', 0))
initargs = (params, filters, save_sed, model_fluxes,
model_parameters, time.time(), mp.Value('i', 0))
if cores == 1: # Do not create a new process
init_worker_fluxes(*initargs)
for idx in range(n_params):
......
......@@ -11,8 +11,9 @@ import numpy as np
from ...warehouse import SedWarehouse
from ..utils import OUT_DIR
def init_fluxes(params, filters, fluxes, info, t_begin, n_computed):
def init_fluxes(params, filters, save_sed, fluxes, info, t_begin, n_computed):
"""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.
......@@ -23,6 +24,8 @@ def init_fluxes(params, filters, fluxes, info, t_begin, n_computed):
Handles the parameters from a 1D index.
filters: OrderedDict
Contains filters to compute the fluxes.
save_sed: boolean
Indicates whether the SED should be saved.
fluxes: RawArray and tuple containing the shape
Fluxes of individual models. Shared among workers.
n_computed: Value
......@@ -32,7 +35,8 @@ def init_fluxes(params, filters, fluxes, info, t_begin, n_computed):
"""
global gbl_model_fluxes, gbl_model_info, gbl_n_computed, gbl_t_begin
global gbl_params, gbl_previous_idx, gbl_filters, gbl_warehouse
global gbl_params, gbl_previous_idx, gbl_filters, gbl_save_sed
global gbl_warehouse
gbl_model_fluxes = np.ctypeslib.as_array(fluxes[0])
gbl_model_fluxes = gbl_model_fluxes.reshape(fluxes[1])
......@@ -49,6 +53,8 @@ def init_fluxes(params, filters, fluxes, info, t_begin, n_computed):
gbl_filters = filters
gbl_save_sed = save_sed
gbl_warehouse = SedWarehouse()
......@@ -72,6 +78,9 @@ def fluxes(idx):
sed = gbl_warehouse.get_sed(gbl_params.modules,
gbl_params.from_index(idx))
if gbl_save_sed == True:
sed.to_votable(OUT_DIR + "{}_best_model.xml".format(idx))
if 'age' in sed.info and sed.info['age'] > sed.info['universe.age']:
model_fluxes = -99. * np.ones(len(gbl_filters))
else:
......
......@@ -171,4 +171,7 @@ def save_fluxes(model_fluxes, model_parameters, filters, names, filename,
out_table = Table(np.hstack((out_fluxes, out_params)),
names=list(filters.keys()) + list(names))
out_table.add_column(Column(np.arange(model_fluxes[1][0]), name='id'),
index=0)
out_table.write("{}/{}".format(directory, filename), format=out_format)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment