add astrophysix support, fix labels
This commit is contained in:
@@ -1,14 +1,16 @@
|
||||
# coding: utf-8
|
||||
|
||||
import pymses.utils.constants as cst
|
||||
import astrophysix.units as cst
|
||||
|
||||
create_unit = cst.Unit.create_unit
|
||||
|
||||
|
||||
def parse_exp_unit(u):
|
||||
splitted = u.split("^")
|
||||
name_u = cst.Unit.from_name(splitted[0]).latex
|
||||
name_u = cst.Unit.from_name(splitted[0]).latex.replace("text", "math")
|
||||
exp = ""
|
||||
if len(splitted) > 1:
|
||||
exp = "$^{" + str(splitted[1]) + "}$"
|
||||
exp = "^{" + str(splitted[1]) + "}"
|
||||
return name_u + exp
|
||||
|
||||
|
||||
@@ -17,66 +19,78 @@ def convert_exp(number, digits=4):
|
||||
splitted = "{num:.{digits}g}".format(num=number, digits=digits).split("e")
|
||||
# If no need of scientific notation (low number of digits)
|
||||
if len(splitted) == 1:
|
||||
return "${}$".format(splitted[0])
|
||||
return "{}".format(splitted[0])
|
||||
else:
|
||||
coeff = splitted[0]
|
||||
exp = splitted[1]
|
||||
exp_str = "10^{" + str(int(exp)) + "}"
|
||||
if float(coeff) == 1.0:
|
||||
return "$" + exp_str + "$"
|
||||
return exp_str
|
||||
else:
|
||||
return "${}\\times {}$".format(coeff, exp_str)
|
||||
return "{}\\times {}".format(coeff, exp_str)
|
||||
|
||||
|
||||
def unit_str(unit, base=None, prefix=""):
|
||||
def unit_str(unit, base=None, prefix="", format=" [{unit}]"):
|
||||
"""
|
||||
Format a unit in matplotlib parsable latex
|
||||
|
||||
unit : astrophysics.units.unit.Unit
|
||||
base : astrophysics.units.unit.Unit to use as base unit (if None `unit` is used)
|
||||
prefix : str to put befor the unit
|
||||
format : str with the {unit} key, to put external decoration
|
||||
"""
|
||||
if unit == cst.none:
|
||||
return ""
|
||||
elif not base is None:
|
||||
coeff = unit.express(base)
|
||||
return unit_str(base, prefix=convert_exp(coeff) + " ")
|
||||
u_str = unit_str(base, prefix=convert_exp(coeff) + " ")
|
||||
elif len(unit.latex) > 0:
|
||||
if ("." in unit.latex or "^" in unit.latex) and not "$" in unit.latex:
|
||||
if "." in unit.latex or "^" in unit.latex:
|
||||
base_str = ".".join(map(parse_exp_unit, unit.name.split(".")))
|
||||
return r" [{}{}]".format(prefix, base_str)
|
||||
u_str = r"${}{}$".format(prefix, base_str)
|
||||
else:
|
||||
return r" [{}{}]".format(prefix, unit.latex)
|
||||
u_str = r"${}{}$".format(prefix, unit.latex.replace("text", "math"))
|
||||
elif len(unit.name) > 0:
|
||||
try:
|
||||
base_str = ".".join(map(parse_exp_unit, unit.name.split(".")))
|
||||
u_str = r" [{}{}]".format(prefix, base_str)
|
||||
u_str = r"${}{}$".format(prefix, base_str)
|
||||
except:
|
||||
u_str = r" [{}{}]".format(prefix, unit.name)
|
||||
return u_str
|
||||
u_str = r"${}{}$".format(prefix, unit.name)
|
||||
else:
|
||||
base_str = ".".join(
|
||||
map(parse_exp_unit, unit._decompose_base_units().split("."))
|
||||
)
|
||||
return r" [{}{} {}]".format(prefix, unit.coeff, base_str)
|
||||
u_str = r"${}{} {}$".format(prefix, unit.coeff, base_str)
|
||||
return format.format(unit=u_str)
|
||||
|
||||
|
||||
cst.Msun_pc3 = cst.create_unit(
|
||||
cst.coldens = create_unit(
|
||||
"Msun.pc^-2", base_unit=cst.Msun / cst.pc ** 2, descr="Column density"
|
||||
)
|
||||
cst.km_s = create_unit("km.s^-1", base_unit=cst.km / cst.s, descr="Speed")
|
||||
|
||||
|
||||
cst.Msun_pc3 = create_unit(
|
||||
"Msun.pc^-3", base_unit=cst.Msun / cst.pc ** 3, descr="Density"
|
||||
)
|
||||
|
||||
cst.Msun_pc3 = cst.create_unit(
|
||||
"Msun.pc^-3", base_unit=cst.Msun / cst.pc ** 3, descr="Density"
|
||||
)
|
||||
cst.kg_m3 = create_unit("kg.m^-3", base_unit=cst.kg / cst.m ** 3, descr="Density")
|
||||
|
||||
cst.ssfr = cst.create_unit(
|
||||
"Msun.yr^-1.pc^-2",
|
||||
cst.ssfr = create_unit(
|
||||
"Msun.year^-1.pc^-2",
|
||||
base_unit=cst.Msun / cst.year / cst.pc ** 2,
|
||||
descr="Surfacic SFR",
|
||||
latex="M$_{\odot}$.yr$^{-1}$.pc$^{-2}$",
|
||||
)
|
||||
# latex='M$_{\odot}$.yr$^{-1}$.pc$^{-2}$')
|
||||
|
||||
cst.ssfrG = cst.create_unit(
|
||||
cst.ssfrG = create_unit(
|
||||
"Msun.Gyr^-1.pc^-2",
|
||||
base_unit=1e-9 * cst.Msun / cst.year / cst.pc ** 2,
|
||||
descr="Surfacic SFR",
|
||||
latex="M$_{\odot}$.Gyr$^{-1}$.pc$^{-2}$",
|
||||
latex="\mathrm{M}_{\odot}.\mathrm{Gyr}^{-1}.\mathrm{pc}^{-2}",
|
||||
)
|
||||
|
||||
|
||||
cst.uG = cst.create_unit(
|
||||
"μG", base_unit=1e-10 * cst.T, descr="Micro Gauss", latex="$\mu\mathrm{G}$"
|
||||
cst.uG = create_unit(
|
||||
"μG", base_unit=1e-10 * cst.T, descr="Micro Gauss", latex="\\mu\\mathrm{G}"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user