Esempio n. 1
0
def set_fn_model(flg=0):
    '''
	Load up f(N) data

    Parameters
    ----------

    Returns
    -------
    fN_data :: List of fN_Constraint Classes

    JXP on 27 Nov 2014
    '''
    if flg==0: # I may choose to pickle a few of these
        sfN_model = xifm.default_model(recalc=True,use_mcmc=True) # Hermite Spline
        tmp = [13., 15., 17., 21.5, 22.]
        val = sfN_model.eval(tmp, 2.5)
        # Not using this! -- JXP 29 Jan 2015
        #xdb.set_trace()
    elif flg==1:
        sfN_model = fNmodel.fN_Model('Gamma')
    else: 
        raise ValueError('mcmc.set_model: Not ready for this type of fN model {:d}'.format(flg))
    #
    #print(sfN_model)
    return sfN_model
Esempio n. 2
0
def set_fn_model(flg=0):
    '''
	Load up f(N) data

    Parameters
    ----------

    Returns
    -------
    fN_data :: List of fN_Constraint Classes

    JXP on 27 Nov 2014
    '''
    if flg == 0:  # I may choose to pickle a few of these
        sfN_model = xifm.default_model(recalc=True,
                                       use_mcmc=True)  # Hermite Spline
    elif flg == 1:
        sfN_model = xifm.fN_Model('Gamma')
    else:
        raise ValueError(
            'mcmc.set_model: Not ready for this type of fN model {:d}'.format(
                flg))
    #
    #print(sfN_model)
    return sfN_model
Esempio n. 3
0
def get_telfer_spec(zqso=0., igm=False, fN_gamma=None, LL_flatten=True):
    '''Generate a Telfer QSO composite spectrum

    Parameters:
    ----------
    zqso: float, optional
      Redshift of the QSO
    igm: bool, optional
      Include IGM opacity? [False]
    fN_gamma: float, optional
      Power-law evolution in f(N,X)
    LL_flatten: bool, optional
      Set Telfer to a constant below the LL?

    Returns:
    --------
    telfer_spec: XSpectrum1D
      Spectrum
    '''
    # Read
    telfer = ascii.read(
        xa_path+'/data/quasar/telfer_hst_comp01_rq.ascii', comment='#')
    scale = telfer['flux'][(telfer['wrest'] == 1450.)]
    telfer_spec = XSpectrum1D.from_tuple((telfer['wrest']*(1+zqso),
        telfer['flux']/scale[0])) # Observer frame

    # IGM?
    if igm is True:
        '''The following is quite experimental.
        Use at your own risk.
        '''
        import multiprocessing
        from xastropy.igm.fN import model as xifm
        from xastropy.igm import tau_eff as xit
        fN_model = xifm.default_model()
        # Expanding range of zmnx (risky)
        fN_model.zmnx = (0.,5.)
        if fN_gamma is not None:
            fN_model.gamma = fN_gamma
        # Parallel
        igm_wv = np.where(telfer['wrest']<1220.)[0]
        adict = []
        for wrest in telfer_spec.dispersion[igm_wv].value:
            tdict = dict(ilambda=wrest, zem=zqso, fN_model=fN_model)
            adict.append(tdict)
        # Run
        #xdb.set_trace()
        pool = multiprocessing.Pool(4) # initialize thread pool N threads
        ateff = pool.map(xit.map_etl, adict)
        # Apply
        telfer_spec.flux[igm_wv] *= np.exp(-1.*np.array(ateff))
        # Flatten?
        if LL_flatten:
            wv_LL = np.where(np.abs(telfer_spec.dispersion/(1+zqso)-914.*u.AA)<3.*u.AA)[0]
            f_LL = np.median(telfer_spec.flux[wv_LL])
            wv_low = np.where(telfer_spec.dispersion/(1+zqso)<911.7*u.AA)[0]
            telfer_spec.flux[wv_low] = f_LL

    # Return
    return telfer_spec
Esempio n. 4
0
def test_lya():
    # f(N)
    fN_model = xifm.default_model()
    #fN_model = xifm.default_model(recalc=True)

    # tau_eff
    lamb = 1215.6701*(1+2.4)
    teff = xit.ew_teff_lyman(lamb, 2.5, fN_model, NHI_MIN=12., NHI_MAX=17.)
    # Test
    np.testing.assert_allclose(teff, 0.19821448846)
Esempio n. 5
0
def test_lya():
    # f(N)
    fN_model = xifm.default_model()
    #fN_model = xifm.default_model(recalc=True)

    # tau_eff
    lamb = 1215.6701 * (1 + 2.4)
    teff = xit.ew_teff_lyman(lamb, 2.5, fN_model, NHI_MIN=12., NHI_MAX=17.)
    # Test
    np.testing.assert_allclose(teff, 0.19821448846)
Esempio n. 6
0
def get_telfer_spec(zqso=0., igm=False):
    '''Generate a Telfer QSO composite spectrum

    Paraemters:
    ----------
    zqso: float, optional
      Redshift of the QSO
    igm: bool, optional
      Include IGM opacity? [False]

    Returns:
    --------
    telfer_spec: XSpectrum1D
      Spectrum
    '''
    # Read
    telfer = ascii.read(xa_path + '/data/quasar/telfer_hst_comp01_rq.ascii',
                        comment='#')
    scale = telfer['flux'][(telfer['wrest'] == 1450.)]
    telfer_spec = XSpectrum1D.from_tuple(
        (telfer['wrest'] * (1 + zqso),
         telfer['flux'] / scale[0]))  # Observer frame

    # IGM?
    if igm is True:
        '''The following is quite experimental.
        Use at your own risk.
        '''
        import multiprocessing
        from xastropy.igm.fN import model as xifm
        from xastropy.igm import tau_eff as xit
        fN_model = xifm.default_model()
        # Expanding range of zmnx (risky)
        fN_model.zmnx = (0., 5.)
        # Parallel
        igm_wv = np.where(telfer['wrest'] < 1220.)[0]
        adict = []
        for wrest in telfer_spec.dispersion[igm_wv].value:
            tdict = dict(ilambda=wrest, zem=zqso, fN_model=fN_model)
            adict.append(tdict)
        # Run
        #xdb.set_trace()
        pool = multiprocessing.Pool(4)  # initialize thread pool N threads
        ateff = pool.map(xit.map_etl, adict)
        # Apply
        telfer_spec.flux[igm_wv] *= np.exp(-1. * np.array(ateff))

    # Return
    return telfer_spec
Esempio n. 7
0
def test_parallel():
    import multiprocessing
    # f(N)
    fN_model = xifm.default_model()
    # Generate dicts
    tst_wv = xit.tau_eff_llist()
    adict = []
    for wrest in tst_wv:
        tdict = dict(ilambda=wrest.value*(1+2.4), zem=2.5, fN_model=fN_model)
        adict.append(tdict)

    pool = multiprocessing.Pool(2) # initialize thread pool N threads
    ateff = pool.map(xit.map_etl, adict)
    #pdb.set_trace()
    np.testing.assert_allclose(ateff[-3:], [0.23437970917295792, 0.20262106481068218, 0.21927058621246398])
Esempio n. 8
0
def get_telfer_spec(zqso=0., igm=False):
    '''Generate a Telfer QSO composite spectrum

    Paraemters:
    ----------
    zqso: float, optional
      Redshift of the QSO
    igm: bool, optional
      Include IGM opacity? [False]

    Returns:
    --------
    telfer_spec: XSpectrum1D
      Spectrum
    '''
    # Read
    telfer = ascii.read(
        xa_path+'/data/quasar/telfer_hst_comp01_rq.ascii', comment='#')
    scale = telfer['flux'][(telfer['wrest'] == 1450.)]
    telfer_spec = XSpectrum1D.from_tuple((telfer['wrest']*(1+zqso),
        telfer['flux']/scale[0])) # Observer frame

    # IGM?
    if igm is True:
        '''The following is quite experimental.
        Use at your own risk.
        '''
        import multiprocessing
        from xastropy.igm.fN import model as xifm
        from xastropy.igm import tau_eff as xit
        fN_model = xifm.default_model()
        # Expanding range of zmnx (risky)
        fN_model.zmnx = (0.,5.)
        # Parallel
        igm_wv = np.where(telfer['wrest']<1220.)[0]
        adict = []
        for wrest in telfer_spec.dispersion[igm_wv].value:
            tdict = dict(ilambda=wrest, zem=zqso, fN_model=fN_model)
            adict.append(tdict)
        # Run
        #xdb.set_trace()
        pool = multiprocessing.Pool(4) # initialize thread pool N threads
        ateff = pool.map(xit.map_etl, adict)
        # Apply
        telfer_spec.flux[igm_wv] *= np.exp(-1.*np.array(ateff))

    # Return
    return telfer_spec
Esempio n. 9
0
def test_parallel():
    import multiprocessing
    # f(N)
    fN_model = xifm.default_model()
    # Generate dicts
    tst_wv = xit.tau_eff_llist()
    adict = []
    for wrest in tst_wv:
        tdict = dict(ilambda=wrest.value * (1 + 2.4),
                     zem=2.5,
                     fN_model=fN_model)
        adict.append(tdict)

    pool = multiprocessing.Pool(2)  # initialize thread pool N threads
    ateff = pool.map(xit.map_etl, adict)
    #pdb.set_trace()
    np.testing.assert_allclose(
        ateff[-3:],
        [0.23437970917295792, 0.20262106481068218, 0.21927058621246398])
Esempio n. 10
0
def set_fn_model(flg=0):
    '''
	Load up f(N) data

    Parameters
    ----------

    Returns
    -------
    fN_data :: List of fN_Constraint Classes

    JXP on 27 Nov 2014
    '''
    if flg==0: # I may choose to pickle a few of these
        sfN_model = xifm.default_model(recalc=True,use_mcmc=True) # Hermite Spline 
    elif flg==1:
        sfN_model = xifm.fN_Model('Gamma')
    else: 
        raise ValueError('mcmc.set_model: Not ready for this type of fN model {:d}'.format(flg))
    #
    #print(sfN_model)
    return sfN_model
Esempio n. 11
0
## #################################
## TESTING
## #################################
if __name__ == "__main__":

    """
    # Make EW spline file
    mk_ew_lyman_spline(24.)
    """

    from xastropy.igm.fN import model as xifm
    import multiprocessing

    # xdb.set_trace()
    # read f(N)
    fN_model = xifm.default_model()
    print(fN_model)

    # tau_eff
    # tst_wv = tau_eff_llist()
    tst_wv = np.arange(915.0, 1255, 1.0)
    # lamb = 1215.6701*(1+2.4)
    adict = []
    for wrest in tst_wv:
        tdict = dict(ilambda=wrest * (1 + 2.4), zem=2.5, fN_model=fN_model)
        adict.append(tdict)

    pool = multiprocessing.Pool(4)  # initialize thread pool N threads
    ateff = pool.map(map_etl, adict)
    # Plot
    xdb.xplot(tst_wv, np.exp(-np.array(ateff)))
Esempio n. 12
0
        chain_file = os.environ.get(
            'DROPBOX_DIR') + 'IGM/fN/MCMC/mcmc_spline_k13r13o13n12_8.fits.gz'
        outp = mcmc.chain_stats(chain_file)

        # Build a model
        NHI_pivots = [12., 15., 17.0, 18.0, 20.0, 21., 21.5, 22.]
        fN_model = xifm.fN_Model('Hspline',
                                 zmnx=(0.5, 3.0),
                                 pivots=NHI_pivots,
                                 param=outp['best_p'])
        #xdb.set_trace()
        print(fN_model)

    # Compare default against P+13
    if (flg_test % 2**2) >= 2**1:
        fN_model = xifm.default_model()
        p13_file = (os.environ.get('DROPBOX_DIR') +
                    'IGM/fN/fN_spline_z24.fits.gz')
        hdu = fits.open(p13_file)
        p13_data = hdu[1].data

        plt.clf()
        plt.scatter(p13_data['LGN'], p13_data['FN'])
        #plt.plot(p13_data['LGN'],p13_data['FN'], '-')
        xplt = np.linspace(12., 22, 10000)
        yplt = fN_model.eval(xplt, 2.4)
        plt.plot(xplt, yplt, '-', color='red')
        #plt.plot(xplt, yplt, '-')
        plt.show()
        #xdb.set_trace()
Esempio n. 13
0
## #################################
## #################################
## TESTING
## #################################
if __name__ == '__main__':
    '''
    # Make EW spline file
    mk_ew_lyman_spline(24.)
    '''

    from xastropy.igm.fN import model as xifm
    import multiprocessing

    #xdb.set_trace()
    # read f(N)
    fN_model = xifm.default_model()
    print(fN_model)

    # tau_eff
    #tst_wv = tau_eff_llist()
    tst_wv = np.arange(915., 1255, 1.)
    #lamb = 1215.6701*(1+2.4)
    adict = []
    for wrest in tst_wv:
        tdict = dict(ilambda=wrest * (1 + 2.4), zem=2.5, fN_model=fN_model)
        adict.append(tdict)

    pool = multiprocessing.Pool(4)  # initialize thread pool N threads
    ateff = pool.map(map_etl, adict)
    # Plot
    xdb.xplot(tst_wv, np.exp(-np.array(ateff)))
Esempio n. 14
0
    
    if (flg_test % 2) == 1:
        # MCMC Analysis
        chain_file = os.environ.get('DROPBOX_DIR')+'IGM/fN/MCMC/mcmc_spline_k13r13o13n12_8.fits.gz'
        outp = mcmc.chain_stats(chain_file)

        # Build a model
        NHI_pivots = [12., 15., 17.0, 18.0, 20.0, 21., 21.5, 22.]
        fN_model = xifm.fN_Model('Hspline', zmnx=(0.5,3.0),
                            pivots=NHI_pivots, param=outp['best_p'])
        #xdb.set_trace()
        print(fN_model)

    # Compare default against P+13
    if (flg_test % 4) >= 2:
        fN_model = xifm.default_model()
        p13_file = (os.environ.get('DROPBOX_DIR')+'IGM/fN/fN_spline_z24.fits.gz')
        hdu = fits.open(p13_file)
        p13_data = hdu[1].data
        
        plt.clf()
        plt.scatter(p13_data['LGN'],p13_data['FN'])
        #plt.plot(p13_data['LGN'],p13_data['FN'], '-')
        xplt = np.linspace(12., 22, 10000)
        yplt = fN_model.eval(xplt, 2.4)
        plt.plot(xplt, yplt, '-', color='red')
        #plt.plot(xplt, yplt, '-')
        plt.show()
        #xdb.set_trace()

    # Reproduce the main figure from P14
Esempio n. 15
0
    
    if (flg_test % 2) == 1:
        # MCMC Analysis
        chain_file = os.environ.get('DROPBOX_DIR')+'IGM/fN/MCMC/mcmc_spline_k13r13o13n12_8.fits.gz'
        outp = mcmc.chain_stats(chain_file)

        # Build a model
        NHI_pivots = [12., 15., 17.0, 18.0, 20.0, 21., 21.5, 22.]
        fN_model = xifm.fN_Model('Hspline', zmnx=(0.5,3.0),
                            pivots=NHI_pivots, param=outp['best_p'])
        #xdb.set_trace()
        print(fN_model)

    # Compare default against P+13
    if (flg_test % 4) >= 2:
        fN_model = xifm.default_model()
        #p13_pivots = ([12.000000,       15.000000,       17.000000,      18.000000,
        #        20.000000,       21.000000,       21.500000,       22.000000])
        #p13_fN = ([-9.7233782,      -14.411575,      -17.941498,      -19.392904,
        #        -21.282812,      -22.820860,      -23.945236,      -25.502331])
        p13_file = (os.environ.get('DROPBOX_DIR')+'IGM/fN/fN_spline_z24.fits.gz')
        hdu = fits.open(p13_file)
        p13_data = hdu[1].data
        
        plt.clf()
        plt.scatter(p13_data['LGN'],p13_data['FN'])
        #plt.plot(p13_data['LGN'],p13_data['FN'], '-')
        xplt = np.linspace(12., 22, 10000)
        yplt = fN_model.eval(2.4,xplt)
        plt.plot(xplt, yplt, '-', color='red')
        #plt.plot(xplt, yplt, '-')
Esempio n. 16
0
    
    if (flg_test % 2) == 1:
        # MCMC Analysis
        chain_file = os.environ.get('DROPBOX_DIR')+'IGM/fN/MCMC/mcmc_spline_k13r13o13n12_8.fits.gz'
        outp = mcmc.chain_stats(chain_file)

        # Build a model
        NHI_pivots = [12., 15., 17.0, 18.0, 20.0, 21., 21.5, 22.]
        fN_model = xifm.fN_Model('Hspline', zmnx=(0.5,3.0),
                            pivots=NHI_pivots, param=outp['best_p'])
        #xdb.set_trace()
        print(fN_model)

    # Compare default against P+13
    if (flg_test % 2**2) >= 2**1:
        fN_model = xifm.default_model()
        p13_file = (os.environ.get('DROPBOX_DIR')+'IGM/fN/fN_spline_z24.fits.gz')
        hdu = fits.open(p13_file)
        p13_data = hdu[1].data
        
        plt.clf()
        plt.scatter(p13_data['LGN'],p13_data['FN'])
        #plt.plot(p13_data['LGN'],p13_data['FN'], '-')
        xplt = np.linspace(12., 22, 10000)
        yplt = fN_model.eval(xplt, 2.4)
        plt.plot(xplt, yplt, '-', color='red')
        #plt.plot(xplt, yplt, '-')
        plt.show()
        #xdb.set_trace()

    # Reproduce the main figure from P14
Esempio n. 17
0
def fig_fn(outfil=None, data_list=None):

    from xastropy.igm.fN import model as xifm
    from xastropy.igm.fN import data as xifd

    # Init
    if outfil == None:
        outfil = 'fig_fN.pdf'

    # Start the plot
    if outfil != None:
        pp = PdfPages(outfil)

    fn_file = xa_path + '/igm/fN/fn_constraints_z2.5_vanilla.fits'
    k13r13_file = xa_path + '/igm/fN/fn_constraints_K13R13_vanilla.fits'
    n12_file = xa_path + '/igm/fN/fn_constraints_N12_vanilla.fits'
    all_fN_cs = xifd.fn_data_from_fits([k13r13_file, fn_file, n12_file])

    fN_model = xifm.default_model()

    # Remove K12
    #data_list = ['K13R13','OPB07', 'N12']
    #outfil = 'tmp.png'
    if data_list is None:
        fN_cs = [
            fN_c for fN_c in all_fN_cs
            if ((fN_c.ref != 'K02') & (fN_c.ref != 'PW09'))
        ]
    else:
        fN_cs = [fN_c for fN_c in all_fN_cs if fN_c.ref in data_list]
    fN_dtype = [fc.fN_dtype for fc in fN_cs]

    # Plot
    ymnx = [-26, -10.]
    xmnx = [12., 22.]

    plt.figure(figsize=(8, 5))
    plt.clf()
    gs = gridspec.GridSpec(1, 1)
    main = plt.subplot(gs[:, :])

    # f(N) data
    main.set_ylabel(r'$\log \, f(N_{\rm HI})$')
    main.set_xlabel(r'$\log \, N_{\rm HI}$')
    main.set_ylim(ymnx)
    main.set_xlim(xmnx)

    syms = ['o', 's', 'D']
    isyms = 0
    for fN_c in fN_cs:
        if fN_c.fN_dtype == 'fN':
            # Length
            ip = range(fN_c.data['NPT'])
            #xdb.set_trace()
            val = np.where(fN_c.data['FN'][ip] > -90)[0]
            #xdb.set_trace()
            if len(val) > 0:
                #xdb.set_trace()
                ipv = np.array(ip)[val]
                xval = np.median(fN_c.data['BINS'][:, ipv], 0)
                xerror = [
                    fN_c.data['BINS'][1, ipv] - xval,
                    xval - fN_c.data['BINS'][0, ipv]
                ]
                yerror = [
                    fN_c.data['SIG_FN'][1, ipv], fN_c.data['SIG_FN'][0, ipv]
                ]  # Inverted!
                main.errorbar(xval,
                              fN_c.data['FN'][ipv],
                              xerr=xerror,
                              yerr=yerror,
                              fmt=syms[isyms],
                              label=fN_c.ref,
                              capthick=2,
                              color='gray')
                isyms += 1

    # Paint on the Media
    lsz = 16.
    main.fill_between([xmnx[0], 14.5],
                      ymnx[0],
                      ymnx[1],
                      color='red',
                      alpha=0.3)
    main.text(13.1, -18., 'IGM\n(85%)', color='red', size=lsz, ha='center')
    main.fill_between([14.5, 19.], ymnx[0], ymnx[1], color='blue', alpha=0.3)
    main.text(16.5, -22., 'CGM\n(10%)', color='blue', size=lsz, ha='center')
    main.fill_between([19., xmnx[1]],
                      ymnx[0],
                      ymnx[1],
                      color='green',
                      alpha=0.3)
    main.text(20.5, -18., 'ISM\n(5%)', color='green', size=lsz, ha='center')

    # Label
    main.text(np.sum(xmnx) / 2.,
              -12.,
              r'$z \approx 2.5$' + '\n(P+14)',
              color='black',
              size=lsz,
              ha='center',
              va='top')

    # Legend
    main.legend(loc='lower left', numpoints=1)

    # Model?
    xplt = 12.01 + 0.01 * np.arange(1100)
    yplt = fN_model.eval(xplt, 2.4)
    main.plot(xplt, yplt, '-', color='black')

    # Fonts
    xputils.set_fontsize(main, 16.)

    # Write
    plt.tight_layout(pad=0.2, h_pad=0., w_pad=0.1)
    pp.savefig()
    plt.close()

    # Finish
    print('tlk_igm.fig_fn: Wrote {:s}'.format(outfil))
    pp.close()