Skip to content

Commit

Permalink
frames2spectra fibermap keyword propagation
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephen Bailey authored and Stephen Bailey committed Jul 22, 2024
1 parent 165842e commit 586a163
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
28 changes: 27 additions & 1 deletion py/desispec/pixgroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ def add_missing_frames(frames):
wave[band], flux, ivar, mask, resolution_data,
frame.fibermap, header, scores)

def frames2spectra(frames, pix=None, nside=64):
def frames2spectra(frames, pix=None, nside=64, onetile=False):
'''
Combine a dict of FrameLite into a SpectraLite for healpix `pix`
Expand All @@ -562,10 +562,14 @@ def frames2spectra(frames, pix=None, nside=64):
Options:
pix: only include targets in this NESTED healpix pixel number
nside: Healpix nside, must be power of 2
onetile: All frames come from the same tile (impacts keyword propagation)
Returns:
SpectraLite object with subset of spectra from frames that are in
the requested healpix pixel `pix`
If `onetile` is True, propagate fibermap header keywords that apply
to a single tile, e.g. TILEID and fiberassign keywords
'''
log = get_logger()

Expand Down Expand Up @@ -715,6 +719,28 @@ def frames2spectra(frames, pix=None, nside=64):
outmap['FIBERSTATUS'] |= banddict[band]['FIBERSTATUS']
merged_over_cams_fmaps.append(outmap)

#- Only keep fibermap keywords that apply to combined spectra, not individual exp
keep_keywords = ['SURVEY', 'PROGRAM', 'EXTNAME', 'LONGSTRN',
'INSTRUME', 'OBSERVAT', 'OBS-LAT', 'OBS-LONG', 'OBS-ELEV', 'TELESCOP']

if onetile:
keep_keywords.extend([
'TILEID', 'TILERA', 'TILEDEC', 'FIELDROT',
'FILEDROT', 'FA_PLAN', 'FA_HA', 'FA_RUN', 'FA_M_GFA',
'FA_M_PET', 'FA_M_POS', 'REQRA', 'REQDEC', 'FIELDNUM',
'FA_VER', 'FA_SURV', 'FAFLAVOR', 'FAPRGRM', 'DESIROOT',
'GFA', 'MTL', 'SCND', 'SCND2', 'SCND3', 'SCNDMTL', 'SKY',
'SKYSUPP', 'TARG', 'TOO', 'FAARGS', 'FAOUTDIR',
'NOWTIME', 'RUNDATE', 'PMCORR', 'PMTIME', 'MTLTIME',
'OBSCON', 'GOALTIME', 'GOALTYPE', 'EBVFAC', 'SBPROF',
'MINTFRAC', 'FASCRIPT', 'SVNDM', 'SVNMTL',
])

for fm in merged_over_cams_fmaps:
for key in list(fm.meta.keys()):
if key not in keep_keywords:
del fm.meta[key]

#- Convert to Tables to facilitate column add/drop/reorder
for i, fm in enumerate(merged_over_cams_fmaps):
if not isinstance(fm, Table):
Expand Down
3 changes: 2 additions & 1 deletion py/desispec/scripts/group_spectra.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ def main(args=None, comm=None):
framesdict[(night, expid, camera)] = frame

log0.info(f'Combining into spectra at {time.asctime()}')
spectra = frames2spectra(framesdict, pix=args.healpix, nside=args.nside)
spectra = frames2spectra(framesdict, pix=args.healpix, nside=args.nside,
onetile=args.onetile)

#- Record input files
if spectra.meta is None:
Expand Down
41 changes: 41 additions & 0 deletions py/desispec/test/test_pixgroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,10 +376,19 @@ def test_frames2spectra(self):
"""Test frames2spectra"""
from ..pixgroup import FrameLite, frames2spectra

#- Read input frames
frames = list()
for filename in self.framefiles:
frames.append( FrameLite.read(filename) )

#- Set some header keywords for testing
for i, fr in enumerate(frames):
fr.fibermap.meta['BLAT'] = i #- conflicting, never merged
fr.fibermap.meta['FOO'] = 'biz' #- consistent, but still not merged
fr.fibermap.meta['TILEID'] = 1234 #- only merged if onetile=True
fr.fibermap.meta['SURVEY'] = 'main' #- always merged
fr.fibermap.meta['PROGRAM'] = 'dark' #- always merged

#- converting list of frames
spectra = frames2spectra(frames)

Expand All @@ -393,3 +402,35 @@ def test_frames2spectra(self):

spectra = frames2spectra(framedict)

#- Check header keyword propagation
self.assertNotIn('BLAT', spectra.fibermap.meta)
self.assertNotIn('FOO', spectra.fibermap.meta)
self.assertNotIn('TILEID', spectra.fibermap.meta)
self.assertIn('SURVEY', spectra.fibermap.meta)
self.assertIn('PROGRAM', spectra.fibermap.meta)

#- but originals were not changed
for i, fr in enumerate(frames):
self.assertEqual(fr.fibermap.meta['BLAT'], i)
self.assertEqual(fr.fibermap.meta['FOO'], 'biz')
self.assertEqual(fr.fibermap.meta['TILEID'], 1234)
self.assertEqual(fr.fibermap.meta['SURVEY'], 'main')
self.assertEqual(fr.fibermap.meta['PROGRAM'], 'dark')

#- frames2spectra(..., onetile=True) should propagate TILEID
spectra = frames2spectra(framedict, onetile=True)
self.assertNotIn('BLAT', spectra.fibermap.meta)
self.assertNotIn('FOO', spectra.fibermap.meta)
self.assertIn('TILEID', spectra.fibermap.meta) #- different from before
self.assertIn('SURVEY', spectra.fibermap.meta)
self.assertIn('PROGRAM', spectra.fibermap.meta)

#- originals are still not changed
for i, fr in enumerate(frames):
self.assertEqual(fr.fibermap.meta['BLAT'], i)
self.assertEqual(fr.fibermap.meta['FOO'], 'biz')
self.assertEqual(fr.fibermap.meta['TILEID'], 1234)
self.assertEqual(fr.fibermap.meta['SURVEY'], 'main')
self.assertEqual(fr.fibermap.meta['PROGRAM'], 'dark')


0 comments on commit 586a163

Please sign in to comment.