# Path: ciao_contrib/stacklib.py
# def make_stackfile(infiles, dir="/tmp/", suffix='', delete=True):
#     """Create a temporary file object which points to a file listing
#     the contents of infiles. So if
# 
#       tfh = make_stackfile(infiles)
# 
#     You can use '@' + tfh.name to use this as a stack and you should
#     use tfh.close() to remove the temporary file when handling errors.
# 
#     The dir, suffix, and delete arguments are passed through to the
#     NamedTemporaryFile constructor.
# 
#     The infile values are written to the stack file using their
#     absolute location (on input they are taken to be relative to the
#     current working directory and NOT the dir argument).
#     """
# 
#     # Change the mode, since the default is 'wb+' which makes
#     # Python 3.5 use byte strings. I could set the encoding, but
#     # this used to be used with Python 2.7 so leave as is.
#     #
#     tfh = tempfile.NamedTemporaryFile(mode='w+', dir=dir,
#                                       suffix=suffix,
#                                       delete=delete)
# 
#     for infile in infiles:
#         tfh.write(os.path.abspath(infile) + "\n")
# 
#     tfh.flush()
#     return tfh


import functools
import os
import subprocess as sbp
import tempfile
import numpy as np
import cxcdm
import paramio
import stk
import ciao_contrib.logger_wrapper as lw
import ciao_contrib.runtool as rt
import ciao_contrib._tools.fileio as fileio
from region import CXCRegion
from ciao_contrib.stacklib import make_stackfile
    If lookupTab is not None it is passed to dmimgcalc.
    """

    args = ["infile=" + infile1,
            "infile2=" + infile2,
            "outfile=" + outfile,
            "operation=" + op,
            "weight={}".format(weight1),
            "weight2={}".format(weight2),
            "mode=h"]
    if lookupTab is not None:
        args.append("lookupTab=" + lookupTab)

    add_defargs(args, clobber, verbose)
    punlearn("dmimgcalc")
    run("dmimgcalc", args)


def dmimgcalc(infiles, outfile, op,
              verbose=0,
              clobber=False,
              lookupTab=None,
              tmpdir="/tmp/"):
    """Run dmimgcalc with infile set to infiles (input is an array)
    creating outfile. The imgout parameter is set to the op
    argument, so op should be "img1+img2" rather than "imgout=img1+img2".

    If lookupTab is not None it is passed to dmimgcalc.
    """


# Path: ciao_contrib/stacklib.py
# def make_stackfile(infiles, dir="/tmp/", suffix='', delete=True):
#     """Create a temporary file object which points to a file listing
#     the contents of infiles. So if
# 
#       tfh = make_stackfile(infiles)
# 
#     You can use '@' + tfh.name to use this as a stack and you should
#     use tfh.close() to remove the temporary file when handling errors.
# 
#     The dir, suffix, and delete arguments are passed through to the
#     NamedTemporaryFile constructor.
# 
#     The infile values are written to the stack file using their
#     absolute location (on input they are taken to be relative to the
#     current working directory and NOT the dir argument).
#     """
# 
#     # Change the mode, since the default is 'wb+' which makes
#     # Python 3.5 use byte strings. I could set the encoding, but
#     # this used to be used with Python 2.7 so leave as is.
#     #
#     tfh = tempfile.NamedTemporaryFile(mode='w+', dir=dir,
#                                       suffix=suffix,
#                                       delete=delete)
# 
#     for infile in infiles:
#         tfh.write(os.path.abspath(infile) + "\n")
# 
#     tfh.flush()
#     return tfh


# Path: ciao_contrib/_tools/run.py
import functools
import os
import subprocess as sbp
import tempfile
import numpy as np
import cxcdm
import paramio
import stk
import ciao_contrib.logger_wrapper as lw
import ciao_contrib.runtool as rt
import ciao_contrib._tools.fileio as fileio
from region import CXCRegion
from ciao_contrib.stacklib import make_stackfile

    If lookupTab is not None it is passed to dmimgcalc.
    """

    args = ["infile=" + infile1,
            "infile2=" + infile2,
            "outfile=" + outfile,
            "operation=" + op,
            "weight={}".format(weight1),
            "weight2={}".format(weight2),
            "mode=h"]
    if lookupTab is not None:
        args.append("lookupTab=" + lookupTab)

    add_defargs(args, clobber, verbose)
    punlearn("dmimgcalc")
    run("dmimgcalc", args)


def dmimgcalc(infiles, outfile, op,
              verbose=0,
              clobber=False,
              lookupTab=None,
              tmpdir="/tmp/"):
    """Run dmimgcalc with infile set to infiles (input is an array)
    creating outfile. The imgout parameter is set to the op
    argument, so op should be "img1+img2" rather than "imgout=img1+img2".

    If lookupTab is not None it is passed to dmimgcalc.
    """


    instk = make_stackfile(infiles, dir=tmpdir, suffix=".dmimgcalc")