Skip to content

Commit

Permalink
add flux to BGS and MWS too
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephen Bailey authored and Stephen Bailey committed Nov 30, 2016
1 parent 1aee747 commit 6f5adba
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 30 deletions.
29 changes: 27 additions & 2 deletions py/desitarget/mock/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import desitarget.mock.selection as mockselect
from desitarget import obsconditions
from desitarget import mtl

import desispec.brick
from desispec.brick import Bricks
import desitarget.QA as targetQA
Expand Down Expand Up @@ -340,7 +341,7 @@ def targets_truth(params, output_dir, realtargets=None):
true_type_total = np.empty(0, dtype='S10')
source_type_total = np.empty(0, dtype='S10')
obsconditions_total = np.empty(0, dtype='uint16')

decam_flux = np.empty((0,6), dtype='f4')

print('Collects information across mock files')
for source_name in sorted(source_defs.keys()):
Expand All @@ -359,8 +360,10 @@ def targets_truth(params, output_dir, realtargets=None):
desi_target = target_mask[ii]
if target_name in ['BGS']:
bgs_target = target_mask[ii]
desi_target |= desi_mask.BGS_ANY
if target_name in ['MWS_MAIN', 'MWS_WD']:
mws_target = target_mask[ii]
desi_target |= desi_mask.MWS_ANY


# define names that go into Truth
Expand Down Expand Up @@ -422,7 +425,27 @@ def targets_truth(params, output_dir, realtargets=None):
source_type_total = np.append(source_type_total, source_type)
obsconditions_total = np.append(obsconditions_total, source_obsconditions)

#- Add fluxes, which default to 0 if the mocks don't have them
if 'DECAMr_true' in source_data and 'DECAMr_obs' not in source_data:
from desitarget.mock import sfdmap
ra = source_data['RA']
dec = source_data['DEC']
ebv = sfdmap.ebv(ra, dec, mapdir=params['dust_dir'])
#- Magic number for extinction coefficient from https://github.com/dstndstn/tractor/blob/39f883c811f0a6b17a44db140d93d4268c6621a1/tractor/sfd.py
source_data['DECAMr_obs'] = source_data['DECAMr_true'] + ebv*2.165

if 'DECAM_FLUX' in source_data:
decam_flux = np.append(decam_flux, source_data['DECAM_FLUX'][ii])
else:
n = len(desi_target)
tmpflux = np.zeros((n,6), dtype='f4')
if 'DECAMg_obs' in source_data:
tmpflux[:,1] = source_data['DECAMg_obs'][ii]
if 'DECAMr_obs' in source_data:
tmpflux[:,2] = source_data['DECAMr_obs'][ii]
if 'DECAMz_obs' in source_data:
tmpflux[:,4] = source_data['DECAMz_obs'][ii]
decam_flux = np.vstack([decam_flux, tmpflux])

print('source {} target {} truth {}: selected {} out of {}'.format(
source_name, target_name, truth_name, len(source_data['RA'][ii]), len(source_data['RA'])))
Expand Down Expand Up @@ -501,7 +524,9 @@ def targets_truth(params, output_dir, realtargets=None):
targets['SUBPRIORITY'] = subprior
targets['OBSCONDITIONS'] = obsconditions_total
brickname = desispec.brick.brickname(targets['RA'], targets['DEC'])
targets['BRICKNAME'] = brickname
targets['BRICKNAME'] = brickname

targets['DECAM_FLUX'] = decam_flux
add_mock_shapes_and_fluxes(targets, realtargets)
targets.write(targets_filename, overwrite=True)
print('Finished writing Targets file')
Expand Down
48 changes: 28 additions & 20 deletions py/desitarget/mock/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,31 +131,35 @@ def _load_mock_mws_file(filename):
'SDSS[grz]_obs': :class: `numpy.ndarray`
Apparent magnitudes in SDSS grz bands, including extinction.
"""
import desitarget.photo
print('Reading '+filename)
C_LIGHT = 299792.458
desitarget.io.check_fitsio_version()
data = fitsio.read(filename,
columns= ['objid','RA','DEC','v_helio','d_helio', 'SDSSr_true',
'SDSSr_obs', 'SDSSg_obs', 'SDSSz_obs'])


'SDSSg_obs', 'SDSSr_obs', 'SDSSi_obs', 'SDSSz_obs'])

objid = data['objid'].astype('i8')
ra = data['RA'].astype('f8') % 360.0 #enforce 0 < ra < 360
dec = data['DEC'].astype('f8')
v_helio = data['v_helio'].astype('f8')
d_helio = data['d_helio'].astype('f8')
SDSSr_true = data['SDSSr_true'].astype('f8')
SDSSg_obs = data['SDSSg_obs'].astype('f8')
SDSSr_obs = data['SDSSr_obs'].astype('f8')
SDSSz_obs = data['SDSSz_obs'].astype('f8')

return {'objid':objid,
v_helio = data['v_helio'].astype('f4')
d_helio = data['d_helio'].astype('f4')
SDSSr_true = data['SDSSr_true'].astype('f4')
SDSSg_obs = data['SDSSg_obs'].astype('f4')
SDSSr_obs = data['SDSSr_obs'].astype('f4')
SDSSi_obs = data['SDSSi_obs'].astype('f4')
SDSSz_obs = data['SDSSz_obs'].astype('f4')

DECAMg_obs, DECAMr_obs, DECAMz_obs = \
desitarget.photo.sdss2decam(SDSSg_obs, SDSSr_obs, SDSSi_obs, SDSSz_obs)

return {'objid': objid,
'RA':ra, 'DEC':dec, 'Z': v_helio/C_LIGHT,
'd_helio': d_helio,
'SDSSr_true': SDSSr_true, 'SDSSr_obs': SDSSr_obs,
'SDSSg_obs' : SDSSg_obs, 'SDSSz_obs' : SDSSz_obs}


'SDSSr_true': DECAMr_obs,
'DECAMr_obs': DECAMr_obs,
'DECAMg_obs': DECAMg_obs,
'DECAMz_obs': DECAMz_obs }

############################################################
def _load_mock_lya_file(filename):
Expand Down Expand Up @@ -324,7 +328,7 @@ def read_galaxia(mock_dir, target_type, mock_name=None):
Heliocentric radial velocity divided by the speed of light.
'SDSSr_true': :class: `numpy.ndarray`
Apparent magnitudes in SDSS bands, including extinction.
'SDSSr_obs': :class: `numpy.ndarray`
'DECAMr_obs': :class: `numpy.ndarray`
Apparent magnitudes in SDSS bands, including extinction.
"""
# Build iterator of all desi_galfast files
Expand Down Expand Up @@ -382,7 +386,7 @@ def read_galaxia(mock_dir, target_type, mock_name=None):
# Count number of points per file
k = list(target_list[0])[0] # pick the first available column
n_per_file = [len(target_list[itarget][k]) for itarget in file_order]
odered_file_list = [file_list[itarget] for itarget in file_order]
ordered_file_list = [file_list[itarget] for itarget in file_order]

print('Read {} objects'.format(np.sum(n_per_file)))

Expand Down Expand Up @@ -521,7 +525,7 @@ def read_durham_mxxl_hdf5(mock_dir, target_type, mock_name=None):
RA : RA positions for the objects in the mock.
DEC : DEC positions for the objects in the mock.
Z : Heliocentric radial velocity divided by the speed of light.
SDSSr_true : Apparent magnitudes in SDSS r band.
DECAMr_true : Apparent magnitudes in SDSS r band.
"""

filename = os.path.join(mock_dir, target_type+'.hdf5')
Expand All @@ -532,15 +536,19 @@ def read_durham_mxxl_hdf5(mock_dir, target_type, mock_name=None):
zred = f["Data/z_obs"][...].astype('f8')
f.close()

#- Convert SDSSr to DECAMr for a typical BGS target with (r-i)=0.4
DECAMr_true = SDSSr_true - 0.03587 - 0.14144*0.4 #- DESI-1788v1 eqn 5

print('read {} lines from {}'.format(len(ra), filename))

files = list()
files.append(filename)
n_per_file = list()
n_per_file.append(len(ra))

return {'RA':ra, 'DEC':dec, 'Z': zred ,
'SDSSr_true':SDSSr_true, 'FILES': files, 'N_PER_FILE': n_per_file}
return {'RA':ra, 'DEC':dec, 'Z': zred,
'DECAMr_true': DECAMr_true,
'FILES': files, 'N_PER_FILE': n_per_file}

############################################################
def read_mock_durham(core_filename, photo_filename):
Expand Down
16 changes: 8 additions & 8 deletions py/desitarget/mock/selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ def mag_select(data, sourcename, targetname, truthname, brick_info=None, density
grcolor = kwargs['grcolor']
rzcolor = kwargs['rzcolor']
colortol = kwargs['colortol']

SELECTION_MAG_NAME = 'SDSSr_obs'
COLOR_G_NAME = 'SDSSg_obs'
COLOR_R_NAME = 'SDSSr_obs'
COLOR_Z_NAME = 'SDSSz_obs'
SELECTION_MAG_NAME = 'DECAMr_obs'
COLOR_G_NAME = 'DECAMg_obs'
COLOR_R_NAME = 'DECAMr_obs'
COLOR_Z_NAME = 'DECAMz_obs'

# Will populate this array with the bitmask values of each target class
target_class = np.zeros(len(data[SELECTION_MAG_NAME]),dtype=np.int64) - 1
Expand All @@ -140,7 +140,7 @@ def mag_select(data, sourcename, targetname, truthname, brick_info=None, density
mag_faint_filler = kwargs['mag_faint_filler']

# Parameters
SELECTION_MAG_NAME = 'SDSSr_obs'
SELECTION_MAG_NAME = 'DECAMr_obs'

# Will populate this array with the bitmask values of each target class
target_class = np.zeros(len(data[SELECTION_MAG_NAME]),dtype=np.int64) - 1
Expand Down Expand Up @@ -196,7 +196,7 @@ def mag_select(data, sourcename, targetname, truthname, brick_info=None, density
dec = data['DEC']

# Parameters
SELECTION_MAG_NAME = 'SDSSr_true'
SELECTION_MAG_NAME = 'DECAMr_true'
DEPTH_MAG_NAME = 'GALDEPTH_R'

# Will populate this array with the bitmask values of each target class
Expand Down Expand Up @@ -224,7 +224,7 @@ def mag_select(data, sourcename, targetname, truthname, brick_info=None, density
warnings.warn("Tile is on the border. Extinction = 99.0. Depth = 0.0", RuntimeWarning)
else:
depth = brick_info[DEPTH_MAG_NAME][id_binfo]
extinction = brick_info['EBV'][id_binfo] * extinctions['SDSSr']
extinction = brick_info['EBV'][id_binfo] * extinctions['DESr']
# print('DEPTH {} Ext {}'.format(depth, extinction))

tmp = data[SELECTION_MAG_NAME][in_brick] + extinction
Expand Down

0 comments on commit 6f5adba

Please sign in to comment.