Example #1
0
    def write_archive(self,fitsname=None,overwrite=no,quiet=yes):
        """ Saves a copy of the WCS keywords from the image header
            as new keywords with the user-supplied 'prepend'
            character(s) prepended to the old keyword names.

            If the file is a GEIS image and 'fitsname' != None, create
            a FITS copy and update that version; otherwise, raise
            an Exception and do not update anything.

        """
        _fitsname = fitsname

        # Open image in update mode
        #    Copying of GEIS images handled by 'openImage'.
        fimg = fileutil.openImage(self.rootname,mode='update',fitsname=_fitsname)
        if self.rootname.find('.fits') < 0 and _fitsname != None:
            # A non-FITS image was provided, and openImage made a copy
            # Update attributes to point to new copy instead
            self.geisname = self.rootname
            self.rootname = _fitsname

        # extract the extension ID being updated
        _root,_iextn = fileutil.parseFilename(self.rootname)
        _extn = fileutil.getExtn(fimg,_iextn)
        if not quiet:
            print 'Updating archive WCS keywords for ',_fitsname

        # Write out values to header...
        for key in self.orig_wcs.keys():
            _comment = None
            _dkey = self.revert[key]

            # Verify that archive keywords will not be overwritten,
            # unless overwrite=yes.
            _old_key = key in _extn.header
            if  _old_key == True and overwrite == no:
                if not quiet:
                    print 'WCS keyword',key,' already exists! Not overwriting.'
                continue

            # No archive keywords exist yet in file, or overwrite=yes...
            # Extract the value for the original keyword
            if _dkey in _extn.header:

                # Extract any comment string for the keyword as well
                _indx_key = _extn.header.indexf(_dkey)
                _full_key = _extn.header.cards[_indx_key]
                if not quiet:
                    print 'updating ',key,' with value of: ',self.orig_wcs[key]
                _extn.header[key] = (self.orig_wcs[key], _full_key.comment)

        key = 'WCSCDATE'
        if key not in _extn.header:
            # Print out history keywords to record when these keywords
            # were backed up.
            _extn.header[key] = (self.orig_wcs[key], "Time WCS keywords were copied.")

        # Close the now updated image
        fimg.close()
        del fimg
Example #2
0
    def write(self,fitsname=None,wcs=None,archive=True,overwrite=False,quiet=True):
        """
        Write out the values of the WCS keywords to the
        specified image.

        If it is a GEIS image and 'fitsname' has been provided,
        it will automatically make a multi-extension
        FITS copy of the GEIS and update that file. Otherwise, it
        throw an Exception if the user attempts to directly update
        a GEIS image header.

        If archive=True, also write out archived WCS keyword values to file.
        If overwrite=True, replace archived WCS values in file with new values.

        If a WCSObject is passed through the 'wcs' keyword, then the WCS keywords
        of this object are copied to the header of the image to be updated. A use case
        fo rthis is updating the WCS of a WFPC2 data quality (_c1h.fits) file
        in order to be in sync with the science (_c0h.fits) file.

        """
        ## Start by making sure all derived values are in sync with CD matrix
        self.update()

        image = self.rootname
        _fitsname = fitsname

        if image.find('.fits') < 0 and _fitsname != None:
            # A non-FITS image was provided, and openImage made a copy
            # Update attributes to point to new copy instead
            self.geisname = image
            image = self.rootname = _fitsname

        # Open image as writable FITS object
        fimg = fileutil.openImage(image, mode='update', fitsname=_fitsname)

        _root,_iextn = fileutil.parseFilename(image)
        _extn = fileutil.getExtn(fimg,_iextn)

        # Write out values to header...
        if wcs:
            _wcsobj = wcs
        else:
            _wcsobj = self

        for key in _wcsobj.wcstrans.keys():
            _dkey = _wcsobj.wcstrans[key]
            if _dkey != 'pscale':
                _extn.header[key] = _wcsobj.__dict__[_dkey]

        # Close the file
        fimg.close()
        del fimg
        if archive:
            self.write_archive(fitsname=fitsname,overwrite=overwrite,quiet=quiet)
Example #3
0
    def restoreWCS(self,prepend=None):
        """ Resets the WCS values to the original values stored in
            the backup keywords recorded in self.backup.
        """
        # Open header for image
        image = self.rootname

        if prepend: _prepend = prepend
        elif self.prepend: _prepend = self.prepend
        else: _prepend = None

        # Open image as writable FITS object
        fimg = fileutil.openImage(image, mode='update')
        # extract the extension ID being updated
        _root,_iextn = fileutil.parseFilename(self.rootname)
        _extn = fileutil.getExtn(fimg,_iextn)

        if len(self.backup) > 0:
            # If it knows about the backup keywords already,
            # use this to restore the original values to the original keywords
            for newkey in self.revert.keys():
                if newkey != 'opscale':
                    _orig_key = self.revert[newkey]
                    _extn.header[_orig_key] = _extn.header[newkey]
        elif _prepend:
            for key in self.wcstrans.keys():
                # Get new keyword name based on old keyname
                #    and prepend string
                if key != 'pixel scale':
                    _okey = self._buildNewKeyname(key,_prepend)

                    if _okey in _extn.header:
                        _extn.header[key] = _extn.header[_okey]
                    else:
                        print 'No original WCS values found. Exiting...'
                        break
        else:
            print 'No original WCS values found. Exiting...'

        fimg.close()
        del fimg