readout_output.py 4.05 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import os
import galsim
import numpy as np
from astropy.io import fits
from ObservationSim.Instrument.Chip import ChipUtils as chip_utils
from ObservationSim.Instrument.Chip import Effects

def add_prescan_overscan(self, chip, filt, tel, pointing, catalog, obs_param):
    self.chip_output.Log_info("Apply pre/over-scan")
    chip.img = chip_utils.AddPreScan(GSImage=chip.img,
                                    pre1=chip.prescan_x,
                                    pre2=chip.prescan_y,
                                    over1=chip.overscan_x,
                                    over2=chip.overscan_y)
Wei Chengliang's avatar
Wei Chengliang committed
15

Wei Chengliang's avatar
Wei Chengliang committed
16
    if obs_param["add_dark"] == True:
Wei Chengliang's avatar
Wei Chengliang committed
17
18
        ny = int(chip.npix_y/2)
        base_dark = (ny-1)*(chip.readout_time/ny)*chip.dark_noise
Wei Chengliang's avatar
Wei Chengliang committed
19
        chip.img.array[(chip.prescan_y+ny):-(chip.prescan_y+ny),:] = base_dark
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
    return chip, filt, tel, pointing

def add_readout_noise(self, chip, filt, tel, pointing, catalog, obs_param):
    seed = int(self.overall_config["random_seeds"]["seed_readout"]) + pointing.id*30 + chip.chipID
    rng_readout = galsim.BaseDeviate(seed)
    readout_noise = galsim.GaussianNoise(rng=rng_readout, sigma=chip.read_noise)
    chip.img.addNoise(readout_noise)
    return chip, filt, tel, pointing

def apply_gain(self, chip, filt, tel, pointing, catalog, obs_param):
    self.chip_output.Log_info("  Applying Gain")
    if obs_param["gain_16channel"] == True:
        chip.img, chip.gain_channel = Effects.ApplyGainNonUniform16(chip.img, 
                                                                   gain=chip.gain, 
                                                                   nsecy = chip.nsecy, 
                                                                   nsecx=chip.nsecx, 
                                                                   seed=self.overall_config["random_seeds"]["seed_gainNonUniform"]+chip.chipID)
    elif obs_param["gain_16channel"] == False:
        chip.img /= chip.gain
    return chip, filt, tel, pointing

def quantization_and_output(self, chip, filt, tel, pointing, catalog, obs_param):

43
44
    if not hasattr(self, 'h_ext'):
        _, _ = self.prepare_headers(chip=chip, pointing=pointing)
45
        self.updateHeaderInfo(header_flag='ext', keys = ['SHTSTAT','SHTOPEN0','SHTOPEN1','SHTCLOS0','SHTCLOS1'], values = [False,'','','',''])
46
47
48
49
50
51
    
    gains1 = list(chip.gain_channel[0:8])
    gains2 = list(chip.gain_channel[8:])
    gains2.reverse()
    gains = np.append(gains1,gains2)
    self.updateHeaderInfo(header_flag='ext', keys = ['GAIN01','GAIN02','GAIN03','GAIN04','GAIN05','GAIN06','GAIN07','GAIN08','GAIN09','GAIN10','GAIN11','GAIN12','GAIN13','GAIN14','GAIN15','GAIN16'], values = gains)
52

53
54
55
56
57
58
59
60
61
62
    if obs_param["format_output"] == True:
        self.chip_output.Log_info("  Apply 1*16 format")
        chip.img = chip_utils.formatOutput(GSImage=chip.img)
        chip.nsecy = 1
        chip.nsecx = 16

    chip.img.array[chip.img.array > 65535] = 65535
    chip.img.replaceNegative(replace_value=0)
    chip.img.quantize()
    chip.img = galsim.Image(chip.img.array, dtype=np.uint16)
63
64
65
66
67
68
69
70
    fname = os.path.join(self.chip_output.subdir, self.h_prim['FILENAME'] + '.fits')

    f_name_size = 68
    if(len(self.h_prim['FILENAME'])>f_name_size):
        self.updateHeaderInfo(header_flag='prim', keys = ['FILENAME'], values = [self.h_prim['FILENAME'][0:f_name_size]])
        
    
    
71
72
73
74
    hdu1 = fits.PrimaryHDU(header=self.h_prim)
    hdu1.add_checksum()
    hdu1.header.comments['CHECKSUM'] = 'HDU checksum'
    hdu1.header.comments['DATASUM'] = 'data unit checksum'
75
    self.updateHeaderInfo(header_flag='ext', keys = ['DATASECT'], values = [str(chip.img.array.shape[1])+'x'+str(chip.img.array.shape[0])])
76
77
78
79
80
81
82
83
    hdu2 = fits.ImageHDU(chip.img.array, header=self.h_ext)
    hdu2.add_checksum()
    hdu2.header.comments['XTENSION'] = 'extension type'
    hdu2.header.comments['CHECKSUM'] = 'HDU checksum'
    hdu2.header.comments['DATASUM'] = 'data unit checksum'
    hdu2.header.comments["XTENSION"] = "image extension"
    hdu1 = fits.HDUList([hdu1, hdu2])
    hdu1.writeto(fname, output_verify='ignore', overwrite=True)
84

85
    return chip, filt, tel, pointing