test_io.py 3.7 KB
Newer Older
GZhao's avatar
GZhao committed
1
2
3
4
5
6
7
8
9
10
11
import unittest
from unittest import mock

from csst_cpic_sim.config import config

from csst_cpic_sim.io import obsid_parser, primary_hdu, frame_header, set_up_logger
from csst_cpic_sim.camera import CpicVisEmccd
import csst_cpic_sim.io as io
from astropy.io import fits
import numpy as np
import yaml
GZhao's avatar
GZhao committed
12
import logging
GZhao's avatar
GZhao committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

cstar = {
    'magnitude': 5,
    'ra': '0d',
    'dec': '0d',
    'sptype': 'G0III',
    'distance': 10,
    'mag_input_band': 'f661'
}

params = {
    'target': {'cstar': cstar},
    'skybg': 21,
    'expt': 20,
    'nframe': 5,
    'band': 'f661',
    'emgain': 10,
30
    'emset': 10,
GZhao's avatar
GZhao committed
31
32
33
34
35
36
    'obsid': '40112345678',
    'rotation': 20,
    'shift': [0, 1],
    'EXPSTART': '2020-01-01T00:00:00.000',
    'EXPEND': '2020-01-02T00:00:00.000',
    'frame_info': [
GZhao's avatar
GZhao committed
37
38
39
40
41
42
        {
            'expt_start': 0,
            'expt_end': 20,
            'platescale': 1.0,
            'iwa': 0,
            'chiptemp': 1}
GZhao's avatar
GZhao committed
43
44
45
46
47
48
    ]
}

camera = CpicVisEmccd()


GZhao's avatar
GZhao committed
49
class TestIO(unittest.TestCase):
GZhao's avatar
GZhao committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
    def test_obsid_parser(self):
        self.assertRaises(ValueError, obsid_parser, '20190101')
        self.assertRaises(ValueError, obsid_parser, '123456789012')
        self.assertRaises(ValueError, obsid_parser, '51012345678')

        self.assertEqual(obsid_parser('42012345678'), 'BIAS')
        self.assertEqual(obsid_parser('42112345678'), 'DARK')
        self.assertEqual(obsid_parser('42212345678'), 'FLAT')
        self.assertEqual(obsid_parser('42312345678'), 'BKG')
        self.assertEqual(obsid_parser('42412345678'), 'LASER')
        self.assertEqual(obsid_parser('40112345678'), 'SCI')
        self.assertEqual(obsid_parser('40212345678'), 'DSF')
        self.assertEqual(obsid_parser('41012345678'), 'CALS')
        self.assertEqual(obsid_parser('40312345678'), 'DEFT')

    def test_primary_hdu(self):
        hdu1 = primary_hdu(params, {}, filename_output=False)
        hdu2, folder, filename = primary_hdu(params, {}, filename_output=True)
        self.assertIsInstance(hdu1, fits.PrimaryHDU)
        self.assertIsInstance(hdu2, fits.PrimaryHDU)
        self.assertIsInstance(folder, str)
        self.assertIsInstance(filename, str)

    def test_frame_header(self):
        hdu1 = primary_hdu(params, {}, filename_output=False)
        header = frame_header(params, 0, hdu1.header, camera.__dict__)
        self.assertEqual(header['IMGINDEX'], 1)
        self.assertIsInstance(header, fits.Header)

    @mock.patch("os.makedirs")
    @mock.patch("astropy.io.fits.writeto")
    def test_write_fits(self, patch_fits, patch_mkdir):
GZhao's avatar
GZhao committed
82
83
84
85
86
87
88
89

        log = set_up_logger(config['log_dir'])
        self.assertIsInstance(log, logging.Logger)

        self.assertRaises(FileNotFoundError, set_up_logger, 'new folder')

        patch_mkdir.assert_called_once_with('new folder')

GZhao's avatar
GZhao committed
90
91
92
93
94
95
96
97
98
        images = np.zeros((5, 10, 10))
        yaml_str = """
obsid: 42012345678
expt: 300
nframe: 10
band: "f661"
shift: [0, 0]
rotation: 0
emgain: 100
99
emset: -1
GZhao's avatar
GZhao committed
100
skybg: 21
GZhao's avatar
GZhao committed
101
expstart:
GZhao's avatar
GZhao committed
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117

target:
    cstar:
        ra: "10.684792d"
        dec: "41.26917d"
        sptype: "M0.5"
        magnitude: 3.4
    objects:
        - ra: 10.684792
          dec: 41.26917
          sptype: "M0.5"
          magnitude: 3.4
"""
        parameters = yaml.load(yaml_str, Loader=yaml.FullLoader)
        tmp_folder_path = 'test_folder_for_unit_test'
        io.save_fits_simple(images, parameters, output_folder=tmp_folder_path)
GZhao's avatar
GZhao committed
118
119
120
121
122
        # patch_mkdir.assert_called_twice()

        dirname = patch_mkdir.call_args[0][0]
        self.assertEqual(dirname, tmp_folder_path)
        # patch_mkdir.assert_called_once_with(tmp_folder_path)
GZhao's avatar
GZhao committed
123
124
125
126
        output_name = patch_fits.call_args[0][0]
        self.assertEqual(
            output_name[:len(tmp_folder_path)], tmp_folder_path)

GZhao's avatar
GZhao committed
127

GZhao's avatar
GZhao committed
128
129
if __name__ == '__main__':
    unittest.main()