Example #1
0
    def create_from_gti(cls, skydir, tab_sc, tab_gti, zmax, **kwargs):

        radius = kwargs.get('radius', 180.0)
        cth_edges = kwargs.get('cth_edges', None)
        if cth_edges is None:
            cth_edges = 1.0 - np.linspace(0, 1.0, 41)**2
            cth_edges = cth_edges[::-1]

        hpx = HPX(2**4, True, 'CEL', ebins=cth_edges)

        hpx_skydir = hpx.get_sky_dirs()

        m = skydir.separation(hpx_skydir).deg < radius
        map_lt = HpxMap(np.zeros((40, hpx.npix)), hpx)
        map_lt_wt = HpxMap(np.zeros((40, hpx.npix)), hpx)

        lt, lt_wt = fill_livetime_hist(
            hpx_skydir[m], tab_sc, tab_gti, zmax, cth_edges)
        map_lt.data[:, m] = lt
        map_lt_wt.data[:, m] = lt_wt

        hpx2 = HPX(2**6, True, 'CEL', ebins=cth_edges)

        ltc = cls(np.zeros((len(cth_edges) - 1, hpx2.npix)), hpx2, cth_edges)
        ltc_skydir = ltc.hpx.get_sky_dirs()
        m = skydir.separation(ltc_skydir).deg < radius

        ltc.data[:, m] = map_lt.interpolate(ltc_skydir[m].ra.deg,
                                            ltc_skydir[m].dec.deg,
                                            interp_log=False)
        ltc.data_wt[:, m] = map_lt_wt.interpolate(ltc_skydir[m].ra.deg,
                                                  ltc_skydir[m].dec.deg,
                                                  interp_log=False)
        return ltc
Example #2
0
    def create_from_gti(cls, skydir, tab_sc, tab_gti, zmax, **kwargs):

        radius = kwargs.get('radius', 180.0)
        cth_edges = kwargs.get('cth_edges', None)
        if cth_edges is None:
            cth_edges = 1.0 - np.linspace(0, 1.0, 41)**2
            cth_edges = cth_edges[::-1]

        hpx = HPX(2**4, True, 'CEL', ebins=cth_edges)

        hpx_skydir = hpx.get_sky_dirs()

        m = skydir.separation(hpx_skydir).deg < radius
        map_lt = HpxMap(np.zeros((40, hpx.npix)), hpx)
        map_lt_wt = HpxMap(np.zeros((40, hpx.npix)), hpx)

        lt, lt_wt = fill_livetime_hist(
            hpx_skydir[m], tab_sc, tab_gti, zmax, cth_edges)
        map_lt.data[:, m] = lt
        map_lt_wt.data[:, m] = lt_wt

        hpx2 = HPX(2**6, True, 'CEL', ebins=cth_edges)

        ltc = cls(np.zeros((len(cth_edges) - 1, hpx2.npix)), hpx2, cth_edges)
        ltc_skydir = ltc.hpx.get_sky_dirs()
        m = skydir.separation(ltc_skydir).deg < radius

        ltc.data[:, m] = map_lt.interpolate(ltc_skydir[m].ra.deg,
                                            ltc_skydir[m].dec.deg,
                                            interp_log=False)
        ltc.data_wt[:, m] = map_lt_wt.interpolate(ltc_skydir[m].ra.deg,
                                                  ltc_skydir[m].dec.deg,
                                                  interp_log=False)
        return ltc
Example #3
0
def test_hpx():

    hpx0 = HPX(2**3, False, 'GAL', region='DISK(110.,75.,10.)')
    assert_allclose(hpx0[hpx0._ipix], np.arange(len(hpx0._ipix)))

    ebins = np.logspace(2, 5, 8)
    hpx1 = HPX(2**3, False, 'GAL', region='DISK(110.,75.,10.)', ebins=ebins)
    assert_allclose(hpx1[hpx1._ipix], np.arange(len(hpx1._ipix)))
Example #4
0
def read_projection_from_fits(fitsfile, extname=None):
    """
    Load a WCS or HPX projection.
    """
    f = fits.open(fitsfile)
    nhdu = len(f)
    # Try and get the energy bounds
    try:
        ebins = find_and_read_ebins(f)
    except:
        ebins = None

    if extname is None:
        # If there is an image in the Primary HDU we can return a WCS-based
        # projection
        if f[0].header['NAXIS'] != 0:
            proj = WCS(f[0].header)
            return proj, f, f[0]
    else:
        if f[extname].header['XTENSION'] == 'IMAGE':
            proj = WCS(f[extname].header)
            return proj, f, f[extname]
        elif extname in ['SKYMAP', 'SKYMAP2']:
            proj = HPX.create_from_hdu(f[extname], ebins)
            return proj, f, f[extname]
        elif f[extname].header['XTENSION'] == 'BINTABLE':
            try:
                if f[extname].header['PIXTYPE'] == 'HEALPIX':
                    proj = HPX.create_from_hdu(f[extname], ebins)
                    return proj, f, f[extname]
            except:
                pass
        return None, f, None

    # Loop on HDU and look for either an image or a table with HEALPix data
    for i in range(1, nhdu):
        # if there is an image we can return a WCS-based projection
        if f[i].header['XTENSION'] == 'IMAGE':
            proj = WCS(f[i].header)
            return proj, f, f[i]
        elif f[i].header['XTENSION'] == 'BINTABLE':
            if f[i].name in ['SKYMAP', 'SKYMAP2']:
                proj = HPX.create_from_hdu(f[i], ebins)
                return proj, f, f[i]
            try:
                if f[i].header['PIXTYPE'] == 'HEALPIX':
                    proj = HPX.create_from_hdu(f[i], ebins)
                    return proj, f, f[i]
            except:
                pass

    return None, f, None
Example #5
0
def read_projection_from_fits(fitsfile, extname=None):
    """
    Load a WCS or HPX projection.
    """
    f = fits.open(fitsfile)
    nhdu = len(f)
    # Try and get the energy bounds
    try:
        ebins = find_and_read_ebins(f)
    except:
        ebins = None

    if extname is None:
        # If there is an image in the Primary HDU we can return a WCS-based
        # projection
        if f[0].header['NAXIS'] != 0:
            proj = WCS(f[0].header)
            return proj, f, f[0]
    else:
        if f[extname].header['XTENSION'] == 'IMAGE':
            proj = WCS(f[extname].header)
            return proj, f, f[extname]
        elif extname in ['SKYMAP', 'SKYMAP2']:
            proj = HPX.create_from_header(f[extname].header, ebins)
            return proj, f, f[extname]
        elif f[extname].header['XTENSION'] == 'BINTABLE':
            try:
                if f[extname].header['PIXTYPE'] == 'HEALPIX':
                    proj = HPX.create_from_header(f[extname].header, ebins)
                    return proj, f, f[extname]
            except:
                pass
        return None, f, None

    # Loop on HDU and look for either an image or a table with HEALPix data
    for i in range(1, nhdu):
        # if there is an image we can return a WCS-based projection
        if f[i].header['XTENSION'] == 'IMAGE':
            proj = WCS(f[i].header)
            return proj, f, f[i]
        elif f[i].header['XTENSION'] == 'BINTABLE':
            if f[i].name in ['SKYMAP', 'SKYMAP2']:
                proj = HPX.create_from_header(f[i].header, ebins)
                return proj, f, f[i]
            try:
                if f[i].header['PIXTYPE'] == 'HEALPIX':
                    proj = HPX.create_from_header(f[i].header, ebins)
                    return proj, f, f[i]
            except:
                pass

    return None, f, None
Example #6
0
    def create_from_hdu(cls, hdu, ebins):
        """ Creates and returns an HpxMap object from a FITS HDU.

        hdu    : The FITS
        ebins  : Energy bin edges [optional]
        """
        hpx = HPX.create_from_hdu(hdu, ebins)
        colnames = hdu.columns.names
        cnames = []
        if hpx.conv.convname == 'FGST_SRCMAP_SPARSE':
            pixs = hdu.data.field('PIX')
            chans = hdu.data.field('CHANNEL')
            keys = chans * hpx.npix + pixs
            vals = hdu.data.field('VALUE')
            nebin = len(ebins)
            data = np.zeros((nebin, hpx.npix))
            data.flat[keys] = vals
        else:
            for c in colnames:
                if c.find(hpx.conv.colstring) == 0:
                    cnames.append(c)
            nebin = len(cnames)
            data = np.ndarray((nebin, hpx.npix))
            for i, cname in enumerate(cnames):
                data[i, 0:] = hdu.data.field(cname)

        return cls(data, hpx)
Example #7
0
    def create_from_fits(cls, ltfile):

        hdulist = fits.open(ltfile)
        data = hdulist['EXPOSURE'].data.field('COSBINS')
        data_wt = hdulist['WEIGHTED_EXPOSURE'].data.field('COSBINS')
        data = data.astype(float)
        data_wt = data_wt.astype(float)
        tstart = hdulist[0].header['TSTART']
        tstop = hdulist[0].header['TSTOP']
        zmin = hdulist['EXPOSURE'].header['ZENMIN']
        zmax = hdulist['EXPOSURE'].header['ZENMAX']

        cth_min = np.array(hdulist['CTHETABOUNDS'].data.field('CTHETA_MIN'))
        cth_max = np.array(hdulist['CTHETABOUNDS'].data.field('CTHETA_MAX'))
        cth_min = cth_min.astype(float)
        cth_max = cth_max.astype(float)
        cth_edges = np.concatenate((cth_max[:1], cth_min))[::-1]
        hpx = HPX.create_from_header(hdulist['EXPOSURE'].header, cth_edges)
        #header = dict(hdulist['EXPOSURE'].header)
        tab_gti = Table.read(ltfile, 'GTI')
        hdulist.close()

        return cls(data[:, ::-1].T,
                   hpx,
                   cth_edges,
                   tstart=tstart,
                   tstop=tstop,
                   zmin=zmin,
                   zmax=zmax,
                   tab_gti=tab_gti,
                   data_wt=data_wt[:, ::-1].T)
Example #8
0
    def create_from_hdu(cls, hdu, ebins):
        """ Creates and returns an HpxMap object from a FITS HDU.

        hdu    : The FITS
        ebins  : Energy bin edges [optional]
        """
        hpx = HPX.create_from_header(hdu.header, ebins)
        colnames = hdu.columns.names
        cnames = []
        if hpx.conv.convname == 'FGST_SRCMAP_SPARSE':
            keys = hdu.data.field('KEY')
            vals = hdu.data.field('VALUE')
            nebin = len(ebins)
            data = np.zeros((nebin, hpx.npix))
            data.flat[keys] = vals
        else:
            for c in colnames:
                if c.find(hpx.conv.colstring) == 0:
                    cnames.append(c)
            nebin = len(cnames)
            data = np.ndarray((nebin, hpx.npix))
            for i, cname in enumerate(cnames):
                data[i, 0:] = hdu.data.field(cname)

        return cls(data, hpx)
Example #9
0
 def create_empty(cls, tstart, tstop, fill=0.0, nside=64):
     """Create an empty livetime cube."""
     cth_edges = np.linspace(0, 1.0, 41)
     domega = utils.edge_to_width(cth_edges) * 2.0 * np.pi
     hpx = HPX(nside, True, 'CEL', ebins=cth_edges)
     data = np.ones((len(cth_edges) - 1, hpx.npix)) * fill
     return cls(data, hpx, cth_edges, tstart=tstart, tstop=tstop)
Example #10
0
    def create(cls, ltc, event_class, event_types, ebins):
        """Create an exposure map from a livetime cube.  This method will
        generate an exposure map with the same geometry as the
        livetime cube (nside, etc.).

        Parameters
        ----------
        ltc : `~fermipy.irfs.LTCube`
            Livetime cube object.

        event_class : str
            Event class string.

        event_types : list
            List of event type strings, e.g. ['FRONT','BACK'].

        ebins :  `~numpy.ndarray`
            Energy bin edges in MeV.

        """

        evals = np.sqrt(ebins[1:] * ebins[:-1])
        exp = np.zeros((len(evals), ltc.hpx.npix))
        for et in event_types:
            aeff = create_aeff(event_class, et, evals, ltc.costh_center)
            exp += np.sum(aeff.T[:, :, np.newaxis] *
                          ltc.data[:, np.newaxis, :],
                          axis=0)

        hpx = HPX(ltc.hpx.nside, ltc.hpx.nest, ltc.hpx.coordsys, ebins=ebins)
        return cls(exp, hpx)
Example #11
0
def test_hpxmap(tmpdir):
    n = np.ones((10, 192), 'd')
    hpx = HPX(4, False, 'GAL')

    filename = str(tmpdir / 'test_hpx.fits')
    hpx.write_fits(n, filename, clobber=True)

    ebins = np.logspace(2, 5, 8)

    hpx_2 = HPX(1024, False, 'GAL', region='DISK(110.,75.,2.)', ebins=ebins)
    npixels = hpx_2.npix

    n2 = np.ndarray((8, npixels), 'd')
    for i in range(8):
        n2[i].flat = np.arange(npixels)

    hpx_map = HpxMap(n2, hpx_2)
    wcs, wcs_data = hpx_map.make_wcs_from_hpx(normalize=True)

    wcs_out = hpx_2.make_wcs(3)

    filename = str(tmpdir / 'test_hpx_2_wcs.fits')
    write_fits_image(wcs_data, wcs_out.wcs, filename)

    assert_allclose(wcs_data[0, 160, 160], 87.28571429)
    assert_allclose(wcs_data[4, 160, 160], 87.28571429)
Example #12
0
File: irfs.py Project: NAH8/fermipy
    def create_from_file(ltfile):

        hdulist = fits.open(ltfile)
        data = hdulist['EXPOSURE'].data.field(0)
        tstart = hdulist[0].header['TSTART']
        tstop = hdulist[0].header['TSTOP']
        cth_edges = np.array(hdulist['CTHETABOUNDS'].data.field(0))
        cth_edges = np.concatenate(([1], cth_edges))
        cth_edges = cth_edges[::-1]
        hpx = HPX.create_from_header(hdulist['EXPOSURE'].header, cth_edges)
        return LTCube(data[:, ::-1].T, hpx, cth_edges, tstart, tstop)
Example #13
0
def test_hpxmap(tmpdir):
    n = np.ones((10, 192), 'd')
    hpx = HPX(4, False, 'GAL')

    filename = str(tmpdir / 'test_hpx.fits')
    hpx.write_fits(n, filename, clobber=True)

    ebins = np.logspace(2, 5, 8)

    hpx_2 = HPX(1024, False, 'GAL', region='DISK(110.,75.,2.)', ebins=ebins)
    npixels = hpx_2.npix

    n2 = np.ndarray((8, npixels), 'd')
    for i in range(8):
        n2[i].flat = np.arange(npixels)

    hpx_map = HpxMap(n2, hpx_2)
    wcs, wcs_data = hpx_map.make_wcs_from_hpx(normalize=True)

    wcs_out = hpx_2.make_wcs(3)

    filename = str(tmpdir / 'test_hpx_2_wcs.fits')
    write_fits_image(wcs_data, wcs_out.wcs, filename)
Example #14
0
def stack_energy_planes_hpx(filelist, **kwargs):
    """
    """
    from fermipy.skymap import HpxMap
    from fermipy.hpx_utils import HPX
    maplist = [HpxMap.create_from_fits(fname, **kwargs) for fname in filelist]
    energies = np.log10(np.hstack([amap.hpx.evals
                                   for amap in maplist])).squeeze()

    counts = np.hstack([amap.counts.flat for amap in maplist])
    counts = counts.reshape((len(energies), int(len(counts) / len(energies))))

    template_map = maplist[0]
    hpx = HPX.create_from_header(template_map.hpx.make_header(), energies)
    return HpxMap(counts, hpx)
Example #15
0
def stack_energy_planes_hpx(filelist, **kwargs):
    """
    """
    from fermipy.skymap import HpxMap
    from fermipy.hpx_utils import HPX
    maplist = [HpxMap.create_from_fits(fname, **kwargs) for fname in filelist]
    energies = np.log10(
        np.hstack([amap.hpx.evals for amap in maplist])).squeeze()

    counts = np.hstack([amap.counts.flat for amap in maplist])
    counts = counts.reshape((len(energies), int(len(counts) / len(energies))))

    template_map = maplist[0]
    hpx = HPX.create_from_header(template_map.hpx.make_header(), energies)
    return HpxMap(counts, hpx)
Example #16
0
def update_hpx_skymap_allsky(map_in, map_out):
    """ 'Update' a HEALPix skymap

    This checks map_out exists and creates it from map_in if it does not.
    If map_out does exist, this adds the data in map_in to map_out
    """
    if map_out is None:
        in_hpx = map_in.hpx
        out_hpx = HPX.create_hpx(in_hpx.nside, in_hpx.nest, in_hpx.coordsys,
                                 None, in_hpx.ebins, None, in_hpx.conv, None)
        data_out = map_in.expanded_counts_map()
        print(data_out.shape, data_out.sum())
        map_out = HpxMap(data_out, out_hpx)
    else:
        map_out.data += map_in.expanded_counts_map()
    return map_out
Example #17
0
def update_hpx_skymap_allsky(map_in, map_out):
    """ 'Update' a HEALPix skymap

    This checks map_out exists and creates it from map_in if it does not.
    If map_out does exist, this adds the data in map_in to map_out
    """
    if map_out is None:
        in_hpx = map_in.hpx
        out_hpx = HPX.create_hpx(in_hpx.nside, in_hpx.nest, in_hpx.coordsys,
                                 None, in_hpx.ebins, None, in_hpx.conv, None)
        data_out = map_in.expanded_counts_map()
        print(data_out.shape, data_out.sum())
        map_out = HpxMap(data_out, out_hpx)
    else:
        map_out.data += map_in.expanded_counts_map()
    return map_out
Example #18
0
    def create_from_hdu(hdu, ebins):
        """ Creates and returns an HpxMap object from a FITS HDU.

        hdu    : The FITS
        ebins  : Energy bin edges [optional]
        """
        hpx = HPX.create_from_header(hdu.header, ebins)
        colnames = hdu.columns.names
        nebin = 0
        for c in colnames:
            if c.find("CHANNEL") == 0:
                nebin += 1
            pass
        data = np.ndarray((nebin, hpx.npix))
        for i in range(nebin):
            cname = "CHANNEL%i" % (i + 1)
            data[i, 0:] = hdu.data.field(cname)
            pass
        return HpxMap(data, hpx)
Example #19
0
    def create_from_fits(cls, ltfile):

        hdulist = fits.open(ltfile)
        data = hdulist['EXPOSURE'].data.field('COSBINS')
        data_wt = hdulist['WEIGHTED_EXPOSURE'].data.field('COSBINS')
        if hdulist['EXPOSURE'].header['PHIBINS'] > 0:
            data = data[:, :hdulist['EXPOSURE'].header['NBRBINS']]
            # first phibin is the same as phibin = 0 or sum of phibin 1:n
            # data = reshape(data.shape[0],
            # hdulist["EXPOSURE"].header["PHIBINS"]+1,
            # hdulist["EXPOSURE"].header["NBRBINS"])
        if hdulist['WEIGHTED_EXPOSURE'].header['PHIBINS'] > 0:
            data_wt = data[:, :hdulist['WEIGHTED_EXPOSURE'].header['NBRBINS']]
            # first phibin is the same as phibin = 0 or sum of phibin 1:n
            # data_wt = reshape(data_wt.shape[0],
            # hdulist["WEIGHTED_EXPOSURE"].header["PHIBINS"]+1,
            # hdulist["WEIGHTED_EXPOSURE"].header["NBRBINS"])
        data = data.astype(float)
        data_wt = data_wt.astype(float)
        tstart = hdulist[0].header['TSTART']
        tstop = hdulist[0].header['TSTOP']
        zmin = hdulist['EXPOSURE'].header['ZENMIN']
        zmax = hdulist['EXPOSURE'].header['ZENMAX']

        if not tstart:
            tstart = None
        if not tstop:
            tstop = None
        
        cth_min = np.array(hdulist['CTHETABOUNDS'].data.field('CTHETA_MIN'))
        cth_max = np.array(hdulist['CTHETABOUNDS'].data.field('CTHETA_MAX'))
        cth_min = cth_min.astype(float)
        cth_max = cth_max.astype(float)
        cth_edges = np.concatenate((cth_max[:1], cth_min))[::-1]
        hpx = HPX.create_from_header(hdulist['EXPOSURE'].header, cth_edges)
        #header = dict(hdulist['EXPOSURE'].header)
        tab_gti = Table.read(ltfile, 'GTI')
        hdulist.close()

        return cls(data[:, ::-1].T, hpx, cth_edges,
                   tstart=tstart, tstop=tstop,
                   zmin=zmin, zmax=zmax, tab_gti=tab_gti,
                   data_wt=data_wt[:, ::-1].T)
Example #20
0
    def create_from_fits(cls, ltfile):

        hdulist = fits.open(ltfile)
        data = hdulist['EXPOSURE'].data.field('COSBINS')
        data_wt = hdulist['WEIGHTED_EXPOSURE'].data.field('COSBINS')
        if hdulist['EXPOSURE'].header['PHIBINS'] > 0:
            data = data[:, :hdulist['EXPOSURE'].header['NBRBINS']]
            # first phibin is the same as phibin = 0 or sum of phibin 1:n
            # data = reshape(data.shape[0],
            # hdulist["EXPOSURE"].header["PHIBINS"]+1,
            # hdulist["EXPOSURE"].header["NBRBINS"])
        if hdulist['WEIGHTED_EXPOSURE'].header['PHIBINS'] > 0:
            data_wt = data[:, :hdulist['WEIGHTED_EXPOSURE'].header['NBRBINS']]
            # first phibin is the same as phibin = 0 or sum of phibin 1:n
            # data_wt = reshape(data_wt.shape[0],
            # hdulist["WEIGHTED_EXPOSURE"].header["PHIBINS"]+1,
            # hdulist["WEIGHTED_EXPOSURE"].header["NBRBINS"])
        data = data.astype(float)
        data_wt = data_wt.astype(float)
        tstart = hdulist[0].header['TSTART']
        tstop = hdulist[0].header['TSTOP']
        zmin = hdulist['EXPOSURE'].header['ZENMIN']
        zmax = hdulist['EXPOSURE'].header['ZENMAX']

        cth_min = np.array(hdulist['CTHETABOUNDS'].data.field('CTHETA_MIN'))
        cth_max = np.array(hdulist['CTHETABOUNDS'].data.field('CTHETA_MAX'))
        cth_min = cth_min.astype(float)
        cth_max = cth_max.astype(float)
        cth_edges = np.concatenate((cth_max[:1], cth_min))[::-1]
        hpx = HPX.create_from_header(hdulist['EXPOSURE'].header, cth_edges)
        #header = dict(hdulist['EXPOSURE'].header)
        tab_gti = Table.read(ltfile, 'GTI')
        hdulist.close()

        return cls(data[:, ::-1].T, hpx, cth_edges,
                   tstart=tstart, tstop=tstop,
                   zmin=zmin, zmax=zmax, tab_gti=tab_gti,
                   data_wt=data_wt[:, ::-1].T)
Example #21
0
    def create_from_fits(ltfile):

        hdulist = fits.open(ltfile)
        data = hdulist['EXPOSURE'].data.field('COSBINS')
        data_wt = hdulist['WEIGHTED_EXPOSURE'].data.field('COSBINS')
        data = data.astype(float)
        data_wt = data_wt.astype(float)
        tstart = hdulist[0].header['TSTART']
        tstop = hdulist[0].header['TSTOP']
        zmin = hdulist['EXPOSURE'].header['ZENMIN']
        zmax = hdulist['EXPOSURE'].header['ZENMAX']

        cth_min = np.array(hdulist['CTHETABOUNDS'].data.field('CTHETA_MIN'))
        cth_max = np.array(hdulist['CTHETABOUNDS'].data.field('CTHETA_MAX'))
        cth_min = cth_min.astype(float)
        cth_max = cth_max.astype(float)
        cth_edges = np.concatenate((cth_max[:1], cth_min))[::-1]
        hpx = HPX.create_from_header(hdulist['EXPOSURE'].header, cth_edges)
        #header = dict(hdulist['EXPOSURE'].header)
        tab_gti = Table.read(ltfile, 'GTI')
        return LTCube(data[:, ::-1].T, hpx, cth_edges,
                      tstart=tstart, tstop=tstop,
                      zmin=zmin, zmax=zmax, tab_gti=tab_gti,
                      data_wt=data_wt[:, ::-1].T)
Example #22
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)