test_BF_CTE.py 5.35 KB
Newer Older
Wei Chengliang's avatar
Wei Chengliang committed
1
2
import unittest

Fang Yuedong's avatar
Fang Yuedong committed
3
4
5
import sys
import os
import math
Wei Chengliang's avatar
Wei Chengliang committed
6
7
8
9
10
11
12
13
from itertools import islice
import numpy as np
import copy
import ctypes
import galsim
import yaml
from astropy.io import fits

Fang Yuedong's avatar
Fang Yuedong committed
14
from observation_sim.instruments import Chip, Filter, FilterParam, FocalPlane
Fang Yuedong's avatar
Fang Yuedong committed
15
from observation_sim.instruments.chip import chip_utils
Fang Yuedong's avatar
Fang Yuedong committed
16
from observation_sim.instruments.chip.libCTI.CTI_modeling import CTI_sim
Wei Chengliang's avatar
Wei Chengliang committed
17
18
19
20
21
22
23
24

try:
    import importlib.resources as pkg_resources
except ImportError:
    # Try backported to PY<37 'importlib_resources'
    import importlib_resources as pkg_resources


Wei Chengliang's avatar
Wei Chengliang committed
25
# test FUNCTION --- START #
Wei Chengliang's avatar
Wei Chengliang committed
26
def add_brighter_fatter(img):
Fang Yuedong's avatar
Fang Yuedong committed
27
    # Inital dynamic lib
Wei Chengliang's avatar
Wei Chengliang committed
28
    try:
Fang Yuedong's avatar
Fang Yuedong committed
29
        with pkg_resources.files('observation_sim.instruments.chip.libBF').joinpath("libmoduleBF.so") as lib_path:
Wei Chengliang's avatar
Wei Chengliang committed
30
31
            lib_bf = ctypes.CDLL(lib_path)
    except AttributeError:
Fang Yuedong's avatar
Fang Yuedong committed
32
        with pkg_resources.path('observation_sim.instruments.chip.libBF', "libmoduleBF.so") as lib_path:
Wei Chengliang's avatar
Wei Chengliang committed
33
            lib_bf = ctypes.CDLL(lib_path)
Fang Yuedong's avatar
Fang Yuedong committed
34
35
    lib_bf.addEffects.argtypes = [ctypes.c_int, ctypes.c_int, ctypes.POINTER(
        ctypes.c_float), ctypes.POINTER(ctypes.c_float), ctypes.c_int]
Wei Chengliang's avatar
Wei Chengliang committed
36
37
38
39
40
41
42

    # Set bit flag
    bit_flag = 1
    bit_flag = bit_flag | (1 << 2)

    nx, ny = img.array.shape
    nn = nx * ny
Fang Yuedong's avatar
Fang Yuedong committed
43
44
    arr_ima = (ctypes.c_float*nn)()
    arr_imc = (ctypes.c_float*nn)()
Wei Chengliang's avatar
Wei Chengliang committed
45

Fang Yuedong's avatar
Fang Yuedong committed
46
47
    arr_ima[:] = img.array.reshape(nn)
    arr_imc[:] = np.zeros(nn)
Wei Chengliang's avatar
Wei Chengliang committed
48
49
50
51
52

    lib_bf.addEffects(nx, ny, arr_ima, arr_imc, bit_flag)
    img.array[:, :] = np.reshape(arr_imc, [nx, ny])
    del arr_ima, arr_imc
    return img
Wei Chengliang's avatar
Wei Chengliang committed
53
# test FUNCTION --- END #
Wei Chengliang's avatar
Wei Chengliang committed
54

Fang Yuedong's avatar
Fang Yuedong committed
55

Wei Chengliang's avatar
Wei Chengliang committed
56
57
58
59
def defineCCD(iccd, config_file):
    with open(config_file, "r") as stream:
        try:
            config = yaml.safe_load(stream)
Fang Yuedong's avatar
Fang Yuedong committed
60
            # for key, value in config.items():
Wei Chengliang's avatar
Wei Chengliang committed
61
62
63
64
            #    print (key + " : " + str(value))
        except yaml.YAMLError as exc:
            print(exc)
    chip = Chip(chipID=iccd, config=config)
Fang Yuedong's avatar
Fang Yuedong committed
65
66
    # galsim.ImageF(chip.npix_x, chip.npix_y)
    chip.img = galsim.ImageF(400, 200)
Wei Chengliang's avatar
Wei Chengliang committed
67
    focal_plane = FocalPlane(chip_list=[iccd])
Fang Yuedong's avatar
Fang Yuedong committed
68
69
    chip.img.wcs = focal_plane.getTanWCS(
        192.8595, 27.1283, -113.4333*galsim.degrees, chip.pix_scale)
Wei Chengliang's avatar
Wei Chengliang committed
70
71
    return chip

Fang Yuedong's avatar
Fang Yuedong committed
72

Wei Chengliang's avatar
Wei Chengliang committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
def defineFilt(chip):
    filter_param = FilterParam()
    filter_id, filter_type = chip.getChipFilter()
    filt = Filter(
        filter_id=filter_id,
        filter_type=filter_type,
        filter_param=filter_param,
        ccd_bandpass=chip.effCurve)
    bandpass_list = filt.bandpass_sub_list
    return filt


class detModule_coverage(unittest.TestCase):
    def __init__(self, methodName='runTest'):
        super(detModule_coverage, self).__init__(methodName)
Fang Yuedong's avatar
Fang Yuedong committed
88
89
90
        # self.dataPath = "/public/home/chengliang/CSSOSDataProductsSims/csst-simulation/tests/UNIT_TEST_DATA" ##os.path.join(os.getenv('UNIT_TEST_DATA_ROOT'), 'csst_fz_gc1')
        self.dataPath = os.path.join(
            os.getenv('UNIT_TEST_DATA_ROOT'), 'csst_msc_sim/csst_fz_msc')
Wei Chengliang's avatar
Wei Chengliang committed
91
92
93
94
95
96
97
98
99
        self.iccd = 1

    def test_add_brighter_fatter(self):
        config_file = os.path.join(self.dataPath, 'config_test.yaml')
        chip = defineCCD(self.iccd, config_file)
        filt = defineFilt(chip)
        print(chip.chipID)
        print(chip.cen_pix_x, chip.cen_pix_y)

Fang Yuedong's avatar
Fang Yuedong committed
100
        # objA-lowSFB
Wei Chengliang's avatar
Wei Chengliang committed
101
102
        obj = galsim.Gaussian(sigma=0.2, flux=1000)
        arr = obj.drawImage(nx=64, ny=64, scale=0.074).array
Fang Yuedong's avatar
Fang Yuedong committed
103
        chip.img.array[(100-32):(100+32), (200-32):(200+32)] = arr[:, :]
Wei Chengliang's avatar
Wei Chengliang committed
104
105
        img_old = copy.deepcopy(chip.img)
        img_new = add_brighter_fatter(img=chip.img)
Fang Yuedong's avatar
Fang Yuedong committed
106
107
        arr1 = img_old.array
        arr2 = img_new.array
Wei Chengliang's avatar
Wei Chengliang committed
108
        deltaA_max = np.max(np.abs(arr2-arr1))
Fang Yuedong's avatar
Fang Yuedong committed
109
110
        print('deltaA-max:', np.max(np.abs(arr2-arr1)))
        print('deltaA-min:', np.min(np.abs(arr2-arr1)))
Wei Chengliang's avatar
Wei Chengliang committed
111

Fang Yuedong's avatar
Fang Yuedong committed
112
        # objB-highSFB
Wei Chengliang's avatar
Wei Chengliang committed
113
114
        obj = galsim.Gaussian(sigma=0.2, flux=10000)
        arr = obj.drawImage(nx=64, ny=64, scale=0.074).array
Fang Yuedong's avatar
Fang Yuedong committed
115
        chip.img.array[(100-32):(100+32), (200-32):(200+32)] = arr[:, :]
Wei Chengliang's avatar
Wei Chengliang committed
116
117
        img_old = copy.deepcopy(chip.img)
        img_new = add_brighter_fatter(img=chip.img)
Fang Yuedong's avatar
Fang Yuedong committed
118
119
        arr3 = img_old.array
        arr4 = img_new.array
Wei Chengliang's avatar
Wei Chengliang committed
120
        deltaB_max = np.max(np.abs(arr4-arr3))
Fang Yuedong's avatar
Fang Yuedong committed
121
122
        print('deltaB-max:', np.max(np.abs(arr4-arr3)))
        print('deltaB-min:', np.min(np.abs(arr4-arr3)))
Wei Chengliang's avatar
Wei Chengliang committed
123

Fang Yuedong's avatar
Fang Yuedong committed
124
        self.assertTrue(deltaB_max > deltaA_max)
Wei Chengliang's avatar
Wei Chengliang committed
125
126
127
128
129
130
131
132
133

    def test_apply_CTE(self):
        config_file = os.path.join(self.dataPath, 'config_test.yaml')
        chip = defineCCD(self.iccd, config_file)
        filt = defineFilt(chip)
        print(chip.chipID)
        print(chip.cen_pix_x, chip.cen_pix_y)

        print("  Apply CTE Effect")
Fang Yuedong's avatar
Fang Yuedong committed
134
        nx, ny, noverscan, nsp, nmax = 4608, 4616, 84, 3, 10
Wei Chengliang's avatar
Wei Chengliang committed
135
        ntotal = 4700
Fang Yuedong's avatar
Fang Yuedong committed
136
137
138
139
        beta, w, c = 0.478, 84700, 0
        t = np.array([0.74, 7.7, 37], dtype=np.float32)
        rho_trap = np.array([0.6, 1.6, 1.4], dtype=np.float32)
        trap_seeds = np.array([0, 100, 1000], dtype=np.int32)
Wei Chengliang's avatar
Wei Chengliang committed
140
        release_seed = 500
Fang Yuedong's avatar
Fang Yuedong committed
141
142
143
144
145
146
147
148
        image = fits.getdata(os.path.join(
            self.dataPath, "testCTE_image_before.fits")).astype(np.int32)
        # get_trap_map(trap_seeds,nx,ny,nmax,rho_trap,beta,c,".")
        # bin2fits("trap.bin",".",nsp,nx,ny,nmax)
        image_cti = CTI_sim(image, nx, ny, noverscan, nsp, nmax,
                            beta, w, c, t, rho_trap, trap_seeds, release_seed)
        fits.writeto(os.path.join(
            self.dataPath, "testCTE_image_after.fits"), data=image_cti, overwrite=True)
Wei Chengliang's avatar
Wei Chengliang committed
149
150
151
152


if __name__ == '__main__':
    unittest.main()