Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mock_select_target reads Buzzard mocks #657

Closed
wants to merge 8 commits into from
11 changes: 8 additions & 3 deletions py/desitarget/mock/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ def get_spectra_onepixel(data, indx, MakeMock, seed, log, ntarget,
while makemore:
chunkflux, _, chunktargets, chunktruth, chunkobjtruth = MakeMock.make_spectra(
data, indx=iterindx[itercount], seed=iterseeds[itercount], no_spectra=no_spectra)

MakeMock.select_targets(chunktargets, chunktruth, targetname=data['TARGET_NAME'])

keep = np.where(chunktargets['DESI_TARGET'] != 0)[0]
Expand Down Expand Up @@ -401,6 +401,9 @@ def density_fluctuations(data, log, nside, nside_chunk, seed=None):

# Assign targets to healpix chunks.
#ntarget = len(data['RA'])
# Get the healpix index for objects in nside_chunk pixelization
# Get the healpix index for objects in nside_chunk pixelization
# Get the healpix index for objects in nside_chunk pixelization
healpix_chunk = radec2pix(nside_chunk, data['RA'], data['DEC'])

#if 'CONTAM_FACTOR' in data.keys():
Expand All @@ -415,12 +418,13 @@ def density_fluctuations(data, log, nside, nside_chunk, seed=None):
for pixchunk in set(healpix_chunk):

# Subsample the targets on this mini healpixel.
allindxthispix = np.where( np.in1d(healpix_chunk, pixchunk)*1 )[0]
allindxthispix = np.nonzero( np.in1d(healpix_chunk, pixchunk)*1 )[0]

if 'CONTAM_NUMBER' in data.keys():
ntargthispix = np.round( data['CONTAM_NUMBER'] / nchunk ).astype(int)
indxthispix = rand.choice(allindxthispix, size=5 * ntargthispix, replace=False) # fudge factor!
else:
# Downsize the number of target in this chunk healpix by the density factor
ntargthispix = np.round( len(allindxthispix) * density_factor ).astype('int')
indxthispix = allindxthispix
#indxthispix = rand.choice(allindxthispix, size=ntargthispix, replace=False)
Expand Down Expand Up @@ -495,6 +499,7 @@ def get_spectra(data, MakeMock, log, nside, nside_chunk, seed=None,

# Parallelize by chunking the sample into smaller healpixels and
# determine the number of targets per chunk.
# area here is area per nside pixel
indxperchunk, ntargperchunk, area = density_fluctuations(
data, log, nside=nside, nside_chunk=nside_chunk, seed=seed)

Expand Down Expand Up @@ -900,7 +905,7 @@ def targets_truth(params, healpixels=None, nside=None, output_dir='.',
for ii, target_name in enumerate(sorted(params['targets'].keys())):
targets, truth, skytargets, skytruth = [], [], [], []

# Read the data and ithere are no targets, keep going.
# Read the data and if there are no targets, keep going.
log.info('Working on target class {} on healpixel {}'.format(target_name, healpix))
data, MakeMock = read_mock(params['targets'][target_name], log, target_name,
seed=healseed, healpixels=healpix,
Expand Down
78 changes: 60 additions & 18 deletions py/desitarget/mock/mockmaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,14 @@ def scatter_photometry(self, data, truth, targets, indx=None,
targets[fluxkey][these] = truth[fluxkey][these] + rand.normal(scale=sigma)

targets[ivarkey][these] = 1 / sigma**2
else:
# For Buzzard
for band in ('G', 'R', 'Z'):
fluxkey = 'FLUX_{}'.format(band)
ivarkey = 'FLUX_IVAR_{}'.format(band)

sigma = 1/np.sqrt(data[ivarkey][indx])
targets[fluxkey]=truth[fluxkey] + rand.normal(scale=sigma)

# WISE sources are all point sources
for band in ('W1', 'W2'):
Expand Down Expand Up @@ -596,6 +604,8 @@ def sample_GMM(self, nobj, isouth=None, target=None, seed=None, morph=None,

# Next, sample from the GMM for each morphological type. For
# simplicity we ignore the north-south split here.


gmmout = {'MAGFILTER': np.zeros(nobj).astype('U15'), 'TYPE': np.zeros(nobj).astype('U4')}
for key in ('MAG', 'FRACDEV', 'FRACDEV_IVAR',
'SHAPEDEV_R', 'SHAPEDEV_R_IVAR', 'SHAPEDEV_E1', 'SHAPEDEV_E1_IVAR',
Expand Down Expand Up @@ -848,18 +858,21 @@ def _g_and_r(zmag, gr, rz):
# rmag, zmag = _r_and_z(gmag, gr, rz)

if target_name == 'LRG':
if contaminants:
if self.mockformat=='buzzard':
zmag = normmag = data['ZMAG'][indx]
magfilter = data['MAGFILTER-Z'][indx]
else:
zmag = normmag = data['MAG'][indx]
magfilter = data['MAGFILTER'][indx]
gmag, rmag = _g_and_r(zmag, gr, rz)

else:
rmag = normmag = data['MAG'][indx]
magfilter = data['MAGFILTER'][indx]
gmag, zmag = _g_and_z(rmag, gr, rz)



W1mag = zmag - data['ZW1'][indx]
W2mag = W1mag - data['W1W2'][indx]

Expand All @@ -873,6 +886,7 @@ def _g_and_r(zmag, gr, rz):
meta['FLUX_Z'][:] = 1e9 * 10**(-0.4 * zmag)
meta['FLUX_W1'][:] = 1e9 * 10**(-0.4 * W1mag)
meta['FLUX_W2'][:] = 1e9 * 10**(-0.4 * W2mag)


def get_fiberfraction(self, targets, south=True, ref_seeing=1.0, ref_lambda=5500.0):
"""Estimate the fraction of the integrated flux that enters the fiber.
Expand Down Expand Up @@ -1485,7 +1499,8 @@ def __init__(self, **kwargs):
super(ReadBuzzard, self).__init__(**kwargs)

def readmock(self, mockfile=None, healpixels=[], nside=[], nside_buzzard=8,
target_name='', magcut=None, only_coords=False, seed=None):
target_name='', magcut=None, only_coords=False, seed=None,mock_density=None):

"""Read the catalog.

Parameters
Expand Down Expand Up @@ -1608,11 +1623,13 @@ def readmock(self, mockfile=None, healpixels=[], nside=[], nside_buzzard=8,
dec = radec['DEC'][cut].astype('f8')
del radec

cols = ['Z', 'TMAG']
cols = ['Z', 'TMAG', 'TMAG_WISE', 'IVAR']
#cols = ['Z', 'COEFFS', 'TMAG']
data = fitsio.read(buzzardfile, columns=cols, upper=True, ext=1, rows=cut)
zz = data['Z'].astype('f4')
tmag = data['TMAG'].astype('f4')
tmag_wise = data['TMAG_WISE'].astype('f4')
ivar =data['IVAR'].astype('f4')

if magcut:
cut = tmag[:, 2] < magcut # r-band
Expand All @@ -1639,16 +1656,25 @@ def readmock(self, mockfile=None, healpixels=[], nside=[], nside_buzzard=8,

isouth = self.is_south(dec)

## Get photometry and morphologies by sampling from the Gaussian
## mixture models.
#log.info('Sampling from {} Gaussian mixture model.'.format(target_name))
#gmmout = self.sample_GMM(nobj, target=target_name, isouth=isouth,
# Get photometry and morphologies by sampling from the Gaussian
# mixture models.
# log.info('Sampling from {} Gaussian mixture model.'.format(target_name))
# gmmout = self.sample_GMM(nobj, target=target_name, isouth=isouth,
# seed=seed, prior_redshift=zz)
#gmmout = None
# gmmout = None

gmag = data['TMAG'][:, 0].astype('f4') # DES g-band, no MW extinction
rmag = data['TMAG'][:, 1].astype('f4') # DES r-band, no MW extinction
zmag = data['TMAG'][:, 3].astype('f4') # DES z-band, no MW extinction
w1mag = data['TMAG_WISE'][:,0].astype('f4') # WISE W1 band
w2mag = data['TMAG_WISE'][:,1].astype('f4') # WISE W2 band
ivar_g = data['IVAR'][:,0].astype('f4')
ivar_r = data['IVAR'][:,1].astype('f4')
ivar_i = data['IVAR'][:,2].astype('f4')
ivar_z = data['IVAR'][:,3].astype('f4')
# w3mag = data['TMAG_WISE'][:,2].astype('f4')
# w4mag = data['TMAG_WISE'][:,3].astype('f4')

#gmag = data['TMAG'][:, 1].astype('f4') # DES g-band, no MW extinction
rmag = data['TMAG'][:, 2].astype('f4') # DES r-band, no MW extinction
zmag = data['TMAG'][:, 4].astype('f4') # DES z-band, no MW extinction

# Pack into a basic dictionary.
out = {'TARGET_NAME': target_name, 'MOCKFORMAT': 'buzzard',
Expand All @@ -1657,20 +1683,30 @@ def readmock(self, mockfile=None, healpixels=[], nside=[], nside_buzzard=8,
'BRICKID': self.Bricks.brickid(ra, dec),
'RA': ra, 'DEC': dec, 'Z': zz,
'MAG': rmag, 'MAGFILTER': np.repeat('decam2014-r', nobj),
#'GMAG': gmag, 'MAGFILTER-G': np.repeat('decam2014-g', nobj),
'GMAG': gmag, 'MAGFILTER-G': np.repeat('decam2014-g', nobj),
'ZMAG': zmag, 'MAGFILTER-Z': np.repeat('decam2014-z', nobj),
'SOUTH': isouth}
'W1MAG':w1mag, 'MAGFILTER-W1':np.repeat('wise2010-w1',nobj),
'W2MAG':w2mag, 'MAGFILTER-W2':np.repeat('wise2010-w2',nobj),
'GR': gmag - rmag,
'RZ': rmag - zmag,
'ZW1': zmag - w1mag,
'W1W2': w1mag - w2mag,
'SOUTH': isouth,
'FLUX_IVAR_G':ivar_g,
'FLUX_IVAR_R':ivar_r,
'FLUX_IVAR_Z':ivar_z,
}

#if gmmout is not None:
# out.update(gmmout)
# if gmmout is not None:
# out.update(gmmout)

# Add MW transmission and the imaging depth.
self.mw_transmission(out)
self.imaging_depth(out)

## Optionally compute the mean mock density.
#if mock_density:
# out['MOCK_DENSITY'] = self.mock_density(mockfile=mockfile)
if mock_density:
out['MOCK_DENSITY'] = self.mock_density(mockfile=buzzardfile)

return out

Expand Down Expand Up @@ -3624,6 +3660,8 @@ def read(self, mockfile=None, mockformat='gaussianfield', healpixels=None,
self.default_mockfile = os.path.join(
os.getenv('DESI_ROOT'), 'mocks', 'DarkSky', 'v1.0.1', 'lrg_0_inpt.fits')
MockReader = ReadGaussianField()
elif self.mockformat=='buzzard':
MockReader = ReadBuzzard()
else:
log.warning('Unrecognized mockformat {}!'.format(mockformat))
raise ValueError
Expand Down Expand Up @@ -3693,7 +3731,7 @@ def make_spectra(self, data=None, indx=None, seed=None, no_spectra=False):
south = np.where( data['SOUTH'][indx] == True )[0]
north = np.where( data['SOUTH'][indx] == False )[0]

if self.mockformat == 'gaussianfield':
if self.mockformat == 'gaussianfield' or self.mockformat == 'buzzard':
for these, issouth in zip( (north, south), (False, True) ):
if len(these) > 0:
input_meta['MAG'][these] = data['MAG'][indx][these]
Expand Down Expand Up @@ -3847,6 +3885,8 @@ def read(self, mockfile=None, mockformat='gaussianfield', healpixels=None,
self.default_mockfile = os.path.join(
os.getenv('DESI_ROOT'), 'mocks', 'DarkSky', 'v1.0.1', 'elg_0_inpt.fits')
MockReader = ReadGaussianField()
elif self.mockformat == 'buzzard':
MockReader = ReadBuzzard()
else:
log.warning('Unrecognized mockformat {}!'.format(mockformat))
raise ValueError
Expand Down Expand Up @@ -4060,6 +4100,8 @@ def read(self, mockfile=None, mockformat='durham_mxxl_hdf5', healpixels=None,
MockReader = ReadGaussianField()
elif self.mockformat == 'bgs-gama':
MockReader = ReadGAMA()
elif self.mockformat == 'buzzard':
MockReader = ReadBuzzard()
else:
log.warning('Unrecognized mockformat {}!'.format(mockformat))
raise ValueError
Expand Down