def make_cosmological_sources_file(simput_prefix, phlist_prefix, exp_time, fov, sky_center, cat_center=None, absorb_model="wabs", nH=0.05, area=40000.0, append=False, overwrite=False, output_sources=None, prng=None): r""" Make a SIMPUT catalog made up of contributions from galaxy clusters, galaxy groups, and galaxies. Parameters ---------- simput_prefix : string The filename prefix for the SIMPUT file. phlist_prefix : string The filename prefix for the photon list file. exp_time : float, (value, unit) tuple, or :class:`~astropy.units.Quantity` The exposure time of the observation in seconds. fov : float, (value, unit) tuple, or :class:`~astropy.units.Quantity` The field of view in arcminutes. sky_center : array-like The center RA, Dec of the field of view in degrees. cat_center : array-like The center of the field in the coordinates of the halo catalog, which range from -5.0 to 5.0 degrees along both axes. If None is given, a center will be randomly chosen. absorb_model : string, optional The absorption model to use, "wabs" or "tbabs". Default: "wabs" nH : float, (value, unit) tuple, or :class:`~astropy.units.Quantity`, optional The hydrogen column in units of 10**22 atoms/cm**2. Default: 0.05 area : float, (value, unit) tuple, or :class:`~astropy.units.Quantity`, optional The effective area in cm**2. It must be large enough so that a sufficiently large sample is drawn for the ARF. Default: 40000. append : boolean, optional If True, append a new source an existing SIMPUT catalog. Default: False overwrite : boolean, optional Set to True to overwrite previous files. Default: False output_sources : string, optional If set to a filename, output the properties of the sources within the field of view to a file. Default: None prng : :class:`~numpy.random.RandomState` object, integer, or None A pseudo-random number generator. Typically will only be specified if you have a reason to generate the same set of random numbers, such as for a test. Default is None, which sets the seed based on the system time. """ events = make_cosmological_sources(exp_time, fov, sky_center, cat_center=cat_center, absorb_model=absorb_model, nH=nH, area=area, output_sources=output_sources, prng=prng) write_photon_list(simput_prefix, phlist_prefix, events["flux"], events["ra"], events["dec"], events["energy"], append=append, overwrite=overwrite)
def write_simput_file(self, prefix, overwrite=False, emin=None, emax=None): r""" Write events to a SIMPUT file that may be read by the SIMX instrument simulator. Parameters ---------- prefix : string The filename prefix. overwrite : boolean, optional Set to True to overwrite previous files. e_min : float, optional The minimum energy of the photons to save in keV. e_max : float, optional The maximum energy of the photons to save in keV. """ if isinstance(self, ConvolvedEventList): raise NotImplementedError( "Writing SIMPUT files is only supported if " "you didn't convolve with responses!") events = communicate_events(self.events) if comm.rank == 0: mylog.info("Writing SIMPUT catalog file %s_simput.fits " % prefix + "and SIMPUT photon list file %s_phlist.fits." % prefix) if emin is None and emax is None: idxs = slice(None, None, None) else: if emin is None: emin = events["eobs"].min().value if emax is None: emax = events["eobs"].max().value idxs = np.logical_and(events["eobs"].d >= emin, events["eobs"].d <= emax) flux = np.sum(events["eobs"][idxs]).to("erg") / \ self.parameters["exp_time"]/self.parameters["area"] write_photon_list(prefix, prefix, flux.v, events["xsky"][idxs].d, events["ysky"][idxs].d, events["eobs"][idxs].d, overwrite=overwrite) comm.barrier()
def test_append(): tmpdir = tempfile.mkdtemp() curdir = os.getcwd() os.chdir(tmpdir) exp_time = (50.0, "ks") area = (4.0, "m**2") ra0 = 30.0 dec0 = 45.0 spec = Spectrum.from_powerlaw(1.1, 0.05, 1.0e-4) e1 = spec.generate_energies(exp_time, area, prng=prng) pt_src1 = PointSourceModel(ra0 + 0.05, dec0 + 0.05, e1.size) e2 = spec.generate_energies(exp_time, area, prng=prng) pt_src2 = PointSourceModel(ra0 - 0.05, dec0 - 0.05, e1.size) write_photon_list("pt_src", "pt_src1", e1.flux, pt_src1.ra, pt_src1.dec, e1, overwrite=True) write_photon_list("pt_src", "pt_src2", e2.flux, pt_src2.ra, pt_src2.dec, e2, append=True) assert os.path.exists("pt_src_simput.fits") assert os.path.exists("pt_src1_phlist.fits") assert os.path.exists("pt_src2_phlist.fits") f = pyfits.open("pt_src_simput.fits") cat = f["SRC_CAT"].data["SPECTRUM"] assert cat[0] == "pt_src1_phlist.fits[PHLIST,1]" assert cat[1] == "pt_src2_phlist.fits[PHLIST,1]" f.close() os.chdir(curdir) shutil.rmtree(tmpdir)
def test_point_source(): tmpdir = tempfile.mkdtemp() curdir = os.getcwd() os.chdir(tmpdir) e = spec.generate_energies(exp_time, area, prng=prng) pt_src = PointSourceModel(ra0, dec0, e.size) write_photon_list("pt_src", "pt_src", e.flux, pt_src.ra, pt_src.dec, e, overwrite=True) inst = get_instrument_from_registry("hdxi") inst["name"] = "hdxi_big_psf" inst["psf"] = ["gaussian", 5.0] add_instrument_to_registry(inst) instrument_simulator("pt_src_simput.fits", "pt_src_evt.fits", exp_time, "hdxi_big_psf", [ra0, dec0], ptsrc_bkgnd=False, instr_bkgnd=False, foreground=False, prng=prng) psf_scale = inst["psf"][1] dtheta = inst["fov"] * 60.0 / inst["num_pixels"] f = pyfits.open("pt_src_evt.fits") x = f["EVENTS"].data["X"] y = f["EVENTS"].data["Y"] f.close() scalex = np.std(x) * sigma_to_fwhm * dtheta scaley = np.std(y) * sigma_to_fwhm * dtheta assert (scalex - psf_scale) / psf_scale < 0.01 assert (scaley - psf_scale) / psf_scale < 0.01 os.chdir(curdir) shutil.rmtree(tmpdir)
def write_simput_file(self, prefix, overwrite=False, emin=None, emax=None): r""" Write events to a SIMPUT file that may be read by the SIMX instrument simulator. Parameters ---------- prefix : string The filename prefix. overwrite : boolean, optional Set to True to overwrite previous files. e_min : float, optional The minimum energy of the photons to save in keV. e_max : float, optional The maximum energy of the photons to save in keV. """ if isinstance(self, ConvolvedEventList): raise NotImplementedError("Writing SIMPUT files is only supported if " "you didn't convolve with responses!") events = communicate_events(self.events) if comm.rank == 0: mylog.info("Writing SIMPUT catalog file %s_simput.fits " % prefix + "and SIMPUT photon list file %s_phlist.fits." % prefix) if emin is None and emax is None: idxs = slice(None, None, None) else: if emin is None: emin = events["eobs"].min().value if emax is None: emax = events["eobs"].max().value idxs = np.logical_and(events["eobs"].d >= emin, events["eobs"].d <= emax) flux = np.sum(events["eobs"][idxs]).to("erg") / \ self.parameters["exp_time"]/self.parameters["area"] write_photon_list(prefix, prefix, flux.v, events["xsky"][idxs].d, events["ysky"][idxs].d, events["eobs"][idxs].d, overwrite=overwrite) comm.barrier()
def test_append(): tmpdir = tempfile.mkdtemp() curdir = os.getcwd() os.chdir(tmpdir) exp_time = (50.0, "ks") area = (4.0, "m**2") ra0 = 30.0 dec0 = 45.0 spec = Spectrum.from_powerlaw(1.1, 0.05, 1.0e-4, 0.1, 10.0, 10000) e1 = spec.generate_energies(exp_time, area, prng=prng) ra1, dec1 = PointSourceModel(ra0+0.05, dec0+0.05).generate_coords(e1.size, prng=prng) e2 = spec.generate_energies(exp_time, area, prng=prng) ra2, dec2 = PointSourceModel(ra0-0.05, dec0-0.05).generate_coords(e1.size, prng=prng) write_photon_list("pt_src", "pt_src1", e1.flux, ra1, dec1, e1, overwrite=True) write_photon_list("pt_src", "pt_src2", e2.flux, ra2, dec2, e2, append=True) assert os.path.exists("pt_src_simput.fits") assert os.path.exists("pt_src1_phlist.fits") assert os.path.exists("pt_src2_phlist.fits") f = pyfits.open("pt_src_simput.fits") cat = f["SRC_CAT"].data["SPECTRUM"] assert cat[0] == "pt_src1_phlist.fits[PHLIST,1]" assert cat[1] == "pt_src2_phlist.fits[PHLIST,1]" f.close() os.chdir(curdir) shutil.rmtree(tmpdir)
def plaw_fit(alpha_sim): tmpdir = tempfile.mkdtemp() curdir = os.getcwd() os.chdir(tmpdir) nH_sim = 0.02 norm_sim = 1.0e-4 redshift = 0.01 exp_time = 5.0e4 area = 40000.0 inst_name = "hdxi" spec = Spectrum.from_powerlaw(alpha_sim, redshift, norm_sim) spec.apply_foreground_absorption(nH_sim) e = spec.generate_energies(exp_time, area) pt_src = PointSourceModel(30.0, 45.0, e.size) write_photon_list("plaw_model", "plaw_model", e.flux, pt_src.ra, pt_src.dec, e, clobber=True) instrument_simulator("plaw_model_simput.fits", "plaw_model_evt.fits", exp_time, inst_name, [30.0, 45.0], astro_bkgnd=None, instr_bkgnd_scale=0.0) inst = get_instrument_from_registry(inst_name) arf = AuxiliaryResponseFile(inst["arf"]) rmf = RedistributionMatrixFile(inst["rmf"]) os.system("cp %s ." % arf.filename) os.system("cp %s ." % rmf.filename) write_spectrum("plaw_model_evt.fits", "plaw_model_evt.pha", clobber=True) load_user_model(mymodel, "wplaw") add_user_pars("wplaw", ["nH", "norm", "redshift", "alpha"], [0.01, norm_sim * 0.8, redshift, 0.9], parmins=[0.0, 0.0, 0.0, 0.1], parmaxs=[10.0, 1.0e9, 10.0, 10.0], parfrozen=[False, False, True, False]) load_pha("plaw_model_evt.pha") set_stat("cstat") set_method("simplex") ignore(":0.5, 9.0:") set_model("wplaw") fit() set_covar_opt("sigma", 1.645) covar() res = get_covar_results() assert np.abs(res.parvals[0] - nH_sim) < res.parmaxes[0] assert np.abs(res.parvals[1] - norm_sim) < res.parmaxes[1] assert np.abs(res.parvals[2] - alpha_sim) < res.parmaxes[2] os.chdir(curdir) shutil.rmtree(tmpdir)
def test_annulus(): tmpdir = tempfile.mkdtemp() curdir = os.getcwd() os.chdir(tmpdir) r_in = 10.0 r_out = 30.0 e = spec.generate_energies(exp_time, area, prng=prng) ann_src = AnnulusModel(ra0, dec0, r_in, r_out, e.size, prng=prng) write_photon_list("ann", "ann", e.flux, ann_src.ra, ann_src.dec, e, overwrite=True) instrument_simulator("ann_simput.fits", "ann_evt.fits", exp_time, "hdxi", [ra0, dec0], ptsrc_bkgnd=False, instr_bkgnd=False, foreground=False, prng=prng) inst = get_instrument_from_registry("hdxi") arf = AuxiliaryResponseFile(inst["arf"]) cspec = ConvolvedSpectrum(spec, arf) ph_flux = cspec.get_flux_in_band(0.5, 7.0)[0].value S0 = ph_flux / (np.pi * (r_out**2 - r_in**2)) write_radial_profile("ann_evt.fits", "ann_evt_profile.fits", [ra0, dec0], 1.1 * r_in, 0.9 * r_out, 100, ctr_type="celestial", emin=0.5, emax=7.0, overwrite=True) load_data(1, "ann_evt_profile.fits", 3, ["RMID", "SUR_BRI", "SUR_BRI_ERR"]) set_stat("chi2") set_method("levmar") set_source("const1d.src") src.c0 = 0.8 * S0 fit() set_covar_opt("sigma", 1.645) covar() res = get_covar_results() assert np.abs(res.parvals[0] - S0) < res.parmaxes[0] os.chdir(curdir) shutil.rmtree(tmpdir)
def test_beta_model_flux(): tmpdir = tempfile.mkdtemp() curdir = os.getcwd() os.chdir(tmpdir) r_c = 20.0 beta = 1.0 prng = 34 e = spec.generate_energies(exp_time, area, prng=prng) beta_src = BetaModel(ra0, dec0, r_c, beta, e.size, prng=prng) write_photon_list("beta", "beta", e.flux, beta_src.ra, beta_src.dec, e, overwrite=True) instrument_simulator("beta_simput.fits", "beta_evt.fits", exp_time, "acisi_cy0", [ra0, dec0], ptsrc_bkgnd=False, instr_bkgnd=False, foreground=False, roll_angle=37.0, prng=prng) ph_flux = spec.get_flux_in_band(0.5, 7.0)[0].value S0 = 3.0 * ph_flux / (2.0 * np.pi * r_c * r_c) wspec = spec.new_spec_from_band(0.5, 7.0) make_exposure_map("beta_evt.fits", "beta_expmap.fits", wspec.emid.value, weights=wspec.flux.value, overwrite=True) write_radial_profile("beta_evt.fits", "beta_evt_profile.fits", [ra0, dec0], 0.0, 100.0, 200, ctr_type="celestial", emin=0.5, emax=7.0, expmap_file="beta_expmap.fits", overwrite=True) load_data(1, "beta_evt_profile.fits", 3, ["RMID", "SUR_FLUX", "SUR_FLUX_ERR"]) set_stat("chi2") set_method("levmar") set_source("beta1d.src") src.beta = 1.0 src.r0 = 10.0 src.ampl = 0.8 * S0 freeze(src.xpos) fit() set_covar_opt("sigma", 1.645) covar() res = get_covar_results() assert np.abs(res.parvals[0] - r_c) < res.parmaxes[0] assert np.abs(res.parvals[1] - beta) < res.parmaxes[1] assert np.abs(res.parvals[2] - S0) < res.parmaxes[2] os.chdir(curdir) shutil.rmtree(tmpdir)
def test_beta_model(): tmpdir = tempfile.mkdtemp() curdir = os.getcwd() os.chdir(tmpdir) r_c = 20.0 beta = 1.0 exp_time = Quantity(500.0, "ks") e = spec.generate_energies(exp_time, area, prng=prng) beta_src = BetaModel(ra0, dec0, r_c, beta, e.size, prng=prng) write_photon_list("beta", "beta", e.flux, beta_src.ra, beta_src.dec, e, overwrite=True) instrument_simulator("beta_simput.fits", "beta_evt.fits", exp_time, "hdxi", [ra0, dec0], ptsrc_bkgnd=False, instr_bkgnd=False, foreground=False, prng=prng) inst = get_instrument_from_registry("hdxi") arf = AuxiliaryResponseFile(inst["arf"]) cspec = ConvolvedSpectrum(spec, arf) ph_flux = cspec.get_flux_in_band(0.5, 7.0)[0].value S0 = 3.0 * ph_flux / (2.0 * np.pi * r_c * r_c) write_radial_profile("beta_evt.fits", "beta_evt_profile.fits", [ra0, dec0], 0.0, 100.0, 200, ctr_type="celestial", emin=0.5, emax=7.0, overwrite=True) load_data(1, "beta_evt_profile.fits", 3, ["RMID", "SUR_BRI", "SUR_BRI_ERR"]) set_stat("chi2") set_method("levmar") set_source("beta1d.src") src.beta = 1.0 src.r0 = 10.0 src.ampl = 0.8 * S0 freeze(src.xpos) fit() set_covar_opt("sigma", 1.645) covar() res = get_covar_results() assert np.abs(res.parvals[0] - r_c) < res.parmaxes[0] assert np.abs(res.parvals[1] - beta) < res.parmaxes[1] assert np.abs(res.parvals[2] - S0) < res.parmaxes[2] os.chdir(curdir) shutil.rmtree(tmpdir)