Skip to content

Commit

Permalink
return darks Table instead of index
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephen Bailey authored and Stephen Bailey committed May 16, 2024
1 parent 702ab6b commit f06a53f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
19 changes: 15 additions & 4 deletions py/desispec/test/test_workflow_calibration_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ def test_dark_selection(self):

def test_select_calib_dark(self):
"""Test workflow.calibration_selection.select_calib_dark"""
from desispec.workflow.calibration_selection import select_calib_dark
from desispec.workflow.calibration_selection import select_calib_darks
etable = Table()
etable['EXPID'] = [0,1,2,3]
etable['OBSTYPE'] = 'dark'
Expand All @@ -497,16 +497,27 @@ def test_select_calib_dark(self):
etable['BADCAMWORD'] = ['a234', '', '', '']
etable['BADAMPS'] = ''

#- make a copy for testing that original isn't modified
orig_etable = etable.copy()

# EXPID 0 has bad cameras, so should pick EXPID 1
self.assertEqual(select_calib_dark(etable), 1)
dark_expid = select_calib_darks(etable)['EXPID'][0]
self.assertEqual(dark_expid, 1)
self.assertTrue(np.all(etable == orig_etable))

# ... but not if EXPID1 is ignored, then EXPID 2
etable['LASTSTEP'][1] = 'ignore'
self.assertEqual(select_calib_dark(etable), 2)
orig_etable = etable.copy()
dark_expid = select_calib_darks(etable)['EXPID'][0]
self.assertEqual(dark_expid, 2)
self.assertTrue(np.all(etable == orig_etable))

# ... but not if EXPID 2 has more bad cameras than EXPID 3
etable['BADCAMWORD'][2] = 'a78'
etable['BADCAMWORD'][3] = 'a1'
self.assertEqual(select_calib_dark(etable), 3)
orig_etable = etable.copy()
dark_expid = select_calib_darks(etable)['EXPID'][0]
self.assertEqual(dark_expid, 3)
self.assertTrue(np.all(etable == orig_etable))


36 changes: 16 additions & 20 deletions py/desispec/workflow/calibration_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,45 +11,42 @@
from desispec.io.util import decode_camword, parse_badamps, all_impacted_cameras, erow_to_goodcamword


def select_calib_dark(etable):
def select_calib_darks(etable):
"""
Returns index of first dark with most available cameras for badcolumn calibration
Returns subset of etable row(s) with darks to use for badcolumn calibration
Args:
etable (astropy.table.Table): a DESI exposure_table
Returns:
index (int) of which row to use
dark_etable (astropy.table.Table): table of darks to use
Raises ValueError if no good darks are available.
Currently this returns a table of length-1 with a single dark, but in the
future it could return more than one dark if we found that useful.
"""
# make copy so we can add utility columns and sort without modifying original
# copy input so that we can sort without modifying original
etable = etable.copy()

# add ROW so that after filtering and sorting we can still get original index
etable['ROW'] = np.arange(len(etable))

keep = np.where((etable['OBSTYPE']=='dark') & (etable['LASTSTEP'] != 'ignore'))[0]
if len(keep) == 0:
raise ValueError('No good dark exposures exptime>295 found in etable')

etable = etable[keep]

# count good cameras per row
etable['NUM_GOODCAM'] = 0
num_goodcam = np.zeros(len(etable))
for i in range(len(etable)):
cameras = decode_camword(erow_to_goodcamword(etable[i]))
etable['NUM_GOODCAM'][i] = len(cameras)

etable['NUM_MISSING_CAMERAS'] = np.max(etable['NUM_GOODCAM']) - etable['NUM_GOODCAM']

# sort by decreasing number of missing cameras and increasing exposure time
etable.sort( ('NUM_MISSING_CAMERAS', 'EXPID') )
num_goodcam[i] = len(cameras)

print(etable)
# sort by number of missing cameras $while preserving EXPID order for ties
num_missing_cam = np.max(num_goodcam) - num_goodcam
sorted_indices = np.argsort(num_missing_cam, kind='stable')
i = sorted_indices[0]

# original row index of first entry should be best
return etable['ROW'][0]
return etable[i:i+1] #- Table of length 1, not Row object


def determine_calibrations_to_proc(etable, do_cte_flats=True,
Expand Down Expand Up @@ -123,9 +120,8 @@ def determine_calibrations_to_proc(etable, do_cte_flats=True,
## Create the output table with all zeros, the best selected dark,
## the best set of arcs and flats, and all cte flats
zeros = valid_etable[exptypes=='zero']
idark = select_calib_dark(valid_etable)
out_table = vstack([zeros, valid_etable[idark:idark+1]])
out_table = vstack([out_table, best_arcflat_set])
darks = select_calib_darks(valid_etable)
out_table = vstack([zeros, darks, best_arcflat_set])

## If doing cte flats, select one of each exptime based on proximity to the
## last 120s flat
Expand Down

0 comments on commit f06a53f

Please sign in to comment.