|
| 1 | +import astropy.units as u |
| 2 | +from astropy.cosmology.core import Cosmology |
| 3 | +from astropy.cosmology.parameter import Parameter |
| 4 | +from astropy.table import QTable |
| 5 | + |
| 6 | +from .table import from_table, to_table |
| 7 | + |
| 8 | +_FORMAT_TABLE = { |
| 9 | + "H0": "$$H_0 \r{[\mathrm{km\,s^{-1}\,Mpc^{-1}}]}$$", |
| 10 | + "Om0": "$$\\Omega_{m,0}$$", |
| 11 | + "Ode0": "$$\\Omega_{\\Lambda,0}$$", |
| 12 | + "Tcmb0": "$$T_{0} \r{[\mathrm{K}]}$$", |
| 13 | + "Neff": "$$N_{eff}$$", |
| 14 | + "m_nu": "$$m_{nu} \r{[\mathrm{eV}]}$$", |
| 15 | + "Ob0": "$$\\Omega_{b,0}$$", |
| 16 | + "w0": "$$w_{0}$$", |
| 17 | + "wa": "$$w_{a}$$", |
| 18 | + "wz": "$$w_{z}$$", |
| 19 | + "wp": "$$w_{p}$$", |
| 20 | + "zp": "$$z_{p}$$", |
| 21 | +} |
| 22 | + |
| 23 | +def write_latex( |
| 24 | + cosmology, file, *, overwrite=False, cls=QTable, latex_names=True, **kwargs |
| 25 | +): |
| 26 | + r"""Serialize the |Cosmology| into a LaTeX. |
| 27 | +
|
| 28 | + Parameters |
| 29 | + ---------- |
| 30 | + cosmology : `~astropy.cosmology.Cosmology` subclass instance |
| 31 | + file : path-like or file-like |
| 32 | + Location to save the serialized cosmology. |
| 33 | +
|
| 34 | + overwrite : bool |
| 35 | + Whether to overwrite the file, if it exists. |
| 36 | + cls : type (optional, keyword-only) |
| 37 | + Astropy :class:`~astropy.table.Table` (sub)class to use when writing. |
| 38 | + Default is :class:`~astropy.table.QTable`. |
| 39 | + **kwargs |
| 40 | + Passed to ``cls.write`` |
| 41 | +
|
| 42 | + Raises |
| 43 | + ------ |
| 44 | + TypeError |
| 45 | + If kwarg (optional) 'cls' is not a subclass of `astropy.table.Table` |
| 46 | + """ |
| 47 | + # Set cosmology_in_meta as false for now since there is no metadata being kept |
| 48 | + table = to_table(cosmology, cls=cls, cosmology_in_meta=False) |
| 49 | + |
| 50 | + cosmo_cls = type(cosmology) |
| 51 | + for name, col in table.columns.copy().items(): |
| 52 | + param = getattr(cosmo_cls, name, None) |
| 53 | + if not isinstance(param, Parameter) or param.unit in (None, u.one): |
| 54 | + continue |
| 55 | + # Replace column with unitless version |
| 56 | + table.replace_column(name, (col << param.unit).value, copy=False) |
| 57 | + |
| 58 | + # Convert parameter names to LaTeX format |
| 59 | + if name == "cosmology" or name == "name": |
| 60 | + pass |
| 61 | + else: |
| 62 | + table.rename_column(name, _FORMAT_TABLE.get(name)) |
| 63 | + |
| 64 | + table.write(file, overwrite=overwrite, format="latex", **kwargs) |
0 commit comments