vo.py 3.33 KB
Newer Older
1
2
3
4
5
6
7
8
# -*- coding: utf-8 -*-
# Copyright (C) 2014 Yannick Roehlly
# Licensed under the CeCILL-v2 licence - see Licence_CeCILL_V2-en.txt
# Author: Yannick Roehlly

from astropy.io.votable.tree import VOTableFile, Resource, Table, Field, Info


9
def save_sed_to_vo(sed, filename, norm=1.):
10
11
12
13
14
    """
    Save a SED object to a VO-Table file

    Parameters
    ----------
15
    sed: a pcigale.sed.SED object
16
        The SED to save.
17
    filename: string
18
        Name of the file to save the SED to.
19
    norm: float
20
        Normalisation factor of the SED
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

    """
    votable = VOTableFile()

    spectra_resource = Resource(id="Spectra")
    votable.resources.append(spectra_resource)

    # Total F_nu
    fnu_table = Table(votable, name="Fnu", id="Fnu")
    spectra_resource.tables.append(fnu_table)
    fnu_table.fields.extend([
        Field(votable, name="wavelength", datatype="double", unit="nm",
              ucd="em.wl"),
        Field(votable, name="F_nu", datatype="double", unit="mJy",
              ucd="phot.flux")
    ])
    fnu_table.create_arrays(len(sed.wavelength_grid))
    fnu_table.array["wavelength"] = sed.wavelength_grid
39
    fnu_table.array["F_nu"] = norm * sed.fnu
40

41
42
43
44
    # L_lambda contributions and total
    Llambda_table = Table(votable, name="Llambda", id="Llambda")
    spectra_resource.tables.append(Llambda_table)
    Llambda_fields = [
45
46
        Field(votable, name="wavelength", datatype="double", unit="nm",
              ucd="em.wl"),
47
        Field(votable, name="L_lambda_total", datatype="double", unit="W/nm",
48
49
              ucd="phot.flux")]
    for name in sed.contribution_names:
50
        Llambda_fields.append(Field(votable, name=name, datatype="double",
51
                                    unit="W/nm", ucd="phot.flux"))
52
53
54
55
    Llambda_table.fields.extend(Llambda_fields)
    Llambda_table.create_arrays(len(sed.wavelength_grid))
    Llambda_table.array["wavelength"] = sed.wavelength_grid
    Llambda_table.array["L_lambda_total"] = norm * sed.luminosity
56
    for name in sed.contribution_names:
57
        Llambda_table.array[name] = norm * sed.get_lumin_contribution(name)
58
59

    # SFH
60
61
62
63
64
65
66
    if sed.sfh is not None:
        sfh_resource = Resource(id="Star_Formation_History")
        votable.resources.append(sfh_resource)
        sfh_table = Table(votable, name="SFH", id="SFH")
        sfh_resource.tables.append(sfh_table)
        sfh_table.fields.extend([
            Field(votable, name="time", datatype="double", unit="Myr",
Médéric Boquien's avatar
Médéric Boquien committed
67
                  ucd="time.age"),
68
            Field(votable, name="SFR", datatype="double", unit="Msun/yr",
Médéric Boquien's avatar
Médéric Boquien committed
69
                  ucd="phys.SFR")
70
71
72
73
        ])
        sfh_table.create_arrays(len(sed.sfh[0]))
        sfh_table.array["time"] = sed.sfh[0]
        sfh_table.array["SFR"] = norm * sed.sfh[1]
74
75

    # SED information to keywords
76
77
78
79
    if sed.sfh is not None:
        # If there is a stellar population then the norm factor is the stellar
        # mass.
        votable.infos.append(Info(name="Galaxy mass in Msun", value=norm))
Médéric Boquien's avatar
Médéric Boquien committed
80
81
    votable.infos.append(Info(name="Redshift",
                              value=sed.info['universe.redshift']))
82
83
    for name, value in sed.info.items():
        if name in sed.mass_proportional_info:
84
            votable.infos.append(Info(name=name, value=norm * value))
85
86
87
88
89
        else:
            votable.infos.append(Info(name=name, value=value))

    votable.set_all_tables_format('binary')
    votable.to_xml(filename)