import os, yaml
import warnings

cpism_refdata = 'D:/CPIC_image_process/cpic-img-sim-master/cpism_refdata'

if not os.path.exists(cpism_refdata):  # pragma: no cover
    raise Exception(
        "Can not find CPISM reference data.")


if not os.path.exists(os.environ.get('PYSYN_CDBS', './trd')):  # pragma: no cover
    raise Exception(
        "Can not find PYSYN Stellar reference data.")

# we need to ignore the warning from pysynphot, because we only use the star models.
with warnings.catch_warnings():  # pragma: no cover
    warnings.filterwarnings("ignore")
    import pysynphot as S

solar_spectrum = S.FileSpectrum(
    f"{os.environ['PYSYN_CDBS']}/grid/solsys/solar_spec.fits")
solar_spectrum.convert('photlam')

config_file = cpism_refdata + '/optics/optics_config.yaml'
if not os.path.exists(config_file):  # pragma: no cover
    raise FileNotFoundError(f"光学配置文件不存在({config_file})")
with open(config_file, 'r') as f:
    optics_config = yaml.load(f, Loader=yaml.FullLoader)

MAG_SYSTEM = 'abmag'

__version__ = '1.0.0'

def which_focalplane(band):
    """
    Return the name of the focalplane which the band belongs to.

    Parameters
    -----------
    band: str
        The name of the band.
        from ['f565', 'f661', 'f743', 'f883', 'f940', 'f1265', 'f1425', 'f1542', 'wfs']

    Returns
    --------
    str
        The name of the focalplane.
        'vis' or 'nir' or 'wfs'

    Raises
    -------
    ValueError
        If the band is not in ['f565', 'f661', 'f743', 'f883', 'f940', 'f1265', 'f1425', 'f1542', 'wfs']
    """
    band = band.lower()
    if band in ['f520', 'f662', 'f720', 'f850']:
        return 'vis'
    if band in ['f940', 'f1265', 'f1425', 'f1542']:
        return 'nir'
    if band in ['wfs']:
        return 'wfs'

    raise ValueError(f"未知的波段{band}")