def imgdiff_likelihood(img, refimg, params): import ciao_contrib.runtool as crt import numpy as np import pyfits as fits # create temporary copy of img tempimg = img + '_trans' crt.dmcopy.punlearn() crt.dmcopy.infile = img crt.dmcopy.outfile = tempimg crt.dmcopy.clobber = 'yes' crt.dmcopy() # shift image crt.wcs_update.punlearn() crt.wcs_update.infile = tempimg crt.wcs_update.wcsfile = tempimg crt.wcs_update.deltax = str(params[0]) crt.wcs_update.deltay = str(params[1]) crt.wcs_update.rotang = 0.0 crt.wcs_update.scalefac = 1.0 crt.wcs_update.clobber = 'yes' crt.wcs_update() # create difference^2 image # difference image diffimg = img + '_diff' crt.dmimgcalc.punlearn() crt.dmimgcalc.infile = tempimg crt.dmimgcalc.infile2 = refimg crt.dmimgcalc.outfile = diffimg crt.dmimgcalc.op = 'sub' crt.dmimgcalc.clobber = 'yes' crt.dmimgcalc() # square difference image diffsqimg = img + '_diffsq' crt.dmimgcalc.punlearn() crt.dmimgcalc.infile = diffimg crt.dmimgcalc.infile2 = 'none' crt.dmimgcalc.outfile = diffsqimg crt.dmimgcalc.op = 'imgout=img1*img1' crt.dmimgcalc.clobber = 'yes' crt.dmimgcalc() # calculate chi2 # read in diff image as array and get total imgfits = fits.open(diffimg) img = imgfits[0].data imgfits.close() chi2 = np.sum(img) #-calculate likelihood- N = img.shape[0] * img.shape[1] #number of pixels (i.e. measurements) Li = (2.0 * np.pi)**(-N / 2.0) * np.exp(-1.0 * chi2 / 2.0) return Li
def make_mask_file(observation): from astropy.io import fits print("Creating an image mask for {}.".format(observation.id)) original_fits_filename = observation.acisI_comb_img mask = fits.open(original_fits_filename) print("{} shape: {}".format(original_fits_filename, mask[0].shape)) mask[0].data = np.ones_like(mask[0].data) mask_filename = observation.temp_acis_comb_mask_filename mask.writeto(mask_filename, overwrite=True) rt.dmcopy.punlearn() # infile = "{mask_filename}[sky=region({fov_file})][opt full]".format( # for ACIS-I & ACIS-S infile = "{mask_filename}[sky=region({fov_file}[ccd_id=0:3])][opt full]".format( # for ACIS-I mask_filename=mask_filename, fov_file=observation.fov_file ) outfile = observation.acisI_combined_mask clobber = True rt.dmcopy(infile=infile, outfile=outfile, clobber=clobber) print("Image mask created for {obsid} and saved as {filename}".format( obsid=observation.id, filename=outfile )) io.delete(observation.temp_acis_comb_mask_filename)
def get_bkg(obsid_list, evt2_file): for obs in obsid_list: obs_file = str(obs)+evt2_file acis = acis_bkgrnd_lookup(infile='reprojected_data/%s' % obsfile) os.system("cp %s blank_sky/%s_bkgevt2.fits" % (acis, obs)) # Copies the acis file and renames it dmcopy(infile='blank_sky/%s_bkgevt2.fits[status=0]' % obs, outfile='blank_sky/%s_bkgevt2_clean.fits' % obs)
def ccd_sort(cluster): print("Running ccd_sort on {}.".format(cluster.name)) for observation in cluster.observations: print("Working on {}/{}".format(cluster.name, observation.id)) analysis_path = observation.analysis_directory os.chdir(analysis_path) evt1_filename = io.get_path("{}/{}".format(analysis_path, io.get_filename_matching("acis*evt1.fits")[0])) evt2_filename = io.get_path("{}/{}".format(analysis_path, io.get_filename_matching("evt2.fits")[0])) detname = rt.dmkeypar(infile=evt1_filename, keyword="DETNAM", echo=True) print("evt1 : {}\nevt2 : {}\ndetname : {}".format(evt1_filename, evt2_filename, detname)) assert not isinstance(detname, type(None)), "detname returned nothing!" detnums = [int(x) for x in detname.split('-')[-1]] for acis_id in detnums: print("{cluster}/{observation}: Making level 2 event file for ACIS Chip id: {acis_id}".format( cluster=cluster.name, observation=observation.id, acis_id=acis_id)) rt.dmcopy(infile="{evt2_file}[ccd_id={acis_id}]".format(evt2_file=evt2_filename, acis_id=acis_id), outfile="acis_ccd{acis_id}.fits".format(acis_id=acis_id), clobber=True) acisI_list = io.get_filename_matching("acis_ccd[0-3].fits") for i in range(len(acisI_list)): acisI_list[i] = io.get_path("{obs_analysis_dir}/{file}".format(obs_analysis_dir=observation.analysis_directory, file=acisI_list[i])) io.write_contents_to_file("\n".join(acisI_list), observation.ccd_merge_list, binary=False) merge_data_and_backgrounds(cluster, acisI_list) return
def espace_filt(obsid_list, ccd_id, energy): for obs in obsid_list: dmcopy(infile= 'reprojected_data/%s_reproj_evt.fits[energy=%s:%s, ccd_id=%s]' % (obs, energy[0], energy[1], ccd_id[obsid_list.index(obs)]), outfile='reprojected_data/%s_efilter.fits' % obs, opt='all', clobber='yes')
def get_bkg(obsid_list, evt2_file): for obs in obsid_list: obs_file = str(obs) + evt2_file acis = acis_bkgrnd_lookup(infile='reprojected_data/%s' % obsfile) os.system("cp %s blank_sky/%s_bkgevt2.fits" % (acis, obs)) # Copies the acis file and renames it dmcopy(infile='blank_sky/%s_bkgevt2.fits[status=0]' % obs, outfile='blank_sky/%s_bkgevt2_clean.fits' % obs)
def bkg_lightcurve(obsid_list): for obs in obsid_list: dmcopy( infile= 'reprojected_data/%s_efilter.fits[exclude sky=region(cluster.reg)]' % obs, outfile='%s_background.fits' % obs, opt='all', clobber='yes')
def ciao_hiE_sources(observation): # I don't know that this function is actually worthwhile data = observation.data_filename #background = observation.back_filename #cts_min = '625' # print("Finding optimal binning fators for ACIS images") #acis_bin = 4 rt.dmcopy.punlearn() infile = "{infile}".format(infile=data) outfile = "{}/img_acisI_fullE.fits".format(observation.analysis_directory) rt.dmcopy(infile=infile, outfile=outfile, clobber=True)
def wavdetectExtractor(imageFile, rebin_pix=5., scales='32, 64, 128'): imageNumber = str(np.random.random_integers(0, 10000)) #Reduce the values of the image image = fits.open(imageFile)[0].data image /= np.max(image) / 100. tmpInFile = 'tmp_' + imageNumber + '.fits' fits.writeto(tmpInFile, image, clobber=True) InImageFile = \ 'ciao_InImage'+imageNumber+'.fits' sourceFile = \ 'ciao_sources'+imageNumber+'.fits' OutImageFile = \ 'ciao_OutImage'+imageNumber+'.fits' scellfile = 'cell_' + imageNumber + '.fits' defnbkgfile = 'bkg_' + imageNumber + '.fits' ciao.dmcopy.punlearn() ciao.dmcopy( tmpInFile+"[bin x=::"+str(rebin_pix)+\ ",y=::"+str(rebin_pix)+"]", outfile=InImageFile,clobber=True) dims = image.shape if not os.path.isfile('PSF.fits'): fits.writeto('PSF.fits', np.ones( (np.int(dims[0]/rebin_pix), \ np.int(dims[1]/rebin_pix)) ), clobber=True) ciao.wavdetect.punlearn() ciao.wavdetect(infile=InImageFile, outfile=sourceFile, scellfile=scellfile, imagefile=OutImageFile, psffile='PSF.fits', defnbkgfile=defnbkgfile, scales=scales, clobber=True) sources = fits.open(sourceFile)[1].data os.system("rm -fr cell.fits") os.system("rm -fr *" + imageNumber + "*") os.system("rm -fr bkg.fits") return sources[np.argsort(sources['SRC_SIGNIFICANCE'])][::-1]
def rebin_img(infile: str, outfile: str, binsize: Union[float, int] = 0.5, nsize: int = 64, xcen: Union[float, int] = 4060., ycen: Union[float, int] = 4090., psf: bool = False, **kwargs) -> PointPhys: """ Bins an event file with the specified size and binning factor The binning will be performed on a standard grid based on the specified factor. :param infile: Path to the events file :param outfile: Path to the output file :param binsize: Binning factor for for the image :param nsize: Size of the image :param xcen: X-coordinate of the center in PHYS coordinates :param ycen: Y-coordinate of the center in PHYS coordinates :param psf: Set this to True to output real values instead of integers in the output image ... :rtype: PointPhys """ N = nsize B = binsize xcen3 = xcen ycen3 = ycen px = floor((xcen - floor(xcen)) / binsize) * binsize xcen3 = floor(xcen3) + px # py = floor((ycen - floor(ycen)) / binsize) * binsize ycen3 = floor(ycen3) + py if N % 2 == 0: xmin = round(xcen3 - (N * B) * 0.5, 2) xmax = round(xcen3 + (N * B) * 0.5, 2) ymin = round(ycen3 - (N * B) * 0.5, 2) ymax = round(ycen3 + (N * B) * 0.5, 2) else: xmin = round(xcen3 - ((N - 1) * B) * 0.5, 2) xmax = round(xcen3 + ((N + 1) * B) * 0.5, 2) ymin = round(ycen3 - ((N - 1) * B) * 0.5, 2) ymax = round(ycen3 + ((N + 1) * B) * 0.5, 2) outformat = 'r4' if psf else 'i4' newfile = f"{infile}[bin x={xmin}:{xmax}:#{N},y={ymin}:{ymax}:#{N}][opt type={outformat}]" dmcopy.punlearn() dmcopy(newfile, outfile, clobber=True) return PointPhys(xcen3, ycen3)
def make_acisI_and_back_for(observation, cluster): rt.dmcopy.punlearn() rt.dmcopy(infile="{clean_file}[sky=region({mask})]".format( clean_file=observation.clean, mask=cluster.master_crop_file), outfile=cluster.temp_acisI_comb, clobber=True) rt.dmcopy.punlearn() rt.dmcopy( infile="{temp_acisI_combined}[bin sky=4][energy=700:8000]".format( temp_acisI_combined=cluster.temp_acisI_comb), outfile=observation.acisI_comb_img, clobber=True) rt.dmcopy.punlearn() rt.dmcopy(infile="{back_file}[sky=region({mask})]".format( back_file=observation.back, mask=cluster.master_crop_file), outfile=cluster.temp_backI_comb, clobber=True) rt.dmcopy.punlearn() rt.dmcopy( infile="{temp_backI_combined}[bin sky=4][energy=700:8000]".format( temp_backI_combined=cluster.temp_backI_comb), outfile=observation.backI_comb_img, clobber=True) io.delete(cluster.temp_acisI_comb) io.delete(cluster.temp_backI_comb)
def dmcopyEvtFile(evt2, dataPath, region, obsID, tag, erange=[], ftype='evt'): inEvtFile = evt2 + "[EVENTS]" + "[sky=region(" + region + ")]" if len(erange) > 0: inEvtFile = inEvtFile + "[energy=" + str(erange[0]) + ":" + str( erange[1]) + "]" opt = 'all' if ftype == 'img': opt = 'image' outEvtName = str(obsID) + tag + '.fits' outEvtFile = dataPath + outEvtName rt.dmcopy(infile=inEvtFile, outfile=outEvtFile, option=opt, clobber='yes') print(" created " + outEvtName) return outEvtFile
def make_binmap_regions(directory, coord_values): ###### Need heasoft - maybe something to check that it is installed and need a way to call the program, also need it for contbin ?? ###### os.chdir(directory + '/contbin/') # Need to change into contbin directory so that contbin can output to the directory os.system("farith contbin_input.fits 0 temp.fits MUL") os.system("farith temp.fits 1 allones.fits ADD") os.system("rm temp.fits") dmcopy(infile='allones.fits[sky=region(contbin_mask.reg)][opt full]', outfile='mask.fits') os.system("rm allones.fits") sn = raw_input("Input signal to noise (sn) (e.g. 30): ") constrain_val = raw_input("Input constrainval value (e.g. 2.0): ") os.system("contbin --mask=mask.fits --sn=%s --smoothsn=3 --constrainfill --constrainval=%s contbin_input.fits" % (sn, constrain_val)) # Create contbin binmap os.chdir(directory) os.system("make_region_files --minx=%s --miny=%s --bin=1 --outdir=spectral_maps/ contbin/contbin_binmap.fits" % (coord_values[0], coord_values[2])) os.system("cp contbin/contbin_binmap.fits spectral_maps/") # Copy contbin_binmap
def runpipe5(cluster): # This portion of the pypeline combined_dir = cluster.combined_directory io.make_directory(combined_dir) while not io.file_exists(cluster.master_crop_file): print("Master crop file not found") run_ds9_for_master_crop(cluster) for observation in cluster.observations: # clean data infile = "{}[sky=region({})]".format(observation.clean, cluster.master_crop_file) outfile = cluster.temp_acisI_comb clobber = True rt.dmcopy.punlearn() rt.dmcopy(infile=infile, outfile=outfile, clobber=clobber) infile = "{}[bin sky=4][energy=700:8000]".format( cluster.temp_acisI_comb) outfile = observation.acisI_comb_img clobber = True print("ObsID: {}\t- Extracting just 0.7keV - 8keV.".format( observation.id)) rt.dmcopy.punlearn() rt.dmcopy(infile=infile, outfile=outfile, clobber=clobber) # background infile = "{}[sky=region({})]".format(observation.back, cluster.master_crop_file) outfile = cluster.temp_backI_comb clobber = True rt.dmcopy.punlearn() rt.dmcopy(infile=infile, outfile=outfile, clobber=clobber) infile = "{}[bin sky=4][energy=700:8000]".format( cluster.temp_backI_comb) outfile = observation.backI_comb_img clobber = True rt.dmcopy.punlearn() rt.dmcopy(infile=infile, outfile=outfile, clobber=clobber) io.delete(cluster.temp_acisI_comb) io.delete(cluster.temp_backI_comb) make_mask_file(observation) make_cumulative_mask_file(cluster, observation) create_combined_images(cluster) make_nosrc_cropped_xray_sb(cluster)
def generate_pixel_mask_from_region_file(image_file, reg_file): """Generate a pixel mask from a region file that overlaps with the image file. The values of the pixels withing the region will be assigned one and zero to the ones outside""" if not (exists(image_file) and isfile(image_file)): return { "status_code": -1, "err_msg": "File {0} does not exist ".format(image_file), } if not (exists(reg_file) and isfile(reg_file)): return { "status_code": -1, "err_msg": "File {0} does not exist ".format(reg_file), } img_basename = Path(image_file).stem img_ext = Path(image_file).suffix reg_basename = Path(reg_file).stem # generate the temp and outfile names temp_file = next(tempfile._get_candidate_names()) + "." + img_ext mask_file = img_basename + "." + reg_basename + img_ext dmimgcalc.punlearn() # create an 'ones' image with the same size dmimgcalc( infile=image_file, infile2=None, outfile=temp_file, operation="imgout=(1+(img1-img1))", clobber=True, ) dmcopy.punlearn() # apply the region and generate the pixel mask dmcopy( infile="{0}[sky=region({1})][opt full]".format(temp_file, reg_file), outfile=mask_file, clobber=True, ) remove(temp_file)
def runpipe5(cluster): print("runpipe5") from astropy.io import fits # This portion of the pypeline combined_dir = cluster.combined_directory io.make_directory(combined_dir) while not io.file_exists(cluster.master_crop_file): print("Master crop file not found") run_ds9_for_master_crop(cluster) # the contents of this for loop should be refactored/replaced with the make_acisI_and_back function for observation in cluster.observations: infile = "{}[sky=region({})]".format(observation.clean, cluster.master_crop_file) outfile = cluster.temp_acisI_comb clobber = True rt.dmcopy.punlearn() rt.dmcopy(infile=infile, outfile=outfile, clobber=clobber) print("{} shape: {}".format(outfile, fits.open(outfile)[0].shape)) infile = "{}[bin sky=4][energy=700:8000]".format(cluster.temp_acisI_comb) outfile = observation.acisI_comb_img clobber = True print("ObsID: {}\t- Extracting just 0.7keV - 8keV.".format(observation.id)) rt.dmcopy.punlearn() rt.dmcopy(infile=infile, outfile=outfile, clobber=clobber) # background infile = "{}[sky=region({})]".format(observation.back, cluster.master_crop_file) outfile = cluster.temp_backI_comb clobber = True rt.dmcopy.punlearn() rt.dmcopy(infile=infile, outfile=outfile, clobber=clobber) infile = "{}[bin sky=4][energy=700:8000]".format(cluster.temp_backI_comb) outfile = observation.backI_comb_img clobber = True rt.dmcopy.punlearn() rt.dmcopy(infile=infile, outfile=outfile, clobber=clobber) io.delete(cluster.temp_acisI_comb) io.delete(cluster.temp_backI_comb) make_mask_file(observation) make_cumulative_mask_file(cluster, observation) create_combined_images(cluster) make_nosrc_cropped_xray_sb(cluster)
def wcsRoutines(filename): infile = filename wcsEvt = 'acisf' + str(obsID) + '_repro_evt2.fits' rotang = rotation_angle + 0.8 #the number I am adding isn't legit, the goal_phi and the detected phi have to match for it to be legit outfile = filename[:-11] + '2_thresh.img' clobber = 'yes' print("Copying thresh file") rt.dmcopy(infile=infile, outfile=outfile, clobber=clobber) print("starting wcs_update for evt file") rt.wcs_update.punlearn() rt.wcs_update(infile=outfile, wcsfile=wcsEvt, rotang=rotang, scalefac=scalefac, outfile='') print("Done")
def remove_sources_from_observation(observation): print("removing sources from {}".format(observation.id)) # remove sources from foreground and background fore_or_back = [observation.data_filename, observation.back_filename] for i, type_of_obs in enumerate(fore_or_back): infile = "{type_of_obs}[exclude sky=region({sources})]".format( type_of_obs=type_of_obs, sources=observation.cluster.sources_file ) outfile = [observation.acis_nosrc_filename, observation.background_nosrc_filename][i] clobber = True print("infile: {}".format(infile)) print("outfile: {}".format(outfile)) rt.dmcopy.punlearn() rt.dmcopy(infile=infile, outfile=outfile, clobber=clobber) if type_of_obs is observation.background_nosrc_filename: print("Copying background to {}".format(observation.back)) io.copy(outfile, observation.back)
def make_binmap_regions(directory, coord_values): ###### Need heasoft - maybe something to check that it is installed and need a way to call the program, also need it for contbin ?? ###### os.chdir( directory + '/contbin/' ) # Need to change into contbin directory so that contbin can output to the directory os.system("farith contbin_input.fits 0 temp.fits MUL") os.system("farith temp.fits 1 allones.fits ADD") os.system("rm temp.fits") dmcopy(infile='allones.fits[sky=region(contbin_mask.reg)][opt full]', outfile='mask.fits') os.system("rm allones.fits") sn = raw_input("Input signal to noise (sn) (e.g. 30): ") constrain_val = raw_input("Input constrainval value (e.g. 2.0): ") os.system( "contbin --mask=mask.fits --sn=%s --smoothsn=3 --constrainfill --constrainval=%s contbin_input.fits" % (sn, constrain_val)) # Create contbin binmap os.chdir(directory) os.system( "make_region_files --minx=%s --miny=%s --bin=1 --outdir=spectral_maps/ contbin/contbin_binmap.fits" % (coord_values[0], coord_values[2])) os.system( "cp contbin/contbin_binmap.fits spectral_maps/") # Copy contbin_binmap
def make_acisI_and_back_for(observation, cluster): from astropy.io import fits rt.dmcopy.punlearn() rt.dmcopy( infile="{clean_file}[sky=region({mask})]".format( clean_file=observation.clean, mask=cluster.master_crop_file), outfile=cluster.temp_acisI_comb, clobber=True ) shp = fits.open(cluster.temp_acisI_comb)[0].shape print(observation.clean) print("{} shape {}".format(cluster.temp_acisI_comb, shp)) rt.dmcopy.punlearn() rt.dmcopy( infile="{temp_acisI_combined}[bin sky=4][energy=700:8000]".format( temp_acisI_combined=cluster.temp_acisI_comb), outfile=observation.acisI_comb_img, clobber=True ) shp = fits.open(observation.acisI_comb_img)[0].shape print("{} shape {}".format(observation.acisI_comb_img, shp)) rt.dmcopy.punlearn() rt.dmcopy( infile="{back_file}[sky=region({mask})]".format( back_file=observation.back, mask=cluster.master_crop_file), outfile=cluster.temp_backI_comb, clobber=True ) rt.dmcopy.punlearn() rt.dmcopy( infile="{temp_backI_combined}[bin sky=4][energy=700:8000]".format(temp_backI_combined=cluster.temp_backI_comb), outfile=observation.backI_comb_img, clobber=True ) io.delete(cluster.temp_acisI_comb) io.delete(cluster.temp_backI_comb)
#----Piled Spectrum---- with fits.open(piled_spec,mode='update') as hdu2: hdr = hdu2[1].header hdr['ANCRFILE'] = arf_file hdr['RESPFILE'] = rmf_file hdr['BACKFILE'] = bkg_file #fits.writeto(unpiled_spec,) #--------------------------------------- # Update no contamination #--------------------------------------- skip = 1 if skip == 0: #----Copy Updated UnPiled Spectrum---- crt.dmcopy(unpiled_spec,nocontam_spec,clobber=args.clobber) #with fits.open(nocontam_spec,mode='update') as hdu1: # hdr = hdu1[1].header # print 'NoContam AREASCAL = ',hdr['AREASCAL'] # hdr['ANCRFILE'] = nocontam_arf_file hdulist = fits.open(nocontam_spec,mode='update') shdu = hdulist[1] #print 'Unpiled AREASCAL = ',shdu.header['AREASCAL'] shdu.header['ANCRFILE'] = nocontam_arf_file hdulist.close(output_verify='ignore') #otherwise yells about bad AREASCAL
def imgdiff_likelihood(img,refimg,params): import ciao_contrib.runtool as crt import numpy as np import pyfits as fits # create temporary copy of img tempimg = img+'_trans' crt.dmcopy.punlearn() crt.dmcopy.infile = img crt.dmcopy.outfile = tempimg crt.dmcopy.clobber = 'yes' crt.dmcopy() # shift image crt.wcs_update.punlearn() crt.wcs_update.infile = tempimg crt.wcs_update.wcsfile = tempimg crt.wcs_update.deltax = str(params[0]) crt.wcs_update.deltay = str(params[1]) crt.wcs_update.rotang = 0.0 crt.wcs_update.scalefac = 1.0 crt.wcs_update.clobber = 'yes' crt.wcs_update() # create difference^2 image # difference image diffimg = img+'_diff' crt.dmimgcalc.punlearn() crt.dmimgcalc.infile = tempimg crt.dmimgcalc.infile2 = refimg crt.dmimgcalc.outfile = diffimg crt.dmimgcalc.op = 'sub' crt.dmimgcalc.clobber = 'yes' crt.dmimgcalc() # square difference image diffsqimg = img+'_diffsq' crt.dmimgcalc.punlearn() crt.dmimgcalc.infile = diffimg crt.dmimgcalc.infile2 = 'none' crt.dmimgcalc.outfile = diffsqimg crt.dmimgcalc.op = 'imgout=img1*img1' crt.dmimgcalc.clobber = 'yes' crt.dmimgcalc() # calculate chi2 # read in diff image as array and get total imgfits = fits.open(diffimg) img = imgfits[0].data imgfits.close() chi2 = np.sum(img) #-calculate likelihood- N = img.shape[0]*img.shape[1] #number of pixels (i.e. measurements) Li = (2.0*np.pi)**(-N/2.0)*np.exp(-1.0*chi2/2.0) return Li
final_dx = np.median(mcmc_pars[args.burnin:, 1]) final_dy = np.median(mcmc_pars[args.burnin:, 2]) print 'final dx,dy = ', final_dx, final_dy sigmadx = np.std(mcmc_pars[args.burnin:, 1]) sigmady = np.std(mcmc_pars[args.burnin:, 2]) print 'sigmadx,sigmady = ', sigmadx, sigmady #--------------------------------------- # Translate Image with Best dx, dy #--------------------------------------- # copy image crt.dmcopy.punlearn() crt.dmcopy.infile = args.targetimg crt.dmcopy.outfile = args.targetimg + '_trans' crt.dmcopy.clobber = args.clobber crt.dmcopy() # shift image crt.wcs_update.punlearn() crt.wcs_update.infile = args.targetimg + '_trans' crt.wcs_update.wcsfile = args.targetimg + '_trans' crt.wcs_update.deltax = str(final_dx) crt.wcs_update.deltay = str(final_dy) crt.wcs_update.rotang = 0.0 crt.wcs_update.scalefac = 1.0 crt.wcs_update.clobber = 'yes' crt.wcs_update()
def prepare_effective_time_circles_for(observation: cluster.Observation): io.delete_if_exists(observation.effbtime) io.delete_if_exists(observation.effdtime) if not io.file_exists(observation.acisI_nosrc_combined_mask_file): print("Removing point sources from the observations combined mask file.") print("dmcopy infile='{}[exclude sky=region({})]' outfile={} clobber=True".format( observation.acisI_combined_mask_file, observation.cluster.sources_file, observation.acisI_nosrc_combined_mask_file )) rt.dmcopy.punlearn() rt.dmcopy( infile="{fits_file}[exclude sky=region({source_file})]".format( fits_file=observation.acisI_combined_mask_file, source_file=observation.cluster.sources_file ), outfile=observation.acisI_nosrc_combined_mask_file, clobber=True ) else: print("{acis} already exists.".format( acis=observation.acisI_nosrc_combined_mask_file )) # if not io.file_exists(observation.acisI_high_energy_combined_image_file): print("Creating high band (9.5-12 keV) source image cropped to combined region.") rt.dmcopy.punlearn() rt.dmcopy( infile="{fits_file}[sky=region({crop_file})]".format( fits_file=observation.clean, crop_file=observation.cluster.master_crop_file ), outfile=observation.acisI_high_energy_temp_image, clobber=True ) ##########need to change to obs specific rt.dmcopy.punlearn() rt.dmcopy( infile="{fits_file}[EVENTS][bin sky=4][energy=9500:12000]".format( fits_file=observation.acisI_high_energy_temp_image ), outfile=observation.acisI_high_energy_combined_image_file, option="image", clobber=True ) io.delete_if_exists(observation.acisI_high_energy_temp_image) print("Creating high band (9.5-12 keV) background image cropped to combined region.") rt.dmcopy.punlearn() rt.dmcopy( infile="{fits_file}[sky=region({crop_file})]".format( fits_file=observation.back, crop_file=observation.cluster.master_crop_file ), outfile=observation.backI_high_energy_temp_image, clobber=True ) rt.dmcopy.punlearn() rt.dmcopy( infile="{fits_file}[EVENTS][bin sky=4][energy=9500:12000]".format( fits_file=observation.backI_high_energy_temp_image ), outfile=observation.backI_high_energy_combined_image_file, option="image", clobber=True ) io.delete_if_exists(observation.backI_high_energy_temp_image)
def prepare_efftime_circle(cluster): try: from ciao_contrib import runtool as rt except ImportError: print("Failed to import CIAO python scripts. ") raise for observation in cluster.observations: io.delete_if_exists(observation.effbtime) io.delete_if_exists(observation.effdtime) if not io.file_exists(observation.acisI_nosrc_combined_mask_file): print("Removing point sources from the observations combined mask file.") print("dmcopy infile='{}[exclude sky=region({})]' outfile={} clobber=True".format( observation.acisI_combined_mask_file, cluster.sources_file, observation.acisI_nosrc_combined_mask_file )) rt.dmcopy.punlearn() rt.dmcopy( infile="{fits_file}[exclude sky=region({source_file})]".format( fits_file=observation.acisI_combined_mask_file, source_file=cluster.sources_file ), outfile=observation.acisI_nosrc_combined_mask_file, clobber=True ) else: print("{acis} already exists.".format( acis=observation.acisI_nosrc_combined_mask_file )) if not io.file_exists(observation.acisI_high_energy_combined_image_file): print("Creating high band (9.5-12 keV) source image cropped to combined region.") rt.dmcopy.punlearn() rt.dmcopy( infile="{fits_file}[sky=region({crop_file})]".format( fits_file=observation.clean, crop_file=cluster.master_crop_file ), outfile=observation.acisI_high_energy_temp_image, clobber=True ) rt.dmcopy.punlearn() rt.dmcopy( infile="{fits_file}[EVENTS][bin sky=4][energy=9500:12000]".format( fits_file=observation.acisI_high_energy_temp_image ), outfile=observation.acisI_high_energy_combined_image_file, option="image", clobber=True ) else: print("{fits_file} already exists.".format( fits_file=observation.acisI_high_energy_combined_image_file )) io.delete_if_exists(observation.acisI_high_energy_temp_image) if not io.file_exists(observation.backI_high_energy_combined_image_file): print("Creating high band (9.5-12 keV) background image cropped to combined region.") rt.dmcopy.punlearn() rt.dmcopy( infile="{fits_file}[sky=region({crop_file})]".format( fits_file=observation.back, crop_file=cluster.master_crop_file ), outfile=observation.backI_high_energy_temp_image, clobber=True ) rt.dmcopy.punlearn() rt.dmcopy( infile="{fits_file}[EVENTS][bin sky=4][energy=9500:12000]".format( fits_file=observation.backI_high_energy_temp_image ), outfile=observation.backI_high_energy_combined_image_file, option="image", clobber=True ) else: print("{fits_file} already exists.".format( fits_file=observation.backI_high_energy_combined_image_file )) io.delete_if_exists(observation.backI_high_energy_temp_image)
def getDmcopy(filename, outfile): infile = filename outfile = outfile rt.dmcopy(infile=infile, outfile=outfile, clobber='yes')
def copy_image(infile, outfile, overwrite=False): rt.dmcopy.punlearn() rt.dmcopy(infile=infile, outfile=outfile, clobber=overwrite)
def bkg_lightcurve(obsid_list): for obs in obsid_list: dmcopy(infile='reprojected_data/%s_efilter.fits[exclude sky=region(cluster.reg)]' % obs, outfile='%s_background.fits' % obs, opt='all', clobber='yes')
def espace_filt(obsid_list, ccd_id, energy): for obs in obsid_list: dmcopy(infile='reprojected_data/%s_reproj_evt.fits[energy=%s:%s, ccd_id=%s]' % (obs, energy[0], energy[1], ccd_id[obsid_list.index(obs)]), outfile='reprojected_data/%s_efilter.fits' % obs, opt='all', clobber='yes')
final_dx = np.median(mcmc_pars[args.burnin:,1]) final_dy = np.median(mcmc_pars[args.burnin:,2]) print 'final dx,dy = ',final_dx,final_dy sigmadx = np.std(mcmc_pars[args.burnin:,1]) sigmady = np.std(mcmc_pars[args.burnin:,2]) print 'sigmadx,sigmady = ',sigmadx,sigmady #--------------------------------------- # Translate Image with Best dx, dy #--------------------------------------- # copy image crt.dmcopy.punlearn() crt.dmcopy.infile = args.targetimg crt.dmcopy.outfile = args.targetimg+'_trans' crt.dmcopy.clobber = args.clobber crt.dmcopy() # shift image crt.wcs_update.punlearn() crt.wcs_update.infile = args.targetimg+'_trans' crt.wcs_update.wcsfile = args.targetimg+'_trans' crt.wcs_update.deltax = str(final_dx) crt.wcs_update.deltay = str(final_dy) crt.wcs_update.rotang = 0.0 crt.wcs_update.scalefac = 1.0 crt.wcs_update.clobber = 'yes' crt.wcs_update()
def evt_list_filt(obsid_list): for obs in obsid_list: dmcopy(infile='reprojected_data/%s_efilter.fits[@%s_bkg_deflare.gti]' % (obs, obs), outfile='reprojected_data/%s_reproj_clean.fits' % obs, clobber='yes')
def ciao_back(cluster, overwrite=False): print("Running ciao_back on {}.".format(cluster.name)) for observation in cluster.observations: pcad_file = make_pcad_lis(cluster, observation.id) backI_lis = [] backS_lis = [] analysis_path = observation.analysis_directory filelist = io.read_contents_of_file(observation.ccd_merge_list).split('\n') pcad = io.read_contents_of_file(pcad_file) for acis_file in filelist: rt.acis_bkgrnd_lookup.punlearn() print("Finding background for {}".format(acis_file)) path_to_background = rt.acis_bkgrnd_lookup(infile=acis_file) print("Found background at {}".format(path_to_background)) acis_id = int(acis_file.split('/')[-1].split('.')[-2][-1]) assert isinstance(acis_id, int), "acis_id = {}".format(acis_id) assert not isinstance(path_to_background, type(None)), "Cannot find background {}".format(acis_file) local_background_path = io.get_path("{}/back_ccd{}.fits".format(analysis_path, acis_id)) try: if io.file_exists(local_background_path) and overwrite: io.delete(local_background_path) io.copy(path_to_background, local_background_path) except OSError: print("Problem copying background file {}. Do you have the right permissions and a full CALDB?".format( path_to_background)) raise acis_gain = rt.dmkeypar(infile=acis_file, keyword="GAINFILE", echo=True) background_gain = rt.dmkeypar(infile=local_background_path, keyword="GAINFILE", echo=True) print("{}/{}/acis_ccd{}.fits gain: {}".format(cluster.name, observation.id, acis_id, acis_gain)) print("{}/{}/back_ccd{}.fits gain: {}".format(cluster.name, observation.id, acis_id, background_gain)) if dates_and_versions_match(acis_gain, background_gain): print("Date/version numbers don't match on the acis data and background. Reprocessing.") local_background_path = reprocess(cluster, observation.id, acis_gain, background_gain, acis_id) print("Reprojecting background") rt.reproject_events.punlearn() infile = local_background_path outfile = io.get_path("{local_path}/back_reproj_ccd{acis_id}.fits".format(local_path=analysis_path, acis_id=acis_id)) match = acis_file print( "Running:\n reproject_events(infile={infile}, outfile={outfile}, aspect={pcad}, match={match})".format( infile=infile, outfile=outfile, pcad=pcad, match=match) ) rt.reproject_events(infile=infile, outfile=outfile, aspect="{pcad_file}".format(pcad_file=pcad), match=match, random=0, clobber=True) back_reproject = outfile datamode = rt.dmkeypar(infile=io.get_filename_matching(io.get_path("{}/acis*evt1*.fits".format(analysis_path))), keyword="DATAMODE") if datamode == "VFAINT": print("VFAINT Mode, resetting setting status bits") rt.dmcopy.punlearn() rt.dmcopy(infile="{}[status=0]".format(back_reproject), outfile=outfile, clobber=True) if acis_id <= 3: backI_lis.append(back_reproject) else: backS_lis.append(back_reproject) merged_back_list = backI_lis + backS_lis print("writing backI.lis and backS.lis") io.write_contents_to_file("\n".join(backI_lis), io.get_path("{}/backI.lis".format(analysis_path)), binary=False) io.write_contents_to_file("\n".join(backS_lis), io.get_path("{}/backS.lis".format(analysis_path)), binary=False) io.write_contents_to_file("\n".join(merged_back_list), observation.merged_back_lis, binary=False) return
def lightcurves_with_exclusion(cluster): for observation in cluster.observations: # data_nosrc_hiEfilter = "{}/acisI_nosrc_fullE.fits".format(obs_analysis_dir) data_nosrc_hiEfilter = "{}/acisI_nosrc_hiEfilter.fits".format(observation.analysis_directory) print("Creating the image with sources removed") data = observation.acis_nosrc_filename image_nosrc = "{}/img_acisI_nosrc_fullE.fits".format(observation.analysis_directory) if io.file_exists(observation.exclude_file): print("Removing sources from event file to be used in lightcurve") infile = "{}[exclude sky=region({})]".format(data_nosrc_hiEfilter, observation.exclude) outfile = "{}/acisI_lcurve.fits".format(observation.analysis_directory) clobber = True rt.dmcopy.punlearn() rt.dmcopy(infile=infile, outfile=outfile, clobber=clobber) data_lcurve = "{}/acisI_lcurve.fits".format(observation.analysis_directory) else: yes_or_no = io.check_yes_no( "Are there sources to be excluded from observation {} while making the lightcurve? ".format(observation.id)) if yes_or_no: # yes_or_no == True print("Create the a region file with the region to be excluded and save it as {}".format(observation.exclude_file)) else: data_lcurve = data_nosrc_hiEfilter backbin = 259.28 echo = True tstart = rt.dmkeypar(infile=data_nosrc_hiEfilter, keyword="TSTART", echo=echo) tstop = rt.dmkeypar(infile=data_nosrc_hiEfilter, keyword="TSTOP", echo=echo) print("Creating lightcurve from the events list with dmextract") infile = "{}[bin time={}:{}:{}]".format(data_lcurve, tstart, tstop, backbin) outfile = "{}/acisI_lcurve.lc".format(observation.analysis_directory) opt = "ltc1" rt.dmextract.punlearn() rt.dmextract(infile=infile, outfile=outfile, opt=opt, clobber=clobber) lcurve = outfile print("Cleaning the lightcurve by removing flares with deflare. Press enter to continue.") rt.deflare.punlearn() infile = lcurve outfile = "{}/acisI_gti.gti".format(observation.analysis_directory) method = "clean" save = "{}/acisI_lcurve".format(observation.analysis_directory) rt.deflare(infile=infile, outfile=outfile, method=method, save=save) gti = outfile print("filtering the event list using GTI info just obtained.") infile = "{}[@{}]".format(data_nosrc_hiEfilter, gti) outfile = observation.clean clobber = True rt.dmcopy(infile=infile, outfile=outfile, clobber=clobber) data_clean = outfile print("Don't forget to check the light curves!")
def generate_light_curve(observation): # filter out high energy background flares obsid_analysis_dir = observation.analysis_directory data = observation.acis_nosrc_filename background = observation.background_nosrc_filename infile = "{}[energy=9000:12000]".format(data) outfile = "{}/acisI_hiE.fits".format(obsid_analysis_dir) clobber = True rt.dmcopy.punlearn() rt.dmcopy(infile=infile, outfile=outfile, clobber=clobber) data_hiE = outfile infile = "{}[bin sky=8]".format(data_hiE) outfile = "{}/img_acisI_hiE.fits".format(obsid_analysis_dir) rt.dmcopy.punlearn() rt.dmcopy(infile=infile, outfile=outfile, clobber=clobber) backbin = 259.28 echo = True tstart = rt.dmkeypar(infile=data_hiE, keyword="TSTART", echo=echo) tstop = rt.dmkeypar(infile=data_hiE, keyword="TSTOP", echo=echo) print("Creating a lightcurve from the high energy events list with dmextract") rt.dmextract.punlearn() infile = "{}[bin time={}:{}:{}]".format(data_hiE, tstart, tstop, backbin) outfile = "{}/acisI_lcurve_hiE.lc".format(obsid_analysis_dir) print('Running dmextract infile={} outfile={} opt=ltc1 clobber=True'.format(infile, outfile)) rt.dmextract(infile=infile, outfile=outfile, opt='ltc1', clobber=True) lcurve_hiE = outfile print("cleaning the lightcurve for {}, press enter to continue.".format(observation.id)) rt.deflare.punlearn() outfile = "{}/acisI_gti_hiE.gti".format(obsid_analysis_dir) method = "clean" save = "{}/acisI_lcurve_hiE".format(obsid_analysis_dir) rt.deflare(infile=lcurve_hiE, outfile=outfile, method=method, save=save) gti_hiE = outfile print("Filtering the event list using GTI info from high energy flares.") infile = "{}[@{}]".format(data, gti_hiE) outfile = "{}/acisI_nosrc_hiEfilter.fits".format(obsid_analysis_dir) print("running: dmcopy infile={} outfile={} clobber={}".format(infile, outfile, clobber)) rt.dmcopy.punlearn() rt.dmcopy(infile=infile, outfile=outfile, clobber=clobber) data_nosrc_hiEfilter = outfile infile = "{}[bin sky=8]".format(data_nosrc_hiEfilter) outfile = "{}/img_acisI_nosrc_fullE.fits".format(obsid_analysis_dir) rt.dmcopy.punlearn() rt.dmcopy(infile=infile, outfile=outfile, clobber=clobber)
#-- open file -- with open('subbands_out.lis','w') as outlis: #-- write evtfile name with filters -- outlis.write(evtname+'_300-800.fits\n') outlis.write(evtname+'_800-1200.fits\n') outlis.write(evtname+'_300-1200.fits\n') outlis.write(evtname+'_1200-8000.fits\n') outlis.write(evtname+'_300-8000.fits\n') outlis.write(evtname+'_500-2000.fits\n') outlis.write(evtname+'_3000-8000.fits\n') outlis.write(evtname+'_2000-10000.fits') #--------------------------------------- # Run dmcopy #--------------------------------------- crt.dmcopy.punlearn() crt.dmcopy.infile = '@subbands_in.lis' crt.dmcopy.outfile = '@subbands_out.lis' crt.dmcopy(clobber=args.clobber) #--------------------------------------- # Delete/rename lis files #--------------------------------------- os.remove('subbands_in.lis') #os.remove('subbands_out.lis') os.rename('subbands_out.lis','subband_evtfiles.lis')
print "" #--------------------------------------- # Remove flares #--------------------------------------- #--Create background light curve-- #timebin = 200 #eventually make this an optional argument #how to decide on timebin size? bkg_lc_file = args.output_dir + 'bkg_lc.fits' #- filter on energy - crt.dmcopy.punlearn() estr = evt2_infile + '[energy=2400:6000]' crt.dmcopy(estr, evt2_infile + '_2400-6000') #- extract light curve - crt.dmextract.punlearn() if args.exclude_region != 'none': evt2_file_str = evt2_infile + '[exclude sky=region(' + args.exclude_region + ')][bin time=::' + str( args.timebin) + ']' else: evt2_file_str = evt2_infile + '_2400-6000[bin time=::' + str( args.timebin) + ']' crt.dmextract(evt2_file_str, outfile=bkg_lc_file, opt='ltc1', clobber=args.clobber)
#-- open file -- with open('subbands_out.lis', 'w') as outlis: #-- write evtfile name with filters -- outlis.write(evtname + '_300-800.fits\n') outlis.write(evtname + '_800-1200.fits\n') outlis.write(evtname + '_300-1200.fits\n') outlis.write(evtname + '_1200-8000.fits\n') outlis.write(evtname + '_300-8000.fits\n') outlis.write(evtname + '_500-2000.fits\n') outlis.write(evtname + '_3000-8000.fits\n') outlis.write(evtname + '_2000-10000.fits') #--------------------------------------- # Run dmcopy #--------------------------------------- crt.dmcopy.punlearn() crt.dmcopy.infile = '@subbands_in.lis' crt.dmcopy.outfile = '@subbands_out.lis' crt.dmcopy(clobber=args.clobber) #--------------------------------------- # Delete/rename lis files #--------------------------------------- os.remove('subbands_in.lis') #os.remove('subbands_out.lis') os.rename('subbands_out.lis', 'subband_evtfiles.lis')
print "" #--------------------------------------- # Remove flares #--------------------------------------- #--Create background light curve-- #timebin = 200 #eventually make this an optional argument #how to decide on timebin size? bkg_lc_file = args.output_dir+'bkg_lc.fits' #- filter on energy - crt.dmcopy.punlearn() estr = evt2_infile+'[energy=2400:6000]' crt.dmcopy(estr,evt2_infile+'_2400-6000') #- extract light curve - crt.dmextract.punlearn() if args.exclude_region != 'none': evt2_file_str = evt2_infile+'[exclude sky=region('+args.exclude_region+')][bin time=::'+str(args.timebin)+']' else: evt2_file_str = evt2_infile+'_2400-6000[bin time=::'+str(args.timebin)+']' crt.dmextract(evt2_file_str,outfile=bkg_lc_file,opt='ltc1',clobber=args.clobber) #- remove energy filtered event file - os.remove(evt2_infile+'_2400-6000') #--Create GTI file-- gti_outfile = args.output_dir+'flare_gti.fits'