def _process_input_wcs_single(fname, wcskey, updatewcs): """ See docs for _process_input_wcs. This is separated to be spawned in parallel. """ if wcskey in ['', ' ', 'INDEF', None]: if updatewcs: uw.updatewcs(fname, checkfiles=False) else: numext = fileutil.countExtn(fname) extlist = [] for extn in range(1, numext + 1): extlist.append(('SCI', extn)) if wcskey in string.ascii_uppercase: wkey = wcskey wname = ' ' else: wname = wcskey wkey = ' ' altwcs.restoreWCS(fname, extlist, wcskey=wkey, wcsname=wname) # make an asn table at the end # Make sure there is a WCSCORR table for each input image if wcskey not in ['', ' ', 'INDEF', None] or updatewcs: wcscorr.init_wcscorr(fname)
def updatewcs_with_shift(image,reference,wcsname=None, reusename=False, fitgeom='rscale', rot=0.0,scale=1.0,xsh=0.0,ysh=0.0,fit=None, xrms=None, yrms = None, verbose=False,force=False,sciext='SCI'): """ Update the SCI headers in 'image' based on the fit provided as determined in the WCS specified by 'reference'. The fit should be a 2-D matrix as generated for use with 'make_vector_plot()'. Notes ----- The algorithm used to apply the provided fit solution to the image involves applying the following steps to the WCS of each of the input image's chips: 1. compute RA/Dec with full distortion correction for reference point as (Rc_i,Dc_i) 2. find the Xc,Yc for each Rc_i,Dc_i and get the difference from the CRPIX position for the reference WCS as (dXc_i,dYc_i) 3. apply fit (rot&scale) to (dXc_i,dYc_i) then apply shift, then add CRPIX back to get new (Xcs_i,Ycs_i) position 4. compute (Rcs_i,Dcs_i) as the sky coordinates for (Xcs_i,Ycs_i) 5. compute delta of (Rcs_i-Rc_i, Dcs_i-Dcs_i) as (dRcs_i,dDcs_i) 6. apply the fit to the chip's undistorted CD matrix, the apply linear distortion terms back in to create a new CD matrix 7. add (dRcs_i,dDcs_i) to CRVAL of the reference chip's WCS 8. update header with new WCS values Parameters ---------- image : str or PyFITS.HDUList object Filename, or PyFITS object, of image with WCS to be updated. All extensions with EXTNAME matches the value of the 'sciext' parameter value (by default, all 'SCI' extensions) will be updated. reference : str Filename of image/headerlet (FITS file) which contains the WCS used to define the tangent plane in which all the fit parameters (shift, rot, scale) were measured. wcsname : str Label to give to new WCS solution being created by this fit. If a value of None is given, it will automatically use 'TWEAK' as the label. If a WCS has a name with this specific value, the code will automatically append a version ID using the format '_n', such as 'TWEAK_1', 'TWEAK_2',or 'TWEAK_update_1'. [Default =None] reusename : bool User can specify whether or not to over-write WCS with same name. [Default: False] rot : float Amount of rotation measured in fit to be applied. [Default=0.0] scale : float Amount of scale change measured in fit to be applied. [Default=1.0] xsh : float Offset in X pixels from defined tangent plane to be applied to image. [Default=0.0] ysh : float Offset in Y pixels from defined tangent plane to be applied to image. [Default=0.0] fit : arr Linear coefficients for fit [Default = None] xrms : float RMS of fit in RA (in decimal degrees) that will be recorded as CRDER1 in WCS and header [Default = None] yrms : float RMS of fit in Dec (in decimal degrees) that will be recorded as CRDER2 in WCS and header [Default = None] verbose : bool Print extra messages during processing? [Default=False] force : bool Update header even though WCS already exists with this solution or wcsname? [Default=False] sciext : string Value of FITS EXTNAME keyword for extensions with WCS headers to be updated with the fit values. [Default='SCI'] """ # if input reference is a ref_wcs file from tweakshifts, use it if isinstance(reference, wcsutil.HSTWCS) or isinstance(reference, pywcs.WCS): wref = reference else: refimg = fits.open(reference, memmap=False) wref = None for extn in refimg: if 'extname' in extn.header and extn.header['extname'] == 'WCS': wref = pywcs.WCS(refimg['wcs'].header) break refimg.close() # else, we have presumably been provided a full undistorted image # as a reference, so use it with HSTWCS instead if wref is None: wref = wcsutil.HSTWCS(reference) if isinstance(image, fits.HDUList): open_image = False filename = image.filename() if image.fileinfo(0)['filemode'] is 'update': image_update = True else: image_update = False else: open_image = True filename = image image_update = None # Now that we are sure we have a good reference WCS to use, # continue with the update logstr = "....Updating header for {:s}...".format(filename) if verbose: print("\n{:s}\n".format(logstr)) else: log.info(logstr) # reset header WCS keywords to original (OPUS generated) values extlist = get_ext_list(image, extname='SCI') if extlist: if image_update: # Create initial WCSCORR extension wcscorr.init_wcscorr(image,force=force) else: extlist = [0] # insure that input PRIMARY WCS has been archived before overwriting # with new solution if open_image: fimg = fits.open(image, mode='update', memmap=False) image_update = True else: fimg = image if image_update: wcsutil.altwcs.archiveWCS(fimg,extlist,reusekey=True) # Process MEF images... for ext in extlist: logstr = "Processing {:s}[{:s}]".format(fimg.filename(), ext2str(ext)) if verbose: print("\n{:s}\n".format(logstr)) else: log.info(logstr) chip_wcs = wcsutil.HSTWCS(fimg,ext=ext) update_refchip_with_shift(chip_wcs, wref, fitgeom=fitgeom, rot=rot, scale=scale, xsh=xsh, ysh=ysh, fit=fit, xrms=xrms, yrms=yrms) #if util.is_blank(wcsname): #wcsname = 'TWEAK' # Update FITS file with newly updated WCS for this chip extnum = fimg.index(fimg[ext]) update_wcs(fimg, extnum, chip_wcs, wcsname=wcsname, reusename=reusename, verbose=verbose) if open_image: fimg.close()