Commit 38e2c452 authored by Fang Yuedong's avatar Fang Yuedong
Browse files

Merge branch 'master' into 'current_stable_for_tests'

for tests before release

See merge request !23
parents e0f2b9f7 1c35c0e2
import numpy as np
import os
import math
from pylab import *
from scipy import interpolate
try:
import importlib.resources as pkg_resources
except ImportError:
# Try backported to PY<37 'importlib_resources'
import importlib_resources as pkg_resources
VC_A = 2.99792458e+18 # speed of light: A/s
VC_M = 2.99792458e+8 # speed of light: m/s
H_PLANK = 6.626196e-27 # Plank constant: erg s
ALL_FILTERS = ["NUV", "u", "g", "r", "i", "z", "y", "GU", "GV", "GI", "FGS"]
PHOT_FILTERS = ["NUV", "u", "g", 'r', 'i', 'z', 'y', 'FGS']
SPEC_FILTERS = ["GI", "GV", "GU"]
def rotate_conterclockwise(x0, y0, x, y, angle):
"""
Rotate a point counterclockwise by a given angle around a given origin.
The angle should be given in radians.
"""
angle = np.deg2rad(angle)
qx = x0 + np.cos(angle)*(x - x0) - np.sin(angle) * (y - y0)
qy = y0 + np.sin(angle)*(x - x0) + np.cos(angle) * (y - y0)
return qx, qy
def photonEnergy(lambd):
""" The energy of photon at a given wavelength
Parameter:
lambd: the wavelength in unit of Angstrom
Return:
eph: energy of photon in unit of erg
"""
nu = VC_A / lambd
eph = H_PLANK * nu
return eph
def calculateLimitMag(aperture=2.0, psf_fwhm=0.1969, pixelSize=0.074, pmRation=0.8, throughputFn='i_throughput.txt', readout=5.0, skyFn='sky_emiss_hubble_50_50_A.dat', darknoise=0.02, exTime=150, exNum=1, fw=90000):
'''
description:
param {*} aperture: unit m, default 2 m
param {*} psf_fwhm: psf fwhm, default 0.1969"
param {*} pixelSize: pixel size, default 0.074"
param {*} pmRation: the ratio of souce flux in the limit mag calculation
param {*} throughputFn: throuput file name
param {*} readout: unit, e-/pixel
param {*} skyFn: sky sed file name, average of hst, 'sky_emiss_hubble_50_50_A.dat'
param {*} darknoise: unit, e-/pixel/s
param {*} exTime: exposure time one time, default 150s
param {*} exNum: exposure number, defautl 1
param {*} fw, full well value( or saturation value),default 90000e-/pixel
return {*} limit mag and saturation mag
'''
try:
with pkg_resources.files('observation_sim.instruments.data.throughputs').joinpath(throughputFn) as data_file:
throughput_f = np.loadtxt(data_file)
except AttributeError:
with pkg_resources.path('observation_sim.instruments.data.throughputs', throughputFn) as data_file:
throughput_f = np.loadtxt(data_file)
thr_i = interpolate.interp1d(
throughput_f[:, 0]/10, throughput_f[:, 1]) # wavelength in anstrom
f_s = 200
f_e = 1100
delt_f = 0.5
data_num = int((f_e-f_s)/delt_f+1)
eff = np.zeros([data_num, 2])
eff[:, 0] = np.arange(f_s, f_e+delt_f, delt_f)
eff[:, 1] = thr_i(eff[:, 0])
wave = np.arange(f_s, f_e+delt_f, delt_f)
wavey = np.ones(wave.shape[0])
try:
with pkg_resources.files('observation_sim.instruments.data.throughputs').joinpath(skyFn) as data_file:
skydata = np.loadtxt(data_file)
except AttributeError:
with pkg_resources.path('observation_sim.instruments.data.throughputs', skyFn) as data_file:
skydata = np.loadtxt(data_file)
skydatai = interpolate.interp1d(skydata[:, 0]/10, skydata[:, 1]*10)
sky_data = np.zeros([data_num, 2])
sky_data[:, 0] = np.arange(f_s, f_e+delt_f, delt_f)
sky_data[:, 1] = skydatai(sky_data[:, 0])
flux_sky = trapz((sky_data[:, 1])*eff[:, 1], sky_data[:, 0])
skyPix = flux_sky*pixelSize*pixelSize*pi*(aperture*aperture/4)
# limit mag
r_pix = psf_fwhm*0.7618080243778568/pixelSize # radius RE80, pixel
cnum = math.pi * r_pix * r_pix
sn = 5
d = skyPix*exTime*exNum*cnum + darknoise * \
exTime*exNum*cnum+readout*readout*cnum*exNum
a = 1
b = -sn*sn
c = -sn*sn*d
flux = (-b+sqrt(b*b-4*a*c))/(2*a)/pmRation
limitMag = -2.5*log10(flux/(54799275581.04437 * trapz(wavey *
eff[:, 1]/wave, wave, 0.1)*exTime*exNum*pi*(aperture/2)*(aperture/2)))
# saturation mag
from astropy.modeling.models import Gaussian2D
m_size = int(20 * psf_fwhm/pixelSize)
if m_size % 2 == 0:
m_size + 1
m_cen = m_size//2
psf_sigma = psf_fwhm/2.355/pixelSize
gaussShape = Gaussian2D(1, m_cen, m_cen, psf_sigma, psf_sigma)
yp, xp = np.mgrid[0:m_size, 0:m_size]
psfMap = gaussShape(xp, yp)
maxRatio = np.amax(psfMap)/np.sum(psfMap)
# print(maxRatio)
flux_sat = fw/maxRatio*exNum
satMag = -2.5*log10(flux_sat/(54799275581.04437 * trapz(wavey *
eff[:, 1]/wave, wave, 0.1)*exTime*exNum*pi*(aperture/2)*(aperture/2)))
return limitMag, satMag
import galsim
import os
import numpy as np
import pickle
import json
from astropy.table import Table
from numpy.random import Generator, PCG64
from astropy.io import fits
from datetime import datetime
import observation_sim.instruments._util as _util
from observation_sim.instruments.chip import effects
from observation_sim.instruments.FocalPlane import FocalPlane
from observation_sim.config.header import generatePrimaryHeader, generateExtensionHeader
from observation_sim.instruments._util import rotate_conterclockwise
from observation_sim.instruments.chip import chip_utils
from observation_sim.instruments.chip.libCTI.CTI_modeling import CTI_sim
try:
import importlib.resources as pkg_resources
except ImportError:
# Try backported to PY<37 'importlib_resources'
import importlib_resources as pkg_resources
class Chip(FocalPlane):
def __init__(self, chipID, ccdEffCurve_dir=None, CRdata_dir=None, sls_dir=None, config=None, treering_func=None, logger=None):
# Get focal plane (instance of paraent class) info
super().__init__()
self.nsecy = 2
self.nsecx = 8
self.gain_channel = np.ones(self.nsecy * self.nsecx)
self._set_attributes_from_config(config)
self.logger = logger
# A chip ID must be assigned
self.chipID = int(chipID)
self.chip_name = str(chipID).rjust(2, '0')
# Get corresponding filter info
self.filter_id, self.filter_type = self.getChipFilter()
self.survey_type = self._getSurveyType()
if self.filter_type != "FGS":
self._getChipRowCol()
# Set the relavent specs for detectors
try:
with pkg_resources.files('observation_sim.instruments.data.ccd').joinpath("chip_definition.json") as chip_definition:
with open(chip_definition, "r") as f:
chip_dict = json.load(f)[str(self.chipID)]
except AttributeError:
with pkg_resources.path('observation_sim.instruments.data.ccd', "chip_definition.json") as chip_definition:
with open(chip_definition, "r") as f:
chip_dict = json.load(f)[str(self.chipID)]
for key in chip_dict:
setattr(self, key, chip_dict[key])
self.fdModel = None
if self.filter_type == "FGS":
fgs_name = self.chip_name[0:4]
try:
with pkg_resources.files('observation_sim.instruments.data.field_distortion').joinpath("FieldDistModelGlobal_pr4_%s.pickle" % (fgs_name.lower())) as field_distortion:
with open(field_distortion, "rb") as f:
self.fdModel = pickle.load(f)
except AttributeError:
with pkg_resources.path('observation_sim.instruments.data.field_distortion', "FieldDistModelGlobal_pr4_%s.pickle" % (fgs_name.lower())) as field_distortion:
with open(field_distortion, "rb") as f:
self.fdModel = pickle.load(f)
else:
# Get the corresponding field distortion model
try:
with pkg_resources.files('observation_sim.instruments.data.field_distortion').joinpath("FieldDistModel_v2.0.pickle") as field_distortion:
with open(field_distortion, "rb") as f:
self.fdModel = pickle.load(f)
except AttributeError:
with pkg_resources.path('observation_sim.instruments.data.field_distortion', "FieldDistModelGlobal_mainFP_v1.0.pickle") as field_distortion:
with open(field_distortion, "rb") as f:
self.fdModel = pickle.load(f)
# Get boundary (in pix)
self.bound = self.getChipLim()
self.ccdEffCurve_dir = ccdEffCurve_dir
self.CRdata_dir = CRdata_dir
slsconfs = chip_utils.getChipSLSConf(chipID=self.chipID)
if np.size(slsconfs) == 1:
try:
with pkg_resources.files('observation_sim.instruments.data.sls_conf').joinpath(slsconfs) as conf_path:
self.sls_conf = str(conf_path)
except AttributeError:
with pkg_resources.path('observation_sim.instruments.data.sls_conf', slsconfs) as conf_path:
self.sls_conf = str(conf_path)
else:
# self.sls_conf = [os.path.join(self.sls_dir, slsconfs[0]), os.path.join(self.sls_dir, slsconfs[1])]
self.sls_conf = []
try:
with pkg_resources.files('observation_sim.instruments.data.sls_conf').joinpath(slsconfs[0]) as conf_path:
self.sls_conf.append(str(conf_path))
except AttributeError:
with pkg_resources.path('observation_sim.instruments.data.sls_conf', slsconfs[0]) as conf_path:
self.sls_conf.append(str(conf_path))
try:
with pkg_resources.files('observation_sim.instruments.data.sls_conf').joinpath(slsconfs[1]) as conf_path:
self.sls_conf.append(str(conf_path))
except AttributeError:
with pkg_resources.path('observation_sim.instruments.data.sls_conf', slsconfs[1]) as conf_path:
self.sls_conf.append(str(conf_path))
self.effCurve = self._getChipEffCurve(self.filter_type)
self._getCRdata()
# # Define the sensor model
self.sensor = galsim.Sensor()
self.flat_cube = None # for spectroscopic flat field cube simulation
def _set_attributes_from_config(self, config):
# Default setting
self.read_noise = 5.0 # e/pix
self.dark_noise = 0.02 # e/pix/s
self.rotate_angle = 0.
self.overscan = 1000
# Override default values
# for key in ["gain", "bias_level, dark_exptime", "flat_exptime", "readout_time", "full_well", "read_noise", "dark_noise", "overscan"]:
# if key in config["ins_effects"]:
# setattr(self, key, config["ins_effects"][key])
def _getChipRowCol(self):
self.rowID, self.colID = self.getChipRowCol(self.chipID)
def getChipRowCol(self, chipID):
rowID = ((chipID - 1) % 5) + 1
colID = 6 - ((chipID - 1) // 5)
return rowID, colID
def _getSurveyType(self):
if self.filter_type in _util.SPEC_FILTERS:
return "spectroscopic"
elif self.filter_type in _util.PHOT_FILTERS:
return "photometric"
# elif self.filter_type in ["FGS"]:
# return "FGS"
def _getChipEffCurve(self, filter_type):
# CCD efficiency curves
if filter_type in ['NUV', 'u', 'GU']:
filename = 'UV0.txt'
if filter_type in ['g', 'r', 'GV', 'FGS']:
# TODO, need to switch to the right efficiency curvey for FGS CMOS
filename = 'Astro_MB.txt'
if filter_type in ['i', 'z', 'y', 'GI']:
filename = 'Basic_NIR.txt'
try:
with pkg_resources.files('observation_sim.instruments.data.ccd').joinpath(filename) as ccd_path:
table = Table.read(ccd_path, format='ascii')
except AttributeError:
with pkg_resources.path('observation_sim.instruments.data.ccd', filename) as ccd_path:
table = Table.read(ccd_path, format='ascii')
throughput = galsim.LookupTable(
x=table['col1'], f=table['col2'], interpolant='linear')
bandpass = galsim.Bandpass(throughput, wave_type='nm')
return bandpass
def _getCRdata(self):
try:
with pkg_resources.files('observation_sim.instruments.data').joinpath("wfc-cr-attachpixel.dat") as cr_path:
self.attachedSizes = np.loadtxt(cr_path)
except AttributeError:
with pkg_resources.path('observation_sim.instruments.data', "wfc-cr-attachpixel.dat") as cr_path:
self.attachedSizes = np.loadtxt(cr_path)
# def loadSLSFLATCUBE(self, flat_fn='flat_cube.fits'):
# try:
# with pkg_resources.files('observation_sim.instruments.data').joinpath(flat_fn) as data_path:
# flat_fits = fits.open(data_path, ignore_missing_simple=True)
# except AttributeError:
# with pkg_resources.path('observation_sim.instruments.data', flat_fn) as data_path:
# flat_fits = fits.open(data_path, ignore_missing_simple=True)
# fl = len(flat_fits)
# fl_sh = flat_fits[0].data.shape
# assert fl == 4, 'FLAT Field Cube is Not 4 layess!!!!!!!'
# self.flat_cube = np.zeros([fl, fl_sh[0], fl_sh[1]])
# for i in np.arange(0, fl, 1):
# self.flat_cube[i] = flat_fits[i].data
def getChipFilter(self, chipID=None):
"""Return the filter index and type for a given chip #(chipID)
"""
filter_type_list = _util.ALL_FILTERS
if chipID == None:
chipID = self.chipID
# updated configurations
if chipID > 42 or chipID < 1:
raise ValueError("!!! Chip ID: [1,42]")
if chipID in [6, 15, 16, 25]:
filter_type = "y"
if chipID in [11, 20]:
filter_type = "z"
if chipID in [7, 24]:
filter_type = "i"
if chipID in [14, 17]:
filter_type = "u"
if chipID in [9, 22]:
filter_type = "r"
if chipID in [12, 13, 18, 19]:
filter_type = "NUV"
if chipID in [8, 23]:
filter_type = "g"
if chipID in [1, 10, 21, 30]:
filter_type = "GI"
if chipID in [2, 5, 26, 29]:
filter_type = "GV"
if chipID in [3, 4, 27, 28]:
filter_type = "GU"
if chipID in range(31, 43):
filter_type = 'FGS'
filter_id = filter_type_list.index(filter_type)
return filter_id, filter_type
def getChipLim(self, chipID=None):
"""Calculate the edges in pixel for a given CCD chip on the focal plane
NOTE: There are 5*4 CCD chips in the focus plane for photometric / spectroscopic observation.
Parameters:
chipID: int
the index of the chip
Returns:
A galsim BoundsD object
"""
xmin, xmax, ymin, ymax = 1e10, -1e10, 1e10, -1e10
xcen = self.x_cen / self.pix_size
ycen = self.y_cen / self.pix_size
sign_x = [-1., 1., -1., 1.]
sign_y = [-1., -1., 1., 1.]
for i in range(4):
x = xcen + sign_x[i] * self.npix_x / 2.
y = ycen + sign_y[i] * self.npix_y / 2.
x, y = _util.rotate_conterclockwise(
x0=xcen, y0=ycen, x=x, y=y, angle=self.rotate_angle)
xmin, xmax = min(xmin, x), max(xmax, x)
ymin, ymax = min(ymin, y), max(ymax, y)
return galsim.BoundsD(xmin, xmax, ymin, ymax)
def getSkyCoverage(self, wcs):
# print("In getSkyCoverage: xmin = %.3f, xmax = %.3f, ymin = %.3f, ymax = %.3f"%(self.bound.xmin, self.bound.xmax, self.bound.ymin, self.bound.ymax))
return super().getSkyCoverage(wcs, self.bound.xmin, self.bound.xmax, self.bound.ymin, self.bound.ymax)
def getSkyCoverageEnlarged(self, wcs, margin=0.5):
"""The enlarged sky coverage of the chip
"""
margin /= 60.0
bound = self.getSkyCoverage(wcs)
return galsim.BoundsD(bound.xmin - margin, bound.xmax + margin, bound.ymin - margin, bound.ymax + margin)
def isContainObj(self, ra_obj=None, dec_obj=None, x_image=None, y_image=None, wcs=None, margin=1):
# magin in number of pix
if (ra_obj is not None) and (dec_obj is not None):
if wcs is None:
wcs = self.img.wcs
pos_obj = wcs.toImage(galsim.CelestialCoord(
ra=ra_obj*galsim.degrees, dec=dec_obj*galsim.degrees))
x_image, y_image = pos_obj.x, pos_obj.y
elif (x_image is None) or (y_image is None):
raise ValueError(
"Either (ra_obj, dec_obj) or (x_image, y_image) should be given")
xmin, xmax = self.bound.xmin - margin, self.bound.xmax + margin
ymin, ymax = self.bound.ymin - margin, self.bound.ymax + margin
if (x_image - xmin) * (x_image - xmax) > 0.0 or (y_image - ymin) * (y_image - ymax) > 0.0:
return False
return True
def getChipNoise(self, exptime=150.0):
noise = self.dark_noise * exptime + self.read_noise**2
return noise
...@@ -5,8 +5,8 @@ import numpy as np ...@@ -5,8 +5,8 @@ import numpy as np
from astropy.io import fits from astropy.io import fits
from datetime import datetime from datetime import datetime
from ObservationSim.Instrument.Chip import Effects as effects from observation_sim.instruments.chip import effects
from ObservationSim.Config.Header import generatePrimaryHeader, generateExtensionHeader from observation_sim.config.header import generatePrimaryHeader, generateExtensionHeader
try: try:
import importlib.resources as pkg_resources import importlib.resources as pkg_resources
...@@ -21,52 +21,79 @@ def log_info(msg, logger=None): ...@@ -21,52 +21,79 @@ def log_info(msg, logger=None):
else: else:
print(msg, flush=True) print(msg, flush=True)
def getChipSLSGratingID(chipID): def getChipSLSGratingID(chipID):
gratingID = ['',''] gratingID = ['', '']
if chipID == 1: gratingID = ['GI2', 'GI1'] if chipID == 1:
if chipID == 2: gratingID = ['GV4', 'GV3'] gratingID = ['GI2', 'GI1']
if chipID == 3: gratingID = ['GU2', 'GU1'] if chipID == 2:
if chipID == 4: gratingID = ['GU4', 'GU3'] gratingID = ['GV4', 'GV3']
if chipID == 5: gratingID = ['GV2', 'GV1'] if chipID == 3:
if chipID == 10: gratingID = ['GI4', 'GI3'] gratingID = ['GU2', 'GU1']
if chipID == 21: gratingID = ['GI6', 'GI5'] if chipID == 4:
if chipID == 26: gratingID = ['GV8', 'GV7'] gratingID = ['GU4', 'GU3']
if chipID == 27: gratingID = ['GU6', 'GU5'] if chipID == 5:
if chipID == 28: gratingID = ['GU8', 'GU7'] gratingID = ['GV2', 'GV1']
if chipID == 29: gratingID = ['GV6', 'GV5'] if chipID == 10:
if chipID == 30: gratingID = ['GI8', 'GI7'] gratingID = ['GI4', 'GI3']
if chipID == 21:
gratingID = ['GI6', 'GI5']
if chipID == 26:
gratingID = ['GV8', 'GV7']
if chipID == 27:
gratingID = ['GU6', 'GU5']
if chipID == 28:
gratingID = ['GU8', 'GU7']
if chipID == 29:
gratingID = ['GV6', 'GV5']
if chipID == 30:
gratingID = ['GI8', 'GI7']
return gratingID return gratingID
def getChipSLSConf(chipID): def getChipSLSConf(chipID):
confFile = '' confFile = ''
if chipID == 1: confFile = ['CSST_GI2.conf', 'CSST_GI1.conf'] if chipID == 1:
if chipID == 2: confFile = ['CSST_GV4.conf', 'CSST_GV3.conf'] confFile = ['CSST_GI2.conf', 'CSST_GI1.conf']
if chipID == 3: confFile = ['CSST_GU2.conf', 'CSST_GU1.conf'] if chipID == 2:
if chipID == 4: confFile = ['CSST_GU4.conf', 'CSST_GU3.conf'] confFile = ['CSST_GV4.conf', 'CSST_GV3.conf']
if chipID == 5: confFile = ['CSST_GV2.conf', 'CSST_GV1.conf'] if chipID == 3:
if chipID == 10: confFile = ['CSST_GI4.conf', 'CSST_GI3.conf'] confFile = ['CSST_GU2.conf', 'CSST_GU1.conf']
if chipID == 21: confFile = ['CSST_GI6.conf', 'CSST_GI5.conf'] if chipID == 4:
if chipID == 26: confFile = ['CSST_GV8.conf', 'CSST_GV7.conf'] confFile = ['CSST_GU4.conf', 'CSST_GU3.conf']
if chipID == 27: confFile = ['CSST_GU6.conf', 'CSST_GU5.conf'] if chipID == 5:
if chipID == 28: confFile = ['CSST_GU8.conf', 'CSST_GU7.conf'] confFile = ['CSST_GV2.conf', 'CSST_GV1.conf']
if chipID == 29: confFile = ['CSST_GV6.conf', 'CSST_GV5.conf'] if chipID == 10:
if chipID == 30: confFile = ['CSST_GI8.conf', 'CSST_GI7.conf'] confFile = ['CSST_GI4.conf', 'CSST_GI3.conf']
if chipID == 21:
confFile = ['CSST_GI6.conf', 'CSST_GI5.conf']
if chipID == 26:
confFile = ['CSST_GV8.conf', 'CSST_GV7.conf']
if chipID == 27:
confFile = ['CSST_GU6.conf', 'CSST_GU5.conf']
if chipID == 28:
confFile = ['CSST_GU8.conf', 'CSST_GU7.conf']
if chipID == 29:
confFile = ['CSST_GV6.conf', 'CSST_GV5.conf']
if chipID == 30:
confFile = ['CSST_GI8.conf', 'CSST_GI7.conf']
return confFile return confFile
def generateHeader(chip, pointing, img_type=None, img_type_code=None, project_cycle='9', run_counter='1'): def generateHeader(chip, pointing, img_type=None, img_type_code=None, project_cycle='9', run_counter='1'):
if (img_type is None) or (img_type_code is None): if (img_type is None) or (img_type_code is None):
img_type = pointing.pointing_type img_type = pointing.pointing_type
img_type_code = pointing.pointing_type_code img_type_code = pointing.pointing_type_code
h_prim = generatePrimaryHeader( h_prim = generatePrimaryHeader(
xlen=chip.npix_x, xlen=chip.npix_x,
ylen=chip.npix_y, ylen=chip.npix_y,
pointing_id = pointing.obs_id, pointing_id=pointing.obs_id,
pointing_type_code = img_type_code, pointing_type_code=img_type_code,
ra=pointing.ra, ra=pointing.ra,
dec=pointing.dec, dec=pointing.dec,
pixel_scale=chip.pix_scale, pixel_scale=chip.pix_scale,
time_pt = pointing.timestamp, time_pt=pointing.timestamp,
exptime=pointing.exp_time, exptime=pointing.exp_time,
im_type=img_type, im_type=img_type,
sat_pos=[pointing.sat_x, pointing.sat_y, pointing.sat_z], sat_pos=[pointing.sat_x, pointing.sat_y, pointing.sat_z],
...@@ -76,16 +103,16 @@ def generateHeader(chip, pointing, img_type=None, img_type_code=None, project_cy ...@@ -76,16 +103,16 @@ def generateHeader(chip, pointing, img_type=None, img_type_code=None, project_cy
chip_name=str(chip.chipID).rjust(2, '0')) chip_name=str(chip.chipID).rjust(2, '0'))
h_ext = generateExtensionHeader( h_ext = generateExtensionHeader(
chip=chip, chip=chip,
xlen=chip.npix_x, xlen=chip.npix_x,
ylen=chip.npix_y, ylen=chip.npix_y,
ra=pointing.ra, ra=pointing.ra,
dec=pointing.dec, dec=pointing.dec,
pa=pointing.img_pa.deg, pa=pointing.img_pa.deg,
gain=chip.gain, gain=chip.gain,
readout=chip.read_noise, readout=chip.read_noise,
dark=chip.dark_noise, dark=chip.dark_noise,
saturation=90000, saturation=90000,
pixel_scale=chip.pix_scale, pixel_scale=chip.pix_scale,
pixel_size=chip.pix_size, pixel_size=chip.pix_size,
xcen=chip.x_cen, xcen=chip.x_cen,
ycen=chip.y_cen, ycen=chip.y_cen,
...@@ -93,15 +120,16 @@ def generateHeader(chip, pointing, img_type=None, img_type_code=None, project_cy ...@@ -93,15 +120,16 @@ def generateHeader(chip, pointing, img_type=None, img_type_code=None, project_cy
timestamp=pointing.timestamp, timestamp=pointing.timestamp,
exptime=pointing.exp_time, exptime=pointing.exp_time,
readoutTime=chip.readout_time, readoutTime=chip.readout_time,
t_shutter_open=pointing.t_shutter_open, t_shutter_open=pointing.t_shutter_open,
t_shutter_close=pointing.t_shutter_close) t_shutter_close=pointing.t_shutter_close)
return h_prim, h_ext return h_prim, h_ext
def output_fits_image(chip, pointing, img, output_dir, img_type=None, img_type_code=None, project_cycle='9', run_counter='1'): def output_fits_image(chip, pointing, img, output_dir, img_type=None, img_type_code=None, project_cycle='9', run_counter='1'):
h_prim, h_ext = generateHeader( h_prim, h_ext = generateHeader(
chip=chip, chip=chip,
pointing=pointing, pointing=pointing,
img_type=img_type, img_type=img_type,
img_type_code=img_type_code, img_type_code=img_type_code,
project_cycle=project_cycle, project_cycle=project_cycle,
run_counter=run_counter) run_counter=run_counter)
...@@ -118,6 +146,7 @@ def output_fits_image(chip, pointing, img, output_dir, img_type=None, img_type_c ...@@ -118,6 +146,7 @@ def output_fits_image(chip, pointing, img, output_dir, img_type=None, img_type_c
fname = os.path.join(output_dir, h_prim['FILENAME']+'.fits') fname = os.path.join(output_dir, h_prim['FILENAME']+'.fits')
hdu1.writeto(fname, output_verify='ignore', overwrite=True) hdu1.writeto(fname, output_verify='ignore', overwrite=True)
def add_sky_background(img, filt, exptime, sky_map=None, tel=None): def add_sky_background(img, filt, exptime, sky_map=None, tel=None):
# Add sky background # Add sky background
if sky_map is None: if sky_map is None:
...@@ -130,24 +159,26 @@ def add_sky_background(img, filt, exptime, sky_map=None, tel=None): ...@@ -130,24 +159,26 @@ def add_sky_background(img, filt, exptime, sky_map=None, tel=None):
# sky_map.addNoise(poisson_noise) # sky_map.addNoise(poisson_noise)
elif img.array.shape != sky_map.shape: elif img.array.shape != sky_map.shape:
raise ValueError("The shape img and sky_map must be equal.") raise ValueError("The shape img and sky_map must be equal.")
elif tel is not None: # If sky_map is given in flux elif tel is not None: # If sky_map is given in flux
sky_map = sky_map * tel.pupil_area * exptime sky_map = sky_map * tel.pupil_area * exptime
img += sky_map img += sky_map
return img, sky_map return img, sky_map
def get_flat(img, seed): def get_flat(img, seed):
flat_img = effects.MakeFlatSmooth( flat_img = effects.MakeFlatSmooth(
GSBounds=img.bounds, GSBounds=img.bounds,
seed=seed) seed=seed)
flat_normal = flat_img / np.mean(flat_img.array) flat_normal = flat_img / np.mean(flat_img.array)
return flat_img, flat_normal return flat_img, flat_normal
def add_cosmic_rays(img, chip, exptime=150, seed=0): def add_cosmic_rays(img, chip, exptime=150, seed=0):
cr_map, cr_event_num = effects.produceCR_Map( cr_map, cr_event_num = effects.produceCR_Map(
xLen=chip.npix_x, yLen=chip.npix_y, xLen=chip.npix_x, yLen=chip.npix_y,
exTime=exptime+0.5*chip.readout_time, exTime=exptime+0.5*chip.readout_time,
cr_pixelRatio=0.003*(exptime+0.5*chip.readout_time)/600., cr_pixelRatio=0.003*(exptime+0.5*chip.readout_time)/600.,
gain=chip.gain, gain=chip.gain,
attachedSizes=chip.attachedSizes, attachedSizes=chip.attachedSizes,
seed=seed) # seed: obj-imaging:+0; bias:+1; dark:+2; flat:+3; seed=seed) # seed: obj-imaging:+0; bias:+1; dark:+2; flat:+3;
img += cr_map img += cr_map
...@@ -157,24 +188,27 @@ def add_cosmic_rays(img, chip, exptime=150, seed=0): ...@@ -157,24 +188,27 @@ def add_cosmic_rays(img, chip, exptime=150, seed=0):
del cr_map del cr_map
return img, crmap_gsimg, cr_event_num return img, crmap_gsimg, cr_event_num
def add_PRNU(img, chip, seed=0): def add_PRNU(img, chip, seed=0):
prnu_img = effects.PRNU_Img( prnu_img = effects.PRNU_Img(
xsize=chip.npix_x, xsize=chip.npix_x,
ysize=chip.npix_y, ysize=chip.npix_y,
sigma=0.01, sigma=0.01,
seed=seed) seed=seed)
img *= prnu_img img *= prnu_img
return img, prnu_img return img, prnu_img
def get_poisson(seed=0, sky_level=0.): def get_poisson(seed=0, sky_level=0.):
rng_poisson = galsim.BaseDeviate(seed) rng_poisson = galsim.BaseDeviate(seed)
poisson_noise = galsim.PoissonNoise(rng_poisson, sky_level=sky_level) poisson_noise = galsim.PoissonNoise(rng_poisson, sky_level=sky_level)
return rng_poisson, poisson_noise return rng_poisson, poisson_noise
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 == 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)
base_img1 = base_level * np.ones_like(img.array) base_img1 = base_level * np.ones_like(img.array)
else: else:
...@@ -186,53 +220,60 @@ def get_base_img(img, chip, read_noise, readout_time, dark_noise, exptime=150., ...@@ -186,53 +220,60 @@ def get_base_img(img, chip, read_noise, readout_time, dark_noise, exptime=150.,
arr = np.broadcast_to(arr, (ny, nx)) arr = np.broadcast_to(arr, (ny, nx))
base_img2 = np.zeros_like(img.array) base_img2 = np.zeros_like(img.array)
base_img2[:ny, :] = arr base_img2[:ny, :] = arr
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
def add_poisson(img, chip, exptime=150., seed=0, sky_level=0., poisson_noise=None, dark_noise=None, InputDark=None): def add_poisson(img, chip, exptime=150., seed=0, sky_level=0., poisson_noise=None, dark_noise=None, InputDark=None):
if poisson_noise is None: if poisson_noise is None:
_, poisson_noise = get_poisson(seed=seed, sky_level=sky_level) _, poisson_noise = get_poisson(seed=seed, sky_level=sky_level)
read_noise = chip.read_noise read_noise = chip.read_noise
if dark_noise is None: if dark_noise is None:
dark_noise = chip.dark_noise dark_noise = chip.dark_noise
base_img = get_base_img(img=img, chip=chip, read_noise=read_noise, readout_time=chip.readout_time, dark_noise=dark_noise, exptime=exptime, InputDark=InputDark) base_img = get_base_img(img=img, chip=chip, read_noise=read_noise, readout_time=chip.readout_time,
dark_noise=dark_noise, exptime=exptime, InputDark=InputDark)
img += base_img img += base_img
img.addNoise(poisson_noise) img.addNoise(poisson_noise)
# img -= read_noise**2 # img -= read_noise**2
if InputDark != None: if InputDark != None:
hdu = fits.open(InputDark) ##"Instrument/data/dark/dark_1000s_example_0.fits" # "Instrument/data/dark/dark_1000s_example_0.fits"
hdu = fits.open(InputDark)
img += hdu[0].data/hdu[0].header['exptime']*exptime img += hdu[0].data/hdu[0].header['exptime']*exptime
hdu.close() hdu.close()
return img, base_img return img, base_img
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.libBF').joinpath("libmoduleBF.so") as lib_path: with pkg_resources.files('observation_sim.instruments.chip.libBF').joinpath("libmoduleBF.so") as 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.libBF', "libmoduleBF.so") as lib_path: with pkg_resources.path('observation_sim.instruments.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]
# Set bit flag # Set bit flag
bit_flag = 1 bit_flag = 1
bit_flag = bit_flag | (1 << 2) bit_flag = bit_flag | (1 << 2)
nx, ny = img.array.shape nx, ny = img.array.shape
nn = nx * ny nn = nx * ny
arr_ima= (ctypes.c_float*nn)() arr_ima = (ctypes.c_float*nn)()
arr_imc= (ctypes.c_float*nn)() arr_imc = (ctypes.c_float*nn)()
arr_ima[:]= img.array.reshape(nn) arr_ima[:] = img.array.reshape(nn)
arr_imc[:]= np.zeros(nn) arr_imc[:] = np.zeros(nn)
lib_bf.addEffects(nx, ny, arr_ima, arr_imc, bit_flag) lib_bf.addEffects(nx, ny, arr_ima, arr_imc, bit_flag)
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): 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" fname = "/share/home/weichengliang/CSST_git/test_new_sim/csst-simulation/ObservationSim/Instrument/data/dark/dark_1000s_example_0.fits"
...@@ -244,65 +285,81 @@ def add_inputdark(img, chip, exptime): ...@@ -244,65 +285,81 @@ def add_inputdark(img, chip, exptime):
del inputdark del inputdark
return img return img
""" """
def AddPreScan(GSImage, pre1=27, pre2=4, over1=71, over2=80, nsecy = 2, nsecx=8):
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:
tx = ix tx = ix
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] # 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)
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:
imgtemp[pre2:pre2+dy, pre1:pre1+dx] = img[iy*dy:(iy+1)*dy, ix*dx:(ix+1)*dx] imgtemp[pre2:pre2+dy, pre1:pre1 +
dx] = img[iy*dy:(iy+1)*dy, ix*dx:(ix+1)*dx]
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 +
imgt[chunkidx, :, :] = imgtemp #[:, ::-1] dx] = img[iy*dy:(iy+1)*dy, ix*dx:(ix+1)*dx]
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 +
imgt[chunkidx, :, :] = imgtemp #[::-1, ::-1] dx] = img[iy*dy:(iy+1)*dy, ix*dx:(ix+1)*dx]
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 +
imgt[chunkidx, :, :] = imgtemp #[::-1, :] dx] = img[iy*dy:(iy+1)*dy, ix*dx:(ix+1)*dx]
imgt[chunkidx, :, :] = imgtemp # [::-1, :]
imgtx1 = np.hstack(imgt[:nsecx:, :, :]) #hstack chunk(1,2)-[1,2,3,4,5,6,7,8] # 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] imgtx1 = np.hstack(imgt[:nsecx:, :, :])
# hstack chunk(4,3)-[16,15,14,13,12,11,,10,9]
imgtx2 = np.hstack(imgt[:(nsecx-1):-1, :, :])
newimg = galsim.Image(int(nx+(pre1+over1)*nsecx), int(ny+(pre2+over2)*nsecy), init_value=0) newimg = galsim.Image(int(nx+(pre1+over1)*nsecx),
newimg.array[:, :] = np.concatenate([imgtx1, imgtx2]) #vstack chunk(1,2) & chunk(4,3) int(ny+(pre2+over2)*nsecy), init_value=0)
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
def AddPreScanFO(GSImage, pre1=27, pre2=4, over1=71, over2=80, nsecy = 1, nsecx=16):
img= GSImage.array def AddPreScanFO(GSImage, pre1=27, pre2=4, over1=71, over2=80, nsecy=1, nsecx=16):
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)
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)
for ix in range(nsecx): for ix in range(nsecx):
newimg.array[pre2:pre2+dy, pre1+ix*(dx+pre1+over1):pre1+dx+ix*(dx+pre1+over1)] = img[0:dy, 0+ix*dx:dx+ix*dx] newimg.array[pre2:pre2+dy, pre1+ix *
(dx+pre1+over1):pre1+dx+ix*(dx+pre1+over1)] = img[0:dy, 0+ix*dx:dx+ix*dx]
newimg.wcs = GSImage.wcs newimg.wcs = GSImage.wcs
return newimg return newimg
def formatOutput(GSImage, nsecy = 2, nsecx=8): def formatOutput(GSImage, nsecy=2, nsecx=8):
img = GSImage.array 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(nsecx*nsecy), dy, dx]) imgt = np.zeros([int(nsecx*nsecy), dy, dx])
for iy in range(nsecy): for iy in range(nsecy):
for ix in range(nsecx): for ix in range(nsecx):
...@@ -320,17 +377,18 @@ def formatOutput(GSImage, nsecy = 2, nsecx=8): ...@@ -320,17 +377,18 @@ def formatOutput(GSImage, nsecy = 2, nsecx=8):
imgt[chunkidx, :, :] = img[iy*dy:(iy+1)*dy, ix*dx:(ix+1)*dx] imgt[chunkidx, :, :] = img[iy*dy:(iy+1)*dy, ix*dx:(ix+1)*dx]
if int(chunkidx/4) == 3: if int(chunkidx/4) == 3:
imgt[chunkidx, :, :] = img[iy*dy:(iy+1)*dy, ix*dx:(ix+1)*dx] imgt[chunkidx, :, :] = img[iy*dy:(iy+1)*dy, ix*dx:(ix+1)*dx]
imgttx0 = np.hstack(imgt[ 0:4:, :, :]) imgttx0 = np.hstack(imgt[0:4:, :, :])
imgttx1 = np.hstack(imgt[ 4:8:, :, ::-1]) imgttx1 = np.hstack(imgt[4:8:, :, ::-1])
imgttx2 = np.hstack(imgt[8:12:, ::-1, ::-1]) imgttx2 = np.hstack(imgt[8:12:, ::-1, ::-1])
imgttx3 = np.hstack(imgt[12:16:,::-1, :]) imgttx3 = np.hstack(imgt[12:16:, ::-1, :])
newimg = galsim.Image(int(dx*nsecx*nsecy), dy, init_value=0) newimg = galsim.Image(int(dx*nsecx*nsecy), dy, init_value=0)
newimg.array[:, :] = np.hstack([imgttx0, imgttx1, imgttx2, imgttx3]) newimg.array[:, :] = np.hstack([imgttx0, imgttx1, imgttx2, imgttx3])
return newimg return newimg
def formatRevert(GSImage, nsecy = 1, nsecx=16):
def formatRevert(GSImage, nsecy=1, nsecx=16):
img = GSImage.array img = GSImage.array
ny, nx = img.shape ny, nx = img.shape
dx = int(nx/nsecx) dx = int(nx/nsecx)
...@@ -338,17 +396,20 @@ def formatRevert(GSImage, nsecy = 1, nsecx=16): ...@@ -338,17 +396,20 @@ def formatRevert(GSImage, nsecy = 1, nsecx=16):
newimg = galsim.Image(int(dx*8), int(dy*2), init_value=0) newimg = galsim.Image(int(dx*8), int(dy*2), init_value=0)
for ix in range(0,4): for ix in range(0, 4):
tx = ix tx = ix
newimg.array[0:dy, 0+tx*dx:dx+tx*dx] = img[:, 0+ix*dx:dx+ix*dx] newimg.array[0:dy, 0+tx*dx:dx+tx*dx] = img[:, 0+ix*dx:dx+ix*dx]
for ix in range(4,8): for ix in range(4, 8):
tx = ix tx = ix
newimg.array[0:dy, 0+tx*dx:dx+tx*dx] = img[:, 0+ix*dx:dx+ix*dx][:, ::-1] newimg.array[0:dy, 0+tx*dx:dx+tx *
for ix in range(8,12): dx] = img[:, 0+ix*dx:dx+ix*dx][:, ::-1]
for ix in range(8, 12):
tx = 7-(ix-8) tx = 7-(ix-8)
newimg.array[0+dy:dy+dy, 0+tx*dx:dx+tx*dx] = img[:, 0+ix*dx:dx+ix*dx][::-1, ::-1] newimg.array[0+dy:dy+dy, 0+tx*dx:dx+tx *
for ix in range(12,16): dx] = img[:, 0+ix*dx:dx+ix*dx][::-1, ::-1]
for ix in range(12, 16):
tx = 7-(ix-8) tx = 7-(ix-8)
newimg.array[0+dy:dy+dy, 0+tx*dx:dx+tx*dx] = img[:, 0+ix*dx:dx+ix*dx][::-1, :] newimg.array[0+dy:dy+dy, 0+tx*dx:dx+tx *
dx] = img[:, 0+ix*dx:dx+ix*dx][::-1, :]
return newimg return newimg
from ctypes import CDLL, POINTER, c_int, c_double,c_float,c_long,c_char_p from ctypes import CDLL, POINTER, c_int, c_double, c_float, c_long, c_char_p
from numpy.ctypeslib import ndpointer from numpy.ctypeslib import ndpointer
import numpy.ctypeslib as clb import numpy.ctypeslib as clb
import numpy as np import numpy as np
...@@ -10,12 +10,12 @@ import os ...@@ -10,12 +10,12 @@ import os
lib_path = os.path.dirname(os.path.realpath(__file__)) lib_path = os.path.dirname(os.path.realpath(__file__))
#lib_path += "/add_CTI.so" # lib_path += "/add_CTI.so"
lib_path += "/libmoduleCTI.so" lib_path += "/libmoduleCTI.so"
lib = CDLL(lib_path) lib = CDLL(lib_path)
CTI_simul = lib.__getattr__('CTI_simul') CTI_simul = lib.__getattr__('CTI_simul')
CTI_simul.argtypes = [POINTER(POINTER(c_int)),c_int,c_int,c_int,c_int,POINTER(c_float),POINTER(c_float),\ CTI_simul.argtypes = [POINTER(POINTER(c_int)), c_int, c_int, c_int, c_int, POINTER(c_float), POINTER(c_float),
c_float,c_float,c_float,c_int,POINTER(c_int),c_int,POINTER(POINTER(c_int))] c_float, c_float, c_float, c_int, POINTER(c_int), c_int, POINTER(POINTER(c_int))]
''' '''
get_trap_h = lib.__getattr__('save_trap_map') get_trap_h = lib.__getattr__('save_trap_map')
get_trap_h.argtypes = [POINTER(c_int), c_int, c_int, c_int, c_int, POINTER(c_float), c_float, c_float, c_char_p] get_trap_h.argtypes = [POINTER(c_int), c_int, c_int, c_int, c_int, POINTER(c_float), c_float, c_float, c_char_p]
...@@ -45,42 +45,50 @@ def bin2fits(bin_file,fits_dir,nsp,nx,ny,nmax): ...@@ -45,42 +45,50 @@ def bin2fits(bin_file,fits_dir,nsp,nx,ny,nmax):
datai[j+1,:,:] = h datai[j+1,:,:] = h
fits.writeto(fits_dir+"/trap_"+str(i+1)+".fits",datai,overwrite=True) fits.writeto(fits_dir+"/trap_"+str(i+1)+".fits",datai,overwrite=True)
''' '''
def numpy_matrix_to_int_pointer(arr): def numpy_matrix_to_int_pointer(arr):
int_pointer_array = (POINTER(c_int)*arr.shape[0])() int_pointer_array = (POINTER(c_int)*arr.shape[0])()
for i in range(arr.shape[0]): for i in range(arr.shape[0]):
arr1 = np.array(arr[i].copy().tolist(),dtype=np.int32) arr1 = np.array(arr[i].copy().tolist(), dtype=np.int32)
int_pointer_array[i] = np.ctypeslib.as_ctypes(arr1) int_pointer_array[i] = np.ctypeslib.as_ctypes(arr1)
return int_pointer_array return int_pointer_array
def pointer_to_numpy_matrix(arr_pointer,row,col):
arr = np.zeros((row,col))
def pointer_to_numpy_matrix(arr_pointer, row, col):
arr = np.zeros((row, col))
for i in range(row): for i in range(row):
for j in range(col): for j in range(col):
arr[i,j] = arr_pointer[i][j] arr[i, j] = arr_pointer[i][j]
return arr return arr
def CTI_sim(im,nx,ny,noverscan,nsp,nmax,beta,w,c,t,rho_trap,trap_seeds,release_seed=0):
def CTI_sim(im, nx, ny, noverscan, nsp, nmax, beta, w, c, t, rho_trap, trap_seeds, release_seed=0):
image = im.T image = im.T
nx_c,ny_c,noverscan_c,nsp_c,nmax_c = c_int(nx),c_int(ny),c_int(noverscan),c_int(nsp),c_int(nmax) nx_c, ny_c, noverscan_c, nsp_c, nmax_c = c_int(nx), c_int(
ny), c_int(noverscan), c_int(nsp), c_int(nmax)
ntotal = ny+noverscan ntotal = ny+noverscan
beta_c,w_c,c_c = c_float(beta),c_float(w),c_float(c) beta_c, w_c, c_c = c_float(beta), c_float(w), c_float(c)
t_p = np.ctypeslib.as_ctypes(t) t_p = np.ctypeslib.as_ctypes(t)
rho_trap_p = np.ctypeslib.as_ctypes(rho_trap) rho_trap_p = np.ctypeslib.as_ctypes(rho_trap)
image_p = numpy_matrix_to_int_pointer(image) image_p = numpy_matrix_to_int_pointer(image)
trap_seeds1 = trap_seeds.astype(np.int32) trap_seeds1 = trap_seeds.astype(np.int32)
trap_seeds_p = np.ctypeslib.as_ctypes(trap_seeds1) trap_seeds_p = np.ctypeslib.as_ctypes(trap_seeds1)
release_seed_c = c_int(release_seed) release_seed_c = c_int(release_seed)
image_cti = np.zeros((nx,ntotal)) image_cti = np.zeros((nx, ntotal))
image_cti = image_cti.astype(np.int32) image_cti = image_cti.astype(np.int32)
image_cti_p = numpy_matrix_to_int_pointer(image_cti) image_cti_p = numpy_matrix_to_int_pointer(image_cti)
print(datetime.now()) print(datetime.now())
CTI_simul(image_p,nx,ny,noverscan,nsp,rho_trap_p,t_p,beta,w,c,nmax,trap_seeds_p,release_seed_c,image_cti_p) CTI_simul(image_p, nx, ny, noverscan, nsp, rho_trap_p, t_p, beta,
w, c, nmax, trap_seeds_p, release_seed_c, image_cti_p)
print(datetime.now()) print(datetime.now())
image_cti_result = np.zeros((nx,ntotal)) image_cti_result = np.zeros((nx, ntotal))
for i in range(nx): for i in range(nx):
for j in range(ntotal): for j in range(ntotal):
image_cti_result[i,j] = image_cti_p[i][j] image_cti_result[i, j] = image_cti_p[i][j]
return image_cti_result.T return image_cti_result.T
""" """
if __name__ =='__main__': if __name__ =='__main__':
nx,ny,noverscan,nsp,nmax = 4608,4616,84,3,10 nx,ny,noverscan,nsp,nmax = 4608,4616,84,3,10
......
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