def test_eq_ne(): """ Check that equality/inequality works as expected.""" gsp = galsim.GSParams(folding_threshold=1.1e-3) diff_gals = [] for mode in ("InclinedExponential", "InclinedSersic"): # First test that some different initializations that should be equivalent: same_gals = [get_prof(mode, 0.1 * galsim.radians, 3.0), get_prof(mode, 0.1 * galsim.radians, 3.0, 0.3), # default h/r = 0.1 get_prof(mode, 0.1 * galsim.radians, 3.0, scale_height=0.3), get_prof(mode, 0.1 * galsim.radians, 3.0, scale_h_over_r=0.1), get_prof(mode, 0.1 * galsim.radians, 3.0, flux=1.0), # default flux=1 get_prof(mode, -0.1 * galsim.radians, 3.0), # negative i is equivalent get_prof(mode, (np.pi - 0.1) * galsim.radians, 3.0), # also pi-theta get_prof(mode, 18. / np.pi * galsim.degrees, 3.0), get_prof(mode, inclination=0.1 * galsim.radians, scale_radius=3.0, scale_height=0.3, flux=1.0), get_prof(mode, flux=1.0, scale_radius=3.0, scale_height=0.3, inclination=0.1 * galsim.radians), get_prof(mode, flux=1.0, half_light_radius=3.0 * galsim.Exponential._hlr_factor, scale_height=0.3, inclination=0.1 * galsim.radians), get_prof(mode, flux=1.0, half_light_radius=3.0 * galsim.Exponential._hlr_factor, scale_h_over_r=0.1, inclination=0.1 * galsim.radians)] for gal in same_gals[1:]: print(gal) gsobject_compare(gal, same_gals[0]) # Set up list of galaxies we expect to all be different diff_gals += [get_prof(mode, 0.1 * galsim.radians, 3.0, 0.3), get_prof(mode, 0.1 * galsim.degrees, 3.0, 0.3), get_prof(mode, 0.1 * galsim.degrees, 3.0, scale_h_over_r=0.2), get_prof(mode, 0.1 * galsim.radians, 3.0, 3.0), get_prof(mode, 0.2 * galsim.radians, 3.0, 0.3), get_prof(mode, 0.1 * galsim.radians, 3.1, 0.3), get_prof(mode, 0.1 * galsim.radians, 3.1), get_prof(mode, 0.1 * galsim.radians, 3.0, 0.3, flux=0.5), get_prof(mode, 0.1 * galsim.radians, 3.0, 0.3, gsparams=gsp)] # Add some more Sersic profiles to the diff_gals list diff_gals += [get_prof("InclinedSersic", 0.1 * galsim.radians, 3.0, 0.3, n=1.1), get_prof("InclinedSersic", 0.1 * galsim.radians, 3.0, 0.3, trunc=4.5), galsim.InclinedSersic(n=1.0, inclination=0.1 * galsim.radians, half_light_radius=3.0, scale_height=0.3), galsim.InclinedSersic(n=1.0, inclination=0.1 * galsim.radians, scale_radius=3.0, scale_h_over_r=0.3)] all_obj_diff(diff_gals)
def get_prof(mode, *args, **kwargs): """Function to get either InclinedExponential or InclinedSersic (with n=1, trunc=0) depending on mode """ new_kwargs = deepcopy(kwargs) if len(args) > 0: new_kwargs["inclination"] = args[0] if len(args) > 1: new_kwargs["scale_radius"] = args[1] if len(args) > 2: new_kwargs["scale_height"] = args[2] if mode == "InclinedSersic": if not "trunc" in new_kwargs: new_kwargs["trunc"] = 0. if not "n" in new_kwargs: new_kwargs["n"] = 1. prof = galsim.InclinedSersic(**new_kwargs) else: if "trunc" in new_kwargs: del new_kwargs["trunc"] if "n" in new_kwargs: del new_kwargs["n"] if "flux_untruncated" in new_kwargs: del new_kwargs["flux_untruncated"] prof = galsim.InclinedExponential(**new_kwargs) return prof
def make_a_galaxy(ud,wcs,affine,cosmos_cat,nfw,optics,sbparams): """ Method to make a single galaxy object and return stamp for injecting into larger GalSim image """ # Choose a random RA, Dec around the sky_center. # Note that for this to come out close to a square shape, we need to account for the # cos(dec) part of the metric: ds^2 = dr^2 + r^2 d(dec)^2 + r^2 cos^2(dec) d(ra)^2 # So need to calculate dec first. dec = sbparams.center_dec + (ud()-0.5) * sbparams.image_ysize_arcsec * galsim.arcsec ra = sbparams.center_ra + (ud()-0.5) * sbparams.image_xsize_arcsec / numpy.cos(dec) * galsim.arcsec world_pos = galsim.CelestialCoord(ra,dec) # We will need the image position as well, so use the wcs to get that image_pos = wcs.toImage(world_pos) # We also need this in the tangent plane, which we call "world coordinates" here. # This is still an x/y corrdinate uv_pos = affine.toWorld(image_pos) logger.debug('created galaxy position') ## Draw a Sersic galaxy from scratch index = int(np.floor(ud()*len(cosmos_cat))) # This is a kludge to obain a repeatable index gal_z = cosmos_cat[index]['ZPDF'] gal_flux = cosmos_cat[index][sbparams.bandpass]*sbparams.exp_time inclination = cosmos_cat[index]['phi_cosmos10']*galsim.radians scale_h_over_r = cosmos_cat[index]['q_cosmos10'] # Cosmos HLR is in units of HST pix, convert to arcsec. # AG put a factor of q in there, unclear why? half_light_radius=cosmos_cat[index]['hlr_cosmos10']*0.03*np.sqrt(scale_h_over_r) n = cosmos_cat[index]['n_sersic_cosmos10'] print('galaxy index=%d z=%f flux=%f hlr=%f sersic_index=%f'%(index,gal_z,gal_flux,half_light_radius,n)) ## InclinedSersic requires 0.3 < n < 6; ## set galaxy's n to another value if it falls outside this range if n<0.3: n=0.5 elif n>=6: n=4 else: pass ## Very large HLRs will also make GalSim fail ## Set to a default, ~large but physical value. if half_light_radius > 3: half_light_radius = 3 else: pass gal = galsim.InclinedSersic(n=n, flux=gal_flux, half_light_radius=half_light_radius, inclination=inclination, scale_h_over_r=scale_h_over_r ) logger.debug('created galaxy') ## Apply a random rotation theta = ud()*2.0*numpy.pi*galsim.radians gal = gal.rotate(theta) ## Get the reduced shears and magnification at this point try: nfw_shear, mu = nfw_lensing(nfw, uv_pos, gal_z) g1=nfw_shear.g1; g2=nfw_shear.g2 gal = gal.lens(g1, g2, mu) except: print("could not lens galaxy at z = %f, setting default values..." % gal_z) g1 = 0.0; g2 = 0.0 mu = 1.0 jitter_psf = galsim.Gaussian(flux=1,fwhm=0.05) final=galsim.Convolve([jitter_psf,gal,optics]) logger.debug("Convolved star and PSF at galaxy position") stamp = final.drawImage(wcs=wcs.local(image_pos)) stamp.setCenter(image_pos.x,image_pos.y) logger.debug('drew & centered galaxy!') galaxy_truth=truth() galaxy_truth.ra=ra.deg; galaxy_truth.dec=dec.deg galaxy_truth.x=image_pos.x; galaxy_truth.y=image_pos.y galaxy_truth.g1=g1; galaxy_truth.g2=g2 galaxy_truth.mu = mu; galaxy_truth.z = gal_z galaxy_truth.flux = stamp.added_flux galaxy_truth.n = n; galaxy_truth.hlr = half_light_radius galaxy_truth.inclination = inclination.deg # storing in degrees for human readability galaxy_truth.scale_h_over_r = scale_h_over_r logger.debug('created truth values') try: galaxy_truth.fwhm=final.calculateFWHM() except galsim.errors.GalSimError: logger.debug('fwhm calculation failed') galaxy_truth.fwhm=-9999.0 try: galaxy_truth.mom_size=stamp.FindAdaptiveMom().moments_sigma except galsim.errors.GalSimError: logger.debug('sigma calculation failed') galaxy_truth.mom_size=-9999. logger.debug('stamp made, moving to next galaxy') return stamp, galaxy_truth
# Characterizing observations band = 'b' # name of filter in which galaxy is being observed n_obs = 30 # number of observations of duration 'exp_time' exp_time = 600 # s sky_level = base_sky_level * exp_time # ADU / pix gal_flux = base_gal_flux * exp_time # ADU seed = np.random.randint(11111111, 99999999) ### ### Draw galaxy and PSF, correcting for (what I think) are inclination effects ### gal = galsim.InclinedSersic(n=n, flux=gal_flux, half_light_radius=half_light_radius, inclination=inclination, scale_h_over_r=scale_h_over_r) psf = galsim.Gaussian(flux=1., fwhm=psf_fwhm) final = galsim.Convolve([gal, psf]) gal_only_stamp = gal.drawImage(scale=pixel_scale, nx=32, ny=32) psf_stamp = psf.drawImage(scale=pixel_scale, nx=32, ny=32) obs_stamp = final.drawImage(scale=pixel_scale, nx=32, ny=32) ## Create noisy observations image_obslist = [] for obs in range(n_obs):