Skip to content

Utilities

cm_2_to_nm(cm)

Convert a wavenumber in cm^-1 to a wavelength in nm.

Parameters:

Name Type Description Default
cm Union[float, ndarray]

Wavenumber in cm^-1.

required

Returns:

Type Description
Union[float, ndarray]

Wavelength in nm.

Source code in src/rimsschemedrawer/utils.py
def cm_2_to_nm(cm: Union[float, np.ndarray]) -> Union[float, np.ndarray]:
    """Convert a wavenumber in cm^-1 to a wavelength in nm.

    :param cm: Wavenumber in cm^-1.
    :return: Wavelength in nm.
    """
    return 1e7 / cm

color_wavelength(lmb, darkmode=False)

Color the wavelength according to the wavelength.

When darkmode is turned on, pastel colors are used. Definitions of colors are taken from here: https://sciencenotes.org/visible-light-spectrum-wavelengths-and-colors/

Parameters:

Name Type Description Default
lmb float

Wavelength in nm.

required

Returns:

Type Description
str

Color string.

Source code in src/rimsschemedrawer/utils.py
def color_wavelength(lmb: float, darkmode: bool = False) -> str:
    """Color the wavelength according to the wavelength.

    When darkmode is turned on, pastel colors are used. Definitions of colors are
    taken from here:
    https://sciencenotes.org/visible-light-spectrum-wavelengths-and-colors/

    :param lmb: Wavelength in nm.

    :return: Color string.
    """
    dict_light = {
        "ir": "#500000",
        "red": "#b70000",
        "orange": "#d75700",
        "yellow": "#c09e00",
        "green": "#0aa000",
        "light_blue": "#0098ff",
        "deep_blue": "#0012a0",
        "violet": "#5f00a0",
    }
    dict_dark = {
        "ir": "#b98989",
        "red": "#d27878",
        "orange": "#ffa669",
        "yellow": "#f4e67c",
        "green": "#60a55b",
        "light_blue": "#82b1d1",
        "deep_blue": "#8c96df",
        "violet": "#9e85af",
    }

    if darkmode:
        dict_use = dict_dark
    else:
        dict_use = dict_light

    if lmb > 700:  # infrared
        return dict_use["ir"]
    elif lmb >= 625:  # red
        return dict_use["red"]
    elif lmb >= 590:  # orange
        return dict_use["orange"]
    elif lmb >= 565:  # yellow
        return dict_use["yellow"]
    elif lmb >= 500:  # green
        return dict_use["green"]
    elif lmb >= 484:  # light blue
        return dict_use["light_blue"]
    elif lmb >= 450:  # deep blue
        return dict_use["deep_blue"]
    else:  # violet
        return dict_use["violet"]

get_elements()

Get a list of all elements.

Source code in src/rimsschemedrawer/utils.py
def get_elements() -> list:
    """Get a list of all elements."""
    return list(IP_DICTIONARY.keys())

get_ip(ele)

Get the ionization potential for a given element.

Parameters:

Name Type Description Default
ele str

Element symbol, not case sensitive.

required

Returns:

Type Description
float

Ionization potential in cm-1.

Source code in src/rimsschemedrawer/utils.py
def get_ip(ele: str) -> float:
    """Get the ionization potential for a given element.

    :param ele: Element symbol, not case sensitive.

    :return: Ionization potential in cm-1.
    """

    return IP_DICTIONARY[ele.capitalize()]

get_ip_reference(ele)

Get a reference for the ionization potential.

If element is in IP_REFERENCES_NON_NIST, return the reference dictionary. Otherwise, return a dictionary with "NIST" entries.

Returns:

Type Description
dict

Dictionary with "author", "year", and "url" entries.

Source code in src/rimsschemedrawer/utils.py
def get_ip_reference(ele: str) -> dict:
    """Get a reference for the ionization potential.

    If element is in `IP_REFERENCES_NON_NIST`, return the reference dictionary.
    Otherwise, return a dictionary with "NIST" entries.

    :return: Dictionary with "author", "year", and "url" entries.
    """
    nist_dict = {
        "author": "NIST ASD",
        "year": 2024,
        "url": "https://www.nist.gov/pml/atomic-spectra-database",
    }

    if ele in IP_REFERENCES_NON_NIST.keys():
        return IP_REFERENCES_NON_NIST[ele]
    else:
        return nist_dict

guess_element_from_ip(ip)

Guess an element from the ionization potential.

This routine is mainly provided for backwards compatibility with older RIMSSchemeDrawer instances, where the user used to define the IP manually.

Parameters:

Name Type Description Default
ip float

Ionization potential in cm-1.

required

Returns:

Type Description
str

Element symbol (best guess).

Source code in src/rimsschemedrawer/utils.py
def guess_element_from_ip(ip: float) -> str:
    """Guess an element from the ionization potential.

    This routine is mainly provided for backwards compatibility with older
    RIMSSchemeDrawer instances, where the user used to define the IP manually.

    :param ip: Ionization potential in cm-1.
    :return: Element symbol (best guess).
    """
    eles, values = list(IP_DICTIONARY.keys()), list(IP_DICTIONARY.values())
    values = np.array(values)
    diff = np.abs(values - ip)
    ind = np.argmin(diff)
    return eles[ind]

my_exp_formatter(val, prec)

Format a value with a given precision to LaTeX output.

Source code in src/rimsschemedrawer/utils.py
def my_exp_formatter(val: float, prec: int) -> str:
    """Format a value with a given precision to LaTeX output."""
    value_str = f"{val:.{prec}e}"
    numb, exp = value_str.split("e")
    return f"${numb} \\times 10^{{{int(exp)}}}$"

my_formatter(val, *args)

Format the axis labels for the left y-axis in scientific notation.

Parameters:

Name Type Description Default
val float

Value to format, must be >= 0.

required
args

Additional arguments - will be ignored.

()

Returns:

Type Description
str

Properly formatted string.

Source code in src/rimsschemedrawer/utils.py
def my_formatter(val: float, *args) -> str:
    """Format the axis labels for the left y-axis in scientific notation.

    :param val: Value to format, must be >= 0.
    :param args: Additional arguments - will be ignored.

    :return: Properly formatted string.
    """
    fform = matplotlib.ticker.ScalarFormatter(useOffset=False, useMathText=True)
    fform.set_scientific((0, 0))
    if val <= 1e-9:  # some reasonable cutoff
        val_ret = "$0$"
    else:
        val_ret = f"${fform.format_data(val)}$"

    return val_ret

nm_to_cm_2(nm)

Convert a wavelength in nm to wavenumber in cm^-1.

Parameters:

Name Type Description Default
nm Union[float, ndarray]

Wavelength in nm.

required

Returns:

Type Description
Union[float, ndarray]

Wavenumber in cm^-1.

Source code in src/rimsschemedrawer/utils.py
def nm_to_cm_2(nm: Union[float, np.ndarray]) -> Union[float, np.ndarray]:
    """Convert a wavelength in nm to wavenumber in cm^-1.

    :param nm: Wavelength in nm.
    :return: Wavenumber in cm^-1.
    """
    return 1e7 / nm

term_to_string(tstr)

Convert term symbol to LaTeX enabled string.

Converts a term symbol string to a LaTeX enabled matplotlib string If already a LaTeX string, just return it.

Parameters:

Name Type Description Default
tstr str

Input string to convert

required

Returns:

Type Description

Output string LaTeX enabled for Matplotlib

Source code in src/rimsschemedrawer/utils.py
def term_to_string(tstr: str):
    """Convert term symbol to LaTeX enabled string.

    Converts a term symbol string to a LaTeX enabled matplotlib string
    If already a LaTeX string, just return it.
    :param tstr:   Input string to convert
    :return:       Output string LaTeX enabled for Matplotlib
    """
    if tstr == "":
        return None

    latex_characters = ["{", "}", "^", "_"]
    for lch in latex_characters:
        if lch in tstr:
            tstr = tstr.replace(" ", "\\,")
            return f"${tstr}$"

    # some exceptionslike AI and IP
    if tstr == "IP":
        return "IP"
    if tstr == "AI":
        return "AI"
    if tstr == "Rydberg":
        return "Rydberg"
    if tstr == "Ryd":
        return "Ryd"

    # if there is an equal sign in there, leave it as is
    if tstr.find("=") != -1:
        return tstr

    # find the first slash and start looking for the letter after that
    start = tstr.find("/") + 1
    letterind = -1
    for it in range(start, len(tstr)):
        try:
            float(tstr[it])
        except ValueError:
            letterind = it
            break
    # if / comes after the letter:
    if letterind == -1:
        start = 0
        letterind = -1
        for it in range(start, len(tstr)):
            try:
                float(tstr[it])
            except ValueError:
                letterind = it
                break
    if letterind == -1:
        return tstr

    # set up the three parts for the latex string
    tmp1 = "$^{" + tstr[0:letterind] + "}$"
    tmp2 = tstr[letterind]
    tmp3 = "$_{" + tstr[letterind + 1 :] + "}$"

    return tmp1 + tmp2 + tmp3