Esempio n. 1
0
def update_image_wcs_info(tweakwcs_output,imagelist):
    """Write newly computed WCS information to image headers

    Parameters
    ----------
    tweakwcs_output : list
        output of tweakwcs. Contains sourcelist tables, newly computed WCS info, etc. for every chip of every valid
        input image.

    imagelist : list
        list of valid processed images to be updated

    Returns
    -------
    Nothing!
    """
    imgctr = 0
    for item in tweakwcs_output:
        #print('YYYYYYYYY',item.wcs.pscale)
        if item.meta['chip'] == 1:  # to get the image name straight regardless of the number of chips
            image_name = imagelist[imgctr]
            if imgctr > 0: #close previously opened image
                print("CLOSE {}".format(hdulist[0].header['FILENAME'])) #TODO: Remove before deployment
                hdulist.flush()
                hdulist.close()
            hdulist = fits.open(image_name, mode='update')
            sciExtDict = {}
            for sciExtCtr in range(1, amutils.countExtn(hdulist) + 1): #establish correct mapping to the science extensions
                sciExtDict["{}".format(sciExtCtr)] = fileutil.findExtname(hdulist,'sci',extver=sciExtCtr)
            imgctr += 1
        updatehdr.update_wcs(hdulist, sciExtDict["{}".format(item.meta['chip'])], item.wcs, wcsname='TWEAKDEV', reusename=True, verbose=True) #TODO: May want to settle on a better name for 'wcsname'
        print()
    print("CLOSE {}".format(hdulist[0].header['FILENAME'])) #TODO: Remove before deployment
    hdulist.flush() #close last image
    hdulist.close()
Esempio n. 2
0
def update_image_wcs(image, ext, wcs, wcsname=None):
    """
    Updates the WCS of the specified extension with the new WCS
    after archiving the original WCS.

    Parameters
    ----------
    image : str, astropy.io.fits.HDUList
        Filename of image with WCS that needs to be updated

    ext : int, str or tuple of (string, int)
       The key identifying the HDU.  If ``ext`` is a tuple, it is of the
       form ``(name, ver)`` where ``ver`` is an ``EXTVER`` value that must
       match the HDU being searched for.

       If the key is ambiguous (e.g. there are multiple 'SCI' extensions)
       the first match is returned.  For a more precise match use the
       ``(name, ver)`` pair.

       If even the ``(name, ver)`` pair is ambiguous (it shouldn't be
       but it's not impossible) the numeric index must be used to index
       the duplicate HDU.

    wcs : object
        Full HSTWCS object which will replace/update the existing WCS

    wcsname : str, None, optional
        Label to give newly updated WCS. The default value will set the
        WCS name to `SUBPIXAL`.

    """
    close = False

    try:
        if not isinstance(image, fits.HDUList):
            image = fits.open(image, mode='update')
            close = True

        extnum = image.index_of(ext)

        if wcsname is None or wcsname.strip().upper() in ['', 'NONE', 'INDEF']:
            wcsname = 'SUBPIXAL'

        updatehdr.update_wcs(image,
                             extnum,
                             wcs,
                             wcsname=wcsname,
                             reusename=True,
                             verbose=False)

    except:
        raise

    finally:
        if close:
            image.close()
Esempio n. 3
0
def update_image_wcs_info(tweakwcs_output):
    """Write newly computed WCS information to image headers and write headerlet files

        Parameters
        ----------
        tweakwcs_output : list
            output of tweakwcs. Contains sourcelist tables, newly computed WCS info, etc. for every chip of every valid
            input image.

        Returns
        -------
        out_headerlet_list : dictionary
            a dictionary of the headerlet files created by this subroutine, keyed by flt/flc fits filename.
        """
    out_headerlet_dict = {}
    for item in tweakwcs_output:
        imageName = item.meta['filename']
        chipnum = item.meta['chip']
        if chipnum == 1:
            chipctr = 1
            hdulist = fits.open(imageName, mode='update')
            num_sci_ext = amutils.countExtn(hdulist)

            # generate wcs name for updated image header, headerlet
            if not hdulist['SCI', 1].header['WCSNAME'] or hdulist[
                    'SCI', 1].header[
                        'WCSNAME'] == "":  #Just in case header value 'wcsname' is empty.
                wcsName = "FIT_{}".format(item.meta['catalog_name'])
            else:
                wname = hdulist['sci', 1].header['wcsname']
                if "-" in wname:
                    wcsName = '{}-FIT_{}'.format(
                        wname[:wname.index('-')],
                        item.meta['tweakwcs_info']['catalog'])
                else:
                    wcsName = '{}-FIT_{}'.format(
                        wname, item.meta['tweakwcs_info']['catalog'])

            # establish correct mapping to the science extensions
            sciExtDict = {}
            for sciExtCtr in range(1, num_sci_ext + 1):
                sciExtDict["{}".format(sciExtCtr)] = fileutil.findExtname(
                    hdulist, 'sci', extver=sciExtCtr)

        # update header with new WCS info
        updatehdr.update_wcs(hdulist,
                             sciExtDict["{}".format(item.meta['chip'])],
                             item.wcs,
                             wcsname=wcsName,
                             reusename=True,
                             verbose=True)
        if chipctr == num_sci_ext:
            # Close updated flc.fits or flt.fits file
            print("CLOSE {}\n".format(
                imageName))  # TODO: Remove before deployment
            hdulist.flush()
            hdulist.close()

            # Create headerlet
            out_headerlet = headerlet.create_headerlet(imageName,
                                                       hdrname=wcsName,
                                                       wcsname=wcsName)

            # Update headerlet
            update_headerlet_phdu(item, out_headerlet)

            # Write headerlet
            if imageName.endswith("flc.fits"):
                headerlet_filename = imageName.replace("flc", "flt_hlet")
            if imageName.endswith("flt.fits"):
                headerlet_filename = imageName.replace("flt", "flt_hlet")
            out_headerlet.writeto(headerlet_filename, clobber=True)
            print("Wrote headerlet file {}.\n\n".format(headerlet_filename))
            out_headerlet_dict[imageName] = headerlet_filename

        chipctr += 1
    return (out_headerlet_dict)