Commit fe70f366 authored by Fang Yuedong's avatar Fang Yuedong
Browse files

add unittest for focal plane

parent fcfee677
...@@ -280,774 +280,774 @@ class Chip(FocalPlane): ...@@ -280,774 +280,774 @@ class Chip(FocalPlane):
noise = self.dark_noise * exptime + self.read_noise**2 noise = self.dark_noise * exptime + self.read_noise**2
return noise return noise
def addEffects(self, config, img, chip_output, filt, ra_cen, dec_cen, img_rot, exptime=150., pointing_ID=0, timestamp_obs=1621915200, pointing_type='SCI', sky_map=None, post_flash_map=None, tel=None, logger=None): # def addEffects(self, config, img, chip_output, filt, ra_cen, dec_cen, img_rot, exptime=150., pointing_ID=0, timestamp_obs=1621915200, pointing_type='SCI', sky_map=None, post_flash_map=None, tel=None, logger=None):
# Set random seeds # # Set random seeds
SeedGainNonuni = int(config["random_seeds"]["seed_gainNonUniform"]) # SeedGainNonuni = int(config["random_seeds"]["seed_gainNonUniform"])
SeedBiasNonuni = int(config["random_seeds"]["seed_biasNonUniform"]) # SeedBiasNonuni = int(config["random_seeds"]["seed_biasNonUniform"])
SeedRnNonuni = int(config["random_seeds"]["seed_rnNonUniform"]) # SeedRnNonuni = int(config["random_seeds"]["seed_rnNonUniform"])
SeedBadColumns = int(config["random_seeds"]["seed_badcolumns"]) # SeedBadColumns = int(config["random_seeds"]["seed_badcolumns"])
SeedDefective = int(config["random_seeds"]["seed_defective"]) # SeedDefective = int(config["random_seeds"]["seed_defective"])
SeedCosmicRay = int(config["random_seeds"]["seed_CR"]) # SeedCosmicRay = int(config["random_seeds"]["seed_CR"])
fullwell = int(self.full_well) # fullwell = int(self.full_well)
if config["ins_effects"]["add_hotpixels"] == True: # if config["ins_effects"]["add_hotpixels"] == True:
BoolHotPix = True # BoolHotPix = True
else: # else:
BoolHotPix = False # BoolHotPix = False
if config["ins_effects"]["add_deadpixels"] == True: # if config["ins_effects"]["add_deadpixels"] == True:
BoolDeadPix = True # BoolDeadPix = True
else: # else:
BoolDeadPix = False # BoolDeadPix = False
self.logger = logger # self.logger = logger
# Get Poisson noise generator # # Get Poisson noise generator
rng_poisson, poisson_noise = chip_utils.get_poisson( # rng_poisson, poisson_noise = chip_utils.get_poisson(
seed=int(config["random_seeds"]["seed_poisson"]) + pointing_ID*30 + self.chipID, sky_level=0.) # seed=int(config["random_seeds"]["seed_poisson"]) + pointing_ID*30 + self.chipID, sky_level=0.)
# Add sky background # # Add sky background
if config["ins_effects"]["add_back"] == True: # if config["ins_effects"]["add_back"] == True:
img, sky_map = chip_utils.add_sky_background( # img, sky_map = chip_utils.add_sky_background(
img=img, filt=filt, exptime=exptime, sky_map=sky_map, tel=tel) # img=img, filt=filt, exptime=exptime, sky_map=sky_map, tel=tel)
del sky_map # del sky_map
# Apply flat-field large scale structure for one chip # # Apply flat-field large scale structure for one chip
if config["ins_effects"]["flat_fielding"] == True: # if config["ins_effects"]["flat_fielding"] == True:
chip_utils.log_info( # chip_utils.log_info(
msg=" Creating and applying Flat-Fielding", logger=self.logger) # msg=" Creating and applying Flat-Fielding", logger=self.logger)
chip_utils.log_info(msg=str(img.bounds), logger=self.logger) # chip_utils.log_info(msg=str(img.bounds), logger=self.logger)
flat_img, flat_normal = chip_utils.get_flat( # flat_img, flat_normal = chip_utils.get_flat(
img=img, seed=int(config["random_seeds"]["seed_flat"])) # img=img, seed=int(config["random_seeds"]["seed_flat"]))
if self.survey_type == "photometric": # if self.survey_type == "photometric":
img *= flat_normal # img *= flat_normal
del flat_normal # del flat_normal
if config["output_setting"]["flat_output"] == False: # if config["output_setting"]["flat_output"] == False:
del flat_img # del flat_img
if post_flash_map is not None: # if post_flash_map is not None:
img = img + post_flash_map # img = img + post_flash_map
# Apply Shutter-effect for one chip # # Apply Shutter-effect for one chip
if config["ins_effects"]["shutter_effect"] == True: # if config["ins_effects"]["shutter_effect"] == True:
chip_utils.log_info( # chip_utils.log_info(
msg=" Apply shutter effect", logger=self.logger) # msg=" Apply shutter effect", logger=self.logger)
# shutter effect normalized image for this chip # # shutter effect normalized image for this chip
shuttimg = effects.ShutterEffectArr( # shuttimg = effects.ShutterEffectArr(
img, t_shutter=1.3, dist_bearing=735, dt=1E-3) # img, t_shutter=1.3, dist_bearing=735, dt=1E-3)
if self.survey_type == "photometric": # if self.survey_type == "photometric":
img *= shuttimg # img *= shuttimg
# output 16-bit shutter effect image with pixel value <=65535 # # output 16-bit shutter effect image with pixel value <=65535
if config["output_setting"]["shutter_output"] == True: # if config["output_setting"]["shutter_output"] == True:
shutt_gsimg = galsim.ImageUS(shuttimg*6E4) # shutt_gsimg = galsim.ImageUS(shuttimg*6E4)
shutt_gsimg.write("%s/ShutterEffect_%s_1.fits" % # shutt_gsimg.write("%s/ShutterEffect_%s_1.fits" %
(chip_output.subdir, self.chipID)) # (chip_output.subdir, self.chipID))
del shutt_gsimg # del shutt_gsimg
del shuttimg # del shuttimg
# # Add Poisson noise to the resulting images # # # Add Poisson noise to the resulting images
# # (NOTE): this can only applied to the slitless image # # # (NOTE): this can only applied to the slitless image
# # since it dose not use photon shooting to draw stamps # # # since it dose not use photon shooting to draw stamps
# if self.survey_type == "spectroscopic": # # if self.survey_type == "spectroscopic":
# img.addNoise(poisson_noise) # # img.addNoise(poisson_noise)
# Add cosmic-rays # # Add cosmic-rays
if config["ins_effects"]["cosmic_ray"] == True and pointing_type == 'SCI': # if config["ins_effects"]["cosmic_ray"] == True and pointing_type == 'SCI':
chip_utils.log_info(msg=" Adding Cosmic-Ray", logger=self.logger) # chip_utils.log_info(msg=" Adding Cosmic-Ray", logger=self.logger)
img, crmap_gsimg, cr_event_num = chip_utils.add_cosmic_rays(img=img, chip=self, exptime=exptime, # img, crmap_gsimg, cr_event_num = chip_utils.add_cosmic_rays(img=img, chip=self, exptime=exptime,
seed=SeedCosmicRay+pointing_ID*30+self.chipID) # seed=SeedCosmicRay+pointing_ID*30+self.chipID)
chip_utils.outputCal( # chip_utils.outputCal(
chip=self, # chip=self,
img=crmap_gsimg, # img=crmap_gsimg,
ra_cen=ra_cen, # ra_cen=ra_cen,
dec_cen=dec_cen, # dec_cen=dec_cen,
img_rot=img_rot, # img_rot=img_rot,
im_type='CRS', # im_type='CRS',
pointing_ID=pointing_ID, # pointing_ID=pointing_ID,
output_dir=chip_output.subdir, # output_dir=chip_output.subdir,
exptime=exptime, # exptime=exptime,
project_cycle=config["project_cycle"], # project_cycle=config["project_cycle"],
run_counter=config["run_counter"], # run_counter=config["run_counter"],
timestamp=timestamp_obs) # timestamp=timestamp_obs)
del crmap_gsimg # del crmap_gsimg
# Apply PRNU effect and output PRNU flat file: # # Apply PRNU effect and output PRNU flat file:
if config["ins_effects"]["prnu_effect"] == True: # if config["ins_effects"]["prnu_effect"] == True:
chip_utils.log_info( # chip_utils.log_info(
msg=" Applying PRNU effect", logger=self.logger) # msg=" Applying PRNU effect", logger=self.logger)
img, prnu_img = chip_utils.add_PRNU(img=img, chip=self, # img, prnu_img = chip_utils.add_PRNU(img=img, chip=self,
seed=int(config["random_seeds"]["seed_prnu"]+self.chipID)) # seed=int(config["random_seeds"]["seed_prnu"]+self.chipID))
if config["output_setting"]["prnu_output"] == True: # if config["output_setting"]["prnu_output"] == True:
prnu_img.write("%s/FlatImg_PRNU_%s.fits" % # prnu_img.write("%s/FlatImg_PRNU_%s.fits" %
(chip_output.subdir, self.chipID)) # (chip_output.subdir, self.chipID))
if config["output_setting"]["flat_output"] == False: # if config["output_setting"]["flat_output"] == False:
del prnu_img # del prnu_img
# # Add dark current # # # Add dark current
# # if config["ins_effects"]["add_dark"] == True:
# # dark_noise = galsim.DeviateNoise(galsim.PoissonDeviate(rng_poisson, self.dark_noise*(exptime+0.5*self.readout_time)))
# # img.addNoise(dark_noise)
# # Add dark current & Poisson noise
# InputDark = False
# if config["ins_effects"]["add_dark"] == True: # if config["ins_effects"]["add_dark"] == True:
# dark_noise = galsim.DeviateNoise(galsim.PoissonDeviate(rng_poisson, self.dark_noise*(exptime+0.5*self.readout_time))) # if InputDark:
# img.addNoise(dark_noise) # img = chip_utils.add_inputdark(
# img=img, chip=self, exptime=exptime)
# Add dark current & Poisson noise # else:
InputDark = False # img, _ = chip_utils.add_poisson(
if config["ins_effects"]["add_dark"] == True: # img=img, chip=self, exptime=exptime, poisson_noise=poisson_noise)
if InputDark: # else:
img = chip_utils.add_inputdark( # img, _ = chip_utils.add_poisson(
img=img, chip=self, exptime=exptime) # img=img, chip=self, exptime=exptime, poisson_noise=poisson_noise, dark_noise=0.)
else:
img, _ = chip_utils.add_poisson( # # Add diffusion & brighter-fatter effects
img=img, chip=self, exptime=exptime, poisson_noise=poisson_noise) # if config["ins_effects"]["bright_fatter"] == True:
else: # img = chip_utils.add_brighter_fatter(img=img)
img, _ = chip_utils.add_poisson(
img=img, chip=self, exptime=exptime, poisson_noise=poisson_noise, dark_noise=0.) # # Add Hot Pixels or/and Dead Pixels
# rgbadpix = Generator(PCG64(int(SeedDefective+self.chipID)))
# Add diffusion & brighter-fatter effects # badfraction = 5E-5*(rgbadpix.random()*0.5+0.7)
if config["ins_effects"]["bright_fatter"] == True: # img = effects.DefectivePixels(img, IfHotPix=BoolHotPix, IfDeadPix=BoolDeadPix,
img = chip_utils.add_brighter_fatter(img=img) # fraction=badfraction, seed=SeedDefective+self.chipID, biaslevel=0)
# Add Hot Pixels or/and Dead Pixels # # Apply Bad lines
rgbadpix = Generator(PCG64(int(SeedDefective+self.chipID))) # if config["ins_effects"]["add_badcolumns"] == True:
badfraction = 5E-5*(rgbadpix.random()*0.5+0.7) # img = effects.BadColumns(
img = effects.DefectivePixels(img, IfHotPix=BoolHotPix, IfDeadPix=BoolDeadPix, # img, seed=SeedBadColumns, chipid=self.chipID, logger=self.logger)
fraction=badfraction, seed=SeedDefective+self.chipID, biaslevel=0)
# # Apply Nonlinearity on the chip image
# Apply Bad lines # if config["ins_effects"]["non_linear"] == True:
if config["ins_effects"]["add_badcolumns"] == True: # chip_utils.log_info(
img = effects.BadColumns( # msg=" Applying Non-Linearity on the chip image", logger=self.logger)
img, seed=SeedBadColumns, chipid=self.chipID, logger=self.logger) # img = effects.NonLinearity(GSImage=img, beta1=5.e-7, beta2=0)
# Apply Nonlinearity on the chip image # # Apply CCD Saturation & Blooming
if config["ins_effects"]["non_linear"] == True: # if config["ins_effects"]["saturbloom"] == True:
chip_utils.log_info( # chip_utils.log_info(
msg=" Applying Non-Linearity on the chip image", logger=self.logger) # msg=" Applying CCD Saturation & Blooming", logger=self.logger)
img = effects.NonLinearity(GSImage=img, beta1=5.e-7, beta2=0) # img = effects.SaturBloom(
# GSImage=img, nsect_x=1, nsect_y=1, fullwell=fullwell)
# Apply CCD Saturation & Blooming
if config["ins_effects"]["saturbloom"] == True: # # Apply CTE Effect
chip_utils.log_info( # # if config["ins_effects"]["cte_trail"] == True:
msg=" Applying CCD Saturation & Blooming", logger=self.logger) # # chip_utils.log_info(msg=" Apply CTE Effect", logger=self.logger)
img = effects.SaturBloom( # # img = effects.CTE_Effect(GSImage=img, threshold=27)
GSImage=img, nsect_x=1, nsect_y=1, fullwell=fullwell)
# pre1 = self.prescan_x # 27
# Apply CTE Effect # over1 = self.overscan_x # 71
# pre2 = self.prescan_y # 0 #4
# over2 = self.overscan_y # 84 #80
# if config["ins_effects"]["cte_trail"] == True: # if config["ins_effects"]["cte_trail"] == True:
# chip_utils.log_info(msg=" Apply CTE Effect", logger=self.logger) # chip_utils.log_info(msg=" Apply CTE Effect", logger=self.logger)
# img = effects.CTE_Effect(GSImage=img, threshold=27) # # img = effects.CTE_Effect(GSImage=img, threshold=27)
# # CTI_modeling
pre1 = self.prescan_x # 27 # # 2*8 -> 1*16 img-layout
over1 = self.overscan_x # 71 # img = chip_utils.formatOutput(GSImage=img)
pre2 = self.prescan_y # 0 #4 # self.nsecy = 1
over2 = self.overscan_y # 84 #80 # self.nsecx = 16
if config["ins_effects"]["cte_trail"] == True: # img_arr = img.array
chip_utils.log_info(msg=" Apply CTE Effect", logger=self.logger) # ny, nx = img_arr.shape
# img = effects.CTE_Effect(GSImage=img, threshold=27) # dx = int(nx/self.nsecx)
# CTI_modeling # dy = int(ny/self.nsecy)
# 2*8 -> 1*16 img-layout # newimg = galsim.Image(nx, int(ny+over2), init_value=0)
img = chip_utils.formatOutput(GSImage=img) # for ichannel in range(16):
self.nsecy = 1 # print('\n***add CTI effects: pointing-{:} chip-{:} channel-{:}***'.format(
self.nsecx = 16 # pointing_ID, self.chipID, ichannel+1))
# # nx,ny,noverscan,nsp,nmax = 4608,4616,84,3,10
img_arr = img.array # noverscan, nsp, nmax = over2, 3, 10
ny, nx = img_arr.shape # beta, w, c = 0.478, 84700, 0
dx = int(nx/self.nsecx) # t = np.array([0.74, 7.7, 37], dtype=np.float32)
dy = int(ny/self.nsecy) # rho_trap = np.array([0.6, 1.6, 1.4], dtype=np.float32)
newimg = galsim.Image(nx, int(ny+over2), init_value=0) # trap_seeds = np.array(
for ichannel in range(16): # [0, 1000, 10000], dtype=np.int32) + ichannel + self.chipID*16
print('\n***add CTI effects: pointing-{:} chip-{:} channel-{:}***'.format( # release_seed = 50 + ichannel + pointing_ID*30 + self.chipID*16
pointing_ID, self.chipID, ichannel+1)) # newimg.array[:, 0+ichannel*dx:dx+ichannel*dx] = CTI_sim(
# nx,ny,noverscan,nsp,nmax = 4608,4616,84,3,10 # img_arr[:, 0+ichannel*dx:dx+ichannel*dx], dx, dy, noverscan, nsp, nmax, beta, w, c, t, rho_trap, trap_seeds, release_seed)
noverscan, nsp, nmax = over2, 3, 10 # newimg.wcs = img.wcs
beta, w, c = 0.478, 84700, 0 # del img
t = np.array([0.74, 7.7, 37], dtype=np.float32) # img = newimg
rho_trap = np.array([0.6, 1.6, 1.4], dtype=np.float32)
trap_seeds = np.array( # # 1*16 -> 2*8 img-layout
[0, 1000, 10000], dtype=np.int32) + ichannel + self.chipID*16 # img = chip_utils.formatRevert(GSImage=img)
release_seed = 50 + ichannel + pointing_ID*30 + self.chipID*16 # self.nsecy = 2
newimg.array[:, 0+ichannel*dx:dx+ichannel*dx] = CTI_sim( # self.nsecx = 8
img_arr[:, 0+ichannel*dx:dx+ichannel*dx], dx, dy, noverscan, nsp, nmax, beta, w, c, t, rho_trap, trap_seeds, release_seed)
newimg.wcs = img.wcs # # prescan & overscan
del img # if config["ins_effects"]["add_prescan"] == True:
img = newimg # chip_utils.log_info(
# msg=" Apply pre/over-scan", logger=self.logger)
# 1*16 -> 2*8 img-layout # if config["ins_effects"]["cte_trail"] == False:
img = chip_utils.formatRevert(GSImage=img) # img = chip_utils.AddPreScan(
self.nsecy = 2 # GSImage=img, pre1=pre1, pre2=pre2, over1=over1, over2=over2)
self.nsecx = 8 # if config["ins_effects"]["cte_trail"] == True:
# img = chip_utils.AddPreScan(
# prescan & overscan # GSImage=img, pre1=pre1, pre2=pre2, over1=over1, over2=0)
if config["ins_effects"]["add_prescan"] == True:
chip_utils.log_info( # # 1*16 output
msg=" Apply pre/over-scan", logger=self.logger) # if config["ins_effects"]["format_output"] == True:
if config["ins_effects"]["cte_trail"] == False: # chip_utils.log_info(msg=" Apply 1*16 format", logger=self.logger)
img = chip_utils.AddPreScan( # img = chip_utils.formatOutput(GSImage=img)
GSImage=img, pre1=pre1, pre2=pre2, over1=over1, over2=over2) # self.nsecy = 1
if config["ins_effects"]["cte_trail"] == True: # self.nsecx = 16
img = chip_utils.AddPreScan(
GSImage=img, pre1=pre1, pre2=pre2, over1=over1, over2=0) # # Add Bias level
# if config["ins_effects"]["add_bias"] == True:
# 1*16 output # chip_utils.log_info(
if config["ins_effects"]["format_output"] == True: # msg=" Adding Bias level and 16-channel non-uniformity", logger=self.logger)
chip_utils.log_info(msg=" Apply 1*16 format", logger=self.logger) # if config["ins_effects"]["bias_16channel"] == True:
img = chip_utils.formatOutput(GSImage=img) # img = effects.AddBiasNonUniform16(img,
self.nsecy = 1 # bias_level=float(
self.nsecx = 16 # self.bias_level),
# nsecy=self.nsecy, nsecx=self.nsecx,
# Add Bias level
if config["ins_effects"]["add_bias"] == True:
chip_utils.log_info(
msg=" Adding Bias level and 16-channel non-uniformity", logger=self.logger)
if config["ins_effects"]["bias_16channel"] == True:
img = effects.AddBiasNonUniform16(img,
bias_level=float(
self.bias_level),
nsecy=self.nsecy, nsecx=self.nsecx,
seed=SeedBiasNonuni+self.chipID,
logger=self.logger)
elif config["ins_effects"]["bias_16channel"] == False:
img += self.bias_level
# Add Read-out Noise
if config["ins_effects"]["add_readout"] == True:
seed = int(config["random_seeds"]["seed_readout"]
) + pointing_ID*30 + self.chipID
rng_readout = galsim.BaseDeviate(seed)
readout_noise = galsim.GaussianNoise(
rng=rng_readout, sigma=self.read_noise)
img.addNoise(readout_noise)
# Apply Gain & Quantization
chip_utils.log_info(
msg=" Applying Gain (and 16 channel non-uniformity) & Quantization", logger=self.logger)
if config["ins_effects"]["gain_16channel"] == True:
img, self.gain_channel = effects.ApplyGainNonUniform16(
img, gain=self.gain,
nsecy=self.nsecy, nsecx=self.nsecx,
seed=SeedGainNonuni+self.chipID,
logger=self.logger)
elif config["ins_effects"]["gain_16channel"] == False:
img /= self.gain
img.array[img.array > 65535] = 65535
img.replaceNegative(replace_value=0)
img.quantize()
######################################################################################
# Output images for calibration pointing
######################################################################################
# Bias output
if config["ins_effects"]["add_bias"] == True and config["output_setting"]["bias_output"] == True and pointing_type == 'CAL':
if self.logger is not None:
self.logger.info(" Output N frame Bias files")
else:
print(" Output N frame Bias files", flush=True)
NBias = int(config["output_setting"]["NBias"])
for i in range(NBias):
# BiasCombImg, BiasTag = effects.MakeBiasNcomb(
# self.npix_x, self.npix_y,
# bias_level=float(self.bias_level),
# ncombine=1, read_noise=self.read_noise, gain=1,
# seed=SeedBiasNonuni+self.chipID, # seed=SeedBiasNonuni+self.chipID,
# logger=self.logger) # logger=self.logger)
BiasCombImg = galsim.Image( # elif config["ins_effects"]["bias_16channel"] == False:
self.npix_x, self.npix_y, init_value=0) # img += self.bias_level
if config["ins_effects"]["add_bias"] == True:
biaslevel = self.bias_level # # Add Read-out Noise
overscan = biaslevel-2 # if config["ins_effects"]["add_readout"] == True:
elif config["ins_effects"]["add_bias"] == False: # seed = int(config["random_seeds"]["seed_readout"]
biaslevel = 0 # ) + pointing_ID*30 + self.chipID
overscan = 0 # rng_readout = galsim.BaseDeviate(seed)
# readout_noise = galsim.GaussianNoise(
# Readout noise for Biases is not generated with random seeds. So readout noise for bias images can't be reproduced. # rng=rng_readout, sigma=self.read_noise)
if config["ins_effects"]["cosmic_ray"] == True: # img.addNoise(readout_noise)
if config["ins_effects"]["cray_differ"] == True:
cr_map, cr_event_num = effects.produceCR_Map( # # Apply Gain & Quantization
xLen=self.npix_x, yLen=self.npix_y, # chip_utils.log_info(
exTime=0.01, # msg=" Applying Gain (and 16 channel non-uniformity) & Quantization", logger=self.logger)
cr_pixelRatio=0.003 * # if config["ins_effects"]["gain_16channel"] == True:
(0.01+0.5*self.readout_time)/150., # img, self.gain_channel = effects.ApplyGainNonUniform16(
gain=self.gain, # img, gain=self.gain,
attachedSizes=self.attachedSizes, # nsecy=self.nsecy, nsecx=self.nsecx,
seed=SeedCosmicRay+pointing_ID*30+self.chipID+1) # seed=SeedGainNonuni+self.chipID,
# seed: obj-imaging:+0; bias:+1; dark:+2; flat:+3; # logger=self.logger)
BiasCombImg += cr_map # elif config["ins_effects"]["gain_16channel"] == False:
del cr_map # img /= self.gain
# Apply Bad lines # img.array[img.array > 65535] = 65535
if config["ins_effects"]["add_badcolumns"] == True: # img.replaceNegative(replace_value=0)
BiasCombImg = effects.BadColumns( # img.quantize()
BiasCombImg-float(self.bias_level)+5, seed=SeedBadColumns, chipid=self.chipID, logger=self.logger) + float(self.bias_level)-5
# ######################################################################################
# Non-Linearity for Bias # # Output images for calibration pointing
if config["ins_effects"]["non_linear"] == True: # ######################################################################################
if self.logger is not None: # # Bias output
self.logger.info( # if config["ins_effects"]["add_bias"] == True and config["output_setting"]["bias_output"] == True and pointing_type == 'CAL':
" Applying Non-Linearity on the Bias image") # if self.logger is not None:
else: # self.logger.info(" Output N frame Bias files")
print( # else:
" Applying Non-Linearity on the Bias image", flush=True) # print(" Output N frame Bias files", flush=True)
BiasCombImg = effects.NonLinearity( # NBias = int(config["output_setting"]["NBias"])
GSImage=BiasCombImg, beta1=5.e-7, beta2=0) # for i in range(NBias):
# # BiasCombImg, BiasTag = effects.MakeBiasNcomb(
# START # # self.npix_x, self.npix_y,
pre1 = self.prescan_x # 27 # # bias_level=float(self.bias_level),
over1 = self.overscan_x # 71 # # ncombine=1, read_noise=self.read_noise, gain=1,
pre2 = self.prescan_y # 0 #4 # # seed=SeedBiasNonuni+self.chipID,
over2 = self.overscan_y # 84 #80 # # logger=self.logger)
# BiasCombImg = galsim.Image(
# prescan & overscan # self.npix_x, self.npix_y, init_value=0)
if config["ins_effects"]["add_prescan"] == True: # if config["ins_effects"]["add_bias"] == True:
chip_utils.log_info( # biaslevel = self.bias_level
msg=" Apply pre/over-scan", logger=self.logger) # overscan = biaslevel-2
BiasCombImg = chip_utils.AddPreScan( # elif config["ins_effects"]["add_bias"] == False:
GSImage=BiasCombImg, pre1=pre1, pre2=pre2, over1=over1, over2=over2) # biaslevel = 0
# overscan = 0
# 1*16 output
if config["ins_effects"]["format_output"] == True: # # Readout noise for Biases is not generated with random seeds. So readout noise for bias images can't be reproduced.
chip_utils.log_info( # if config["ins_effects"]["cosmic_ray"] == True:
msg=" Apply 1*16 format", logger=self.logger) # if config["ins_effects"]["cray_differ"] == True:
BiasCombImg = chip_utils.formatOutput(GSImage=BiasCombImg) # cr_map, cr_event_num = effects.produceCR_Map(
self.nsecy = 1 # xLen=self.npix_x, yLen=self.npix_y,
self.nsecx = 16 # exTime=0.01,
# END # cr_pixelRatio=0.003 *
# (0.01+0.5*self.readout_time)/150.,
# Add Bias level # gain=self.gain,
if config["ins_effects"]["add_bias"] == True: # attachedSizes=self.attachedSizes,
if self.logger is not None: # seed=SeedCosmicRay+pointing_ID*30+self.chipID+1)
self.logger.info( # # seed: obj-imaging:+0; bias:+1; dark:+2; flat:+3;
" Adding Bias level and 16-channel non-uniformity") # BiasCombImg += cr_map
else: # del cr_map
print(" Adding Bias level and 16-channel non-uniformity")
BiasCombImg = effects.AddBiasNonUniform16(BiasCombImg, # # Apply Bad lines
bias_level=biaslevel, # if config["ins_effects"]["add_badcolumns"] == True:
nsecy=self.nsecy, nsecx=self.nsecx, # BiasCombImg = effects.BadColumns(
seed=SeedBiasNonuni+self.chipID, # BiasCombImg-float(self.bias_level)+5, seed=SeedBadColumns, chipid=self.chipID, logger=self.logger) + float(self.bias_level)-5
logger=self.logger)
rng = galsim.UniformDeviate() # # Non-Linearity for Bias
ncombine = 1 # if config["ins_effects"]["non_linear"] == True:
NoiseBias = galsim.GaussianNoise( # if self.logger is not None:
rng=rng, sigma=self.read_noise*ncombine**0.5) # self.logger.info(
BiasCombImg.addNoise(NoiseBias) # " Applying Non-Linearity on the Bias image")
# else:
BiasCombImg, self.gain_channel = effects.ApplyGainNonUniform16(BiasCombImg, gain=self.gain, # print(
nsecy=self.nsecy, nsecx=self.nsecx, # " Applying Non-Linearity on the Bias image", flush=True)
seed=SeedGainNonuni+self.chipID, # BiasCombImg = effects.NonLinearity(
logger=self.logger) # GSImage=BiasCombImg, beta1=5.e-7, beta2=0)
# BiasCombImg = effects.AddOverscan(
# BiasCombImg, # # START
# overscan=float(config["ins_effects"]["bias_level"])-2, gain=self.gain, # pre1 = self.prescan_x # 27
# widthl=27, widthr=27, widtht=8, widthb=8) # over1 = self.overscan_x # 71
BiasCombImg.replaceNegative(replace_value=0) # pre2 = self.prescan_y # 0 #4
BiasCombImg.quantize() # over2 = self.overscan_y # 84 #80
BiasCombImg = galsim.ImageUS(BiasCombImg)
timestamp_obs += 10 * 60 # # prescan & overscan
chip_utils.outputCal( # if config["ins_effects"]["add_prescan"] == True:
chip=self, # chip_utils.log_info(
img=BiasCombImg, # msg=" Apply pre/over-scan", logger=self.logger)
ra_cen=ra_cen, # BiasCombImg = chip_utils.AddPreScan(
dec_cen=dec_cen, # GSImage=BiasCombImg, pre1=pre1, pre2=pre2, over1=over1, over2=over2)
img_rot=img_rot,
im_type='BIAS', # # 1*16 output
pointing_ID=pointing_ID, # if config["ins_effects"]["format_output"] == True:
output_dir=chip_output.subdir, # chip_utils.log_info(
exptime=0.0, # msg=" Apply 1*16 format", logger=self.logger)
project_cycle=config["project_cycle"], # BiasCombImg = chip_utils.formatOutput(GSImage=BiasCombImg)
run_counter=config["run_counter"], # self.nsecy = 1
timestamp=timestamp_obs) # self.nsecx = 16
del BiasCombImg # # END
# Export combined (ncombine, Vignetting + PRNU) & single vignetting flat-field file # # Add Bias level
if config["ins_effects"]["flat_fielding"] == True and config["output_setting"]["flat_output"] == True and pointing_type == 'CAL': # if config["ins_effects"]["add_bias"] == True:
if self.logger is not None: # if self.logger is not None:
self.logger.info(" Output N frame Flat-Field files") # self.logger.info(
else: # " Adding Bias level and 16-channel non-uniformity")
print(" Output N frame Flat-Field files", flush=True) # else:
NFlat = int(config["output_setting"]["NFlat"]) # print(" Adding Bias level and 16-channel non-uniformity")
if config["ins_effects"]["add_bias"] == True: # BiasCombImg = effects.AddBiasNonUniform16(BiasCombImg,
biaslevel = self.bias_level # bias_level=biaslevel,
overscan = biaslevel-2 # nsecy=self.nsecy, nsecx=self.nsecx,
elif config["ins_effects"]["add_bias"] == False: # seed=SeedBiasNonuni+self.chipID,
biaslevel = 0 # logger=self.logger)
overscan = 0 # rng = galsim.UniformDeviate()
darklevel = self.dark_noise * \ # ncombine = 1
(self.flat_exptime+0.5*self.readout_time) # NoiseBias = galsim.GaussianNoise(
for i in range(NFlat): # rng=rng, sigma=self.read_noise*ncombine**0.5)
FlatSingle = flat_img * prnu_img + darklevel # BiasCombImg.addNoise(NoiseBias)
FlatCombImg, FlatTag = effects.MakeFlatNcomb(
flat_single_image=FlatSingle, # BiasCombImg, self.gain_channel = effects.ApplyGainNonUniform16(BiasCombImg, gain=self.gain,
ncombine=1, # nsecy=self.nsecy, nsecx=self.nsecx,
read_noise=self.read_noise, # seed=SeedGainNonuni+self.chipID,
gain=1, # logger=self.logger)
overscan=overscan, # # BiasCombImg = effects.AddOverscan(
biaslevel=0, # # BiasCombImg,
seed_bias=SeedDefective+self.chipID, # # overscan=float(config["ins_effects"]["bias_level"])-2, gain=self.gain,
logger=self.logger # # widthl=27, widthr=27, widtht=8, widthb=8)
) # BiasCombImg.replaceNegative(replace_value=0)
if config["ins_effects"]["cosmic_ray"] == True: # BiasCombImg.quantize()
if config["ins_effects"]["cray_differ"] == True: # BiasCombImg = galsim.ImageUS(BiasCombImg)
cr_map, cr_event_num = effects.produceCR_Map( # timestamp_obs += 10 * 60
xLen=self.npix_x, yLen=self.npix_y, # chip_utils.outputCal(
exTime=self.flat_exptime+0.5*self.readout_time, # chip=self,
cr_pixelRatio=0.003 * # img=BiasCombImg,
(self.flat_exptime+0.5*self.readout_time)/150., # ra_cen=ra_cen,
gain=self.gain, # dec_cen=dec_cen,
attachedSizes=self.attachedSizes, # img_rot=img_rot,
seed=SeedCosmicRay+pointing_ID*30+self.chipID+3) # im_type='BIAS',
# seed: obj-imaging:+0; bias:+1; dark:+2; flat:+3; # pointing_ID=pointing_ID,
FlatCombImg += cr_map # output_dir=chip_output.subdir,
del cr_map # exptime=0.0,
# project_cycle=config["project_cycle"],
# Add Hot Pixels or/and Dead Pixels # run_counter=config["run_counter"],
rgbadpix = Generator(PCG64(int(SeedDefective+self.chipID))) # timestamp=timestamp_obs)
badfraction = 5E-5*(rgbadpix.random()*0.5+0.7) # del BiasCombImg
FlatCombImg = effects.DefectivePixels(
FlatCombImg, IfHotPix=BoolHotPix, IfDeadPix=BoolDeadPix, fraction=badfraction, seed=SeedDefective+self.chipID, biaslevel=0) # # Export combined (ncombine, Vignetting + PRNU) & single vignetting flat-field file
# if config["ins_effects"]["flat_fielding"] == True and config["output_setting"]["flat_output"] == True and pointing_type == 'CAL':
# Apply Bad lines # if self.logger is not None:
if config["ins_effects"]["add_badcolumns"] == True: # self.logger.info(" Output N frame Flat-Field files")
FlatCombImg = effects.BadColumns( # else:
FlatCombImg, seed=SeedBadColumns, chipid=self.chipID, logger=self.logger) # print(" Output N frame Flat-Field files", flush=True)
# NFlat = int(config["output_setting"]["NFlat"])
if config["ins_effects"]["non_linear"] == True: # if config["ins_effects"]["add_bias"] == True:
if self.logger is not None: # biaslevel = self.bias_level
self.logger.info( # overscan = biaslevel-2
" Applying Non-Linearity on the Flat image") # elif config["ins_effects"]["add_bias"] == False:
else: # biaslevel = 0
print( # overscan = 0
" Applying Non-Linearity on the Flat image", flush=True) # darklevel = self.dark_noise * \
FlatCombImg = effects.NonLinearity( # (self.flat_exptime+0.5*self.readout_time)
GSImage=FlatCombImg, beta1=5.e-7, beta2=0) # 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,
# biaslevel=0,
# seed_bias=SeedDefective+self.chipID,
# logger=self.logger
# )
# if config["ins_effects"]["cosmic_ray"] == True:
# if config["ins_effects"]["cray_differ"] == True:
# cr_map, cr_event_num = effects.produceCR_Map(
# xLen=self.npix_x, yLen=self.npix_y,
# exTime=self.flat_exptime+0.5*self.readout_time,
# cr_pixelRatio=0.003 *
# (self.flat_exptime+0.5*self.readout_time)/150.,
# gain=self.gain,
# attachedSizes=self.attachedSizes,
# seed=SeedCosmicRay+pointing_ID*30+self.chipID+3)
# # seed: obj-imaging:+0; bias:+1; dark:+2; flat:+3;
# FlatCombImg += cr_map
# del cr_map
# # 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)
# # Apply Bad lines
# if config["ins_effects"]["add_badcolumns"] == True:
# FlatCombImg = effects.BadColumns(
# FlatCombImg, seed=SeedBadColumns, chipid=self.chipID, logger=self.logger)
# if config["ins_effects"]["non_linear"] == True:
# if self.logger is not None:
# self.logger.info(
# " Applying Non-Linearity on the Flat image")
# else:
# print(
# " Applying Non-Linearity on the Flat image", flush=True)
# FlatCombImg = effects.NonLinearity(
# GSImage=FlatCombImg, beta1=5.e-7, beta2=0)
# # if config["ins_effects"]["cte_trail"] == True:
# # FlatCombImg = effects.CTE_Effect(GSImage=FlatCombImg, threshold=3)
# # START
# pre1 = self.prescan_x # 27
# over1 = self.overscan_x # 71
# pre2 = self.prescan_y # 0 #4
# over2 = self.overscan_y # 84 #80
# if config["ins_effects"]["cte_trail"] == True: # if config["ins_effects"]["cte_trail"] == True:
# FlatCombImg = effects.CTE_Effect(GSImage=FlatCombImg, threshold=3) # chip_utils.log_info(
# START # msg=" Apply CTE Effect", logger=self.logger)
pre1 = self.prescan_x # 27 # # img = effects.CTE_Effect(GSImage=img, threshold=27)
over1 = self.overscan_x # 71 # # CTI_modeling
pre2 = self.prescan_y # 0 #4 # # 2*8 -> 1*16 img-layout
over2 = self.overscan_y # 84 #80 # FlatCombImg = chip_utils.formatOutput(GSImage=FlatCombImg)
if config["ins_effects"]["cte_trail"] == True: # self.nsecy = 1
chip_utils.log_info( # self.nsecx = 16
msg=" Apply CTE Effect", logger=self.logger)
# img = effects.CTE_Effect(GSImage=img, threshold=27) # img_arr = FlatCombImg.array
# CTI_modeling # ny, nx = img_arr.shape
# 2*8 -> 1*16 img-layout # dx = int(nx/self.nsecx)
FlatCombImg = chip_utils.formatOutput(GSImage=FlatCombImg) # dy = int(ny/self.nsecy)
self.nsecy = 1 # newimg = galsim.Image(nx, int(ny+over2), init_value=0)
self.nsecx = 16 # for ichannel in range(16):
# print('\n***add CTI effects: pointing-{:} chip-{:} channel-{:}***'.format(
img_arr = FlatCombImg.array # pointing_ID, self.chipID, ichannel+1))
ny, nx = img_arr.shape # # nx,ny,noverscan,nsp,nmax = 4608,4616,84,3,10
dx = int(nx/self.nsecx) # noverscan, nsp, nmax = over2, 3, 10
dy = int(ny/self.nsecy) # beta, w, c = 0.478, 84700, 0
newimg = galsim.Image(nx, int(ny+over2), init_value=0) # t = np.array([0.74, 7.7, 37], dtype=np.float32)
for ichannel in range(16): # rho_trap = np.array([0.6, 1.6, 1.4], dtype=np.float32)
print('\n***add CTI effects: pointing-{:} chip-{:} channel-{:}***'.format( # trap_seeds = np.array(
pointing_ID, self.chipID, ichannel+1)) # [0, 1000, 10000], dtype=np.int32) + ichannel + self.chipID*16
# nx,ny,noverscan,nsp,nmax = 4608,4616,84,3,10 # release_seed = 50 + ichannel + pointing_ID*30 + self.chipID*16
noverscan, nsp, nmax = over2, 3, 10 # newimg.array[:, 0+ichannel*dx:dx+ichannel*dx] = CTI_sim(
beta, w, c = 0.478, 84700, 0 # img_arr[:, 0+ichannel*dx:dx+ichannel*dx], dx, dy, noverscan, nsp, nmax, beta, w, c, t, rho_trap, trap_seeds, release_seed)
t = np.array([0.74, 7.7, 37], dtype=np.float32) # newimg.wcs = FlatCombImg.wcs
rho_trap = np.array([0.6, 1.6, 1.4], dtype=np.float32) # del FlatCombImg
trap_seeds = np.array( # FlatCombImg = newimg
[0, 1000, 10000], dtype=np.int32) + ichannel + self.chipID*16
release_seed = 50 + ichannel + pointing_ID*30 + self.chipID*16 # # 1*16 -> 2*8 img-layout
newimg.array[:, 0+ichannel*dx:dx+ichannel*dx] = CTI_sim( # FlatCombImg = chip_utils.formatRevert(GSImage=FlatCombImg)
img_arr[:, 0+ichannel*dx:dx+ichannel*dx], dx, dy, noverscan, nsp, nmax, beta, w, c, t, rho_trap, trap_seeds, release_seed) # self.nsecy = 2
newimg.wcs = FlatCombImg.wcs # self.nsecx = 8
del FlatCombImg
FlatCombImg = newimg # # prescan & overscan
# if config["ins_effects"]["add_prescan"] == True:
# 1*16 -> 2*8 img-layout # chip_utils.log_info(
FlatCombImg = chip_utils.formatRevert(GSImage=FlatCombImg) # msg=" Apply pre/over-scan", logger=self.logger)
self.nsecy = 2 # if config["ins_effects"]["cte_trail"] == False:
self.nsecx = 8 # FlatCombImg = chip_utils.AddPreScan(
# GSImage=FlatCombImg, pre1=pre1, pre2=pre2, over1=over1, over2=over2)
# prescan & overscan # if config["ins_effects"]["cte_trail"] == True:
if config["ins_effects"]["add_prescan"] == True: # FlatCombImg = chip_utils.AddPreScan(
chip_utils.log_info( # GSImage=FlatCombImg, pre1=pre1, pre2=pre2, over1=over1, over2=0)
msg=" Apply pre/over-scan", logger=self.logger)
if config["ins_effects"]["cte_trail"] == False: # # 1*16 output
FlatCombImg = chip_utils.AddPreScan( # if config["ins_effects"]["format_output"] == True:
GSImage=FlatCombImg, pre1=pre1, pre2=pre2, over1=over1, over2=over2) # chip_utils.log_info(
if config["ins_effects"]["cte_trail"] == True: # msg=" Apply 1*16 format", logger=self.logger)
FlatCombImg = chip_utils.AddPreScan( # FlatCombImg = chip_utils.formatOutput(GSImage=FlatCombImg)
GSImage=FlatCombImg, pre1=pre1, pre2=pre2, over1=over1, over2=0) # self.nsecy = 1
# self.nsecx = 16
# 1*16 output # # END
if config["ins_effects"]["format_output"] == True:
chip_utils.log_info( # # Add Bias level
msg=" Apply 1*16 format", logger=self.logger) # if config["ins_effects"]["add_bias"] == True:
FlatCombImg = chip_utils.formatOutput(GSImage=FlatCombImg) # if self.logger is not None:
self.nsecy = 1 # self.logger.info(
self.nsecx = 16 # " Adding Bias level and 16-channel non-uniformity")
# END # else:
# print(" Adding Bias level and 16-channel non-uniformity")
# Add Bias level # # img += float(config["ins_effects"]["bias_level"])
if config["ins_effects"]["add_bias"] == True: # FlatCombImg = effects.AddBiasNonUniform16(FlatCombImg,
if self.logger is not None: # bias_level=biaslevel,
self.logger.info( # nsecy=self.nsecy, nsecx=self.nsecx,
" Adding Bias level and 16-channel non-uniformity") # seed=SeedBiasNonuni+self.chipID,
else: # logger=self.logger)
print(" Adding Bias level and 16-channel non-uniformity")
# img += float(config["ins_effects"]["bias_level"])
FlatCombImg = effects.AddBiasNonUniform16(FlatCombImg,
bias_level=biaslevel,
nsecy=self.nsecy, nsecx=self.nsecx,
seed=SeedBiasNonuni+self.chipID,
logger=self.logger)
# Add Read-out Noise
if config["ins_effects"]["add_readout"] == True:
seed = int(config["random_seeds"]["seed_readout"]
) + pointing_ID*30 + self.chipID + 3
rng_readout = galsim.BaseDeviate(seed)
readout_noise = galsim.GaussianNoise(
rng=rng_readout, sigma=self.read_noise)
FlatCombImg.addNoise(readout_noise)
FlatCombImg, self.gain_channel = effects.ApplyGainNonUniform16(FlatCombImg, gain=self.gain,
nsecy=self.nsecy, nsecx=self.nsecx,
seed=SeedGainNonuni+self.chipID,
logger=self.logger)
# 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)
timestamp_obs += 10 * 60
chip_utils.outputCal(
chip=self,
img=FlatCombImg,
ra_cen=ra_cen,
dec_cen=dec_cen,
img_rot=img_rot,
im_type='FLAT',
pointing_ID=pointing_ID,
output_dir=chip_output.subdir,
exptime=self.flat_exptime,
project_cycle=config["project_cycle"],
run_counter=config["run_counter"],
timestamp=timestamp_obs)
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
if config["ins_effects"]["add_dark"] == True and config["output_setting"]["dark_output"] == True and pointing_type == 'CAL':
if self.logger is not None:
self.logger.info(" Output N frame Dark Current files")
else:
print(" Output N frame Dark Current files", flush=True)
NDark = int(config["output_setting"]["NDark"])
if config["ins_effects"]["add_bias"] == True:
biaslevel = self.bias_level
overscan = biaslevel-2
elif config["ins_effects"]["add_bias"] == False:
biaslevel = 0
overscan = 0
for i in range(NDark):
DarkCombImg, DarkTag = effects.MakeDarkNcomb(
self.npix_x, self.npix_y,
overscan=overscan, bias_level=0, darkpsec=0.02, exptime=self.dark_exptime+0.5*self.readout_time,
ncombine=1, read_noise=self.read_noise,
gain=1, seed_bias=SeedBiasNonuni+self.chipID,
logger=self.logger)
if config["ins_effects"]["cosmic_ray"] == True:
if config["ins_effects"]["cray_differ"] == True:
cr_map, cr_event_num = effects.produceCR_Map(
xLen=self.npix_x, yLen=self.npix_y,
exTime=self.dark_exptime+0.5*self.readout_time,
cr_pixelRatio=0.003 *
(self.dark_exptime+0.5*self.readout_time)/150.,
gain=self.gain,
attachedSizes=self.attachedSizes,
seed=SeedCosmicRay+pointing_ID*30+self.chipID+2)
# seed: obj-imaging:+0; bias:+1; dark:+2; flat:+3;
DarkCombImg += cr_map
cr_map[cr_map > 65535] = 65535
cr_map[cr_map < 0] = 0
crmap_gsimg = galsim.Image(cr_map, dtype=np.uint16)
del cr_map
# START
# prescan & overscan
if config["ins_effects"]["add_prescan"] == True:
chip_utils.log_info(
msg=" Apply pre/over-scan", logger=self.logger)
crmap_gsimg = chip_utils.AddPreScan(
GSImage=crmap_gsimg, pre1=pre1, pre2=pre2, over1=over1, over2=over2)
# 1*16 output
if config["ins_effects"]["format_output"] == True:
chip_utils.log_info(
msg=" Apply 1*16 format", logger=self.logger)
crmap_gsimg = chip_utils.formatOutput(
GSImage=crmap_gsimg)
self.nsecy = 1
self.nsecx = 16
# END
chip_utils.outputCal(
chip=self,
img=crmap_gsimg,
ra_cen=ra_cen,
dec_cen=dec_cen,
img_rot=img_rot,
im_type='CRD',
pointing_ID=pointing_ID,
output_dir=chip_output.subdir,
exptime=self.dark_exptime,
project_cycle=config["project_cycle"],
run_counter=config["run_counter"],
timestamp=timestamp_obs)
del crmap_gsimg
# 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)
# Apply Bad lines
if config["ins_effects"]["add_badcolumns"] == True:
DarkCombImg = effects.BadColumns(
DarkCombImg, seed=SeedBadColumns, chipid=self.chipID, logger=self.logger)
# Non-Linearity for Dark
if config["ins_effects"]["non_linear"] == True:
if self.logger is not None:
self.logger.info(
" Applying Non-Linearity on the Dark image")
else:
print(
" Applying Non-Linearity on the Dark image", flush=True)
DarkCombImg = effects.NonLinearity(
GSImage=DarkCombImg, beta1=5.e-7, beta2=0)
# # Add Read-out Noise
# if config["ins_effects"]["add_readout"] == True:
# seed = int(config["random_seeds"]["seed_readout"]
# ) + pointing_ID*30 + self.chipID + 3
# rng_readout = galsim.BaseDeviate(seed)
# readout_noise = galsim.GaussianNoise(
# rng=rng_readout, sigma=self.read_noise)
# FlatCombImg.addNoise(readout_noise)
# FlatCombImg, self.gain_channel = effects.ApplyGainNonUniform16(FlatCombImg, gain=self.gain,
# nsecy=self.nsecy, nsecx=self.nsecx,
# seed=SeedGainNonuni+self.chipID,
# logger=self.logger)
# # 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)
# timestamp_obs += 10 * 60
# chip_utils.outputCal(
# chip=self,
# img=FlatCombImg,
# ra_cen=ra_cen,
# dec_cen=dec_cen,
# img_rot=img_rot,
# im_type='FLAT',
# pointing_ID=pointing_ID,
# output_dir=chip_output.subdir,
# exptime=self.flat_exptime,
# project_cycle=config["project_cycle"],
# run_counter=config["run_counter"],
# timestamp=timestamp_obs)
# 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
# if config["ins_effects"]["add_dark"] == True and config["output_setting"]["dark_output"] == True and pointing_type == 'CAL':
# if self.logger is not None:
# self.logger.info(" Output N frame Dark Current files")
# else:
# print(" Output N frame Dark Current files", flush=True)
# NDark = int(config["output_setting"]["NDark"])
# if config["ins_effects"]["add_bias"] == True:
# biaslevel = self.bias_level
# overscan = biaslevel-2
# elif config["ins_effects"]["add_bias"] == False:
# biaslevel = 0
# overscan = 0
# for i in range(NDark):
# DarkCombImg, DarkTag = effects.MakeDarkNcomb(
# self.npix_x, self.npix_y,
# overscan=overscan, bias_level=0, darkpsec=0.02, exptime=self.dark_exptime+0.5*self.readout_time,
# ncombine=1, read_noise=self.read_noise,
# gain=1, seed_bias=SeedBiasNonuni+self.chipID,
# logger=self.logger)
# if config["ins_effects"]["cosmic_ray"] == True:
# if config["ins_effects"]["cray_differ"] == True:
# cr_map, cr_event_num = effects.produceCR_Map(
# xLen=self.npix_x, yLen=self.npix_y,
# exTime=self.dark_exptime+0.5*self.readout_time,
# cr_pixelRatio=0.003 *
# (self.dark_exptime+0.5*self.readout_time)/150.,
# gain=self.gain,
# attachedSizes=self.attachedSizes,
# seed=SeedCosmicRay+pointing_ID*30+self.chipID+2)
# # seed: obj-imaging:+0; bias:+1; dark:+2; flat:+3;
# DarkCombImg += cr_map
# cr_map[cr_map > 65535] = 65535
# cr_map[cr_map < 0] = 0
# crmap_gsimg = galsim.Image(cr_map, dtype=np.uint16)
# del cr_map
# # START
# # prescan & overscan
# if config["ins_effects"]["add_prescan"] == True:
# chip_utils.log_info(
# msg=" Apply pre/over-scan", logger=self.logger)
# crmap_gsimg = chip_utils.AddPreScan(
# GSImage=crmap_gsimg, pre1=pre1, pre2=pre2, over1=over1, over2=over2)
# # 1*16 output
# if config["ins_effects"]["format_output"] == True:
# chip_utils.log_info(
# msg=" Apply 1*16 format", logger=self.logger)
# crmap_gsimg = chip_utils.formatOutput(
# GSImage=crmap_gsimg)
# self.nsecy = 1
# self.nsecx = 16
# # END
# chip_utils.outputCal(
# chip=self,
# img=crmap_gsimg,
# ra_cen=ra_cen,
# dec_cen=dec_cen,
# img_rot=img_rot,
# im_type='CRD',
# pointing_ID=pointing_ID,
# output_dir=chip_output.subdir,
# exptime=self.dark_exptime,
# project_cycle=config["project_cycle"],
# run_counter=config["run_counter"],
# timestamp=timestamp_obs)
# del crmap_gsimg
# # 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)
# # Apply Bad lines
# if config["ins_effects"]["add_badcolumns"] == True:
# DarkCombImg = effects.BadColumns(
# DarkCombImg, seed=SeedBadColumns, chipid=self.chipID, logger=self.logger)
# # Non-Linearity for Dark
# if config["ins_effects"]["non_linear"] == True:
# if self.logger is not None:
# self.logger.info(
# " Applying Non-Linearity on the Dark image")
# else:
# print(
# " Applying Non-Linearity on the Dark image", flush=True)
# DarkCombImg = effects.NonLinearity(
# GSImage=DarkCombImg, beta1=5.e-7, beta2=0)
# # if config["ins_effects"]["cte_trail"] == True:
# # DarkCombImg = effects.CTE_Effect(GSImage=DarkCombImg, threshold=3)
# # START
# pre1 = self.prescan_x # 27
# over1 = self.overscan_x # 71
# pre2 = self.prescan_y # 0 #4
# over2 = self.overscan_y # 84 #80
# if config["ins_effects"]["cte_trail"] == True: # if config["ins_effects"]["cte_trail"] == True:
# DarkCombImg = effects.CTE_Effect(GSImage=DarkCombImg, threshold=3) # chip_utils.log_info(
# START # msg=" Apply CTE Effect", logger=self.logger)
pre1 = self.prescan_x # 27 # # img = effects.CTE_Effect(GSImage=img, threshold=27)
over1 = self.overscan_x # 71 # # CTI_modeling
pre2 = self.prescan_y # 0 #4 # # 2*8 -> 1*16 img-layout
over2 = self.overscan_y # 84 #80 # DarkCombImg = chip_utils.formatOutput(GSImage=DarkCombImg)
if config["ins_effects"]["cte_trail"] == True: # self.nsecy = 1
chip_utils.log_info( # self.nsecx = 16
msg=" Apply CTE Effect", logger=self.logger)
# img = effects.CTE_Effect(GSImage=img, threshold=27) # img_arr = DarkCombImg.array
# CTI_modeling # ny, nx = img_arr.shape
# 2*8 -> 1*16 img-layout # dx = int(nx/self.nsecx)
DarkCombImg = chip_utils.formatOutput(GSImage=DarkCombImg) # dy = int(ny/self.nsecy)
self.nsecy = 1 # newimg = galsim.Image(nx, int(ny+over2), init_value=0)
self.nsecx = 16 # for ichannel in range(16):
# print('\n***add CTI effects: pointing-{:} chip-{:} channel-{:}***'.format(
img_arr = DarkCombImg.array # pointing_ID, self.chipID, ichannel+1))
ny, nx = img_arr.shape # # nx,ny,noverscan,nsp,nmax = 4608,4616,84,3,10
dx = int(nx/self.nsecx) # noverscan, nsp, nmax = over2, 3, 10
dy = int(ny/self.nsecy) # beta, w, c = 0.478, 84700, 0
newimg = galsim.Image(nx, int(ny+over2), init_value=0) # t = np.array([0.74, 7.7, 37], dtype=np.float32)
for ichannel in range(16): # rho_trap = np.array([0.6, 1.6, 1.4], dtype=np.float32)
print('\n***add CTI effects: pointing-{:} chip-{:} channel-{:}***'.format( # trap_seeds = np.array(
pointing_ID, self.chipID, ichannel+1)) # [0, 1000, 10000], dtype=np.int32) + ichannel + self.chipID*16
# nx,ny,noverscan,nsp,nmax = 4608,4616,84,3,10 # release_seed = 50 + ichannel + pointing_ID*30 + self.chipID*16
noverscan, nsp, nmax = over2, 3, 10 # newimg.array[:, 0+ichannel*dx:dx+ichannel*dx] = CTI_sim(
beta, w, c = 0.478, 84700, 0 # img_arr[:, 0+ichannel*dx:dx+ichannel*dx], dx, dy, noverscan, nsp, nmax, beta, w, c, t, rho_trap, trap_seeds, release_seed)
t = np.array([0.74, 7.7, 37], dtype=np.float32) # newimg.wcs = DarkCombImg.wcs
rho_trap = np.array([0.6, 1.6, 1.4], dtype=np.float32) # del DarkCombImg
trap_seeds = np.array( # DarkCombImg = newimg
[0, 1000, 10000], dtype=np.int32) + ichannel + self.chipID*16
release_seed = 50 + ichannel + pointing_ID*30 + self.chipID*16 # # 1*16 -> 2*8 img-layout
newimg.array[:, 0+ichannel*dx:dx+ichannel*dx] = CTI_sim( # DarkCombImg = chip_utils.formatRevert(GSImage=DarkCombImg)
img_arr[:, 0+ichannel*dx:dx+ichannel*dx], dx, dy, noverscan, nsp, nmax, beta, w, c, t, rho_trap, trap_seeds, release_seed) # self.nsecy = 2
newimg.wcs = DarkCombImg.wcs # self.nsecx = 8
del DarkCombImg
DarkCombImg = newimg # # prescan & overscan
# if config["ins_effects"]["add_prescan"] == True:
# 1*16 -> 2*8 img-layout # chip_utils.log_info(
DarkCombImg = chip_utils.formatRevert(GSImage=DarkCombImg) # msg=" Apply pre/over-scan", logger=self.logger)
self.nsecy = 2 # if config["ins_effects"]["cte_trail"] == False:
self.nsecx = 8 # DarkCombImg = chip_utils.AddPreScan(
# GSImage=DarkCombImg, pre1=pre1, pre2=pre2, over1=over1, over2=over2)
# if config["ins_effects"]["cte_trail"] == True:
# DarkCombImg = chip_utils.AddPreScan(
# GSImage=DarkCombImg, pre1=pre1, pre2=pre2, over1=over1, over2=0)
# # 1*16 output
# if config["ins_effects"]["format_output"] == True:
# chip_utils.log_info(
# msg=" Apply 1*16 format", logger=self.logger)
# DarkCombImg = chip_utils.formatOutput(GSImage=DarkCombImg)
# self.nsecy = 1
# self.nsecx = 16
# # END
# # Add Bias level
# if config["ins_effects"]["add_bias"] == True:
# if self.logger is not None:
# self.logger.info(
# " Adding Bias level and 16-channel non-uniformity")
# else:
# print(" Adding Bias level and 16-channel non-uniformity")
# # img += float(config["ins_effects"]["bias_level"])
# DarkCombImg = effects.AddBiasNonUniform16(DarkCombImg,
# bias_level=biaslevel,
# nsecy=self.nsecy, nsecx=self.nsecx,
# seed=SeedBiasNonuni+self.chipID,
# logger=self.logger)
# prescan & overscan # # Add Read-out Noise
if config["ins_effects"]["add_prescan"] == True: # if config["ins_effects"]["add_readout"] == True:
chip_utils.log_info( # seed = int(config["random_seeds"]["seed_readout"]
msg=" Apply pre/over-scan", logger=self.logger) # ) + pointing_ID*30 + self.chipID + 2
if config["ins_effects"]["cte_trail"] == False: # rng_readout = galsim.BaseDeviate(seed)
DarkCombImg = chip_utils.AddPreScan( # readout_noise = galsim.GaussianNoise(
GSImage=DarkCombImg, pre1=pre1, pre2=pre2, over1=over1, over2=over2) # rng=rng_readout, sigma=self.read_noise)
if config["ins_effects"]["cte_trail"] == True: # DarkCombImg.addNoise(readout_noise)
DarkCombImg = chip_utils.AddPreScan(
GSImage=DarkCombImg, pre1=pre1, pre2=pre2, over1=over1, over2=0) # DarkCombImg, self.gain_channel = effects.ApplyGainNonUniform16(
# DarkCombImg, gain=self.gain,
# 1*16 output # nsecy=self.nsecy, nsecx=self.nsecx,
if config["ins_effects"]["format_output"] == True: # seed=SeedGainNonuni+self.chipID,
chip_utils.log_info( # logger=self.logger)
msg=" Apply 1*16 format", logger=self.logger) # # DarkCombImg = effects.AddOverscan(
DarkCombImg = chip_utils.formatOutput(GSImage=DarkCombImg) # # DarkCombImg,
self.nsecy = 1 # # overscan=overscan, gain=self.gain,
self.nsecx = 16 # # widthl=27, widthr=27, widtht=8, widthb=8)
# END # DarkCombImg.replaceNegative(replace_value=0)
# DarkCombImg.quantize()
# Add Bias level # DarkCombImg = galsim.ImageUS(DarkCombImg)
if config["ins_effects"]["add_bias"] == True: # timestamp_obs += 10 * 60
if self.logger is not None: # chip_utils.outputCal(
self.logger.info( # chip=self,
" Adding Bias level and 16-channel non-uniformity") # img=DarkCombImg,
else: # ra_cen=ra_cen,
print(" Adding Bias level and 16-channel non-uniformity") # dec_cen=dec_cen,
# img += float(config["ins_effects"]["bias_level"]) # img_rot=img_rot,
DarkCombImg = effects.AddBiasNonUniform16(DarkCombImg, # im_type='DARK',
bias_level=biaslevel, # pointing_ID=pointing_ID,
nsecy=self.nsecy, nsecx=self.nsecx, # output_dir=chip_output.subdir,
seed=SeedBiasNonuni+self.chipID, # exptime=self.dark_exptime,
logger=self.logger) # project_cycle=config["project_cycle"],
# run_counter=config["run_counter"],
# Add Read-out Noise # timestamp=timestamp_obs)
if config["ins_effects"]["add_readout"] == True: # del DarkCombImg
seed = int(config["random_seeds"]["seed_readout"] # # img = galsim.ImageUS(img)
) + pointing_ID*30 + self.chipID + 2
rng_readout = galsim.BaseDeviate(seed) # # # 16 output channel, with each a single image file
readout_noise = galsim.GaussianNoise( # # if config["ins_effects"]["readout16"] == True:
rng=rng_readout, sigma=self.read_noise) # # print(" 16 Output Channel simulation")
DarkCombImg.addNoise(readout_noise) # # for coli in [0, 1]:
# # for rowi in range(8):
DarkCombImg, self.gain_channel = effects.ApplyGainNonUniform16( # # sub_img = effects.readout16(
DarkCombImg, gain=self.gain, # # GSImage=img,
nsecy=self.nsecy, nsecx=self.nsecx, # # rowi=rowi,
seed=SeedGainNonuni+self.chipID, # # coli=coli,
logger=self.logger) # # overscan_value=self.overscan)
# DarkCombImg = effects.AddOverscan( # # rowcoltag = str(rowi) + str(coli)
# DarkCombImg, # # img_name_root = chip_output.img_name.split(".")[0]
# overscan=overscan, gain=self.gain, # # sub_img.write("%s/%s_%s.fits" % (chip_output.subdir, img_name_root, rowcoltag))
# widthl=27, widthr=27, widtht=8, widthb=8) # # del sub_img
DarkCombImg.replaceNegative(replace_value=0) # return img
DarkCombImg.quantize()
DarkCombImg = galsim.ImageUS(DarkCombImg)
timestamp_obs += 10 * 60
chip_utils.outputCal(
chip=self,
img=DarkCombImg,
ra_cen=ra_cen,
dec_cen=dec_cen,
img_rot=img_rot,
im_type='DARK',
pointing_ID=pointing_ID,
output_dir=chip_output.subdir,
exptime=self.dark_exptime,
project_cycle=config["project_cycle"],
run_counter=config["run_counter"],
timestamp=timestamp_obs)
del DarkCombImg
# img = galsim.ImageUS(img)
# # 16 output channel, with each a single image file
# if config["ins_effects"]["readout16"] == True:
# 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
return img
import galsim import galsim
import numpy as np import numpy as np
class FocalPlane(object): class FocalPlane(object):
def __init__(self, config=None, chip_list=None, survey_type='Photometric', bad_chips=None): def __init__(self, chip_list=None, survey_type='Photometric', bad_chips=None):
"""Get the focal plane layout """Get the focal plane layout
""" """
self.nchips = 42 self.nchips = 42
...@@ -39,18 +40,18 @@ class FocalPlane(object): ...@@ -39,18 +40,18 @@ class FocalPlane(object):
for i in range(1, 31): for i in range(1, 31):
self.ignore_chips.append(i) self.ignore_chips.append(i)
if config is not None: # if config is not None:
self.nchip_x = config["nchip_x"] # self.nchip_x = config["nchip_x"]
self.nchip_y = config["nchip_y"] # self.nchip_y = config["nchip_y"]
self.npix_tot_x = config["npix_tot_x"] # self.npix_tot_x = config["npix_tot_x"]
self.npix_tot_y = config["npix_tot_y"] # self.npix_tot_y = config["npix_tot_y"]
self.npix_gap_x = config["npix_gap_x"] # self.npix_gap_x = config["npix_gap_x"]
self.npix_gap_y = config["npix_gap_y"] # self.npix_gap_y = config["npix_gap_y"]
if "chipLabelIDs" in config: # if "chipLabelIDs" in config:
self.chipLabelIDs = config["chipLabelIDs"] # self.chipLabelIDs = config["chipLabelIDs"]
if "bad_chips" in config: # if "bad_chips" in config:
self.bad_chips = config["bad_chips"] # self.bad_chips = config["bad_chips"]
else: # else:
self.nchip_x = 6 self.nchip_x = 6
self.nchip_y = 5 self.nchip_y = 5
self.npix_tot_x = 59516 self.npix_tot_x = 59516
...@@ -65,7 +66,7 @@ class FocalPlane(object): ...@@ -65,7 +66,7 @@ class FocalPlane(object):
self.cen_pix_y = 0 self.cen_pix_y = 0
def getChipLabel(self, chipID): def getChipLabel(self, chipID):
return str("0%d"%chipID)[-2:] return str("0%d" % chipID)[-2:]
def isBadChip(self, chipID): def isBadChip(self, chipID):
"""Check if chip #(chipID) on the focal plane is bad or not """Check if chip #(chipID) on the focal plane is bad or not
...@@ -89,7 +90,8 @@ class FocalPlane(object): ...@@ -89,7 +90,8 @@ class FocalPlane(object):
WCS of the focal plane WCS of the focal plane
""" """
if logger is not None: if logger is not None:
logger.info(" Construct the wcs of the entire image mosaic using Gnomonic/TAN projection") logger.info(
" Construct the wcs of the entire image mosaic using Gnomonic/TAN projection")
if (xcen == None) or (ycen == None): if (xcen == None) or (ycen == None):
xcen = self.cen_pix_x xcen = self.cen_pix_x
ycen = self.cen_pix_y ycen = self.cen_pix_y
...@@ -104,7 +106,8 @@ class FocalPlane(object): ...@@ -104,7 +106,8 @@ class FocalPlane(object):
dvdy = -np.cos(img_rot.rad) * pix_scale dvdy = -np.cos(img_rot.rad) * pix_scale
moscen = galsim.PositionD(x=xcen, y=ycen) moscen = galsim.PositionD(x=xcen, y=ycen)
sky_center = galsim.CelestialCoord(ra=ra*galsim.degrees, dec=dec*galsim.degrees) sky_center = galsim.CelestialCoord(
ra=ra*galsim.degrees, dec=dec*galsim.degrees)
affine = galsim.AffineTransform(dudx, dudy, dvdx, dvdy, origin=moscen) affine = galsim.AffineTransform(dudx, dudy, dvdx, dvdy, origin=moscen)
WCS = galsim.TanWCS(affine, sky_center, units=galsim.arcsec) WCS = galsim.TanWCS(affine, sky_center, units=galsim.arcsec)
...@@ -115,10 +118,10 @@ class FocalPlane(object): ...@@ -115,10 +118,10 @@ class FocalPlane(object):
The sky coverage of an area The sky coverage of an area
""" """
r2d = 180.0/np.pi r2d = 180.0/np.pi
s1 = wcs.toWorld(galsim.PositionD(x0,y0)) s1 = wcs.toWorld(galsim.PositionD(x0, y0))
s2 = wcs.toWorld(galsim.PositionD(x0,y1)) s2 = wcs.toWorld(galsim.PositionD(x0, y1))
s3 = wcs.toWorld(galsim.PositionD(x1,y0)) s3 = wcs.toWorld(galsim.PositionD(x1, y0))
s4 = wcs.toWorld(galsim.PositionD(x1,y1)) s4 = wcs.toWorld(galsim.PositionD(x1, y1))
ra = [s1.ra.rad*r2d, s2.ra.rad*r2d, s3.ra.rad*r2d, s4.ra.rad*r2d] ra = [s1.ra.rad*r2d, s2.ra.rad*r2d, s3.ra.rad*r2d, s4.ra.rad*r2d]
dec = [s1.dec.rad*r2d, s2.dec.rad*r2d, s3.dec.rad*r2d, s4.dec.rad*r2d] dec = [s1.dec.rad*r2d, s2.dec.rad*r2d, s3.dec.rad*r2d, s4.dec.rad*r2d]
......
import unittest
import os
import galsim
from ObservationSim.Instrument import FocalPlane, Chip
class TestFocalPlane(unittest.TestCase):
def __init__(self, methodName='runTest'):
super(TestFocalPlane, self).__init__(methodName)
self.dataPath = os.path.join(
os.getenv('UNIT_TEST_DATA_ROOT'), 'csst_msc_sim/csst_fz_msc')
self.focal_plane = FocalPlane(
chip_list=['8'])
self.assertTrue(self.focal_plane.cen_pix_x == 0)
self.assertTrue(self.focal_plane.cen_pix_y == 0)
test_focal_plane_phot = FocalPlane(survey_type='Photometric')
test_focal_plane_spec = FocalPlane(survey_type='Spectroscopic')
test_focal_plane_FGS = FocalPlane(survey_type='FGS')
test_focal_plane_bad_chips = FocalPlane(bad_chips=["1"])
def test_fp_method(self):
wcs = self.focal_plane.getTanWCS(
192.8595, 0., 0.*galsim.degrees, 0.0074)
sky_coverage = self.focal_plane.getSkyCoverage(
wcs, x0=-1, x1=0, y0=-1, y1=0)
print(sky_coverage.area())
self.assertTrue(abs(sky_coverage.area() - 0.0074**2/(3600.**2)) < 1e13)
if __name__ == '__main_':
unittest.main()
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment