Exemple #1
0
def test_calc_int_flux_sensitivity():

    ltc = LTCube.create_from_obs_time(3.1536E8)
    c = SkyCoord(10.0, 10.0, unit='deg', frame='galactic')
    ebins = 10**np.linspace(2.0, 5.0, 8 * 3 + 1)

    gdiff = Map.create_from_fits(galdiff_path)
    iso = np.loadtxt(
        os.path.expandvars('$FERMI_DIFFUSE_DIR/iso_P8R3_SOURCE_V3_v1.txt'),
        unpack=True)
    scalc = SensitivityCalc(gdiff, iso, ltc, ebins, 'P8R3_SOURCE_V3',
                            [['FRONT', 'BACK']])

    fn = spectrum.PowerLaw([1E-13, -2.0], scale=1E3)
    o = scalc.int_flux_threshold(c, fn, 25.0, 3.0)

    assert_allclose(o['eflux'], 1.15296479181e-06, rtol=3E-3)
    assert_allclose(o['flux'], 1.66741840222e-09, rtol=3E-3)
    assert_allclose(o['npred'], 549.71066228, rtol=3E-3)
    assert_allclose(o['dnde'], 1.66908748971e-14, rtol=3E-3)
    assert_allclose(o['e2dnde'], 1.66908748971e-07, rtol=3E-3)

    assert_allclose(o['bins']['flux'],
                    np.array([
                        4.180875e-10, 3.135214e-10, 2.351078e-10, 1.763060e-10,
                        1.322108e-10, 9.914417e-11, 7.434764e-11, 5.575286e-11,
                        4.180876e-11, 3.135216e-11, 2.351078e-11, 1.763060e-11,
                        1.322108e-11, 9.914417e-12, 7.434764e-12, 5.575286e-12,
                        4.180875e-12, 3.135214e-12, 2.351078e-12, 1.763060e-12,
                        1.322108e-12, 9.914417e-13, 7.434764e-13, 5.575286e-13
                    ]),
                    rtol=1E-3)
def test_calc_int_flux_sensitivity():

    ltc = LTCube.create_from_obs_time(3.1536E8)
    c = SkyCoord(10.0, 10.0, unit='deg', frame='galactic')
    ebins = 10**np.linspace(2.0, 5.0, 8 * 3 + 1)

    gdiff = Map.create_from_fits(galdiff_path)
    iso = np.loadtxt(os.path.expandvars('$FERMIPY_ROOT/data/iso_P8R2_SOURCE_V6_v06.txt'),
                     unpack=True)
    scalc = flux_sensitivity.SensitivityCalc(gdiff, iso, ltc, ebins,
                                             'P8R2_SOURCE_V6', [['FRONT', 'BACK']])

    fn = spectrum.PowerLaw([1E-13, -2.0], scale=1E3)
    o = scalc.int_flux_threshold(c, fn, 25.0, 3.0)

    assert_allclose(o['eflux'], 1.0719847971553671e-06, rtol=1E-3)
    assert_allclose(o['flux'], 1.550305083355546e-09, rtol=1E-3)
    assert_allclose(o['npred'], 511.16725330021416, rtol=1E-3)
    assert_allclose(o['dnde'], 1.5518569402958423e-14, rtol=1E-3)
    assert_allclose(o['e2dnde'], 1.5518569402958427e-07, rtol=1E-3)

    assert_allclose(o['bins']['flux'],
                    np.array([3.88128407e-10,   2.91055245e-10,   2.18260643e-10,
                              1.63672392e-10,   1.22736979e-10,   9.20397499e-11,
                              6.90200755e-11,   5.17577549e-11,   3.88128407e-11,
                              2.91055245e-11,   2.18260643e-11,   1.63672392e-11,
                              1.22736979e-11,   9.20397499e-12,   6.90200755e-12,
                              5.17577549e-12,   3.88128407e-12,   2.91055245e-12,
                              2.18260643e-12,   1.63672392e-12,   1.22736979e-12,
                              9.20397499e-13,   6.90200755e-13,   5.17577549e-13]),
                    rtol=1E-3)
Exemple #3
0
def test_calc_int_flux_sensitivity():

    ltc = LTCube.create_from_obs_time(3.1536E8)
    c = SkyCoord(10.0, 10.0, unit='deg', frame='galactic')
    ebins = 10**np.linspace(2.0, 5.0, 8 * 3 + 1)

    gdiff = Map.create_from_fits(galdiff_path)
    iso = np.loadtxt(
        os.path.expandvars('$FERMIPY_ROOT/data/iso_P8R2_SOURCE_V6_v06.txt'),
        unpack=True)
    scalc = SensitivityCalc(gdiff, iso, ltc, ebins, 'P8R2_SOURCE_V6',
                            [['FRONT', 'BACK']])

    fn = spectrum.PowerLaw([1E-13, -2.0], scale=1E3)
    o = scalc.int_flux_threshold(c, fn, 25.0, 3.0)

    assert_allclose(o['eflux'], 1.0719847971553671e-06, rtol=3E-3)
    assert_allclose(o['flux'], 1.550305083355546e-09, rtol=3E-3)
    assert_allclose(o['npred'], 511.16725330021416, rtol=3E-3)
    assert_allclose(o['dnde'], 1.5518569402958423e-14, rtol=3E-3)
    assert_allclose(o['e2dnde'], 1.5518569402958427e-07, rtol=3E-3)

    assert_allclose(
        o['bins']['flux'],
        np.array([
            3.88128407e-10, 2.91055245e-10, 2.18260643e-10, 1.63672392e-10,
            1.22736979e-10, 9.20397499e-11, 6.90200755e-11, 5.17577549e-11,
            3.88128407e-11, 2.91055245e-11, 2.18260643e-11, 1.63672392e-11,
            1.22736979e-11, 9.20397499e-12, 6.90200755e-12, 5.17577549e-12,
            3.88128407e-12, 2.91055245e-12, 2.18260643e-12, 1.63672392e-12,
            1.22736979e-12, 9.20397499e-13, 6.90200755e-13, 5.17577549e-13
        ]),
        rtol=1E-3)
def test_calc_diff_flux_sensitivity():

    ltc = LTCube.create_from_obs_time(3.1536E8)
    c = SkyCoord(10.0, 10.0, unit='deg', frame='galactic')
    ebins = 10**np.linspace(2.0, 5.0, 8 * 3 + 1)

    gdiff = Map.create_from_fits(galdiff_path)
    iso = np.loadtxt(os.path.expandvars('$FERMIPY_ROOT/data/iso_P8R2_SOURCE_V6_v06.txt'),
                     unpack=True)
    scalc = flux_sensitivity.SensitivityCalc(gdiff, iso, ltc, ebins,
                                             'P8R2_SOURCE_V6', [['FRONT', 'BACK']])

    fn = spectrum.PowerLaw([1E-13, -2.0], scale=1E3)
    o = scalc.diff_flux_threshold(c, fn, 25.0, 3.0)
    assert_allclose(o['eflux'],
                    np.array([9.46940878e-07,   8.18350327e-07,   7.08228859e-07,
                              6.25785181e-07,   5.45241744e-07,   4.80434705e-07,
                              4.16747935e-07,   3.68406513e-07,   3.16719850e-07,
                              2.79755007e-07,   2.50862769e-07,   2.31349646e-07,
                              2.16286209e-07,   2.08381335e-07,   2.02673929e-07,
                              2.05372045e-07,   2.14355673e-07,   2.29584275e-07,
                              2.53220397e-07,   2.80878974e-07,   3.19989251e-07,
                              3.74686765e-07,   4.49321103e-07,   5.48865583e-07]),
                    rtol=1E-3)

    assert_allclose(o['flux'],
                    np.array([8.22850449e-09,   5.33257909e-09,   3.46076145e-09,
                              2.29310172e-09,   1.49825986e-09,   9.89993669e-10,
                              6.43978699e-10,   4.26899204e-10,   2.75215778e-10,
                              1.82295486e-10,   1.22584132e-10,   8.47748218e-11,
                              5.94328933e-11,   4.29394879e-11,   3.13181379e-11,
                              2.37979404e-11,   1.86265760e-11,   1.49602959e-11,
                              1.23736187e-11,   1.02924147e-11,   8.79292634e-12,
                              7.72087281e-12,   6.94312304e-12,   6.36010146e-12]),
                    rtol=1E-3)
def test_calc_diff_flux_sensitivity(create_diffuse_dir):

    ltc = LTCube.create_from_obs_time(3.1536E8)
    c = SkyCoord(10.0, 10.0, unit='deg', frame='galactic')
    ebins = 10**np.linspace(2.0, 5.0, 8 * 3 + 1)

    gdiff = Map.create_from_fits(galdiff_path)
    iso = np.loadtxt(os.path.expandvars('$FERMI_DIFFUSE_DIR/iso_P8R3_SOURCE_V3_v1.txt'),
                     unpack=True)
    scalc = SensitivityCalc(gdiff, iso, ltc, ebins,
                            'P8R3_SOURCE_V3', [['FRONT', 'BACK']])

    fn = spectrum.PowerLaw([1E-13, -2.0], scale=1E3)
    o = scalc.diff_flux_threshold(c, fn, 25.0, 3.0)
    
    assert_allclose(o['eflux'],
                    np.array([9.93077714e-07, 8.59371115e-07, 7.44896026e-07, 6.61397166e-07,
                                  5.67842613e-07, 4.93802411e-07, 4.22501374e-07, 3.69299411e-07,
                                  3.16648674e-07, 2.81050363e-07, 2.52941925e-07, 2.35549281e-07,
                                  2.27269823e-07, 2.33109741e-07, 2.28212061e-07, 2.22473996e-07,
                                  2.26623213e-07, 2.46539227e-07, 2.61423960e-07, 3.04766386e-07,
                                  3.35688695e-07, 4.09265051e-07, 4.79776973e-07, 5.93001330e-07]),
                    rtol=3E-3)

    assert_allclose(o['flux'],
                    np.array([8.62941353e-09, 5.59988099e-09, 3.63993562e-09, 2.42359683e-09,
                                  1.56036437e-09, 1.01753944e-09, 6.52869186e-10, 4.27933870e-10,
                                  2.75153929e-10, 1.83139573e-10, 1.23600112e-10, 8.63137188e-11,
                                  6.24510606e-11, 4.80350743e-11, 3.52644113e-11, 2.57796669e-11,
                                  1.96925718e-11, 1.60651237e-11, 1.27744860e-11, 1.11677353e-11,
                                  9.22432852e-12, 8.43340011e-12, 7.41374161e-12, 6.87153420e-12]),
                    rtol=3E-3)
Exemple #6
0
    def process(self, filename):

        tab = Table.read(filename, 'EVENTS')
        tab_gti = Table.read(filename, 'GTI')
        print('loading events')
        self.load_events(tab)

        skydir = SkyCoord(0.0, 0.0, unit='deg')

        print('creating LT Cube')
        ltc = LTCube.create_from_gti(skydir, self._tab_sc, tab_gti, self._zmax)
        self._ltc.load(ltc)
Exemple #7
0
    def process(self, filename):

        tab = Table.read(filename, 'EVENTS')
        tab_gti = Table.read(filename, 'GTI')
        print('loading events')
        self.load_events(tab)

        skydir = SkyCoord(0.0, 0.0, unit='deg')

        print('creating LT Cube')
        
        
        ltc = LTCube.create_from_gti(skydir, self._tab_sc, tab_gti,
                                     self._zmax)

        if self._ltc is None:
            self._ltc = ltc
        else:
            self._ltc.load(ltc)
Exemple #8
0
    def __init__(self, scfile, zmax):

        self._scfile = scfile
        self._energy_bins = 10**np.linspace(1.0, 6.0, 41)
        self._ctheta_bins = np.linspace(0.0, 1.0, 11)
        self._xsep_bins = np.linspace(0.0, 1.0, 101)**2
        self._evclass_bins = np.linspace(0.0, 16.0, 17)
        self._evtype_bins = np.linspace(0.0, 16.0, 17)

        egy = np.sqrt(self._energy_bins[1:] * self._energy_bins[:-1])
        scale = np.sqrt((20. * (egy / 100.)**-0.8)**2 + 2.**2)
        scale[scale > 30.] = 30.
        self._psf_scale = scale
        self._sep_bins = self._xsep_bins[None, :] * self._psf_scale[:, None]
        self._domega = np.pi * \
            (self._sep_bins[:, 1:]**2 - self._sep_bins[:, :-1]**2)
        self._hists = {}
        self.init()
        self._tab_sc = Table.read(scfile)
        self._zmax = zmax
        self._ltc = LTCube.create_empty()
Exemple #9
0
def test_calc_diff_flux_sensitivity():

    ltc = LTCube.create_from_obs_time(3.1536E8)
    c = SkyCoord(10.0, 10.0, unit='deg', frame='galactic')
    ebins = 10**np.linspace(2.0, 5.0, 8 * 3 + 1)

    gdiff = Map.create_from_fits(galdiff_path)
    iso = np.loadtxt(
        os.path.expandvars('$FERMIPY_ROOT/data/iso_P8R2_SOURCE_V6_v06.txt'),
        unpack=True)
    scalc = SensitivityCalc(gdiff, iso, ltc, ebins, 'P8R2_SOURCE_V6',
                            [['FRONT', 'BACK']])

    fn = spectrum.PowerLaw([1E-13, -2.0], scale=1E3)
    o = scalc.diff_flux_threshold(c, fn, 25.0, 3.0)
    assert_allclose(
        o['eflux'],
        np.array([
            9.46940878e-07, 8.18350327e-07, 7.08228859e-07, 6.25785181e-07,
            5.45241744e-07, 4.80434705e-07, 4.16747935e-07, 3.68406513e-07,
            3.16719850e-07, 2.79755007e-07, 2.50862769e-07, 2.31349646e-07,
            2.16286209e-07, 2.08381335e-07, 2.02673929e-07, 2.05372045e-07,
            2.14355673e-07, 2.29584275e-07, 2.53220397e-07, 2.80878974e-07,
            3.19989251e-07, 3.74686765e-07, 4.49321103e-07, 5.48865583e-07
        ]),
        rtol=3E-3)

    assert_allclose(
        o['flux'],
        np.array([
            8.22850449e-09, 5.33257909e-09, 3.46076145e-09, 2.29310172e-09,
            1.49825986e-09, 9.89993669e-10, 6.43978699e-10, 4.26899204e-10,
            2.75215778e-10, 1.82295486e-10, 1.22584132e-10, 8.47748218e-11,
            5.94328933e-11, 4.29394879e-11, 3.13181379e-11, 2.37979404e-11,
            1.86265760e-11, 1.49602959e-11, 1.23736187e-11, 1.02924147e-11,
            8.79292634e-12, 7.72087281e-12, 6.94312304e-12, 6.36010146e-12
        ]),
        rtol=3E-3)
Exemple #10
0
def run_flux_sensitivity(**kwargs):

    index = kwargs.get('index', 2.0)
    sedshape = kwargs.get('sedshape', 'PowerLaw')
    cutoff = kwargs.get('cutoff', 1e3)
    curvindex = kwargs.get('curvindex', 1.0)
    beta = kwargs.get('beta', 0.0)
    emin = kwargs.get('emin', 10**1.5)
    emax = kwargs.get('emax', 10**6.0)
    nbin = kwargs.get('nbin', 18)
    glon = kwargs.get('glon', 0.0)
    glat = kwargs.get('glat', 0.0)
    ltcube_filepath = kwargs.get('ltcube', None)
    galdiff_filepath = kwargs.get('galdiff', None)
    isodiff_filepath = kwargs.get('isodiff', None)
    galdiff_fit_filepath = kwargs.get('galdiff_fit', None)
    isodiff_fit_filepath = kwargs.get('isodiff_fit', None)
    wcs_npix = kwargs.get('wcs_npix', 40)
    wcs_cdelt = kwargs.get('wcs_cdelt', 0.5)
    wcs_proj = kwargs.get('wcs_proj', 'AIT')
    map_type = kwargs.get('map_type', None)
    spatial_model = kwargs.get('spatial_model', 'PointSource')
    spatial_size = kwargs.get('spatial_size', 1E-2)

    obs_time_yr = kwargs.get('obs_time_yr', None)
    event_class = kwargs.get('event_class', 'P8R2_SOURCE_V6')
    min_counts = kwargs.get('min_counts', 3.0)
    ts_thresh = kwargs.get('ts_thresh', 25.0)
    nside = kwargs.get('hpx_nside', 16)
    output = kwargs.get('output', None)

    event_types = [['FRONT', 'BACK']]

    if sedshape == 'PowerLaw':
        fn = spectrum.PowerLaw([1E-13, -index], scale=1E3)
    elif sedshape == 'PLSuperExpCutoff':
        fn = spectrum.PLSuperExpCutoff([1E-13, -index, cutoff, curvindex],
                                       scale=1E3)
    elif sedshape == 'LogParabola':
        fn = spectrum.LogParabola([1E-13, -index, beta], scale=1E3)

    log_ebins = np.linspace(np.log10(emin), np.log10(emax), nbin + 1)
    ebins = 10**log_ebins
    ectr = np.exp(utils.edge_to_center(np.log(ebins)))

    c = SkyCoord(glon, glat, unit='deg', frame='galactic')

    if ltcube_filepath is None:

        if obs_time_yr is None:
            raise Exception('No observation time defined.')

        ltc = LTCube.create_from_obs_time(obs_time_yr * 365 * 24 * 3600.)
    else:
        ltc = LTCube.create(ltcube_filepath)
        if obs_time_yr is not None:
            ltc._counts *= obs_time_yr * 365 * \
                24 * 3600. / (ltc.tstop - ltc.tstart)

    gdiff = skymap.Map.create_from_fits(galdiff_filepath)
    gdiff_fit = None
    if galdiff_fit_filepath is not None:
        gdiff_fit = skymap.Map.create_from_fits(galdiff_fit_filepath)

    if isodiff_filepath is None:
        isodiff = utils.resolve_file_path('iso_%s_v06.txt' % event_class,
                                          search_dirs=[
                                              os.path.join(
                                                  '$FERMIPY_ROOT', 'data'),
                                              '$FERMI_DIFFUSE_DIR'
                                          ])
        isodiff = os.path.expandvars(isodiff)
    else:
        isodiff = isodiff_filepath

    iso = np.loadtxt(isodiff, unpack=True)
    iso_fit = None
    if isodiff_fit_filepath is not None:
        iso_fit = np.loadtxt(isodiff_fit_filepath, unpack=True)

    scalc = SensitivityCalc(gdiff,
                            iso,
                            ltc,
                            ebins,
                            event_class,
                            event_types,
                            gdiff_fit=gdiff_fit,
                            iso_fit=iso_fit,
                            spatial_model=spatial_model,
                            spatial_size=spatial_size)

    # Compute Maps
    map_diff_flux = None
    map_diff_npred = None
    map_int_flux = None
    map_int_npred = None

    map_nstep = 500

    if map_type == 'hpx':

        hpx = HPX(nside, True, 'GAL', ebins=ebins)
        map_diff_flux = HpxMap(np.zeros((nbin, hpx.npix)), hpx)
        map_diff_npred = HpxMap(np.zeros((nbin, hpx.npix)), hpx)
        map_skydir = map_diff_flux.hpx.get_sky_dirs()

        for i in range(0, len(map_skydir), map_nstep):
            s = slice(i, i + map_nstep)
            o = scalc.diff_flux_threshold(map_skydir[s], fn, ts_thresh,
                                          min_counts)
            map_diff_flux.data[:, s] = o['flux'].T
            map_diff_npred.data[:, s] = o['npred'].T

        hpx = HPX(nside, True, 'GAL')
        map_int_flux = HpxMap(np.zeros((hpx.npix)), hpx)
        map_int_npred = HpxMap(np.zeros((hpx.npix)), hpx)
        map_skydir = map_int_flux.hpx.get_sky_dirs()

        for i in range(0, len(map_skydir), map_nstep):
            s = slice(i, i + map_nstep)
            o = scalc.int_flux_threshold(map_skydir[s], fn, ts_thresh,
                                         min_counts)
            map_int_flux.data[s] = o['flux']
            map_int_npred.data[s] = o['npred']

    elif map_type == 'wcs':

        wcs_shape = [wcs_npix, wcs_npix]
        wcs_size = wcs_npix * wcs_npix

        map_diff_flux = Map.create(c,
                                   wcs_cdelt,
                                   wcs_shape,
                                   'GAL',
                                   wcs_proj,
                                   ebins=ebins)
        map_diff_npred = Map.create(c,
                                    wcs_cdelt,
                                    wcs_shape,
                                    'GAL',
                                    wcs_proj,
                                    ebins=ebins)
        map_skydir = map_diff_flux.get_pixel_skydirs()

        for i in range(0, len(map_skydir), map_nstep):
            idx = np.unravel_index(np.arange(i, min(i + map_nstep, wcs_size)),
                                   wcs_shape)
            s = (slice(None), idx[1], idx[0])
            o = scalc.diff_flux_threshold(map_skydir[slice(i, i + map_nstep)],
                                          fn, ts_thresh, min_counts)
            map_diff_flux.data[s] = o['flux'].T
            map_diff_npred.data[s] = o['npred'].T

        map_int_flux = Map.create(c, wcs_cdelt, wcs_shape, 'GAL', wcs_proj)
        map_int_npred = Map.create(c, wcs_cdelt, wcs_shape, 'GAL', wcs_proj)
        map_skydir = map_int_flux.get_pixel_skydirs()

        for i in range(0, len(map_skydir), map_nstep):
            idx = np.unravel_index(np.arange(i, min(i + map_nstep, wcs_size)),
                                   wcs_shape)
            s = (idx[1], idx[0])
            o = scalc.int_flux_threshold(map_skydir[slice(i, i + map_nstep)],
                                         fn, ts_thresh, min_counts)
            map_int_flux.data[s] = o['flux']
            map_int_npred.data[s] = o['npred']

    o = scalc.diff_flux_threshold(c, fn, ts_thresh, min_counts)

    cols = [
        Column(name='e_min', dtype='f8', data=scalc.ebins[:-1], unit='MeV'),
        Column(name='e_ref', dtype='f8', data=o['e_ref'], unit='MeV'),
        Column(name='e_max', dtype='f8', data=scalc.ebins[1:], unit='MeV'),
        Column(name='flux', dtype='f8', data=o['flux'], unit='ph / (cm2 s)'),
        Column(name='eflux', dtype='f8', data=o['eflux'],
               unit='MeV / (cm2 s)'),
        Column(name='dnde',
               dtype='f8',
               data=o['dnde'],
               unit='ph / (MeV cm2 s)'),
        Column(name='e2dnde',
               dtype='f8',
               data=o['e2dnde'],
               unit='MeV / (cm2 s)'),
        Column(name='npred', dtype='f8', data=o['npred'], unit='ph')
    ]

    tab_diff = Table(cols)

    cols = [
        Column(name='index', dtype='f8'),
        Column(name='e_min', dtype='f8', unit='MeV'),
        Column(name='e_ref', dtype='f8', unit='MeV'),
        Column(name='e_max', dtype='f8', unit='MeV'),
        Column(name='flux', dtype='f8', unit='ph / (cm2 s)'),
        Column(name='eflux', dtype='f8', unit='MeV / (cm2 s)'),
        Column(name='dnde', dtype='f8', unit='ph / (MeV cm2 s)'),
        Column(name='e2dnde', dtype='f8', unit='MeV / (cm2 s)'),
        Column(name='npred', dtype='f8', unit='ph'),
        Column(name='ebin_e_min', dtype='f8', unit='MeV', shape=(len(ectr), )),
        Column(name='ebin_e_ref', dtype='f8', unit='MeV', shape=(len(ectr), )),
        Column(name='ebin_e_max', dtype='f8', unit='MeV', shape=(len(ectr), )),
        Column(name='ebin_flux',
               dtype='f8',
               unit='ph / (cm2 s)',
               shape=(len(ectr), )),
        Column(name='ebin_eflux',
               dtype='f8',
               unit='MeV / (cm2 s)',
               shape=(len(ectr), )),
        Column(name='ebin_dnde',
               dtype='f8',
               unit='ph / (MeV cm2 s)',
               shape=(len(ectr), )),
        Column(name='ebin_e2dnde',
               dtype='f8',
               unit='MeV / (cm2 s)',
               shape=(len(ectr), )),
        Column(name='ebin_npred', dtype='f8', unit='ph', shape=(len(ectr), ))
    ]

    cols_ebounds = [
        Column(name='E_MIN', dtype='f8', unit='MeV', data=ebins[:-1]),
        Column(name='E_MAX', dtype='f8', unit='MeV', data=ebins[1:]),
    ]

    tab_int = Table(cols)
    tab_ebounds = Table(cols_ebounds)

    index = np.linspace(1.0, 5.0, 4 * 4 + 1)

    for g in index:
        fn = spectrum.PowerLaw([1E-13, -g], scale=10**3.5)
        o = scalc.int_flux_threshold(c, fn, ts_thresh, 3.0)
        row = [g]
        for colname in tab_int.columns:
            if colname == 'index':
                continue
            if 'ebin' in colname:
                row += [o['bins'][colname.replace('ebin_', '')]]
            else:
                row += [o[colname]]

        tab_int.add_row(row)

    hdulist = fits.HDUList()
    hdulist.append(fits.table_to_hdu(tab_diff))
    hdulist.append(fits.table_to_hdu(tab_int))
    hdulist.append(fits.table_to_hdu(tab_ebounds))

    hdulist[1].name = 'DIFF_FLUX'
    hdulist[2].name = 'INT_FLUX'
    hdulist[3].name = 'EBOUNDS'

    if map_type is not None:
        hdu = map_diff_flux.create_image_hdu()
        hdu.name = 'MAP_DIFF_FLUX'
        hdulist.append(hdu)
        hdu = map_diff_npred.create_image_hdu()
        hdu.name = 'MAP_DIFF_NPRED'
        hdulist.append(hdu)

        hdu = map_int_flux.create_image_hdu()
        hdu.name = 'MAP_INT_FLUX'
        hdulist.append(hdu)
        hdu = map_int_npred.create_image_hdu()
        hdu.name = 'MAP_INT_NPRED'
        hdulist.append(hdu)

    hdulist.writeto(output, clobber=True)
def run_flux_sensitivity(**kwargs):

    index = kwargs.get('index', 2.0)
    emin = kwargs.get('emin', 10**1.5)
    emax = kwargs.get('emax', 10**6.0)
    nbin = kwargs.get('nbin', 18)
    glon = kwargs.get('glon', 0.0)
    glat = kwargs.get('glat', 0.0)
    ltcube_filepath = kwargs.get('ltcube', None)
    galdiff_filepath = kwargs.get('galdiff', None)
    isodiff_filepath = kwargs.get('isodiff', None)
    obs_time_yr = kwargs.get('obs_time_yr', None)
    event_class = kwargs.get('event_class', 'P8R2_SOURCE_V6')
    min_counts = kwargs.get('min_counts', 3.0)
    ts_thresh = kwargs.get('ts_thresh', 25.0)
    output = kwargs.get('output', None)

    event_types = [['FRONT', 'BACK']]
    fn = spectrum.PowerLaw([1E-13, -index], scale=1E3)

    log_ebins = np.linspace(np.log10(emin),
                            np.log10(emax), nbin + 1)
    ebins = 10**log_ebins
    ectr = np.exp(utils.edge_to_center(np.log(ebins)))

    c = SkyCoord(glon, glat, unit='deg', frame='galactic')

    if ltcube_filepath is None:

        if obs_time_yr is None:
            raise Exception('No observation time defined.')

        ltc = LTCube.create_from_obs_time(obs_time_yr * 365 * 24 * 3600.)
    else:
        ltc = LTCube.create(ltcube_filepath)
        if obs_time_yr is not None:
            ltc._counts *= obs_time_yr * 365 * \
                24 * 3600. / (ltc.tstop - ltc.tstart)

    gdiff = skymap.Map.create_from_fits(galdiff_filepath)

    if isodiff_filepath is None:
        isodiff = utils.resolve_file_path('iso_%s_v06.txt' % event_class,
                                          search_dirs=[os.path.join('$FERMIPY_ROOT', 'data'),
                                                       '$FERMI_DIFFUSE_DIR'])
        isodiff = os.path.expandvars(isodiff)
    else:
        isodiff = isodiff_filepath

    iso = np.loadtxt(isodiff, unpack=True)

    scalc = SensitivityCalc(gdiff, iso, ltc, ebins,
                            event_class, event_types)

    o = scalc.diff_flux_threshold(c, fn, ts_thresh, min_counts)

    cols = [Column(name='e_min', dtype='f8', data=scalc.ebins[:-1], unit='MeV'),
            Column(name='e_ref', dtype='f8', data=o['e_ref'], unit='MeV'),
            Column(name='e_max', dtype='f8', data=scalc.ebins[1:], unit='MeV'),
            Column(name='flux', dtype='f8', data=o[
                   'flux'], unit='ph / (cm2 s)'),
            Column(name='eflux', dtype='f8', data=o[
                   'eflux'], unit='MeV / (cm2 s)'),
            Column(name='dnde', dtype='f8', data=o['dnde'],
                   unit='ph / (MeV cm2 s)'),
            Column(name='e2dnde', dtype='f8',
                   data=o['e2dnde'], unit='MeV / (cm2 s)'),
            Column(name='npred', dtype='f8', data=o['npred'], unit='ph')]

    tab_diff = Table(cols)

    cols = [Column(name='index', dtype='f8'),
            Column(name='e_min', dtype='f8', unit='MeV'),
            Column(name='e_ref', dtype='f8', unit='MeV'),
            Column(name='e_max', dtype='f8', unit='MeV'),
            Column(name='flux', dtype='f8', unit='ph / (cm2 s)'),
            Column(name='eflux', dtype='f8', unit='MeV / (cm2 s)'),
            Column(name='dnde', dtype='f8', unit='ph / (MeV cm2 s)'),
            Column(name='e2dnde', dtype='f8', unit='MeV / (cm2 s)'),
            Column(name='npred', dtype='f8', unit='ph')]

    cols_ebin = [Column(name='index', dtype='f8'),
                 Column(name='e_min', dtype='f8',
                        unit='MeV', shape=(len(ectr),)),
                 Column(name='e_ref', dtype='f8',
                        unit='MeV', shape=(len(ectr),)),
                 Column(name='e_max', dtype='f8',
                        unit='MeV', shape=(len(ectr),)),
                 Column(name='flux', dtype='f8',
                        unit='ph / (cm2 s)', shape=(len(ectr),)),
                 Column(name='eflux', dtype='f8',
                        unit='MeV / (cm2 s)', shape=(len(ectr),)),
                 Column(name='dnde', dtype='f8',
                        unit='ph / (MeV cm2 s)', shape=(len(ectr),)),
                 Column(name='e2dnde', dtype='f8',
                        unit='MeV / (cm2 s)', shape=(len(ectr),)),
                 Column(name='npred', dtype='f8', unit='ph', shape=(len(ectr),))]

    tab_int = Table(cols)
    tab_int_ebin = Table(cols_ebin)

    index = np.linspace(1.0, 5.0, 4 * 4 + 1)

    for g in index:
        fn = spectrum.PowerLaw([1E-13, -g], scale=10**3.5)
        o = scalc.int_flux_threshold(c, fn, ts_thresh, 3.0)
        row = [g]
        for colname in tab_int.columns:
            if not colname in o:
                continue
            row += [o[colname]]

        tab_int.add_row(row)

        row = [g]
        for colname in tab_int.columns:
            if not colname in o:
                continue
            row += [o['bins'][colname]]
        tab_int_ebin.add_row(row)

    hdulist = fits.HDUList()
    hdulist.append(fits.table_to_hdu(tab_diff))
    hdulist.append(fits.table_to_hdu(tab_int))
    hdulist.append(fits.table_to_hdu(tab_int_ebin))

    hdulist[1].name = 'DIFF_FLUX'
    hdulist[2].name = 'INT_FLUX'
    hdulist[3].name = 'INT_FLUX_EBIN'

    hdulist.writeto(output, clobber=True)
Exemple #12
0
    def setup(self,
              phibins=0,
              force_create_srcmap=False,
              overwrite=False,
              **kwargs):
        """
	Same as GTBinnedAnalysis.setup function, which does not create 
	srcmap, running the setup for each component

	{options} 

	overwrite : bool

	Run all pre-processing steps even if the output file of
	that step is present in the working directory.

	phibins : int
	number of phibins for ltcube (default: 0; averaged ltcube)
	"""

        for c in self.components:
            loglevel = kwargs.get('loglevel', c.loglevel)

            c.logger.log(loglevel, 'Running setup for component %s', c.name)
            #use_external_srcmap = c.config['gtlike']['use_external_srcmap']

            # Run data selection
            #if not use_external_srcmap:
            #c._select_data(overwrite=overwrite, **kwargs)
            # Create LT Cube
            if c._ext_ltcube is not None:
                c.logger.log(loglevel, 'Using external LT cube.')
            else:
                c._create_ltcube(phibins=phibins,
                                 overwrite=overwrite,
                                 **kwargs)

            c.logger.debug('Loading LT Cube %s', c.files['ltcube'])
            c._ltc = LTCube.create(c.files['ltcube'])

            # Extract tmin, tmax from LT cube
            c._tmin = c._ltc.tstart
            c._tmax = c._ltc.tstop
            c.logger.debug('Creating PSF model')
            c._psf = irfs.PSFModel.create(c.roi.skydir, c._ltc,
                                          c.config['gtlike']['irfs'],
                                          c.config['selection']['evtype'],
                                          c.energies)

            # we don't need this for unbinned analysis
            # Bin data and create exposure cube
            #if not use_external_srcmap:
            #c._bin_data(overwrite=overwrite, **kwargs)
            #c._create_expcube(overwrite=overwrite, **kwargs)
            #c._bexp = Map.create_from_fits(c.files['bexpmap'])

            # compute exposure
            c._create_expmap(overwrite=overwrite, **kwargs)

            # Make spatial maps for extended sources
            for s in c.roi.sources:
                if s.diffuse:
                    continue
                if not s.extended:
                    continue
                c.make_template(s, c.config['file_suffix'])
            # Write ROI XML
            c.roi.write_xml(c.files['srcmdl'])

            # compute diffuse response
            c._diffrsp(c.files['srcmdl'], overwrite=overwrite, **kwargs)

            # we don't need this for unbinned
            # Create source maps file
            #if force_create_srcmap:
            #	if not use_external_srcmap:
            #	    c._create_srcmaps(overwrite=overwrite)
            if not c.config['data']['cacheft1'] and os.path.isfile(
                    c.files['ft1']):
                c.logger.debug('Deleting FT1 file.')
                os.remove(c.files['ft1'])

            c.logger.log(loglevel, 'Finished setup for component %s', c.name)
        return
def run_flux_sensitivity(**kwargs):

    index = kwargs.get('index', 2.0)
    sedshape = kwargs.get('sedshape', 'PowerLaw')
    cutoff = kwargs.get('cutoff', 1e3)
    curvindex = kwargs.get('curvindex', 1.0)
    beta = kwargs.get('beta', 0.0)
    dmmass = kwargs.get('DMmass', 100.0)
    dmchannel = kwargs.get('DMchannel', 'bb')
    emin = kwargs.get('emin', 10**1.5)
    emax = kwargs.get('emax', 10**6.0)
    nbin = kwargs.get('nbin', 18)
    glon = kwargs.get('glon', 0.0)
    glat = kwargs.get('glat', 0.0)
    ltcube_filepath = kwargs.get('ltcube', None)
    galdiff_filepath = kwargs.get('galdiff', None)
    isodiff_filepath = kwargs.get('isodiff', None)
    galdiff_fit_filepath = kwargs.get('galdiff_fit', None)
    isodiff_fit_filepath = kwargs.get('isodiff_fit', None)
    wcs_npix = kwargs.get('wcs_npix', 40)
    wcs_cdelt = kwargs.get('wcs_cdelt', 0.5)
    wcs_proj = kwargs.get('wcs_proj', 'AIT')
    map_type = kwargs.get('map_type', None)
    spatial_model = kwargs.get('spatial_model', 'PointSource')
    spatial_size = kwargs.get('spatial_size', 1E-2)

    obs_time_yr = kwargs.get('obs_time_yr', None)
    event_class = kwargs.get('event_class', 'P8R2_SOURCE_V6')
    min_counts = kwargs.get('min_counts', 3.0)
    ts_thresh = kwargs.get('ts_thresh', 25.0)
    nside = kwargs.get('hpx_nside', 16)
    output = kwargs.get('output', None)

    event_types = [['FRONT', 'BACK']]

    if sedshape == 'PowerLaw':
        fn = spectrum.PowerLaw([1E-13, -index], scale=1E3)
    elif sedshape == 'PLSuperExpCutoff':
        fn = spectrum.PLSuperExpCutoff(
            [1E-13, -index, cutoff, curvindex], scale=1E3)
    elif sedshape == 'LogParabola':
        fn = spectrum.LogParabola([1E-13, -index, beta], scale=1E3)
    elif sedshape == 'DM':
        fn = spectrum.DMFitFunction([1E-26, dmmass], chan=dmchannel)

    log_ebins = np.linspace(np.log10(emin),
                            np.log10(emax), nbin + 1)
    ebins = 10**log_ebins
    ectr = np.exp(utils.edge_to_center(np.log(ebins)))

    c = SkyCoord(glon, glat, unit='deg', frame='galactic')

    if ltcube_filepath is None:

        if obs_time_yr is None:
            raise Exception('No observation time defined.')

        ltc = LTCube.create_from_obs_time(obs_time_yr * 365 * 24 * 3600.)
    else:
        ltc = LTCube.create(ltcube_filepath)
        if obs_time_yr is not None:
            ltc._counts *= obs_time_yr * 365 * \
                24 * 3600. / (ltc.tstop - ltc.tstart)

    gdiff = skymap.Map.create_from_fits(galdiff_filepath)
    gdiff_fit = None
    if galdiff_fit_filepath is not None:
        gdiff_fit = skymap.Map.create_from_fits(galdiff_fit_filepath)

    if isodiff_filepath is None:
        isodiff = utils.resolve_file_path('iso_%s_v06.txt' % event_class,
                                          search_dirs=[os.path.join('$FERMIPY_ROOT', 'data'),
                                                       '$FERMI_DIFFUSE_DIR'])
        isodiff = os.path.expandvars(isodiff)
    else:
        isodiff = isodiff_filepath

    iso = np.loadtxt(isodiff, unpack=True)
    iso_fit = None
    if isodiff_fit_filepath is not None:
        iso_fit = np.loadtxt(isodiff_fit_filepath, unpack=True)

    scalc = SensitivityCalc(gdiff, iso, ltc, ebins,
                            event_class, event_types, gdiff_fit=gdiff_fit,
                            iso_fit=iso_fit, spatial_model=spatial_model,
                            spatial_size=spatial_size)

    # Compute Maps
    map_diff_flux = None
    map_diff_npred = None
    map_int_flux = None
    map_int_npred = None

    map_nstep = 500

    if map_type == 'hpx':

        hpx = HPX(nside, True, 'GAL', ebins=ebins)
        map_diff_flux = HpxMap(np.zeros((nbin, hpx.npix)), hpx)
        map_diff_npred = HpxMap(np.zeros((nbin, hpx.npix)), hpx)
        map_skydir = map_diff_flux.hpx.get_sky_dirs()

        for i in range(0, len(map_skydir), map_nstep):
            s = slice(i, i + map_nstep)
            o = scalc.diff_flux_threshold(
                map_skydir[s], fn, ts_thresh, min_counts)
            map_diff_flux.data[:, s] = o['flux'].T
            map_diff_npred.data[:, s] = o['npred'].T

        hpx = HPX(nside, True, 'GAL')
        map_int_flux = HpxMap(np.zeros((hpx.npix)), hpx)
        map_int_npred = HpxMap(np.zeros((hpx.npix)), hpx)
        map_skydir = map_int_flux.hpx.get_sky_dirs()

        for i in range(0, len(map_skydir), map_nstep):
            s = slice(i, i + map_nstep)
            o = scalc.int_flux_threshold(
                map_skydir[s], fn, ts_thresh, min_counts)
            map_int_flux.data[s] = o['flux']
            map_int_npred.data[s] = o['npred']

    elif map_type == 'wcs':

        wcs_shape = [wcs_npix, wcs_npix]
        wcs_size = wcs_npix * wcs_npix

        map_diff_flux = Map.create(
            c, wcs_cdelt, wcs_shape, 'GAL', wcs_proj, ebins=ebins)
        map_diff_npred = Map.create(
            c, wcs_cdelt, wcs_shape, 'GAL', wcs_proj, ebins=ebins)
        map_skydir = map_diff_flux.get_pixel_skydirs()

        for i in range(0, len(map_skydir), map_nstep):
            idx = np.unravel_index(
                np.arange(i, min(i + map_nstep, wcs_size)), wcs_shape)
            s = (slice(None), idx[1], idx[0])
            o = scalc.diff_flux_threshold(
                map_skydir[slice(i, i + map_nstep)], fn, ts_thresh, min_counts)
            map_diff_flux.data[s] = o['flux'].T
            map_diff_npred.data[s] = o['npred'].T

        map_int_flux = Map.create(c, wcs_cdelt, wcs_shape, 'GAL', wcs_proj)
        map_int_npred = Map.create(c, wcs_cdelt, wcs_shape, 'GAL', wcs_proj)
        map_skydir = map_int_flux.get_pixel_skydirs()

        for i in range(0, len(map_skydir), map_nstep):
            idx = np.unravel_index(
                np.arange(i, min(i + map_nstep, wcs_size)), wcs_shape)
            s = (idx[1], idx[0])
            o = scalc.int_flux_threshold(
                map_skydir[slice(i, i + map_nstep)], fn, ts_thresh, min_counts)
            map_int_flux.data[s] = o['flux']
            map_int_npred.data[s] = o['npred']

    o = scalc.diff_flux_threshold(c, fn, ts_thresh, min_counts)

    cols = [Column(name='e_min', dtype='f8', data=scalc.ebins[:-1], unit='MeV'),
            Column(name='e_ref', dtype='f8', data=o['e_ref'], unit='MeV'),
            Column(name='e_max', dtype='f8', data=scalc.ebins[1:], unit='MeV'),
            Column(name='flux', dtype='f8', data=o[
                   'flux'], unit='ph / (cm2 s)'),
            Column(name='eflux', dtype='f8', data=o[
                   'eflux'], unit='MeV / (cm2 s)'),
            Column(name='dnde', dtype='f8', data=o['dnde'],
                   unit='ph / (MeV cm2 s)'),
            Column(name='e2dnde', dtype='f8',
                   data=o['e2dnde'], unit='MeV / (cm2 s)'),
            Column(name='npred', dtype='f8', data=o['npred'], unit='ph')]

    tab_diff = Table(cols)

    cols = [Column(name='index', dtype='f8'),
            Column(name='e_min', dtype='f8', unit='MeV'),
            Column(name='e_ref', dtype='f8', unit='MeV'),
            Column(name='e_max', dtype='f8', unit='MeV'),
            Column(name='flux', dtype='f8', unit='ph / (cm2 s)'),
            Column(name='eflux', dtype='f8', unit='MeV / (cm2 s)'),
            Column(name='dnde', dtype='f8', unit='ph / (MeV cm2 s)'),
            Column(name='e2dnde', dtype='f8', unit='MeV / (cm2 s)'),
            Column(name='npred', dtype='f8', unit='ph'),
            Column(name='ebin_e_min', dtype='f8',
                   unit='MeV', shape=(len(ectr),)),
            Column(name='ebin_e_ref', dtype='f8',
                   unit='MeV', shape=(len(ectr),)),
            Column(name='ebin_e_max', dtype='f8',
                        unit='MeV', shape=(len(ectr),)),
            Column(name='ebin_flux', dtype='f8',
                   unit='ph / (cm2 s)', shape=(len(ectr),)),
            Column(name='ebin_eflux', dtype='f8',
                   unit='MeV / (cm2 s)', shape=(len(ectr),)),
            Column(name='ebin_dnde', dtype='f8',
                   unit='ph / (MeV cm2 s)', shape=(len(ectr),)),
            Column(name='ebin_e2dnde', dtype='f8',
                   unit='MeV / (cm2 s)', shape=(len(ectr),)),
            Column(name='ebin_npred', dtype='f8', unit='ph', shape=(len(ectr),))]

    cols_ebounds = [Column(name='E_MIN', dtype='f8',
                           unit='MeV', data=ebins[:-1]),
                    Column(name='E_MAX', dtype='f8',
                           unit='MeV', data=ebins[1:]), ]

    tab_int = Table(cols)
    tab_ebounds = Table(cols_ebounds)

    index = np.linspace(1.0, 5.0, 4 * 4 + 1)

    for g in index:
        fn = spectrum.PowerLaw([1E-13, -g], scale=10**3.5)
        o = scalc.int_flux_threshold(c, fn, ts_thresh, 3.0)
        row = [g]
        for colname in tab_int.columns:
            if colname == 'index':
                continue
            if 'ebin' in colname:
                row += [o['bins'][colname.replace('ebin_', '')]]
            else:
                row += [o[colname]]

        tab_int.add_row(row)

    hdulist = fits.HDUList()
    hdulist.append(fits.table_to_hdu(tab_diff))
    hdulist.append(fits.table_to_hdu(tab_int))
    hdulist.append(fits.table_to_hdu(tab_ebounds))

    hdulist[1].name = 'DIFF_FLUX'
    hdulist[2].name = 'INT_FLUX'
    hdulist[3].name = 'EBOUNDS'

    if map_type is not None:
        hdu = map_diff_flux.create_image_hdu()
        hdu.name = 'MAP_DIFF_FLUX'
        hdulist.append(hdu)
        hdu = map_diff_npred.create_image_hdu()
        hdu.name = 'MAP_DIFF_NPRED'
        hdulist.append(hdu)

        hdu = map_int_flux.create_image_hdu()
        hdu.name = 'MAP_INT_FLUX'
        hdulist.append(hdu)
        hdu = map_int_npred.create_image_hdu()
        hdu.name = 'MAP_INT_NPRED'
        hdulist.append(hdu)

    hdulist.writeto(output, overwrite=True)