test_io.py 4.41 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

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

15
from astropy.io import fits
BO ZHANG's avatar
BO ZHANG committed
16
from astropy import table
17

BO ZHANG's avatar
BO ZHANG committed
18
from csst_common import io
BO ZHANG's avatar
BO ZHANG committed
19

20

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

        h1.set("A", 1, "comment")
BO ZHANG's avatar
BO ZHANG committed
27
28
        h1.set("B", 1, "comment")
        h1.set("C", 1, "comment")
29
30
31
32
        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
33
34
35
        h2.set("B", 2, "comment")
        h2.set("C", 2, "comment")
        h2.set("D", 2, "comment")
36
37
38
39
        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
40
        h_update = io.append_header(h1, h2, duplicates="update")
41
        self.assertEqual(
BO ZHANG's avatar
BO ZHANG committed
42
            tuple(h_update.keys()),
43
44
45
46
47
48
            (
                "COMMENT",
                "COMMENT",
                "COMMENT",
                "A",
                "B",
BO ZHANG's avatar
BO ZHANG committed
49
                "C",
50
51
52
                "COMMENT",
                "COMMENT",
                "COMMENT",
BO ZHANG's avatar
BO ZHANG committed
53
                "D",
54
55
56
            ),
            "update mode failed",
        )
BO ZHANG's avatar
BO ZHANG committed
57
58
59
60
61
        self.assertEqual(h_update["A"], 1)
        self.assertEqual(h_update["B"], 2)
        self.assertEqual(h_update["C"], 2)
        self.assertEqual(h_update["D"], 2)

BO ZHANG's avatar
BO ZHANG committed
62
        h_delete = io.append_header(h1, h2, duplicates="delete")
63
        self.assertEqual(
BO ZHANG's avatar
BO ZHANG committed
64
            tuple(h_delete.keys()),
65
66
67
68
69
70
71
72
73
74
            (
                "COMMENT",
                "COMMENT",
                "COMMENT",
                "A",
                "COMMENT",
                "COMMENT",
                "COMMENT",
                "B",
                "C",
BO ZHANG's avatar
BO ZHANG committed
75
                "D",
76
77
78
            ),
            "delete mode failed",
        )
BO ZHANG's avatar
BO ZHANG committed
79
80
81
82
        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
83
84
85
86
87
88
89
90
91
92
93
94

    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)

BO ZHANG's avatar
BO ZHANG committed
95
        h_rfmt = io.reformat_header(h, strip=True, comment="WCS info")
BO ZHANG's avatar
BO ZHANG committed
96
97
98
99
100
        self.assertEqual(
            tuple(h_rfmt.keys()),
            ("COMMENT", "COMMENT", "COMMENT", "A", "NAXIS1"),
        )

BO ZHANG's avatar
BO ZHANG committed
101
        h_rfmt = io.reformat_header(h, strip=False, comment="WCS info")
BO ZHANG's avatar
BO ZHANG committed
102
103
104
105
        self.assertEqual(
            tuple(h_rfmt.keys()),
            ("COMMENT", "COMMENT", "COMMENT", "A", "SIMPLE", "NAXIS1"),
        )
BO ZHANG's avatar
BO ZHANG committed
106
107
108

    def test_delete_section(self):
        h = fits.Header()
BO ZHANG's avatar
BO ZHANG committed
109
110
111
112
113
114
115
116
117
118
119
        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
120

BO ZHANG's avatar
BO ZHANG committed
121
        h_del = io.delete_section(h, title="WCS")
BO ZHANG's avatar
BO ZHANG committed
122
        self.assertEqual(len(h_del.cards), 7)
BO ZHANG's avatar
BO ZHANG committed
123

BO ZHANG's avatar
tweaks    
BO ZHANG committed
124
    def test_generate_meta(self):
BO ZHANG's avatar
BO ZHANG committed
125
        h = io.generate_meta()
BO ZHANG's avatar
BO ZHANG committed
126
        self.assertIsInstance(h, dict)
BO ZHANG's avatar
tweaks    
BO ZHANG committed
127

BO ZHANG's avatar
BO ZHANG committed
128
    def test_append_meta(self):
BO ZHANG's avatar
BO ZHANG committed
129
        hdulist = fits.HDUList([fits.PrimaryHDU()])
BO ZHANG's avatar
BO ZHANG committed
130
131
        meta = io.generate_meta(obs_id="123456")
        hdulist = io.append_meta(hdulist, meta)
BO ZHANG's avatar
BO ZHANG committed
132
        self.assertIn("META", set(hdulist[0].header.keys()))
BO ZHANG's avatar
BO ZHANG committed
133
        meta = io.extract_meta(hdulist)
BO ZHANG's avatar
BO ZHANG committed
134
        self.assertEqual(meta["obs_id"], "123456")
BO ZHANG's avatar
BO ZHANG committed
135

BO ZHANG's avatar
BO ZHANG committed
136
    def test_append_meta_to_table_hdu(self):
BO ZHANG's avatar
BO ZHANG committed
137
        t = table.Table([{"a": 1, "b": 2}] * 5)
BO ZHANG's avatar
BO ZHANG committed
138
139
140
141
142
143
        hdu = io.table_to_hdu(t)
        meta = io.generate_meta(obs_id="123456")
        hdu = io.append_meta_to_table_hdu(hdu, meta)
        self.assertEqual(
            set(io.hdu_to_table(hdu).colnames), {"a", "b", "healpix", "data_uuid"}
        )