Coverage for tests/test_groups.py: 25%
55 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-08 03:00 -0700
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-08 03:00 -0700
1# This file is part of astro_metadata_translator.
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 LICENSE file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# Use of this source code is governed by a 3-clause BSD-style
10# license that can be found in the LICENSE file.
12import os.path
13import unittest
15from astro_metadata_translator import ObservationGroup
16from astro_metadata_translator.serialize import group_to_fits, info_to_fits
17from astro_metadata_translator.tests import read_test_file
19TESTDIR = os.path.abspath(os.path.dirname(__file__))
22class ObservationGroupTestCase(unittest.TestCase):
23 """Test grouping of observations."""
25 datadir = os.path.join(TESTDIR, "data")
27 def setUp(self):
28 self.decam_files = (
29 "fitsheader-decam.yaml",
30 "fitsheader-decam-0160496.yaml",
31 "fitsheader-decam-calexp-0412037_10.yaml",
32 )
33 self.hsc_files = ("fitsheader-hsc-HSCA04090107.yaml", "fitsheader-hsc.yaml")
35 def _files_to_headers(self, files):
36 return [read_test_file(os.path.join(self.datadir, f)) for f in files]
38 def test_groups(self):
39 headers = self._files_to_headers(self.decam_files)
41 obs_group = ObservationGroup(headers)
42 self.assertEqual(len(obs_group), 3)
43 self.assertEqual(
44 str(obs_group),
45 "[(DECam, 2013-09-01T06:02:55.754), (DECam, 2012-12-11T22:06:32.859),"
46 " (DECam, 2015-02-20T00:47:21.127)]",
47 )
49 sorted_group = ObservationGroup(sorted(obs_group))
50 self.assertIsInstance(sorted_group, ObservationGroup)
51 self.assertEqual(len(sorted_group), 3)
52 self.assertEqual(sorted_group[0], obs_group[1])
54 self.assertNotEqual(obs_group, sorted_group)
55 obs_group.sort()
56 self.assertEqual(obs_group, sorted_group)
57 obs_group.reverse()
58 self.assertEqual(obs_group[0], sorted_group[-1])
60 newest = obs_group.newest()
61 oldest = obs_group.oldest()
62 self.assertEqual(newest, sorted_group[-1])
63 self.assertEqual(oldest, sorted_group[0])
65 self.assertLess(oldest, newest)
66 self.assertGreater(newest, oldest)
68 self.assertNotEqual(oldest, obs_group)
69 self.assertNotEqual(obs_group, oldest)
71 # Add some headers and check that sorting still works
72 obs_group.extend(self._files_to_headers(self.hsc_files))
73 self.assertEqual(len(obs_group), 5)
74 self.assertEqual(obs_group.newest(), obs_group[3])
76 instruments = obs_group.property_values("instrument")
77 self.assertEqual(instruments, {"HSC", "DECam"})
79 # Check that simplified form round trips
80 self.assertEqual(ObservationGroup.from_simple(obs_group.to_simple()), obs_group)
82 def test_fits_group(self):
83 headers = self._files_to_headers(self.decam_files)
85 obs_group = ObservationGroup(headers)
86 cards, comments = group_to_fits(obs_group)
88 expected = {
89 "INSTRUME": "DECam",
90 "TIMESYS": "TAI",
91 "DATE-OBS": "2012-12-11T22:07:07.859",
92 "MJD-OBS": 56272.92161874134,
93 "DATE-BEG": "2012-12-11T22:07:07.859",
94 "MJD-BEG": 56272.92161874134,
95 "DATE-END": "2015-02-20T00:50:11.000",
96 "MJD-END": 57073.034849537034,
97 "DATE-AVG": "2014-01-15T23:28:39.430",
98 "MJD-AVG": 56672.97823413919,
99 }
100 self.assertEqual(cards, expected)
102 def test_fits_info(self):
103 header = self._files_to_headers(self.decam_files)[0]
104 obs_group = ObservationGroup([header])
105 cards, comments = info_to_fits(obs_group[0])
107 expected = {
108 "INSTRUME": "DECam",
109 "TIMESYS": "TAI",
110 "MJD-AVG": 56536.25417681625,
111 "MJD-END": 56536.25591435185,
112 "MJD-OBS": 56536.25243928065,
113 "MJD-BEG": 56536.25243928065,
114 "DATE-OBS": "2013-09-01T06:03:30.754",
115 "DATE-BEG": "2013-09-01T06:03:30.754",
116 "DATE-AVG": "2013-09-01T06:06:00.877",
117 "DATE-END": "2013-09-01T06:08:31.000",
118 }
119 self.assertEqual(cards, expected)
122if __name__ == "__main__": 122 ↛ 123line 122 didn't jump to line 123, because the condition on line 122 was never true
123 unittest.main()