Commit e13a656a authored by Wei Chengliang's avatar Wei Chengliang
Browse files

add pre/over-scan & format output

parent 278305de
Loading
Loading
Loading
Loading
+18 −4
Original line number Original line Diff line number Diff line
@@ -358,7 +358,11 @@ class Chip(FocalPlane):
        #     img.addNoise(dark_noise)
        #     img.addNoise(dark_noise)


        # Add dark current & Poisson noise
        # Add dark current & Poisson noise
        InputDark = False
        if config["ins_effects"]["add_dark"] == True:
        if config["ins_effects"]["add_dark"] == True:
            if InputDark:
                img = chip_utils.add_inputdark(img=img, chip=self, exptime=exptime)
            else:
                img, _ = chip_utils.add_poisson(img=img, chip=self, exptime=exptime, poisson_noise=poisson_noise)
                img, _ = chip_utils.add_poisson(img=img, chip=self, exptime=exptime, poisson_noise=poisson_noise)
        else:
        else:
            img, _ = chip_utils.add_poisson(img=img, chip=self, exptime=exptime, poisson_noise=poisson_noise, dark_noise=0.)
            img, _ = chip_utils.add_poisson(img=img, chip=self, exptime=exptime, poisson_noise=poisson_noise, dark_noise=0.)
@@ -391,13 +395,23 @@ class Chip(FocalPlane):
            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)


        ### prescan & overscan
        if config["ins_effects"]["add_prescan"] == True:
            img = chip_utils.AddPreScan(GSImage=img, pre1=27, pre2=4, over1=71, over2=80)
        ### 1*16 output
        if config["ins_effects"]["format_output"] == True:
            img = chip_utils.formatOutput(GSImage=img)
            self.nsecy = 1
            self.nsecx = 16
                    

        # Add Bias level
        # Add Bias level
        if config["ins_effects"]["add_bias"] == True:
        if config["ins_effects"]["add_bias"] == True:
            chip_utils.log_info(msg="  Adding Bias level and 16-channel non-uniformity", logger=self.logger)
            chip_utils.log_info(msg="  Adding Bias level and 16-channel non-uniformity", logger=self.logger)
            if config["ins_effects"]["bias_16channel"] == True:
            if config["ins_effects"]["bias_16channel"] == True:
                img = effects.AddBiasNonUniform16(img, 
                img = effects.AddBiasNonUniform16(img, 
                    bias_level=float(self.bias_level), 
                    bias_level=float(self.bias_level), 
                    nsecy = 2, nsecx=8, 
                    nsecy = self.nsecy, nsecx=self.nsecx, 
                    seed=SeedBiasNonuni+self.chipID,
                    seed=SeedBiasNonuni+self.chipID,
                    logger=self.logger)
                    logger=self.logger)
            elif config["ins_effects"]["bias_16channel"] == False:
            elif config["ins_effects"]["bias_16channel"] == False:
@@ -415,7 +429,7 @@ class Chip(FocalPlane):
        if config["ins_effects"]["gain_16channel"] == True:
        if config["ins_effects"]["gain_16channel"] == True:
            img, self.gain_channel = effects.ApplyGainNonUniform16(
            img, self.gain_channel = effects.ApplyGainNonUniform16(
                img, gain=self.gain, 
                img, gain=self.gain, 
                nsecy = 2, nsecx=8, 
                nsecy = self.nsecy, nsecx=self.nsecx, 
                seed=SeedGainNonuni+self.chipID,
                seed=SeedGainNonuni+self.chipID,
                logger=self.logger)
                logger=self.logger)
        elif config["ins_effects"]["gain_16channel"] == False:
        elif config["ins_effects"]["gain_16channel"] == False:
+87 −3
Original line number Original line Diff line number Diff line
@@ -171,10 +171,11 @@ def add_poisson(img, chip, exptime=150., seed=0, sky_level=0., poisson_noise=Non
def add_brighter_fatter(img):
def add_brighter_fatter(img):
    #Inital dynamic lib
    #Inital dynamic lib
    try:
    try:
        with pkg_resources.files('ObservationSim.Instrument.Chip.lib_bf').joinpath("libmoduleBF.so") as lib_path:
        with pkg_resources.files('ObservationSim.Instrument.Chip.libBF').joinpath("libmoduleBF.so") as lib_path:
            print('--1', lib_path)
            lib_bf = ctypes.CDLL(lib_path)
            lib_bf = ctypes.CDLL(lib_path)
    except AttributeError:
    except AttributeError:
        with pkg_resources.path('ObservationSim.Instrument.Chip.lib_bf', "libmoduleBF.so") as lib_path:
        with pkg_resources.path('ObservationSim.Instrument.Chip.libBF', "libmoduleBF.so") as lib_path:
            lib_bf = ctypes.CDLL(lib_path)
            lib_bf = ctypes.CDLL(lib_path)
    lib_bf.addEffects.argtypes = [ctypes.c_int, ctypes.c_int, ctypes.POINTER(ctypes.c_float), ctypes.POINTER(ctypes.c_float), ctypes.c_int]
    lib_bf.addEffects.argtypes = [ctypes.c_int, ctypes.c_int, ctypes.POINTER(ctypes.c_float), ctypes.POINTER(ctypes.c_float), ctypes.c_int]
    
    
@@ -194,3 +195,86 @@ def add_brighter_fatter(img):
    img.array[:, :] = np.reshape(arr_imc, [nx, ny])
    img.array[:, :] = np.reshape(arr_imc, [nx, ny])
    del arr_ima, arr_imc
    del arr_ima, arr_imc
    return img
    return img

def add_inputdark(img, chip, exptime):
    fname = "/share/home/weichengliang/CSST_git/test_new_sim/csst-simulation/ObservationSim/Instrument/data/dark/dark_1000s_example_0.fits"
    hdu = fits.open(fname)
    #ny, nx = img.array.shape
    #inputdark = np.zeros([ny, nx])
    img.array[:, :] += hdu[0].data/hdu[0].header['exptime']*exptime
    hdu.close()
    del inputdark
    return img

def AddPreScan(GSImage, pre1=27, pre2=4, over1=71, over2=80, nsecy = 2, nsecx=8):
    img= GSImage.array
    ny, nx = img.shape
    dx = int(nx/nsecx)
    dy = int(ny/nsecy)

    imgt=np.zeros([int(nsecy*nsecx), int(ny/nsecy+pre2+over2), int(nx/nsecx+pre1+over1)])
    for iy in range(nsecy):
        for ix in range(nsecx):
            if iy % 2 == 0:
                tx = ix
            else:
                tx = (nsecx-1)-ix
            ty = iy 
            chunkidx = int(tx+ty*nsecx) #chunk1-[1,2,3,4], chunk2-[5,6,7,8], chunk3-[9,10,11,12], chunk4-[13,14,15,16]

            imgtemp = np.zeros([int(ny/nsecy+pre2+over2), int(nx/nsecx+pre1+over1)])
            if int(chunkidx/4) == 0:
                imgtemp[pre2:-over2, pre1:-over1] = img[iy*dy:(iy+1)*dy, ix*dx:(ix+1)*dx]
                imgt[chunkidx, :, :] = imgtemp
            if int(chunkidx/4) == 1:
                imgtemp[pre2:-over2, over1:-pre1] = img[iy*dy:(iy+1)*dy, ix*dx:(ix+1)*dx]
                imgt[chunkidx, :, :] = imgtemp #[:, ::-1]
            if int(chunkidx/4) == 2:
                imgtemp[over2:-pre2, over1:-pre1] = img[iy*dy:(iy+1)*dy, ix*dx:(ix+1)*dx]
                imgt[chunkidx, :, :] = imgtemp #[::-1, ::-1]
            if int(chunkidx/4) == 3:
                imgtemp[over2:-pre2, pre1:-over1] = img[iy*dy:(iy+1)*dy, ix*dx:(ix+1)*dx]
                imgt[chunkidx, :, :] = imgtemp #[::-1, :]

    imgtx1 = np.hstack(imgt[:nsecx:,       :, :])
    imgtx2 = np.hstack(imgt[:(nsecx-1):-1, :, :])

    newimg = galsim.Image(int(nx+(pre1+over1)*nsecx), int(ny+(pre2+over2)*nsecy), init_value=0)
    newimg.array[:, :] = np.concatenate([imgtx1, imgtx2])

    newimg.wcs = GSImage.wcs
    return newimg

def formatOutput(GSImage, nsecy = 2, nsecx=8):
    img = GSImage.array
    ny, nx = img.shape
    dx = int(nx/nsecx)
    dy = int(ny/nsecy)
    
    imgt = np.zeros([int(nsecx*nsecy), dy, dx])
    for iy in range(nsecy):
        for ix in range(nsecx):
            if iy % 2 == 0:
                tx = ix
            else:
                tx = (nsecx-1)-ix
            ty = iy
            chunkidx = int(tx+ty*nsecx)
            if int(chunkidx/4) == 0:
                imgt[chunkidx, :, :] = img[iy*dy:(iy+1)*dy, ix*dx:(ix+1)*dx]
            if int(chunkidx/4) == 1:
                imgt[chunkidx, :, :] = img[iy*dy:(iy+1)*dy, ix*dx:(ix+1)*dx]
            if int(chunkidx/4) == 2:
                imgt[chunkidx, :, :] = img[iy*dy:(iy+1)*dy, ix*dx:(ix+1)*dx]
            if int(chunkidx/4) == 3:
                imgt[chunkidx, :, :] = img[iy*dy:(iy+1)*dy, ix*dx:(ix+1)*dx]
    
    imgttx0 = np.hstack(imgt[ 0:4:,    :,    :])
    imgttx1 = np.hstack(imgt[ 4:8:,    :, ::-1])
    imgttx2 = np.hstack(imgt[8:12:, ::-1, ::-1])
    imgttx3 = np.hstack(imgt[12:16:,::-1,    :])
    
    newimg = galsim.Image(int(dx*nsecx*nsecy), dy, init_value=0)
    newimg.array[:, :] = np.hstack([imgttx0, imgttx1, imgttx2, imgttx3])
    return newimg
+7 −5
Original line number Original line Diff line number Diff line
@@ -9,13 +9,13 @@
# Base diretories and naming setup
# Base diretories and naming setup
# Can add some of the command-line arguments here as well;
# Can add some of the command-line arguments here as well;
# OK to pass either way or both, as long as they are consistent
# OK to pass either way or both, as long as they are consistent
work_dir: "/share/home/fangyuedong/new_sim/workplace/"
work_dir: "/share/home/weichengliang/CSST_git/test_new_sim/outputs/"
data_dir: "/share/simudata/CSSOSDataProductsSims/data/"
data_dir: "/share/simudata/CSSOSDataProductsSims/data/"
run_name: "test_new_sim"
run_name: "testRun0"


# Whether to use MPI
# Whether to use MPI
run_option:
run_option:
  use_mpi: NO
  use_mpi: YES
  # NOTE: "n_threads" paramters is currently not used in the backend
  # NOTE: "n_threads" paramters is currently not used in the backend
  # simulation codes. It should be implemented later in the web frontend
  # simulation codes. It should be implemented later in the web frontend
  # in order to config the number of threads to request from NAOC cluster
  # in order to config the number of threads to request from NAOC cluster
@@ -85,7 +85,7 @@ obs_setting:
  # if you just want to run default pointing:
  # if you just want to run default pointing:
  # - pointing_dir: null
  # - pointing_dir: null
  # - pointing_file: null
  # - pointing_file: null
  pointing_dir: "/share/home/fangyuedong/50sqdeg_pointings/"
  pointing_dir: "/share/simudata/CSSOSDataProductsSims/data/"
  pointing_file: "pointing_50_combined.dat"
  pointing_file: "pointing_50_combined.dat"


  # Number of calibration pointings
  # Number of calibration pointings
@@ -163,6 +163,7 @@ ins_effects:
  add_dark:       ON  # Whether to add dark noise
  add_dark:       ON  # Whether to add dark noise
  add_readout:    ON  # Whether to add read-out (Gaussian) noise
  add_readout:    ON  # Whether to add read-out (Gaussian) noise
  add_bias:       ON  # Whether to add bias-level to images
  add_bias:       ON  # Whether to add bias-level to images
  add_prescan:    OFF
  bias_16channel: ON  # Whether to add different biases for 16 channels
  bias_16channel: ON  # Whether to add different biases for 16 channels
  gain_16channel: ON  # Whether to make different gains for 16 channels
  gain_16channel: ON  # Whether to make different gains for 16 channels
  shutter_effect: ON  # Whether to add shutter effect
  shutter_effect: ON  # Whether to add shutter effect
@@ -177,6 +178,7 @@ ins_effects:
  add_hotpixels:  ON  # Whether to add hot pixels
  add_hotpixels:  ON  # Whether to add hot pixels
  add_deadpixels: ON  # Whether to add dead(dark) pixels
  add_deadpixels: ON  # Whether to add dead(dark) pixels
  bright_fatter:  ON  # Whether to simulate Brighter-Fatter (also diffusion) effect
  bright_fatter:  ON  # Whether to simulate Brighter-Fatter (also diffusion) effect
  format_output:  OFF


  # Values: 
  # Values: 
  # default values have been defined individually for each chip in:
  # default values have been defined individually for each chip in: