def updateObs(self, obsname): """Update observation with any available solutions. Parameters ========== obsname : str Filename for observation to be updated """ if not self.perform_step: return # Parse observation name obspath, obsroot = os.path.split(obsname) # use `obspath` for location of output files, # if anything gets written out observationID = obsroot.split('_')[:1][0] logger.info("Updating astrometry for {}".format(observationID)) # # apply to file... fileobj = pf.open(obsname, mode='update') # take inventory of what hdrlets are already appended to this file hdrnames = headerlet.get_headerlet_kw_names(fileobj, 'hdrname') headerlets, best_solution_id = self.getObservation(observationID) if headerlets is None: logger.warning("Problems getting solutions from database") logger.warning( " NO Updates performed for {}".format(observationID)) if self.raise_errors: raise ValueError("No new solution found in AstrometryDB.") else: return # If no headerlet found in database, update database with this WCS if self.new_observation: logger.warning(" No new solution found in AstrometryDB.") logger.warning( " Updating database with initial WCS {}".format(observationID)) hlet_buffer = BytesIO() hlet_new = headerlet.create_headerlet(fileobj) newhdrname = hlet_new[0].header['hdrname'] hlet_new.writeto(hlet_buffer) logger.info("Updating AstrometryDB with entry for {}".format( observationID)) logger.info("\t using WCS with HDRNAME={}".format(newhdrname)) # Add WCS solution from this observation to the database self.addObservation(observationID, hlet_buffer) else: # Attach new unique hdrlets to file... logger.info("Updating {} with:".format(observationID)) for h in headerlets: newhdrname = headerlets[h][0].header['hdrname'] if newhdrname in hdrnames: continue # do not add duplicate hdrlets # Add solution as an alternate WCS try: if best_solution_id and newhdrname == best_solution_id: # replace primary WCS with this solution headerlets[h].apply_as_primary(fileobj) logger.info('Replacing primary WCS with') logger.info( '\tHeaderlet with HDRNAME={}'.format(newhdrname)) else: logger.info( "\tHeaderlet with HDRNAME={}".format(newhdrname)) headerlets[h].attach_to_file(fileobj) except ValueError: pass fileobj.close()
def apply_shifts(event): flt_file = event['fits_s3_key'] flt_file_bucket = event['fits_s3_bucket'] root = flt_file.split('/')[-1].split('_')[0] root_file = flt_file.split('/')[-1] s3 = boto3.resource('s3') s3_client = boto3.client('s3') bkt = s3.Bucket(flt_file_bucket) bkt.download_file(flt_file, '/tmp/{0}'.format(root_file), ExtraArgs={"RequestPayer": "requester"}) im = fits.open('/tmp/{0}'.format(root_file), mode='update') refframe = im[0].header['refframe'].strip() if refframe == 'ICRS': refframe = 'GSC2-GAIA' if refframe == '': refframe = 'GSC1-GAIA' # else: # refframe = '{}-GAIA'.format(refframe) # Determine shifts from GSC catalog serviceEndPoint = 'https://gsss.stsci.edu/webservices/GSCconvert/GSCconvert.aspx?TRANSFORM={0}&IPPPSSOOT={1}'.format( refframe, root) print(serviceEndPoint) headers = {'Content-Type': 'text/xml'} try: refRequest = requests.get(serviceEndPoint, headers=headers) except Exception: return print(refRequest.content) """ Request returns the following information: <ReferenceUpdate observationID="ib6v06c4q"> <name>GS</name> <inputCatalog>GSC23</inputCatalog> <outputCatalog>GSC240</outputCatalog> <dGS>S0W5147815</dGS> <dGSinputRA>5.85821717</dGSinputRA> <dGSinputDEC>-72.20652745</dGSinputDEC> <dGSinputEpoch>2010.43566895</dGSinputEpoch> <dGSoutputRA>5.85879994</dGSoutputRA> <dGSoutputDEC>-72.20663689</dGSoutputDEC> <dGSoutputEpoch>2010.43561600</dGSoutputEpoch> <dGSraCorrection>0.00058278</dGSraCorrection> <dGSdecCorrection>-0.00010944</dGSdecCorrection> <dGSxiCorrection>0.64111482</dGSxiCorrection> <dGSetaCorrection>-0.39399528</dGSetaCorrection> <sGS>S0W5000579</sGS> <sGSinputRA>4.87186721</sGSinputRA> <sGSinputDEC>-72.18208800</sGSinputDEC> <sGSinputEpoch>2010.43566895</sGSinputEpoch> <sGSoutputRA>4.87235426</sGSoutputRA> <sGSoutputDEC>-72.18211243</sGSoutputDEC> <sGSoutputEpoch>2010.43561600</sGSoutputEpoch> <sGSraCorrection>0.00048706</sGSraCorrection> <sGSdecCorrection>-0.00002443</sGSdecCorrection> <sGSxiCorrection>0.53652883</sGSxiCorrection> <sGSetaCorrection>-0.08793566</sGSetaCorrection> <inputSep>1089.36339581</inputSep> <inputPA>274.16279727</inputPA> <outputSep>1089.48923968</outputSep> <outputPA>274.17836310</outputPA> <FGSlock> </FGSlock> <RefFrame>ICRS</RefFrame> <MTflag> </MTflag> <deltaRA>0.00058278</deltaRA> <deltaDEC>-0.00010944</deltaDEC> <deltaXI>0.64111482</deltaXI> <deltaETA>-0.39399528</deltaETA> <deltaROT>0.01556583</deltaROT> <deltaSCALE>1.00011552</deltaSCALE> <Nstars>2</Nstars> <status>200</status> <msg>Success : Dgstar (S0W5147815) + Sgstar (S0W5000579) correction applied</msg> </ReferenceUpdate>" """ refXMLtree = etree.fromstring(refRequest.content) deltaRA = float(refXMLtree.findtext('deltaRA')) deltaDEC = float(refXMLtree.findtext('deltaDEC')) deltaROT = float(refXMLtree.findtext('deltaROT')) deltaSCALE = float(refXMLtree.findtext('deltaSCALE')) old_gs = (float(refXMLtree.findtext('dGSinputRA')), float(refXMLtree.findtext('dGSinputDEC'))) new_gs = (float(refXMLtree.findtext('dGSoutputRA')), float(refXMLtree.findtext('dGSoutputDEC'))) # Update file with astrometric GS corrections wcsName = 'AWSUpdate' print('Calling shift_exposure for: {}'.format(root)) im = shift_exposure(im, old_gs, new_gs, wcsname=wcsName, deltaRA=deltaRA, deltaDEC=deltaDEC) """ # Build reference frame im_wcs = wcs.WCS(im[1].header, relax=True, fobj=im) ref_wcs = output_wcs([im_wcs]) # convert deltaRA/deltaDEC(deg) to pixels ref_wcs_shift = output_wcs([im_wcs]) ref_wcs_shift.wcs.crval += (deltaRA, deltaDEC) ref_pix = (2048, 1024) ref_sky_shift = ref_wcs_shift.all_pix2world(ref_pix[0],ref_pix[1],1) ref_pix_shift = ref_wcs.all_world2pix(ref_sky_shift[0], ref_sky_shift[1],1) delta_x = ref_pix_shift[0] - ref_pix[0] delta_y = ref_pix_shift[1] - ref_pix[1] print('Calling updatewcs_with_shift') print(root) updatewcs_with_shift(im, ref_wcs, xsh=delta_x, ysh=delta_y,wcsname=wcsName) """ # Create headerlet with updated, a priori-corrected WCS hdrName = '{}_AWS_hdrlet'.format(root) author = 'AWS' descrip = 'Test shift done on AWS' nmatch = 0 catalog = 'No catalog' hdrlet = headerlet.create_headerlet(im, hdrname=hdrName, wcsname=wcsName, author=author, descrip=descrip, nmatch=nmatch, catalog=catalog) hdrlet_file = "{}.fits".format(hdrName) hdrlet.tofile(hdrlet_file, clobber=True) """
def update_image_wcs_info(tweakwcs_output, headerlet_filenames=None, fit_label=None): """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 every valid input image. headerlet_filenames : dictionary, optional dictionary that maps the flt/flc.fits file name to the corresponding custom headerlet filename. 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: image_name = item.meta['filename'] chipnum = item.meta['chip'] hdulist = fits.open(image_name, mode='update') if chipnum == 1: chipctr = 1 num_sci_ext = amutils.countExtn(hdulist) # generate wcs name for updated image header, headerlet # Just in case header value 'wcs_name' is empty. if fit_label is None: if 'relative' in item.meta['fit method']: fit_label = 'REL' else: fit_label = 'IMG' if not hdulist['SCI', 1].header['WCSNAME'] or hdulist[ 'SCI', 1].header['WCSNAME'] == "": wcs_name = "FIT_{}_{}".format(fit_label, item.meta['catalog_name']) else: wname = hdulist['sci', 1].header['wcsname'] if "-" in wname: wcs_name = '{}-FIT_{}_{}'.format( wname[:wname.index('-')], fit_label, item.meta['fit_info']['catalog']) else: wcs_name = '{}-FIT_{}_{}'.format( wname, fit_label, item.meta['fit_info']['catalog']) # establish correct mapping to the science extensions sci_ext_dict = {} for sci_ext_ctr in range(1, num_sci_ext + 1): sci_ext_dict["{}".format(sci_ext_ctr)] = fileutil.findExtname( hdulist, 'sci', extver=sci_ext_ctr) # update header with new WCS info sci_extn = sci_ext_dict["{}".format(item.meta['chip'])] hdr_name = "{}_{}-hlet.fits".format(image_name.rstrip(".fits"), wcs_name) updatehdr.update_wcs(hdulist, sci_extn, item.wcs, wcsname=wcs_name, reusename=True) info = item.meta['fit_info'] hdulist[sci_extn].header['RMS_RA'] = info['RMS_RA'].value if info[ 'RMS_RA'] is not None else -1.0 hdulist[sci_extn].header['RMS_DEC'] = info['RMS_DEC'].value if info[ 'RMS_DEC'] is not None else -1.0 hdulist[sci_extn].header['CRDER1'] = info['RMS_RA'].value if info[ 'RMS_RA'] is not None else -1.0 hdulist[sci_extn].header['CRDER2'] = info['RMS_DEC'].value if info[ 'RMS_DEC'] is not None else -1.0 hdulist[sci_extn].header['NMATCHES'] = len( info['ref_mag']) if info['ref_mag'] is not None else -1.0 if 'HDRNAME' in hdulist[sci_extn].header: del hdulist[sci_extn].header['HDRNAME'] hdulist[sci_extn].header['HDRNAME'] = hdr_name hdulist.flush() hdulist.close() # Create headerlet out_headerlet = headerlet.create_headerlet(image_name, hdrname=hdr_name, wcsname=wcs_name, logging=False) # Update headerlet update_headerlet_phdu(item, out_headerlet) # Write headerlet if headerlet_filenames: headerlet_filename = headerlet_filenames[ image_name] # Use HAP-compatible filename defined in runhlaprocessing.py else: if image_name.endswith("flc.fits"): headerlet_filename = image_name.replace("flc", "flt_hlet") if image_name.endswith("flt.fits"): headerlet_filename = image_name.replace("flt", "flt_hlet") out_headerlet.writeto(headerlet_filename, overwrite=True) log.info("Wrote headerlet file {}.\n\n".format(headerlet_filename)) out_headerlet_dict[image_name] = headerlet_filename # Attach headerlet as HDRLET extension if headerlet.verify_hdrname_is_unique(hdulist, hdr_name): headerlet.attach_headerlet(image_name, headerlet_filename, logging=False) chipctr += 1 return (out_headerlet_dict)
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)
def updateObs(self, obsname): """Update observation with any available solutions. Parameters ========== obsname : str Filename for observation to be updated """ if not self.perform_step: return # Parse observation name obspath, obsroot = os.path.split(obsname) # use `obspath` for location of output files, # if anything gets written out observationID = obsroot.split('_')[:1][0] logger.info("Updating astrometry for {}".format(observationID)) # # apply to file... fileobj = pf.open(obsname, mode='update') # take inventory of what hdrlets are already appended to this file hdrnames = headerlet.get_headerlet_kw_names(fileobj, 'hdrname') headerlets, best_solution_id = self.getObservation(observationID) if headerlets is None: logger.warning("Problems getting solutions from database") logger.warning(" NO Updates performed for {}".format( observationID)) if self.raise_errors: raise ValueError("No new solution found in AstrometryDB.") else: return # If no headerlet found in database, update database with this WCS if self.new_observation: logger.warning(" No new solution found in AstrometryDB.") logger.warning(" Updating database with initial WCS {}". format(observationID)) hlet_buffer = BytesIO() hlet_new = headerlet.create_headerlet(fileobj) newhdrname = hlet_new[0].header['hdrname'] hlet_new.writeto(hlet_buffer) logger.info("Updating AstrometryDB with entry for {}".format( observationID)) logger.info("\t using WCS with HDRNAME={}".format(newhdrname)) # Add WCS solution from this observation to the database self.addObservation(observationID, hlet_buffer) else: # Attach new unique hdrlets to file... logger.info("Updating {} with:".format(observationID)) for h in headerlets: newhdrname = headerlets[h][0].header['hdrname'] if newhdrname in hdrnames: continue # do not add duplicate hdrlets # Add solution as an alternate WCS try: if best_solution_id and newhdrname == best_solution_id: # replace primary WCS with this solution headerlets[h].apply_as_primary(fileobj) logger.info('Replacing primary WCS with') logger.info('\tHeaderlet with HDRNAME={}'.format( newhdrname)) else: logger.info("\tHeaderlet with HDRNAME={}".format( newhdrname)) headerlets[h].attach_to_file(fileobj) except ValueError: pass fileobj.close()