test_BF_CTE.py 5.28 KB
Newer Older
Wei Chengliang's avatar
Wei Chengliang committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
import unittest

import sys,os,math
from itertools import islice
import numpy as np
import copy
import ctypes
import galsim
import yaml
from astropy.io import fits

from ObservationSim.Instrument import Chip, Filter, FilterParam, FocalPlane
from ObservationSim.Instrument.Chip import ChipUtils as chip_utils
#from ObservationSim.sim_steps import add_brighter_fatter_CTE
from ObservationSim.Instrument.Chip.libCTI.CTI_modeling import CTI_sim

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


### test FUNCTION --- START ###
def add_brighter_fatter(img):
    #Inital dynamic lib
    try:
        with pkg_resources.files('ObservationSim.Instrument.Chip.libBF').joinpath("libmoduleBF.so") as lib_path:
            lib_bf = ctypes.CDLL(lib_path)
    except AttributeError:
        with pkg_resources.path('ObservationSim.Instrument.Chip.libBF', "libmoduleBF.so") as lib_path:
            lib_bf = ctypes.CDLL(lib_path)
    lib_bf.addEffects.argtypes = [ctypes.c_int, ctypes.c_int, ctypes.POINTER(ctypes.c_float), ctypes.POINTER(ctypes.c_float), ctypes.c_int]

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

    nx, ny = img.array.shape
    nn = nx * ny
    arr_ima= (ctypes.c_float*nn)()
    arr_imc= (ctypes.c_float*nn)()

    arr_ima[:]= img.array.reshape(nn)
    arr_imc[:]= np.zeros(nn)

    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
### test FUNCTION --- END ###

def defineCCD(iccd, config_file):
    with open(config_file, "r") as stream:
        try:
            config = yaml.safe_load(stream)
            #for key, value in config.items():
            #    print (key + " : " + str(value))
        except yaml.YAMLError as exc:
            print(exc)
    chip = Chip(chipID=iccd, config=config)
    chip.img = galsim.ImageF(400, 200) #galsim.ImageF(chip.npix_x, chip.npix_y)
    focal_plane = FocalPlane(chip_list=[iccd])
    chip.img.wcs= focal_plane.getTanWCS(192.8595, 27.1283, -113.4333*galsim.degrees, chip.pix_scale)
    return chip

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)
Wei Chengliang's avatar
Wei Chengliang committed
82
        ##self.dataPath = "/public/home/chengliang/CSSOSDataProductsSims/csst-simulation/tests/UNIT_TEST_DATA" ##os.path.join(os.getenv('UNIT_TEST_DATA_ROOT'), 'csst_fz_gc1')
Wei Chengliang's avatar
Wei Chengliang committed
83
        self.dataPath = os.path.join(os.getenv('UNIT_TEST_DATA_ROOT'), 'csst_msc_sim/csst_fz_msc')
Wei Chengliang's avatar
Wei Chengliang committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
        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)

        #objA-lowSFB
        obj = galsim.Gaussian(sigma=0.2, flux=1000)
        arr = obj.drawImage(nx=64, ny=64, scale=0.074).array
        chip.img.array[(100-32):(100+32),(200-32):(200+32)] = arr[:,:] 
        img_old = copy.deepcopy(chip.img)
        img_new = add_brighter_fatter(img=chip.img)
        arr1= img_old.array
        arr2= img_new.array
        deltaA_max = np.max(np.abs(arr2-arr1))
        print('deltaA-max:', np.max(np.abs(arr2-arr1))) 
        print('deltaA-min:', np.min(np.abs(arr2-arr1))) 

        #objB-highSFB
        obj = galsim.Gaussian(sigma=0.2, flux=10000)
        arr = obj.drawImage(nx=64, ny=64, scale=0.074).array
        chip.img.array[(100-32):(100+32),(200-32):(200+32)] = arr[:,:] 
        img_old = copy.deepcopy(chip.img)
        img_new = add_brighter_fatter(img=chip.img)
        arr3= img_old.array
        arr4= img_new.array
        deltaB_max = np.max(np.abs(arr4-arr3))
        print('deltaB-max:', np.max(np.abs(arr4-arr3))) 
        print('deltaB-min:', np.min(np.abs(arr4-arr3))) 
        
        self.assertTrue( deltaB_max > deltaA_max )


    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")
        nx,ny,noverscan,nsp,nmax = 4608,4616,84,3,10
        ntotal = 4700
        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)
        release_seed = 500
Wei Chengliang's avatar
Wei Chengliang committed
136
        image = fits.getdata(os.path.join(self.dataPath, "testCTE_image_before.fits")).astype(np.int32) 
Wei Chengliang's avatar
Wei Chengliang committed
137
138
139
        #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)
Wei Chengliang's avatar
Wei Chengliang committed
140
        fits.writeto(os.path.join(self.dataPath, "testCTE_image_after.fits"),data=image_cti,overwrite=True)
Wei Chengliang's avatar
Wei Chengliang committed
141
142
143
144


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