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()
def generate_source_catalogs(imglist, **pars): """Generates a dictionary of source catalogs keyed by image name. Parameters ---------- imglist : list List of one or more calibrated fits images that will be used for source detection. Returns ------- sourcecatalogdict : dictionary a dictionary (keyed by image name) of two element dictionaries which in tern contain 1) a dictionary of the detector-specific processing parameters and 2) an astropy table of position and photometry information of all detected sources """ output = pars.get('output', False) sourcecatalogdict = {} for imgname in imglist: print("Image name: ", imgname) sourcecatalogdict[imgname] = {} # open image imghdu = fits.open(imgname) imgprimaryheader = imghdu[0].header instrument = imgprimaryheader['INSTRUME'].lower() detector = imgprimaryheader['DETECTOR'].lower() # get instrument/detector-specific image alignment parameters if instrument in detector_specific_params.keys(): if detector in detector_specific_params[instrument].keys(): detector_pars = detector_specific_params[instrument][detector] # to allow generate_source_catalog to get detector specific parameters detector_pars.update(pars) sourcecatalogdict[imgname]["params"] = detector_pars else: sys.exit("ERROR! Unrecognized detector '{}'. Exiting...".format(detector)) else: sys.exit("ERROR! Unrecognized instrument '{}'. Exiting...".format(instrument)) # Identify sources in image, convert coords from chip x, y form to reference WCS sky RA, Dec form. imgwcs = HSTWCS(imghdu, 1) fwhmpsf_pix = sourcecatalogdict[imgname]["params"]['fwhmpsf']/imgwcs.pscale #Convert fwhmpsf from arsec to pixels sourcecatalogdict[imgname]["catalog_table"] = amutils.generate_source_catalog(imghdu, fwhm=fwhmpsf_pix, **detector_pars) # write out coord lists to files for diagnostic purposes. Protip: To display the sources in these files in DS9, # set the "Coordinate System" option to "Physical" when loading the region file. imgroot = os.path.basename(imgname).split('_')[0] numSci = amutils.countExtn(imghdu) # Allow user to decide when and how to write out catalogs to files if output: for chip in range(1,numSci+1): regfilename = "{}_sci{}_src.reg".format(imgroot, chip) out_table = Table(sourcecatalogdict[imgname]["catalog_table"][chip]) out_table.write(regfilename, include_names=["xcentroid", "ycentroid"], format="ascii.fast_commented_header") print("Wrote region file {}\n".format(regfilename)) imghdu.close() return(sourcecatalogdict)
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)