test_fits.py 1.73 KB
Newer Older
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:
8
    2025-10-23, Bo Zhang, add tests for fits operations
9
10
11
12
13
"""

import os
import unittest

14
import numpy as np
15
16
17
18
19
20
21
22
23
24
from csst_common import fits

test_fits_file = os.path.join(
    os.environ["UNIT_TEST_DATA_ROOT"],
    "csst_common/test_fits/hl.fits",
)


class TestFitsHeaderOps(unittest.TestCase):

25
26
27
28
29
30
31
    def setUp(self):
        hl = fits.HDUList(
            [
                fits.PrimaryHDU(),
                fits.ImageHDU(data=np.random.randn(5, 5)),
            ]
        )
32
        hl.writeto(test_fits_file, overwrite=True)
33

34
35
36
37
38
39
40
41
42
43
44
45
46
    def test_fits_open(self):
        hl = fits.open(test_fits_file)
        self.assertIsInstance(hl, fits.HDUList)

    def test_fits_header(self):
        header = fits.getheader(test_fits_file, ext=0)
        self.assertIsInstance(header, fits.Header)

    def test_fits_getval(self):
        val = fits.getval(test_fits_file, ext=0, keyword="SIMPLE")
        self.assertEqual(val, True)

    def test_fits_getdata(self):
47
        data = fits.getdata(test_fits_file, ext=1)
48
49
        self.assertIsInstance(data, np.ndarray)
        self.assertEqual(data.shape, (5, 5))
BO ZHANG's avatar
BO ZHANG committed
50
51
52
53
54

    def test_fits_to_bytes(self):
        hl = fits.open(test_fits_file)
        fits_bytes = hl.to_bytes()
        self.assertIsInstance(fits_bytes, bytes)
55
56
57
58
59
60

    def test_prepare_fits_files_for_ingestion(self):
        ingestion_files = fits.prepare_fits_files_for_ingestion([test_fits_file])
        self.assertIsInstance(ingestion_files, list)
        self.assertEqual(len(ingestion_files), 1)
        self.assertEqual(set(ingestion_files[0].keys()), {"file_name", "file_content"})