# Path: astroNN/config.py
# MAGIC_NUMBER = magic_num_reader()


import os
import warnings
import numpy as np
from astropy import units as u
from astroNN.config import MAGIC_NUMBER
    from astroNN.config import ENVVAR_WARN_FLAG


def mag_to_fakemag(mag, parallax, parallax_err=None):
    """
    To convert apparent magnitude to astroNN fakemag, Magic Number will be preserved

    :param mag: apparent magnitude
    :type mag: Union[float, ndarray]
    :param parallax: parallax (mas) or with astropy(can be distance with units) so astroNN will convert to appropriate units
    :type parallax: Union[float, ndarray, astropy Quantity]
    :param parallax_err: parallax_error (mas) or with astropy so astroNN will convert to appropriate units
    :type parallax_err: Union[NoneType, float, ndarray, astropy Quantity]
    :return: astroNN fakemag, with addition (with additional return of propagated error if parallax_err is provided)
    :rtype: Union[float, ndarray]
    :History: 2017-Oct-14 - Written - Henry Leung (University of Toronto)
    """
    # Check unit if available
    if isinstance(parallax, u.Quantity):
        original_parallax_unit = parallax.unit
        parallax = parallax.to(default_parallax_unit, equivalencies=u.parallax())
        if parallax_err is not None:
            if not isinstance(parallax_err, u.Quantity):
                # assume parallax error carry the same original unit as parallax if no units detected
                parallax_err = (parallax_err * original_parallax_unit).to(default_parallax_unit).value
            if isinstance(parallax_err, u.Quantity):
                parallax_err = parallax_err.to(default_parallax_unit).value

    mag = np.array(mag)
    parallax_unitless = np.array(parallax)  # Take the value as we cant apply pow() to astropy unit


# Path: astroNN/config.py
# MAGIC_NUMBER = magic_num_reader()


# Path: astroNN/gaia/gaia_shared.py
import os
import warnings
import numpy as np
from astropy import units as u
from astroNN.config import MAGIC_NUMBER
    from astroNN.config import ENVVAR_WARN_FLAG



def mag_to_fakemag(mag, parallax, parallax_err=None):
    """
    To convert apparent magnitude to astroNN fakemag, Magic Number will be preserved

    :param mag: apparent magnitude
    :type mag: Union[float, ndarray]
    :param parallax: parallax (mas) or with astropy(can be distance with units) so astroNN will convert to appropriate units
    :type parallax: Union[float, ndarray, astropy Quantity]
    :param parallax_err: parallax_error (mas) or with astropy so astroNN will convert to appropriate units
    :type parallax_err: Union[NoneType, float, ndarray, astropy Quantity]
    :return: astroNN fakemag, with addition (with additional return of propagated error if parallax_err is provided)
    :rtype: Union[float, ndarray]
    :History: 2017-Oct-14 - Written - Henry Leung (University of Toronto)
    """
    # Check unit if available
    if isinstance(parallax, u.Quantity):
        original_parallax_unit = parallax.unit
        parallax = parallax.to(default_parallax_unit, equivalencies=u.parallax())
        if parallax_err is not None:
            if not isinstance(parallax_err, u.Quantity):
                # assume parallax error carry the same original unit as parallax if no units detected
                parallax_err = (parallax_err * original_parallax_unit).to(default_parallax_unit).value
            if isinstance(parallax_err, u.Quantity):
                parallax_err = parallax_err.to(default_parallax_unit).value

    mag = np.array(mag)
    parallax_unitless = np.array(parallax)  # Take the value as we cant apply pow() to astropy unit


    magic_idx = ((parallax_unitless == MAGIC_NUMBER) | (mag == MAGIC_NUMBER) | (mag < -90.) | np.isnan(parallax_unitless) | np.isnan(mag))  # check for magic number