diff --git a/ciao_contrib/_tools/merging.py b/ciao_contrib/_tools/merging.py index 9aebc982..d72ab748 100644 --- a/ciao_contrib/_tools/merging.py +++ b/ciao_contrib/_tools/merging.py @@ -49,6 +49,7 @@ import ciao_contrib._tools.utils as utils from ciao_contrib._tools.headers import HeaderMerge +from ciao_contrib.stacklib import TemporaryStack __all__ = ( "match_obsid", @@ -2869,15 +2870,16 @@ def merge_psfmaps(mergetype, psfmap, psfmap_files, expmap_files, outfile = "{}[PSFMAP]".format(psfmap) if mergetype in dmfilttypes: - run.punlearn("dmimgfilt") - args = ["infile={}".format(','.join(psfmap_files)), - "outfile={}".format(outfile), - "function={}".format(mergetype), - "mask=point(0,0)", - "lookupTab={}".format(lookupTable), - "clobber={}".format(clstr), - "verbose={}".format(verbose)] - run.run("dmimgfilt", args) + with TemporaryStack(psfmap_files, dir=tmpdir) as tmp_stk: + run.punlearn("dmimgfilt") + args = ["infile=@{}".format(tmp_stk.name), + "outfile={}".format(outfile), + "function={}".format(mergetype), + "mask=point(0,0)", + "lookupTab={}".format(lookupTable), + "clobber={}".format(clstr), + "verbose={}".format(verbose)] + run.run("dmimgfilt", args) elif mergetype == 'exptime': diff --git a/ciao_contrib/stacklib.py b/ciao_contrib/stacklib.py index f3e7297e..d9ffb777 100644 --- a/ciao_contrib/stacklib.py +++ b/ciao_contrib/stacklib.py @@ -1,13 +1,11 @@ - -# Python35Support - # -# Copyright (C) 2011, 2012, 2016, 2019 Smithsonian Astrophysical Observatory +# Copyright (C) 2011, 2012, 2016, 2019, 2025 +# Smithsonian Astrophysical Observatory # # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2 of the License, or +# the Free Software Foundation; either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, @@ -32,9 +30,10 @@ import os import tempfile +import contextlib -__all__ = ("make_stackfile", ) +__all__ = ("make_stackfile", "TemporaryStack") def make_stackfile(infiles, dir="/tmp/", suffix='', delete=True): @@ -68,4 +67,43 @@ def make_stackfile(infiles, dir="/tmp/", suffix='', delete=True): tfh.flush() return tfh + +class TemporaryStack(contextlib.AbstractContextManager): + ''' + A context manager that will create a stack for a list of values. + + If abspath=True, then it will save the values in the tempfile + with the full path name. If false then it will save the values + with a preceeding "!" so as not to append the path to the satck + file onto the values. + ''' + + def __init__(self, values, abspath=False, *args, **kwargs): + 'Create temp stack and populate it with values' + + # use ASCDS_WORK_PATH for tmpdir if not set + if 'dir' not in kwargs: + kwargs['dir'] = os.environ["ASCDS_WORK_PATH"] + + # use ".lis" for suffix if not set + if 'suffix' not in kwargs: + kwargs['suffix'] = ".lis" + + # Use 'w' so stack is always overwritten + self.tfh = tempfile.NamedTemporaryFile(mode='w', *args,**kwargs) + + for val in values: + if abspath: + putstr = os.path.abspath(str(val)) + else: + putstr = "!"+str(val) + self.tfh.write(putstr+"\n") + + self.tfh.flush() + self.name = self.tfh.name + + def __exit__(self, exc_type, exc_value, traceback): + 'Close the stack and delete file' + self.tfh.close() + # End