예제 #1
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
예제 #2
0
파일: ltcube.py 프로젝트: sarabuson/fermipy
    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)
예제 #3
0
파일: skymap.py 프로젝트: sarabuson/fermipy
    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)
예제 #4
0
파일: irfs.py 프로젝트: 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)
예제 #5
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)
예제 #6
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)
예제 #7
0
파일: skymap.py 프로젝트: NAH8/fermipy
    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)
예제 #8
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)
예제 #9
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)
예제 #10
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)