Exemplo n.º 1
0
def new_exposure(flavor, nspec=5000, night=None, expid=None, tileid=None, \
    airmass=1.0, exptime=None):
    """
    Create a new exposure and output input simulation files.
    Does not generate pixel-level simulations or noisy spectra.
    
    Args:
        nspec (optional): integer number of spectra to simulate
        night (optional): YEARMMDD string
        expid (optional): positive integer exposure ID
        tileid (optional): tile ID
        airmass (optional): airmass, default 1.0
    
    Writes:
        $DESI_SPECTRO_SIM/$PIXPROD/{night}/fibermap-{expid}.fits
        $DESI_SPECTRO_SIM/$PIXPROD/{night}/simspec-{expid}.fits
        
    Returns:
        fibermap numpy structured array
        truth dictionary
    """
    if expid is None:
        expid = get_next_expid()

    if tileid is None:
        tileid = get_next_tileid()

    if night is None:
        #- simulation obs time = now, even if sun is up
        dateobs = time.gmtime()
        night = get_night(utc=dateobs)
    else:
        #- 10pm on night YEARMMDD
        dateobs = time.strptime(night + ':22', '%Y%m%d:%H')

    params = desimodel.io.load_desiparams()
    if flavor == 'arc':
        infile = os.getenv(
            'DESI_ROOT'
        ) + '/spectro/templates/calib/v0.2/arc-lines-average.fits'
        d = fits.getdata(infile, 1)
        wave = d['AIRWAVE']
        phot = d['ELECTRONS']

        truth = dict(WAVE=wave)
        meta = None
        fibermap = desispec.io.fibermap.empty_fibermap(nspec)
        for channel in ('B', 'R', 'Z'):
            thru = desimodel.io.load_throughput(channel)
            ii = np.where((thru.wavemin <= wave) & (wave <= thru.wavemax))[0]
            truth['WAVE_' + channel] = wave[ii]
            truth['PHOT_' + channel] = np.tile(phot[ii],
                                               nspec).reshape(nspec, len(ii))

    elif flavor == 'flat':
        infile = os.getenv(
            'DESI_ROOT'
        ) + '/spectro/templates/calib/v0.2/flat-3100K-quartz-iodine.fits'
        flux = fits.getdata(infile, 0)
        hdr = fits.getheader(infile, 0)
        wave = desispec.io.util.header2wave(hdr)

        #- resample to 0.2 A grid
        dw = 0.2
        ww = np.arange(wave[0], wave[-1] + dw / 2, dw)
        flux = resample_flux(ww, wave, flux)
        wave = ww

        #- Convert to 2D for projection
        flux = np.tile(flux, nspec).reshape(nspec, len(wave))

        truth = dict(WAVE=wave, FLUX=flux)
        meta = None
        fibermap = desispec.io.fibermap.empty_fibermap(nspec)
        for channel in ('B', 'R', 'Z'):
            thru = desimodel.io.load_throughput(channel)
            ii = (thru.wavemin <= wave) & (wave <= thru.wavemax)
            phot = thru.photons(wave[ii],
                                flux[:, ii],
                                units=hdr['BUNIT'],
                                objtype='CALIB',
                                exptime=10)

            truth['WAVE_' + channel] = wave[ii]
            truth['PHOT_' + channel] = phot

    elif flavor == 'science':
        fibermap, truth = get_targets(nspec, tileid=tileid)

        flux = truth['FLUX']
        wave = truth['WAVE']
        nwave = len(wave)

        if exptime is None:
            exptime = params['exptime']

        #- Load sky [Magic knowledge of units 1e-17 erg/s/cm2/A/arcsec2]
        skyfile = os.getenv('DESIMODEL') + '/data/spectra/spec-sky.dat'
        skywave, skyflux = np.loadtxt(skyfile, unpack=True)
        skyflux = np.interp(wave, skywave, skyflux)
        truth['SKYFLUX'] = skyflux

        for channel in ('B', 'R', 'Z'):
            thru = desimodel.io.load_throughput(channel)

            ii = np.where((thru.wavemin <= wave) & (wave <= thru.wavemax))[0]

            #- Project flux to photons
            phot = thru.photons(wave[ii],
                                flux[:, ii],
                                units=truth['UNITS'],
                                objtype=truth['OBJTYPE'],
                                exptime=exptime,
                                airmass=airmass)

            truth['PHOT_' + channel] = phot
            truth['WAVE_' + channel] = wave[ii]

            #- Project sky flux to photons
            skyphot = thru.photons(wave[ii],
                                   skyflux[ii] * airmass,
                                   units='1e-17 erg/s/cm2/A/arcsec2',
                                   objtype='SKY',
                                   exptime=exptime,
                                   airmass=airmass)

            #- 2D version
            ### truth['SKYPHOT_'+channel] = np.tile(skyphot, nspec).reshape((nspec, len(ii)))
            #- 1D version
            truth['SKYPHOT_' + channel] = skyphot.astype(np.float32)

        #- NOTE: someday skyflux and skyphot may be 2D instead of 1D

        #- Extract the metadata part of the truth dictionary into a table
        columns = (
            'OBJTYPE',
            'REDSHIFT',
            'TEMPLATEID',
            'D4000',
            'OIIFLUX',
            'VDISP',
        )
        meta = {key: truth[key] for key in columns}

    #- (end indentation for arc/flat/science flavors)

    #- Override $DESI_SPECTRO_DATA in order to write to simulation area
    datadir_orig = os.getenv('DESI_SPECTRO_DATA')
    simbase = os.path.join(os.getenv('DESI_SPECTRO_SIM'), os.getenv('PIXPROD'))
    os.environ['DESI_SPECTRO_DATA'] = simbase

    #- Write fibermap
    telera, teledec = io.get_tile_radec(tileid)
    hdr = dict(
        NIGHT=(night, 'Night of observation YEARMMDD'),
        EXPID=(expid, 'DESI exposure ID'),
        TILEID=(tileid, 'DESI tile ID'),
        FLAVOR=(flavor, 'Flavor [arc, flat, science, ...]'),
        TELRA=(telera, 'Telescope pointing RA [degrees]'),
        TELDEC=(teledec, 'Telescope pointing dec [degrees]'),
    )
    #- ISO 8601 DATE-OBS year-mm-ddThh:mm:ss
    fiberfile = desispec.io.findfile('fibermap', night, expid)
    desispec.io.write_fibermap(fiberfile, fibermap, header=hdr)
    print fiberfile

    #- Write simspec; expand fibermap header
    hdr['AIRMASS'] = (airmass, 'Airmass at middle of exposure')
    hdr['EXPTIME'] = (exptime, 'Exposure time [sec]')
    hdr['DATE-OBS'] = (time.strftime('%FT%T', dateobs), 'Start of exposure')

    simfile = io.write_simspec(meta, truth, expid, night, header=hdr)
    print(simfile)

    #- Update obslog that we succeeded with this exposure
    update_obslog(flavor, expid, dateobs, tileid)

    #- Restore $DESI_SPECTRO_DATA
    if datadir_orig is not None:
        os.environ['DESI_SPECTRO_DATA'] = datadir_orig
    else:
        del os.environ['DESI_SPECTRO_DATA']

    return fibermap, truth
Exemplo n.º 2
0
import matplotlib.pyplot as plt
import numpy as np
import os
import subprocess

import psql 
import targets
import plots

#load from \o psql dump
m_gal= psql.load_ofile('../truth_table_results/deep2_f2_matched_galaxy.txt',str_kws=['type'])
m_star= psql.load_ofile('../truth_table_results/deep2_f2_matched_star.txt',str_kws=['type'])
un_gal= psql.load_ofile('../truth_table_results/deep2_f2_unmatched_galaxy.txt')
un_star= psql.load_ofile('../truth_table_results/deep2_f2_unmatched_star.txt')
#matched have dr2 tractor catalog data, color cut 
targets.get_targets(m_gal)
targets.get_targets(m_star)

#base name for pngs
base= 'truth_v_dr2_'
#locations of matched, unmatched objects
kwargs=dict(s=50,facecolors='none',linewidths=2.)
plt.scatter(m_gal['dr2_ra'],m_gal['dr2_dec'],edgecolors='b',label='m_gal',marker='o',**kwargs)
plt.scatter(m_star['dr2_ra'],m_star['dr2_dec'],edgecolors='m',label='m_star',marker='*',**kwargs)
plt.scatter(un_gal['ra'],un_gal['dec'],edgecolors='g',label='un_gal',marker='o',**kwargs)
plt.scatter(un_star['ra'],un_star['dec'],edgecolors='y',label='un_star',marker='*',**kwargs)
plt.legend(loc=(1.01,0),scatterpoints=1)
plt.savefig('./'+base+'radec.png')
plt.close()

#r-z vs. g-r plot
Exemplo n.º 3
0
def new_exposure(flavor, nspec=5000, night=None, expid=None, tileid=None, airmass=1.0, \
    exptime=None):
    """
    Create a new exposure and output input simulation files.
    Does not generate pixel-level simulations or noisy spectra.
    
    Args:
        nspec (optional): integer number of spectra to simulate
        night (optional): YEARMMDD string
        expid (optional): positive integer exposure ID
        tileid (optional): tile ID
        airmass (optional): airmass, default 1.0
    
    Writes:
        $DESI_SPECTRO_SIM/$PIXPROD/{night}/fibermap-{expid}.fits
        $DESI_SPECTRO_SIM/$PIXPROD/{night}/simspec-{expid}.fits
        
    Returns:
        fibermap numpy structured array
        truth dictionary
    """
    if expid is None:
        expid = get_next_expid()
    
    if tileid is None:
        tileid = get_next_tileid()

    if night is None:
        #- simulation obs time = now, even if sun is up
        dateobs = time.gmtime()
        night = get_night(utc=dateobs)
    else:
        #- 10pm on night YEARMMDD
        dateobs = time.strptime(night+':22', '%Y%m%d:%H')
    
    params = desimodel.io.load_desiparams()    
    if flavor == 'arc':
        infile = os.getenv('DESI_ROOT')+'/spectro/templates/calib/v0.1/arc-lines-average.fits'
        d = fits.getdata(infile, 1)
        wave = d['AIRWAVE']
        phot = d['ELECTRONS']
        
        truth = dict(WAVE=wave)
        meta = None
        fibermap = desispec.io.fibermap.empty_fibermap(nspec)
        for channel in ('B', 'R', 'Z'):
            thru = desimodel.io.load_throughput(channel)        
            ii = np.where( (thru.wavemin <= wave) & (wave <= thru.wavemax) )[0]
            truth['WAVE_'+channel] = wave[ii]
            truth['PHOT_'+channel] = np.tile(phot[ii], nspec).reshape(nspec, len(ii))

    elif flavor == 'flat':
        infile = os.getenv('DESI_ROOT')+'/spectro/templates/calib/v0.1/flat-3100K-quartz-iodine.fits'
        flux = fits.getdata(infile, 0)
        hdr = fits.getheader(infile, 0)
        wave = desispec.io.util.header2wave(hdr)

        #- resample to 0.2 A grid
        dw = 0.2
        ww = np.arange(wave[0], wave[-1]+dw/2, dw)
        flux = resample_flux(ww, wave, flux)
        wave = ww

        #- Convert to 2D for projection
        flux = np.tile(flux, nspec).reshape(nspec, len(wave))

        truth = dict(WAVE=wave, FLUX=flux)
        meta = None
        fibermap = desispec.io.fibermap.empty_fibermap(nspec)
        for channel in ('B', 'R', 'Z'):
            psf = desimodel.io.load_psf(channel)
            thru = desimodel.io.load_throughput(channel)
            ii = (psf.wmin <= wave) & (wave <= psf.wmax)
            phot = thru.photons(wave[ii], flux[:,ii], units=hdr['BUNIT'], objtype='CALIB')
        
            truth['WAVE_'+channel] = wave[ii]
            truth['PHOT_'+channel] = phot
        
    elif flavor == 'science':
        fibermap, truth = get_targets(nspec, tileid=tileid)
            
        flux = truth['FLUX']
        wave = truth['WAVE']
        nwave = len(wave)
    
        if exptime is None:
            exptime = params['exptime']
    
        #- Load sky [Magic knowledge of units 1e-17 erg/s/cm2/A/arcsec2]
        skyfile = os.getenv('DESIMODEL')+'/data/spectra/spec-sky.dat'
        skywave, skyflux = np.loadtxt(skyfile, unpack=True)
        skyflux = np.interp(wave, skywave, skyflux)
        truth['SKYFLUX'] = skyflux

        for channel in ('B', 'R', 'Z'):
            thru = desimodel.io.load_throughput(channel)
        
            ii = np.where( (thru.wavemin <= wave) & (wave <= thru.wavemax) )[0]
        
            #- Project flux to photons
            phot = thru.photons(wave[ii], flux[:,ii], units='1e-17 erg/s/cm2/A',
                    objtype=truth['OBJTYPE'], exptime=exptime,
                    airmass=airmass)
                
            truth['PHOT_'+channel] = phot
            truth['WAVE_'+channel] = wave[ii]
    
            #- Project sky flux to photons
            skyphot = thru.photons(wave[ii], skyflux[ii]*airmass,
                units='1e-17 erg/s/cm2/A/arcsec2',
                objtype='SKY', exptime=exptime, airmass=airmass)
    
            #- 2D version
            ### truth['SKYPHOT_'+channel] = np.tile(skyphot, nspec).reshape((nspec, len(ii)))
            #- 1D version
            truth['SKYPHOT_'+channel] = skyphot.astype(np.float32)
        
        #- NOTE: someday skyflux and skyphot may be 2D instead of 1D
        
        #- Extract the metadata part of the truth dictionary into a table
        columns = (
            'OBJTYPE',
            'REDSHIFT',
            'TEMPLATEID',
            'O2FLUX',
        )
        meta = _dict2ndarray(truth, columns)
        
    #- (end indentation for arc/flat/science flavors)
        
    #- Write fibermap
    telera, teledec = io.get_tile_radec(tileid)
    hdr = dict(
        NIGHT = (night, 'Night of observation YEARMMDD'),
        EXPID = (expid, 'DESI exposure ID'),
        TILEID = (tileid, 'DESI tile ID'),
        FLAVOR = (flavor, 'Flavor [arc, flat, science, ...]'),
        TELERA = (telera, 'Telescope pointing RA [degrees]'),
        TELEDEC = (teledec, 'Telescope pointing dec [degrees]'),
        )
    fiberfile = desispec.io.findfile('fibermap', night, expid)
    desispec.io.write_fibermap(fiberfile, fibermap, header=hdr)
    print fiberfile
    
    #- Write simfile
    hdr = dict(
        AIRMASS=(airmass, 'Airmass at middle of exposure'),
        EXPTIME=(exptime, 'Exposure time [sec]'),
        FLAVOR=(flavor, 'exposure flavor [arc, flat, science]'),
        )
    simfile = io.write_simspec(meta, truth, expid, night, header=hdr)
    print simfile

    #- Update obslog that we succeeded with this exposure
    update_obslog(flavor, expid, dateobs, tileid)
    
    return fibermap, truth
Exemplo n.º 4
0
import subprocess

import psql
import targets
import plots

#load from \o psql dump
m_gal = psql.load_ofile('../truth_table_results/deep2_f2_matched_galaxy.txt',
                        str_kws=['type'])
m_star = psql.load_ofile('../truth_table_results/deep2_f2_matched_star.txt',
                         str_kws=['type'])
un_gal = psql.load_ofile(
    '../truth_table_results/deep2_f2_unmatched_galaxy.txt')
un_star = psql.load_ofile('../truth_table_results/deep2_f2_unmatched_star.txt')
#matched have dr2 tractor catalog data, color cut
targets.get_targets(m_gal)
targets.get_targets(m_star)

#base name for pngs
base = 'truth_v_dr2_'
#locations of matched, unmatched objects
kwargs = dict(s=50, facecolors='none', linewidths=2.)
plt.scatter(m_gal['dr2_ra'],
            m_gal['dr2_dec'],
            edgecolors='b',
            label='m_gal',
            marker='o',
            **kwargs)
plt.scatter(m_star['dr2_ra'],
            m_star['dr2_dec'],
            edgecolors='m',