Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge obs psfmap tmpstk #945

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions ciao_contrib/_tools/merging.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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':

Expand Down
50 changes: 44 additions & 6 deletions ciao_contrib/stacklib.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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):
Expand Down Expand Up @@ -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