def updateHeader(self, wcsname=None, reusename=False):
        """ Update header of image with shifts computed by *perform_fit()*.
        """
        # Insure filehandle is open and available...
        self.openFile()

        verbose_level = 1
        if not self.perform_update:
            verbose_level = 0
        # Create WCSCORR table to keep track of WCS revisions anyway
        if self.perform_update:
            wcscorr.init_wcscorr(self._im.hdu)

        extlist = []
        wcscorr_extname = self.ext_name
        if self.ext_name == "PRIMARY":
            extlist = [0]
        else:
            for ext in range(1,self.nvers+1):
                extlist.append((self.ext_name,ext))
                # add WCSNAME to SCI headers, if not provided (such as for
                # drizzled images directly obtained from the archive pre-AD)
                if ('wcsname' not in self._im.hdu[self.ext_name,ext].header and
                    self._im.hdu.fileinfo(0)['filemode'] == 'update'):
                    self._im.hdu[self.ext_name,ext].header['wcsname'] = 'Default'

        if not self.identityfit and self.goodmatch and \
                self.fit['offset'][0] != np.nan:
            updatehdr.updatewcs_with_shift(self._im.hdu, self.refWCS,
                wcsname=wcsname, reusename=reusename,
                fitgeom=self.fit_pars['fitgeometry'],
                xsh=self.fit['offset'][0],ysh=self.fit['offset'][1],
                rot=self.fit['rot'],scale=self.fit['scale'][0],
                fit=self.fit['fit_matrix'], verbose=verbose_level,
                xrms=self.fit['rms_keys']['RMS_RA'],
                yrms=self.fit['rms_keys']['RMS_DEC'])

            wnames = altwcs.wcsnames(self._im.hdu,ext=extlist[0])

            altkeys = []
            for k in wnames:
                if wnames[k] == wcsname:
                    altkeys.append(k)
            if len(altkeys) > 1 and ' ' in altkeys:
                altkeys.remove(' ')
            if len(altkeys) == 0:
                next_key = ' '
            else:
                next_key = altkeys[-1]
            if self.perform_update:
                log.info('    Writing out new WCS to alternate WCS: "%s"'%next_key)

            self.next_key = next_key
        else: #if self.identityfit or not self.goodmatch:
            if reusename:
                # Look for key of WCS with this name
                next_key = altwcs.getKeyFromName(self._im.hdu[extlist[0]].header,wcsname)
                # This wcsname is new, so start fresh
                if next_key is None:
                    next_key = altwcs.next_wcskey(self._im.hdu[extlist[0]].header)
            else:
                # Find key for next WCS and save again to replicate an updated solution
                next_key = altwcs.next_wcskey(self._im.hdu[extlist[0]].header)

            if self.perform_update:
                # archive current WCS as alternate WCS with specified WCSNAME
                # Start by archiving original PRIMARY WCS
                wnames = altwcs.wcsnames(self._im.hdu,ext=extlist[0])

                # Define a default WCSNAME in the case that the file to be
                # updated did not have the WCSNAME keyword defined already
                # (as will happen when updating images that have not been
                #  updated using updatewcs).
                if len(wnames) == 0:
                    pri_wcsname = None
                else:
                    # Safeguard against headers not having WCSNAME defined
                    # This would occur if they were written out by something
                    # other than stwcs.updatewcs v
                    if ' ' not in wnames:
                        self._im.hdu[extlist[0]].header['wscname'] = ''
                        wnames[' '] = ''
                    pri_wcsname = wnames[' ']

                next_pkey = altwcs.getKeyFromName(fits.getheader(self.name, extlist[0], memmap=False),pri_wcsname)
                log.info('    Saving Primary WCS to alternate WCS: "%s"'%next_pkey)

                altwcs.archiveWCS(self._im.hdu, extlist,
                                    wcskey=next_pkey, wcsname=pri_wcsname,
                                    reusekey=True)
                if reusename:
                    # Look for key of WCS with this name
                    next_key = altwcs.getKeyFromName(self._im.hdu[extlist[0]].header,wcsname)
                    # This wcsname is new, so start fresh
                    if next_key is None:
                        next_key = altwcs.next_wcskey(self._im.hdu[extlist[0]].header)
                else:
                    # Find key for next WCS and save again to replicate an updated solution
                    next_key = altwcs.next_wcskey(self._im.hdu[extlist[0]].header)
                    # update WCSNAME to be the new name
                    for ext in extlist:
                        self._im.hdu[ext].header['WCSNAME'] = wcsname

                # save again using new WCSNAME
                altwcs.archiveWCS(self._im.hdu, extlist,
                    wcskey=next_key,wcsname=wcsname, reusekey=reusename)
            self.next_key = ' '

        # add FIT values to image's PRIMARY header
        fimg = self._im.hdu

        if wcsname in ['',' ',None,"INDEF"]:
            wcsname = 'TWEAK'
        # Record values for the fit with both the PRIMARY WCS being updated
        # and the alternate WCS which will be created.
        assert(not self._im.closed)

        for ext in extlist:
            self._im.hdu[ext].header['FITNAME'+next_key] = wcsname
            for kw in self.fit['rms_keys']:
                self._im.hdu[ext].header.set(kw+next_key,
                                     self.fit['rms_keys'][kw],
                                     after='FITNAME'+next_key)

        if self.perform_update:
            log.info('Updating WCSCORR table with new WCS solution "%s"'%wcsname)
            wcscorr.update_wcscorr(self._im.hdu, wcs_id=wcsname,
                                   extname=self.ext_name)
def updateHeader ( self , wcsname = None , reusename = False ) : self . openFile ( ) verbose_level = 1 if not self . perform_update : verbose_level = 0 if self . perform_update : wcscorr . init_wcscorr ( self . _im . hdu ) extlist = [ ] wcscorr_extname = self . ext_name if self . ext_name == "PRIMARY" : extlist = [ 0 ] else : for ext in range ( 1 , self . nvers + 1 ) : extlist . append ( ( self . ext_name , ext ) ) if ( 'wcsname' not in self . _im . hdu [ self . ext_name , ext ] . header and self . _im . hdu . fileinfo ( 0 ) [ 'filemode' ] == 'update' ) : self . _im . hdu [ self . ext_name , ext ] . header [ 'wcsname' ] = 'Default' if not self . identityfit and self . goodmatch and self . fit [ 'offset' ] [ 0 ] != np . nan : updatehdr . updatewcs_with_shift ( self . _im . hdu , self . refWCS , wcsname = wcsname , reusename = reusename , fitgeom = self . fit_pars [ 'fitgeometry' ] , xsh = self . fit [ 'offset' ] [ 0 ] , ysh = self . fit [ 'offset' ] [ 1 ] , rot = self . fit [ 'rot' ] , scale = self . fit [ 'scale' ] [ 0 ] , fit = self . fit [ 'fit_matrix' ] , verbose = verbose_level , xrms = self . fit [ 'rms_keys' ] [ 'RMS_RA' ] , yrms = self . fit [ 'rms_keys' ] [ 'RMS_DEC' ] ) wnames = altwcs . wcsnames ( self . _im . hdu , ext = extlist [ 0 ] ) altkeys = [ ] for k in wnames : if wnames [ k ] == wcsname : altkeys . append ( k ) if len ( altkeys ) > 1 and ' ' in altkeys : altkeys . remove ( ' ' ) if len ( altkeys ) == 0 : next_key = ' ' else : next_key = altkeys [ - 1 ] if self . perform_update : log . info ( '    Writing out new WCS to alternate WCS: "%s"' % next_key ) self . next_key = next_key else : if reusename : next_key = altwcs . getKeyFromName ( self . _im . hdu [ extlist [ 0 ] ] . header , wcsname ) if next_key is None : next_key = altwcs . next_wcskey ( self . _im . hdu [ extlist [ 0 ] ] . header ) else : next_key = altwcs . next_wcskey ( self . _im . hdu [ extlist [ 0 ] ] . header ) if self . perform_update : wnames = altwcs . wcsnames ( self . _im . hdu , ext = extlist [ 0 ] ) if len ( wnames ) == 0 : pri_wcsname = None else : if ' ' not in wnames : self . _im . hdu [ extlist [ 0 ] ] . header [ 'wscname' ] = '' wnames [ ' ' ] = '' pri_wcsname = wnames [ ' ' ] next_pkey = altwcs . getKeyFromName ( fits . getheader ( self . name , extlist [ 0 ] , memmap = False ) , pri_wcsname ) log . info ( '    Saving Primary WCS to alternate WCS: "%s"' % next_pkey ) altwcs . archiveWCS ( self . _im . hdu , extlist , wcskey = next_pkey , wcsname = pri_wcsname , reusekey = True ) if reusename : next_key = altwcs . getKeyFromName ( self . _im . hdu [ extlist [ 0 ] ] . header , wcsname ) if next_key is None : next_key = altwcs . next_wcskey ( self . _im . hdu [ extlist [ 0 ] ] . header ) else : next_key = altwcs . next_wcskey ( self . _im . hdu [ extlist [ 0 ] ] . header ) for ext in extlist : self . _im . hdu [ ext ] . header [ 'WCSNAME' ] = wcsname altwcs . archiveWCS ( self . _im . hdu , extlist , wcskey = next_key , wcsname = wcsname , reusekey = reusename ) self . next_key = ' ' fimg = self . _im . hdu if wcsname in [ '' , ' ' , None , "INDEF" ] : wcsname = 'TWEAK' assert ( not self . _im . closed ) for ext in extlist : self . _im . hdu [ ext ] . header [ 'FITNAME' + next_key ] = wcsname for kw in self . fit [ 'rms_keys' ] : self . _im . hdu [ ext ] . header . set ( kw + next_key , self . fit [ 'rms_keys' ] [ kw ] , after = 'FITNAME' + next_key ) if self . perform_update : log . info ( 'Updating WCSCORR table with new WCS solution "%s"' % wcsname ) wcscorr . update_wcscorr ( self . _im . hdu , wcs_id = wcsname , extname = self . ext_name )
Update header of image with shifts computed by *perform_fit()*.