Ejemplo n.º 1
0
def test_beta_model_flux(answer_store, answer_dir):
    tmpdir = tempfile.mkdtemp()
    curdir = os.getcwd()
    os.chdir(tmpdir)

    r_c = 20.0
    beta = 1.0

    prng = 34

    beta_src_pos = BetaModel(ra0, dec0, r_c, beta)
    sim_cat = SimputCatalog.from_models("beta", "beta", spec, beta_src_pos,
                                        exp_time, area, prng=prng)
    sim_cat.write_catalog(overwrite=True)

    instrument_simulator("beta_simput.fits", "beta_flux_evt.fits", exp_time,
                         "acisi_cy0", [ra0, dec0], ptsrc_bkgnd=False,
                         instr_bkgnd=False, foreground=False, 
                         roll_angle=37.0, prng=prng)

    wspec = spec.new_spec_from_band(0.5, 7.0)

    make_exposure_map("beta_flux_evt.fits", "beta_expmap.fits", wspec.emid.value,
                      weights=wspec.flux.value, overwrite=True)

    write_radial_profile("beta_flux_evt.fits", "beta_flux_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)

    file_answer_testing("EVENTS", "beta_flux_evt.fits", answer_store, answer_dir)
    file_answer_testing("PROFILE", "beta_flux_evt_profile.fits", answer_store, answer_dir)

    os.chdir(curdir)
    shutil.rmtree(tmpdir)
Ejemplo n.º 2
0
def test_beta_model_flux(answer_store, answer_dir):
    tmpdir = tempfile.mkdtemp()
    curdir = os.getcwd()
    os.chdir(tmpdir)

    r_c = 20.0
    beta = 1.0

    prng = 34

    beta_src_pos = BetaModel(ra0, dec0, r_c, beta)
    sim_cat = SimputCatalog.from_models("beta",
                                        "beta",
                                        spec,
                                        beta_src_pos,
                                        exp_time,
                                        area,
                                        prng=prng)
    sim_cat.write_catalog(overwrite=True)

    instrument_simulator("beta_simput.fits",
                         "beta_flux_evt.fits",
                         exp_time,
                         "acisi_cy0", [ra0, dec0],
                         ptsrc_bkgnd=False,
                         instr_bkgnd=False,
                         foreground=False,
                         roll_angle=37.0,
                         prng=prng)

    wspec = spec.new_spec_from_band(0.5, 7.0)

    make_exposure_map("beta_flux_evt.fits",
                      "beta_expmap.fits",
                      wspec.emid.value,
                      weights=wspec.flux.value,
                      overwrite=True)

    write_radial_profile("beta_flux_evt.fits",
                         "beta_flux_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)

    file_answer_testing("EVENTS", "beta_flux_evt.fits", answer_store,
                        answer_dir)
    file_answer_testing("PROFILE", "beta_flux_evt_profile.fits", answer_store,
                        answer_dir)

    os.chdir(curdir)
    shutil.rmtree(tmpdir)
Ejemplo n.º 3
0
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)
Ejemplo n.º 4
0
def make_mosaic_image(evtfile_list,
                      image_file,
                      emin=None,
                      emax=None,
                      reblock=1,
                      use_expmap=False,
                      expmap_energy=None,
                      expmap_weights=None,
                      normalize=True,
                      nhistx=16,
                      nhisty=16,
                      overwrite=False):
    """
    Make a single FITS image from a grid of observations. Optionally,
    an exposure map can be computed and a flux image may be generated.

    Parameters
    ----------
    evtfile_list : filename
        The ASCII table produced by :meth:`~soxs.grid.observe_grid_source`
        containing the information about the event files and their
        locations on the sky.
    image_file : filename
        The name of the FITS image file to be written. This name will
        also be used for the exposure map and flux files if they are
        written.
    emin : float, (value, unit) tuple, or :class:`~astropy.units.Quantity`, optional
        The minimum energy of the photons to put in the image, in keV.
    emax : float, (value, unit) tuple, or :class:`~astropy.units.Quantity`, optional
        The maximum energy of the photons to put in the image, in keV.
    reblock : integer, optional
        Supply an integer power of 2 here to make an exposure map 
        with a different binning. Default: 1
    use_expmap : boolean, optional
        Whether or not to use (and potentially generate) an exposure map
        and a flux map. Default: False
    expmap_energy : float, (value, unit) tuple, or :class:`~astropy.units.Quantity`, or NumPy array, optional
        The energy in keV to use when computing the exposure map, or 
        a set of energies to be used with the *weights* parameter. If
        providing a set, it must be in keV.
    expmap_weights : array-like, optional
        The weights to use with a set of energies given in the
        *energy* parameter. Used to create a more accurate exposure
        map weighted by a range of energies. Default: None
    overwrite : boolean, optional
        Whether or not to overwrite an existing file with the same name.
        Default: False
    """
    try:
        from reproject.mosaicking import find_optimal_celestial_wcs, \
            reproject_and_coadd
        from reproject import reproject_interp
    except ImportError:
        raise ImportError("The mosaic functionality of SOXS requires the "
                          "'reproject' package to be installed!")
    t = ascii.read(evtfile_list,
                   format='commented_header',
                   guess=False,
                   header_start=0,
                   delimiter="\t")

    files = []
    for row in t:
        evt_file = row["evtfile"]
        img_file = evt_file.replace("evt", "img")
        if use_expmap:
            emap_file = evt_file.replace("evt", "expmap")
            make_exposure_map(evt_file,
                              emap_file,
                              energy=expmap_energy,
                              weights=expmap_weights,
                              normalize=normalize,
                              overwrite=overwrite,
                              reblock=reblock,
                              nhistx=nhistx,
                              nhisty=nhisty)
        else:
            emap_file = None
        write_image(evt_file,
                    img_file,
                    emin=emin,
                    emax=emax,
                    overwrite=overwrite,
                    reblock=reblock)
        files.append([img_file, emap_file])

    img_hdus = [fits.open(fns[0], memmap=True)[0] for fns in files]
    wcs_out, shape_out = find_optimal_celestial_wcs(img_hdus)
    img, footprint = reproject_and_coadd(img_hdus,
                                         wcs_out,
                                         shape_out=shape_out,
                                         reproject_function=reproject_interp,
                                         combine_function='sum')
    hdu = fits.PrimaryHDU(img, header=wcs_out.to_header())
    hdu.writeto(image_file, overwrite=overwrite)

    if use_expmap:
        if expmap_energy is None:
            raise RuntimeError("The 'expmap_energy' argument must be set if "
                               "making a mosaicked exposure map!")
        emap_hdus = [fits.open(fns[1], memmap=True)[1] for fns in files]
        emap, footprint = reproject_and_coadd(
            emap_hdus,
            wcs_out,
            shape_out=shape_out,
            reproject_function=reproject_interp,
            combine_function='sum')
        hdu = fits.PrimaryHDU(emap, header=wcs_out.to_header())
        expmap_file = image_file.replace("fits", "expmap")
        hdu.writeto(expmap_file, overwrite=overwrite)

        with np.errstate(invalid='ignore', divide='ignore'):
            flux = img / emap
        flux[np.isinf(flux)] = 0.0
        flux = np.nan_to_num(flux)
        flux[flux < 0.0] = 0.0
        hdu = fits.PrimaryHDU(flux, header=wcs_out.to_header())
        flux_file = image_file.replace("fits", "flux")
        hdu.writeto(flux_file, overwrite=overwrite)