Chip.py 36 KB
Newer Older
Fang Yuedong's avatar
Fang Yuedong committed
1
2
3
4
5
import galsim
import os
import numpy as np
from astropy.table import Table
from numpy.random import Generator, PCG64
Fang Yuedong's avatar
Fang Yuedong committed
6
7
from astropy.io import fits
from datetime import datetime
Fang Yuedong's avatar
Fang Yuedong committed
8

9
10
11
12
from ObservationSim.Instrument.Chip import Effects as effects
from ObservationSim.Instrument.FocalPlane import FocalPlane
from ObservationSim.Config.Header import generatePrimaryHeader, generateExtensionHeader

Fang Yuedong's avatar
Fang Yuedong committed
13
14
15
16
17
18
try:
    import importlib.resources as pkg_resources
except ImportError:
    # Try backported to PY<37 'importlib_resources'
    import importlib_resources as pkg_resources

Fang Yuedong's avatar
Fang Yuedong committed
19
class Chip(FocalPlane):
Fang Yuedong's avatar
Fang Yuedong committed
20
    def __init__(self, chipID, ccdEffCurve_dir=None, CRdata_dir=None, sls_dir=None, config=None, treering_func=None):
Fang Yuedong's avatar
Fang Yuedong committed
21
22
23
24
25
26
27
28
        # Get focal plane (instance of paraent class) info
        # TODO: use chipID to config individual chip?
        super().__init__()
        self.npix_x = 9216
        self.npix_y = 9232
        self.read_noise = 5.0   # e/pix
        self.dark_noise = 0.02  # e/pix/s
        self.pix_scale  = 0.074 # pixel scale
Fang Yuedong's avatar
Fang Yuedong committed
29
30
        self.gain = float(config["ins_effects"]["gain"])
        self.bias_level = float(config["ins_effects"]["bias_level"])
Fang Yuedong's avatar
Fang Yuedong committed
31
        self.overscan   = 1000
Fang Yuedong's avatar
Fang Yuedong committed
32
33
34
35
        self.exptime    = float(config["obs_setting"]["exp_time"])   # second
        self.dark_exptime = float(config["ins_effects"]['dark_exptime'])
        self.flat_exptime = float(config["ins_effects"]['flat_exptime'])
        self.readout_time = float(config["ins_effects"]['readout_time'])
Fang Yuedong's avatar
Fang Yuedong committed
36
37
38
39
40
41
42
43
44
45
46
47
48

        # A chip ID must be assigned
        self.chipID = int(chipID)
        self._getChipRowCol()

        # Get corresponding filter info
        self.filter_id, self.filter_type = self.getChipFilter()
        self.survey_type = self._getSurveyType()

        # Get boundary (in pix)
        self.bound = self.getChipLim()
        self.ccdEffCurve_dir = ccdEffCurve_dir
        self.CRdata_dir = CRdata_dir
Fang Yuedong's avatar
Fang Yuedong committed
49
        # self.sls_dir=sls_dir
Fang Yuedong's avatar
Fang Yuedong committed
50
51
52
        # self.sls_conf = os.path.join(self.sls_dir, self.getChipSLSConf())
        slsconfs = self.getChipSLSConf()
        if np.size(slsconfs) == 1:
Fang Yuedong's avatar
Fang Yuedong committed
53
54
55
            # self.sls_conf = [os.path.join(self.sls_dir, slsconfs)]
            with pkg_resources.path('ObservationSim.Instrument.data.sls_conf', slsconfs) as conf_path:
                self.sls_conf = str(conf_path)
Fang Yuedong's avatar
Fang Yuedong committed
56
        else:
Fang Yuedong's avatar
Fang Yuedong committed
57
58
59
60
61
62
            # self.sls_conf = [os.path.join(self.sls_dir, slsconfs[0]), os.path.join(self.sls_dir, slsconfs[1])]
            self.sls_conf = []
            with pkg_resources.path('ObservationSim.Instrument.data.sls_conf', slsconfs[0]) as conf_path:
                self.sls_conf.append(str(conf_path))
            with pkg_resources.path('ObservationSim.Instrument.data.sls_conf', slsconfs[1]) as conf_path:
                self.sls_conf.append(str(conf_path))
Fang Yuedong's avatar
Fang Yuedong committed
63
64
65
66
67
        
        self.effCurve = self._getChipEffCurve(self.filter_type)
        self._getCRdata()

        # Define the sensor
Fang Yuedong's avatar
Fang Yuedong committed
68
        if config["ins_effects"]["bright_fatter"] == True and self.survey_type == "photometric":
Fang Yuedong's avatar
Fang Yuedong committed
69
            self.sensor = galsim.SiliconSensor(strength=config["ins_effects"]["df_strength"], treering_func=treering_func)
Fang Yuedong's avatar
Fang Yuedong committed
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
        else:
            self.sensor = galsim.Sensor()

    # def _getChipRowCol(self):
    #     self.rowID = (self.chipID - 1) // 5 + 1
    #     self.colID = (self.chipID - 1) % 5 + 1
    def _getChipRowCol(self):
        self.rowID, self.colID = self.getChipRowCol(self.chipID)

    def getChipRowCol(self, chipID):
        rowID = ((chipID - 1) % 5) + 1
        colID = 6 - ((chipID - 1) // 5)
        return rowID, colID

    def _getSurveyType(self):
        if self.filter_type in ["GI", "GV", "GU"]:
            return "spectroscopic"
        else:
            return "photometric"

    def _getChipEffCurve(self, filter_type):
        # CCD efficiency curves
        if filter_type in ['nuv', 'u', 'GU']: filename = 'UV0.txt'
        if filter_type in ['g', 'r', 'GV']: filename = 'Astro_MB.txt'
        if filter_type in ['i', 'z', 'y', 'GI']: filename = 'Basic_NIR.txt'
        # Mirror efficiency:
        if filter_type == 'nuv': mirror_eff = 0.54
        if filter_type == 'u': mirror_eff = 0.68
        if filter_type in ['g', 'r', 'i', 'z', 'y']: mirror_eff = 0.8
        if filter_type in ['GU', 'GV', 'GI']: mirror_eff = 1. # Not sure if this is right
        
Fang Yuedong's avatar
Fang Yuedong committed
101
102
103
104
        # path = os.path.join(self.ccdEffCurve_dir, filename)
        # table = Table.read(path, format='ascii')
        with pkg_resources.path('ObservationSim.Instrument.data.ccd', filename) as ccd_path:
            table = Table.read(ccd_path, format='ascii')
Fang Yuedong's avatar
Fang Yuedong committed
105
106
107
108
109
        throughput = galsim.LookupTable(x=table['col1'], f=table['col2']*mirror_eff, interpolant='linear')
        bandpass = galsim.Bandpass(throughput, wave_type='nm')
        return bandpass

    def _getCRdata(self):
Fang Yuedong's avatar
Fang Yuedong committed
110
111
112
113
        # path = os.path.join(self.CRdata_dir, 'wfc-cr-attachpixel.dat')
        # self.attachedSizes = np.loadtxt(path)
        with pkg_resources.path('ObservationSim.Instrument.data', "wfc-cr-attachpixel.dat") as cr_path:
            self.attachedSizes = np.loadtxt(cr_path)
Fang Yuedong's avatar
Fang Yuedong committed
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249

    def getChipFilter(self, chipID=None, filter_layout=None):
        """Return the filter index and type for a given chip #(chipID)
        """
        filter_type_list = ["nuv","u", "g", "r", "i","z","y","GU", "GV", "GI"]
        # TODO: maybe a more elegent way other than hard coded?
        # e.g. use something like a nested dict:
        if filter_layout is not None:
            return filter_layout[chipID][0], filter_layout[chipID][1]
        if chipID == None:
            chipID = self.chipID

        # updated configurations
        # if chipID>30 or chipID<1: raise ValueError("!!! Chip ID: [1,30]")
        # if chipID in [10, 15, 16, 21]: filter_type = 'y'
        # if chipID in [11, 20]:         filter_type = "z"
        # if chipID in [9, 22]:           filter_type = "i"
        # if chipID in [12, 19]:         filter_type = "u"
        # if chipID in [7, 24]:         filter_type = "r"
        # if chipID in [14, 13, 18, 17]:    filter_type = "nuv"
        # if chipID in [8, 23]:         filter_type = "g"
        # if chipID in [6, 5, 25, 26]:    filter_type = "GI"
        # if chipID in [27, 30, 1, 4]:    filter_type = "GV"
        # if chipID in [28, 29, 2, 3]:    filter_type = "GU"
        if chipID in [6, 15, 16, 25]: filter_type = "y"
        if chipID in [11, 20]:         filter_type = "z"
        if chipID in [7, 24]:           filter_type = "i"
        if chipID in [14, 17]:         filter_type = "u"
        if chipID in [9, 22]:         filter_type = "r"
        if chipID in [12, 13, 18, 19]:    filter_type = "nuv"
        if chipID in [8, 23]:         filter_type = "g"
        if chipID in [1, 10, 21, 30]:    filter_type = "GI"
        if chipID in [2, 5, 26, 29]:    filter_type = "GV"
        if chipID in [3, 4, 27, 28]:    filter_type = "GU"
        filter_id = filter_type_list.index(filter_type)

        return filter_id, filter_type

    def getChipLim(self, chipID=None):
        """Calculate the edges in pixel for a given CCD chip on the focal plane
        NOTE: There are 5*4 CCD chips in the focus plane for photometric observation.
        Parameters:
            chipID:         int
                            the index of the chip
        Returns:
            A galsim BoundsD object
        """
        # if chipID == None:
        #     chipID = self.chipID
        
        # gx = self.npix_gap_x
        # gy1, gy2 = self.npix_gap_y

        # # xlim of a given ccd chip
        # xrem = (chipID-1)%self.nchip_x - self.nchip_x // 2
        # xcen = (self.npix_x + gx) * xrem
        # nx0 = xcen - self.npix_x//2 + 1
        # nx1 = xcen + self.npix_x//2

        # # ylim of a given ccd chip
        # yrem = 2*((chipID-1)//self.nchip_x) - (self.nchip_y-1)
        # ycen = (self.npix_y//2 + gy1//2) * yrem
        # if chipID <= 6: ycen = (self.npix_y//2 + gy1//2) * yrem - (gy2-gy1)
        # if chipID >= 25: ycen = (self.npix_y//2 + gy1//2) * yrem + (gy2-gy1)
        # ny0 = ycen - self.npix_y//2 + 1
        # ny1 = ycen + self.npix_y//2

        if chipID == None:
            chipID = self.chipID
            rowID, colID = self.rowID, self.colID
        else:
            rowID, colID = self.getChipRowCol(chipID)
        gx1, gx2 = self.npix_gap_x
        gy = self.npix_gap_y

        # xlim of a given CCD chip
        xrem = 2*(colID - 1) - (self.nchip_x - 1)
        xcen = (self.npix_x//2 + gx1//2) * xrem
        if chipID >= 26 or chipID == 21:
            xcen = (self.npix_x//2 + gx1//2) * xrem - (gx2-gx1)
        if chipID <= 5 or chipID == 10:
            xcen = (self.npix_x//2 + gx1//2) * xrem + (gx2-gx1)
        nx0 = xcen - self.npix_x//2 + 1
        nx1 = xcen + self.npix_x//2

        # ylim of a given CCD chip
        yrem = (rowID - 1) - self.nchip_y // 2
        ycen = (self.npix_y + gy) * yrem
        ny0 = ycen - self.npix_y//2 + 1
        ny1 = ycen + self.npix_y//2

        return galsim.BoundsD(nx0-1, nx1-1, ny0-1, ny1-1)


    def getSkyCoverage(self, wcs):
        return super().getSkyCoverage(wcs, self.bound.xmin, self.bound.xmax, self.bound.ymin, self.bound.ymax)


    def getSkyCoverageEnlarged(self, wcs, margin=0.5):
        """The enlarged sky coverage of the chip
        """
        margin /= 60.0
        bound = self.getSkyCoverage(wcs)
        return galsim.BoundsD(bound.xmin - margin, bound.xmax + margin, bound.ymin - margin, bound.ymax + margin)

    def isContainObj(self, ra_obj, dec_obj, wcs=None, margin=1):
        # magin in number of pix
        if wcs is None:
            wcs = self.img.wcs
        pos_obj = wcs.toImage(galsim.CelestialCoord(ra=ra_obj*galsim.degrees, dec=dec_obj*galsim.degrees))
        xmin, xmax = self.bound.xmin - margin, self.bound.xmax + margin
        ymin, ymax = self.bound.ymin - margin, self.bound.ymax + margin
        if (pos_obj.x - xmin) * (pos_obj.x - xmax) > 0.0 or (pos_obj.y - ymin) * (pos_obj.y - ymax) > 0.0:
            return False
        return True

    def getChipNoise(self, exptime=150.0):
        noise = self.dark_noise * exptime + self.read_noise**2
        return noise

    def getChipSLSConf(self):
        confFile = ''
        if self.chipID == 1: confFile = ['CSST_GI2.conf', 'CSST_GI1.conf']
        if self.chipID == 2: confFile = ['CSST_GV4.conf', 'CSST_GV3.conf']
        if self.chipID == 3: confFile = ['CSST_GU2.conf', 'CSST_GU1.conf']
        if self.chipID == 4: confFile = ['CSST_GU4.conf', 'CSST_GU3.conf']
        if self.chipID == 5: confFile = ['CSST_GV2.conf', 'CSST_GV1.conf']
        if self.chipID == 10: confFile = ['CSST_GI4.conf', 'CSST_GI3.conf']
        if self.chipID == 21: confFile = ['CSST_GI6.conf', 'CSST_GI5.conf']
        if self.chipID == 26: confFile = ['CSST_GV8.conf', 'CSST_GV7.conf']
        if self.chipID == 27: confFile = ['CSST_GU6.conf', 'CSST_GU5.conf']
        if self.chipID == 28: confFile = ['CSST_GU8.conf', 'CSST_GU7.conf']
        if self.chipID == 29: confFile = ['CSST_GV6.conf', 'CSST_GV5.conf']
        if self.chipID == 30: confFile = ['CSST_GI8.conf', 'CSST_GI7.conf']
        return confFile

Fang Yuedong's avatar
Fang Yuedong committed
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
    def generateHeader(self, ra_cen, dec_cen, img_rot, im_type, pointing_ID, date_obs, time_obs, exptime=150.):
        h_prim = generatePrimaryHeader(
            xlen=self.npix_x, 
            ylen=self.npix_y, 
            pointNum = str(pointing_ID),
            ra=ra_cen, 
            dec=dec_cen, 
            psize=self.pix_scale, 
            row_num=self.rowID, 
            col_num=self.colID,
            date=date_obs,
            time_obs=time_obs,
            im_type = im_type,
            exptime=exptime
            )
        h_ext = generateExtensionHeader(
            xlen=self.npix_x, 
            ylen=self.npix_y, 
            ra=ra_cen, 
            dec=dec_cen, 
            pa=img_rot.deg, 
            gain=self.gain, 
            readout=self.read_noise, 
            dark=self.dark_noise, 
            saturation=90000, 
            psize=self.pix_scale, 
            row_num=self.rowID, 
            col_num=self.colID,
            extName='raw')
        return h_prim, h_ext

    def outputCal(self, img, ra_cen, dec_cen, img_rot, im_type, pointing_ID, date_obs, time_obs, output_dir, exptime=150.):
        h_prim, h_ext = self.generateHeader(
            ra_cen=ra_cen,
            dec_cen=dec_cen,
            img_rot=img_rot,
            im_type=im_type,
            pointing_ID=pointing_ID,
            date_obs=date_obs,
            time_obs=time_obs,
            exptime=exptime)
        hdu1 = fits.PrimaryHDU(header=h_prim)
        hdu2 = fits.ImageHDU(img.array, header=h_ext)
        hdu1 = fits.HDUList([hdu1, hdu2])
        fname = os.path.join(output_dir, h_prim['FILENAME']+'.fits')
        hdu1.writeto(fname, output_verify='ignore', overwrite=True)

297
    def addEffects(self, config, img, chip_output, filt, ra_cen, dec_cen, img_rot, exptime=150., pointing_ID=0, timestamp_obs=1621915200, pointing_type='MS', sky_map=None, tel=None):
Fang Yuedong's avatar
Fang Yuedong committed
298
299
300
301
302
303
304
305
        SeedGainNonuni=int(config["random_seeds"]["seed_gainNonUniform"])
        SeedBiasNonuni=int(config["random_seeds"]["seed_biasNonUniform"])
        SeedRnNonuni = int(config["random_seeds"]["seed_rnNonUniform"])
        SeedBadColumns = int(config["random_seeds"]["seed_badcolumns"])
        SeedDefective = int(config["random_seeds"]["seed_defective"])
        SeedCosmicRay = int(config["random_seeds"]["seed_CR"])
        fullwell = int(config["ins_effects"]["full_well"])
        if config["ins_effects"]["add_hotpixels"] == True:
Fang Yuedong's avatar
Fang Yuedong committed
306
307
308
            BoolHotPix = True
        else:
            BoolHotPix = False
Fang Yuedong's avatar
Fang Yuedong committed
309
        if config["ins_effects"]["add_deadpixels"]== True:
Fang Yuedong's avatar
Fang Yuedong committed
310
311
312
313
            BoolDeadPix = True
        else:
            BoolDeadPix = False

314
        # Add sky background
Zhang Xin's avatar
Zhang Xin committed
315
        if sky_map is None:
316
317
318
319
320
            sky_map = filt.getSkyNoise(exptime=self.exptime)
        elif img.array.shape != sky_map.shape:
            raise ValueError("The shape img and sky_map must be equal.")
        elif tel is not None: # If sky_map is given in flux
            sky_map = sky_map * tel.pupil_area * self.exptime
Fang Yuedong's avatar
Fang Yuedong committed
321
        if config["ins_effects"]["add_back"] == True:
322
323
324
            img += sky_map
        del sky_map

Fang Yuedong's avatar
Fang Yuedong committed
325
        # Apply flat-field large scale structure for one chip
Fang Yuedong's avatar
Fang Yuedong committed
326
        if config["ins_effects"]["flat_fielding"] == True:
Fang Yuedong's avatar
Fang Yuedong committed
327
328
329
330
            print("  Creating and applying Flat-Fielding", flush=True)
            print(img.bounds, flush=True)
            flat_img = effects.MakeFlatSmooth(
                img.bounds, 
Fang Yuedong's avatar
Fang Yuedong committed
331
                int(config["random_seeds"]["seed_flat"]))
Fang Yuedong's avatar
Fang Yuedong committed
332
            flat_normal = flat_img / np.mean(flat_img.array)
333
334
            if self.survey_type == "photometric":
                img *= flat_normal
Fang Yuedong's avatar
Fang Yuedong committed
335
            del flat_normal
Fang Yuedong's avatar
Fang Yuedong committed
336
            if config["output_setting"]["flat_output"] == False:
Fang Yuedong's avatar
Fang Yuedong committed
337
338
339
                del flat_img

        # Apply Shutter-effect for one chip
Fang Yuedong's avatar
Fang Yuedong committed
340
        if config["ins_effects"]["shutter_effect"] == True:
Fang Yuedong's avatar
Fang Yuedong committed
341
342
            print("  Apply shutter effect", flush=True)
            shuttimg = effects.ShutterEffectArr(img, t_shutter=1.3, dist_bearing=735, dt=1E-3)    # shutter effect normalized image for this chip
343
344
            if self.survey_type == "photometric":
                img *= shuttimg
Fang Yuedong's avatar
Fang Yuedong committed
345
            if config["output_setting"]["shutter_output"] == True:    # output 16-bit shutter effect image with pixel value <=65535
Fang Yuedong's avatar
Fang Yuedong committed
346
347
348
349
350
                shutt_gsimg = galsim.ImageUS(shuttimg*6E4)
                shutt_gsimg.write("%s/ShutterEffect_%s_1.fits" % (chip_output.subdir, self.chipID))
                del shutt_gsimg
            del shuttimg

351
        # Add Poisson noise
Fang Yuedong's avatar
Fang Yuedong committed
352
        seed = int(config["random_seeds"]["seed_poisson"]) + pointing_ID*30 + self.chipID
353
354
355
        rng_poisson = galsim.BaseDeviate(seed)
        poisson_noise = galsim.PoissonNoise(rng_poisson, sky_level=0.)
        img.addNoise(poisson_noise)
Fang Yuedong's avatar
Fang Yuedong committed
356
357

        # Add cosmic-rays
Fang Yuedong's avatar
Fang Yuedong committed
358
        if config["ins_effects"]["cosmic_ray"] == True and pointing_type=='MS':
Fang Yuedong's avatar
Fang Yuedong committed
359
            print("  Adding Cosmic-Ray", flush=True)
Fang Yuedong's avatar
Fang Yuedong committed
360
            cr_map, cr_event_num = effects.produceCR_Map(
Fang Yuedong's avatar
Fang Yuedong committed
361
                xLen=self.npix_x, yLen=self.npix_y, 
362
                exTime=self.exptime+0.5*self.readout_time, 
Fang Yuedong's avatar
Fang Yuedong committed
363
                cr_pixelRatio=0.003*(self.exptime+0.5*self.readout_time)/150., 
Fang Yuedong's avatar
Fang Yuedong committed
364
365
                gain=self.gain, 
                attachedSizes=self.attachedSizes,
Fang Yuedong's avatar
Fang Yuedong committed
366
                seed=SeedCosmicRay+pointing_ID*30+self.chipID)   # seed: obj-imaging:+0; bias:+1; dark:+2; flat:+3;
Fang Yuedong's avatar
Fang Yuedong committed
367
368
369
370
            img += cr_map
            cr_map[cr_map > 65535] = 65535
            cr_map[cr_map < 0] = 0
            crmap_gsimg = galsim.Image(cr_map, dtype=np.uint16)
Fang Yuedong's avatar
Fang Yuedong committed
371
            del cr_map
Fang Yuedong's avatar
Fang Yuedong committed
372
            # crmap_gsimg.write("%s/CosmicRay_%s_1.fits" % (chip_output.subdir, self.chipID))
Fang Yuedong's avatar
Fang Yuedong committed
373
374
375
376
377
378
379
380
381
382
383
384
385
386
            # crmap_gsimg.write("%s/CosmicRay_%s.fits" % (chip_output.subdir, self.chipID))
            datetime_obs = datetime.fromtimestamp(timestamp_obs)
            date_obs = datetime_obs.strftime("%y%m%d")
            time_obs = datetime_obs.strftime("%H%M%S")
            self.outputCal(
                img=crmap_gsimg,
                ra_cen=ra_cen,
                dec_cen=dec_cen,
                img_rot=img_rot,
                im_type='CRS',
                pointing_ID=pointing_ID,
                date_obs=date_obs,
                time_obs=time_obs,
                output_dir=chip_output.subdir,
Fang Yuedong's avatar
Fang Yuedong committed
387
                exptime=self.exptime)
Fang Yuedong's avatar
Fang Yuedong committed
388
389
            del crmap_gsimg

390
        # Apply PRNU effect and output PRNU flat file:
Fang Yuedong's avatar
Fang Yuedong committed
391
        if config["ins_effects"]["prnu_effect"] == True:
392
393
394
395
396
            print("  Applying PRNU effect", flush=True)
            prnu_img = effects.PRNU_Img(
                xsize=self.npix_x, 
                ysize=self.npix_y, 
                sigma=0.01, 
Fang Yuedong's avatar
Fang Yuedong committed
397
                seed=int(config["random_seeds"]["seed_prnu"]+self.chipID))
398
            img *= prnu_img
Fang Yuedong's avatar
Fang Yuedong committed
399
            if config["output_setting"]["prnu_output"] == True:
400
                prnu_img.write("%s/FlatImg_PRNU_%s.fits" % (chip_output.subdir,self.chipID))
Fang Yuedong's avatar
Fang Yuedong committed
401
            if config["output_setting"]["flat_output"] == False:
402
403
404
                del prnu_img

        # Add dark current
Fang Yuedong's avatar
Fang Yuedong committed
405
        if config["ins_effects"]["add_dark"] == True:
406
407
408
409
410
411
412
413
414
            dark_noise = galsim.DeviateNoise(galsim.PoissonDeviate(rng_poisson, self.dark_noise*(self.exptime+0.5*self.readout_time)))
            img.addNoise(dark_noise)

        # Add Hot Pixels or/and Dead Pixels
        rgbadpix = Generator(PCG64(int(SeedDefective+self.chipID)))
        badfraction = 5E-5*(rgbadpix.random()*0.5+0.7)
        img = effects.DefectivePixels(img, IfHotPix=BoolHotPix, IfDeadPix=BoolDeadPix, fraction=badfraction, seed=SeedDefective+self.chipID, biaslevel=0)

        # Apply Bad lines 
Fang Yuedong's avatar
Fang Yuedong committed
415
        if config["ins_effects"]["add_badcolumns"] == True:
416
417
            img = effects.BadColumns(img, seed=SeedBadColumns, chipid=self.chipID)

Fang Yuedong's avatar
Fang Yuedong committed
418
        # Add Bias level
Fang Yuedong's avatar
Fang Yuedong committed
419
        if config["ins_effects"]["add_bias"] == True:
Fang Yuedong's avatar
Fang Yuedong committed
420
421
            print("  Adding Bias level and 16-channel non-uniformity")
            img = effects.AddBiasNonUniform16(img, 
Fang Yuedong's avatar
Fang Yuedong committed
422
                bias_level=float(config["ins_effects"]["bias_level"]), 
Fang Yuedong's avatar
Fang Yuedong committed
423
424
425
                nsecy = 2, nsecx=8, 
                seed=SeedBiasNonuni+self.chipID)

426
        # Apply Nonlinearity on the chip image
Fang Yuedong's avatar
Fang Yuedong committed
427
        if config["ins_effects"]["non_linear"] == True:
428
429
430
431
            print("  Applying Non-Linearity on the chip image", flush=True)
            img = effects.NonLinearity(GSImage=img, beta1=5.e-7, beta2=0)

        # Apply CCD Saturation & Blooming
Fang Yuedong's avatar
Fang Yuedong committed
432
        if config["ins_effects"]["saturbloom"] == True:
433
434
435
436
            print("  Applying CCD Saturation & Blooming")
            img = effects.SaturBloom(GSImage=img, nsect_x=1, nsect_y=1, fullwell=fullwell)

        # Apply CTE Effect
Fang Yuedong's avatar
Fang Yuedong committed
437
        if config["ins_effects"]["cte_trail"] == True:
438
439
440
441
            print("  Apply CTE Effect")
            img = effects.CTE_Effect(GSImage=img, threshold=27)

        # Add Read-out Noise
Fang Yuedong's avatar
Fang Yuedong committed
442
443
        if config["ins_effects"]["add_readout"] == True:
            seed = int(config["random_seeds"]["seed_readout"]) + pointing_ID*30 + self.chipID
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
            rng_readout = galsim.BaseDeviate(seed)
            readout_noise = galsim.GaussianNoise(rng=rng_readout, sigma=self.read_noise)
            img.addNoise(readout_noise)


        # Apply Gain & Quantization
        print("  Applying Gain (and 16 channel non-uniformity) & Quantization", flush=True)
        img = effects.ApplyGainNonUniform16(
            img, gain=self.gain, 
            nsecy = 2, nsecx=8, 
            seed=SeedGainNonuni+self.chipID)
        img.array[img.array > 65535] = 65535
        img.replaceNegative(replace_value=0)
        img.quantize()

        ######################################################################################
        # Output images for calibration pointing
        ######################################################################################
Fang Yuedong's avatar
Fang Yuedong committed
462
        # Bias output
Fang Yuedong's avatar
Fang Yuedong committed
463
        if config["output_setting"]["bias_output"] == True and pointing_type=='CAL':
Fang Yuedong's avatar
Fang Yuedong committed
464
            print("  Output N frame Bias files", flush=True)
Fang Yuedong's avatar
Fang Yuedong committed
465
            NBias = int(config["ins_effects"]["NBias"])
Fang Yuedong's avatar
Fang Yuedong committed
466
467
468
            for i in range(NBias):
                BiasCombImg, BiasTag = effects.MakeBiasNcomb(
                    self.npix_x, self.npix_y, 
Fang Yuedong's avatar
Fang Yuedong committed
469
                    bias_level=float(config["ins_effects"]["bias_level"]), 
Fang Yuedong's avatar
Fang Yuedong committed
470
471
                    ncombine=1, read_noise=self.read_noise, gain=1,
                    seed=SeedBiasNonuni+self.chipID)
Fang Yuedong's avatar
Fang Yuedong committed
472
473
474
                if config["ins_effects"]["cosmic_ray"] == True:
                    if config["ins_effects"]["cray_differ"] == True:
                        cr_map, cr_event_num = effects.produceCR_Map(
Fang Yuedong's avatar
Fang Yuedong committed
475
476
                            xLen=self.npix_x, yLen=self.npix_y, 
                            exTime=0.01, 
Fang Yuedong's avatar
Fang Yuedong committed
477
                            cr_pixelRatio=0.003*(0.01+0.5*self.readout_time)/150., 
Fang Yuedong's avatar
Fang Yuedong committed
478
479
480
481
482
483
484
                            gain=self.gain, 
                            attachedSizes=self.attachedSizes,
                            seed=SeedCosmicRay+pointing_ID*30+self.chipID+1)
                            # seed: obj-imaging:+0; bias:+1; dark:+2; flat:+3;
                    BiasCombImg += cr_map
                    del cr_map

Fang Yuedong's avatar
Fang Yuedong committed
485
                # Non-Linearity for Bias
Fang Yuedong's avatar
Fang Yuedong committed
486
                if config["ins_effects"]["non_linear"] == True:
Fang Yuedong's avatar
Fang Yuedong committed
487
                    print("  Applying Non-Linearity on the Bias image", flush=True)
488
489
490
                    BiasCombImg = effects.NonLinearity(GSImage=BiasCombImg, beta1=5.e-7, beta2=0)

                # Apply Bad lines 
Fang Yuedong's avatar
Fang Yuedong committed
491
492
                if config["ins_effects"]["add_badcolumns"] == True:
                    BiasCombImg = effects.BadColumns(BiasCombImg-float(config["ins_effects"]["bias_level"])+5, seed=SeedBadColumns, chipid=self.chipID) + float(config["ins_effects"]["bias_level"])-5
Fang Yuedong's avatar
Fang Yuedong committed
493
494
495
496
497
498

                BiasCombImg = effects.ApplyGainNonUniform16(BiasCombImg, gain=self.gain, 
                    nsecy = 2, nsecx=8, 
                    seed=SeedGainNonuni+self.chipID)
                # BiasCombImg = effects.AddOverscan(
                #     BiasCombImg, 
Fang Yuedong's avatar
Fang Yuedong committed
499
                #     overscan=float(config["ins_effects"]["bias_level"])-2, gain=self.gain, 
Fang Yuedong's avatar
Fang Yuedong committed
500
501
502
503
                #     widthl=27, widthr=27, widtht=8, widthb=8)
                BiasCombImg.replaceNegative(replace_value=0)
                BiasCombImg.quantize()
                BiasCombImg = galsim.ImageUS(BiasCombImg)
Fang Yuedong's avatar
Fang Yuedong committed
504
505
506
507
                # BiasCombImg.write("%s/BiasImg_%s_%s_%s.fits" % (chip_output.subdir, BiasTag, self.chipID, i+1))
                datetime_obs = datetime.fromtimestamp(timestamp_obs)
                date_obs = datetime_obs.strftime("%y%m%d")
                time_obs = datetime_obs.strftime("%H%M%S")
508
                timestamp_obs += 10 * 60
Fang Yuedong's avatar
Fang Yuedong committed
509
510
511
512
513
514
515
516
517
518
519
                self.outputCal(
                    img=BiasCombImg,
                    ra_cen=ra_cen,
                    dec_cen=dec_cen,
                    img_rot=img_rot,
                    im_type='CLB',
                    pointing_ID=pointing_ID,
                    date_obs=date_obs,
                    time_obs=time_obs,
                    output_dir=chip_output.subdir,
                    exptime=0.0)
Fang Yuedong's avatar
Fang Yuedong committed
520
521
522
            del BiasCombImg

        # Export combined (ncombine, Vignetting + PRNU) & single vignetting flat-field file
Fang Yuedong's avatar
Fang Yuedong committed
523
        if config["output_setting"]["flat_output"] == True and pointing_type=='CAL':
Fang Yuedong's avatar
Fang Yuedong committed
524
            print("  Output N frame Flat-Field files", flush=True)
Fang Yuedong's avatar
Fang Yuedong committed
525
526
            NFlat = int(config["ins_effects"]["NFlat"])
            if config["ins_effects"]["add_bias"] == True:
Fang Yuedong's avatar
Fang Yuedong committed
527
528
                biaslevel = self.bias_level
                overscan = biaslevel-2
Fang Yuedong's avatar
Fang Yuedong committed
529
            elif config["ins_effects"]["add_bias"] == True:
Fang Yuedong's avatar
Fang Yuedong committed
530
531
                biaslevel = 0
                overscan = 0
532
            darklevel = self.dark_noise*(self.flat_exptime+0.5*self.readout_time)
Fang Yuedong's avatar
Fang Yuedong committed
533
534
535
536
537
538
539
540
            for i in range(NFlat):
                FlatSingle = flat_img * prnu_img + darklevel
                FlatCombImg, FlatTag = effects.MakeFlatNcomb(
                    flat_single_image=FlatSingle, 
                    ncombine=1, 
                    read_noise=self.read_noise,
                    gain=1, 
                    overscan=overscan, 
541
                    biaslevel=0,
Fang Yuedong's avatar
Fang Yuedong committed
542
543
                    seed_bias=SeedDefective+self.chipID
                    )
Fang Yuedong's avatar
Fang Yuedong committed
544
545
546
                if config["ins_effects"]["cosmic_ray"] == True:
                    if config["ins_effects"]["cray_differ"] == True:
                        cr_map, cr_event_num = effects.produceCR_Map(
Fang Yuedong's avatar
Fang Yuedong committed
547
                            xLen=self.npix_x, yLen=self.npix_y, 
548
                            exTime=self.flat_exptime+0.5*self.readout_time, 
Fang Yuedong's avatar
Fang Yuedong committed
549
                            cr_pixelRatio=0.003*(self.flat_exptime+0.5*self.readout_time)/150., 
Fang Yuedong's avatar
Fang Yuedong committed
550
551
552
553
                            gain=self.gain, 
                            attachedSizes=self.attachedSizes,
                            seed=SeedCosmicRay+pointing_ID*30+self.chipID+3)
                            # seed: obj-imaging:+0; bias:+1; dark:+2; flat:+3;
Fang Yuedong's avatar
Fang Yuedong committed
554
                    FlatCombImg += cr_map
Fang Yuedong's avatar
Fang Yuedong committed
555
                    del cr_map
Fang Yuedong's avatar
Fang Yuedong committed
556

Fang Yuedong's avatar
Fang Yuedong committed
557
                if config["ins_effects"]["non_linear"] == True:
Fang Yuedong's avatar
Fang Yuedong committed
558
                    print("  Applying Non-Linearity on the Flat image", flush=True)
559
                    FlatCombImg = effects.NonLinearity(GSImage=FlatCombImg, beta1=5.e-7, beta2=0)
Fang Yuedong's avatar
Fang Yuedong committed
560

Fang Yuedong's avatar
Fang Yuedong committed
561
                if config["ins_effects"]["cte_trail"] == True:
Fang Yuedong's avatar
Fang Yuedong committed
562
563
                    FlatCombImg = effects.CTE_Effect(GSImage=FlatCombImg, threshold=3)

564
565
566
567
568
                # Add Hot Pixels or/and Dead Pixels
                rgbadpix = Generator(PCG64(int(SeedDefective+self.chipID)))
                badfraction = 5E-5*(rgbadpix.random()*0.5+0.7)
                FlatCombImg = effects.DefectivePixels(FlatCombImg, IfHotPix=BoolHotPix, IfDeadPix=BoolDeadPix, fraction=badfraction, seed=SeedDefective+self.chipID, biaslevel=0)

Fang Yuedong's avatar
Fang Yuedong committed
569
                # Apply Bad lines 
Fang Yuedong's avatar
Fang Yuedong committed
570
                if config["ins_effects"]["add_badcolumns"] == True:
Fang Yuedong's avatar
Fang Yuedong committed
571
572
                    FlatCombImg = effects.BadColumns(FlatCombImg, seed=SeedBadColumns, chipid=self.chipID)

573
                # Add Bias level
Fang Yuedong's avatar
Fang Yuedong committed
574
                if config["ins_effects"]["add_bias"] == True:
575
                    print("  Adding Bias level and 16-channel non-uniformity")
Fang Yuedong's avatar
Fang Yuedong committed
576
                    # img += float(config["ins_effects"]["bias_level"])
577
578
579
580
581
582
                    FlatCombImg = effects.AddBiasNonUniform16(FlatCombImg, 
                        bias_level=biaslevel, 
                        nsecy = 2, nsecx=8, 
                        seed=SeedBiasNonuni+self.chipID)
                
                # Add Read-out Noise
Fang Yuedong's avatar
Fang Yuedong committed
583
584
                if config["ins_effects"]["add_readout"] == True:
                    seed = int(config["random_seeds"]["seed_readout"]) + pointing_ID*30 + self.chipID
585
586
587
                    rng_readout = galsim.BaseDeviate(seed)
                    readout_noise = galsim.GaussianNoise(rng=rng_readout, sigma=self.read_noise)
                    FlatCombImg.addNoise(readout_noise)
Fang Yuedong's avatar
Fang Yuedong committed
588
589
590
591
592
593
594
595

                FlatCombImg = effects.ApplyGainNonUniform16(FlatCombImg, gain=self.gain, 
                    nsecy = 2, nsecx=8, 
                    seed=SeedGainNonuni+self.chipID)
                # FlatCombImg = effects.AddOverscan(FlatCombImg, overscan=overscan, gain=self.gain, widthl=27, widthr=27, widtht=8, widthb=8)
                FlatCombImg.replaceNegative(replace_value=0)
                FlatCombImg.quantize()
                FlatCombImg = galsim.ImageUS(FlatCombImg)
Fang Yuedong's avatar
Fang Yuedong committed
596
597
598
599
                # FlatCombImg.write("%s/FlatImg_%s_%s_%s.fits" % (chip_output.subdir, FlatTag, self.chipID, i+1))
                datetime_obs = datetime.fromtimestamp(timestamp_obs)
                date_obs = datetime_obs.strftime("%y%m%d")
                time_obs = datetime_obs.strftime("%H%M%S")
600
                timestamp_obs += 10 * 60
Fang Yuedong's avatar
Fang Yuedong committed
601
602
603
604
605
606
607
608
609
610
611
612
                self.outputCal(
                    img=FlatCombImg,
                    ra_cen=ra_cen,
                    dec_cen=dec_cen,
                    img_rot=img_rot,
                    im_type='CLF',
                    pointing_ID=pointing_ID,
                    date_obs=date_obs,
                    time_obs=time_obs,
                    output_dir=chip_output.subdir,
                    exptime=self.flat_exptime)

Fang Yuedong's avatar
Fang Yuedong committed
613
614
615
616
617
618
619
            del FlatCombImg, FlatSingle, prnu_img
            # flat_img.replaceNegative(replace_value=0)
            # flat_img.quantize()
            # galsim.ImageUS(flat_img).write("%s/FlatImg_Vignette_%s.fits" % (chip_output.subdir, self.chipID))
            del flat_img

        # Export Dark current images
Fang Yuedong's avatar
Fang Yuedong committed
620
        if config["output_setting"]["dark_output"] == True and pointing_type=='CAL':
Fang Yuedong's avatar
Fang Yuedong committed
621
            print("  Output N frame Dark Current files", flush=True)
Fang Yuedong's avatar
Fang Yuedong committed
622
623
            NDark = int(config["ins_effects"]["NDark"])
            if config["ins_effects"]["add_bias"] == True:
Fang Yuedong's avatar
Fang Yuedong committed
624
625
                biaslevel = self.bias_level
                overscan = biaslevel-2
Fang Yuedong's avatar
Fang Yuedong committed
626
            elif config["ins_effects"]["add_bias"] == True:
Fang Yuedong's avatar
Fang Yuedong committed
627
628
629
630
631
                biaslevel = 0
                overscan = 0
            for i in range(NDark):
                DarkCombImg, DarkTag = effects.MakeDarkNcomb(
                    self.npix_x, self.npix_y, 
632
                    overscan=overscan, bias_level=0, darkpsec=0.02, exptime=self.dark_exptime+0.5*self.readout_time,
Fang Yuedong's avatar
Fang Yuedong committed
633
634
                    ncombine=1, read_noise=self.read_noise, 
                    gain=1, seed_bias=SeedBiasNonuni+self.chipID)
Fang Yuedong's avatar
Fang Yuedong committed
635
636
637
                if config["ins_effects"]["cosmic_ray"] == True:
                    if config["ins_effects"]["cray_differ"] == True:
                        cr_map, cr_event_num = effects.produceCR_Map(
Fang Yuedong's avatar
Fang Yuedong committed
638
                            xLen=self.npix_x, yLen=self.npix_y, 
639
                            exTime=self.dark_exptime+0.5*self.readout_time, 
Fang Yuedong's avatar
Fang Yuedong committed
640
                            cr_pixelRatio=0.003*(self.dark_exptime+0.5*self.readout_time)/150., 
Fang Yuedong's avatar
Fang Yuedong committed
641
642
643
644
                            gain=self.gain, 
                            attachedSizes=self.attachedSizes,
                            seed=SeedCosmicRay+pointing_ID*30+self.chipID+2)
                            # seed: obj-imaging:+0; bias:+1; dark:+2; flat:+3;
Fang Yuedong's avatar
Fang Yuedong committed
645
                    DarkCombImg += cr_map
Fang Yuedong's avatar
Fang Yuedong committed
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
                    cr_map[cr_map > 65535] = 65535
                    cr_map[cr_map < 0] = 0
                    crmap_gsimg = galsim.Image(cr_map, dtype=np.uint16)
                    del cr_map
                    datetime_obs = datetime.fromtimestamp(timestamp_obs)
                    date_obs = datetime_obs.strftime("%y%m%d")
                    time_obs = datetime_obs.strftime("%H%M%S")
                    self.outputCal(
                        img=crmap_gsimg,
                        ra_cen=ra_cen,
                        dec_cen=dec_cen,
                        img_rot=img_rot,
                        im_type='CRD',
                        pointing_ID=pointing_ID,
                        date_obs=date_obs,
                        time_obs=time_obs,
                        output_dir=chip_output.subdir,
                        exptime=self.dark_exptime)
                    del crmap_gsimg
Fang Yuedong's avatar
Fang Yuedong committed
665
666

                # Non-Linearity for Dark
Fang Yuedong's avatar
Fang Yuedong committed
667
                if config["ins_effects"]["non_linear"] == True:
Fang Yuedong's avatar
Fang Yuedong committed
668
                    print("  Applying Non-Linearity on the Dark image", flush=True)
669
                    DarkCombImg = effects.NonLinearity(GSImage=DarkCombImg, beta1=5.e-7, beta2=0)
Fang Yuedong's avatar
Fang Yuedong committed
670

Fang Yuedong's avatar
Fang Yuedong committed
671
                if config["ins_effects"]["cte_trail"] == True:
Fang Yuedong's avatar
Fang Yuedong committed
672
673
                    DarkCombImg = effects.CTE_Effect(GSImage=DarkCombImg, threshold=3)

674
675
676
677
678
                # Add Hot Pixels or/and Dead Pixels
                rgbadpix = Generator(PCG64(int(SeedDefective+self.chipID)))
                badfraction = 5E-5*(rgbadpix.random()*0.5+0.7)
                DarkCombImg = effects.DefectivePixels(DarkCombImg, IfHotPix=BoolHotPix, IfDeadPix=BoolDeadPix, fraction=badfraction, seed=SeedDefective+self.chipID, biaslevel=0)

Fang Yuedong's avatar
Fang Yuedong committed
679
                # Apply Bad lines 
Fang Yuedong's avatar
Fang Yuedong committed
680
                if config["ins_effects"]["add_badcolumns"] == True:
Fang Yuedong's avatar
Fang Yuedong committed
681
682
                    DarkCombImg = effects.BadColumns(DarkCombImg, seed=SeedBadColumns, chipid=self.chipID)

683
                # Add Bias level
Fang Yuedong's avatar
Fang Yuedong committed
684
                if config["ins_effects"]["add_bias"] == True:
685
                    print("  Adding Bias level and 16-channel non-uniformity")
Fang Yuedong's avatar
Fang Yuedong committed
686
                    # img += float(config["ins_effects"]["bias_level"])
687
688
689
690
691
692
                    DarkCombImg = effects.AddBiasNonUniform16(DarkCombImg, 
                        bias_level=biaslevel, 
                        nsecy = 2, nsecx=8, 
                        seed=SeedBiasNonuni+self.chipID)

                # Add Read-out Noise
Fang Yuedong's avatar
Fang Yuedong committed
693
694
                if config["ins_effects"]["add_readout"] == True:
                    seed = int(config["random_seeds"]["seed_readout"]) + pointing_ID*30 + self.chipID
695
696
697
                    rng_readout = galsim.BaseDeviate(seed)
                    readout_noise = galsim.GaussianNoise(rng=rng_readout, sigma=self.read_noise)
                    DarkCombImg.addNoise(readout_noise)
Fang Yuedong's avatar
Fang Yuedong committed
698
699
700
701
702
703
704
705
706
707
708
709

                DarkCombImg = effects.ApplyGainNonUniform16(
                    DarkCombImg, gain=self.gain, 
                    nsecy = 2, nsecx=8, 
                    seed=SeedGainNonuni+self.chipID)
                # DarkCombImg = effects.AddOverscan(
                #     DarkCombImg, 
                #     overscan=overscan, gain=self.gain, 
                #     widthl=27, widthr=27, widtht=8, widthb=8)
                DarkCombImg.replaceNegative(replace_value=0)
                DarkCombImg.quantize()
                DarkCombImg = galsim.ImageUS(DarkCombImg)
Fang Yuedong's avatar
Fang Yuedong committed
710
711
712
713
                # DarkCombImg.write("%s/DarkImg_%s_%s_%s.fits" % (chip_output.subdir, DarkTag, self.chipID, i+1))
                datetime_obs = datetime.fromtimestamp(timestamp_obs)
                date_obs = datetime_obs.strftime("%y%m%d")
                time_obs = datetime_obs.strftime("%H%M%S")
714
                timestamp_obs += 10 * 60
Fang Yuedong's avatar
Fang Yuedong committed
715
716
717
718
719
720
721
722
723
724
725
                self.outputCal(
                    img=DarkCombImg,
                    ra_cen=ra_cen,
                    dec_cen=dec_cen,
                    img_rot=img_rot,
                    im_type='CLD',
                    pointing_ID=pointing_ID,
                    date_obs=date_obs,
                    time_obs=time_obs,
                    output_dir=chip_output.subdir,
                    exptime=self.dark_exptime)
Fang Yuedong's avatar
Fang Yuedong committed
726
727
728
729
            del DarkCombImg
        # img = galsim.ImageUS(img)

        # # 16 output channel, with each a single image file
Fang Yuedong's avatar
Fang Yuedong committed
730
        # if config["ins_effects"]["readout16"] == True:
Fang Yuedong's avatar
Fang Yuedong committed
731
732
733
734
735
736
737
738
739
740
741
742
        #     print("  16 Output Channel simulation")
        #     for coli in [0, 1]:
        #         for rowi in range(8):
        #             sub_img = effects.readout16(
        #                 GSImage=img, 
        #                 rowi=rowi, 
        #                 coli=coli, 
        #                 overscan_value=self.overscan)
        #             rowcoltag = str(rowi) + str(coli)
        #             img_name_root = chip_output.img_name.split(".")[0]
        #             sub_img.write("%s/%s_%s.fits" % (chip_output.subdir, img_name_root, rowcoltag))
        #     del sub_img
Zhang Xin's avatar
Zhang Xin committed
743
        return img