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

Merge branch 'master' into 'release_v3.0'

Release version v3.2.0

See merge request !33
parents 1b4f4012 428f2c1e
...@@ -13,13 +13,14 @@ import galsim ...@@ -13,13 +13,14 @@ import galsim
import h5py import h5py
from observation_sim.psf.PSFModel import PSFModel from observation_sim.psf.PSFModel import PSFModel
from observation_sim.psf._util import psf_extrapolate
NPSF = 900 # ***# 30*30 NPSF = 900 # ***# 30*30
PixSizeInMicrons = 5. # ***# in microns PixSizeInMicrons = 5. # ***# in microns
### find neighbors-KDtree### # find neighbors-KDtree #
def findNeighbors(tx, ty, px, py, dr=0.1, dn=1, OnlyDistance=True): def findNeighbors(tx, ty, px, py, dr=0.1, dn=1, OnlyDistance=True):
""" """
find nearest neighbors by 2D-KDTree find nearest neighbors by 2D-KDTree
...@@ -40,9 +41,9 @@ def findNeighbors(tx, ty, px, py, dr=0.1, dn=1, OnlyDistance=True): ...@@ -40,9 +41,9 @@ def findNeighbors(tx, ty, px, py, dr=0.1, dn=1, OnlyDistance=True):
dataq = [] dataq = []
rr = dr rr = dr
if OnlyDistance == True: if OnlyDistance is True:
dataq = tree.query_ball_point([tx, ty], rr) dataq = tree.query_ball_point([tx, ty], rr)
if OnlyDistance == False: if OnlyDistance is False:
while len(dataq) < dn: while len(dataq) < dn:
dataq = tree.query_ball_point([tx, ty], rr) dataq = tree.query_ball_point([tx, ty], rr)
rr += dr rr += dr
...@@ -51,7 +52,7 @@ def findNeighbors(tx, ty, px, py, dr=0.1, dn=1, OnlyDistance=True): ...@@ -51,7 +52,7 @@ def findNeighbors(tx, ty, px, py, dr=0.1, dn=1, OnlyDistance=True):
dataq = np.array(dataq)[ddSortindx[0:dn]] dataq = np.array(dataq)[ddSortindx[0:dn]]
return dataq return dataq
### find neighbors-hoclist### # find neighbors-hoclist#
def hocBuild(partx, party, nhocx, nhocy, dhocx, dhocy): def hocBuild(partx, party, nhocx, nhocy, dhocx, dhocy):
...@@ -139,7 +140,7 @@ def findNeighbors_hoclist(px, py, tx=None, ty=None, dn=4, hoc=None, hoclist=None ...@@ -139,7 +140,7 @@ def findNeighbors_hoclist(px, py, tx=None, ty=None, dn=4, hoc=None, hoclist=None
return neigh return neigh
### PSF-IDW### # PSF-IDW#
def psfMaker_IDW(px, py, PSFMat, cen_col, cen_row, IDWindex=2, OnlyNeighbors=True, hoc=None, hoclist=None, PSFCentroidWgt=False): def psfMaker_IDW(px, py, PSFMat, cen_col, cen_row, IDWindex=2, OnlyNeighbors=True, hoc=None, hoclist=None, PSFCentroidWgt=False):
""" """
psf interpolation by IDW psf interpolation by IDW
...@@ -163,7 +164,7 @@ def psfMaker_IDW(px, py, PSFMat, cen_col, cen_row, IDWindex=2, OnlyNeighbors=Tru ...@@ -163,7 +164,7 @@ def psfMaker_IDW(px, py, PSFMat, cen_col, cen_row, IDWindex=2, OnlyNeighbors=Tru
npsf = PSFMat[:, :, :].shape[0] npsf = PSFMat[:, :, :].shape[0]
psfWeight = np.zeros([npsf]) psfWeight = np.zeros([npsf])
if OnlyNeighbors == True: if OnlyNeighbors is True:
if hoc is None: if hoc is None:
neigh = findNeighbors(px, py, cen_col, cen_row, neigh = findNeighbors(px, py, cen_col, cen_row,
dr=5., dn=4, OnlyDistance=False) dr=5., dn=4, OnlyDistance=False)
...@@ -175,7 +176,7 @@ def psfMaker_IDW(px, py, PSFMat, cen_col, cen_row, IDWindex=2, OnlyNeighbors=Tru ...@@ -175,7 +176,7 @@ def psfMaker_IDW(px, py, PSFMat, cen_col, cen_row, IDWindex=2, OnlyNeighbors=Tru
neighFlag[neigh] = 1 neighFlag[neigh] = 1
for ipsf in range(npsf): for ipsf in range(npsf):
if OnlyNeighbors == True: if OnlyNeighbors is True:
if neighFlag[ipsf] != 1: if neighFlag[ipsf] != 1:
continue continue
...@@ -195,7 +196,7 @@ def psfMaker_IDW(px, py, PSFMat, cen_col, cen_row, IDWindex=2, OnlyNeighbors=Tru ...@@ -195,7 +196,7 @@ def psfMaker_IDW(px, py, PSFMat, cen_col, cen_row, IDWindex=2, OnlyNeighbors=Tru
psfMaker = np.zeros([ngy, ngx], dtype=np.float32) psfMaker = np.zeros([ngy, ngx], dtype=np.float32)
for ipsf in range(npsf): for ipsf in range(npsf):
if OnlyNeighbors == True: if OnlyNeighbors is True:
if neighFlag[ipsf] != 1: if neighFlag[ipsf] != 1:
continue continue
...@@ -208,7 +209,7 @@ def psfMaker_IDW(px, py, PSFMat, cen_col, cen_row, IDWindex=2, OnlyNeighbors=Tru ...@@ -208,7 +209,7 @@ def psfMaker_IDW(px, py, PSFMat, cen_col, cen_row, IDWindex=2, OnlyNeighbors=Tru
return psfMaker return psfMaker
### define PSFInterp### # define PSFInterp#
class PSFInterp(PSFModel): class PSFInterp(PSFModel):
def __init__(self, chip, npsf=NPSF, PSF_data=None, PSF_data_file=None, PSF_data_prefix="", sigSpin=0, psfRa=0.15, HocBuild=False, LOG_DEBUG=False): def __init__(self, chip, npsf=NPSF, PSF_data=None, PSF_data_file=None, PSF_data_prefix="", sigSpin=0, psfRa=0.15, HocBuild=False, LOG_DEBUG=False):
self.LOG_DEBUG = LOG_DEBUG self.LOG_DEBUG = LOG_DEBUG
...@@ -223,7 +224,7 @@ class PSFInterp(PSFModel): ...@@ -223,7 +224,7 @@ class PSFInterp(PSFModel):
self.iccd = int(chip.getChipLabel(chipID=chip.chipID)) self.iccd = int(chip.getChipLabel(chipID=chip.chipID))
# self.iccd = chip.chip_name # self.iccd = chip.chip_name
if PSF_data_file == None: if PSF_data_file is None:
print('Error - PSF_data_file is None') print('Error - PSF_data_file is None')
sys.exit() sys.exit()
...@@ -324,7 +325,7 @@ class PSFInterp(PSFModel): ...@@ -324,7 +325,7 @@ class PSFInterp(PSFModel):
return twave return twave
return -1 return -1
def get_PSF(self, chip, pos_img, bandpass, galsimGSObject=True, findNeighMode='treeFind', folding_threshold=5.e-3, pointing_pa=0.0): def get_PSF(self, chip, pos_img, bandpass, galsimGSObject=True, findNeighMode='treeFind', folding_threshold=5.e-3, pointing_pa=0.0, extrapolate=False, ngg=2048):
""" """
Get the PSF at a given image position Get the PSF at a given image position
...@@ -358,20 +359,18 @@ class PSFInterp(PSFModel): ...@@ -358,20 +359,18 @@ class PSFInterp(PSFModel):
imPSF = psfMaker_IDW(px, py, PSFMat, cen_col, cen_row, IDWindex=2, OnlyNeighbors=True, imPSF = psfMaker_IDW(px, py, PSFMat, cen_col, cen_row, IDWindex=2, OnlyNeighbors=True,
hoc=self.hoc[twave], hoclist=self.hoclist[twave], PSFCentroidWgt=True) hoc=self.hoc[twave], hoclist=self.hoclist[twave], PSFCentroidWgt=True)
''' if extrapolate is True:
############TEST: START ccdList = [6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 22, 23, 24, 25]
TestGaussian = False rr_trim_list = [72, 64, 96, 88, 64, 72, 72, 76, 72, 72, 76, 72, 72, 64, 88, 96, 64, 72]
if TestGaussian: imPSF = psf_extrapolate(imPSF, rr_trim=rr_trim_list[ccdList.index(chip.chipID)], ngg=ngg)
gsx = galsim.Gaussian(sigma=0.04)
#pointing_pa = -23.433333
imPSF= gsx.shear(g1=0.8, g2=0.).rotate(0.*galsim.degrees).drawImage(nx = 256, ny=256, scale=pixSize).array
############TEST: END
'''
if galsimGSObject: if galsimGSObject:
imPSFt = np.zeros([257, 257]) if extrapolate is True:
imPSFt[0:256, 0:256] = imPSF imPSFt = np.zeros([ngg+1, ngg+1])
# imPSFt[120:130, 0:256] = 1. imPSFt[:-1, :-1] = imPSF
else:
imPSFt = np.zeros([257, 257])
imPSFt[0:256, 0:256] = imPSF
img = galsim.ImageF(imPSFt, scale=pixSize) img = galsim.ImageF(imPSFt, scale=pixSize)
gsp = galsim.GSParams(folding_threshold=folding_threshold) gsp = galsim.GSParams(folding_threshold=folding_threshold)
...@@ -402,7 +401,7 @@ class PSFInterp(PSFModel): ...@@ -402,7 +401,7 @@ class PSFInterp(PSFModel):
Spinned PSF: g1, g2 and axis ratio 'a/b' Spinned PSF: g1, g2 and axis ratio 'a/b'
""" """
a2Rad = np.pi/(60.0*60.0*180.0) a2Rad = np.pi/(60.0*60.0*180.0)
ff = self.sigGauss * 0.107 * (1000.0/10.0) # in unit of [pixels] ff = self.sigGauss * 0.107 * (1000.0/10.0) # in unit of [pixels]
rc = np.sqrt(x*x + y*y) rc = np.sqrt(x*x + y*y)
cpix = rc*(self.sigSpin*a2Rad) cpix = rc*(self.sigSpin*a2Rad)
......
...@@ -23,6 +23,9 @@ from astropy.modeling.models import Gaussian2D ...@@ -23,6 +23,9 @@ from astropy.modeling.models import Gaussian2D
from scipy import signal, interpolate from scipy import signal, interpolate
import datetime import datetime
import gc import gc
from astropy.io import fits
from observation_sim.psf._util import psf_extrapolate, psf_extrapolate1
# from jax import numpy as jnp # from jax import numpy as jnp
LOG_DEBUG = False # ***# LOG_DEBUG = False # ***#
...@@ -30,7 +33,7 @@ NPSF = 900 # ***# 30*30 ...@@ -30,7 +33,7 @@ NPSF = 900 # ***# 30*30
PIX_SIZE_MICRON = 5. # ***# in microns PIX_SIZE_MICRON = 5. # ***# in microns
### find neighbors-KDtree### # find neighbors-KDtree
# def findNeighbors(tx, ty, px, py, dr=0.1, dn=1, OnlyDistance=True): # def findNeighbors(tx, ty, px, py, dr=0.1, dn=1, OnlyDistance=True):
# """ # """
# find nearest neighbors by 2D-KDTree # find nearest neighbors by 2D-KDTree
...@@ -212,7 +215,7 @@ PIX_SIZE_MICRON = 5. # ***# in microns ...@@ -212,7 +215,7 @@ PIX_SIZE_MICRON = 5. # ***# in microns
# return psfMaker # return psfMaker
### define PSFInterp### # define PSFInterp
class PSFInterpSLS(PSFModel): class PSFInterpSLS(PSFModel):
def __init__(self, chip, filt, PSF_data_prefix="", sigSpin=0, psfRa=0.15, pix_size=0.005): def __init__(self, chip, filt, PSF_data_prefix="", sigSpin=0, psfRa=0.15, pix_size=0.005):
if LOG_DEBUG: if LOG_DEBUG:
...@@ -354,7 +357,7 @@ class PSFInterpSLS(PSFModel): ...@@ -354,7 +357,7 @@ class PSFInterpSLS(PSFModel):
convImg = convImg/np.sum(convImg) convImg = convImg/np.sum(convImg)
return convImg return convImg
def get_PSF(self, chip, pos_img_local=[1000, 1000], bandNo=1, galsimGSObject=True, folding_threshold=5.e-3, g_order='A', grating_split_pos=3685): def get_PSF(self, chip, pos_img_local=[1000, 1000], bandNo=1, galsimGSObject=True, folding_threshold=5.e-3, g_order='A', grating_split_pos=3685, extrapolate=False, ngg=2048):
""" """
Get the PSF at a given image position Get the PSF at a given image position
...@@ -435,14 +438,27 @@ class PSFInterpSLS(PSFModel): ...@@ -435,14 +438,27 @@ class PSFInterpSLS(PSFModel):
# PSF_int_trans[ids_szero] = 0 # PSF_int_trans[ids_szero] = 0
# print(PSF_int_trans[ids_szero].shape[0],PSF_int_trans.shape) # print(PSF_int_trans[ids_szero].shape[0],PSF_int_trans.shape)
PSF_int_trans = PSF_int_trans/np.sum(PSF_int_trans) PSF_int_trans = PSF_int_trans/np.sum(PSF_int_trans)
###DEBGU PSF_int_trans = PSF_int_trans-np.min(PSF_int_trans)
ids_szero = PSF_int_trans<0 PSF_int_trans = PSF_int_trans/np.sum(PSF_int_trans)
# fits.writeto('/home/zhangxin/CSST_SIM/CSST_sim_develop/psf_test/psf.fits',PSF_int_trans)
# DEBGU
ids_szero = PSF_int_trans < 0
n01 = PSF_int_trans[ids_szero].shape[0] n01 = PSF_int_trans[ids_szero].shape[0]
n1 = np.sum(np.isinf(PSF_int_trans)) n1 = np.sum(np.isinf(PSF_int_trans))
n2 = np.sum(np.isnan(PSF_int_trans)) n2 = np.sum(np.isnan(PSF_int_trans))
if n1>0 or n2>0: if n1 > 0 or n2 > 0:
print("DEBUG: PSFInterpSLS, inf:%d, nan:%d, 0 num:%d"%(n1, n2, n01)) print("DEBUG: PSFInterpSLS, inf:%d, nan:%d, 0 num:%d" %
(n1, n2, n01))
if extrapolate is True:
# for rep_i in np.arange(0, 2, 1):
# PSF_int_trans[rep_i,:] = 1e9*pow(10,rep_i)
# PSF_int_trans[-1-rep_i,:] = 1e9*pow(10,rep_i)
# PSF_int_trans[:,rep_i] = 1e9*pow(10,rep_i)
# PSF_int_trans[:,-1-rep_i] = 1e9*pow(10,rep_i)
PSF_int_trans = psf_extrapolate1(PSF_int_trans, ngg=ngg)
# fits.writeto('/home/zhangxin/CSST_SIM/CSST_sim_develop/psf_test/psf_large.fits',PSF_int_trans)
#### ####
# from astropy.io import fits # from astropy.io import fits
...@@ -537,36 +553,37 @@ class PSFInterpSLS(PSFModel): ...@@ -537,36 +553,37 @@ class PSFInterpSLS(PSFModel):
sumImg = np.sum(cutImg.array) sumImg = np.sum(cutImg.array)
tmp_img = cutImg*0 tmp_img = cutImg*0
for j in np.arange(npc): for j in np.arange(npc):
X_ = np.hstack((pos_p[:,1].flatten()[:, None], pos_p[:,0].flatten()[:, None]),dtype=np.float32) X_ = np.hstack((pos_p[:, 1].flatten()[:, None], pos_p[:, 0].flatten()[
:, None]), dtype=np.float32)
Z_ = (pc_coeff[j].astype(np.float32)).flatten() Z_ = (pc_coeff[j].astype(np.float32)).flatten()
# print(pc_coeff[j].shape[0], pos_p[:,1].shape[0], pos_p[:,0].shape[0]) # print(pc_coeff[j].shape[0], pos_p[:,1].shape[0], pos_p[:,0].shape[0])
cx_len = int(chip.npix_x) cx_len = int(chip.npix_x)
cy_len = int(chip.npix_y) cy_len = int(chip.npix_y)
n_x = np.arange(0, cx_len, 1, dtype = int) n_x = np.arange(0, cx_len, 1, dtype=int)
n_y = np.arange(0, cy_len, 1, dtype = int) n_y = np.arange(0, cy_len, 1, dtype=int)
M, N = np.meshgrid(n_x, n_y) M, N = np.meshgrid(n_x, n_y)
# t1=datetime.datetime.now() # t1=datetime.datetime.now()
# U = interpolate.griddata(X_, Z_, (M[0:cy_len, 0:cx_len],N[0:cy_len, 0:cx_len]), # U = interpolate.griddata(X_, Z_, (M[0:cy_len, 0:cx_len],N[0:cy_len, 0:cx_len]),
# method='nearest',fill_value=1.0) # method='nearest',fill_value=1.0)
b_img = galsim.Image(cx_len, cy_len) b_img = galsim.Image(cx_len, cy_len)
b_img.setOrigin(0,0) b_img.setOrigin(0, 0)
bounds = cutImg.bounds & b_img.bounds bounds = cutImg.bounds & b_img.bounds
if bounds.area() == 0: if bounds.area() == 0:
continue continue
# ys = cutImg.ymin # ys = cutImg.ymin
# if ys < 0: # if ys < 0:
# ys = 0 # ys = 0
# ye = cutImg.ymin+cutImg.nrow # ye = cutImg.ymin+cutImg.nrow
# if ye >= cy_len-1: # if ye >= cy_len-1:
# ye = cy_len-1 # ye = cy_len-1
# if ye - ys <=0: # if ye - ys <=0:
# continue # continue
# xs = cutImg.xmin # xs = cutImg.xmin
# if xs < 0: # if xs < 0:
# xs = 0 # xs = 0
# xe = cutImg.xmin+cutImg.ncol # xe = cutImg.xmin+cutImg.ncol
# if xe >= cx_len-1: # if xe >= cx_len-1:
# xe = cx_len-1 # xe = cx_len-1
# if xe - xs <=0: # if xe - xs <=0:
# continue # continue
...@@ -574,10 +591,10 @@ class PSFInterpSLS(PSFModel): ...@@ -574,10 +591,10 @@ class PSFInterpSLS(PSFModel):
ye = bounds.ymax+1 ye = bounds.ymax+1
xs = bounds.xmin xs = bounds.xmin
xe = bounds.xmax+1 xe = bounds.xmax+1
U = interpolate.griddata(X_, Z_, (M[ys:ye, xs:xe],N[ys:ye, xs:xe]), U = interpolate.griddata(X_, Z_, (M[ys:ye, xs:xe], N[ys:ye, xs:xe]),
method='nearest',fill_value=1.0) method='nearest', fill_value=1.0)
# t2=datetime.datetime.now() # t2=datetime.datetime.now()
# print("time interpolate:", t2-t1) # print("time interpolate:", t2-t1)
# if U.shape != cutImg.array.shape: # if U.shape != cutImg.array.shape:
...@@ -586,53 +603,53 @@ class PSFInterpSLS(PSFModel): ...@@ -586,53 +603,53 @@ class PSFInterpSLS(PSFModel):
img_tmp = cutImg img_tmp = cutImg
img_tmp[bounds] = img_tmp[bounds]*U img_tmp[bounds] = img_tmp[bounds]*U
psf = pcs[:, j].reshape(m_size, m_size) psf = pcs[:, j].reshape(m_size, m_size)
tmp_img = tmp_img + signal.fftconvolve(img_tmp.array, psf, mode='same', axes=None) tmp_img = tmp_img + \
signal.fftconvolve(img_tmp.array, psf, mode='same', axes=None)
# t3=datetime.datetime.now() # t3=datetime.datetime.now()
# print("time convole:", t3-t2) # print("time convole:", t3-t2)
del U del U
del img_tmp del img_tmp
if np.sum(tmp_img.array)==0: if np.sum(tmp_img.array) == 0:
tmp_img = cutImg tmp_img = cutImg
else: else:
tmp_img = tmp_img/np.sum(tmp_img.array)*sumImg tmp_img = tmp_img/np.sum(tmp_img.array)*sumImg
return tmp_img return tmp_img
def convolveFullImgWithPCAPSF(self, chip, folding_threshold=5.e-3): def convolveFullImgWithPCAPSF(self, chip, folding_threshold=5.e-3):
keys_L1= chip_utils.getChipSLSGratingID(chip.chipID) keys_L1 = chip_utils.getChipSLSGratingID(chip.chipID)
# keys_L2 = ['order-2','order-1','order0','order1','order2'] # keys_L2 = ['order-2','order-1','order0','order1','order2']
keys_L2 = ['order0','order1'] keys_L2 = ['order0', 'order1']
keys_L3 = ['w1','w2','w3','w4'] keys_L3 = ['w1', 'w2', 'w3', 'w4']
npca = 10 npca = 10
x_start = chip.x_cen/chip.pix_size - chip.npix_x / 2. x_start = chip.x_cen/chip.pix_size - chip.npix_x / 2.
y_start = chip.y_cen/chip.pix_size - chip.npix_y / 2. y_start = chip.y_cen/chip.pix_size - chip.npix_y / 2.
for i,gt in enumerate(keys_L1): for i, gt in enumerate(keys_L1):
psfCo = self.grating1_data psfCo = self.grating1_data
if i > 0: if i > 0:
psfCo = self.grating2_data psfCo = self.grating2_data
for od in keys_L2: for od in keys_L2:
psfCo_L2 = psfCo['order1'] psfCo_L2 = psfCo['order1']
if od in ['order-2','order-1','order0','order2']: if od in ['order-2', 'order-1', 'order0', 'order2']:
psfCo_L2 = psfCo['order0'] psfCo_L2 = psfCo['order0']
for w in keys_L3: for w in keys_L3:
img = chip.img_stack[gt][od][w] img = chip.img_stack[gt][od][w]
pcs = psfCo_L2['band'+w[1]]['band_data'][0].data pcs = psfCo_L2['band'+w[1]]['band_data'][0].data
pos_p = psfCo_L2['band'+w[1]]['band_data'][1].data/chip.pix_size - np.array([y_start, x_start]) pos_p = psfCo_L2['band'+w[1]]['band_data'][1].data / \
chip.pix_size - np.array([y_start, x_start])
pc_coeff = psfCo_L2['band'+w[1]]['band_data'][2].data pc_coeff = psfCo_L2['band'+w[1]]['band_data'][2].data
# print("DEBUG-----------",np.max(pos_p[:,1]),np.min(pos_p[:,1]), np.max(pos_p[:,0]),np.min(pos_p[:,0])) # print("DEBUG-----------",np.max(pos_p[:,1]),np.min(pos_p[:,1]), np.max(pos_p[:,0]),np.min(pos_p[:,0]))
sum_img = np.sum(img.array) sum_img = np.sum(img.array)
# coeff_mat = np.zeros([npca, chip.npix_y, chip.npix_x]) # coeff_mat = np.zeros([npca, chip.npix_y, chip.npix_x])
# for m in np.arange(chip.npix_y): # for m in np.arange(chip.npix_y):
# for n in np.arange(chip.npix_x): # for n in np.arange(chip.npix_x):
# px = n # px = n
# py = m # py = m
# dist2 = (pos_p[:, 1] - px)*(pos_p[:, 1] - px) + (pos_p[:, 0] - py)*(pos_p[:, 0] - py) # dist2 = (pos_p[:, 1] - px)*(pos_p[:, 1] - px) + (pos_p[:, 0] - py)*(pos_p[:, 0] - py)
# temp_sort_dist = np.zeros([dist2.shape[0], 2]) # temp_sort_dist = np.zeros([dist2.shape[0], 2])
# temp_sort_dist[:, 0] = np.arange(0, dist2.shape[0], 1) # temp_sort_dist[:, 0] = np.arange(0, dist2.shape[0], 1)
...@@ -660,41 +677,45 @@ class PSFInterpSLS(PSFModel): ...@@ -660,41 +677,45 @@ class PSFInterpSLS(PSFModel):
# coeff_mat[:, m, n] = coeff_int # coeff_mat[:, m, n] = coeff_int
m_size = int(pcs.shape[0]**0.5) m_size = int(pcs.shape[0]**0.5)
tmp_img = np.zeros_like(img.array,dtype=np.float32) tmp_img = np.zeros_like(img.array, dtype=np.float32)
for j in np.arange(npca): for j in np.arange(npca):
print(gt, od, w, j) print(gt, od, w, j)
X_ = np.hstack((pos_p[:,1].flatten()[:, None], pos_p[:,0].flatten()[:, None]),dtype=np.float32) X_ = np.hstack((pos_p[:, 1].flatten()[:, None], pos_p[:, 0].flatten()[
:, None]), dtype=np.float32)
Z_ = (pc_coeff[j].astype(np.float32)).flatten() Z_ = (pc_coeff[j].astype(np.float32)).flatten()
# print(pc_coeff[j].shape[0], pos_p[:,1].shape[0], pos_p[:,0].shape[0]) # print(pc_coeff[j].shape[0], pos_p[:,1].shape[0], pos_p[:,0].shape[0])
sub_size = 4 sub_size = 4
cx_len = int(chip.npix_x/sub_size) cx_len = int(chip.npix_x/sub_size)
cy_len = int(chip.npix_y/sub_size) cy_len = int(chip.npix_y/sub_size)
n_x = np.arange(0, chip.npix_x, sub_size, dtype = int) n_x = np.arange(0, chip.npix_x, sub_size, dtype=int)
n_y = np.arange(0, chip.npix_y, sub_size, dtype = int) n_y = np.arange(0, chip.npix_y, sub_size, dtype=int)
M, N = np.meshgrid(n_x, n_y) M, N = np.meshgrid(n_x, n_y)
t1=datetime.datetime.now() t1 = datetime.datetime.now()
# U = interpolate.griddata(X_, Z_, (M[0:cy_len, 0:cx_len],N[0:cy_len, 0:cx_len]), # U = interpolate.griddata(X_, Z_, (M[0:cy_len, 0:cx_len],N[0:cy_len, 0:cx_len]),
# method='nearest',fill_value=1.0) # method='nearest',fill_value=1.0)
U1 = interpolate.griddata(X_, Z_, (M, N), U1 = interpolate.griddata(X_, Z_, (M, N),
method='nearest',fill_value=1.0) method='nearest', fill_value=1.0)
U = np.zeros_like(chip.img.array, dtype=np.float32) U = np.zeros_like(chip.img.array, dtype=np.float32)
for mi in np.arange(cy_len): for mi in np.arange(cy_len):
for mj in np.arange(cx_len): for mj in np.arange(cx_len):
U[mi*sub_size:(mi+1)*sub_size, mj*sub_size:(mj+1)*sub_size]=U1[mi,mj] U[mi*sub_size:(mi+1)*sub_size, mj *
t2=datetime.datetime.now() sub_size:(mj+1)*sub_size] = U1[mi, mj]
t2 = datetime.datetime.now()
print("time interpolate:", t2-t1) print("time interpolate:", t2-t1)
img_tmp = img.array*U img_tmp = img.array*U
psf = pcs[:, j].reshape(m_size, m_size) psf = pcs[:, j].reshape(m_size, m_size)
tmp_img = tmp_img + signal.fftconvolve(img_tmp, psf, mode='same', axes=None) tmp_img = tmp_img + \
signal.fftconvolve(
img_tmp, psf, mode='same', axes=None)
t3=datetime.datetime.now() t3 = datetime.datetime.now()
print("time convole:", t3-t2) print("time convole:", t3-t2)
del U del U
del U1 del U1
chip.img = chip.img + tmp_img*sum_img/np.sum(tmp_img) chip.img = chip.img + tmp_img*sum_img/np.sum(tmp_img)
del tmp_img del tmp_img
gc.collect() gc.collect()
......
...@@ -3,4 +3,4 @@ from .PSFGauss import PSFGauss ...@@ -3,4 +3,4 @@ from .PSFGauss import PSFGauss
# from .PSFInterp.PSFInterp import PSFInterp # from .PSFInterp.PSFInterp import PSFInterp
from .PSFInterp import PSFInterp from .PSFInterp import PSFInterp
from .PSFInterpSLS import PSFInterpSLS from .PSFInterpSLS import PSFInterpSLS
from .FieldDistortion import FieldDistortion from .FieldDistortion import FieldDistortion
\ No newline at end of file
import numpy as np
from scipy.interpolate import interp1d
def binningPSF(img, ngg):
imgX = img.reshape(ngg, img.shape[0]//ngg, ngg, img.shape[1]//ngg).mean(-1).mean(1)
return imgX
def radial_average_at_pixel(image, center_x, center_y, dr=10):
# Get coordinates relative to the specified center pixel (x, y)
y, x = np.indices(image.shape)
r = np.sqrt((x - center_x)**2 + (y - center_y)**2)
# Set up bins
max_radius = int(r.max()) # Maximum distance from the center pixel
radial_bins = np.arange(0, max_radius, dr)
# Compute average value in each bin
radial_means = []
for i in range(len(radial_bins) - 1):
mask = (r >= radial_bins[i]) & (r < radial_bins[i + 1])
if np.any(mask):
radial_means.append(image[mask].mean())
else:
radial_means.append(0) # In case no pixels are in the bin
return radial_bins[:-1], radial_means # Exclude last bin since no mean calculated
def psf_extrapolate(psf, rr_trim=64, ngg=256):
# ngg = 256
# extrapolate PSF
if True:
xim = np.arange(256)-128
xim, yim = np.meshgrid(xim, xim)
rim = np.sqrt(xim**2 + yim**2)
# rr_trim = 96
psf_temp = psf
psf_temp[rim > rr_trim] = 0
radii, means = radial_average_at_pixel(psf_temp, 128, 128, dr=4)
radii_log = np.log(radii[1:])
means_log = np.log(means[1:])
finite_mask = np.isfinite(means_log)
f_interp = interp1d(radii_log[finite_mask][:-1], means_log[finite_mask][:-1], kind='linear', fill_value="extrapolate")
# ngg = 1024
xim = np.arange(ngg)-int(ngg/2)
xim, yim = np.meshgrid(xim, xim)
rim = np.sqrt(xim**2 + yim**2)
rim[int(ngg/2), int(ngg/2)] = np.finfo(float).eps # 1e-7
rim_log = np.log(rim)
y_new = f_interp(rim_log)
arr = np.zeros([ngg, ngg])
arr[int(ngg/2-128):int(ngg/2+128), int(ngg/2-128):int(ngg/2+128)] = np.log(psf_temp + np.finfo(float).eps)
arr[rim > rr_trim] = 0
arr[arr == 0] = y_new[arr == 0]
psf = np.exp(arr)
psf[rim > int(ngg/2)] = 0
imPSF = psf # binningPSF(psf, int(ngg/2))
imPSF = imPSF/np.nansum(imPSF)
return imPSF
def psf_extrapolate1(psf, rr_trim=64, ngg=256):
# ngg = 256
# extrapolate PSF
if True:
psf_enlar = np.zeros([ngg, ngg])
psf_enlar[int(ngg/2-128):int(ngg/2+128), int(ngg/2-128):int(ngg/2+128)] = psf
xim = np.arange(ngg)-ngg/2
xim, yim = np.meshgrid(xim, xim)
rim = np.sqrt(xim**2 + yim**2)
psf_temp = psf_enlar
# psf_temp[rim >= rr_trim] = 0
psf_temp[rim >= ngg/2-2] = np.finfo(float).eps
radii, means = radial_average_at_pixel(psf_temp, ngg/2, ngg/2, dr=2)
radii_log = np.log(radii[1:])
# radii_log = radii[1:]
means_log = np.log(means[1:])
# xim = np.arange(256)-128
# xim, yim = np.meshgrid(xim, xim)
# rim = np.sqrt(xim**2 + yim**2)
# # rr_trim = 96
# psf_temp = psf
# psf_temp[rim > rr_trim] = 0
# radii, means = radial_average_at_pixel(psf_temp, 128, 128, dr=4)
# radii_log = np.log10(radii[1:])
# # radii_log = radii[1:]
# means_log = np.log10(means[1:])
finite_mask = np.isfinite(means_log)
f_interp = interp1d(radii_log[finite_mask][:-1], means_log[finite_mask][:-1], kind='linear', fill_value="extrapolate")
# ngg = 1024
# xim = np.arange(ngg)-int(ngg/2)
# xim, yim = np.meshgrid(xim, xim)
# rim = np.sqrt(xim**2 + yim**2)
# rim[int(ngg/2), int(ngg/2)] = np.finfo(float).eps # 1e-7
rim_log = np.log(rim)
y_new = f_interp(rim_log)
arr = np.zeros([ngg, ngg])
arr[int(ngg/2-128):int(ngg/2+128), int(ngg/2-128):int(ngg/2+128)] = np.log(psf + np.finfo(float).eps)
arr[rim > 128-2] = 0
arr[arr == 0] = y_new[arr == 0]
psf_n = np.exp(arr)
# psf_n[int(ngg/2-128):int(ngg/2+128), int(ngg/2-128):int(ngg/2+128)] = psf
# psf[rim > int(ngg/2)] = 0
imPSF = psf_n # binningPSF(psf, int(ngg/2))
# imPSF = imPSF/np.nansum(imPSF)
return imPSF
...@@ -11,11 +11,11 @@ class SimSteps: ...@@ -11,11 +11,11 @@ class SimSteps:
from .prepare_headers import prepare_headers, updateHeaderInfo from .prepare_headers import prepare_headers, updateHeaderInfo
from .add_sky_background import add_sky_background_sci, add_sky_flat_calibration, add_sky_background from .add_sky_background import add_sky_background_sci, add_sky_flat_calibration, add_sky_background
from .add_objects import add_objects from .add_objects import add_objects, _is_obj_valid
from .add_cosmic_rays import add_cosmic_rays from .add_cosmic_rays import add_cosmic_rays
from .add_pattern_noise import apply_PRNU, add_poisson_and_dark, add_detector_defects, add_nonlinearity, add_blooming, add_bias from .add_pattern_noise import apply_PRNU, add_poisson_and_dark, add_detector_defects, add_nonlinearity, add_blooming, add_bias
from .add_brighter_fatter_CTE import add_brighter_fatter, apply_CTE from .add_brighter_fatter_CTE import add_brighter_fatter, apply_CTE
from .readout_output import add_prescan_overscan, add_readout_noise, apply_gain, quantization_and_output from .readout_output import add_prescan_overscan, add_readout_noise, apply_gain, quantization_and_output, add_crosstalk
from .add_LED_flat import add_LED_Flat from .add_LED_flat import add_LED_Flat
...@@ -37,4 +37,5 @@ SIM_STEP_TYPES = { ...@@ -37,4 +37,5 @@ SIM_STEP_TYPES = {
"quantization_and_output": "quantization_and_output", "quantization_and_output": "quantization_and_output",
"led_calib_model": "add_LED_Flat", "led_calib_model": "add_LED_Flat",
"sky_flatField": "add_sky_flat_calibration", "sky_flatField": "add_sky_flat_calibration",
"cross_talk": "add_crosstalk"
} }
...@@ -27,7 +27,7 @@ def add_LED_Flat(self, chip, filt, tel, pointing, catalog, obs_param): ...@@ -27,7 +27,7 @@ def add_LED_Flat(self, chip, filt, tel, pointing, catalog, obs_param):
self.updateHeaderInfo(header_flag='ext', keys=['LEDT01', 'LEDT02', 'LEDT03', 'LEDT04', 'LEDT05', 'LEDT06', self.updateHeaderInfo(header_flag='ext', keys=['LEDT01', 'LEDT02', 'LEDT03', 'LEDT04', 'LEDT05', 'LEDT06',
'LEDT07', 'LEDT08', 'LEDT09', 'LEDT10', 'LEDT11', 'LEDT12', 'LEDT13', 'LEDT14'], values=letts) 'LEDT07', 'LEDT08', 'LEDT09', 'LEDT10', 'LEDT11', 'LEDT12', 'LEDT13', 'LEDT14'], values=letts)
if obs_param["shutter_effect"] == True: if obs_param["shutter_effect"] is True:
pf_map = pf_map * chip.shutter_img pf_map = pf_map * chip.shutter_img
pf_map = np.array(pf_map, dtype='float32') pf_map = np.array(pf_map, dtype='float32')
self.updateHeaderInfo(header_flag='ext', keys=[ self.updateHeaderInfo(header_flag='ext', keys=[
......
...@@ -11,6 +11,24 @@ from astropy.time import Time ...@@ -11,6 +11,24 @@ from astropy.time import Time
from datetime import datetime, timezone from datetime import datetime, timezone
def _is_obj_valid(self, obj):
if obj.param['star'] == 4:
# Currently there's no parameter checks for 'calib' type
return True
pos_keys = ['ra', 'dec']
shape_keys = ['hlr_bulge', 'hlr_disk',
'e1_disk', 'e2_disk', 'e1_bulge', 'e2_bulge']
if any(obj.param[key] == -999. for key in pos_keys):
msg = 'One or more positional information (ra, dec) is missing'
self.chip_output.Log_error(msg)
return False
if obj.param['star'] == 0 and any(obj.param[key] == -999. for key in shape_keys):
msg = 'One or more shape information (hlr_bulge, hlr_disk, e1_disk, e2_disk, e1_bulge, e2_bulge) is missing'
self.chip_output.Log_error(msg)
return False
return True
def add_objects(self, chip, filt, tel, pointing, catalog, obs_param): def add_objects(self, chip, filt, tel, pointing, catalog, obs_param):
# Get exposure time # Get exposure time
...@@ -48,7 +66,7 @@ def add_objects(self, chip, filt, tel, pointing, catalog, obs_param): ...@@ -48,7 +66,7 @@ def add_objects(self, chip, filt, tel, pointing, catalog, obs_param):
self.chip_output.Log_error("unrecognized PSF model type!!", flush=True) self.chip_output.Log_error("unrecognized PSF model type!!", flush=True)
# Apply field distortion model # Apply field distortion model
if obs_param["field_dist"] == True: if obs_param["field_dist"] is True:
fd_model = FieldDistortion(chip=chip, img_rot=pointing.img_pa.deg) fd_model = FieldDistortion(chip=chip, img_rot=pointing.img_pa.deg)
else: else:
fd_model = None fd_model = None
...@@ -58,7 +76,10 @@ def add_objects(self, chip, filt, tel, pointing, catalog, obs_param): ...@@ -58,7 +76,10 @@ def add_objects(self, chip, filt, tel, pointing, catalog, obs_param):
for ifilt in range(len(self.all_filters)): for ifilt in range(len(self.all_filters)):
temp_filter = self.all_filters[ifilt] temp_filter = self.all_filters[ifilt]
temp_filter.update_limit_saturation_mags( temp_filter.update_limit_saturation_mags(
exptime=pointing.get_full_depth_exptime(temp_filter.filter_type), chip=chip) exptime=pointing.exp_time,
full_depth_exptime=pointing.get_full_depth_exptime(
temp_filter.filter_type),
chip=chip)
if temp_filter.filter_type.lower() == self.overall_config["obs_setting"]["cut_in_band"].lower(): if temp_filter.filter_type.lower() == self.overall_config["obs_setting"]["cut_in_band"].lower():
cut_filter = temp_filter cut_filter = temp_filter
...@@ -83,12 +104,17 @@ def add_objects(self, chip, filt, tel, pointing, catalog, obs_param): ...@@ -83,12 +104,17 @@ def add_objects(self, chip, filt, tel, pointing, catalog, obs_param):
# # [DEBUG] [TODO] # # [DEBUG] [TODO]
# if j >= 10: # if j >= 10:
# break # break
obj = cat.objs[j] obj = cat.objs[j]
if not self._is_obj_valid(obj):
continue
# load and convert SED; also caculate object's magnitude in all CSST bands # load and convert SED; also caculate object's magnitude in all CSST bands
try: try:
sed_data = cat.load_sed(obj) sed_data = cat.load_sed(obj)
norm_filt = cat.load_norm_filt(obj) norm_filt = cat.load_norm_filt(obj)
obj.sed, obj.param["mag_%s" % filt.filter_type.lower()], obj.param["flux_%s" % filt.filter_type.lower()] = cat.convert_sed( obj.sed, obj.param["mag_%s" % filt.filter_type.lower()], obj.param["flux_%s" % filt.filter_type.lower()] = cat.convert_sed(
mag=obj.param["mag_use_normal"], mag=obj.param["mag_use_normal"],
sed=sed_data, sed=sed_data,
...@@ -96,11 +122,12 @@ def add_objects(self, chip, filt, tel, pointing, catalog, obs_param): ...@@ -96,11 +122,12 @@ def add_objects(self, chip, filt, tel, pointing, catalog, obs_param):
norm_filt=norm_filt, norm_filt=norm_filt,
mu=obj.mu mu=obj.mu
) )
_, obj.param["mag_%s" % cut_filter.filter_type.lower()], obj.param["flux_%s" % cut_filter.filter_type.lower()] = cat.convert_sed( _, obj.param["mag_%s" % cut_filter.filter_type.lower()], obj.param["flux_%s" % cut_filter.filter_type.lower()] = cat.convert_sed(
mag=obj.param["mag_use_normal"], mag=obj.param["mag_use_normal"],
sed=sed_data, sed=sed_data,
target_filt=cut_filter, target_filt=cut_filter,
norm_filt=norm_filt, norm_filt=(norm_filt if norm_filt else filt),
mu=obj.mu mu=obj.mu
) )
except Exception as e: except Exception as e:
...@@ -116,7 +143,7 @@ def add_objects(self, chip, filt, tel, pointing, catalog, obs_param): ...@@ -116,7 +143,7 @@ def add_objects(self, chip, filt, tel, pointing, catalog, obs_param):
mag=obj.param["mag_%s" % mag=obj.param["mag_%s" %
self.overall_config["obs_setting"]["cut_in_band"].lower()], self.overall_config["obs_setting"]["cut_in_band"].lower()],
margin=self.overall_config["obs_setting"]["mag_sat_margin"]): margin=self.overall_config["obs_setting"]["mag_sat_margin"]):
self.chip_output.Log_info("obj %s too birght!! mag_%s = %.3f" % ( self.chip_output.Log_info("obj %s too bright!! mag_%s = %.3f" % (
obj.id, cut_filter.filter_type, obj.param["mag_%s" % self.overall_config["obs_setting"]["cut_in_band"].lower()])) obj.id, cut_filter.filter_type, obj.param["mag_%s" % self.overall_config["obs_setting"]["cut_in_band"].lower()]))
bright_obj += 1 bright_obj += 1
obj.unload_SED() obj.unload_SED()
...@@ -150,10 +177,11 @@ def add_objects(self, chip, filt, tel, pointing, catalog, obs_param): ...@@ -150,10 +177,11 @@ def add_objects(self, chip, filt, tel, pointing, catalog, obs_param):
# [TODO] For now, only consider objects which their centers (after field distortion) are projected within the focal plane # [TODO] For now, only consider objects which their centers (after field distortion) are projected within the focal plane
# Otherwise they will be considered missed objects # Otherwise they will be considered missed objects
# if pos_img.x == -1 or pos_img.y == -1 or (not chip.isContainObj(x_image=pos_img.x, y_image=pos_img.y, margin=0.)): # if pos_img.x == -1 or pos_img.y == -1 or (not chip.isContainObj(x_image=pos_img.x, y_image=pos_img.y, margin=0.)):
if pos_img.x == -1 or pos_img.y == -1: # if pos_img.x == -1 or pos_img.y == -1:
if pos_img is None:
self.chip_output.Log_info('obj_ra = %.6f, obj_dec = %.6f, obj_ra_orig = %.6f, obj_dec_orig = %.6f' % ( self.chip_output.Log_info('obj_ra = %.6f, obj_dec = %.6f, obj_ra_orig = %.6f, obj_dec_orig = %.6f' % (
obj.ra, obj.dec, obj.ra_orig, obj.dec_orig)) obj.ra, obj.dec, obj.ra_orig, obj.dec_orig))
self.chip_output.Log_error("Objected missed: %s" % (obj.id)) self.chip_output.Log_error("Object missed: %s" % (obj.id))
missed_obj += 1 missed_obj += 1
obj.unload_SED() obj.unload_SED()
continue continue
...@@ -199,7 +227,7 @@ def add_objects(self, chip, filt, tel, pointing, catalog, obs_param): ...@@ -199,7 +227,7 @@ def add_objects(self, chip, filt, tel, pointing, catalog, obs_param):
pass pass
elif isUpdated == 0: elif isUpdated == 0:
missed_obj += 1 missed_obj += 1
self.chip_output.Log_error("Objected missed: %s" % (obj.id)) self.chip_output.Log_error("Object missed: %s" % (obj.id))
else: else:
self.chip_output.Log_error( self.chip_output.Log_error(
"Draw error, object omitted: %s" % (obj.id)) "Draw error, object omitted: %s" % (obj.id))
...@@ -213,11 +241,12 @@ def add_objects(self, chip, filt, tel, pointing, catalog, obs_param): ...@@ -213,11 +241,12 @@ def add_objects(self, chip, filt, tel, pointing, catalog, obs_param):
self.chip_output.Log_error("obj id: %s" % (obj.param['id'])) self.chip_output.Log_error("obj id: %s" % (obj.param['id']))
self.chip_output.Log_error(" e1: %.5f\n e2: %.5f\n size: %f\n bfrac: %f\n detA: %f\n g1: %.5f\n g2: %.5f\n" % ( self.chip_output.Log_error(" e1: %.5f\n e2: %.5f\n size: %f\n bfrac: %f\n detA: %f\n g1: %.5f\n g2: %.5f\n" % (
obj.param['e1'], obj.param['e2'], obj.param['size'], obj.param['bfrac'], obj.param['detA'], obj.param['g1'], obj.param['g2'])) obj.param['e1'], obj.param['e2'], obj.param['size'], obj.param['bfrac'], obj.param['detA'], obj.param['g1'], obj.param['g2']))
# Unload SED: # Unload SED:
obj.unload_SED() obj.unload_SED()
del obj del obj
# gc.collect() # gc.collect()
cat.free_mem()
del cat
if chip.survey_type == "spectroscopic" and not self.overall_config["run_option"]["out_cat_only"] and chip.slsPSFOptim: if chip.survey_type == "spectroscopic" and not self.overall_config["run_option"]["out_cat_only"] and chip.slsPSFOptim:
# from observation_sim.instruments.chip import chip_utils as chip_utils # from observation_sim.instruments.chip import chip_utils as chip_utils
...@@ -253,10 +282,10 @@ def add_objects(self, chip, filt, tel, pointing, catalog, obs_param): ...@@ -253,10 +282,10 @@ def add_objects(self, chip, filt, tel, pointing, catalog, obs_param):
# Apply flat fielding (with shutter effects) # Apply flat fielding (with shutter effects)
flat_normal = np.ones_like(chip.img.array) flat_normal = np.ones_like(chip.img.array)
if obs_param["flat_fielding"] == True: if obs_param["flat_fielding"] is True:
flat_normal = flat_normal * chip.flat_img.array / \ flat_normal = flat_normal * chip.flat_img.array / \
np.mean(chip.flat_img.array) np.mean(chip.flat_img.array)
if obs_param["shutter_effect"] == True: if obs_param["shutter_effect"] is True:
flat_normal = flat_normal * chip.shutter_img flat_normal = flat_normal * chip.shutter_img
flat_normal = np.array(flat_normal, dtype='float32') flat_normal = np.array(flat_normal, dtype='float32')
self.updateHeaderInfo(header_flag='ext', keys=[ self.updateHeaderInfo(header_flag='ext', keys=[
......
...@@ -5,7 +5,7 @@ from observation_sim.instruments.chip import effects ...@@ -5,7 +5,7 @@ from observation_sim.instruments.chip import effects
def apply_PRNU(self, chip, filt, tel, pointing, catalog, obs_param): def apply_PRNU(self, chip, filt, tel, pointing, catalog, obs_param):
chip.img *= chip.prnu_img chip.img *= chip.prnu_img
if self.overall_config["output_setting"]["prnu_output"] == True: if self.overall_config["output_setting"]["prnu_output"] is True:
chip.prnu_img.write("%s/FlatImg_PRNU_%s.fits" % chip.prnu_img.write("%s/FlatImg_PRNU_%s.fits" %
(self.chip_output.subdir, str(chip.chipID).rjust(2, '0'))) (self.chip_output.subdir, str(chip.chipID).rjust(2, '0')))
return chip, filt, tel, pointing return chip, filt, tel, pointing
...@@ -19,10 +19,10 @@ def add_poisson_and_dark(self, chip, filt, tel, pointing, catalog, obs_param): ...@@ -19,10 +19,10 @@ def add_poisson_and_dark(self, chip, filt, tel, pointing, catalog, obs_param):
else: else:
exptime = pointing.exp_time exptime = pointing.exp_time
if obs_param["add_dark"] == True: if obs_param["add_dark"] is True:
chip.img, _ = chip_utils.add_poisson(img=chip.img, chip.img, _ = chip_utils.add_poisson(img=chip.img,
chip=chip, chip=chip,
exptime=pointing.exp_time, exptime=exptime,
poisson_noise=chip.poisson_noise, poisson_noise=chip.poisson_noise,
InputDark=None) InputDark=None)
else: else:
...@@ -46,7 +46,7 @@ def add_detector_defects(self, chip, filt, tel, pointing, catalog, obs_param): ...@@ -46,7 +46,7 @@ def add_detector_defects(self, chip, filt, tel, pointing, catalog, obs_param):
fraction=badfraction, fraction=badfraction,
seed=self.overall_config["random_seeds"]["seed_defective"]+chip.chipID, biaslevel=0) seed=self.overall_config["random_seeds"]["seed_defective"]+chip.chipID, biaslevel=0)
# Apply Bad columns # Apply Bad columns
if obs_param["bad_columns"] == True: if obs_param["bad_columns"] is True:
chip.img = effects.BadColumns(chip.img, chip.img = effects.BadColumns(chip.img,
seed=self.overall_config["random_seeds"]["seed_badcolumns"], seed=self.overall_config["random_seeds"]["seed_badcolumns"],
chipid=chip.chipID) chipid=chip.chipID)
...@@ -73,13 +73,13 @@ def add_blooming(self, chip, filt, tel, pointing, catalog, obs_param): ...@@ -73,13 +73,13 @@ def add_blooming(self, chip, filt, tel, pointing, catalog, obs_param):
def add_bias(self, chip, filt, tel, pointing, catalog, obs_param): def add_bias(self, chip, filt, tel, pointing, catalog, obs_param):
self.chip_output.Log_info( self.chip_output.Log_info(
" Adding Bias level and 16-channel non-uniformity") " Adding Bias level and 16-channel non-uniformity")
if obs_param["bias_16channel"] == True: if obs_param["bias_16channel"] is True:
chip.img = effects.AddBiasNonUniform16(chip.img, chip.img = effects.AddBiasNonUniform16(chip.img,
bias_level=float( bias_level=float(
chip.bias_level), chip.bias_level),
nsecy=chip.nsecy, nsecy=chip.nsecy,
nsecx=chip.nsecx, nsecx=chip.nsecx,
seed=self.overall_config["random_seeds"]["seed_biasNonUniform"]+chip.chipID) seed=self.overall_config["random_seeds"]["seed_biasNonUniform"]+chip.chipID)
elif obs_param["bias_16channel"] == False: elif obs_param["bias_16channel"] is False:
chip.img += chip.bias_level chip.img += chip.bias_level
return chip, filt, tel, pointing return chip, filt, tel, pointing
...@@ -16,10 +16,10 @@ def add_sky_background_sci(self, chip, filt, tel, pointing, catalog, obs_param): ...@@ -16,10 +16,10 @@ def add_sky_background_sci(self, chip, filt, tel, pointing, catalog, obs_param):
exptime = pointing.exp_time exptime = pointing.exp_time
flat_normal = np.ones_like(chip.img.array) flat_normal = np.ones_like(chip.img.array)
if obs_param["flat_fielding"] == True: if obs_param["flat_fielding"] is True:
flat_normal = flat_normal * chip.flat_img.array / \ flat_normal = flat_normal * chip.flat_img.array / \
np.mean(chip.flat_img.array) np.mean(chip.flat_img.array)
if obs_param["shutter_effect"] == True: if obs_param["shutter_effect"] is True:
flat_normal = flat_normal * chip.shutter_img flat_normal = flat_normal * chip.shutter_img
flat_normal = np.array(flat_normal, dtype='float32') flat_normal = np.array(flat_normal, dtype='float32')
self.updateHeaderInfo(header_flag='ext', keys=[ self.updateHeaderInfo(header_flag='ext', keys=[
...@@ -81,14 +81,14 @@ def add_sky_flat_calibration(self, chip, filt, tel, pointing, catalog, obs_param ...@@ -81,14 +81,14 @@ def add_sky_flat_calibration(self, chip, filt, tel, pointing, catalog, obs_param
norm_scaler = skyback_level/exptime / filter_param.param[sky_level_filt][5] norm_scaler = skyback_level/exptime / filter_param.param[sky_level_filt][5]
flat_normal = np.ones_like(chip.img.array) flat_normal = np.ones_like(chip.img.array)
if obs_param["flat_fielding"] == True: if obs_param["flat_fielding"] is True:
flat_normal = flat_normal * chip.flat_img.array / \ flat_normal = flat_normal * chip.flat_img.array / \
np.mean(chip.flat_img.array) np.mean(chip.flat_img.array)
if obs_param["shutter_effect"] == True: if obs_param["shutter_effect"] is True:
flat_normal = flat_normal * chip.shutter_img flat_normal = flat_normal * chip.shutter_img
flat_normal = np.array(flat_normal, dtype='float32') flat_normal = np.array(flat_normal, dtype='float32')
# output 16-bit shutter effect image with pixel value <=65535 # output 16-bit shutter effect image with pixel value <=65535
if self.overall_config["output_setting"]["shutter_output"] == True: if self.overall_config["output_setting"]["shutter_output"] is True:
shutt_gsimg = galsim.ImageUS(chip.shutter_img*6E4) shutt_gsimg = galsim.ImageUS(chip.shutter_img*6E4)
shutt_gsimg.write("%s/ShutterEffect_%s_1.fits" % shutt_gsimg.write("%s/ShutterEffect_%s_1.fits" %
(self.chip_output.subdir, str(chip.chipID).rjust(2, '0'))) (self.chip_output.subdir, str(chip.chipID).rjust(2, '0')))
...@@ -103,11 +103,11 @@ def add_sky_flat_calibration(self, chip, filt, tel, pointing, catalog, obs_param ...@@ -103,11 +103,11 @@ def add_sky_flat_calibration(self, chip, filt, tel, pointing, catalog, obs_param
filter_param.param[chip.filter_type][5] / tel.pupil_area * exptime filter_param.param[chip.filter_type][5] / tel.pupil_area * exptime
elif chip.survey_type == "spectroscopic": elif chip.survey_type == "spectroscopic":
# flat_normal = np.ones_like(chip.img.array) # flat_normal = np.ones_like(chip.img.array)
if obs_param["flat_fielding"] == True: if obs_param["flat_fielding"] is True:
flat_normal = flat_normal * chip.flat_img.array / \ flat_normal = flat_normal * chip.flat_img.array / \
np.mean(chip.flat_img.array) np.mean(chip.flat_img.array)
if obs_param["shutter_effect"] == True: if obs_param["shutter_effect"] is True:
flat_normal = flat_normal * chip.shutter_img flat_normal = flat_normal * chip.shutter_img
flat_normal = np.array(flat_normal, dtype='float32') flat_normal = np.array(flat_normal, dtype='float32')
......
...@@ -17,7 +17,9 @@ def prepare_headers(self, chip, pointing): ...@@ -17,7 +17,9 @@ def prepare_headers(self, chip, pointing):
sat_vel=[pointing.sat_vx, pointing.sat_vy, pointing.sat_vz], sat_vel=[pointing.sat_vx, pointing.sat_vy, pointing.sat_vz],
project_cycle=self.overall_config["project_cycle"], project_cycle=self.overall_config["project_cycle"],
run_counter=self.overall_config["run_counter"], run_counter=self.overall_config["run_counter"],
chip_name=str(chip.chipID).rjust(2, '0')) chip_name=str(chip.chipID).rjust(2, '0'),
obstype=pointing.survey_field_type,
dataset=pointing.dataset)
self.h_ext = generateExtensionHeader( self.h_ext = generateExtensionHeader(
chip=chip, chip=chip,
xlen=chip.npix_x, xlen=chip.npix_x,
......
...@@ -17,13 +17,52 @@ def add_prescan_overscan(self, chip, filt, tel, pointing, catalog, obs_param): ...@@ -17,13 +17,52 @@ def add_prescan_overscan(self, chip, filt, tel, pointing, catalog, obs_param):
over1=chip.overscan_x, over1=chip.overscan_x,
over2=chip.overscan_y) over2=chip.overscan_y)
if obs_param["add_dark"] == True: if obs_param["add_dark"] is True:
ny = int(chip.npix_y/2) ny = int(chip.npix_y/2)
base_dark = (ny-1)*(chip.readout_time/ny)*chip.dark_noise base_dark = (ny-1)*(chip.readout_time/ny)*chip.dark_noise
chip.img.array[(chip.prescan_y+ny):-(chip.prescan_y+ny), :] = base_dark chip.img.array[(chip.prescan_y+ny):-(chip.prescan_y+ny), :] = base_dark
return chip, filt, tel, pointing return chip, filt, tel, pointing
def add_crosstalk(self, chip, filt, tel, pointing, catalog, obs_param):
crosstalk = np.zeros([16, 16])
crosstalk[0, :] = np.array([1., 1e-4, 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
crosstalk[1, :] = np.array([1e-4, 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
crosstalk[2, :] = np.array([0., 0., 1., 1e-4, 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
crosstalk[3, :] = np.array([0., 0., 1e-4, 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
crosstalk[4, :] = np.array([0., 0., 0., 0., 1., 1e-4, 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
crosstalk[5, :] = np.array([0., 0., 0., 0., 1e-4, 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
crosstalk[6, :] = np.array([0., 0., 0., 0., 0., 0., 1., 1e-4, 0., 0., 0., 0., 0., 0., 0., 0.])
crosstalk[7, :] = np.array([0., 0., 0., 0., 0., 0., 1e-4, 1., 0., 0., 0., 0., 0., 0., 0., 0.])
crosstalk[8, :] = np.array([0., 0., 0., 0., 0., 0., 0., 0., 1., 1e-4, 0., 0., 0., 0., 0., 0.])
crosstalk[9, :] = np.array([0., 0., 0., 0., 0., 0., 0., 0., 1e-4, 1., 0., 0., 0., 0., 0., 0.])
crosstalk[10, :] = np.array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 1e-4, 0., 0., 0., 0.])
crosstalk[11, :] = np.array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1e-4, 1., 0., 0., 0., 0.])
crosstalk[12, :] = np.array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 1e-4, 0., 0.])
crosstalk[13, :] = np.array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1e-4, 1., 0., 0.])
crosstalk[14, :] = np.array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 1e-4])
crosstalk[15, :] = np.array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1e-4, 1.])
# 2*8 -> 1*16
img = chip_utils.formatOutput(chip.img)
ny, nx = img.array.shape
nsecy = 1
nsecx = 16
dy = int(ny/nsecy)
dx = int(nx/nsecx)
newimg = galsim.Image(nx, ny, init_value=0)
for i in range(16):
for j in range(16):
newimg.array[:, int(i*dx):int(i*dx+dx)] += crosstalk[i, j]*img.array[:, int(j*dx):int(j*dx+dx)]
# 1*16 -> 2*8
newimg = chip_utils.formatRevert(newimg)
chip.img.array[:, :] = newimg.array[:, :]
return chip, filt, tel, pointing
def add_readout_noise(self, chip, filt, tel, pointing, catalog, obs_param): def add_readout_noise(self, chip, filt, tel, pointing, catalog, obs_param):
seed = int(self.overall_config["random_seeds"] seed = int(self.overall_config["random_seeds"]
["seed_readout"]) + pointing.id*30 + chip.chipID ["seed_readout"]) + pointing.id*30 + chip.chipID
...@@ -36,13 +75,13 @@ def add_readout_noise(self, chip, filt, tel, pointing, catalog, obs_param): ...@@ -36,13 +75,13 @@ def add_readout_noise(self, chip, filt, tel, pointing, catalog, obs_param):
def apply_gain(self, chip, filt, tel, pointing, catalog, obs_param): def apply_gain(self, chip, filt, tel, pointing, catalog, obs_param):
self.chip_output.Log_info(" Applying Gain") self.chip_output.Log_info(" Applying Gain")
if obs_param["gain_16channel"] == True: if obs_param["gain_16channel"] is True:
chip.img, chip.gain_channel = effects.ApplyGainNonUniform16(chip.img, chip.img, chip.gain_channel = effects.ApplyGainNonUniform16(chip.img,
gain=chip.gain, gain=chip.gain,
nsecy=chip.nsecy, nsecy=chip.nsecy,
nsecx=chip.nsecx, nsecx=chip.nsecx,
seed=self.overall_config["random_seeds"]["seed_gainNonUniform"]+chip.chipID) seed=self.overall_config["random_seeds"]["seed_gainNonUniform"]+chip.chipID)
elif obs_param["gain_16channel"] == False: elif obs_param["gain_16channel"] is False:
chip.img /= chip.gain chip.img /= chip.gain
chip.gain_channel = np.ones(chip.nsecy*chip.nsecx)*chip.gain chip.gain_channel = np.ones(chip.nsecy*chip.nsecx)*chip.gain
return chip, filt, tel, pointing return chip, filt, tel, pointing
...@@ -74,7 +113,7 @@ def quantization_and_output(self, chip, filt, tel, pointing, catalog, obs_param) ...@@ -74,7 +113,7 @@ def quantization_and_output(self, chip, filt, tel, pointing, catalog, obs_param)
self.updateHeaderInfo(header_flag='ext', keys=['GAIN01', 'GAIN02', 'GAIN03', 'GAIN04', 'GAIN05', 'GAIN06', 'GAIN07', self.updateHeaderInfo(header_flag='ext', keys=['GAIN01', 'GAIN02', 'GAIN03', 'GAIN04', 'GAIN05', 'GAIN06', 'GAIN07',
'GAIN08', 'GAIN09', 'GAIN10', 'GAIN11', 'GAIN12', 'GAIN13', 'GAIN14', 'GAIN15', 'GAIN16'], values=gains) 'GAIN08', 'GAIN09', 'GAIN10', 'GAIN11', 'GAIN12', 'GAIN13', 'GAIN14', 'GAIN15', 'GAIN16'], values=gains)
if obs_param["format_output"] == True: if obs_param["format_output"] is True:
self.chip_output.Log_info(" Apply 1*16 format") self.chip_output.Log_info(" Apply 1*16 format")
chip.img = chip_utils.formatOutput(GSImage=chip.img) chip.img = chip_utils.formatOutput(GSImage=chip.img)
chip.nsecy = 1 chip.nsecy = 1
......
...@@ -18,7 +18,7 @@ def run_sim(): ...@@ -18,7 +18,7 @@ def run_sim():
Parameters Parameters
---------- ----------
Catalog : Class Catalog : Class
a catalog class which is inherited from observation_sim.mock_objects.CatalogBase a catalog class which is inherited from observation_sim.mock_objects.CatalogBase
Returns Returns
---------- ----------
...@@ -63,6 +63,9 @@ def run_sim(): ...@@ -63,6 +63,9 @@ def run_sim():
if "run_counter" not in config: if "run_counter" not in config:
config["run_counter"] = 0 config["run_counter"] = 0
if "data_set" not in config:
config["data_set"] = "csst-msc"
# Generate lists pointings based on the input pointing list (or default # Generate lists pointings based on the input pointing list (or default
# pointing RA, DEC) and "config["obs_setting"]["run_pointings"]". # pointing RA, DEC) and "config["obs_setting"]["run_pointings"]".
# "config['obs_setting']['np_cal']"" is the number of CAL pointings which will be # "config['obs_setting']['np_cal']"" is the number of CAL pointings which will be
...@@ -72,7 +75,7 @@ def run_sim(): ...@@ -72,7 +75,7 @@ def run_sim():
if "pointing_dir" in config['obs_setting']: if "pointing_dir" in config['obs_setting']:
pointing_dir = config['obs_setting']["pointing_dir"] pointing_dir = config['obs_setting']["pointing_dir"]
pointing_list = generate_pointing_list( pointing_list = generate_pointing_list(
config=config, pointing_filename=config['obs_setting']['pointing_file'], data_dir=pointing_dir) config=config, pointing_filename=config['obs_setting']['pointing_file'], data_dir=pointing_dir, dataset=config["data_set"])
# Make the main output directories # Make the main output directories
run_dir = make_run_dirs( run_dir = make_run_dirs(
......
...@@ -22,7 +22,7 @@ except ImportError: ...@@ -22,7 +22,7 @@ except ImportError:
import importlib_resources as pkg_resources import importlib_resources as pkg_resources
### test FUNCTION --- START ### # test FUNCTION --- START #
def add_brighter_fatter(img): def add_brighter_fatter(img):
# Inital dynamic lib # Inital dynamic lib
try: try:
...@@ -50,7 +50,7 @@ def add_brighter_fatter(img): ...@@ -50,7 +50,7 @@ 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
### test FUNCTION --- END ### # test FUNCTION --- END #
def defineCCD(iccd, config_file): def defineCCD(iccd, config_file):
......
...@@ -72,7 +72,7 @@ class PSFInterpModule_coverage(unittest.TestCase): ...@@ -72,7 +72,7 @@ class PSFInterpModule_coverage(unittest.TestCase):
psfB = psfModel.get_PSF( psfB = psfModel.get_PSF(
chip=chip, pos_img=pos_img, findNeighMode='hoclistFind', bandpass=bandpass[0], galsimGSObject=False) chip=chip, pos_img=pos_img, findNeighMode='hoclistFind', bandpass=bandpass[0], galsimGSObject=False)
self.assertTrue(psf != None) self.assertTrue(psf is not None)
self.assertTrue(np.max(np.abs(psfA-psfB)) < 1e-6) self.assertTrue(np.max(np.abs(psfA-psfB)) < 1e-6)
......
...@@ -428,8 +428,8 @@ class TestSpecDisperse(unittest.TestCase): ...@@ -428,8 +428,8 @@ class TestSpecDisperse(unittest.TestCase):
plt.xlim(6200, 10000) plt.xlim(6200, 10000)
plt.ylim(1, 4) plt.ylim(1, 4)
plt.yscale('log') plt.yscale('log')
plt.xlabel('$\lambda$') plt.xlabel(r'$\lambda$')
plt.ylabel('$F\lambda$') plt.ylabel(r'$F\lambda$')
plt.legend(['one spec', 'split in 8000 A']) plt.legend(['one spec', 'split in 8000 A'])
plt.show() plt.show()
......
...@@ -192,7 +192,7 @@ class TestStraylight(unittest.TestCase): ...@@ -192,7 +192,7 @@ class TestStraylight(unittest.TestCase):
plt.figure() plt.figure()
plt.plot(spec['WAVELENGTH'], spec['FLUX'], 'r') plt.plot(spec['WAVELENGTH'], spec['FLUX'], 'r')
plt.xlabel('WAVELENGTH') plt.xlabel('WAVELENGTH')
plt.ylabel('F$\lambda$(erg/s/cm2/A/arcsec2)') plt.ylabel(r'F$\lambda$(erg/s/cm2/A/arcsec2)')
plt.xlim(2000, 10000) plt.xlim(2000, 10000)
plt.show() plt.show()
median = np.median(sl_e_pix[0:tnum]) median = np.median(sl_e_pix[0:tnum])
......
import unittest
import sys
import os
import math
from itertools import islice
import numpy as np
import copy
import ctypes
import galsim
import yaml
from astropy.io import fits
from observation_sim.instruments.chip import chip_utils
import matplotlib.pyplot as plt
# test FUNCTION --- START #
def add_crosstalk(GSimg):
crosstalk = np.zeros([16, 16])
crosstalk[0, :] = np.array([1., 1e-4, 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
crosstalk[1, :] = np.array([1e-4, 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
crosstalk[2, :] = np.array([0., 0., 1., 1e-4, 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
crosstalk[3, :] = np.array([0., 0., 1e-4, 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
crosstalk[4, :] = np.array([0., 0., 0., 0., 1., 1e-4, 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
crosstalk[5, :] = np.array([0., 0., 0., 0., 1e-4, 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
crosstalk[6, :] = np.array([0., 0., 0., 0., 0., 0., 1., 1e-4, 0., 0., 0., 0., 0., 0., 0., 0.])
crosstalk[7, :] = np.array([0., 0., 0., 0., 0., 0., 1e-4, 1., 0., 0., 0., 0., 0., 0., 0., 0.])
crosstalk[8, :] = np.array([0., 0., 0., 0., 0., 0., 0., 0., 1., 1e-4, 0., 0., 0., 0., 0., 0.])
crosstalk[9, :] = np.array([0., 0., 0., 0., 0., 0., 0., 0., 1e-4, 1., 0., 0., 0., 0., 0., 0.])
crosstalk[10, :] = np.array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 1e-4, 0., 0., 0., 0.])
crosstalk[11, :] = np.array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1e-4, 1., 0., 0., 0., 0.])
crosstalk[12, :] = np.array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 1e-4, 0., 0.])
crosstalk[13, :] = np.array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1e-4, 1., 0., 0.])
crosstalk[14, :] = np.array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 1e-4])
crosstalk[15, :] = np.array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1e-4, 1.])
# 2*8 -> 1*16
img = chip_utils.formatOutput(GSimg)
ny, nx = img.array.shape
nsecy = 1
nsecx = 16
dy = int(ny/nsecy)
dx = int(nx/nsecx)
newimg = galsim.Image(nx, ny, init_value=0)
for i in range(16):
for j in range(16):
newimg.array[:, int(i*dx):int(i*dx+dx)] += crosstalk[i, j]*img.array[:, int(j*dx):int(j*dx+dx)]
# 1*16 -> 2*8
newimg = chip_utils.formatRevert(newimg)
return newimg
# test FUNCTION --- END #
class detModule_coverage(unittest.TestCase):
def __init__(self, methodName='runTest'):
super(detModule_coverage, self).__init__(methodName)
self.dataPath = os.path.join(
os.getenv('UNIT_TEST_DATA_ROOT'), 'csst_msc_sim/csst_fz_msc')
def test_add_crosstalk(self):
nsecy = 2
nsecx = 8
ny, nx = 1024, 1024
dy = int(ny/nsecy)
dx = int(nx/nsecx)
mapclip = np.zeros([dy, int(nsecx*nsecy*dx)])
for i in range(int(nsecx*nsecy)):
mapclip[:, i*dx:dx+i*dx] = np.random.randint(10)+np.random.rand(int(dy*dx)).reshape([dy, dx])
mapclip = galsim.ImageF(mapclip)
nsecy = 1
nsecx = 16
ny, nx = mapclip.array.shape
dy = int(ny/nsecy)
dx = int(nx/nsecx)
for i in range(int(nsecy*nsecx)):
gal = galsim.Gaussian(sigma=0.2, flux=500000).drawImage(nx=32, ny=32).array
py = np.random.randint(450)+10
mapclip.array[py:py+32, int(i*dx)+10:int(i*dx)+42] += gal
tmap = chip_utils.formatRevert(mapclip, nsecy=1, nsecx=16) # 1*16 -> 2*8
temp = add_crosstalk(tmap)
fig = plt.figure(figsize=(20, 60))
ax = plt.subplot(311)
plt.imshow(np.log10(mapclip.array+1), origin='lower', cmap='gray')
ax = plt.subplot(312)
plt.imshow(np.log10(temp.array+1), origin='lower', cmap='gray')
ax = plt.subplot(313)
plt.imshow(np.log10(temp.array-tmap.array+1), origin='lower', cmap='gray')
plt.savefig(os.path.join(self.dataPath, "./test_crosstalk.png"), dpi=300, bbox_inches='tight')
self.assertTrue(True)
if __name__ == '__main__':
unittest.main()
...@@ -10,11 +10,11 @@ import yaml ...@@ -10,11 +10,11 @@ import yaml
from observation_sim.instruments import Chip, Filter, FilterParam, FocalPlane from observation_sim.instruments import Chip, Filter, FilterParam, FocalPlane
### test FUNCTION --- START ### # test FUNCTION --- START ###
def get_base_img(img, chip, read_noise, readout_time, dark_noise, exptime=150., InputDark=None): def get_base_img(img, chip, read_noise, readout_time, dark_noise, exptime=150., InputDark=None):
if InputDark == None: if InputDark is None:
# base_level = read_noise**2 + dark_noise*(exptime+0.5*readout_time) # base_level = read_noise**2 + dark_noise*(exptime+0.5*readout_time)
# base_level = dark_noise*(exptime+0.5*readout_time) # base_level = dark_noise*(exptime+0.5*readout_time)
base_level = dark_noise*(exptime) base_level = dark_noise*(exptime)
...@@ -31,7 +31,7 @@ def get_base_img(img, chip, read_noise, readout_time, dark_noise, exptime=150., ...@@ -31,7 +31,7 @@ def get_base_img(img, chip, read_noise, readout_time, dark_noise, exptime=150.,
base_img2[ny:, :] = arr[::-1, :] base_img2[ny:, :] = arr[::-1, :]
base_img2[:, :] = base_img2[:, :]*(readout_time/ny)*dark_noise base_img2[:, :] = base_img2[:, :]*(readout_time/ny)*dark_noise
return base_img1+base_img2 return base_img1+base_img2
### test FUNCTION --- END ### # test FUNCTION --- END ###
def defineCCD(iccd, config_file): def defineCCD(iccd, config_file):
......
...@@ -209,7 +209,7 @@ class imagingModule_coverage(unittest.TestCase): ...@@ -209,7 +209,7 @@ class imagingModule_coverage(unittest.TestCase):
gal = gal + gal_temp gal = gal + gal_temp
print(gal) print(gal)
self.assertTrue(gal != None) self.assertTrue(gal is not None)
if __name__ == '__main__': if __name__ == '__main__':
......
import unittest import unittest
import sys,os,math import sys
import os
import math
from itertools import islice from itertools import islice
import numpy as np import numpy as np
import galsim import galsim
...@@ -8,14 +10,15 @@ import yaml ...@@ -8,14 +10,15 @@ import yaml
from observation_sim.instruments import Chip, Filter, FilterParam, FocalPlane from observation_sim.instruments import Chip, Filter, FilterParam, FocalPlane
### test FUNCTION --- START ###
def AddPreScan(GSImage, pre1=27, pre2=4, over1=71, over2=80, nsecy = 2, nsecx=8): # test FUNCTION --- START #
img= GSImage.array def AddPreScan(GSImage, pre1=27, pre2=4, over1=71, over2=80, nsecy=2, nsecx=8):
img = GSImage.array
ny, nx = img.shape ny, nx = img.shape
dx = int(nx/nsecx) dx = int(nx/nsecx)
dy = int(ny/nsecy) dy = int(ny/nsecy)
imgt=np.zeros([int(nsecy*nsecx), int(ny/nsecy+pre2+over2), int(nx/nsecx+pre1+over1)]) imgt = np.zeros([int(nsecy*nsecx), int(ny/nsecy+pre2+over2), int(nx/nsecx+pre1+over1)])
for iy in range(nsecy): for iy in range(nsecy):
for ix in range(nsecx): for ix in range(nsecx):
if iy % 2 == 0: if iy % 2 == 0:
...@@ -23,7 +26,7 @@ def AddPreScan(GSImage, pre1=27, pre2=4, over1=71, over2=80, nsecy = 2, nsecx=8) ...@@ -23,7 +26,7 @@ def AddPreScan(GSImage, pre1=27, pre2=4, over1=71, over2=80, nsecy = 2, nsecx=8)
else: else:
tx = (nsecx-1)-ix tx = (nsecx-1)-ix
ty = iy 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] 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)]) imgtemp = np.zeros([int(ny/nsecy+pre2+over2), int(nx/nsecx+pre1+over1)])
if int(chunkidx/4) == 0: if int(chunkidx/4) == 0:
...@@ -31,40 +34,41 @@ def AddPreScan(GSImage, pre1=27, pre2=4, over1=71, over2=80, nsecy = 2, nsecx=8) ...@@ -31,40 +34,41 @@ def AddPreScan(GSImage, pre1=27, pre2=4, over1=71, over2=80, nsecy = 2, nsecx=8)
imgt[chunkidx, :, :] = imgtemp imgt[chunkidx, :, :] = imgtemp
if int(chunkidx/4) == 1: if int(chunkidx/4) == 1:
imgtemp[pre2:pre2+dy, over1:over1+dx] = img[iy*dy:(iy+1)*dy, ix*dx:(ix+1)*dx] imgtemp[pre2:pre2+dy, over1:over1+dx] = img[iy*dy:(iy+1)*dy, ix*dx:(ix+1)*dx]
imgt[chunkidx, :, :] = imgtemp #[:, ::-1] imgt[chunkidx, :, :] = imgtemp # [:, ::-1]
if int(chunkidx/4) == 2: if int(chunkidx/4) == 2:
imgtemp[over2:over2+dy, over1:over1+dx] = img[iy*dy:(iy+1)*dy, ix*dx:(ix+1)*dx] imgtemp[over2:over2+dy, over1:over1+dx] = img[iy*dy:(iy+1)*dy, ix*dx:(ix+1)*dx]
imgt[chunkidx, :, :] = imgtemp #[::-1, ::-1] imgt[chunkidx, :, :] = imgtemp # [::-1, ::-1]
if int(chunkidx/4) == 3: if int(chunkidx/4) == 3:
imgtemp[over2:over2+dy, pre1:pre1+dx] = img[iy*dy:(iy+1)*dy, ix*dx:(ix+1)*dx] imgtemp[over2:over2+dy, pre1:pre1+dx] = img[iy*dy:(iy+1)*dy, ix*dx:(ix+1)*dx]
imgt[chunkidx, :, :] = imgtemp #[::-1, :] imgt[chunkidx, :, :] = imgtemp # [::-1, :]
imgtx1 = np.hstack(imgt[:nsecx:, :, :]) #hstack chunk(1,2)-[1,2,3,4,5,6,7,8] imgtx1 = np.hstack(imgt[:nsecx:, :, :]) # hstack chunk(1,2)-[1,2,3,4,5,6,7,8]
imgtx2 = np.hstack(imgt[:(nsecx-1):-1, :, :]) #hstack chunk(4,3)-[16,15,14,13,12,11,,10,9] imgtx2 = np.hstack(imgt[:(nsecx-1):-1, :, :]) # hstack chunk(4,3)-[16,15,14,13,12,11,,10,9]
newimg = galsim.Image(int(nx+(pre1+over1)*nsecx), int(ny+(pre2+over2)*nsecy), init_value=0) newimg = galsim.Image(int(nx+(pre1+over1)*nsecx), int(ny+(pre2+over2)*nsecy), init_value=0)
newimg.array[:, :] = np.concatenate([imgtx1, imgtx2]) #vstack chunk(1,2) & chunk(4,3) newimg.array[:, :] = np.concatenate([imgtx1, imgtx2]) # vstack chunk(1,2) & chunk(4,3)
newimg.wcs = GSImage.wcs newimg.wcs = GSImage.wcs
return newimg return newimg
### test FUNCTION --- END ### # test FUNCTION --- END #
def defineCCD(iccd, config_file): def defineCCD(iccd, config_file):
with open(config_file, "r") as stream: with open(config_file, "r") as stream:
try: try:
config = yaml.safe_load(stream) config = yaml.safe_load(stream)
#for key, value in config.items(): # for key, value in config.items():
# print (key + " : " + str(value)) # print (key + " : " + str(value))
except yaml.YAMLError as exc: except yaml.YAMLError as exc:
print(exc) print(exc)
chip = Chip(chipID=iccd, config=config) chip = Chip(chipID=iccd, config=config)
chip.img = galsim.ImageF(chip.npix_x, chip.npix_y) chip.img = galsim.ImageF(chip.npix_x, chip.npix_y)
focal_plane = FocalPlane(chip_list=[iccd]) focal_plane = FocalPlane(chip_list=[iccd])
chip.img.wcs= focal_plane.getTanWCS(192.8595, 27.1283, -113.4333*galsim.degrees, chip.pix_scale) chip.img.wcs = focal_plane.getTanWCS(192.8595, 27.1283, -113.4333*galsim.degrees, chip.pix_scale)
return chip return chip
def defineFilt(chip): def defineFilt(chip):
filter_param = FilterParam() filter_param = FilterParam()
filter_id, filter_type = chip.getChipFilter() filter_id, filter_type = chip.getChipFilter()
...@@ -91,13 +95,13 @@ class detModule_coverage(unittest.TestCase): ...@@ -91,13 +95,13 @@ class detModule_coverage(unittest.TestCase):
print(chip.cen_pix_x, chip.cen_pix_y) print(chip.cen_pix_x, chip.cen_pix_y)
chip.img = AddPreScan(GSImage=chip.img, chip.img = AddPreScan(GSImage=chip.img,
pre1=chip.prescan_x, pre1=chip.prescan_x,
pre2=chip.prescan_y, pre2=chip.prescan_y,
over1=chip.overscan_x, over1=chip.overscan_x,
over2=chip.overscan_y) over2=chip.overscan_y)
self.assertTrue( (chip.prescan_x+chip.overscan_x)*8+chip.npix_x == np.shape(chip.img.array)[1] ) self.assertTrue((chip.prescan_x+chip.overscan_x)*8+chip.npix_x == np.shape(chip.img.array)[1])
self.assertTrue( (chip.prescan_y+chip.overscan_y)*2+chip.npix_y == np.shape(chip.img.array)[0] ) self.assertTrue((chip.prescan_y+chip.overscan_y)*2+chip.npix_y == np.shape(chip.img.array)[0])
if __name__ == '__main__': if __name__ == '__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