test_prescan_overscan_func.py 3.94 KB
Newer Older
Wei Chengliang's avatar
Wei Chengliang committed
1
2
import unittest

Wei Chengliang's avatar
Wei Chengliang committed
3
4
5
import sys
import os
import math
Wei Chengliang's avatar
Wei Chengliang committed
6
7
8
9
10
from itertools import islice
import numpy as np
import galsim
import yaml

Fang Yuedong's avatar
Fang Yuedong committed
11
from observation_sim.instruments import Chip, Filter, FilterParam, FocalPlane
Wei Chengliang's avatar
Wei Chengliang committed
12

Wei Chengliang's avatar
Wei Chengliang committed
13

Wei Chengliang's avatar
Wei Chengliang committed
14
15
16
# test FUNCTION --- START #
def AddPreScan(GSImage, pre1=27, pre2=4, over1=71, over2=80, nsecy=2, nsecx=8):
    img = GSImage.array
Wei Chengliang's avatar
Wei Chengliang committed
17
18
19
20
    ny, nx = img.shape
    dx = int(nx/nsecx)
    dy = int(ny/nsecy)

Wei Chengliang's avatar
Wei Chengliang committed
21
    imgt = np.zeros([int(nsecy*nsecx), int(ny/nsecy+pre2+over2), int(nx/nsecx+pre1+over1)])
Wei Chengliang's avatar
Wei Chengliang committed
22
23
24
25
26
27
28
    for iy in range(nsecy):
        for ix in range(nsecx):
            if iy % 2 == 0:
                tx = ix
            else:
                tx = (nsecx-1)-ix
            ty = iy
Wei Chengliang's avatar
Wei Chengliang committed
29
            chunkidx = int(tx+ty*nsecx)  # chunk1-[1,2,3,4], chunk2-[5,6,7,8], chunk3-[9,10,11,12], chunk4-[13,14,15,16]
Wei Chengliang's avatar
Wei Chengliang committed
30
31
32
33
34
35
36

            imgtemp = np.zeros([int(ny/nsecy+pre2+over2), int(nx/nsecx+pre1+over1)])
            if int(chunkidx/4) == 0:
                imgtemp[pre2:pre2+dy, pre1:pre1+dx] = img[iy*dy:(iy+1)*dy, ix*dx:(ix+1)*dx]
                imgt[chunkidx, :, :] = imgtemp
            if int(chunkidx/4) == 1:
                imgtemp[pre2:pre2+dy, over1:over1+dx] = img[iy*dy:(iy+1)*dy, ix*dx:(ix+1)*dx]
Wei Chengliang's avatar
Wei Chengliang committed
37
                imgt[chunkidx, :, :] = imgtemp  # [:, ::-1]
Wei Chengliang's avatar
Wei Chengliang committed
38
39
            if int(chunkidx/4) == 2:
                imgtemp[over2:over2+dy, over1:over1+dx] = img[iy*dy:(iy+1)*dy, ix*dx:(ix+1)*dx]
Wei Chengliang's avatar
Wei Chengliang committed
40
                imgt[chunkidx, :, :] = imgtemp  # [::-1, ::-1]
Wei Chengliang's avatar
Wei Chengliang committed
41
42
            if int(chunkidx/4) == 3:
                imgtemp[over2:over2+dy, pre1:pre1+dx] = img[iy*dy:(iy+1)*dy, ix*dx:(ix+1)*dx]
Wei Chengliang's avatar
Wei Chengliang committed
43
                imgt[chunkidx, :, :] = imgtemp  # [::-1, :]
Wei Chengliang's avatar
Wei Chengliang committed
44

Wei Chengliang's avatar
Wei Chengliang committed
45
46
    imgtx1 = np.hstack(imgt[:nsecx:, :, :])  # hstack chunk(1,2)-[1,2,3,4,5,6,7,8]
    imgtx2 = np.hstack(imgt[:(nsecx-1):-1, :, :])  # hstack chunk(4,3)-[16,15,14,13,12,11,,10,9]
Wei Chengliang's avatar
Wei Chengliang committed
47
48

    newimg = galsim.Image(int(nx+(pre1+over1)*nsecx), int(ny+(pre2+over2)*nsecy), init_value=0)
Wei Chengliang's avatar
Wei Chengliang committed
49
    newimg.array[:, :] = np.concatenate([imgtx1, imgtx2])  # vstack chunk(1,2) & chunk(4,3)
Wei Chengliang's avatar
Wei Chengliang committed
50
51
52
53

    newimg.wcs = GSImage.wcs
    return newimg

Wei Chengliang's avatar
Wei Chengliang committed
54
# test FUNCTION --- END #
Wei Chengliang's avatar
Wei Chengliang committed
55
56
57
58
59
60


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

Wei Chengliang's avatar
Wei Chengliang committed
71

Wei Chengliang's avatar
Wei Chengliang committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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 bandpass_list


class detModule_coverage(unittest.TestCase):
    def __init__(self, methodName='runTest'):
        super(detModule_coverage, self).__init__(methodName)
Wei Chengliang's avatar
Wei Chengliang committed
87
        self.dataPath = os.path.join(os.getenv('UNIT_TEST_DATA_ROOT'), 'csst_msc_sim/csst_fz_msc')
Wei Chengliang's avatar
Wei Chengliang committed
88
89
90
91
92
93
94
95
96
97
        self.iccd = 1

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

        chip.img = AddPreScan(GSImage=chip.img,
Wei Chengliang's avatar
Wei Chengliang committed
98
99
100
101
                              pre1=chip.prescan_x,
                              pre2=chip.prescan_y,
                              over1=chip.overscan_x,
                              over2=chip.overscan_y)
Wei Chengliang's avatar
Wei Chengliang committed
102

Wei Chengliang's avatar
Wei Chengliang committed
103
104
        self.assertTrue((chip.prescan_x+chip.overscan_x)*8+chip.npix_x == np.shape(chip.img.array)[1])
        self.assertTrue((chip.prescan_y+chip.overscan_y)*2+chip.npix_y == np.shape(chip.img.array)[0])
Wei Chengliang's avatar
Wei Chengliang committed
105
106
107
108


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