コード例 #1
0
ファイル: gMasks.py プロジェクト: nmik/GRATools
def mask_src_weighted_custom(cat_file, ENERGY, NSIDE):
    """Returns the 'bad pixels' defined by the position of a source and a 
       certain radius away from that point. The radii increase with the 
       brightness and rescaled by a factor between 1 and 0.3 shaped as the PSF.

       cat_file: str
          .fits file with the sorce catalog
       ENERGY: float
          Mean energy of the map to be masked
       NSIDE: int
          healpix nside parameter
    """
    psf_ref_file = os.path.join(GRATOOLS_CONFIG, 'ascii/PSF_UCV_PSF1.txt')
    src_cat = pf.open(cat_file)
    NPIX = hp.pixelfunc.nside2npix(NSIDE)
    CAT = src_cat[1]
    BAD_PIX_SRC = []
    SOURCES = CAT.data
    src_cat.close()
    psf_ref = get_psf_ref(psf_ref_file)
    psf_en = psf_ref(ENERGY)
    psf_min, psf_max =  psf_ref.y[5], psf_ref.y[-1] 
    norm_min, norm_max = 1, 0.3
    norm = norm_min + psf_en*((norm_max - norm_min)/(psf_max - psf_min)) -\
        psf_min*((norm_max - norm_min)/(psf_max - psf_min))
    logger.info('Normalization of radii due to energy: %.3f'%norm)
    logger.info('Psf(%.2f)= %.2f'%(ENERGY, psf_en))
    FLUX = np.log10(SOURCES.field('eflux1000'))
    flux_min, flux_max = min(FLUX), max(FLUX)
    rad_min, rad_max = 1, 5.
    RADdeg = rad_min + FLUX*((rad_max - rad_min)/(flux_max - flux_min)) -\
        flux_min*((rad_max - rad_min)/(flux_max - flux_min))
    RADrad = np.radians(RADdeg)
    logger.info('Flux-weighted mask for sources activated')
    TS = SOURCES.field('ts') 
    indTS25 = TS > 25.
    GLON = SOURCES.field('GLON')[indTS25]
    GLAT = SOURCES.field('GLAT')[indTS25]
    logger.info('Num Src: %i'%len(TS))
    logger.info('Num Src TS>25: %i'%len(TS[indTS25]))
    for i, src in enumerate(SOURCES[indTS25]):
        x, y, z = hp.rotator.dir2vec(GLON[i],GLAT[i],lonlat=True)
        b_pix= hp.pixelfunc.vec2pix(NSIDE, x, y, z)
        BAD_PIX_SRC.append(b_pix)
        radintpix = hp.query_disc(NSIDE, (x, y, z), RADrad[i]*norm)
        BAD_PIX_SRC.extend(radintpix)
    return BAD_PIX_SRC
コード例 #2
0
ファイル: gMasks.py プロジェクト: mahmoud-lsw/GRATools
def main():
    """Simple test unit
    """
    from GRATools.utils.gWindowFunc import get_psf_ref
    psf_ref_file = os.path.join(GRATOOLS_CONFIG, 'ascii/PSF_UCV_PSF1.txt')
    psf_ref = get_psf_ref(psf_ref_file)
    energy = np.array([
        743.73, 1340.69, 2208.67, 3692.56, 6416.93, 11151.34, 18370.95,
        30713.33, 53373.66, 92752.78, 152802.88, 255462.38, 443942.75
    ])
    psf_en = psf_ref(energy)
    psf_min, psf_max = psf_ref.y[5], psf_ref.y[-1]
    norm_min, norm_max = 1, 0.3
    norm = norm_min + psf_en*((norm_max - norm_min)/(psf_max - psf_min)) -\
        psf_min*((norm_max - norm_min)/(psf_max - psf_min))
    plt.title('Normalization Factor')
    plt.plot(energy, norm, 'ro--', ms=5, alpha=0.75)
    #plt.plot((1e-12, 1e-5), (2, 2), '-', color='silver', linewidth=1.0)
    plt.xlabel('Energy [MeV]')
    plt.ylabel('Normalization factor')
    plt.yscale('log')
    plt.xscale('log')
    plt.ylim(0.09, 2)
    plt.show()

    nside = 512
    SRC_CATALOG_FILE = os.path.join(FT_DATA_FOLDER, 'catalogs/gll_psc_v16.fit')
    bad_pix = mask_src_weighted(SRC_CATALOG_FILE, 10000, nside)
    bad_pix += mask_gp(30, nside)
    npix = hp.nside2npix(nside)
    mask = np.ones(npix)
    for bpix in np.unique(bad_pix):
        mask[bpix] = 0
    fsky = 1 - (len(np.unique(bad_pix)) / float(npix))
    title = 'f$_{sky}$ = %.3f' % fsky
    hp.mollview(mask, title=title, coord='G')
    plt.show()
コード例 #3
0
ファイル: drawsame_Cls.py プロジェクト: mahmoud-lsw/GRATools
rebinning = np.unique(np.int64(np.logspace(0, 3, 31)))
psf_ref_file = os.path.join(GRATOOLS_CONFIG, 'ascii/PSF_UCV_PSF1.txt')
_l_min = [200, 200, 100, 100, 100, 100, 49, 49, 49, 49, 49, 49, 49]
_l_max = [300, 400, 700, 1000, 1000, 1000, 1000, 1000, 1000, 
          1000, 1000, 1000, 1000]

emin, emax, emean = [], [], []
cls_tocompare, clerrs_tocompare = [], []
for f in Cl_FILES:
    emin, emax, emean, cls, clerrs = cl_parse(f)
    cls_tocompare.append(cls)
    clerrs_tocompare.append(clerrs)

from GRATools.utils.gWindowFunc import get_psf_ref
psf_ref = get_psf_ref(psf_ref_file)
ymin, ymax = -1e-15, 1e-15
for i in range(0, len(cls_tocompare[0])):
    psf_en = psf_ref(emean[i])
    l_max = _l_max[i]#min(500, 1.9*(np.pi/np.radians(psf_en)))
    l_min = _l_min[i]#min(60, max(50-i*5,10))
    plt.figure(figsize=(10, 7), dpi=80)
    for j, f in enumerate(Cl_FILES):
        _l = np.arange(1, len(cls_tocompare[j][i]))
        _l_rebin, _cls_rebin,  _clerrs_rebin = [], [], []
        xerrL, xerrR = [], []
        for bmin, bmax in zip(rebinning[:-1], rebinning[1:]):
            _l_rebin.append(np.sqrt(bmin*bmax))
            xerrL.append(abs(np.sqrt(bmin*bmax)-bmin))
            xerrR.append(abs(np.sqrt(bmin*bmax)-bmax))
            _index = np.where(np.logical_and(_l>=bmin, _l<bmax)) 
コード例 #4
0
ファイル: mkcsi.py プロジェクト: nmik/GRATools
def mkCsi(**kwargs):
    """                                      
    """
    get_var_from_file(kwargs['config'])
    ncores = kwargs['ncores']
    psf_file = data.PSF_REF_FILE
    p = multiprocessing.Pool(processes=ncores)
    logger.info('Starting Csi analysis...')
    in_label = data.IN_LABEL
    out_label = data.OUT_LABEL
    binning_label = data.BINNING_LABEL
    cl_param_file = os.path.join(GRATOOLS_OUT, '%s_%s_parameters.txt' \
                                     %(in_label, binning_label))
    from GRATools.utils.gFTools import get_cl_param
    _emin, _emax, _emean, _f, _ferr, _cn, _fsky = get_cl_param(cl_param_file)
    csi_txt = open(os.path.join(GRATOOLS_OUT, '%s_%s_csi.txt' \
                                   %(out_label, binning_label)), 'w')
    psf_ref = get_psf_ref(psf_file)
    #psf_ref.plot(show=False)
    #plt.xscale('log')
    #plt.yscale('log')
    #plt.show()
    for i, (emin, emax) in enumerate(zip(_emin, _emax)):
        logger.info('Considering bin %.2f - %.2f ...'%(emin, emax))
        cont_ang = np.radians(psf_ref(_emean[i]))
        csi_txt.write('ENERGY\t %.2f %.2f %.2f\n'%(emin, emax, _emean[i]))
        flux_map_name = in_label+'_flux_%i-%i.fits'%(emin, emax)
        flux_map = hp.read_map(os.path.join(GRATOOLS_OUT_FLUX, flux_map_name))
        flux_map = udgrade_as_psf(flux_map, cont_ang)
        R = hp.read_map(os.path.join(GRATOOLS_OUT_FLUX, flux_map_name))
        R = udgrade_as_psf(R, cont_ang)
        fsky = 1.-(len(np.where(flux_map == hp.UNSEEN)[0])/\
                       float(len(flux_map)))
        logger.info('fsky = %f'%fsky)
        npix = len(flux_map)
        nside = hp.npix2nside(npix)        
        _unmask = np.where(flux_map != hp.UNSEEN)[0]
        npix_unmask = len(_unmask)
        Imean = _f[i]
        dI = flux_map - Imean
        dR = R - Imean
        R = permute_unmasked_pix(R)
        dR = permute_unmasked_pix(dR)
        th_bins = data.TH_BINNING
        theta = []
        for thmin, thmax in zip(th_bins[:-1], th_bins[1:]):
            th_mean = np.sqrt(thmin*thmax)
            theta.append(th_mean)
        theta = np.array(theta)
        logger.info('Computing Csi...')
        diri = hp.pixelfunc.pix2ang(nside, _unmask)
        veci = hp.rotator.dir2vec(diri)
        xyz = np.array([(veci[0][i], veci[1][i], veci[2][i]) 
                        for i in range(0, len(veci[0]))])
        args = zip(_unmask, xyz, [dI]*npix_unmask, [dR]*npix_unmask,
                   [nside]*npix_unmask)
        #args = zip(_unmask, xyz, [flux_map]*npix_unmask, [R]*npix_unmask, 
        #           [nside]*npix_unmask)
        a = np.array(p.map(csi_compute, args))
        SUMij_list = a[:, 0]   
        SUMf_list = a[:, 1]
        SUMR_list = a[:, 2]
        SUMij_th = []
        SUMf_th = []
        SUMR_th = []
        for i, s in enumerate(SUMij_list[0]):
            SUMij_th.append(np.sum(SUMij_list[:, i]))
            SUMf_th.append(np.sum(SUMf_list[:, i]))
            SUMR_th.append(np.sum(SUMR_list[:, i]))
        csi = (np.array(SUMij_th))/np.array(SUMf_th)#-Imean**2
        r = (np.array(SUMR_th))/np.array(SUMf_th)#-Imean**2
        csi_txt.write('THETA\t%s\n'%str(list(theta)).replace('[',''). \
                          replace(']','').replace(', ', ' ')) 
        csi_txt.write('CSI\t%s\n'%str(list(csi)).replace('[',''). \
                          replace(']','').replace(', ', ' '))
        csi_txt.write('R\t%s\n'%str(list(r)).replace('[',''). \
                          replace(']','').replace(', ', ' '))
    csi_txt.close()
    p.close()
    p.join()
    logger.info('Created %s'%(os.path.join(GRATOOLS_OUT, '%s_%s_csi.txt' \
                                               %(out_label, binning_label))))
コード例 #5
0
ファイル: gMasks.py プロジェクト: mahmoud-lsw/GRATools
def mask_src_weighted(cat_file, ENERGY, NSIDE):
    """Returns the 'bad pixels' defined by the position of a source and a 
       certain radius away from that point. The radii increase with the 
       brightness.

       SOURCE_CAT: str
           opened fits file with the sorce catalog
       NSIDE: int
           healpix nside parameter
    """
    from GRATools.utils.gWindowFunc import get_psf_ref
    psf_ref_file = os.path.join(GRATOOLS_CONFIG, 'ascii/PSF_UCV_PSF1.txt')
    src_cat = pf.open(cat_file)
    NPIX = hp.pixelfunc.nside2npix(NSIDE)
    CAT = src_cat['LAT_Point_Source_Catalog']
    CAT_EXTENDED = src_cat['ExtendedSources']
    BAD_PIX_SRC = []
    SOURCES = CAT.data
    EXT_SOURCES = CAT_EXTENDED.data
    src_cat.close()
    psf_ref = get_psf_ref(psf_ref_file)
    psf_en = psf_ref(ENERGY)
    psf_min, psf_max = psf_ref.y[5], psf_ref.y[-1]
    norm_min, norm_max = 1, 0.3
    norm = norm_min + psf_en*((norm_max - norm_min)/(psf_max - psf_min)) -\
        psf_min*((norm_max - norm_min)/(psf_max - psf_min))
    logger.info('Normalization of radii due to energy: %.3f' % norm)
    print 'Psf(%.2f)= %.2f' % (ENERGY, psf_en)
    FLUX = SOURCES.field('Flux1000')
    flux_min, flux_max = min(FLUX), max(FLUX)
    rad_min, rad_max = 2., 5.
    RADdeg = rad_min + FLUX*((rad_max - rad_min)/(flux_max - flux_min)) -\
        flux_min*((rad_max - rad_min)/(flux_max - flux_min))
    RADrad = np.radians(RADdeg)
    #*****
    #plt.title('Radius($\phi$)')
    #plt.plot(FLUX, RADdeg, 'ro', ms=3, alpha=0.75)
    #plt.plot((1e-12, 1e-5), (2, 2), '-', color='silver', linewidth=1.0)
    #plt.xlabel('$\phi$')
    #plt.ylabel('Radius [$\circ$]')
    #plt.yscale('log')
    #plt.xscale('log')
    #plt.ylim(0.7, 6)
    #plt.show()
    #*****
    logger.info('Masking the extended Sources')
    logger.info('-> 10deg around CenA and LMC')
    logger.info('-> 5deg around the remaining')
    for i, src in enumerate(EXT_SOURCES):
        NAME = EXT_SOURCES[i][0]
        GLON = EXT_SOURCES[i][4]
        GLAT = EXT_SOURCES[i][5]
        if NAME == 'LMC' or NAME == 'CenA Lobes':
            x, y, z = hp.rotator.dir2vec(GLON, GLAT, lonlat=True)
            b_pix = hp.pixelfunc.vec2pix(NSIDE, x, y, z)
            BAD_PIX_SRC.append(b_pix)
            radintpix = hp.query_disc(NSIDE, (x, y, z), np.radians(10) * norm)
            BAD_PIX_SRC.extend(radintpix)
        else:
            x, y, z = hp.rotator.dir2vec(GLON, GLAT, lonlat=True)
            b_pix = hp.pixelfunc.vec2pix(NSIDE, x, y, z)
            BAD_PIX_SRC.append(b_pix)
            radintpix = hp.query_disc(NSIDE, (x, y, z), np.radians(5) * norm)
            BAD_PIX_SRC.extend(radintpix)
    logger.info('Flux-weighted mask for sources activated')
    for i, src in enumerate(SOURCES):
        GLON = SOURCES[i][3]
        GLAT = SOURCES[i][4]
        x, y, z = hp.rotator.dir2vec(GLON, GLAT, lonlat=True)
        b_pix = hp.pixelfunc.vec2pix(NSIDE, x, y, z)
        BAD_PIX_SRC.append(b_pix)
        radintpix = hp.query_disc(NSIDE, (x, y, z), RADrad[i] * norm)
        BAD_PIX_SRC.extend(radintpix)
    return BAD_PIX_SRC
コード例 #6
0
ファイル: gMasks.py プロジェクト: nmik/GRATools
def mask_src_weighted(cat_src_file, cat_extsrc_file, ENERGY, NSIDE):
    """Returns the 'bad pixels' defined by the position of a source and a 
       certain radius away from that point. The radii increase with the 
       brightness and rescaled by a factor between 1 and 0.3 shaped as the PSF.

       cat_src_file: str
          .fits file with the source catalog
       cat_extsrc_file: str
          .fits file with the extended sources catalog
       ENERGY: float
          Mean energy of the map to be masked
       NSIDE: int
          healpix nside parameter
    """
    psf_ref_file = os.path.join(GRATOOLS_CONFIG, 'ascii/PSF_UCV_custom.txt')
    src_cat = pf.open(cat_src_file)
    extsrc_cat = pf.open(cat_extsrc_file)
    NPIX = hp.pixelfunc.nside2npix(NSIDE)
    CAT = src_cat['LAT_Point_Source_Catalog']
    CAT_EXTENDED = extsrc_cat['ExtendedSources']
    BAD_PIX_SRC = []
    SOURCES = CAT.data
    EXT_SOURCES = CAT_EXTENDED.data
    src_cat.close()
    psf_ref = get_psf_ref(psf_ref_file)
    if '4FGL' in cat_src_file:
        FLUX = np.log10(SOURCES.field('Flux_Density'))
    if 'psch' in cat_src_file:
        FLUX = np.log10(SOURCES.field('Flux'))
    else:
        FLUX = np.log10(SOURCES.field('Flux1000'))
    flux_min, flux_max = min(FLUX), max(FLUX)
    rad_min = 2*psf_ref(ENERGY)
    rad_max = 5*psf_ref(ENERGY)
    RADdeg = rad_min + FLUX*((rad_max - rad_min)/(flux_max - flux_min)) -\
        flux_min*((rad_max - rad_min)/(flux_max - flux_min))
    RADrad = np.radians(RADdeg)
    logger.info('Masking the extended Sources')
    logger.info('-> 10xPSF(%.2f GeV) around CenA and LMC'%(ENERGY/1000))
    logger.info('-> 5xPSF(%.2f GeV) around the remaining'%(ENERGY/1000))
    for i, src in enumerate(EXT_SOURCES):
        NAME = EXT_SOURCES[i][0]
        GLON = EXT_SOURCES.field('GLON')[i]
        GLAT = EXT_SOURCES.field('GLAT')[i]
        if 'LMC' in NAME or 'CenA Lobes' in NAME:
            rad = 10*psf_ref(ENERGY)
            x, y, z = hp.rotator.dir2vec(GLON,GLAT,lonlat=True)
            b_pix= hp.pixelfunc.vec2pix(NSIDE, x, y, z)
            BAD_PIX_SRC.append(b_pix) 
            radintpix = hp.query_disc(NSIDE, (x, y, z), np.radians(10))
            BAD_PIX_SRC.extend(radintpix)
        else:
            rad = 5*psf_ref(ENERGY)
            x, y, z = hp.rotator.dir2vec(GLON,GLAT,lonlat=True)
            b_pix = hp.pixelfunc.vec2pix(NSIDE, x, y, z)
            BAD_PIX_SRC.append(b_pix) 
            radintpix = hp.query_disc(NSIDE, (x, y, z), np.radians(5))
            BAD_PIX_SRC.extend(radintpix)
    logger.info('Flux-weighted mask for sources activated')
    for i, src in enumerate(SOURCES):
        GLON = SOURCES.field('GLON')[i]
        GLAT = SOURCES.field('GLAT')[i]
        x, y, z = hp.rotator.dir2vec(GLON,GLAT,lonlat=True)
        b_pix= hp.pixelfunc.vec2pix(NSIDE, x, y, z)
        BAD_PIX_SRC.append(b_pix) 
        radintpix = hp.query_disc(NSIDE, (x, y, z), RADrad[i])
        BAD_PIX_SRC.extend(radintpix)
    return BAD_PIX_SRC
コード例 #7
0
ファイル: mkcsi.py プロジェクト: mahmoud-lsw/GRATools
def mkCsi(**kwargs):
    """                                      
    """
    get_var_from_file(kwargs['config'])
    ncores = kwargs['ncores']
    psf_file = data.PSF_REF_FILE
    p = multiprocessing.Pool(processes=ncores)
    logger.info('Starting Csi analysis...')
    in_label = data.IN_LABEL
    out_label = data.OUT_LABEL
    binning_label = data.BINNING_LABEL
    cl_param_file = os.path.join(GRATOOLS_OUT, '%s_%s_parameters.txt' \
                                     %(in_label, binning_label))
    from GRATools.utils.gFTools import get_cl_param
    _emin, _emax, _emean, _f, _ferr, _cn, _fsky = get_cl_param(cl_param_file)
    csi_txt = open(os.path.join(GRATOOLS_OUT, '%s_%s_csi.txt' \
                                   %(out_label, binning_label)), 'w')
    psf_ref = get_psf_ref(psf_file)
    #psf_ref.plot(show=False)
    #plt.xscale('log')
    #plt.yscale('log')
    #plt.show()
    for i, (emin, emax) in enumerate(zip(_emin, _emax)):
        logger.info('Considering bin %.2f - %.2f ...' % (emin, emax))
        cont_ang = np.radians(psf_ref(_emean[i]))
        csi_txt.write('ENERGY\t %.2f %.2f %.2f\n' % (emin, emax, _emean[i]))
        flux_map_name = in_label + '_flux_%i-%i.fits' % (emin, emax)
        flux_map = hp.read_map(os.path.join(GRATOOLS_OUT_FLUX, flux_map_name))
        flux_map = udgrade_as_psf(flux_map, cont_ang)
        R = hp.read_map(os.path.join(GRATOOLS_OUT_FLUX, flux_map_name))
        R = udgrade_as_psf(R, cont_ang)
        fsky = 1.-(len(np.where(flux_map == hp.UNSEEN)[0])/\
                       float(len(flux_map)))
        logger.info('fsky = %f' % fsky)
        npix = len(flux_map)
        nside = hp.npix2nside(npix)
        _unmask = np.where(flux_map != hp.UNSEEN)[0]
        npix_unmask = len(_unmask)
        Imean = _f[i]
        dI = flux_map - Imean
        dR = R - Imean
        R = permute_unmasked_pix(R)
        dR = permute_unmasked_pix(dR)
        th_bins = data.TH_BINNING
        theta = []
        for thmin, thmax in zip(th_bins[:-1], th_bins[1:]):
            th_mean = np.sqrt(thmin * thmax)
            theta.append(th_mean)
        theta = np.array(theta)
        logger.info('Computing Csi...')
        diri = hp.pixelfunc.pix2ang(nside, _unmask)
        veci = hp.rotator.dir2vec(diri)
        xyz = np.array([(veci[0][i], veci[1][i], veci[2][i])
                        for i in range(0, len(veci[0]))])
        args = zip(_unmask, xyz, [dI] * npix_unmask, [dR] * npix_unmask,
                   [nside] * npix_unmask)
        #args = zip(_unmask, xyz, [flux_map]*npix_unmask, [R]*npix_unmask,
        #           [nside]*npix_unmask)
        a = np.array(p.map(csi_compute, args))
        SUMij_list = a[:, 0]
        SUMf_list = a[:, 1]
        SUMR_list = a[:, 2]
        SUMij_th = []
        SUMf_th = []
        SUMR_th = []
        for i, s in enumerate(SUMij_list[0]):
            SUMij_th.append(np.sum(SUMij_list[:, i]))
            SUMf_th.append(np.sum(SUMf_list[:, i]))
            SUMR_th.append(np.sum(SUMR_list[:, i]))
        csi = (np.array(SUMij_th)) / np.array(SUMf_th)  #-Imean**2
        r = (np.array(SUMR_th)) / np.array(SUMf_th)  #-Imean**2
        csi_txt.write('THETA\t%s\n'%str(list(theta)).replace('[',''). \
                          replace(']','').replace(', ', ' '))
        csi_txt.write('CSI\t%s\n'%str(list(csi)).replace('[',''). \
                          replace(']','').replace(', ', ' '))
        csi_txt.write('R\t%s\n'%str(list(r)).replace('[',''). \
                          replace(']','').replace(', ', ' '))
    csi_txt.close()
    p.close()
    p.join()
    logger.info('Created %s'%(os.path.join(GRATOOLS_OUT, '%s_%s_csi.txt' \
                                               %(out_label, binning_label))))