"""
Text
"""

import os
import urllib.request

import numpy as np

from astropy.io import fits


def add_vega(input_path, database):
    """
    Function for adding a flux-calibrated spectrum of Vega to the database.

    Parameters
    ----------
    input_path : str
        Path of the data folder.
    database : h5py._hl.files.File
        Database.

    Returns
    -------
    NoneType
        None
    """

    data_file = os.path.join(input_path, "alpha_lyr_stis_008.fits")

    if not os.path.isfile(data_file):
        print("Downloading Vega spectrum (270 kB)...", end="", flush=True)

        try:
            url = "http://ssb.stsci.edu/cdbs/calspec/alpha_lyr_stis_008.fits"
            urllib.request.urlretrieve(url, data_file)

        except urllib.error.HTTPError:
            url = "https://home.strw.leidenuniv.nl/~stolker/species/alpha_lyr_stis_008.fits"
            urllib.request.urlretrieve(url, data_file)

        print(" [DONE]")

    if "spectra/calibration" not in database:
        database.create_group("spectra/calibration")

    if "spectra/calibration/vega" in database:
        del database["spectra/calibration/vega"]

    hdu = fits.open(data_file)
    data = hdu[1].data
    wavelength = data["WAVELENGTH"]  # (Angstrom)
    flux = data["FLUX"]  # (erg s-1 cm-2 A-1)
    error_stat = data["STATERROR"]  # (erg s-1 cm-2 A-1)
    error_sys = data["SYSERROR"]  # (erg s-1 cm-2 A-1)
    hdu.close()

    wavelength *= 1e-4  # (Angstrom) -> (um)
    flux *= 1e-3 * 1e4  # (erg s-1 cm-2 A-1) -> (W m-2 um-1)
    error_stat *= 1e-3 * 1e4  # (erg s-1 cm-2 A-1) -> (W m-2 um-1)
    error_sys *= 1e-3 * 1e4  # (erg s-1 cm-2 A-1) -> (W m-2 um-1)

    print("Adding Vega spectrum...", end="", flush=True)

    database.create_dataset(
        "spectra/calibration/vega", data=np.vstack((wavelength, flux, error_stat))
    )

    print(" [DONE]")
