Beispiel #1
0
    def __init__(self):
        from speclite import filters
        from scipy.spatial import cKDTree as KDTree
        from desisim.io import read_basis_templates

        self.bgs_meta = read_basis_templates(objtype='BGS', onlymeta=True)
        self.bgs_tree = KDTree(self._bgs())
Beispiel #2
0
    def test_HBETA(self):
        '''Confirm that BGS H-beta flux matches meta table description'''
        print('In function test_HBETA, seed = {}'.format(self.seed))
        wave = np.arange(5000, 7000.1, 0.2)
        # Need to choose just the star-forming galaxies.
        from desisim.io import read_basis_templates
        baseflux, basewave, basemeta = read_basis_templates(objtype='BGS')
        keep = np.where(basemeta['HBETA_LIMIT'] == 0)[0]
        bgs = BGS(wave=wave,
                  basewave=basewave,
                  baseflux=baseflux[keep, :],
                  basemeta=basemeta[keep])
        flux, ww, meta = bgs.make_templates(
            seed=self.seed,
            nmodel=10,
            zrange=(0.05, 0.4),
            logvdisp_meansig=[np.log10(75), 0.0],
            nocolorcuts=True,
            nocontinuum=True)

        for i in range(len(meta)):
            z = meta['REDSHIFT'][i]
            ii = (4854 * (1 + z) < wave) & (wave < 4868 * (1 + z))
            hbetaflux = 1e-17 * np.sum(flux[i, ii] * np.gradient(wave[ii]))
            self.assertAlmostEqual(hbetaflux, meta['HBETAFLUX'][i], 2)
Beispiel #3
0
def main():

    seed = 123
    ngal = 100
    npergal = 50

    # Read the mock catalog.
    cat = h5py.File('galaxy_catalogue_0.hdf5')

    gr = cat['Data']['g_r'][:ngal]
    mag = cat['Data']['app_mag'][:ngal]
    z = cat['Data']['z_obs'][:ngal]
    zmin, zmax = np.min(z), np.max(z)

    # Read the basis templates and initialize the output metadata table.
    baseflux, basewave, basemeta = read_basis_templates('BGS')
    meta = empty_metatable(objtype='BGS', nmodel=ngal)

    bgs = BGS(minwave=3000, maxwave=11000.0)

    for igal in range(ngal):
        flux, wave, meta = bgs.make_templates(npergal, zrange=(zmin, zmax),
                                              nocolorcuts=True)

        pdb.set_trace()
Beispiel #4
0
 def test_read_templates(self):
     wave = np.arange(7000, 7020)
     nspec = 3
     for objtype in ['ELG', 'LRG', 'QSO', 'STAR', 'WD']:
         flux, wave1, meta = io.read_basis_templates(objtype, outwave=wave, nspec=3)
         ntemplates, nwave = flux.shape
         self.assertEqual(nwave, len(wave))
         self.assertEqual(ntemplates, nspec)
         self.assertEqual(len(meta), nspec)
Beispiel #5
0
 def test_read_templates(self):
     wave = np.arange(7000, 7020)
     nspec = 3
     for objtype in ['ELG', 'LRG', 'QSO', 'STAR', 'WD']:
         flux, wave1, meta = io.read_basis_templates(objtype,
                                                     outwave=wave,
                                                     nspec=3)
         ntemplates, nwave = flux.shape
         self.assertEqual(nwave, len(wave))
         self.assertEqual(ntemplates, nspec)
         self.assertEqual(len(meta), nspec)
Beispiel #6
0
    def __init__(self, nproc=1):
        from speclite import filters
        from scipy.spatial import KDTree

        self.nproc = nproc

        self.bgs_meta = read_basis_templates(objtype='BGS', onlymeta=True)
        self.elg_meta = read_basis_templates(objtype='ELG', onlymeta=True)
        self.lrg_meta = read_basis_templates(objtype='LRG', onlymeta=True)
        self.qso_meta = read_basis_templates(objtype='QSO', onlymeta=True)
        self.wd_da_meta = read_basis_templates(objtype='WD', subtype='DA', onlymeta=True)
        self.wd_db_meta = read_basis_templates(objtype='WD', subtype='DB', onlymeta=True)

        self.decamwise = filters.load_filters('decam2014-g', 'decam2014-r', 'decam2014-z',
                                              'wise2010-W1', 'wise2010-W2')

        # Read all the stellar spectra and synthesize DECaLS/WISE fluxes.
        self.star_phot()

        #self.elg_phot()
        #self.elg_kcorr = read_basis_templates(objtype='ELG', onlykcorr=True)

        self.bgs_tree = KDTree(self._bgs())
        self.elg_tree = KDTree(self._elg())
        #self.lrg_tree = KDTree(self._lrg())
        #self.qso_tree = KDTree(self._qso())
        self.star_tree = KDTree(self._star())
        self.wd_da_tree = KDTree(self._wd_da())
        self.wd_db_tree = KDTree(self._wd_db())
Beispiel #7
0
    def elg_kcorr(self):
        """Compute K-corrections for the ELG templates on a grid of redshift."""
        
        flux, wave, meta = read_basis_templates(objtype='ELG')
        nt = len(meta)

        zmin, zmax, dz = 0.0, 2.0, 0.1
        nz = np.round( (zmax - zmin) / dz ).astype('i2')

        colors = dict(
            redshift = np.linspace(0.0, 2.0, nz),
            gr = np.zeros( (nt, nz) ),
            rz = np.zeros( (nt, nz) )
            )

        #from time import time
        #t0 = time()
        #for iz, red in enumerate(colors['redshift']):
        #    print(iz)
        #    zwave = wave.astype('float') * (1 + red)
        #    phot = self.grz.get_ab_maggies(flux, zwave, mask_invalid=False)
        #    colors['gr'][:, iz] = -2.5 * np.log10( phot['decam2014-g'] / phot['decam2014-r'] )
        #    colors['rz'][:, iz] = -2.5 * np.log10( phot['decam2014-r'] / phot['decam2014-z'] )
        #print( (time() - t0) / 60 )

        from time import time
        t0 = time()
        zargs = list()
        for red in colors['redshift']:
            zargs.append( (red, flux, wave, self.grz) )

        if self.nproc > 1:
            pool = multiprocessing.Pool(self.nproc)
            result = pool.map( _get_colors_onez, zargs )
            pool.close()
        else:
            result = list()
            for onearg in zargs:
                result.append( _get_colors_onez(onearg) )
        print( (time() - t0) / 60 )

        import matplotlib.pyplot as plt
        for tt in range(10):
            plt.scatter(colors['rz'][tt, :], colors['gr'][tt, :])
        plt.xlim(-0.5, 2.0) ;  plt.ylim(-0.5, 2.0)
        plt.show()
        import pdb ; pdb.set_trace()
Beispiel #8
0
    def star_phot(self):
        """Synthesize photometry for the full set of stellar templates."""
        star_normfilter = 'decam2014-r'

        star_flux, star_wave, star_meta = read_basis_templates(objtype='STAR')
        star_maggies_table = self.decamwise.get_ab_maggies(star_flux, star_wave, mask_invalid=True)

        star_maggies = np.zeros( (len(star_meta), len(self.decamwise)) )
        for ff, key in enumerate(star_maggies_table.columns):
            star_maggies[:, ff] = star_maggies_table[key] / star_maggies_table[star_normfilter] # maggies
        self.star_flux_g = star_maggies[:, 0]
        self.star_flux_r = star_maggies[:, 1]
        self.star_flux_z = star_maggies[:, 2]
        self.star_flux_w1 = star_maggies[:, 3]
        self.star_flux_w2 = star_maggies[:, 4]
        
        self.star_meta = star_meta
Beispiel #9
0
    def __init__(self):
        """Read and cache the BAL template set.

        Attributes:
          balflux (numpy.ndarray): Array [nbase,npix] of the rest-frame BAL
            templates.
          balwave (numpy.ndarray): Array [npix] of rest-frame wavelengths
            corresponding to BASEFLUX (Angstrom).
          balmeta (astropy.Table): Table of metadata [nbase] for each template.

        """
        from desisim.io import read_basis_templates

        balflux, balwave, balmeta = read_basis_templates(objtype='BAL')
        self.balflux = balflux
        self.balwave = balwave
        self.balmeta = balmeta
Beispiel #10
0
    def __init__(self):
        """Read and cache the BAL template set.

        Attributes:
          balflux (numpy.ndarray): Array [nbase,npix] of the rest-frame BAL
            templates.
          balwave (numpy.ndarray): Array [npix] of rest-frame wavelengths
            corresponding to BASEFLUX (Angstrom).
          balmeta (astropy.Table): Table of metadata [nbase] for each template.

        """
        from desisim.io import read_basis_templates
        
        balflux, balwave, balmeta = read_basis_templates(objtype='BAL')
        self.balflux = balflux
        self.balwave = balwave
        self.balmeta = balmeta
Beispiel #11
0
def main(args):

    parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter,
                                     description='ELG archetypes')
    parser.add_argument('-o', '--objtype', type=str,  default='ELG', help='ELG', metavar='')

    # Set up the logger.
    if args.verbose:
        log = get_logger(DEBUG)
    else:
        log = get_logger()
        
    objtype = args.objtype.upper()
    log.debug('Using OBJTYPE {}'.format(objtype))

    baseflux, basewave, basemeta = read_basis_templates(objtype=objtype)



    pdb.set_trace()
def sim_lenssource_spectra(BGSmags, fratios, seed=None, exptime=1000., nperchunk=500,
                           infofile='lenssource-truth.fits', debug=False):
    """Build the (noisy) lens+source spectra. No redshift-fitting.

    """
    from astropy.io import fits
    from desisim.templates import BGS, ELG
    from desisim.scripts.quickspectra import sim_spectra
    from desisim.io import read_basis_templates
    from desispec.io import read_spectra
    
    rand = np.random.RandomState(seed)
    nsim = len(BGSmags)
    assert(nsim == len(fratios))

    if nperchunk > 500:
        raise ValueError('nperchunk={} exceeds the maximum number allowed by redrock'.format(nperchunk))

    nchunk = np.ceil(nsim / nperchunk).astype(int)

    # [1] Build the noise-less lens (BGS) spectra.

    # Read one healpix of the Buzzard mocks for redshift distribution.
    mockfile = os.path.join(os.getenv('DESI_ROOT'), 'mocks', 'buzzard', 'buzzard_v1.6_desicut',
                            '8', '0', '0', 'Buzzard_v1.6_lensed-8-0.fits')
    print('Reading {}'.format(mockfile))
    mock_BGS = Table(fitsio.read(mockfile)) #columns='lmag z'.split()
    
    # From the BGS template library, select a reddish galaxy
    tflux, twave, tmeta_BGS = read_basis_templates('BGS')
    i = np.argmin(np.abs(2.0 - tmeta_BGS['D4000']))
    iredBGS = i
    redspecBGS = tflux[i, :]
    Itempl_BGS = np.array([iredBGS])

    # LMAG: observed mag, DECam grizY
    mock_mag_r = mock_BGS['LMAG'][:, 1] # r-band
    dm = 0.01
    zz_BGS = np.zeros_like(BGSmags)
    for ii, mag in enumerate(BGSmags):
        I = np.flatnonzero(np.abs(mock_mag_r - mag) <= dm)
        zz_BGS[ii] = mock_BGS['Z'][rand.choice(I, size=1, replace=False)]

    input_meta_BGS = Table()
    input_meta_BGS['TEMPLATEID'] = [Itempl_BGS]*nsim
    input_meta_BGS['SEED'] = np.arange(nsim) # [seed]*nsim # 
    input_meta_BGS['REDSHIFT'] = zz_BGS
    input_meta_BGS['MAG'] = BGSmags
    input_meta_BGS['MAGFILTER'] = ['decam2014-r']*nsim

    BGSflux, BGSwave, BGSmeta, BGSobjmeta = BGS().make_templates(
        input_meta=input_meta_BGS, nocolorcuts=True, seed=seed)

    # [2] Build the noise-less source (ELG) spectra.
    #ELGmags = maggen(BGSmags[:, np.newaxis], fratios[np.newaxis, :])
    ELGmags = maggen(BGSmags, fratios)

    # Select a single ELG template.
    tflux, twave, tmeta_ELG = read_basis_templates('ELG')
    i = np.argmin(np.abs(1.0 - tmeta_ELG['D4000'])) # MIGHT NEED TO ADJUST THIS LINE 
    iblueELG = i
    bluespecELG = tflux[i, :]
    Itempl_ELG = np.array([iblueELG])

    # uncorrelated redshifts
    zmin_ELG, zmax_ELG = 0.8, 1.4
    zz_ELG = rand.uniform(zmin_ELG, zmax_ELG, nsim)
    
    input_meta_ELG = Table()
    input_meta_ELG['TEMPLATEID'] = [Itempl_ELG]*nsim
    input_meta_ELG['SEED'] = [3]*nsim # [seed]*nsim # np.arange(nsim) hack!
    input_meta_ELG['REDSHIFT'] = zz_ELG
    input_meta_ELG['MAG'] = ELGmags
    input_meta_ELG['MAGFILTER'] = ['decam2014-r']*nsim

    ELGflux, ELGwave, ELGmeta, ELGobjmeta = ELG().make_templates(
        input_meta=input_meta_ELG, nocolorcuts=True, seed=seed)
    assert(np.all(BGSwave == ELGwave))

    # Pack the simulation info into a table, for convenience.
    siminfo = Table()
    siminfo['TARGETID'] = np.arange(nsim, dtype=np.int64)
    siminfo['LENS_Z'] = input_meta_BGS['REDSHIFT'].astype('f4')
    siminfo['LENS_MAG'] = input_meta_BGS['MAG'].astype('f4')
    siminfo['SOURCE_Z'] = input_meta_ELG['REDSHIFT'].astype('f4')
    siminfo['SOURCE_MAG'] = input_meta_ELG['MAG'].astype('f4')
    siminfo['FRATIO'] = fratios.astype('f4')
    siminfo['CHUNK'] = np.zeros(nsim, dtype=np.int32)

    # Generate simulated DESI spectra given real spectra and observing
    # conditions. Divide the sample into chunks with a fixed number of
    # spectra per chunk (but no more than 500).
    obscond = {'AIRMASS': 1.3, 'EXPTIME': exptime, 'SEEING': 1.1,
               'MOONALT': -60, 'MOONFRAC': 0.0, 'MOONSEP': 180}

    simflux = BGSflux + ELGflux
    simwave = BGSwave

    for ichunk in np.arange(nchunk):
        specfile = 'lenssource-spectra-chunk{:03d}.fits'.format(ichunk)
        print('Writing chunk {}/{} to {}'.format(ichunk, nchunk-1, specfile))
        i1 = ichunk * nperchunk
        i2 = (ichunk+1) * nperchunk
        siminfo['CHUNK'][i1:i2] = ichunk
        sim_spectra(simwave, simflux[i1:i2, :], 'dark', specfile, obsconditions=obscond,
                    sourcetype='bgs', seed=seed, targetid=siminfo['TARGETID'][i1:i2],
                    redshift=siminfo['LENS_Z'][i1:i2])
        if debug:
            spectra = read_spectra(specfile)
            for igal in np.arange(spectra.num_targets()):
                qafile = 'lenssource-spectra-chunk{:03d}-{}.png'.format(ichunk, igal)
                fig, ax = plt.subplots()
                for band in spectra.bands:
                    ax.plot(spectra.wave[band], spectra.flux[band][igal, :])
                ax.plot(simwave, simflux[i1:i2, :][igal, :], color='k', lw=2)
                ax.set_ylim(np.median(simflux[i1:i2, :][igal, :]) + np.std(spectra.flux['r'][igal, :]) * np.array([-1.5, 3]))
                fig.savefig(qafile)
                plt.close()
                
    # write out and return
    hduflux = fits.PrimaryHDU(simflux)
    hduflux.header['EXTNAME'] = 'FLUX'
    hduflux.header['BUNIT'] = '10^(-17) erg/(s cm2 Angstrom)'

    hdubgs = fits.ImageHDU(BGSflux)
    hdubgs.header['EXTNAME'] = 'BGSFLUX'

    hduelg = fits.ImageHDU(ELGflux)
    hduelg.header['EXTNAME'] = 'ELGFLUX'

    hduwave = fits.ImageHDU(simwave)
    hduwave.header['EXTNAME'] = 'WAVE'
    hduwave.header['BUNIT'] = 'Angstrom'
    hduwave.header['AIRORVAC'] = ('vac', 'vacuum wavelengths')

    hdutable = fits.convenience.table_to_hdu(siminfo)
    hdutable.header['EXTNAME'] = 'METADATA'

    hx = fits.HDUList([hduflux, hdubgs, hduelg, hduwave, hdutable])

    print('Writing {}'.format(infofile))
    hx.writeto(infofile, overwrite=True)

    return siminfo
Beispiel #13
0
    def __init__(self, nmodel=50, minwave=3600.0, maxwave=10000.0,
                 cdelt=2.0, wave=None, seed=None):
        """Read the QSO basis continuum templates, grzW1W2 filter profiles and initialize 
           the output wavelength array.

        Note: 
          * Only a linearly-spaced output wavelength array is currently supported.
          * The basis templates are  only defined in the range 3500-10000 A (observed). 

        Args:
          objtype (str): object type
          nmodel (int, optional): Number of models to generate (default 50). 
          minwave (float, optional): minimum value of the output wavelength
            array [default 3600 Angstrom].
          maxwave (float, optional): minimum value of the output wavelength
            array [default 10000 Angstrom].
          cdelt (float, optional): spacing of the output wavelength array
            [default 2 Angstrom/pixel].
          wave (numpy.ndarray): Input/output observed-frame wavelength array,
            overriding the minwave, maxwave, and cdelt arguments [Angstrom].
          seed (long, optional): input seed for the random numbers
    
        Attributes:
          objtype (str): See Args.
          nmodel (int): See Args.
          seed (long): See Args.
          rand (numpy.RandomState): instance of numpy.random.RandomState(seed)
          wave (numpy.ndarray): Output wavelength array [Angstrom].
          baseflux (numpy.ndarray): Array [nbase,npix] of the base rest-frame
            QSO continuum spectra [erg/s/cm2/A].
          basewave (numpy.ndarray): Array [npix] of rest-frame wavelengths 
            corresponding to BASEFLUX [Angstrom].
          basemeta (astropy.Table): Table of meta-data for each base template [nbase].
          gfilt (FILTERFUNC instance): DECam g-band filter profile class.
          rfilt (FILTERFUNC instance): DECam r-band filter profile class.
          zfilt (FILTERFUNC instance): DECam z-band filter profile class.
          w1filt (FILTERFUNC instance): WISE W1-band filter profile class.
          w2filt (FILTERFUNC instance): WISE W2-band filter profile class.

        """
        from desisim.filterfunc import filterfunc as filt
        from desisim.io import read_basis_templates

        self.objtype = 'QSO'
        self.nmodel = nmodel
        self.seed = seed
        self.rand = np.random.RandomState(seed=self.seed)

        # Initialize the output wavelength array (linear spacing) unless it is
        # already provided.
        if wave is None:
            npix = (maxwave-minwave)/cdelt+1
            wave = np.linspace(minwave,maxwave,npix) 
        self.wave = wave

        # Read the basis spectra.
        baseflux, basewave, basemeta = read_basis_templates(objtype=self.objtype)
        self.baseflux = baseflux
        self.basewave = basewave
        self.basemeta = basemeta

        # Initialize the filter profiles.
        self.gfilt = filt(filtername='decam_g.txt')
        self.rfilt = filt(filtername='decam_r.txt')
        self.zfilt = filt(filtername='decam_z.txt')
        self.w1filt = filt(filtername='wise_w1.txt')
        self.w2filt = filt(filtername='wise_w2.txt')