"""
The acsccd module contains a function `acsccd` that calls the ACSCCD executable.
Use this function to facilitate batch runs of ACSCCD, or for the TEAL interface.

.. warning:: Do not use with SBC MAMA images.

Examples
--------

In Python without TEAL:

>>> from acstools import acsccd
>>> acsccd.acsccd('*raw.fits')

In Python with TEAL:

>>> from stsci.tools import teal
>>> from acstools import acsccd
>>> teal.teal('acsccd')

In Pyraf::

    --> import acstools
    --> epar acsccd

"""
# STDLIB
import os.path
import subprocess

# STSCI
from stsci.tools import parseinput
try:
    from stsci.tools import teal
except:
    teal = None


__taskname__ = "acsccd"
__version__ = "1.0"
__vdate__ = "18-Dec-2012"


#
# These keywords do not work because they get overwritten by header in
# acsccd/getacsflags.c
#
# dqicorr=False, atodcorr=False, blevcorr=False, biascorr=False, pctecorr=False
#
# dqicorr, atodcorr, blevcorr, biascorr, pctecorr : bool, optional
#     Enable XXXXCORR.
#     If all False, will set all but ATODCORR to PERFORM.
#     If any is True, will set that to PERFORM and the rest to OMIT.
#
def acsccd(input, exec_path='', time_stamps=False, verbose=False, quiet=False,
           single_core=False):
    """
    Run the acsccd.e executable as from the shell.

    Output is automatically named based on input and PCTECORR:

        +----------------+----------+--------------------+
        | INPUT          | PCTECORR | OUTPUT             |
        +================+==========+====================+
        | ``*_raw.fits`` | OMIT     | ``*_blv_tmp.fits`` |
        +----------------+----------+--------------------+
        | ``*_raw.fits`` | PERFORM  | ``*_blc_tmp.fits`` |
        +----------------+----------+--------------------+

    .. note:: Calibration flags are controlled by primary header.

    .. warning:: Do not use with SBC MAMA images.

    Parameters
    ----------
    input : str or list of str
        Input filenames in one of these formats:

            * a single filename ('j1234567q_raw.fits')
            * a Python list of filenames
            * a partial filename with wildcards ('\*raw.fits')
            * filename of an ASN table ('j12345670_asn.fits')
            * an at-file (``@input``)

    exec_path : str, optional
        The complete path to ACSCCD executable.
        If not given, run ACSCCD given by 'acsccd.e'.

    time_stamps : bool, optional
        Set to True to turn on the printing of time stamps.

    verbose : bool, optional
        Set to True for verbose output.

    quiet : bool, optional
        Set to True for quiet output.

    single_core : bool, optional
        CTE correction in ACSCCD will by default try to use all available
        CPUs on your computer. Set this to True to force the use of just
        one CPU.

    """
    if exec_path:
        if not os.path.exists(exec_path):
            raise OSError('Executable not found: ' + exec_path)
        call_list = [exec_path]
    else:
        call_list = ['acsccd.e']

    # Parse input to get list of filenames to process.
    # acsccd.e only takes 'file1,file2,...'
    infiles, dummy_out = parseinput.parseinput(input)
    call_list.append(','.join(infiles))

    if time_stamps:
        call_list.append('-t')

    if verbose:
        call_list.append('-v')

    if quiet:
        call_list.append('-q')

    if single_core:
        call_list.append('-1')

    #if dqicorr:
    #    call_list.append('-dqi')

    #if atodcor:
    #    call_list.append('-atod')

    #if blevcorr:
    #    call_list.append('-blev')

    #if biascorr:
    #    call_list.append('-bias')

    #if pctecorr:
    #    call_list.append('-pcte')

    subprocess.call(call_list)


def getHelpAsString():
    """
    Returns documentation on the `acsccd` function. Required by TEAL.

    """
    return acsccd.__doc__


def run(configobj=None):
    """
    TEAL interface for the `acsccd` function.

    """
    acsccd(configobj['input'],
           exec_path=configobj['exec_path'],
           time_stamps=configobj['time_stamps'],
           verbose=configobj['verbose'],
           quiet=configobj['quiet'],
           single_core=configobj['single_core'] #,
           #dqicorr=configobj['dqicorr'],
           #atodcorr=configobj['atodcorr'],
           #blevcorr=configobj['blevcorr'],
           #biascorr=configobj['biascorr'],
           #pctecorr=configobj['pctecorr']
           )


# Taken out from pars/acsccd.cfg
#
#dqicorr = False
#atodcorr = False
#blevcorr = False
#biascorr = False
#pctecorr = False

# Taken out from pars/acsccd.cfgspc
#
#dqicorr = boolean_kw(default=False, comment="Initialize DQ array")
#atodcorr = boolean_kw(default=False, comment="Apply A-to-D correction")
#blevcorr = boolean_kw(default=False, comment="Subtract bias from overscan")
#biascorr = boolean_kw(default=False, comment="Subtract bias image")
#pctecorr = boolean_kw(default=False, comment="Apply pixel CTE correction")
