# Path: emirdrp/processing/wavecal/get_islitlet.py
# def get_islitlet(ipixel):
#     """Return islilet (from 1 to EMIR_NBARS) from Y-pixel coordinate
# 
#     Parameters
#     ----------
#     ipixel : int
#         Y-pixel coordinate in the rectified image.
# 
#     Returns
#     -------
#     islitlet : int
#         Slitlet number (from 1 to EMIR_NBARS).
# 
#     """
# 
#     if ipixel < 1:
#         raise ValueError('ipixel={} cannot be < 1'.format(ipixel))
# 
#     if ipixel > EMIR_NPIXPERSLIT_RECTIFIED * EMIR_NBARS:
#         raise ValueError('ipixel={} cannot be > {}'.format(
#             ipixel, EMIR_NPIXPERSLIT_RECTIFIED * EMIR_NBARS
#         ))
# 
#     islitlet = int(ipixel / EMIR_NPIXPERSLIT_RECTIFIED) + 1
# 
#     if ipixel % EMIR_NPIXPERSLIT_RECTIFIED == 0:
#         islitlet -= 1
# 
#     return islitlet


import numpy as np
import pkgutil
from six import StringIO
from numina.array.display.ximshow import ximshow
from numina.array.display.pause_debugplot import pause_debugplot
from emirdrp.processing.wavecal.get_islitlet import get_islitlet


def useful_mos_xpixels(reduced_mos_data,
                       base_header,
                       vpix_region,
                       npix_removed_near_ohlines=0,
                       list_valid_wvregions=None,
                       debugplot=0):
    """Useful X-axis pixels removing +/- npixaround pixels around each OH line
    """

    # get wavelength calibration from image header
    naxis1 = base_header['naxis1']
    naxis2 = base_header['naxis2']
    crpix1 = base_header['crpix1']
    crval1 = base_header['crval1']
    cdelt1 = base_header['cdelt1']

    # check vertical region
    nsmin = int(vpix_region[0] + 0.5)
    nsmax = int(vpix_region[1] + 0.5)
    if nsmin > nsmax:
        raise ValueError('vpix_region values in wrong order')
    elif nsmin < 1 or nsmax > naxis2:
        raise ValueError('vpix_region outside valid range')

    # minimum and maximum pixels in the wavelength direction
    islitlet_min = get_islitlet(nsmin)
    ncmin = base_header['jmnslt{:02d}'.format(islitlet_min)]
    ncmax = base_header['jmxslt{:02d}'.format(islitlet_min)]

# Path: emirdrp/processing/wavecal/get_islitlet.py
# def get_islitlet(ipixel):
#     """Return islilet (from 1 to EMIR_NBARS) from Y-pixel coordinate
# 
#     Parameters
#     ----------
#     ipixel : int
#         Y-pixel coordinate in the rectified image.
# 
#     Returns
#     -------
#     islitlet : int
#         Slitlet number (from 1 to EMIR_NBARS).
# 
#     """
# 
#     if ipixel < 1:
#         raise ValueError('ipixel={} cannot be < 1'.format(ipixel))
# 
#     if ipixel > EMIR_NPIXPERSLIT_RECTIFIED * EMIR_NBARS:
#         raise ValueError('ipixel={} cannot be > {}'.format(
#             ipixel, EMIR_NPIXPERSLIT_RECTIFIED * EMIR_NBARS
#         ))
# 
#     islitlet = int(ipixel / EMIR_NPIXPERSLIT_RECTIFIED) + 1
# 
#     if ipixel % EMIR_NPIXPERSLIT_RECTIFIED == 0:
#         islitlet -= 1
# 
#     return islitlet


# Path: emirdrp/processing/wavecal/useful_mos_xpixels.py
import numpy as np
import pkgutil
from six import StringIO
from numina.array.display.ximshow import ximshow
from numina.array.display.pause_debugplot import pause_debugplot
from emirdrp.processing.wavecal.get_islitlet import get_islitlet



def useful_mos_xpixels(reduced_mos_data,
                       base_header,
                       vpix_region,
                       npix_removed_near_ohlines=0,
                       list_valid_wvregions=None,
                       debugplot=0):
    """Useful X-axis pixels removing +/- npixaround pixels around each OH line
    """

    # get wavelength calibration from image header
    naxis1 = base_header['naxis1']
    naxis2 = base_header['naxis2']
    crpix1 = base_header['crpix1']
    crval1 = base_header['crval1']
    cdelt1 = base_header['cdelt1']

    # check vertical region
    nsmin = int(vpix_region[0] + 0.5)
    nsmax = int(vpix_region[1] + 0.5)
    if nsmin > nsmax:
        raise ValueError('vpix_region values in wrong order')
    elif nsmin < 1 or nsmax > naxis2:
        raise ValueError('vpix_region outside valid range')

    # minimum and maximum pixels in the wavelength direction
    islitlet_min = get_islitlet(nsmin)
    ncmin = base_header['jmnslt{:02d}'.format(islitlet_min)]
    ncmax = base_header['jmxslt{:02d}'.format(islitlet_min)]

    islitlet_max = get_islitlet(nsmax)