add_sky_background.py 5.34 KB
Newer Older
1
2
3
import numpy as np
import galsim
from ObservationSim.Straylight import calculateSkyMap_split_g
Zhang Xin's avatar
Zhang Xin committed
4
from ObservationSim.Instrument import FilterParam
5
6
7
8
9
10
11
12

def add_sky_background_sci(self, chip, filt, tel, pointing, catalog, obs_param):
    flat_normal = np.ones_like(chip.img.array)
    if obs_param["flat_fielding"] == True:
        flat_normal = flat_normal * chip.flat_img.array / np.mean(chip.flat_img.array)
    if obs_param["shutter_effect"] == True:
        flat_normal = flat_normal * chip.shutter_img
        flat_normal = np.array(flat_normal, dtype='float32')
13
14
15
        self.updateHeaderInfo(header_flag='ext', keys = ['SHTSTAT'], values = [True])
    else:
        self.updateHeaderInfo(header_flag='ext', keys = ['SHTSTAT','SHTOPEN0','SHTOPEN1','SHTCLOS0','SHTCLOS1'], values = [False,'','','',''])
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
    
    if obs_param["enable_straylight_model"]:
        # Filter.sky_background, Filter.zodical_spec will be updated
        filt.setFilterStrayLightPixel(
            jtime = pointing.jdt,
            sat_pos = np.array([pointing.sat_x, pointing.sat_y, pointing.sat_z]),
            pointing_radec = np.array([pointing.ra,pointing.dec]),
            sun_pos = np.array([pointing.sun_x, pointing.sun_y, pointing.sun_z]))
        self.chip_output.Log_info("================================================")
        self.chip_output.Log_info("sky background + stray light pixel flux value: %.5f"%(filt.sky_background))
    
    if chip.survey_type == "photometric":
        sky_map = filt.getSkyNoise(exptime = obs_param["exptime"])
        sky_map = sky_map * np.ones_like(chip.img.array) * flat_normal
        sky_map = galsim.Image(array=sky_map)
    else:
        # chip.loadSLSFLATCUBE(flat_fn='flat_cube.fits')
        sky_map = calculateSkyMap_split_g(
                skyMap=flat_normal,
                blueLimit=filt.blue_limit,
                redLimit=filt.red_limit,
                conf=chip.sls_conf,
                pixelSize=chip.pix_scale,
                isAlongY=0,
                flat_cube=chip.flat_cube, 
                zoldial_spec = filt.zodical_spec)
        sky_map = (sky_map + filt.sky_background)*obs_param["exptime"]
    
    # sky_map = sky_map * tel.pupil_area * obs_param["exptime"]
    chip.img += sky_map
    return chip, filt, tel, pointing

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

    if not hasattr(self, 'h_ext'):
        _, _ = self.prepare_headers(chip=chip, pointing=pointing)
    chip_wcs = galsim.FitsWCS(header = self.h_ext)

    expTime = obs_param["exptime"]
    skyback_level = obs_param["flat_level"]

    filter_param = FilterParam()
    sky_level_filt = obs_param["flat_level_filt"]
    norm_scaler = skyback_level/expTime / filter_param.param[sky_level_filt][5]

    flat_normal = np.ones_like(chip.img.array)
    if obs_param["flat_fielding"] == True:
        flat_normal = flat_normal * chip.flat_img.array / np.mean(chip.flat_img.array)
    if obs_param["shutter_effect"] == True:
        flat_normal = flat_normal * chip.shutter_img
        flat_normal = np.array(flat_normal, dtype='float32')
67
68
69
        if self.overall_config["output_setting"]["shutter_output"] == True:    # output 16-bit shutter effect image with pixel value <=65535
            shutt_gsimg = galsim.ImageUS(chip.shutter_img*6E4)
            shutt_gsimg.write("%s/ShutterEffect_%s_1.fits" % (self.chip_output.subdir, chip.chipID))
70
71
72
        self.updateHeaderInfo(header_flag='ext', keys = ['SHTSTAT'], values = [True])
    else:
        self.updateHeaderInfo(header_flag='ext', keys = ['SHTSTAT','SHTOPEN0','SHTOPEN1','SHTCLOS0','SHTCLOS1'], values = [False,'','','',''])
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
101
102
103
104
105
106
107
108
109
110
111
    

    if chip.survey_type == "photometric":
        sky_map = flat_normal * np.ones_like(chip.img.array) * norm_scaler * filter_param.param[chip.filter_type][5] / tel.pupil_area * expTime
    elif chip.survey_type == "spectroscopic":
        # flat_normal = np.ones_like(chip.img.array)
        if obs_param["flat_fielding"] == True:
            
            flat_normal = flat_normal * chip.flat_img.array / np.mean(chip.flat_img.array)
        if obs_param["shutter_effect"] == True:
            
            flat_normal = flat_normal * chip.shutter_img
            flat_normal = np.array(flat_normal, dtype='float32')
        sky_map = calculateSkyMap_split_g(
            skyMap=flat_normal,
            blueLimit=filt.blue_limit,
            redLimit=filt.red_limit,
            conf=chip.sls_conf,
            pixelSize=chip.pix_scale,
            isAlongY=0,
            flat_cube=chip.flat_cube)
        sky_map = sky_map * norm_scaler * expTime
    
    chip.img += sky_map
    return chip, filt, tel, pointing

def add_sky_background(self, chip, filt, tel, pointing, catalog, obs_param):
    if not hasattr(self, 'h_ext'):
        _, _ = self.prepare_headers(chip=chip, pointing=pointing)
    chip_wcs = galsim.FitsWCS(header = self.h_ext)

    if "flat_level" not in obs_param or "flat_level_filt" not in obs_param:
        chip, filt, tel, pointing = self.add_sky_background_sci(chip, filt, tel, pointing, catalog, obs_param)
    else:
        if obs_param.get('flat_level') is None or obs_param.get('flat_level_filt')is None:
            chip, filt, tel, pointing = self.add_sky_background_sci(chip, filt, tel, pointing, catalog, obs_param)
        else:
            chip, filt, tel, pointing = self.add_sky_flat_calibration(chip, filt, tel, pointing, catalog, obs_param)
    return chip, filt, tel, pointing