test_fits_header_ops.py 3.38 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
BO ZHANG committed
16
from csst_common.io import append_header, reformat_header, delete_section
BO ZHANG's avatar
BO ZHANG committed
17

18

BO ZHANG's avatar
tweaks    
BO ZHANG committed
19
class TestFitsHeaderOps(unittest.TestCase):
20
21
22
23
24
    def test_append_header(self):
        h1 = fits.Header()
        h2 = fits.Header()

        h1.set("A", 1, "comment")
BO ZHANG's avatar
BO ZHANG committed
25
26
        h1.set("B", 1, "comment")
        h1.set("C", 1, "comment")
27
28
29
30
        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
31
32
33
        h2.set("B", 2, "comment")
        h2.set("C", 2, "comment")
        h2.set("D", 2, "comment")
34
35
36
37
        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
38
        h_update = append_header(h1, h2, duplicates="update")
39
        self.assertEqual(
BO ZHANG's avatar
BO ZHANG committed
40
            tuple(h_update.keys()),
41
42
43
44
45
46
            (
                "COMMENT",
                "COMMENT",
                "COMMENT",
                "A",
                "B",
BO ZHANG's avatar
BO ZHANG committed
47
                "C",
48
49
50
                "COMMENT",
                "COMMENT",
                "COMMENT",
BO ZHANG's avatar
BO ZHANG committed
51
                "D",
52
53
54
            ),
            "update mode failed",
        )
BO ZHANG's avatar
BO ZHANG committed
55
56
57
58
59
60
        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")
61
        self.assertEqual(
BO ZHANG's avatar
BO ZHANG committed
62
            tuple(h_delete.keys()),
63
64
65
66
67
68
69
70
71
72
            (
                "COMMENT",
                "COMMENT",
                "COMMENT",
                "A",
                "COMMENT",
                "COMMENT",
                "COMMENT",
                "B",
                "C",
BO ZHANG's avatar
BO ZHANG committed
73
                "D",
74
75
76
            ),
            "delete mode failed",
        )
BO ZHANG's avatar
BO ZHANG committed
77
78
79
80
        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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103

    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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120

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

        h_del = delete_section(h, title="WCS")
        self.assertEqual(len(h_del.cards), 7)