summaryrefslogtreecommitdiff
path: root/lib/stwcs/wcsutil/headerlet.py
diff options
context:
space:
mode:
authorhack <hack@stsci.edu>2013-10-23 16:40:19 -0400
committerhack <hack@stsci.edu>2013-10-23 16:40:19 -0400
commitd2fa46d680ca75500e77bff3593cf20c911dfe01 (patch)
tree6c5992eb504e4f8b9d88e20eb59117fe6b211a37 /lib/stwcs/wcsutil/headerlet.py
parent689ee24c6c4810dcabb3e6d95a892c976981cfb1 (diff)
downloadstwcs_hcf-d2fa46d680ca75500e77bff3593cf20c911dfe01.tar.gz
STWCS v1.1.2: This update only includes changes to the headerlet module and related TEAL interface modules. The changes include:
- Fix calls to PyFITS to allow writing of headerlet out to files - Implement support for multiple file inputs for apply_header, delete_headerlet and attach_headerlet TEAL tasks and their underlying Python interfaces. (Ticket #1063) The docstrings for all updated headerlet functions were updated to reflect the new input capabilities. git-svn-id: http://svn.stsci.edu/svn/ssb/stsci_python/stwcs/trunk@26987 fe389314-cf27-0410-b35b-8c050e845b92
Diffstat (limited to 'lib/stwcs/wcsutil/headerlet.py')
-rw-r--r--lib/stwcs/wcsutil/headerlet.py117
1 files changed, 91 insertions, 26 deletions
diff --git a/lib/stwcs/wcsutil/headerlet.py b/lib/stwcs/wcsutil/headerlet.py
index 5eff40c..935b280 100644
--- a/lib/stwcs/wcsutil/headerlet.py
+++ b/lib/stwcs/wcsutil/headerlet.py
@@ -1109,10 +1109,10 @@ def apply_headerlet_as_primary(filename, hdrlet, attach=True, archive=True,
Parameters
----------
- filename: string
- File name of science observation whose WCS solution will be updated
- hdrlet: string
- Headerlet file
+ filename: string or list of strings
+ File name(s) of science observation whose WCS solution will be updated
+ hdrlet: string or list of strings
+ Headerlet file(s), must match 1-to-1 with input filename(s)
attach: boolean
True (default): append headerlet to FITS file as a new extension.
archive: boolean
@@ -1126,9 +1126,18 @@ def apply_headerlet_as_primary(filename, hdrlet, attach=True, archive=True,
logmode: 'w' or 'a'
log file open mode
"""
-
- hlet = Headerlet.fromfile(hdrlet, logging=logging, logmode=logmode)
- hlet.apply_as_primary(filename, attach=attach, archive=archive,
+ if not isinstance(filename, list):
+ filename = [filename]
+ if not isinstance(hdrlet, list):
+ hdrlet = [hdrlet]
+ if len(hdrlet) != len(filename):
+ logger.critical("Filenames must have matching headerlets. "
+ "{0:d} filenames and {1:d} headerlets specified".format(len(filename),len(hdrlet)))
+
+ for fname,h in zip(filename,hdrlet):
+ print "Applying {0} as Primary WCS to {1}".format(h,fname)
+ hlet = Headerlet.fromfile(h, logging=logging, logmode=logmode)
+ hlet.apply_as_primary(fname, attach=attach, archive=archive,
force=force)
@@ -1140,10 +1149,10 @@ def apply_headerlet_as_alternate(filename, hdrlet, attach=True, wcskey=None,
Parameters
----------
- filename: string
- File name of science observation whose WCS solution will be updated
- hdrlet: string
- Headerlet file
+ filename: string or list of strings
+ File name(s) of science observation whose WCS solution will be updated
+ hdrlet: string or list of strings
+ Headerlet file(s), must match 1-to-1 with input filename(s)
attach: boolean
flag indicating if the headerlet should be attached as a
HeaderletHDU to fobj. If True checks that HDRNAME is unique
@@ -1159,9 +1168,18 @@ def apply_headerlet_as_alternate(filename, hdrlet, attach=True, wcskey=None,
enable file logging
logmode: 'a' or 'w'
"""
-
- hlet = Headerlet.fromfile(hdrlet, logging=logging, logmode=logmode)
- hlet.apply_as_alternate(filename, attach=attach,
+ if not isinstance(filename, list):
+ filename = [filename]
+ if not isinstance(hdrlet, list):
+ hdrlet = [hdrlet]
+ if len(hdrlet) != len(filename):
+ logger.critical("Filenames must have matching headerlets. "
+ "{0:d} filenames and {1:d} headerlets specified".format(len(filename),len(hdrlet)))
+
+ for fname,h in zip(filename,hdrlet):
+ print 'Applying {0} as an alternate WCS to {1}'.format(h,fname)
+ hlet = Headerlet.fromfile(h, logging=logging, logmode=logmode)
+ hlet.apply_as_alternate(fname, attach=attach,
wcsname=wcsname, wcskey=wcskey)
@@ -1172,24 +1190,72 @@ def attach_headerlet(filename, hdrlet, logging=False, logmode='a'):
Parameters
----------
- filename: string, HDUList
- science file to which the headerlet should be applied
- hdrlet: string or Headerlet object
- string representing a headerlet file
+ filename: HDUList or list of HDULists
+ science file(s) to which the headerlet should be applied
+ hdrlet: string, Headerlet object or list of strings or Headerlet objects
+ string representing a headerlet file(s), must match 1-to-1 input filename(s)
logging: boolean
enable file logging
logmode: 'a' or 'w'
"""
+ if not isinstance(filename, list):
+ filename = [filename]
+ if not isinstance(hdrlet, list):
+ hdrlet = [hdrlet]
+ if len(hdrlet) != len(filename):
+ logger.critical("Filenames must have matching headerlets. "
+ "{0:d} filenames and {1:d} headerlets specified".format(len(filename),len(hdrlet)))
- hlet = Headerlet.fromfile(hdrlet, logging=logging, logmode=logmode)
- hlet.attach_to_file(filename,archive=True)
+ for fname,h in zip(filename,hdrlet):
+ print 'Attaching {0} as Headerlet extension to {1}'.format(h,fname)
+ hlet = Headerlet.fromfile(h, logging=logging, logmode=logmode)
+ hlet.attach_to_file(fname,archive=True)
@with_logging
def delete_headerlet(filename, hdrname=None, hdrext=None, distname=None,
logging=False, logmode='w'):
"""
- Deletes HeaderletHDU(s) from a science file
+ Deletes HeaderletHDU(s) with same HDRNAME from science files
+
+ Notes
+ -----
+ One of hdrname, hdrext or distname should be given.
+ If hdrname is given - delete a HeaderletHDU with a name HDRNAME from fobj.
+ If hdrext is given - delete HeaderletHDU in extension.
+ If distname is given - deletes all HeaderletHDUs with a specific distortion model from fobj.
+ Updates wcscorr
+
+ Parameters
+ ----------
+ filename: string or HDUList
+ Filename can be specified as a single filename or HDUList, a comma-separated
+ list of filenames, a wild-card filename (e.g., 'j6q*flt.fits') or
+ '@-file'.
+ Each input filename (str) will be expanded as necessary to interpret
+ any environmental variables included in the filename.
+ hdrname: string or None
+ HeaderletHDU primary header keyword HDRNAME
+ hdrext: int, tuple or None
+ HeaderletHDU FITS extension number
+ tuple has the form ('HDRLET', 1)
+ distname: string or None
+ distortion model as specified in the DISTNAME keyword
+ logging: boolean
+ enable file logging
+ logmode: 'a' or 'w'
+ """
+ fnames = parseinput.parseinput(filename)[0]
+
+ for f in fnames:
+ print "Deleting Headerlet from ",f
+ _delete_single_headerlet(f, hdrname=hdrname, hdrext=hdrext,
+ distname=distname, logging=logging, logmode='a')
+
+def _delete_single_headerlet(filename, hdrname=None, hdrext=None, distname=None,
+ logging=False, logmode='w'):
+ """
+ Deletes HeaderletHDU(s) from a SINGLE science file
Notes
-----
@@ -1216,7 +1282,6 @@ def delete_headerlet(filename, hdrname=None, hdrext=None, distname=None,
enable file logging
logmode: 'a' or 'w'
"""
-
hdrlet_ind = find_headerlet_HDUs(filename, hdrname=hdrname, hdrext=hdrext,
distname=distname, logging=logging, logmode='a')
if len(hdrlet_ind) == 0:
@@ -1712,11 +1777,9 @@ class Headerlet(pyfits.HDUList):
self.logging = logging
init_logging('class Headerlet', level=logging, mode=logmode)
- #fobj, fname, close_file = parse_filename(fobj)
super(Headerlet, self).__init__(hdus, file=file)
- if hdus == []:
- return
+ def init_attrs(self):
self.fname = self.filename()
self.hdrname = self[0].header["HDRNAME"]
self.wcsname = self[0].header["WCSNAME"]
@@ -1728,6 +1791,7 @@ class Headerlet(pyfits.HDUList):
self.npolfile = self[0].header["NPOLFILE"]
self.d2imfile = self[0].header["D2IMFILE"]
self.distname = self[0].header["DISTNAME"]
+
try:
self.vafactor = self[("SIPWCS", 1)].header.get("VAFACTOR", 1) #None instead of 1?
except (IndexError, KeyError):
@@ -1751,6 +1815,8 @@ class Headerlet(pyfits.HDUList):
save_backup=False, logging=False, logmode='w', **kwargs):
hlet = super(cls, cls).fromfile(fileobj, mode, memmap, save_backup,
**kwargs)
+ if len(hlet) > 0:
+ hlet.init_attrs()
hlet.logging = logging
init_logging('class Headerlet', level=logging, mode=logmode)
return hlet
@@ -2256,7 +2322,6 @@ class Headerlet(pyfits.HDUList):
DESTIM in the primary header of the headerlet must match ROOTNAME
of the science file (or the name of the destination file)
"""
-
try:
if not isinstance(dest, pyfits.HDUList):
droot = pyfits.getval(dest, 'ROOTNAME')