Skip to content

Commit

Permalink
Merge pull request #2067 from desihub/add-night-mjd-cols
Browse files Browse the repository at this point in the history
add NIGHT and MJD columns
  • Loading branch information
sbailey committed Jun 7, 2023
2 parents 2a78d8b + def822b commit 8328a50
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 29 deletions.
64 changes: 35 additions & 29 deletions py/desispec/coaddition.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def coadd_fibermap(fibermap, onetile=False):
mean_cols = [
'DELTA_X', 'DELTA_Y',
'FIBER_RA', 'FIBER_DEC',
'PSF_TO_FIBER_SPECFLUX',
'PSF_TO_FIBER_SPECFLUX', 'MJD'
]
# Note: treat the fiber coordinates separately because of missing coordinate problem
# that require an additional "good_coords" condition relative to other mean cols
Expand All @@ -193,7 +193,7 @@ def coadd_fibermap(fibermap, onetile=False):

for k in mean_cols:
if k in fibermap.colnames :
if k.endswith('_RA') or k.endswith('_DEC'):
if k.endswith('_RA') or k.endswith('_DEC') or k=='MJD':
dtype = np.float64
else:
dtype = np.float32
Expand All @@ -208,24 +208,26 @@ def coadd_fibermap(fibermap, onetile=False):
tfmap.add_column(xx,name='STD_'+k)

tfmap.remove_column(k)

#- TODO: should any of these be retained?
# first_last_cols = ['NIGHT','EXPID','TILEID','SPECTROID','FIBER','MJD']
# for k in first_last_cols:
# if k in fibermap.colnames :
# if k in ['MJD']:
# dtype = np.float32
# else:
# dtype = np.int32
# if not 'FIRST_'+k in tfmap.dtype.names :
# xx = Column(np.arange(ntarget, dtype=dtype))
# tfmap.add_column(xx,name='FIRST_'+k)
# if not 'LAST_'+k in tfmap.dtype.names :
# xx = Column(np.arange(ntarget, dtype=dtype))
# tfmap.add_column(xx,name='LAST_'+k)
# if not 'NUM_'+k in tfmap.dtype.names :
# xx = Column(np.arange(ntarget, dtype=np.int16))
# tfmap.add_column(xx,name='NUM_'+k)

#- MIN_, MAX_MJD
if 'MJD' in fibermap.colnames :
dtype = np.float64
if not 'MIN_MJD' in tfmap.dtype.names :
xx = Column(np.arange(ntarget, dtype=dtype))
tfmap.add_column(xx,name='MIN_MJD')
if not 'MAX_MJD' in tfmap.dtype.names :
xx = Column(np.arange(ntarget, dtype=dtype))
tfmap.add_column(xx,name='MAX_MJD')

#- FIRSTNIGHT, LASTNIGHT
if 'NIGHT' in fibermap.colnames :
dtype = np.int32
if not 'FIRSTNIGHT' in tfmap.dtype.names :
xx = Column(np.arange(ntarget, dtype=dtype))
tfmap.add_column(xx,name='FIRSTNIGHT')
if not 'LASTNIGHT' in tfmap.dtype.names :
xx = Column(np.arange(ntarget, dtype=dtype))
tfmap.add_column(xx,name='LASTNIGHT')

if 'FIBERSTATUS' in tfmap.dtype.names :
tfmap.rename_column('FIBERSTATUS', 'COADD_FIBERSTATUS')
Expand Down Expand Up @@ -318,15 +320,19 @@ def coadd_fibermap(fibermap, onetile=False):
vals=fibermap[k][jj][compute_coadds]
# STD removes mean offset, not same as RMS
tfmap['STD_'+k][i] = np.std(vals).astype(np.float32)

#- TODO: see above, should any of these be retained?
# for k in first_last_cols:
# if k in fibermap.colnames :
# vals=fibermap[k][jj]
# tfmap['FIRST_'+k][i] = np.min(vals)
# tfmap['LAST_'+k][i] = np.max(vals)
# tfmap['NUM_'+k][i] = np.unique(vals).size


# MIN_, MAX_MJD
if 'MJD' in fibermap.colnames :
vals=fibermap['MJD'][jj][compute_coadds]
tfmap['MIN_MJD'][i] = np.min(vals)
tfmap['MAX_MJD'][i] = np.max(vals)

# FIRST, LASTNIGHT
if 'NIGHT' in fibermap.colnames :
vals=fibermap['NIGHT'][jj][compute_coadds]
tfmap['FIRSTNIGHT'][i] = np.min(vals)
tfmap['LASTNIGHT'][i] = np.max(vals)

# Error propagation of IVAR values when taking an unweighted MEAN
#- (Note 1: IVAR will be 0.0 if any of ivar[compute_coadds]=0)
#- (Note 2: these columns are place-holder for possible future use)
Expand Down
54 changes: 54 additions & 0 deletions py/desispec/test/test_coadd.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,60 @@ def test_coadd_fibermap_badradec(self):
self.assertTrue(np.allclose(cofm['MEAN_FIBER_RA'], 0.0))
self.assertTrue(np.allclose(cofm['MEAN_FIBER_DEC'], 0.0))

def test_coadd_fibermap_mjd_night(self):
"""Test adding MIN/MAX/MEAN_MJD and FIRST/LASTNIGHT columns"""
nspec = 5
fm = Table()
fm['TARGETID'] = 111 * np.ones(nspec, dtype=int)
fm['DESI_TARGET'] = 4 * np.ones(nspec, dtype=int)
fm['TILEID'] = np.ones(nspec, dtype=int)
fm['NIGHT'] = 20201220 + np.arange(nspec)
fm['EXPID'] = np.arange(nspec, dtype=int)
fm['FIBERSTATUS'] = np.zeros(nspec, dtype=int)

#- with NIGHT but not MJD
cofm, expfm = coadd_fibermap(fm, onetile=True)
self.assertEqual(len(cofm), 1) #- single target in this test
self.assertEqual(cofm['FIRSTNIGHT'][0], np.min(fm['NIGHT']))
self.assertEqual(cofm['LASTNIGHT'][0], np.max(fm['NIGHT']))
self.assertNotIn('MIN_MJD', cofm.colnames)

#- also with MJD
fm['MJD'] = 55555 + np.arange(nspec)
cofm, expfm = coadd_fibermap(fm, onetile=True)
self.assertEqual(cofm['MIN_MJD'][0], np.min(fm['MJD']))
self.assertEqual(cofm['MAX_MJD'][0], np.max(fm['MJD']))
self.assertEqual(cofm['MEAN_MJD'][0], np.mean(fm['MJD']))

#- with some fibers masked
fm['FIBERSTATUS'][0] = fibermask.BADFIBER #- bad
fm['FIBERSTATUS'][1] = fibermask.RESTRICTED #- ok
fm['FIBERSTATUS'][2] = fibermask.BADAMPR #- ok for fibermap
ok = np.ones(nspec, dtype=bool)
ok[0] = False
cofm, expfm = coadd_fibermap(fm, onetile=True)
self.assertEqual(cofm['FIRSTNIGHT'][0], np.min(fm['NIGHT'][ok]))
self.assertEqual(cofm['LASTNIGHT'][0], np.max(fm['NIGHT'][ok]))
self.assertEqual(cofm['MIN_MJD'][0], np.min(fm['MJD'][ok]))
self.assertEqual(cofm['MAX_MJD'][0], np.max(fm['MJD'][ok]))
self.assertEqual(cofm['MEAN_MJD'][0], np.mean(fm['MJD'][ok]))

#- multiple targets
fm['TARGETID'][0:2] += 1
fm['FIBERSTATUS'] = 0
cofm, expfm = coadd_fibermap(fm, onetile=True)
self.assertEqual(cofm['FIRSTNIGHT'][0], np.min(fm['NIGHT'][0:2]))
self.assertEqual(cofm['LASTNIGHT'][0], np.max(fm['NIGHT'][0:2]))
self.assertEqual(cofm['MIN_MJD'][0], np.min(fm['MJD'][0:2]))
self.assertEqual(cofm['MAX_MJD'][0], np.max(fm['MJD'][0:2]))
self.assertEqual(cofm['MEAN_MJD'][0], np.mean(fm['MJD'][0:2]))
self.assertEqual(cofm['FIRSTNIGHT'][1], np.min(fm['NIGHT'][2:]))
self.assertEqual(cofm['LASTNIGHT'][1], np.max(fm['NIGHT'][2:]))
self.assertEqual(cofm['MIN_MJD'][1], np.min(fm['MJD'][2:]))
self.assertEqual(cofm['MAX_MJD'][1], np.max(fm['MJD'][2:]))
self.assertEqual(cofm['MEAN_MJD'][1], np.mean(fm['MJD'][2:]))


def test_fiberstatus(self):
"""Test that FIBERSTATUS != 0 isn't included in coadd"""
def _makespec(nspec, nwave):
Expand Down

0 comments on commit 8328a50

Please sign in to comment.