コード例 #1
0
ファイル: PolSpice_.py プロジェクト: nmik/Xgam
def new_binning(xmin,
                xmax,
                nbin=25,
                bin_type='lin',
                out_type=int,
                custom_bins=None):
    """
    Define the new binning.

    Parameters
	----------

	Returns
	-------
	array
	   the array with the edges of the new binning
    """
    if bin_type == 'lin' and custom_bins is None:
        binning_ = np.linspace(xmin, xmax, num=nbin + 1, dtype=out_type)
    elif bin_type == 'log' and custom_bins is None:
        if xmin == 0:
            xmin = 1
        binning_ = np.logspace(np.log10(xmin),
                               np.log10(xmax),
                               num=nbin + 1,
                               dtype=out_type)
    elif type(custom_bins) == list or type(custom_bins) == np.ndarray:
        binning_ = np.array(custom_bins)
    else:
        logger.info(
            'ERROR: Invalid binning type. Choose lin or log, or customize it.')
        sys.exit()

    logger.info('Multipole binning:%s' % str(binning_))
    return binning_
コード例 #2
0
ファイル: ScienceTools_.py プロジェクト: nmik/Xgam
def gtEbindef(ebinning_array, file_name='ebinning.txt'):
    """
    Produces a fits file defining the enrgy binning to fed gtbin.

	Parameters
	----------
    ebinning_array: numpy array
        array in which the energy binnin is defined.
    file_name : str
        file name for the output txt file. (Default = 'ebinning.txt')

    Returns
    -------
    str
        file name of the fits file created
    """
    if not os.path.exists(X_OUT):
        os.makedirs(X_OUT)
    txt_file_name = os.path.join(X_OUT, file_name)
    txt_file = open(txt_file_name, 'w')
    fits_file_name = os.path.join(X_OUT, file_name.replace('.txt', '.fits'))
    for emin, emax in zip(ebinning_array[:-1], ebinning_array[1:]):
        txt_file.write('%.4f %.4f\n' % (emin, emax))
    txt_file.close()
    os.system('gtbindef bintype=E binfile=%s outfile=%s energyunits=MeV' \
                  %(txt_file_name, fits_file_name))
    logger.info('Created %s...' % fits_file_name)
    return fits_file_name
コード例 #3
0
ファイル: fluxviewer.py プロジェクト: nmik/Xgam
def flux_view(**kwargs):
    """
    Viewer interface for flux spectrum                                                                                                                                                             
    """

    params = parse_datafluxmaps(kwargs['infile'])
    _emin, _emax, _emean = params[0], params[1], params[2]

    if len(params[-1]) == 0:
        _f, _ferr = params[3], params[4]

    else:
        _n, _nsx, _ndx = params[-6], params[-5], params[-4]
        _f, _fsxerr, _fdxerr = params[-3], params[-2], params[-1]
        _ferr = [abs(_f - _fsxerr), abs(_f - _fdxerr)]

    logger.info('Plotting Energy Spectrum ...')
    fig, ax = plt.subplots(1)
    leg, lab = ref_igrb_band()

    spec = plt.errorbar(_emean, (_f) / (_emax - _emin) * _emean**2,
                        fmt='o',
                        color='red',
                        markersize=3,
                        elinewidth=2,
                        xerr=[(_emean - _emin), (_emax - _emean)],
                        yerr=_ferr / (_emax - _emin) * _emean**2)

    Aeff_sys = plt.fill_between(_emean,
                                (_f - 0.1 * _f) / (_emax - _emin) * _emean**2,
                                (_f + 0.1 * _f) / (_emax - _emin) * _emean**2,
                                color='red',
                                alpha=0.3)
    plt.xscale("log")
    plt.yscale("log")
    plt.xlabel('Energy [MeV]', size=14)
    plt.ylabel('E$^{2}$ $\cdot$ Flux [MeV cm$^{-2}$ s$^{-1}$ sr$^{-1}$]',
               size=14)
    plt.title(kwargs['title'])
    plt.legend([leg, spec, Aeff_sys],
               [lab, 'UGRB flux (Xgam)', 'A$_{eff}$ sys. band'],
               loc=3,
               fontsize=15)

    plt.tick_params(axis='both', which='major', labelsize=15)
    plt.ylim(1e-7, 4.e-2)
    plt.xlim(100, 1000000)

    if len(params[-1]) != 0:
        logger.info('Plotting Foreground best-fit Normalization ...')

        plt.figure()
        plt.title(kwargs['title'])
        plt.fill_between(_emean, _nsx, _ndx, color='lightcoral', alpha=0.5)
        plt.plot(_emean, _n, 'ro-')
        plt.xscale('log')
        plt.xlabel('Energy [MeV]', size=14)
        plt.ylabel('IEM Normalization', size=14)

    plt.show()
コード例 #4
0
ファイル: foregroundfit_.py プロジェクト: nmik/Xgam
def find_outer_energies(en_val, en_arr):
    """
    Gives the first element on the right and the first on the left
    of a given E value (en_val), among all values in an ordered array (en_arr).
    These values are used to integrate the foreground model in the considered energy bin.

    Parameters
    ----------
    en_val : float
           mean energy
    en_arr : float
           array of the energies at which the foreground model is given.
           
    Returns
    -------
    float, float
        first element on the right and the first on the left
    
    """
    en_sx_arr = en_arr[en_arr < en_val]
    en_dx_arr = en_arr[en_arr > en_val]
    if en_sx_arr.size == 0:
        en_sx = en_dx_arr[0]
        en_dx = en_dx_arr[1]
        logger.info('Considering model in the interval %.1f-%.1f MeV' %
                    (en_sx, en_dx))
    elif en_dx_arr.size == 0:
        en_sx = en_sx_arr[-2]
        en_dx = en_sx_arr[-1]
        logger.info('Considering model in the interval %.1f-%.1f MeV' %
                    (en_sx, en_dx))
    else:
        en_sx = en_sx_arr[-1]
        en_dx = en_dx_arr[0]
    return en_sx, en_dx
コード例 #5
0
def aps_compare(**kwargs):
    """
    Viewer interface for APS and Covariance matrix                                                                                                                                                             
    """

    logger.info('Generating comparison APS plots...')

    l_f, cl_f, clerr_f = [], [], []

    for f in kwargs['infile']:
        params = parse_polspice_aps(f)
        _emin, _emax = params[0], params[1]
        _l, _cl, _clerr = params[2], params[3], params[4]
        _cov_ = params[5]

        l_f.append(_l)
        cl_f.append(_cl)
        clerr_f.append(_clerr)

    l_f = np.array(l_f)
    cl_f = np.array(cl_f)
    clerr_f = np.array(clerr_f)

    nfiles = l_f.shape[0]
    nebins = l_f.shape[1]

    lab = kwargs['labels']
    if kwargs['labels'] is None:
        lab = []
        for i in range(nfiles):
            lab.append('File %s' % i)

    for i in range(nebins):
        plt.figure()

        plt.hlines(0, 1, 1000, color='silver')

        plt.title('%s' % kwargs['title'])
        for j in range(nfiles):
            plt.errorbar(l_f[j][i],
                         cl_f[j][i],
                         fmt='o',
                         markersize=5,
                         elinewidth=2,
                         yerr=clerr_f[j][i],
                         label=lab[j])

        plt.xscale(kwargs['xscale'])
        plt.xlabel('$\ell$', size=15)
        plt.ylabel('C$_{\ell}$', size=15)

        plt.legend()

    plt.show()
コード例 #6
0
ファイル: ScienceTools_.py プロジェクト: nmik/Xgam
def gtselect(label, filter_dict):
    """
    Calls gtselect from Science Tools.

    Parameters
    ----------
    label: str
        To automatically set the name of the output file
    filter_dict: python dict
        To define all the parameters

    Returns
    -------
    str
        the autogenerated output file name

    """
    logger.info('Running gtselect...')
    LABEL = label
    OUTPATH = os.path.join(FT_DATA_OUT, 'output_gtselect')
    if not os.path.exists(OUTPATH):
        os.makedirs(OUTPATH)
    OUTFILE = os.path.join(OUTPATH, LABEL + '_outofselect.fits')
    if os.path.exists(OUTFILE):
        logger.info('ATT: Already created %s' % OUTFILE)
    else:
        for key in filter_dict:
            my_apps.filter[key] = filter_dict[key]
        my_apps.filter['outfile'] = OUTFILE
        my_apps.filter.run()
        logger.info('Created %s' % OUTFILE)
        logger.info('gtselect --> CPU time spent: %.2f' % time.clock())
    return OUTFILE
コード例 #7
0
ファイル: wbeamfunc_.py プロジェクト: nmik/Xgam
def build_wbeam(psf, l_, out_file):
    """
    Calculates the Wbeam(l, E) and return a bivariate slpine.
    
    Parameters
    ----------
    psf : spline
        psf bivariate spline generated by get_psf
    l_  :  array
        array of multipoles at wicht to compute the Wbeam
    out_file : str
        name of the output txt file that will be created in output/
             
    Returns
    ------- 
    spline
        spline of the beam window function   
        
    """
    out_txt = open(os.path.join(X_OUT, out_file), 'w')
    en_ = psf.x
    _wb_ = []
    energy = str(list(en_)).replace('[','').replace(']','').replace(', ', ' ')
    out_txt.write('l\t%s\n'%energy)
    for e in en_:
        wb_e = np.array([])
        psf_th = psf.vslice(e)
        for l in l_:
            pl_th = get_pl_vs_th(l, np.cos(psf_th.x))
            fmt = dict(xname='th', xunits='rad', yname='convolution', \
                           yunits='MeV')
            _conv = xInterpolatedUnivariateSplineLinear(psf_th.x, \
                                    np.sin(psf_th.x)*psf_th.y*pl_th, **fmt)
            wb_e_l = min(1., 2*np.pi*(_conv.integral(np.amin(psf_th.x), \
                                                         np.amax(psf_th.x))))
            logger.info('Wbeam(%i, %.2f) = %e'%(l, e, wb_e_l))
            wb_e = np.append(wb_e, [wb_e_l])
        _wb_.append(wb_e)
    _wb_ = np.array(_wb_)
    fmt = dict(xname='$l$', xunits='', yname='Energy',
                             yunits='MeV', zname='W$_{beam}$(E,$l$)')
    wbeam = xInterpolatedBivariateSplineLinear(l_, en_, _wb_.T, **fmt)
    for i, l in enumerate(l_):
        wb = str(list(_wb_.T[i])).replace('[','').replace(']','').\
            replace(', ', ' ')
        out_txt.write('%i\t%s\n'%(l, wb))
    out_txt.close()
    return _wb_
コード例 #8
0
def parse_datafluxmaps(out_dataflux_map_file):
    """Parsing of *_parameters.txt files.

       cl_param_file : str
           This file is created by bin/mkdatarestyle.py app.
    """
    logger.info('Loading parameters from %s' % out_dataflux_map_file)
    ff = open(out_dataflux_map_file, 'r')
    _emin, _emax, _emean = [], [], []
    _f, _ferr, _cn, _fsky = [], [], [], []
    _n, _n_sx, _n_dx, _c, _c_sx, _c_dx = [], [], [], [], [], []
    for line in ff:
        try:
            emin, emax, emean, f, ferr, cn, fsky, n, nsx, ndx, c, csx, cdx = [float(item) for item in \
                                                        line.split()]
            _emin.append(emin)
            _emax.append(emax)
            _emean.append(emean)
            _f.append(f)
            _ferr.append(ferr)
            _cn.append(cn)
            _fsky.append(fsky)
            _n.append(n)
            _n_sx.append(nsx)
            _n_dx.append(ndx)
            _c.append(c)
            _c_sx.append(csx)
            _c_dx.append(cdx)
        except:
            try:
                emin, emax, emean, f, ferr, cn, fsky = [
                    float(item) for item in line.split()
                ]
                _emin.append(emin)
                _emax.append(emax)
                _emean.append(emean)
                _f.append(f)
                _ferr.append(ferr)
                _cn.append(cn)
                _fsky.append(fsky)
            except:
                pass

    ff.close()
    return [np.array(_emin), np.array(_emax), np.array(_emean), np.array(_f), \
        np.array(_ferr), np.array(_cn), np.array(_fsky), np.array(_n),np.array(_n_sx),\
        np.array(_n_dx), np.array(_c), np.array(_c_sx), np.array(_c_dx)]
コード例 #9
0
ファイル: ScienceTools_.py プロジェクト: nmik/Xgam
def mergeft1(path_to_files, out_file_name, N1week, Nnweek):
    """
    Creates a .txt file with the list of the FT files to merge.

    Parameters
	----------
    path_to_files: str
        path where datat files are stored
    out_file_name: str
        name of the txt output file (created in the same folder of data)
    N1week: int
        number of the starting week
    Nnweek: int
        number of the ending week

    Returns
    -------
    str
        string to pass to gtselect function

    """
    if N1week < 9:
        abort('Invalid number of weeks: the minimun must be > or = to 9')
    if Nnweek > 621:
        abort('Invalid number of weeks: the maximum must be < or = to 1000')
    outtxtfile = os.path.join(path_to_files, out_file_name)
    if not os.path.exists(outtxtfile):
        out_file = open(outtxtfile, 'w')
        for i in range(N1week, Nnweek + 1):
            if i == 9:
                out_file.write("%s/lat_photon_weekly_w00%i_p305_v001.fits \n" \
                                   %(path_to_files,i))
            if i >= 10 and i <= 99:
                out_file.write("%s/lat_photon_weekly_w0%i_p305_v001.fits \n" \
                                   %(path_to_files,i))
            if i > 99:
                if i == 512:
                    pass
                else:
                    out_file.write("%s/lat_photon_weekly_w%i_p305_v001.fits \n" \
                                       %(path_to_files,i))
        out_file.close()
    logger.info('Created %s...' % outtxtfile)
    return '@' + outtxtfile
コード例 #10
0
ファイル: mkmask_.py プロジェクト: nmik/Xgam
def mask_galactic_src(cat_file, mask_rad, nside):
    """Returns the 'bad pixels' defined by the position of a galactic source and a
       certain radius away from that point.

       cat_file: str
           .fits file of the sorce catalog
       MASK_S_RAD: float
           radius around each source definig bad pixels to mask
       NSIDE: int
           healpix nside parameter
    """
    logger.info('Mask for sources activated')
    src_cat = pf.open(cat_file)
    NPIX = hp.pixelfunc.nside2npix(nside)
    CAT = src_cat['LAT_Point_Source_Catalog']
    RADrad = np.radians(mask_rad)

    SOURCES = CAT.data
    class1 = SOURCES.field('CLASS1')
    class_mask = (class1 == 'PSR') | (class1 == 'PWN') | (class1 == 'SNR') | \
                 (class1 == 'psr') | (class1 == 'pwn') | (class1 == 'snr') | \
                 (class1 == 'spp') | (class1 == 'SFR') | (class1 == 'HMB') | \
                 (class1 == 'LMB') | (class1 == 'NOV') | (class1 == 'BIN') | \
                 (class1 == 'lmb') | (class1 == 'SPP') | (class1 == 'hmb') | \
                 (class1 == 'glc')

    logger.info('Found %i galactic sources...' % sum(class_mask))
    GLON = SOURCES.field('GLON')[class_mask]
    GLAT = SOURCES.field('GLAT')[class_mask]
    x, y, z = hp.rotator.dir2vec(GLON, GLAT, lonlat=True)
    bad_pix = list(hp.pixelfunc.vec2pix(nside, x, y, z))

    bad_pix_inrad = []
    for bn in bad_pix:
        pixVec = hp.pix2vec(nside, bn)
        radintpix = hp.query_disc(nside, pixVec, RADrad)
        bad_pix_inrad.extend(radintpix)
    bad_pix.extend(bad_pix_inrad)

    src_cat.close()

    return bad_pix
コード例 #11
0
ファイル: ScienceTools_.py プロジェクト: nmik/Xgam
def gtpsf(label, gtpsf_dict):
    """
    Calls gtpsf from Science Tools

	Parameters
	----------
	label: str
        To automatically set the name of the output file
    gtpsf_dict: python dict
        To define all the parameters

    Returns
    -------
    str
        the autogenerated output file name

    """
    logger.info('Running gtpsf...')
    LABEL = label
    OUTPATH = os.path.join(FT_DATA_FOLDER, 'output/output_gtpsf')
    if not os.path.exists(OUTPATH):
        os.makedirs(OUTPATH)
    INFILE = os.path.join(FT_DATA_FOLDER,
                          'output/output_gtltcube/%s_outofltcube.fits' % LABEL)
    OUTFILE = os.path.join(OUTPATH, LABEL + '_psf.fits')
    if os.path.exists(OUTFILE):
        logger.info('ATT: Already created %s' % OUTFILE)
        return OUTFILE
    if gtpsf_dict['outfile'] == 'DEFAULT':
        outfile = OUTFILE
    else:
        outfile = gtpsf_dict['outfile']
    if gtpsf_dict['expcube'] == 'DEFAULT':
        expcube = INFILE
    else:
        expcube = gtpsf_dict['expcube']
    irfs = gtpsf_dict['irfs']
    evtype = gtpsf_dict['evtype']
    ra = gtpsf_dict['ra']
    dec = gtpsf_dict['dec']
    emin = gtpsf_dict['emin']
    emax = gtpsf_dict['emax']
    nenergies = gtpsf_dict['nenergies']
    thetamax = gtpsf_dict['thetamax']
    ntheta = gtpsf_dict['ntheta']
    os.system('gtpsf expcube=%s outfile=%s irfs=%s evtype=%i ra=%f dec=%f emin=%e emax=%e nenergies=%i thetamax=%i ntheta=%i' \
                  %(expcube, outfile, irfs, evtype, ra, dec, emin, emax, \
                        nenergies, thetamax, ntheta))
    logger.info('Created %s' % outfile)
    logger.info('gtpsf --> CPU time spent: %.2f' % time.clock())
    return OUTFILE
コード例 #12
0
ファイル: mkmask_.py プロジェクト: nmik/Xgam
def mask_src(CAT_FILE, MASK_S_RAD, NSIDE, FLUX_THRESHOLD=None):
    """Returns the 'bad pixels' defined by the position of a source and a
       certain radius away from that point.

       cat_file: str
           .fits file of the sorce catalog
       MASK_S_RAD: float
           radius around each source definig bad pixels to mask
       NSIDE: int
           healpix nside parameter
       FLUX_TRHRESHOLD: float
           threshold in Integral photon flux from 1 to 100 GeV to mask sources;
           only sources with 'Flux1000' > FLUX_TRHRESHOLD will be masked.
    """
    logger.info('Mask for sources activated')
    src_cat = pf.open(CAT_FILE)
    NPIX = hp.pixelfunc.nside2npix(NSIDE)
    CAT = src_cat['LAT_Point_Source_Catalog']
    BAD_PIX_SRC = []
    SOURCES = CAT.data
    RADrad = np.radians(MASK_S_RAD)
    for i in range(0, len(SOURCES) - 1):
        FLUX1000 = SOURCES.field('Flux1000')[i]
        if FLUX_THRESHOLD is not None:
            if FLUX1000 < FLUX_THRESHOLD:
                continue
            else:
                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)
    BAD_PIX_inrad = []
    for bn in BAD_PIX_SRC:
        pixVec = hp.pix2vec(NSIDE, bn)
        radintpix = hp.query_disc(NSIDE, pixVec, RADrad)
        BAD_PIX_inrad.extend(radintpix)
    BAD_PIX_SRC.extend(BAD_PIX_inrad)
    src_cat.close()
    return BAD_PIX_SRC
コード例 #13
0
ファイル: foregroundfit_.py プロジェクト: nmik/Xgam
def get_param_error(profilelh_spline, param_array, lh_delta=2.3):
    """
    """

    lh_array = profilelh_spline(param_array)
    lh_min_idx = np.argmin(lh_array)
    lherr = lh_array[lh_min_idx] + lh_delta

    if lh_min_idx == 0:
        logger.info('ATT: UPPER limit!')
        sx_err = param_array[0]
        dx_err = param_array[-1]
    elif lh_min_idx == len(lh_array) - 1:
        logger.info('ATT: LOWER limit!')
        sx_err = param_array[0]
        dx_err = param_array[-1]
    else:
        sx_err = param_array[np.abs(lh_array[:lh_min_idx] - lherr).argmin()]
        dx_err = param_array[lh_min_idx +
                             np.abs(lh_array[lh_min_idx:] - lherr).argmin()]

    return sx_err, dx_err
コード例 #14
0
ファイル: combinemask.py プロジェクト: nmik/Xgam
def combinemasks(**kwargs):
    """Routine to produce sky maps (limited edition)
    """
    f_ = kwargs['maskfiles']
    logger.info('Reading mask %s...' % f_[0])
    combmask = hp.read_map(f_[0])

    for f in f_[1:]:
        m = hp.read_map(f)
        combmask *= m

    hp.write_map(
        os.path.join(X_OUT, 'fits/MaskCombo_%s.fits' % kwargs['outflabel']),
        combmask)
    logger.info(
        'Created %s' %
        os.path.join(X_OUT, 'fits/MaskCombo_%s.fits' % kwargs['outflabel']))

    if kwargs['show'] == True:

        hp.mollview(combmask, cmap='bone')
        plt.show()
コード例 #15
0
ファイル: PolSpice_.py プロジェクト: nmik/Xgam
def pol_create_config(pol_dict, config_file_name):
    """
    Creates and returns PolSpice config ascii file.

    Parameters
	----------
    pol_dict : python dict
        a dictionary where all the parameters of a tipical PolSpice config
        ascii file should have.
    config_file_name : str
        name of the confg ascii file that will be created

    Returns
    -------
    str
        Name of the created PolSpice config file
    """
    pol_config = config_file_name
    pol_config_file = open(pol_config, 'w')
    for key in pol_dict:
        pol_config_file.write('%s = %s \n' % (key, str(pol_dict[key])))
    logger.info(config_file_name)
    return pol_config
コード例 #16
0
ファイル: ScienceTools_.py プロジェクト: nmik/Xgam
def gtltsum(label, sumlt_dict):
    """
    Calls gtltsum from Science Tools

    Parameters
    ----------
    lablel: str
        To automatically set the name of the output file
    sumlt_dict: python dict
        To define all the parameters

    Returns
    -------
    str
        the autogenerated output file name
    """
    logger.info('Running gtltsum...')
    LABEL = label
    OUTPATH = os.path.join(FT_DATA_FOLDER, 'output/output_gtltsum')
    if not os.path.exists(OUTPATH):
        os.makedirs(OUTPATH)
    OUTFILE = os.path.join(OUTPATH, LABEL + '_ltsum.fits')
    if os.path.exists(OUTFILE):
        logger.info('ATT: Already created %s' % OUTFILE)
    else:
        for key in sumlt_dict:
            if key == 'outfile':
                if sumlt_dict[key] == 'DEFAULT':
                    my_apps.addCubes['outfile'] = OUTFILE
                else:
                    my_apps.addCubes[key] = sumlt_dict[key]
                continue
            my_apps.addCubes[key] = sumlt_dict[key]
        my_apps.addCubes.run()
        logger.info('Created %s' % OUTFILE)
        logger.info('gtsumlt --> CPU time spent: %.2f' % time.clock())

    return OUTFILE
コード例 #17
0
ファイル: ScienceTools_.py プロジェクト: nmik/Xgam
def gtmktime(label, maketime_dict):
    """
    Calls gtmktime from Science Tools.

    Parameters
    ----------
    label: str
        To automatically set the name of the output file
    filter_dict: python dict
        To define all the parameters

    Returns
    -------
    str
        the autogenerated output file name

    """
    logger.info('Running gtmktime...')
    LABEL = label
    OUTPATH = os.path.join(FT_DATA_OUT, 'output_gtmktime')
    if not os.path.exists(OUTPATH):
        os.makedirs(OUTPATH)
    OUTFILE = os.path.join(OUTPATH, LABEL + '_outofmktime.fits')
    if os.path.exists(OUTFILE):
        logger.info('ATT: Already created %s' % OUTFILE)
    else:
        for key in maketime_dict:
            if key == 'outfile':
                if maketime_dict[key] == 'DEFAULT':
                    my_apps.maketime['outfile'] = OUTFILE
                else:
                    my_apps.maketime[key] = maketime_dict[key]
                continue
            my_apps.maketime[key] = maketime_dict[key]
        my_apps.maketime.run()
        logger.info('Created %s' % OUTFILE)
        logger.info('gtmktime --> CPU time spent: %.2f' % time.clock())
    return OUTFILE
コード例 #18
0
ファイル: mkmask_.py プロジェクト: nmik/Xgam
def mask_extsrc(CAT_FILE, nside=512):
    """Returns the 'bad pixels' defined by the position of a source and a
       certain radius away from that point.

       cat_file: str
           .fits file of the sorce catalog
       mask_rad: float
           radius around each source definig bad pixels to mask
       nside: int
           healpix nside parameter
    """
    logger.info('Mask for extended sources activated')
    src_cat = pf.open(CAT_FILE)
    NPIX = hp.pixelfunc.nside2npix(nside)
    CAT_EXTENDED = src_cat['ExtendedSources']
    BAD_PIX_SRC = []
    EXT_SOURCES = CAT_EXTENDED.data
    src_cat.close()
    for i, src in enumerate(EXT_SOURCES):
        NAME = EXT_SOURCES.field('Source_Name')[i]
        GLON = EXT_SOURCES.field('GLON')[i]
        GLAT = EXT_SOURCES.field('GLAT')[i]
        if 'LMC' in NAME or 'CenA Lobes' in NAME:
            logger.info('Masking %s with 10 deg radius disk...' % NAME)
            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:
            logger.info('Masking %s with 5 deg radius disk...' % NAME)
            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)
    return BAD_PIX_SRC
コード例 #19
0
ファイル: mksmartmask.py プロジェクト: nmik/Xgam
def mkSmartMask(**kwargs):
    """Routine to produce sky maps, according to defined macro-bins.
    """
    logger.info('SmartMask production...')
    assert(kwargs['config'].endswith('.py'))
    get_var_from_file(kwargs['config'])
    macro_bins = data.MACRO_BINS
    mask_label = data.MASK_LABEL
    in_labels_list = data.IN_LABELS_LIST
    micro_bin_file = data.MICRO_BINS_FILE
    emin, emax, emean = get_energy_from_fits(micro_bin_file, \
                        minbinnum=macro_bins[0][0], maxbinnum=macro_bins[-1][1])
    E_MIN, E_MAX = emin[0], emax[-1]

    logger.info('Checking PSF file...')
    PSF_FILE = kwargs['psffile']
    if PSF_FILE is not None:
        logger.info('Using %s'%PSF_FILE)
    else:
        logger.info('ATT: File not found: %s'%PSF_FILE)
        logger.info('Creating PSF file with gtpsf...')
        try:
            IRFS = kwargs['irfs']
        except:
            logger.info('ERROR: provide IRFS!')
            sys.exit()
        try:
            EVTYPE = kwargs['evtype']
        except:
            logger.info('ERROR: provide event type!')
            sys.exit()
        try:
            LT_FILE = data.LT_FILE
        except:
            LT_FILE = ''
        if os.path.exists(LT_FILE):
            logger.info('Livetime file found.')
        else:
            try:
                LT_FILE = kwargs['ltfile']
            except:
                logger.info('ERROR: provide livetime file or list!')
                sys.exit()
            if LT_FILE.lower().endswith(('.txt', '.dat')):
                lt_dict = {'infile1' : LT_FILE,
                           'outfile' : 'DEFAULT',
                           'chatter': 4,
                           'clobber': 'no'}
                from Xgam.utils.ScienceTools_ import gtltsum
                label_lt = IRFS + '_evt' + str(EVTYPE)
                out_gtltsum = gtltsum(label_lt,lt_dict)
                expcube = out_gtltsum
            else:
                expcube = LT_FILE
            
        from Xgam.utils.ScienceTools_ import gtpsf
        label_psf =  IRFS + '_evt' + str(EVTYPE)
        psf_dict = {'expcube' : expcube,
                    'outfile' : 'DEFAULT',
                    'irfs' : IRFS,
                    'evtype' : EVTYPE,
                    'ra' : 45.0,
                    'dec' : 45.0,
                    'emin' : E_MIN,
                    'emax' : E_MAX,
                    'nenergies' : 500,
                    'thetamax' : 30.0,
                    'ntheta' : 100,
                    'chatter': 4,
                    'clobber': 'no'}
        PSF_FILE = gtpsf(label_psf, psf_dict)

    #NOTE: IMPLEMENT TYPE2 ??
    if kwargs['typesrcmask'] == 1:
        from Xgam.utils.mkmask_ import mask_src_fluxPSFweighted_1 as mask_src
    else:
        from Xgam.utils.mkmask_ import mask_src_fluxPSFweighted_2 as mask_src
    
    from Xgam.utils.mkmask_ import mask_gp
    from Xgam.utils.parsing_ import get_psf_en_univariatespline

    nside = kwargs['nside']
    out_label = kwargs['outflabel']
    npix = hp.nside2npix(nside)
    src_cat = kwargs['srccat']
    src_ext_cat = kwargs['srcextcat']
    out_name_list = os.path.join(X_OUT, 'fits/MaskSmart_'+out_label+'_GAL_list.txt')
    out_list = []

    for i, (minb, maxb) in enumerate(macro_bins):
        logger.info('Considering bins from %i to %i...' %(minb, maxb))
        emin, emax, emean = get_energy_from_fits(micro_bin_file, minbinnum=minb, maxbinnum=maxb)
        E_MIN, E_MAX = emin[0], emax[-1]
        E_MEAN = np.sqrt(emax[0]*emin[-1])
        logger.info('Energies %.1f - %.1f [MeV]' %(E_MIN, E_MAX))

        if kwargs['coord']=='CEL':
            out_name = os.path.join(X_OUT, 'fits/MaskSmart_'+out_label+'_CEL_%i.fits'%(E_MIN))
        else:
            out_name = os.path.join(X_OUT, 'fits/MaskSmart_'+out_label+'_GAL_%i.fits'%(E_MIN))
        out_list.append(out_name)
        if os.path.exists(out_name) and not kwargs['overwrite']:
            logger.info('ATT: %s already exists! \n'%out_name)
        else:
            bad_pix = []
            mask = np.ones(npix)
            bad_pix += mask_gp(kwargs['gpcut'], nside)
            psf_spline = get_psf_en_univariatespline(PSF_FILE)
            bad_pix += mask_src(src_cat, src_ext_cat, psf_spline, E_MIN, nside)

            for bpix in np.unique(bad_pix):
                mask[bpix] = 0
            if not os.path.exists(os.path.join(X_OUT, 'fits')):
                os.system('mkdir %s' %os.path.join(X_OUT, 'fits'))
            fsky = 1-(len(np.unique(bad_pix))/float(npix))
            logger.info('fsky = %.3f'%fsky)
            if kwargs['coord']=='CEL':
                from Xgam.utils.PolSpice_ import change_coord
                mask = change_coord(mask, ['G','C'])
                logger.info('Changing coordinate system to celestial...')
            hp.write_map(out_name, mask, coord='G', overwrite=True)
            logger.info('Created %s \n' %out_name)

        if kwargs['show'] == True:
            import matplotlib.pyplot as plt
            hp.mollview(mask, cmap='bone')
            plt.show()
    
    if kwargs['coord']=='CEL':
        out_name_list = out_name_list.replace('GAL', 'CEL')

    logger.info('Writing list of output files: %s'%out_name_list)
    np.savetxt(out_name_list, out_list, fmt='%s')
    logger.info('Done!')
コード例 #20
0
def mkAuto(**kwargs):
    """                                      
    """
    get_var_from_file(kwargs['config'])

    maps_ = data.MAPS_LIST
    masks_ = data.MASKS_LIST
    fermi_wb_matrix = data.FERMI_WBEAM_MATRIX
    lss_wb_ = data.LSS_TRACER_WBEAM_LIST
    out_label = data.OUT_LABEL
    gamma = data.GAMMA
    l_max_ = data.MAX_APS_MULTIPOLE_FIT
    lmax = data.BINNING_MAX_MULTIPOLE
    lmin = data.BINNING_MIN_MULTIPOLE
    bin_num = data.BINNING_MULTIPOLE_NBIN
    bin_alg = data.BINNING_MULTIPOLE_ALGORITHM
    bin_custom = data.BINNING_CUSTOM
    fit_function = data.FIT_FUNCTION
    cn_list = data.FERMI_CN_LIST
    fit_guess = data.FIT_GUESS
    
    def const_fit_function(x, k):
        return k
    
    if type(maps_) == str and maps_.endswith(('.txt','.dat')):
        maps_ = open(maps_, 'r')
        maps_ = maps_.read().splitlines()
    
    if type(masks_) == str and masks_.endswith(('.txt','.dat')):
        masks_ = open(masks_, 'r')
        masks_ = masks_.read().splitlines()

    logger.info('Starting ...')
    cl_txt_f = os.path.join(X_OUT,'%s_autocorrelation.txt'%(out_label))
    if os.path.exists(cl_txt_f):
        logger.info('ATT: Output file already exists!')
        logger.info(cl_txt_f)
        if not kwargs['overwrite']:
            sys.exit()
        else:
            logger.info('ATT: Overwriting files!')
            
    cl_txt = open(cl_txt_f, 'w')
    emin_, emax_, wl_, fsky_ = [], [], [], []
    logger.info('----- AUTO -----')
    for i, m1_f in enumerate(maps_):
        l_max = l_max_[i]
        logger.info('Considering map: %s'%m1_f)
        m1 = hp.read_map(m1_f)
        
        if len(masks_) == len(maps_):
            mask_f = masks_[i]
        elif len(masks_) == 1:
            mask_f = masks_[0]
        else:
            logger.info('ERROR: Inconsistent number of masks!'+
                         'It should be either 1 or the same number as Fermi maps!')
            sys.exit()
        logger.info('Considering Fermi mask: %s'%mask_f)
        mask = hp.read_map(mask_f)
        
        m1_masked = hp.ma(m1)
        m1_masked.mask = np.logical_not(mask)
        
        logger.info('Computing the pixel window function ...')
 
        nside = hp.npix2nside(len(m1))
        wpix = hp.wpix = hp.sphtfunc.pixwin(nside, lmax=lmax-1)
        cn = cn_list[i]
        fsky = np.sum(mask)/len(mask)
        fsky_.append(fsky)
		
        if fermi_wb_matrix is not None:
            logger.info('Computing the fermi beam window function ...')
            spec = get_powerlaw_spline(gamma)
            emin_str = re.search(r'%s'%EMIN_STR, m1_f)
            emin = float(emin_str.group(0).replace('-',''))
            emax_str = re.search(r'%s'%EMAX_STR, m1_f)
            emax = float(emax_str.group(0).replace('.fits', ''))
            wb_spline = get_1D_wbeam(fermi_wb_matrix, spec, e_min=emin, e_max=emax)
            wb = wb_spline(np.arange(lmax))
            emin_.append(emin)
            emax_.append(emax)
            
            if kwargs['show']:
                wb_spline.plot(show=False, label='%.2f-%.2f GeV'%(emin/1000, emax/1000), color='0.3')
                plt.ylabel('W$_{beam}$', size=15)
                plt.xlabel('$l$', size=15)
                plt.legend(loc=1, fontsize=14)
                plt.grid('off')
            
        # ------- TO BE DEFINED ------
        if lss_wb_ is not None:
            logger.info('Getting the LSS beam window function ...')
            lss_wb = np.ones(len(wb))
        
        cl_txt.write('# ********************************\n')
        cl_txt.write('# AUTOCORR ---> %i x %i\n'%(i, i))
        

        if lss_wb_ is None:
            wl = wb * wpix
            wl_.append(wl)
        else:
            # ------- TO BE DEFINED ------
            wl = lss_wb * wpix
            wl_.append(wl)
            
        out_folder =  os.path.join(X_OUT, 'output_pol')
        if not os.path.exists(out_folder):
            os.makedirs(out_folder)
        pol_dict = data.POLCEPICE_DICT
        for key in pol_dict:
            if key == 'clfile':
                pol_dict[key] = os.path.join(out_folder,'%s_%i_cl.txt'%(out_label, i))
            if key == 'cl_outmap_file':
                pol_dict[key] = os.path.join(out_folder,'%s_%i_clraw.txt'%(out_label, i))
#             if key == 'covfileout':
#                 pol_dict[key] = os.path.join(out_folder,'%s_%i_cov.fits'%(out_label, i))
            if key == 'mapfile':
                pol_dict[key] = m1_f
            if key == 'maskfile':
                pol_dict[key] = mask_f
            if key == 'mapfile2':
                pol_dict[key] = 'NO'
            if key == 'maskfile2':
                pol_dict[key] = 'NO'
        config_file_name = os.path.join(out_folder, 'pol_autoconfig_%s_%i.txt'%(out_label, i))
       
        _l, _cl, _cl_err = pol_cl_calculation(pol_dict, config_file_name, cov=False,
                                                        wl_array=wl, cn=cn, rebin=True,
                                                        nbin=bin_num, bin_type=bin_alg,
                                                        lmin=lmin, lmax=lmax,
                                                        custom_bins=bin_custom)
                                                                
        fit_idx = np.where((_l >= 30)&(_l <= l_max))[0]

        try: 
            popt, pcov = curve_fit(fit_function, _l[fit_idx], _cl[fit_idx], sigma=_cl_err[fit_idx],
                                   bounds=([0, 0, -1e-12], [1e-12, 5, 1e-12]), absolute_sigma=True)
                                   
            chi2 = np.sum(((_cl[fit_idx] - fit_function(_l[fit_idx], *popt))/_cl_err[fit_idx])**2)
            chi2red = chi2/len(_cl[fit_idx])
            
            logger.info('\n')
            logger.info('********************************')
            logger.info('Multipole fit range: %i - %i'%(30, l_max))
            logger.info('Best fit param: %s'%str(popt))
            logger.info('Chi2/dof = %.3f'%chi2red)
            logger.info('********************************\n')
            
            #cl_domain = np.where(_cl[fit_idx] > cn)[0]
            #if len(cl_domain) == 0: 
            if chi2red < 3:
                cl_txt.write('Cn : %s\n'%str(cn))
                cl_txt.write('Multipole fit range: %i - %i\n'%(30, l_max))
                cl_txt.write('Best fit params = %s\n'%str(popt))
                cl_txt.write('Chi2/dof = %.3f\n'%chi2red)
                
            else:
                logger.info('ATT: repeating fit with a constant')
                popt, pcov = curve_fit(const_fit_function, _l[fit_idx], _cl[fit_idx], sigma=_cl_err[fit_idx],
                                   bounds=([-1e-12], [1e-12]), absolute_sigma=True)
                                   
                chi2 = np.sum(((_cl[fit_idx] - const_fit_function(_l[fit_idx], *popt))/_cl_err[fit_idx])**2)
                chi2red_k = chi2/len(_cl[fit_idx])
                cl_txt.write('Cn : %s\n'%str(cn))
                cl_txt.write('Multipole fit range: %i - %i\n'%(30, l_max))
                cl_txt.write('Best fit constant : %s\n'%str(popt))
                cl_txt.write('Chi2/dof = %.3f\n'%chi2red_k)
             
            
            plt.figure()
            plt.errorbar(_l[fit_idx], _cl[fit_idx], yerr=_cl_err[fit_idx], fmt='.', 
                                            label='%.2f-%.2f GeV'%(emin/1000, emax/1000))
            if chi2red < 3.:
                plt.plot(_l[fit_idx], fit_function(_l[fit_idx], *popt), label='Best fit ($\chi^2_{/dof}$=%.2f)'%chi2red)
            else:
                plt.plot(_l[fit_idx], np.full(len(_l[fit_idx]), popt[0]), label='Best fit ($\chi^2_{/dof}$=%.2f)'%chi2red_k)
            plt.hlines(cn, _l[fit_idx][0], _l[fit_idx][-1], linestyle='--', color='0.3', label='C$_N$')
            if bin_alg == 'log':
                plt.xscale('log')
            plt.xlabel('$\ell$')
            plt.ylabel('$C_{\ell}$')
            plt.legend()
            plt.savefig('output/figs/Autocorr_%ix%i'%(i,i))
                 
            if kwargs['show']:  
                plt.show()
             
        except RuntimeError:
            logger.info('\n')
            logger.info('********************************')
            logger.info("Error - curve_fit failed")
            logger.info('********************************\n')
            cl_txt.write('Cn : %s\n'%str(cn))
            cl_txt.write('Error - curve_fit failed\n')

            plt.figure()
            plt.errorbar(_l[fit_idx], _cl[fit_idx], yerr=_cl_err[fit_idx], fmt='.')
            if bin_alg == 'log':
                plt.xscale('log')    
            plt.show()
    
    
    cl_txt.write('\n\n\n')
    
    logger.info('----- CROSS -----')
    
    for i, m1_f in enumerate(maps_):
        wl1 = wl_[i]
        fsky1 = fsky_[i]
        l_max1 = l_max_[i]
        
        for j in range(i, len(maps_)):
            wl2 = wl_[j]
            m2_f = maps_[j]
            fsky2 = fsky_[j]
            l_max2 = l_max_[j]
            
            if m1_f == m2_f:
               continue
            if fsky1 <= fsky2:
                mask_f = masks_[2]
            else:
                mask_f = masks_[2]
                
            out_folder =  os.path.join(X_OUT, 'output_pol')
            if not os.path.exists(out_folder):
                os.makedirs(out_folder)
                
            pol_dict = data.POLCEPICE_DICT
            for key in pol_dict:
                if key == 'clfile':
                    pol_dict[key] = os.path.join(out_folder,'%s_%i%i_cl.txt'%(out_label, i, j))
                if key == 'cl_outmap_file':
                    pol_dict[key] = os.path.join(out_folder,'%s_%i%i_clraw.txt'%(out_label, i, j))
                if key == 'mapfile':
                    pol_dict[key] = m1_f
                if key == 'maskfile':
                    pol_dict[key] = mask_f
                if key == 'mapfile2':
                    pol_dict[key] = m2_f
                if key == 'maskfile2':
                    pol_dict[key] = mask_f
            config_file_name = os.path.join(out_folder, 'pol_autocrossconfig_%s_%i%i.txt'%(out_label, i, j))
       
            wl = np.sqrt(wl1*wl2)
            _l, _cl, _cl_err = pol_cl_calculation(pol_dict, config_file_name, cov=False,
                                                        wl_array=wl, rebin=True,
                                                        nbin=bin_num, bin_type=bin_alg,
                                                        lmin=lmin, lmax=lmax,
                                                        custom_bins=bin_custom)
            
            cl_txt.write('# ********************************\n')
            cl_txt.write('# CROSSCORR ---> %i x %i\n'%(i, j))
            
            if l_max1 <= l_max2:
                l_max = l_max1
            else:
                l_max = l_max2
            
            fit_idx = np.where((_l >= 30)&(_l <= l_max))[0]

            try: 
                popt, pcov = curve_fit(const_fit_function, _l[fit_idx], _cl[fit_idx], sigma=_cl_err[fit_idx],
                                    #bounds=([0, 0, -1e-12], [1e-12, 3, 1e-12]), absolute_sigma=True)
                                    bounds=([-1e-15], [1e-15]), absolute_sigma=True)
                                    
                chi2 = np.sum(((_cl[fit_idx] - const_fit_function(_l[fit_idx], *popt))/_cl_err[fit_idx])**2)
                chi2red = chi2/len(_cl[fit_idx])
                                    
                logger.info('\n')
                logger.info('********************************')
                logger.info('Multipole fit range: %i - %i'%(30, l_max))
                logger.info('Best fit param: %s'%str(popt))
                logger.info('Chi2/dof = %.3f'%chi2red)
                logger.info('********************************\n')
                cl_txt.write('Cn : 0\n')
                cl_txt.write('Multipole fit range: %i - %i\n'%(30, l_max))
                cl_txt.write('Best fit params : %s\n'%str(popt))
                #cl_txt.write('Chi2/dof = %.3f\n'%chi2red)
             
            
                plt.figure()
                plt.errorbar(_l[fit_idx], _cl[fit_idx], yerr=_cl_err[fit_idx], fmt='.', 
                                            label='(%.2f-%.2f)GeV x (%.2f-%.2f)GeV'%(emin_[i]/1000, emax_[i]/1000, 
                                                                   emin_[j]/1000, emax_[j]/1000))
                #plt.plot(_l[fit_idx], fit_function(_l[fit_idx], *popt))
                plt.plot(_l[fit_idx], np.full(len(_l[fit_idx]), popt[0]), label='Best fit ($\chi^2_{/dof}$=%.2f)'%chi2red_k)
                if bin_alg == 'log':
                    plt.xscale('log')
                plt.xlabel('$\ell$')
                plt.ylabel('$C_{\ell}$')
                plt.legend()
                plt.savefig('output/figs/Autocorr_%ix%i'%(i,j))
                 
                if kwargs['show']:  
                    plt.show()
             
            except RuntimeError:
                logger.info('\n')
                logger.info('********************************')
                logger.info("Error - curve_fit failed")
                logger.info('********************************\n')
                cl_txt.write('Error - curve_fit failed\n')

            
    cl_txt.close()

    logger.info('Created %s'%cl_txt_f)
コード例 #21
0
def mkSTanalysis(**kwargs):
    """Science Tools analysis chain
    """
    assert (kwargs['config'].endswith('.py'))
    get_var_from_file(kwargs['config'])
    logger.info('Starting ST analysis...')

    from Xgam.utils.ScienceTools_ import mergeft1
    PH, SC = 'photon', 'spacecraft'
    PH_FOLDER = os.path.join(FT_DATA_FOLDER, PH)
    SC_FOLDER = os.path.join(FT_DATA_FOLDER, SC)
    start_week, end_week = data.START_WEEK, data.STOP_WEEK
    logger.info('Taking data from week %i to week %i' % (start_week, end_week))
    FT1_FILE = mergeft1(PH_FOLDER, 'FT1_w%i-%i.txt'%(start_week, end_week), \
                           start_week, end_week)
    #FT2_FILE = mergeft2(SC_FOLDER, 'FT2_w%i-%i.txt'%(start_week, end_week), \
    #                       start_week, end_week)
    out_label = data.OUT_LABEL
    txt_out_files = open('output/' + out_label + '_outfiles.txt', 'w')

    from Xgam.utils.ScienceTools_ import gtselect
    gtselect_dict = data.GTSELECT_DICT
    if gtselect_dict['infile'] == 'DEFAULT':
        gtselect_dict['infile'] = FT1_FILE
    out_gtselect = gtselect(out_label, gtselect_dict)
    txt_out_files.write(out_gtselect + '\n')

    from Xgam.utils.ScienceTools_ import gtmktime
    gtmktime_dict = data.GTMKTIME_DICT
    if gtmktime_dict['evfile'] == 'DEFAULT':
        gtmktime_dict['evfile'] = out_gtselect
    if gtmktime_dict['scfile'] == 'DEFAULT':
        gtmktime_dict['scfile'] = FT2_FILE
    out_gtmktime = gtmktime(out_label, gtmktime_dict)
    txt_out_files.write(out_gtmktime + '\n')

    from Xgam.utils.ScienceTools_ import gtbin
    gtbin_dict = data.GTBIN_DICT
    if gtbin_dict['evfile'] == 'DEFAULT':
        gtbin_dict['evfile'] = out_gtmktime
    if gtbin_dict['scfile'] == 'DEFAULT':
        gtbin_dict['scfile'] = FT2_FILE
    out_gtbin = gtbin(out_label, gtbin_dict)
    txt_out_files.write(out_gtbin + '\n')

    if kwargs['gtltcube'] == True:
        from Xgam.utils.ScienceTools_ import gtltcube
        gtltcube_dict = data.GTLTCUBE_DICT
        if gtltcube_dict['evfile'] == 'DEFAULT':
            gtltcube_dict['evfile'] = out_gtmktime
        if gtbin_dict['scfile'] == 'DEFAULT':
            gtbin_dict['scfile'] = FT2_FILE
        out_gtltcube = gtltcube(out_label, gtltcube_dict)
        txt_out_files.write(out_gtltcube + '\n')
    else:
        logger.info('Not running gtltcube.')
        pass

    from Xgam.utils.ScienceTools_ import gtexpcube2
    gtexpcube2_dict = data.GTEXPCUBE2_DICT
    if gtexpcube2_dict['infile'] == 'DEFAULT':
        gtexpcube2_dict['infile'] = out_gtltcube
    if gtexpcube2_dict['cmap'] == 'DEFAULT':
        gtexpcube2_dict['cmap'] = out_gtbin
    out_gtexpcube2 = gtexpcube2(out_label, gtexpcube2_dict)
    txt_out_files.write(out_gtexpcube2 + '\n')
    txt_out_files.close()
    logger.info('Created output/' + out_label + '_outfiles.txt')
    logger.info('Done!')
コード例 #22
0
ファイル: APSviewer.py プロジェクト: nmik/Xgam
def aps_view(**kwargs):
    """
    Viewer interface for APS and Covariance matrix                                                                                                                                                             
    """

    params = parse_polspice_aps(kwargs['infile'])
    _emin, _emax = params[0], params[1]
    _l, _cl, _clerr = params[2], params[3], params[4]
    _cov_ = params[5]

    logger.info('Generating APS plots...')
    for i, cl in enumerate(_cl):

        sig = cl / _clerr[i]
        print('-----------------------')
        print('Detection Significance:')
        print(sig)
        print('-----------------------')

        aps_label = '%.1f-%.1f GeV' % (_emin[i] / 1000, _emax[i] / 1000)
        fig = plt.figure()
        ax = fig.add_subplot(111)
        plt.plot([0, 2000], [0, 0], '--', color='silver')
        plt.errorbar(_l[i],
                     cl,
                     fmt='o',
                     markersize=5,
                     elinewidth=2,
                     label=aps_label,
                     color='red',
                     yerr=_clerr[i])
        plt.xlim(np.amin(_l[i]), np.amax(_l[i]) + 1)
        plt.xscale(kwargs['xscale'])
        plt.xlabel('Multipole', size=15)
        plt.ylabel('C$^{sig}_{\ell}$ [(cm$^{-2}$s$^{-1}$sr$^{-1}$)sr]',
                   size=15)
        plt.title(kwargs['title'])
        plt.tight_layout()
        plt.legend(loc=1, fontsize=16)

    logger.info('Generating Covariance matrices plots...')
    for k, cov in enumerate(_cov_):

        _cov2plotij_ = []
        for i in range(0, len(cov) - 1):
            sigii = cov[i][i]
            _cov2plotj = []
            for j in range(0, len(cov[0]) - 1):
                sigjj = cov[j][j]
                sigij = cov[j][i]
                if sigij < 0:
                    sigij = 1e-100
                _cov2plotj.append(np.sqrt(sigij / np.sqrt(sigii * sigjj)))
            _cov2plotij_.append(_cov2plotj)
        _cov2plotij_ = np.array(_cov2plotij_)

        fig = plt.figure()
        ax = fig.add_subplot(111)
        cax = ax.matshow(_cov2plotij_,
                         origin='lower',
                         aspect='auto',
                         cmap='viridis',
                         extent=[_l[k][0], _l[k][-1], _l[k][0], _l[k][-1]])
        ax.xaxis.set_ticks_position('bottom')
        plt.title(kwargs['title'] + ' (%.1f-%.1f GeV)' %
                  (_emin[k] / 1000, _emax[k] / 1000))
        plt.xlabel('Multipole $\ell_{i}$', size=15)
        plt.ylabel('Multipole $\ell_{j}$', size=15)
        cbar = plt.colorbar(cax)
        cbar.set_label('$\sigma_{ij}/\sqrt{\sigma_{ii}\sigma_{jj}}$', size=15)
        #plt.tight_layout()
        plt.grid(False)

    plt.show()
コード例 #23
0
def mkMask(**kwargs):
    """Routine to produce sky maps (limited edition)
    """
    logger.info('Starting mask production...')
    get_var_from_file(kwargs['config'])
    bad_pix = []
    nside = data.NSIDE
    out_label = data.OUT_LABEL
    npix = hp.nside2npix(nside)
    mask = np.ones(npix)
    if kwargs['srcmask'] == True:
        from Xgam.utils.mkmask_ import mask_src
        src_mask_rad = data.SRC_MASK_RAD
        cat_file = data.SRC_CATALOG
        flux_th = kwargs['srcfluxthreshold']
        bad_pix += mask_src(cat_file, src_mask_rad, nside, FLUX_THRESHOLD=flux_th)
    if kwargs['galsrcmask'] == True:
        from Xgam.utils.mkmask_ import mask_galactic_src
        cat_file = data.SRC_CATALOG
        bad_pix += mask_galactic_src(cat_file, 2., nside)
    if kwargs['extsrcmask'] == True:
        from Xgam.utils.mkmask_ import mask_extsrc
        src_mask_rad = data.SRC_MASK_RAD
        cat_file = data.EXTSRC_CATALOG
        bad_pix += mask_extsrc(cat_file, nside)
    if kwargs['gpmask'] == 'flat':
        from Xgam.utils.mkmask_ import mask_gp
        gp_mask_lat = data.GP_MASK_LAT
        bad_pix += mask_gp(gp_mask_lat, nside)
    if kwargs['srcweightedmask'] == True:
    	from Xgam.utils.mkmask_ import mask_src_fluxPSFweighted_1
    	from Xgam.utils.parsing_ import get_psf_en_univariatespline
    	cat_file = data.SRC_CATALOG
    	ext_cat_file = data.EXTSRC_CATALOG
    	psf_f = data.PSF_FILE
    	energy = data.ENERGY
    	psf_spline = get_psf_en_univariatespline(psf_f)
    	bad_pix += mask_src_fluxPSFweighted_1(cat_file, ext_cat_file, psf_spline, energy, nside)
    if kwargs['srcweightedmask2'] == True:
    	from Xgam.utils.mkmask_ import mask_src_fluxPSFweighted_2
    	from Xgam.utils.parsing_ import get_psf_en_univariatespline
    	cat_file = data.SRC_CATALOG
    	ext_cat_file = data.EXTSRC_CATALOG
    	psf_f = data.PSF_FILE
    	e_min = data.E_MIN
    	e_max = data.E_MAX
    	psf_spline = get_psf_en_univariatespline(psf_f)
    	bad_pix += mask_src_fluxPSFweighted_2(cat_file, ext_cat_file, psf_spline, e_min, e_max, nside)
    if kwargs['northmask'] == True:
        from Xgam.utils.mkmask_ import mask_north
        north_lat = data.NORTH_LAT
        bad_pix += mask_north(north_lat, nside)
    if kwargs['southmask'] == True:
        from Xgam.utils.mkmask_ import mask_south
        south_lat = data.SOUTH_LAT
        bad_pix += mask_south(south_lat, nside)
    for bpix in np.unique(bad_pix):
        mask[bpix] = 0
    if not os.path.exists(os.path.join(X_OUT, 'fits')):
    	os.system('mkdir %s' %os.path.join(X_OUT, 'fits'))
    	
    out_name = os.path.join(X_OUT, 'fits/'+out_label+'_GAL.fits')
    fsky = 1-(len(np.unique(bad_pix))/float(npix))
    logger.info('fsky = %.3f'%fsky)
    
    if kwargs['coord']=='CEL':
        from Xgam.utils.PolSpice_ import change_coord
        mask = change_coord(mask, ['G','C'])
        hp.write_map(out_name.replace('GAL', 'CEL'), mask, coord='C')
        logger.info('Created %s' %out_name.replace('GAL', 'CEL'))
    else:
        hp.write_map(out_name, mask, coord='G')
        logger.info('Created %s' %out_name)

    if kwargs['show'] == True:
    	import matplotlib.pyplot as plt
    	hp.mollview(mask, cmap='bone')
    	plt.show()
コード例 #24
0
ファイル: foregroundfit_.py プロジェクト: nmik/Xgam
def main():
    """Test smodule.
    
    """
    logger.info('No test module is available at the moment... bye bye!')
    return 0
コード例 #25
0
ファイル: foregroundfit_.py プロジェクト: nmik/Xgam
def get_fore_integral_flux_map(fore_files_list, e_min, e_max):
    """
    
    A powerlaw is assumed for the foreground energy spectrum, hence
    the interpolation between 2 given maps at given energies (given
    by the model) is done in logarithmic scales.

    Parameters
    ----------
    fore_files_list: list of str
           Ordered list of the foreground files (one for each energy)
    e_min: float
           the min of the energy bin
    e_max: float
           the max of the energy bin
           
    Returns
    -------
    array
        foreground map integrated between e_min and e_max
        
    """
    fore_en = []
    for ff in fore_files_list:
        m = re.search(FORE_EN, ff)
        en = int(m.group(0).replace('_', '').replace('.', ''))
        fore_en.append(en)
    fore_en = np.array(fore_en)
    out_name = fore_files_list[0].replace('_%i.fits' % fore_en[0],
                                          '_%d-%d.fits' % (e_min, e_max))
    if os.path.exists(out_name):
        logger.info('ATT: file %s already exists and returned...' % out_name)
        fore_map = hp.read_map(out_name)
        return fore_map
    else:
        logger.info('Computing the integral flux of the foreground model...')
        logger.info('...between %.2f - %.2f' % (e_min, e_max))
        fore_emin_sx, fore_emin_dx = find_outer_energies(e_min, fore_en)
        fore_emax_sx, fore_emax_dx = find_outer_energies(e_max, fore_en)
        fore_emin_sx_ind = np.where(fore_en == fore_emin_sx)[0][0]
        fore_emin_dx_ind = np.where(fore_en == fore_emin_dx)[0][0]
        fore_emax_sx_ind = np.where(fore_en == fore_emax_sx)[0][0]
        fore_emax_dx_ind = np.where(fore_en == fore_emax_dx)[0][0]
        fore_fmin_sx = hp.read_map(fore_files_list[fore_emin_sx_ind])
        fore_fmin_dx = hp.read_map(fore_files_list[fore_emin_dx_ind])
        fore_fmax_sx = hp.read_map(fore_files_list[fore_emax_sx_ind])
        fore_fmax_dx = hp.read_map(fore_files_list[fore_emax_dx_ind])
        m1 = (np.log10(fore_fmin_sx)-np.log10(fore_fmin_dx))/ \
            (np.log10(fore_emin_sx)-np.log10(fore_emin_dx))
        m2 = (np.log10(fore_fmax_sx)-np.log10(fore_fmax_dx))/ \
            (np.log10(fore_emax_sx)-np.log10(fore_emax_dx))
        logfore1 = m1*(np.log10(e_min)-np.log10(fore_emin_sx))+ \
            np.log10(fore_fmin_sx)
        logfore2 = m2*(np.log10(e_max)-np.log10(fore_emax_sx))+ \
            np.log10(fore_fmax_sx)
        fore1 = 10**(logfore1)
        fore2 = 10**(logfore2)
        fore_integ_map = np.sqrt(fore1 * fore2) * (e_max - e_min)
        hp.write_map(out_name, fore_integ_map)
        logger.info('Created file %s' % out_name)
        return fore_integ_map
コード例 #26
0
ファイル: foregroundfit_.py プロジェクト: nmik/Xgam
def fit_foreground_poisson(fore_map,
                           data_map,
                           mask_map=None,
                           n_guess=1.,
                           c_guess=0.1,
                           exp=None,
                           smooth=False,
                           show=False):
    """
    Performs the poissonian fit, recursively computing the log likelihood
    (using poisson_likelihood) for a grid of values of fit parameters around
    the guess. Returns the values of parameters which minimize the log
    likelihood, togather to the 1-sigma error

    Parameters
    ----------
    n_guess : float
          initial guess for normalization parameter
    c_guess : float
          initial guess for constant parameter
    fore_map : numpy array
          helapix map of foreground model
    data_map : numpy array
          helapix map of data. It could be either a count map or a flux map.
          If a counts map is given, an exposure map should be given too. See
          next parameter.
    exp :  numpy array or None
          helapix map of the exposure. Should be given if the data map is in
          counts (beacause foreground map is in flux units by default and it
          needs to be turned to counts to be fitted). While, If data map is
          in flux units, do not declare this parameter, which is None by
          default.
    smooth : bool
          not implemented yet...
    show : bool
          if true it shows some usefull plot to check if the fit is functioning
          
    Returns
    -------
    float, float, float, float, float, float
        In order: best fit N, best fit C, N's right error, N's left error, 
        C's right error, C's left error
    """
    #show=True
    #mask_map=None
    logger.info('Performing poissonian fit...')
    norm_guess = n_guess
    igrb_guess = c_guess
    nside_out = 64
    mask = 0.
    logger.info('N guess = %.2f - C guess = %.1e' % (norm_guess, igrb_guess))
    if mask_map is None:
        logger.info('fit outside default mask: 30deg gp, 2 deg srcs.')
        mask_f = os.path.join(X_CONFIG, 'Mask_hp64_src2_gp30.fits')
        mask = hp.read_map(mask_f)
    else:
        logger.info('fit outside mask given in config file.')
        mask = mask_map
        mask = hp.ud_grade(mask, nside_out=nside_out, power=-2)
        mask[np.where(mask != np.amax(mask))[0]] = 0
        mask[np.where(mask == np.amax(mask))[0]] = 1
    logger.info('down grade...')
    fore_repix = np.array(hp.ud_grade(fore_map, nside_out=nside_out))
    data_repix = np.array(hp.ud_grade(data_map, nside_out=nside_out, power=-2))
    _unmask = np.where(mask > 1e-30)[0]

    norm_list = np.linspace(norm_guess - 0.8, norm_guess + 0.8, 200)
    igrb_list = np.logspace(np.log10(igrb_guess * 0.01),
                            np.log10(igrb_guess * 10), 200)

    logger.info('-------------------------------')
    logger.info('Minimization likelihood run1...')
    lh_list = []
    combinations = list(product(norm_list, igrb_list))
    if exp is not None:
        exposure = exp
        exposure = np.array(hp.ud_grade(exposure, nside_out=nside_out))
        areapix = 4 * np.pi / (len(data_repix))
        for i, j in product(norm_list, igrb_list):
            lh = poisson_likelihood(i,
                                    j,
                                    fore_repix[_unmask],
                                    data_repix[_unmask],
                                    exp=exposure[_unmask],
                                    sr=areapix)
            lh_list.append(lh)
    else:
        for i, j in product(norm_list, igrb_list):
            lh = poisson_likelihood(i, j, fore_repix[_unmask],
                                    data_repix[_unmask])
            lh_list.append(lh)

    lh_list = np.array(lh_list)
    lh_matrix = lh_list.reshape(len(norm_list), len(igrb_list))
    prof_lh_norm, prof_lh_igrb = get_2params_profile_likelihood(
        lh_matrix, norm_list, igrb_list)

    nn = np.linspace(np.amin(norm_list), np.amax(norm_list), 1000)
    cc = np.linspace(np.amin(igrb_list), np.amax(igrb_list), 1000)

    lh_min = np.amin(prof_lh_norm(nn))
    logger.info('Minimum -LogL = %s' % lh_min)

    norm_min = nn[np.argmin(prof_lh_norm(nn))]
    igrb_min = cc[np.argmin(prof_lh_igrb(cc))]
    logger.info('Run1 results: n=%.3f c=%e' % (norm_min, igrb_min))

    norm_sxerr, norm_dxerr = get_param_error(prof_lh_norm, nn, lh_delta=2.3)
    logger.info('Norm err: %.4f - %.4f' % (norm_sxerr, norm_dxerr))
    igrb_sxerr, igrb_dxerr = get_param_error(prof_lh_igrb, cc, lh_delta=2.3)
    logger.info('Igrb err: %.2e - %.2e' % (igrb_sxerr, igrb_dxerr))
    """
    logger.info('-------------------------------')
    logger.info('Minimization likelihood run2...')
    norm_list = np.linspace(norm_min-0.3, norm_min+0.3, 100)
    igrb_list = np.linspace(igrb_min*0.1, igrb_min*10, 200)
    lh_list = []
    combinations = np.array(list(product(norm_list, igrb_list)))
    if exp is not None:
        exposure = exp
        exposure = np.array(hp.ud_grade(exposure, nside_out=nside_out))
        areapix = 4*np.pi/(len(data_repix))
        for i,j in product(norm_list, igrb_list):
            lh = poisson_likelihood(i, j, fore_repix[_unmask],
                                    data_repix[_unmask],
                                    exp=exposure[_unmask],
                                    sr=areapix)
            lh_list.append(lh)
    else:
        for i,j in product(norm_list, igrb_list):
            lh = poisson_likelihood(i, j, fore_repix[_unmask],
                                    data_repix[_unmask])
            lh_list.append(lh)
            
    lh_list = np.array(lh_list)
    lh_matrix = lh_list.reshape(len(norm_list), len(igrb_list))
    prof_lh_norm, prof_lh_igrb = get_2params_profile_likelihood(lh_matrix, norm_list, igrb_list)
    
    nn = np.linspace(np.amin(norm_list), np.amax(norm_list), 500)
    cc = np.linspace(np.amin(igrb_list), np.amax(igrb_list), 1000)
    
    lh_min = np.amin(prof_lh_norm(nn))
    lh_delta = lh_min+2.3
    logger.info('Minimum -LogL = %s'%lh_min)
    
    norm_min = nn[np.argmin(prof_lh_norm(nn))]
    igrb_min = cc[np.argmin(prof_lh_igrb(cc))]
    logger.info('Run2 results: n=%.3f c=%e'%(norm_min, igrb_min))
    
    norm_sxerr, norm_dxerr = get_param_error(prof_lh_norm, nn, lh_delta)
    logger.info('Norm err: %.4f - %.4f'%(norm_sxerr, norm_dxerr))
    igrb_sxerr, igrb_dxerr = get_param_error(prof_lh_igrb, cc, lh_delta)
    logger.info('Norm err: %.4f - %.4f'%(igrb_sxerr, igrb_dxerr))
    """
    if show == True:

        plt.figure(facecolor='white')
        plt.plot(nn, prof_lh_norm(nn), '-', color='black')
        plt.plot([norm_min, norm_min], [lh_min - 10, lh_min + 40], color='red')
        plt.plot([norm_sxerr, norm_sxerr], [lh_min - 2, lh_min + 40],
                 'r--',
                 alpha=0.7)
        plt.plot([norm_dxerr, norm_dxerr], [lh_min - 2, lh_min + 40],
                 'r--',
                 alpha=0.7)
        plt.xlabel('Normalization')
        plt.ylabel('-Log(Likelihood)')
        plt.ylim(lh_min - 5, lh_min + 30)
        plt.xlim(norm_min - 0.2, norm_min + 0.2)

        plt.figure(facecolor='white')
        plt.plot(cc, prof_lh_igrb(cc), '-', color='black')
        plt.plot([igrb_min, igrb_min], [lh_min - 10, lh_min + 40], color='red')
        plt.plot([igrb_sxerr, igrb_sxerr], [lh_min - 2, lh_min + 40],
                 'r--',
                 alpha=0.7)
        plt.plot([igrb_dxerr, igrb_dxerr], [lh_min - 2, lh_min + 40],
                 'r--',
                 alpha=0.7)
        plt.xlabel('Constant')
        plt.ylabel('-Log(Likelihood)')
        plt.ylim(lh_min - 5, lh_min + 30)
        plt.xlim(igrb_min * 0.9, igrb_min * 1.1)
        plt.xscale('log')
        """
        fig = plt.figure(facecolor='white')
        ax = fig.add_subplot(111)
        
        x, y = np.mgrid(norm_list, igrb_list)
        X, Y = np.mgrid(nn, cc)
        print('---------------', lh_matrix.shape, X.shape, Y.shape)
        print('---------------', lh_matrix.shape, x.shape, y.shape)
        Z = griddata((x, y), lh_matrix, (X, Y), method='linear')


        contours = plt.contour(X, Y, Z, 20, colors='0.4')
        cax = ax.matshow(Z, origin='lower', cmap='RdGy',
                         extent=[np.amin(norm_list), np.amax(norm_list), 
                                np.amin(igrb_list), np.amax(igrb_list)], 
                         aspect='auto', alpha=0.5)
        plt.clabel(contours, inline=True, fontsize=8)
        plt.ylabel('C [cm$^{-2}$s$^{-1}$sr$^{-1}$]')
        plt.xlabel('N')
        ax.xaxis.set_ticks_position('bottom')
        plt.grid('off')
        cb = plt.colorbar(cax, format='$%.1e$')
        cb.set_label('-Log(Likelihood)', rotation=90)
        """
        plt.show()

    return norm_min, igrb_min, norm_sxerr, norm_dxerr, igrb_sxerr, igrb_dxerr
コード例 #27
0
def mkCross(**kwargs):
    """                                      
    """
    get_var_from_file(kwargs['config'])

    fermi_maps_ = data.FERMI_MAPS_LIST
    fermi_masks_ = data.FERMI_MASKS_LIST
    fermi_wb_matrix = data.FERMI_WBEAM_MATRIX
    lss_maps_ = data.LSS_TRACER_MAPS_LIST
    lss_masks_ = data.LSS_TRACER_MASK_LIST
    lss_wb_ = data.LSS_TRACER_WBEAM_LIST
    out_label = data.OUT_LABEL
    bin_label = data.BINNING_LABEL
    gamma = data.GAMMA
    l_max = data.MAX_APS_MULTIPOLE
    lmax = data.BINNING_MAX_MULTIPOLE
    lmin = data.BINNING_MIN_MULTIPOLE
    bin_num = data.BINNING_MULTIPOLE_NBIN
    bin_alg = data.BINNING_MULTIPOLE_ALGORITHM
    bin_custom = data.BINNING_CUSTOM

    if type(fermi_maps_) == str and fermi_maps_.endswith(('.txt', '.dat')):
        fermi_maps_ = open(fermi_maps_, 'r')
        fermi_maps_ = fermi_maps_.read().splitlines()

    if type(fermi_masks_) == str and fermi_masks_.endswith(('.txt', '.dat')):
        fermi_masks_ = open(fermi_masks_, 'r')
        fermi_masks_ = fermi_masks_.read().splitlines()

    if type(lss_maps_) == str and lss_maps_.endswith(('.txt', '.dat')):
        lss_maps_ = open(lss_maps_, 'r')
        lss_maps_ = lss_maps_.read().splitlines()

    if type(lss_masks_) == str and lss_masks_.endswith(('.txt', '.dat')):
        lss_masks_ = open(lss_masks_, 'r')
        lss_masks_ = lss_masks_.read().splitlines()

    logger.info('Starting Cross-Correlation analysis...')
    cl_txt_f = os.path.join(
        X_OUT, '%s_%s_crosscorrelation.txt' % (out_label, bin_label))
    if os.path.exists(cl_txt_f):
        logger.info('ATT: Output file already exists!')
        logger.info(cl_txt_f)
        if not kwargs['overwrite']:
            sys.exit()
        else:
            logger.info('ATT: Overwriting files!')

    cl_txt = open(cl_txt_f, 'w')

    for i, m1_f in enumerate(fermi_maps_):
        logger.info('Considering Fermi map: %s' % m1_f)
        fermi_map = hp.read_map(m1_f)

        if len(fermi_masks_) == len(fermi_maps_):
            fermi_mask_f = fermi_masks_[i]
        elif len(fermi_masks_) == 1:
            fermi_mask_f = fermi_masks_[0]
        else:
            logger.info(
                'ERROR: Inconsistent number of masks!' +
                'It should be either 1 or the same number as Fermi maps!')
            sys.exit()
        logger.info('Considering Fermi mask: %s' % fermi_mask_f)
        fermi_mask = hp.read_map(fermi_mask_f)

        if len(lss_maps_) == len(fermi_maps_):
            m2_f = lss_maps_[i]
        elif len(lss_maps_) == 1:
            m2_f = lss_maps_[0]
        else:
            logger.info(
                'ERROR: Inconsistent number of LSS maps!' +
                'It should be either 1 or the same number as Fermi maps!')
            sys.exit()
        logger.info('Considering LSS map: %s' % m2_f)
        lss_map = hp.read_map(m2_f)

        if len(lss_masks_) == len(fermi_maps_):
            lss_mask_f = lss_masks_[i]
        elif len(lss_masks_) == 1:
            lss_mask_f = lss_masks_[0]
        else:
            logger.info(
                'ERROR: Inconsistent number of LSS masks!' +
                'It should be either 1 or the same number as Fermi maps!')
            sys.exit()
        logger.info('Considering LSS mask: %s' % lss_mask_f)
        lss_mask = hp.read_map(lss_mask_f)

        logger.info('Computing the pixel window function ...')
        if not len(fermi_map) == len(lss_map):
            logger.info('ERROR: Fermi and LSS maps have not the same NSIDE!')
            sys.exit()
        nside = hp.npix2nside(len(fermi_map))
        wpix = hp.wpix = hp.sphtfunc.pixwin(nside, lmax=l_max - 1)
        if len(wpix) < l_max:
            logger.info('-----------------------------')
            logger.info('ATT: Setting new l_max=%i ...' % len(wpix))
            logger.info('-----------------------------')
            l_max = len(wpix)

        logger.info('Computing the fermi beam window function ...')
        spec = get_powerlaw_spline(gamma)
        emin_str = re.search(r'%s' % EMIN_STR, m1_f)
        emin = float(emin_str.group(0).replace('-', ''))
        emax_str = re.search(r'%s' % EMAX_STR, m1_f)
        emax = float(emax_str.group(0).replace('.fits', ''))
        if fermi_wb_matrix:
            wb_spline = get_1D_wbeam(fermi_wb_matrix,
                                     spec,
                                     e_min=emin,
                                     e_max=emax)
            wb = wb_spline(np.arange(l_max))
        else:
            wb = np.ones(l_max)
        if kwargs['show']:
            wb_spline.plot(show=False,
                           label='%.2f-%.2f GeV' % (emin / 1000, emax / 1000),
                           color='0.3')
            plt.ylabel('W$_{beam}$', size=15)
            plt.xlabel('$l$', size=15)
            plt.legend(loc=1, fontsize=14)
            plt.grid('off')
            plt.show()

        # ------- TO BE DEFINED ------
        if lss_wb_ is not None:
            logger.info('Getting the LSS beam window function ...')
            lss_wb = lss_wb_[i][:l_max]
        else:
            lss_wb = np.ones(l_max)

        cl_txt.write('MAP1 ---> %s\n' % (m1_f))
        cl_txt.write('MAP2 ---> %s\n' % (m2_f))
        cl_txt.write('ENERGY\t %.2f %.2f \n' % (emin, emax))

        out_folder = os.path.join(X_OUT, 'output_pol')
        if not os.path.exists(out_folder):
            os.makedirs(out_folder)
        pol_dict = data.POLCEPICE_DICT
        for key in pol_dict:
            if key == 'clfile':
                pol_dict[key] = os.path.join(out_folder,
                                             '%s_%i_cl.txt' % (out_label, i))
            if key == 'corfile':
                pol_dict[key] = os.path.join(out_folder,
                                             '%s_%i_ccf.txt' % (out_label, i))
            if key == 'cl_outmap_file':
                pol_dict[key] = os.path.join(
                    out_folder, '%s_%i_clraw.txt' % (out_label, i))
            if key == 'covfileout':
                pol_dict[key] = os.path.join(out_folder,
                                             '%s_%i_cov.fits' % (out_label, i))
            if key == 'mapfile':
                pol_dict[key] = m1_f
            if key == 'mapfile2':
                pol_dict[key] = m2_f
            if key == 'maskfile':
                pol_dict[key] = fermi_mask_f
            if key == 'maskfile2':
                pol_dict[key] = lss_mask_f
        config_file_name = os.path.join(
            out_folder, 'pol_config_%s_%i.txt' % (out_label, i))

        if lss_wb_ is None:
            wl = np.sqrt(wb * wpix * wpix)
        else:
            # ------- TO BE DEFINED ------
            wl = np.sqrt(wb * lss_wb * wpix * wpix)

        if os.path.exists(
                os.path.join(out_folder, '%s_%i_cl.txt' % (out_label, i))):
            logger.info('Retrivnig APS and Covariance matrix ...')
            _l, _cl, _cl_err = pol_cl_parse(
                os.path.join(out_folder, '%s_%i_cl.txt' % (out_label, i)),
                os.path.join(out_folder, '%s_%i_cov.fits' % (out_label, i)),
                wl_array=wl,
                rebin=True,
                lmin=lmin,
                lmax=lmax,
                nbin=bin_num,
                bin_type=bin_alg,
                custom_bins=bin_custom,
                show=kwargs['show'])
            _cov = pol_cov_parse(os.path.join(
                out_folder, '%s_%i_cov.fits' % (out_label, i)),
                                 wl_array=wl,
                                 rebin=True,
                                 nbin=bin_num,
                                 bin_type=bin_alg,
                                 lmin=lmin,
                                 lmax=lmax,
                                 custom_bins=bin_custom,
                                 show=kwargs['show'])
        else:
            _l, _cl, _cl_err, _cov = pol_cl_calculation(pol_dict,
                                                        config_file_name,
                                                        wl_array=wl,
                                                        rebin=True,
                                                        nbin=bin_num,
                                                        bin_type=bin_alg,
                                                        lmin=lmin,
                                                        lmax=lmax,
                                                        custom_bins=bin_custom,
                                                        show=kwargs['show'])
        cl_txt.write(
            'multipole\t%s\n' %
            str(list(_l)).replace('[', '').replace(']', '').replace(', ', ' '))
        cl_txt.write('Cl\t%s\n' % str(list(_cl)).replace('[', '').replace(
            ']', '').replace(', ', ' '))
        cl_txt.write('Cl_ERR\t%s\n' % str(list(_cl_err)).replace(
            '[', '').replace(']', '').replace(', ', ' '))
        cl_txt.write('COV_FILE ---> %s\n\n\n' %
                     (os.path.join(out_folder, '%s_%i_cov.pkl' %
                                   (out_label, i))))

    cl_txt.close()

    logger.info('Created %s' % cl_txt_f)
コード例 #28
0
ファイル: mkforeground.py プロジェクト: nmik/Xgam
def foreground_map_convert(**kwargs):
    """Viewer interface for healpix maps
    """
    input_file = kwargs['infile']
    nside_out = kwargs['nsideout']
    if not os.path.exists(input_file):
        abort("Map %s not found!" % input_file)
    frmaps = pf.open(input_file)
    maps_slices = frmaps[0].data
    energy = np.array([x[0] for x in frmaps['ENERGIES'].data])
    nside = 2048
    npix = hp.nside2npix(nside)
    iii = np.arange(npix)
    x, y, z = hp.pix2vec(nside, iii)
    lon_hp, lat_hp = hp.rotator.vec2dir(x, y, z, lonlat=True)
    hp_frmap = np.arange(npix, dtype=np.float64)
    lon_fits = np.arange(len(maps_slices[0][0]))
    nresx = 360. / len(lon_fits)
    lon_fits_1 = np.flip(lon_fits[1440:] * nresx - 180, 0)
    lon_fits = np.append(lon_fits_1, np.flip(lon_fits[:1440] * nresx + 180, 0))
    lat_fits = np.arange(len(maps_slices[0]))
    lat_fits = lat_fits * nresx - 90
    fr_e = []
    out_list = []
    i_start = 8
    for i, en in enumerate(energy[i_start:]):
        i = i + i_start
        out_name = os.path.basename(input_file).replace(
            '.fits', '_hp%i_%d.fits' % (nside_out, en))
        out_path = os.path.join(X_OUT, 'fits', out_name)
        if os.path.exists(out_path):
            logger.info('Running map conversion for energy %.2f MeV...' % en)
            logger.info('Retrieving %s' % out_path)
        else:
            logger.info('Running map conversion for energy %.2f MeV...' % en)
            frmap = maps_slices[i]
            fmt = dict(xname='$l$',
                       xunits='deg',
                       yname='$b$',
                       yunits='deg',
                       zname='Flux [cm$^{-2}$s$^{-1}$sr$^{-1}$]')
            lon, _indexx = np.unique(lon_fits, return_index=True)
            lat, _indexy = np.unique(lat_fits, return_index=True)
            frmap = frmap[:, _indexx]
            frspline = xInterpolatedBivariateSplineLinear(
                lon, lat, frmap.T, **fmt)
            for i, pix in enumerate(hp_frmap):
                hp_frmap[i] = frspline((lon_hp[i] + 360) % 360, lat_hp[i])
            fr_e.append(hp_frmap[12426])
            hp_frmap_out = hp.pixelfunc.ud_grade(hp_frmap,
                                                 nside_out,
                                                 pess=True)
            if kwargs['coord'] == 'CEL':
                from Xgam.utils.PolSpice_ import change_coord
                hp_frmap_out = change_coord(hp_frmap_out, ['G', 'C'])
                logger.info('Changing coordinate system to celestial')
            hp.write_map(out_path, hp_frmap_out, coord='G')
            logger.info('Created map %s' % out_path)
        out_list.append(out_path)
    out_name_list = os.path.basename(input_file).replace(
        '.fits', '_hp%i_list.txt' % nside_out)
    out_name_list = os.path.join(X_OUT, 'fits', out_name_list)
    if kwargs['coord'] == 'CEL':
        out_name_list = out_name_list.replace('GAL', 'CEL')
    logger.info('Writing list of output files: %s' % out_name_list)
    np.savetxt(out_name_list, out_list, fmt='%s')

    if kwargs['show']:
        import matplotlib.pyplot as plt
        plt.figure()
        plt.plot(energy, fr_e, 'o--')
        plt.xlabel('Energy [MeV]')
        plt.xscale('log')
        plt.ylabel('Flux [cm$^{-2}$s$^{-1}$sr$^{-1}$]')
        plt.show()

    frmaps.close()
    logger.info('Done!')
コード例 #29
0
ファイル: APSsimplefit.py プロジェクト: nmik/Xgam
def aps_fit(**kwargs):
    """
    Viewer interface for APS and Covariance matrix                                                                                                                                                             
    """

    params = parse_polspice_aps(kwargs['infile'])
    _emin, _emax = params[0], params[1]
    _l, _cl, _clerr = params[2], params[3], params[4]
    _cov_ = params[5]

    logger.info('Generating APS plots...')
    for i, cl in enumerate(_cl):
        valid = ~(np.isnan(cl))
        l = _l[i][valid]
        cl = cl[valid]
        clerr = _clerr[i][valid]
        fitrange = np.where((l > int(kwargs['lminfit']))
                            & (l < int(kwargs['lmaxfit'])))[0]

        fig = plt.figure()
        plt.plot([0, 2000], [0, 0], '--', color='silver')
        aps_label = '%.1f-%.1f GeV' % (_emin[i] / 1000, _emax[i] / 1000)
        plt.errorbar(l,
                     cl,
                     fmt='o',
                     markersize=5,
                     elinewidth=2,
                     label='Data',
                     color='0.3',
                     yerr=clerr)
        if kwargs['model'] == '1h':
            popt, pcov = curve_fit(onehalo_model,
                                   l[fitrange],
                                   cl[fitrange],
                                   sigma=clerr[fitrange],
                                   absolute_sigma=True)
            print('-----------------------')
            print('Fit range: %i - %i' %
                  (kwargs['lminfit'], kwargs['lmaxfit']))
            print('Best-fit Cp = %.2e+-%2e' % (popt[0], np.sqrt(pcov[0][0])))
            print('Detection Significance: %.3f' %
                  (popt[0] / np.sqrt(pcov[0][0])))
            print('-----------------------')

            plt.fill_between(l[fitrange],
                             np.full(len(l[fitrange]),
                                     popt[0] - np.sqrt(pcov[0][0])),
                             np.full(len(l[fitrange]),
                                     popt[0] + np.sqrt(pcov[0][0])),
                             color='lightcoral',
                             alpha=0.5)
            plt.plot([l[fitrange][0], kwargs['lmaxfit']], [popt[0], popt[0]],
                     '--',
                     color='red',
                     label='C$_{p}$ = %.1e' % popt[0])
        else:
            popt, pcov = curve_fit(twohalo_model,
                                   l[fitrange],
                                   cl[fitrange],
                                   sigma=clerr[fitrange],
                                   absolute_sigma=True)

            chi2 = np.sum(((cl[fitrange] - twohalo_model(l[fitrange], *popt)) /
                           clerr[fitrange])**2)
            chi2red = chi2 / len(fitrange)

            print('-----------------------')
            print('Fit range: %i - %i' %
                  (kwargs['lminfit'], kwargs['lmaxfit']))
            print('Best-fit params = %s' % (popt))
            print('Chi2red: %.3f' % (chi2red))
            print('-----------------------')

            plt.plot(l[fitrange],
                     twohalo_model(l[fitrange], *popt),
                     '--',
                     color='red',
                     label='Best fit: %.1e $\ell^{-%.1f}$ + (%.1e)' %
                     (popt[0], popt[1], popt[2]))

        plt.xlim(kwargs['lminfit'] - 10, kwargs['lmaxfit'] + 5)
        #plt.ylim(-1e-11,1e-11)
        plt.xscale(kwargs['xscale'])
        plt.xlabel('Multipole', size=15)
        plt.ylabel('C$^{sig}_{\ell}$',
                   size=15)  #[(cm$^{-2}$s$^{-1}$sr$^{-1}$)sr]
        plt.title(kwargs['title'])
        plt.tight_layout()
        plt.legend(loc=1, fontsize=16)

        plt.savefig('output/figs/%s_fit_%s.png' %
                    (kwargs['outflabel'], kwargs['model']))

    plt.show()
コード例 #30
0
ファイル: ScienceTools_.py プロジェクト: nmik/Xgam
# (at your option) any later version.                                          #
#                                                                              #
#------------------------------------------------------------------------------#
"""Remake of some of the Fermi Science Tools
"""

import os
import time
import gt_apps as my_apps
from Xgam.utils.logging_ import logger
from Xgam import X_OUT
from Xgam import FT_DATA_FOLDER

FT_DATA_OUT = os.path.join(FT_DATA_FOLDER, 'output')
if not os.path.exists(FT_DATA_OUT):
    logger.info('Creating output folder of selected data (%s)' % FT_DATA_OUT)
    os.makedirs(os.path.join(FT_DATA_OUT))


def gtselect(label, filter_dict):
    """
    Calls gtselect from Science Tools.

    Parameters
    ----------
    label: str
        To automatically set the name of the output file
    filter_dict: python dict
        To define all the parameters

    Returns