# Path: astroNN/apogee/apogee_shared.py
# def apogee_default_dr(dr=None):
#     """
#     Check if dr argument is provided, if none then use default
# 
#     :param dr: APOGEE DR
#     :type dr: int
#     :return: APOGEE DR
#     :rtype: int
#     :History:
#         | 2017-Oct-26 - Written - Henry Leung (University of Toronto)
#         | 2018-Sept-08 - Updated - Henry Leung (University of Toronto)
#     """
#     if dr == 15:
#         print("SDSS APOGEE DR15 is equivalent to DR14, so astroNN is using DR14 even you set DR15")
#         dr = 14
# 
#     if dr is None:
#         try:
#             redux_ver = os.environ["RESULTS_VERS"]  # RESULTS_VERS is from Jo Bovy APOGEE Tool
#             if redux_ver == "v402":
#                 dr = 11
#             elif redux_ver == "v603":
#                 dr = 12
#             elif redux_ver == "l30e.2":
#                 dr = 13
#             elif redux_ver == "l31c.2":
#                 dr = 14
#             elif redux_ver == "l33":
#                 dr = 16
#         except KeyError:
#             pass
# 
#         if dr is None:  # if it is still None
#             dr = 16
#             print(f'dr is not provided, using default dr={dr}')
#     else:
#         pass
# 
#     return dr


import os
import numpy as np
import astroNN
import astroNN.data
from astroNN.apogee.apogee_shared import apogee_default_dr
        | 2017-Dec-16 - Updated - Henry Leung (University of Toronto)
    """
    dr = apogee_default_dr(dr=dr)
    spectra = np.atleast_2d(spectra)
    info = chips_pix_info(dr=dr)

    if spectra.shape[1] != 8575 and spectra.shape[1] != info[6]:
        raise EnvironmentError('Are you sure you are giving astroNN APOGEE spectra?')
    if spectra.shape[1] != info[6]:
        spectra = spectra[:, np.r_[info[0]:info[1], info[2]:info[3], info[4]:info[5]]]

    return spectra


def wavelength_solution(dr=None):
    """
    To return wavelegnth_solution, apStarWavegrid was provided by Jo Bovy's apogee tools (Toronto)

    :param dr: data release
    :type dr: Union(int, NoneType)
    :return:
        | lambda_blue, lambda_green, lambda_red which are 3 wavelength solution array
        |   - lambda_blue refers to the wavelength solution for each pixel in blue chips
        |   - lambda_green refers to the wavelength solution for each pixel in green chips
        |   - lambda_red refers to the wavelength solution for each pixel in red chips
    :rtype: ndarray
    :History:
        | 2017-Nov-20 - Written - Henry Leung (University of Toronto)
        | 2017-Dec-16 - Updated - Henry Leung (University of Toronto)
    """

# Path: astroNN/apogee/apogee_shared.py
# def apogee_default_dr(dr=None):
#     """
#     Check if dr argument is provided, if none then use default
# 
#     :param dr: APOGEE DR
#     :type dr: int
#     :return: APOGEE DR
#     :rtype: int
#     :History:
#         | 2017-Oct-26 - Written - Henry Leung (University of Toronto)
#         | 2018-Sept-08 - Updated - Henry Leung (University of Toronto)
#     """
#     if dr == 15:
#         print("SDSS APOGEE DR15 is equivalent to DR14, so astroNN is using DR14 even you set DR15")
#         dr = 14
# 
#     if dr is None:
#         try:
#             redux_ver = os.environ["RESULTS_VERS"]  # RESULTS_VERS is from Jo Bovy APOGEE Tool
#             if redux_ver == "v402":
#                 dr = 11
#             elif redux_ver == "v603":
#                 dr = 12
#             elif redux_ver == "l30e.2":
#                 dr = 13
#             elif redux_ver == "l31c.2":
#                 dr = 14
#             elif redux_ver == "l33":
#                 dr = 16
#         except KeyError:
#             pass
# 
#         if dr is None:  # if it is still None
#             dr = 16
#             print(f'dr is not provided, using default dr={dr}')
#     else:
#         pass
# 
#     return dr


# Path: astroNN/apogee/chips.py
import os
import numpy as np
import astroNN
import astroNN.data
from astroNN.apogee.apogee_shared import apogee_default_dr

        | 2017-Dec-16 - Updated - Henry Leung (University of Toronto)
    """
    dr = apogee_default_dr(dr=dr)
    spectra = np.atleast_2d(spectra)
    info = chips_pix_info(dr=dr)

    if spectra.shape[1] != 8575 and spectra.shape[1] != info[6]:
        raise EnvironmentError('Are you sure you are giving astroNN APOGEE spectra?')
    if spectra.shape[1] != info[6]:
        spectra = spectra[:, np.r_[info[0]:info[1], info[2]:info[3], info[4]:info[5]]]

    return spectra


def wavelength_solution(dr=None):
    """
    To return wavelegnth_solution, apStarWavegrid was provided by Jo Bovy's apogee tools (Toronto)

    :param dr: data release
    :type dr: Union(int, NoneType)
    :return:
        | lambda_blue, lambda_green, lambda_red which are 3 wavelength solution array
        |   - lambda_blue refers to the wavelength solution for each pixel in blue chips
        |   - lambda_green refers to the wavelength solution for each pixel in green chips
        |   - lambda_red refers to the wavelength solution for each pixel in red chips
    :rtype: ndarray
    :History:
        | 2017-Nov-20 - Written - Henry Leung (University of Toronto)
        | 2017-Dec-16 - Updated - Henry Leung (University of Toronto)
    """

    dr = apogee_default_dr(dr=dr)