sh.load_table_model("expo", "G300-0_test_exposure.fits") sh.load_table_model("bkg", "G300-0_test_background.fits") sh.load_psf("psf", "G300-0_test_psf.fits") # In principle one might first want to fit the background amplitude. However the background estimation method already yields the correct normalization, so we freeze the background amplitude to unity instead of adjusting it. The (smoothed) residuals from this background model are then computed and shown. # In[3]: sh.set_full_model(bkg) bkg.ampl = 1 sh.freeze(bkg) data = sh.get_data_image().y - sh.get_model_image().y resid = SkyImage(data=data, wcs=wcs) resid_table = [] # Keep residual images in a list to show them later resid_smo6 = resid.smooth(radius = 6) resid_smo6.plot() resid_table.append(resid_smo6) # ### Find and fit the brightest source # We then find the position of the maximum in the (smoothed) residuals map, and fit a (symmetrical) Gaussian source with that initial position: # In[4]: maxcoord = resid_smo6.lookup_max() maxpix = resid_smo6.wcs_skycoord_to_pixel(maxcoord[0])
sh.set_method("simplex") sh.load_image('../datasets/images/MSH15-52_counts.fits.gz') sh.set_coord("logical") sh.load_table_model("expo", "../datasets/images/MSH15-52_exposure.fits.gz") sh.load_table_model("bkg", "../datasets/images/MSH15-52_background.fits.gz") sh.load_psf("psf", "../datasets/images/MSH15-52_psf.fits.gz") # In[54]: sh.set_full_model(bkg) bkg.ampl = 1 sh.freeze(bkg) data = sh.get_data_image().y - sh.get_model_image().y resid = SkyImage(data=data, wcs=ref_image.wcs) resid_table = [] #Keep residual images in a list to show them later resid_smo6 = resid.smooth(radius=6) resid_smo6.plot(vmax=5, add_cbar=True) resid_table.append(resid_smo6) # In[55]: maxcoord = resid_smo6.lookup_max() maxpix = resid_smo6.wcs_skycoord_to_pixel(maxcoord[0]) print(maxcoord) print(maxpix) sh.set_full_model( bkg + psf(sh.gauss2d.g0) * expo) # creates g0 as a gauss2d instance
def make_images(psf_sigma): # Define width of the source and the PSF source_sigma = 4 sigma = np.sqrt(psf_sigma**2 + source_sigma**2) amplitude = 1E3 / (2 * np.pi * sigma**2) source = Gaussian2D(amplitude, 99, 99, sigma, sigma) background = Const2D(1) model = source + background # Define data shape shape = (200, 200) y, x = np.indices(shape) # Create a new WCS object wcs = WCS(naxis=2) # Set up an Galactic projection wcs.wcs.crpix = [100.5, 100.5] wcs.wcs.cdelt = np.array([0.02, 0.02]) wcs.wcs.crval = [0, 0] wcs.wcs.ctype = ['GLON-CAR', 'GLAT-CAR'] # Fake data random_state = get_random_state(0) data = random_state.poisson(model(x, y)) # Create exclusion mask center = SkyCoord(0, 0, frame='galactic', unit='deg') circle = CircleSkyRegion(center, 0.5 * u.deg) exclusion = SkyImage(data=x, wcs=wcs).region_mask(circle) # Save data header = wcs.to_header() mask = ~exclusion.data hdu = fits.PrimaryHDU(data=mask.astype('int32'), header=header) filename = 'exclusion.fits.gz' print('Writing {}'.format(filename)) hdu.writeto(filename, clobber=True) hdu = fits.PrimaryHDU(data=data.astype('int32'), header=header) filename = 'counts.fits.gz' print('Writing {}'.format(filename)) hdu.writeto(filename, clobber=True) hdu = fits.PrimaryHDU(data=model(x, y).astype('float32'), header=header) filename = 'model.fits.gz' print('Writing {}'.format(filename)) hdu.writeto(filename, clobber=True) hdu = fits.PrimaryHDU(data=background(x, y).astype('float32'), header=header) filename = 'background.fits.gz' print('Writing {}'.format(filename)) hdu.writeto(filename, clobber=True) hdu = fits.PrimaryHDU(data=source(x, y).astype('float32'), header=header) filename = 'source.fits.gz' print('Writing {}'.format(filename)) hdu.writeto(filename, clobber=True) exposure = 1E12 * np.ones(shape) hdu = fits.PrimaryHDU(data=exposure.astype('float32'), header=header) filename = 'exposure.fits.gz' print('Writing {}'.format(filename)) hdu.writeto(filename, clobber=True)