test_fits_header_ops.py 4.05 KB
Newer Older
BO ZHANG's avatar
BO ZHANG committed
1
2
3
4
5
6
7
"""
Identifier:     tests/test_fits_header_ops.py
Name:           test_fits_header_ops.py
Description:    test append_header
Author:         Bo Zhang
Created:        2023-12-15
Modified-History:
BO ZHANG's avatar
tweaks    
BO ZHANG committed
8
9
    2023-12-15, Bo Zhang, add TestFitsHeaderOps.test_append_header
    2023-12-15, Bo Zhang, add TestFitsHeaderOps.test_reformat_header
BO ZHANG's avatar
BO ZHANG committed
10
"""
BO ZHANG's avatar
BO ZHANG committed
11

12
import unittest
BO ZHANG's avatar
BO ZHANG committed
13

14
15
from astropy.io import fits

BO ZHANG's avatar
tweaks    
BO ZHANG committed
16
17
18
19
20
21
22
from csst_common.io import (
    append_header,
    reformat_header,
    delete_section,
    get_proc_info,
    generate_meta,
)
BO ZHANG's avatar
BO ZHANG committed
23

24

BO ZHANG's avatar
tweaks    
BO ZHANG committed
25
class TestFitsHeaderOps(unittest.TestCase):
26
27
28
29
30
    def test_append_header(self):
        h1 = fits.Header()
        h2 = fits.Header()

        h1.set("A", 1, "comment")
BO ZHANG's avatar
BO ZHANG committed
31
32
        h1.set("B", 1, "comment")
        h1.set("C", 1, "comment")
33
34
35
36
        h1.add_comment("=" * 72, before="A")
        h1.add_comment("one", before="A")
        h1.add_comment("=" * 72, before="A")

BO ZHANG's avatar
BO ZHANG committed
37
38
39
        h2.set("B", 2, "comment")
        h2.set("C", 2, "comment")
        h2.set("D", 2, "comment")
40
41
42
43
        h2.add_comment("=" * 72, before="B")
        h2.add_comment("another", before="B")
        h2.add_comment("=" * 72, before="B")

BO ZHANG's avatar
BO ZHANG committed
44
        h_update = append_header(h1, h2, duplicates="update")
45
        self.assertEqual(
BO ZHANG's avatar
BO ZHANG committed
46
            tuple(h_update.keys()),
47
48
49
50
51
52
            (
                "COMMENT",
                "COMMENT",
                "COMMENT",
                "A",
                "B",
BO ZHANG's avatar
BO ZHANG committed
53
                "C",
54
55
56
                "COMMENT",
                "COMMENT",
                "COMMENT",
BO ZHANG's avatar
BO ZHANG committed
57
                "D",
58
59
60
            ),
            "update mode failed",
        )
BO ZHANG's avatar
BO ZHANG committed
61
62
63
64
65
66
        self.assertEqual(h_update["A"], 1)
        self.assertEqual(h_update["B"], 2)
        self.assertEqual(h_update["C"], 2)
        self.assertEqual(h_update["D"], 2)

        h_delete = append_header(h1, h2, duplicates="delete")
67
        self.assertEqual(
BO ZHANG's avatar
BO ZHANG committed
68
            tuple(h_delete.keys()),
69
70
71
72
73
74
75
76
77
78
            (
                "COMMENT",
                "COMMENT",
                "COMMENT",
                "A",
                "COMMENT",
                "COMMENT",
                "COMMENT",
                "B",
                "C",
BO ZHANG's avatar
BO ZHANG committed
79
                "D",
80
81
82
            ),
            "delete mode failed",
        )
BO ZHANG's avatar
BO ZHANG committed
83
84
85
86
        self.assertEqual(h_delete["A"], 1)
        self.assertEqual(h_delete["B"], 2)
        self.assertEqual(h_delete["C"], 2)
        self.assertEqual(h_delete["D"], 2)
BO ZHANG's avatar
BO ZHANG committed
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109

    def test_reformat_header(self):
        h = fits.Header()
        h.add_comment("A")
        h.add_comment("B")
        h.add_comment("B")
        h.add_comment("C")
        h.add_comment("X")
        h.set("A", 1)
        h.set("SIMPLE", True)
        h.set("NAXIS1", 1)

        h_rfmt = reformat_header(h, strip=True, comment="WCS info")
        self.assertEqual(
            tuple(h_rfmt.keys()),
            ("COMMENT", "COMMENT", "COMMENT", "A", "NAXIS1"),
        )

        h_rfmt = reformat_header(h, strip=False, comment="WCS info")
        self.assertEqual(
            tuple(h_rfmt.keys()),
            ("COMMENT", "COMMENT", "COMMENT", "A", "SIMPLE", "NAXIS1"),
        )
BO ZHANG's avatar
BO ZHANG committed
110
111
112

    def test_delete_section(self):
        h = fits.Header()
BO ZHANG's avatar
BO ZHANG committed
113
114
115
116
117
118
119
120
121
122
123
        h.append(("COMMENT", "A", ""), bottom=True)
        h.append(("COMMENT", "C", ""), bottom=True)
        h.append(("COMMENT", "B", ""), bottom=True)
        h.append(("SIMPLE", True, ""), bottom=True)
        h.append(("NAXIS1", 1, ""), bottom=True)
        h.append(("COMMENT", "=" * 72, ""), bottom=True)
        h.append(("COMMENT", "WCS", ""), bottom=True)
        h.append(("COMMENT", "=" * 72, ""), bottom=True)
        h.append(("A", 1, ""), bottom=True)
        h.append(("COMMENT", "=" * 72, ""), bottom=True)
        h.append(("COMMENT", "=" * 72, ""), bottom=True)
BO ZHANG's avatar
BO ZHANG committed
124
125
126

        h_del = delete_section(h, title="WCS")
        self.assertEqual(len(h_del.cards), 7)
BO ZHANG's avatar
BO ZHANG committed
127
128
129
130

    def test_get_proc_info(self):
        proc_info = get_proc_info()
        self.assertEquals(proc_info["BUILD"], "-")
BO ZHANG's avatar
tweaks    
BO ZHANG committed
131
132
133
134

    def test_generate_meta(self):
        h = generate_meta()
        self.assertIsInstance(h, fits.Header)
BO ZHANG's avatar
tweaks    
BO ZHANG committed
135
136
137
138

    def test_generate_meta_with_invalid_args(self):
        with self.assertRaises(AssertionError):
            generate_meta(obs_id=1)