Coverage for tests/test_guider.py: 32%

46 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-11-04 02:35 -0800

1# This file is part of obs_lsst. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (http://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# This program is free software: you can redistribute it and/or modify 

10# it under the terms of the GNU General Public License as published by 

11# the Free Software Foundation, either version 3 of the License, or 

12# (at your option) any later version. 

13# 

14# This program is distributed in the hope that it will be useful, 

15# but WITHOUT ANY WARRANTY; without even the implied warranty of 

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

17# GNU General Public License for more details. 

18# 

19# You should have received a copy of the GNU General Public License 

20# along with this program. If not, see <http://www.gnu.org/licenses/>. 

21# 

22 

23import os 

24import unittest 

25import contextlib 

26from lsst.daf.butler import MissingDatasetTypeError, Config 

27from lsst.daf.butler.tests import makeTestRepo 

28from lsst.daf.butler.tests.utils import makeTestTempDir, removeTestTempDir 

29from lsst.obs.lsst import LsstCam, ingest_guider 

30from lsst.obs.base import RawIngestTask 

31 

32TESTDIR = os.path.abspath(os.path.dirname(__file__)) 

33DATAROOT = os.path.join(TESTDIR, os.path.pardir, "data", "input", "guider") 

34 

35 

36class GuiderIngestTestCase(unittest.TestCase): 

37 """Test guider file ingest.""" 

38 

39 def setUp(self): 

40 # Repository should be re-created for each test case since 

41 # dimension records are set. 

42 self.root = makeTestTempDir(TESTDIR) 

43 self.addClassCleanup(removeTestTempDir, self.root) 

44 

45 config = Config() 

46 config["datastore", "cls"] = "lsst.daf.butler.datastores.fileDatastore.FileDatastore" 

47 self.butler = makeTestRepo(self.root, config=config) 

48 self.instrument = LsstCam() 

49 self.instrument.register(self.butler.registry) 

50 

51 def test_ingest_guider_fail(self): 

52 

53 failed = {} 

54 

55 def on_undefined_exposure(path, obsid): 

56 failed[path] = obsid 

57 

58 with self.assertRaises(MissingDatasetTypeError): 

59 ingest_guider( 

60 self.butler, 

61 [os.path.join(DATAROOT, "guider_data", "MC_C_20230616_000013_R04_SG0.fits")], 

62 on_undefined_exposure=on_undefined_exposure, 

63 ) 

64 

65 with contextlib.suppress(Exception): 

66 ingest_guider( 

67 self.butler, 

68 [os.path.join(DATAROOT, "guider_data", "MC_C_20230616_000013_R04_SG0.fits")], 

69 register_dataset_type=True, 

70 on_undefined_exposure=on_undefined_exposure, 

71 ) 

72 

73 failed_obsids = list(failed.values()) 

74 self.assertEqual(failed_obsids, ["MC_C_20230616_000013"], msg=f"{failed}") 

75 

76 def test_ingest_guider(self): 

77 # First ingest a raw to get the exposure defined. 

78 config = RawIngestTask.ConfigClass() 

79 task = RawIngestTask(config=config, butler=self.butler) 

80 # This will read the metadata from the sidecar file. 

81 task.run([os.path.join(DATAROOT, "raw", "MC_C_20240918_000013_R42_S11.fits")]) 

82 

83 ingested = [] 

84 

85 def on_success(datasets): 

86 ingested.extend(datasets) 

87 

88 # Ingest guider data. 

89 refs = ingest_guider( 

90 self.butler, 

91 [os.path.join(DATAROOT, "guider_data", "MC_C_20240918_000013_R00_SG0_guider.fits")], 

92 group_files=False, 

93 register_dataset_type=True, 

94 on_success=on_success, 

95 ) 

96 

97 self.assertEqual(len(ingested), 1) 

98 

99 # Check that the guider metadata is set. 

100 guider = self.butler.get(refs[0]) 

101 self.assertIsNotNone(guider.metadata) 

102 for stamp in guider: 

103 self.assertIn("STMPTIME", stamp.metadata) 

104 

105 self.assertEqual(guider[0].metadata["STMPTIME"], "2024-09-18T13:51:28.526") 

106 self.assertEqual(guider[-1].metadata["STMPTIME"], "2024-09-18T13:51:43.319") 

107 

108 

109 

110if __name__ == '__main__': 110 ↛ 111line 110 didn't jump to line 111 because the condition on line 110 was never true

111 unittest.main()