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

Add function to add calibration exposures #55

Merged
merged 6 commits into from
Nov 9, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion doc/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ surveysim change log
0.8.3 (unreleased)
------------------

* No changes yet.
* Add ``surveysim.util.add_calibration_exposures()``, to add simulated
calibration exposures to a set of science exposures.

0.8.2 (2017-10-09)
------------------
Expand Down
44 changes: 44 additions & 0 deletions py/surveysim/test/test_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import unittest
import numpy as np
from astropy.table import Table, Column
from ..util import add_calibration_exposures

class TestUtil(unittest.TestCase):

@classmethod
def setUpClass(cls):
pass

@classmethod
def tearDownClass(cls):
pass

def setUp(self):
pass

def tearDown(self):
pass

def test_add_calibration_exposures(self):
exposures = Table()
exposures['TILEID'] = Column(np.array([0, 1], dtype=np.int32))
exposures['PASS'] = Column(np.array([0, 0], dtype=np.int16))
exposures['RA'] = Column(np.array([0.0, 1.0], dtype=np.float64))
exposures['DEC'] = Column(np.array([0.0, 1.0], dtype=np.float64))
exposures['EBMV'] = Column(np.array([0.0, 1.0], dtype=np.float64))
exposures['NIGHT'] = Column(np.array(['20200101', '20200102'], dtype=(str, 8)))
exposures['MJD'] = Column(np.array([0.0, 1.0], dtype=np.float64))
exposures['EXPTIME'] = Column(np.array([0.0, 1.0], dtype=np.float64), unit='s')
exposures['SEEING'] = Column(np.array([0.0, 1.0], dtype=np.float64), unit='arcsec')
exposures['TRANSPARENCY'] = Column(np.array([0.0, 1.0], dtype=np.float64))
exposures['AIRMASS'] = Column(np.array([0.0, 1.0], dtype=np.float64))
exposures['MOONFRAC'] = Column(np.array([0.0, 1.0], dtype=np.float64))
exposures['MOONALT'] = Column(np.array([0.0, 1.0], dtype=np.float64), unit='deg')
exposures['MOONSEP'] = Column(np.array([0.0, 1.0], dtype=np.float64), unit='deg')
exposures['PROGRAM'] = Column(np.array(['DARK', 'DARK'], dtype=(str, 6)))
exposures['FLAVOR'] = Column(np.array(['science', 'science'], dtype=(str, 7)))
output = add_calibration_exposures(exposures)
self.assertEqual(len(output), 14)
self.assertEqual('EXPID', output.colnames[0])
self.assertTrue(np.all(output['EXPID'] == np.arange(14, dtype=np.int32)))
self.assertEqual(output['RA'].unit, 'deg')
53 changes: 53 additions & 0 deletions py/surveysim/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""Simulation utilities that may be used by other packages.
"""
from __future__ import print_function, division, absolute_import
import numpy as np
from astropy.table import Column


def add_calibration_exposures(exposures, arcs_per_night=3, flats_per_night=3):
"""Adds calibration exposures to a set of science exposures and
assigns exposure IDs.

Parameters
----------
exposures : :class:`astropy.table.Table`
A table of science exposures, produced by *e.g.*
``desisurvey.progress.Progress.get_exposures()``.
arcs_per_night : :class:`int`, optional
Add this many arc exposures per night (default 3).
flats_per_night : :class:`int`, optional
Add this many arc exposures per night (default 3).

Returns
-------
:class:`astropy.table.Table`
A table augmented with calibration exposures.
"""
if 'EXPID' not in exposures:
expidc = Column(np.arange(len(exposures), dtype=np.int32),
name='EXPID', description='Exposure ID')
exposures.add_column(expidc, index=0)
for c in ('RA', 'DEC'):
if exposures[c].unit is None:
exposures[c].unit = 'deg'
output = exposures[:0].copy()
current_night = '19730703'
expid = 0
for i, night in enumerate(exposures['NIGHT']):
if night != current_night:
current_night = night
for j in range(flats_per_night):
output.add_row([expid, -1, -1, 0.0, 0.0, 0.0, night,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
'NONE', 'flat'])
expid += 1
for j in range(arcs_per_night):
output.add_row([expid, -1, -1, 0.0, 0.0, 0.0, night,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
'NONE', 'arc'])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This add_row logic makes me nervous because it is fragilely dependent upon exactly which columns exist and in what order they appear in the input exposures table, thus creating a tight coupling with desisurvey.progress.Progress.get_exposures() and making it harder to use outside of that context. How about calling add_row with a dict, where you have filled in the keys of the dict only for columns that actually exist in the input exposures table? If that suggestion doesn't make sense, ping me and I'll put together a code example that might be clearer.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure I understand what you mean here, & that's reasonable.

expid += 1
output.add_row(exposures[i])
output['EXPID'][expid] = expid
expid += 1
return output