Skip to content

Commit

Permalink
Merge pull request #155 from lsst/tickets/DM-20073
Browse files Browse the repository at this point in the history
DM-20073: Move ObservationInfo to VisitInfo code to separate method
  • Loading branch information
timj committed Jun 13, 2019
2 parents c79d625 + f5c29b5 commit 6c1e41c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 6 deletions.
27 changes: 25 additions & 2 deletions python/lsst/obs/base/makeRawVisitInfoViaObsInfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,36 @@ def __call__(self, md, exposureId=None):
`~lsst.afw.image.VisitInfo` derived from the header using
a `~astro_metadata_translator.MetadataTranslator`.
"""
argDict = dict()

obsInfo = ObservationInfo(md, translator_class=self.metadataTranslator)

# Strip all the cards out that were used
for c in obsInfo.cards_used:
del md[c]

return self.observationInfo2visitInfo(obsInfo, log=self.log)

@staticmethod
def observationInfo2visitInfo(obsInfo, log=None):
"""Construct a `~lsst.afw.image.VisitInfo` from an
`~astro_metadata_translator.ObservationInfo`
Parameters
----------
obsInfo : `astro_metadata_translator.ObservationInfo`
Information gathered from the observation metadata.
log : `logging.Logger` or `lsst.log.Log`, optional
Logger to use for logging informational messages.
If `None` logging will be disabled.
Returns
-------
visitInfo : `lsst.afw.image.VisitInfo`
`~lsst.afw.image.VisitInfo` derived from the supplied
`~astro_metadata_translator.ObservationInfo`.
"""
argDict = dict()

# Map the translated information into a form suitable for VisitInfo
if obsInfo.exposure_time is not None:
argDict["exposureTime"] = obsInfo.exposure_time.to_value("s")
Expand Down Expand Up @@ -170,7 +192,8 @@ def __call__(self, md, exposureId=None):

for key in list(argDict.keys()): # use a copy because we may delete items
if argDict[key] is None:
self.log.warn("argDict[{}] is None; stripping".format(key, argDict[key]))
if log is not None:
log.warn("argDict[{}] is None; stripping".format(key, argDict[key]))
del argDict[key]

return VisitInfo(**argDict)
2 changes: 1 addition & 1 deletion tests/test_cameraMapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ def testWcsRaises(self):

def testConflictRaises(self):
policy = dafPersist.Policy(os.path.join(ROOT, "ConflictMapper.yaml"))
with self.assertRaisesRegexp(
with self.assertRaisesRegex(
ValueError,
r"Duplicate mapping policy for dataset type packages"):
mapper = lsst.obs.base.CameraMapper(policy=policy, repositoryDir=ROOT, root=ROOT) # noqa F841
Expand Down
23 changes: 21 additions & 2 deletions tests/test_makeRawVisitInfoViaObsInfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
from astropy.time import Time
import astropy.units as u

from astro_metadata_translator import FitsTranslator, StubTranslator
import lsst.log
from astro_metadata_translator import FitsTranslator, StubTranslator, ObservationInfo
from lsst.daf.base import DateTime

from lsst.obs.base import MakeRawVisitInfoViaObsInfo
Expand Down Expand Up @@ -73,14 +74,32 @@ def setUp(self):

def testMakeRawVisitInfoViaObsInfo(self):
maker = MakeTestableVisitInfo()
visitInfo = maker(self.header)

# Capture the warnings from StubTranslator since they are
# confusing to people but irrelevant for the test.
with self.assertWarns(UserWarning):
with lsst.log.UsePythonLogging():
with self.assertLogs(level="WARNING"):
visitInfo = maker(self.header)

self.assertAlmostEqual(visitInfo.getExposureTime(), self.exposure_time.to_value("s"))
self.assertEqual(visitInfo.getExposureId(), self.exposure_id)
self.assertEqual(visitInfo.getDate(), DateTime("2001-01-02T03:04:06.123456789Z", DateTime.UTC))
self.assertNotIn("EXPTIME", self.header)
self.assertEqual(len(self.header), 2)

def testObservationInfo2VisitInfo(self):

with self.assertWarns(UserWarning):
obsInfo = ObservationInfo(self.header, translator_class=NewTranslator)

# No log specified so no log message should appear
visitInfo = MakeRawVisitInfoViaObsInfo.observationInfo2visitInfo(obsInfo)
self.assertIsInstance(visitInfo, lsst.afw.image.VisitInfo)
self.assertAlmostEqual(visitInfo.getExposureTime(), self.exposure_time.to_value("s"))
self.assertEqual(visitInfo.getExposureId(), self.exposure_id)
self.assertEqual(visitInfo.getDate(), DateTime("2001-01-02T03:04:06.123456789Z", DateTime.UTC))


if __name__ == "__main__":
unittest.main()
2 changes: 1 addition & 1 deletion tests/test_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def test(self):
butler = dafPersist.Butler(outputs=repoArgs)

with open(os.path.join(firstRepoPath, 'repositoryCfg.yaml')) as f:
cfg = yaml.load(f)
cfg = yaml.load(f, Loader=yaml.FullLoader)
self.assertEqual(cfg.policy, policy)
butler.put(objA, 'basicObject1', {'id': 1})

Expand Down

0 comments on commit 6c1e41c

Please sign in to comment.