Commit 8df06b27 authored by Fang Yuedong's avatar Fang Yuedong
Browse files

Merge branch 'sim_scheduler' into develop

parents 81e2570f 93270bbf
import numpy as np
import os
import shutil
import yaml
import galsim
import numpy as np
from astropy.time import Time
from ObservationSim.Config._util import get_obs_id
import ObservationSim.Instrument._util as _util
class Pointing(object):
def __init__(self, id=0, ra=0., dec=0., img_pa=0., timestamp=1621915200, sat_x=0., sat_y=0., sat_z=0., sun_x=0., sun_y=0., sun_z=0., sat_vx=0., sat_vy=0., sat_vz=0., exp_time=150., pointing_type='MS'):
def __init__(self, id=0, ra=0., dec=0., img_pa=0., timestamp=1621915200, sat_x=0., sat_y=0., sat_z=0., sun_x=0., sun_y=0., sun_z=0., sat_vx=0., sat_vy=0., sat_vz=0., exp_time=150., pointing_type='SCI', pointing_type_code='101', pointing_id = '00000001', obs_config_file=None, t_shutter_open = 1.3, t_shutter_close = 1.3):
self.id = id
self.ra = ra
self.dec = dec
......@@ -14,15 +20,58 @@ class Pointing(object):
self.sat_vx, self.sat_vy, self.sat_vz = sat_vx, sat_vy, sat_vz
self.exp_time = exp_time
self.pointing_type = pointing_type
self.pointing_type_code = pointing_type_code
self.obs_id = pointing_id
self.survey_field_type = 'WIDE'
self.jdt = 0.
self.obs_config_file = obs_config_file
self.t_shutter_open = t_shutter_open
self.t_shutter_close = t_shutter_close
self.output_dir = "."
if self.obs_config_file is not None:
with open(self.obs_config_file, "r") as stream:
try:
self.obs_param = yaml.safe_load(stream)
except yaml.YAMLError as exc:
print(exc)
if self.obs_param["obs_type"]:
self.pointing_type = self.obs_param["obs_type"]
if self.obs_param["obs_type_code"]:
self.pointing_type_code = self.obs_param["obs_type_code"]
if self.obs_param["obs_id"]:
self.obs_id = str(self.obs_param["obs_id"])
def get_full_depth_exptime(self, filter_type):
if self.survey_field_type == 'WIDE':
if filter_type in _util.SPEC_FILTERS:
return 150. * 4
else:
if filter_type.lower() in ['nuv', 'y']:
return 150. * 4
elif filter_type.lower() in ['u', 'g', 'r', 'i', 'z']:
return 150. * 2
else:
return max(150., self.exp_time) # [TODO] for FGS
elif self.survey_field_type == 'DEEP':
if filter_type in _util.SPEC_FILTERS:
return 250. * 4 * 4
else:
if filter_type.lower() in ['nuv', 'y']:
return 250. * 4 * 4
elif filter_type.lower() in ['u', 'g', 'r', 'i', 'z']:
return 250. * 2 * 4
else:
return max(150., self.exp_time) # [TODO] for FGS
def read_pointing_columns(self, columns, id=0, t=1621915200, pointing_type='MS'):
def read_pointing_columns(self, columns, id=0, t=1621915200, pointing_type='SCI'):
self.id = id
col_len = len(columns)
self.ra = float(columns[0])
self.dec = float(columns[1])
self.img_pa = float(columns[4]) * galsim.degrees
self.pointing_type = pointing_type
# self.pointing_type = pointing_type
if col_len > 5:
jdt = np.double(columns[5])
t_temp = Time(jdt, format='jd')
......@@ -33,10 +82,55 @@ class Pointing(object):
self.sat_z = float(columns[8])
self.sun_x = float(columns[9])
self.sun_y = float(columns[10])
self.sun_z = float(columns[1])
self.sun_z = float(columns[11])
self.sat_vx = float(columns[15])
self.sat_vy = float(columns[16])
self.sat_vz = float(columns[17])
self.exp_time = float(columns[18])
is_deep = float(columns[19])
# [TODO] Can also define other survey types
if is_deep != -1.0:
self.survey_field_type = "DEEP"
if not self.obs_config_file:
self.obs_config_file = str(columns[20])
with open(self.obs_config_file, "r") as stream:
try:
self.obs_param = yaml.safe_load(stream)
except yaml.YAMLError as exc:
print(exc)
self.pointing_type_code = columns[21][0:3]
self.obs_id = columns[21][3:]
self.pointing_type = self.obs_param["obs_type"]
else:
self.timestamp = t
def make_output_pointing_dir(self, overall_config, copy_obs_config=False):
run_dir = os.path.join(overall_config["work_dir"], overall_config["run_name"])
if not os.path.exists(run_dir):
try:
os.makedirs(run_dir, exist_ok=True)
except OSError:
pass
self.output_prefix = get_obs_id(
img_type=self.pointing_type,
project_cycle=overall_config["project_cycle"],
run_counter=overall_config["run_counter"],
pointing_id=self.obs_id,
pointing_type_code = self.pointing_type_code)
self.output_dir = os.path.join(run_dir, self.output_prefix)
if not os.path.exists(self.output_dir):
try:
os.makedirs(self.output_dir, exist_ok=True)
except OSError:
pass
if copy_obs_config and self.obs_config_file:
obs_config_output_path = os.path.join(self.output_dir, os.path.basename(self.obs_config_file))
if not os.path.exists(obs_config_output_path):
try:
shutil.copy(self.obs_config_file, self.output_dir)
except OSError:
pass
from .Config import *
from .ChipOutput import ChipOutput
from .Pointing import Pointing
\ No newline at end of file
def get_obs_id(img_type='SCI', project_cycle=6, run_counter=0, pointing_id='00000001',pointing_type_code='101'):
# obs_type = {'SCI': '01', 'BIAS': '03', 'DARK': '07', 'FLAT': '11', 'CRS': '98', 'CRD': '99'}
# obs_type = {'SCIE': '01', 'BIAS': '03', 'DARK': '07', 'FLAT': '11', 'CRS': '98', 'CRD': '99', 'CAL': '01'}
obs_id = pointing_type_code + str(int(project_cycle)).rjust(2, '0') + str(int(run_counter)) + pointing_id
return obs_id
# def get_obs_id(img_type='SCI', project_cycle=6, run_counter=0, pointing_num=0):
# # obs_type = {'SCI': '01', 'BIAS': '03', 'DARK': '07', 'FLAT': '11', 'CRS': '98', 'CRD': '99'}
# obs_type = {'SCIE': '01', 'BIAS': '03', 'DARK': '07', 'FLAT': '11', 'CRS': '98', 'CRD': '99', 'CAL': '01'}
# obs_id = '1'+ obs_type[img_type] + str(int(project_cycle)).rjust(2, '0') + str(int(run_counter)) + str(pointing_num).rjust(8,'0')
# return obs_id
def get_file_type(img_type='SCI'):
file_type = {'SCI':'SCI', 'BIAS':'BIAS', 'DARK':'DARK', 'FLAT':'FLAT', 'CRS':'CRS', 'CRD':'CRD','CALS':'CALS','CALF':'CALF'}
return file_type[img_type]
\ No newline at end of file
......@@ -3,6 +3,7 @@ import os
import numpy as np
import pickle
import json
import ObservationSim.Instrument._util as _util
from astropy.table import Table
from numpy.random import Generator, PCG64
from astropy.io import fits
......@@ -12,6 +13,9 @@ from ObservationSim.Instrument.Chip import Effects as effects
from ObservationSim.Instrument.FocalPlane import FocalPlane
from ObservationSim.Config.Header import generatePrimaryHeader, generateExtensionHeader
from ObservationSim.Instrument._util import rotate_conterclockwise
from ObservationSim.Instrument.Chip import ChipUtils as chip_utils
from ObservationSim.Instrument.Chip.libCTI.CTI_modeling import CTI_sim
try:
import importlib.resources as pkg_resources
......@@ -22,11 +26,7 @@ except ImportError:
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
# TODO: use chipID to config individual chip?
super().__init__()
# self.npix_x = 9216
# self.npix_y = 9232
# self.pix_scale = 0.074 # pixel scale
self.nsecy = 2
self.nsecx = 8
self.gain_channel = np.ones(self.nsecy* self.nsecx)
......@@ -42,12 +42,10 @@ class Chip(FocalPlane):
self.filter_id, self.filter_type = self.getChipFilter()
self.survey_type = self._getSurveyType()
# [TODO]
if self.filter_type != "FGS":
self._getChipRowCol()
# Set the relavent specs for FGS detectors
# [TODO]
# Set the relavent specs for detectors
try:
with pkg_resources.files('ObservationSim.Instrument.data.ccd').joinpath("chip_definition.json") as chip_definition:
with open(chip_definition, "r") as f:
......@@ -58,10 +56,9 @@ class Chip(FocalPlane):
chip_dict = json.load(f)[str(self.chipID)]
for key in chip_dict:
setattr(self, key, chip_dict[key])
if self.filter_type == "FGS":
if ("field_dist" in config) and (config["ins_effects"]["field_dist"]) == False:
self.fdModel = None
else:
if self.filter_type == "FGS":
fgs_name = self.chip_name[0:4]
try:
with pkg_resources.files('ObservationSim.Instrument.data.field_distortion').joinpath("FieldDistModelGlobal_pr4_%s.pickle"%(fgs_name.lower())) as field_distortion:
......@@ -73,11 +70,7 @@ class Chip(FocalPlane):
self.fdModel = pickle.load(f)
else:
# Get the corresponding field distortion model
if ("field_dist" in config) and (config["ins_effects"]["field_dist"] == False):
self.fdModel = None
else:
try:
# with pkg_resources.files('ObservationSim.Instrument.data.field_distortion').joinpath("FieldDistModelGlobal_mainFP_v1.0.pickle") as field_distortion:
with pkg_resources.files('ObservationSim.Instrument.data.field_distortion').joinpath("FieldDistModel_v2.0.pickle") as field_distortion:
with open(field_distortion, "rb") as f:
self.fdModel = pickle.load(f)
......@@ -88,9 +81,11 @@ class Chip(FocalPlane):
# Get boundary (in pix)
self.bound = self.getChipLim()
self.ccdEffCurve_dir = ccdEffCurve_dir
self.CRdata_dir = CRdata_dir
slsconfs = self.getChipSLSConf()
slsconfs = chip_utils.getChipSLSConf(chipID=self.chipID)
if np.size(slsconfs) == 1:
try:
with pkg_resources.files('ObservationSim.Instrument.data.sls_conf').joinpath(slsconfs) as conf_path:
......@@ -117,10 +112,7 @@ class Chip(FocalPlane):
self.effCurve = self._getChipEffCurve(self.filter_type)
self._getCRdata()
# Define the sensor model
if "bright_fatter" in config["ins_effects"] and config["ins_effects"]["bright_fatter"] == True and self.survey_type == "photometric":
self.sensor = galsim.SiliconSensor(strength=self.df_strength, treering_func=treering_func)
else:
# # Define the sensor model
self.sensor = galsim.Sensor()
self.flat_cube = None # for spectroscopic flat field cube simulation
......@@ -132,9 +124,9 @@ class Chip(FocalPlane):
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])
# 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)
......@@ -145,9 +137,9 @@ class Chip(FocalPlane):
return rowID, colID
def _getSurveyType(self):
if self.filter_type in ["GI", "GV", "GU"]:
if self.filter_type in _util.SPEC_FILTERS:
return "spectroscopic"
elif self.filter_type in ["NUV", "u", "g", 'r', 'i', 'z', 'y', 'FGS']:
elif self.filter_type in _util.PHOT_FILTERS:
return "photometric"
# elif self.filter_type in ["FGS"]:
# return "FGS"
......@@ -157,13 +149,6 @@ class Chip(FocalPlane):
if filter_type in ['NUV', 'u', 'GU']: filename = 'UV0.txt'
if filter_type in ['g', 'r', 'GV', 'FGS']: filename = 'Astro_MB.txt' # TODO, need to switch to the right efficiency curvey for FGS CMOS
if filter_type in ['i', 'z', 'y', 'GI']: filename = 'Basic_NIR.txt'
# Mirror efficiency:
# if filter_type == 'NUV': mirror_eff = 0.54
# if filter_type == 'u': mirror_eff = 0.68
# if filter_type in ['g', 'r', 'i', 'z', 'y']: mirror_eff = 0.8
# if filter_type in ['GU', 'GV', 'GI']: mirror_eff = 1. # Not sure if this is right
# path = os.path.join(self.ccdEffCurve_dir, filename)
# table = Table.read(path, format='ascii')
try:
with pkg_resources.files('ObservationSim.Instrument.data.ccd').joinpath(filename) as ccd_path:
table = Table.read(ccd_path, format='ascii')
......@@ -182,12 +167,25 @@ class Chip(FocalPlane):
with pkg_resources.path('ObservationSim.Instrument.data', "wfc-cr-attachpixel.dat") as cr_path:
self.attachedSizes = np.loadtxt(cr_path)
def getChipFilter(self, chipID=None, filter_layout=None):
def loadSLSFLATCUBE(self, flat_fn='flat_cube.fits'):
try:
with pkg_resources.files('ObservationSim.Instrument.data').joinpath(flat_fn) as data_path:
flat_fits = fits.open(data_path, ignore_missing_simple=True)
except AttributeError:
with pkg_resources.path('ObservationSim.Instrument.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 = ["NUV","u", "g", "r", "i","z","y","GU", "GV", "GI", "FGS"]
if filter_layout is not None:
return filter_layout[chipID][0], filter_layout[chipID][1]
filter_type_list = _util.ALL_FILTERS
if chipID == None:
chipID = self.chipID
......@@ -217,33 +215,6 @@ class Chip(FocalPlane):
Returns:
A galsim BoundsD object
"""
if ((chipID is not None) and (int(chipID) <= 30)) or (self.chipID <= 30):
# [TODO]
if chipID == None:
chipID = self.chipID
rowID, colID = self.rowID, self.colID
else:
rowID, colID = self.getChipRowCol(chipID)
gx1, gx2 = self.npix_gap_x
gy = self.npix_gap_y
# xlim of a given CCD chip
xrem = 2*(colID - 1) - (self.nchip_x - 1)
xcen = (self.npix_x//2 + gx1//2) * xrem
if chipID >= 26 or chipID == 21:
xcen = (self.npix_x//2 + gx1//2) * xrem - (gx2-gx1)
if chipID <= 5 or chipID == 10:
xcen = (self.npix_x//2 + gx1//2) * xrem + (gx2-gx1)
nx0 = xcen - self.npix_x//2 + 1
nx1 = xcen + self.npix_x//2
# ylim of a given CCD chip
yrem = (rowID - 1) - self.nchip_y // 2
ycen = (self.npix_y + gy) * yrem
ny0 = ycen - self.npix_y//2 + 1
ny1 = ycen + self.npix_y//2
return galsim.BoundsD(nx0-1, nx1-1, ny0-1, ny1-1)
else:
xmin, xmax, ymin, ymax = 1e10, -1e10, 1e10, -1e10
xcen = self.x_cen / self.pix_size
ycen = self.y_cen / self.pix_size
......@@ -252,7 +223,7 @@ class Chip(FocalPlane):
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 = rotate_conterclockwise(x0=xcen, y0=ycen, x=x, y=y, angle=self.rotate_angle)
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)
......@@ -291,83 +262,8 @@ class Chip(FocalPlane):
noise = self.dark_noise * exptime + self.read_noise**2
return noise
def getChipSLSConf(self):
confFile = ''
if self.chipID == 1: confFile = ['CSST_GI2.conf', 'CSST_GI1.conf']
if self.chipID == 2: confFile = ['CSST_GV4.conf', 'CSST_GV3.conf']
if self.chipID == 3: confFile = ['CSST_GU2.conf', 'CSST_GU1.conf']
if self.chipID == 4: confFile = ['CSST_GU4.conf', 'CSST_GU3.conf']
if self.chipID == 5: confFile = ['CSST_GV2.conf', 'CSST_GV1.conf']
if self.chipID == 10: confFile = ['CSST_GI4.conf', 'CSST_GI3.conf']
if self.chipID == 21: confFile = ['CSST_GI6.conf', 'CSST_GI5.conf']
if self.chipID == 26: confFile = ['CSST_GV8.conf', 'CSST_GV7.conf']
if self.chipID == 27: confFile = ['CSST_GU6.conf', 'CSST_GU5.conf']
if self.chipID == 28: confFile = ['CSST_GU8.conf', 'CSST_GU7.conf']
if self.chipID == 29: confFile = ['CSST_GV6.conf', 'CSST_GV5.conf']
if self.chipID == 30: confFile = ['CSST_GI8.conf', 'CSST_GI7.conf']
return confFile
def generateHeader(self, ra_cen, dec_cen, img_rot, im_type, pointing_ID, exptime=150., timestamp = 1621915200):
datetime_obs = datetime.utcfromtimestamp(timestamp)
date_obs = datetime_obs.strftime("%y%m%d")
time_obs = datetime_obs.strftime("%H%M%S")
h_prim = generatePrimaryHeader(
xlen=self.npix_x,
ylen=self.npix_y,
pointNum = str(pointing_ID),
ra=ra_cen,
dec=dec_cen,
pixel_scale=self.pix_scale,
date=date_obs,
time_obs=time_obs,
im_type = im_type,
exptime=exptime,
chip_name=str(self.chipID).rjust(2, '0')
)
h_ext = generateExtensionHeader(
chip=self,
xlen=self.npix_x,
ylen=self.npix_y,
ra=ra_cen,
dec=dec_cen,
pa=img_rot.deg,
gain=self.gain,
readout=self.read_noise,
dark=self.dark_noise,
saturation=90000,
pixel_scale=self.pix_scale,
pixel_size=self.pix_size,
xcen=self.x_cen,
ycen=self.y_cen,
extName='SCI',
timestamp = timestamp,
exptime = exptime,
readoutTime = 40.)
return h_prim, h_ext
def outputCal(self, img, ra_cen, dec_cen, img_rot, im_type, pointing_ID, output_dir, exptime=150., timestamp = 1621915200):
h_prim, h_ext = self.generateHeader(
ra_cen=ra_cen,
dec_cen=dec_cen,
img_rot=img_rot,
im_type=im_type,
pointing_ID=pointing_ID,
exptime=exptime,
timestamp = timestamp)
hdu1 = fits.PrimaryHDU(header=h_prim)
hdu1.add_checksum()
hdu1.header.comments['CHECKSUM'] = 'HDU checksum'
hdu1.header.comments['DATASUM'] = 'data unit checksum'
hdu2 = fits.ImageHDU(img.array, header=h_ext)
hdu2.add_checksum()
hdu2.header.comments['XTENSION'] = 'extension type'
hdu2.header.comments['CHECKSUM'] = 'HDU checksum'
hdu2.header.comments['DATASUM'] = 'data unit checksum'
hdu1 = fits.HDUList([hdu1, hdu2])
fname = os.path.join(output_dir, h_prim['FILENAME']+'.fits')
hdu1.writeto(fname, output_verify='ignore', overwrite=True)
def addEffects(self, config, img, chip_output, filt, ra_cen, dec_cen, img_rot, exptime=150., pointing_ID=0, timestamp_obs=1621915200, pointing_type='MS', sky_map=None, tel=None, logger=None):
def addEffects(self, config, img, chip_output, filt, ra_cen, dec_cen, img_rot, exptime=150., pointing_ID=0, timestamp_obs=1621915200, pointing_type='SCI', sky_map=None, post_flash_map=None, tel=None, logger=None):
# Set random seeds
SeedGainNonuni=int(config["random_seeds"]["seed_gainNonUniform"])
SeedBiasNonuni=int(config["random_seeds"]["seed_biasNonUniform"])
SeedRnNonuni = int(config["random_seeds"]["seed_rnNonUniform"])
......@@ -386,54 +282,31 @@ class Chip(FocalPlane):
self.logger = logger
# Get Poisson noise generator
seed = int(config["random_seeds"]["seed_poisson"]) + pointing_ID*30 + self.chipID
rng_poisson = galsim.BaseDeviate(seed)
poisson_noise = galsim.PoissonNoise(rng_poisson, sky_level=0.)
rng_poisson, poisson_noise = chip_utils.get_poisson(
seed=int(config["random_seeds"]["seed_poisson"]) + pointing_ID*30 + self.chipID, sky_level=0.)
# Add sky background
if sky_map is None:
sky_map = filt.getSkyNoise(exptime=exptime)
sky_map = sky_map * np.ones_like(img.array)
sky_map = galsim.Image(array=sky_map)
# Apply Poisson noise to the sky map
# (NOTE): only for photometric chips
# since it utilize the photon shooting
# to draw stamps
if self.survey_type == "photometric":
sky_map.addNoise(poisson_noise)
elif img.array.shape != sky_map.shape:
raise ValueError("The shape img and sky_map must be equal.")
elif tel is not None: # If sky_map is given in flux
sky_map = sky_map * tel.pupil_area * exptime
if config["ins_effects"]["add_back"] == True:
img += sky_map
img, sky_map = chip_utils.add_sky_background(img=img, filt=filt, exptime=exptime, sky_map=sky_map, tel=tel)
del sky_map
# Apply flat-field large scale structure for one chip
if config["ins_effects"]["flat_fielding"] == True:
if self.logger is not None:
self.logger.info(" Creating and applying Flat-Fielding")
msg = str(img.bounds)
self.logger.info(msg)
else:
print(" Creating and applying Flat-Fielding", flush=True)
print(img.bounds, flush=True)
flat_img = effects.MakeFlatSmooth(
img.bounds,
int(config["random_seeds"]["seed_flat"]))
flat_normal = flat_img / np.mean(flat_img.array)
chip_utils.log_info(msg=" Creating and applying Flat-Fielding", logger=self.logger)
chip_utils.log_info(msg=str(img.bounds), logger=self.logger)
flat_img, flat_normal = chip_utils.get_flat(img=img, seed=int(config["random_seeds"]["seed_flat"]))
if self.survey_type == "photometric":
img *= flat_normal
del flat_normal
if config["output_setting"]["flat_output"] == False:
del flat_img
if post_flash_map is not None:
img = img + post_flash_map
# Apply Shutter-effect for one chip
if config["ins_effects"]["shutter_effect"] == True:
if self.logger is not None:
self.logger.info(" Apply shutter effect")
else:
print(" Apply shutter effect", flush=True)
chip_utils.log_info(msg=" Apply shutter effect", logger=self.logger)
shuttimg = effects.ShutterEffectArr(img, t_shutter=1.3, dist_bearing=735, dt=1E-3) # shutter effect normalized image for this chip
if self.survey_type == "photometric":
img *= shuttimg
......@@ -442,37 +315,19 @@ class Chip(FocalPlane):
shutt_gsimg.write("%s/ShutterEffect_%s_1.fits" % (chip_output.subdir, self.chipID))
del shutt_gsimg
del shuttimg
# Add Poisson noise to the resulting images
# (NOTE): this can only applied to the slitless image
# since it dose not use photon shooting to draw stamps
if self.survey_type == "spectroscopic":
img.addNoise(poisson_noise)
# # Add Poisson noise to the resulting images
# # (NOTE): this can only applied to the slitless image
# # since it dose not use photon shooting to draw stamps
# if self.survey_type == "spectroscopic":
# img.addNoise(poisson_noise)
# Add cosmic-rays
if config["ins_effects"]["cosmic_ray"] == True and pointing_type=='MS':
if self.logger is not None:
self.logger.info((" Adding Cosmic-Ray"))
else:
print(" Adding Cosmic-Ray", flush=True)
cr_map, cr_event_num = effects.produceCR_Map(
xLen=self.npix_x, yLen=self.npix_y,
exTime=exptime+0.5*self.readout_time,
cr_pixelRatio=0.003*(exptime+0.5*self.readout_time)/600.,
gain=self.gain,
attachedSizes=self.attachedSizes,
seed=SeedCosmicRay+pointing_ID*30+self.chipID) # seed: obj-imaging:+0; bias:+1; dark:+2; flat:+3;
img += cr_map
cr_map[cr_map > 65535] = 65535
cr_map[cr_map < 0] = 0
crmap_gsimg = galsim.Image(cr_map, dtype=np.uint16)
del cr_map
# crmap_gsimg.write("%s/CosmicRay_%s_1.fits" % (chip_output.subdir, self.chipID))
# crmap_gsimg.write("%s/CosmicRay_%s.fits" % (chip_output.subdir, self.chipID))
# datetime_obs = datetime.utcfromtimestamp(timestamp_obs)
# date_obs = datetime_obs.strftime("%y%m%d")
# time_obs = datetime_obs.strftime("%H%M%S")
self.outputCal(
if config["ins_effects"]["cosmic_ray"] == True and pointing_type=='SCI':
chip_utils.log_info(msg=" Adding Cosmic-Ray", logger=self.logger)
img, crmap_gsimg, cr_event_num = chip_utils.add_cosmic_rays(img=img, chip=self, exptime=exptime,
seed=SeedCosmicRay+pointing_ID*30+self.chipID)
chip_utils.outputCal(
chip=self,
img=crmap_gsimg,
ra_cen=ra_cen,
dec_cen=dec_cen,
......@@ -481,30 +336,39 @@ class Chip(FocalPlane):
pointing_ID=pointing_ID,
output_dir=chip_output.subdir,
exptime=exptime,
project_cycle=config["project_cycle"],
run_counter=config["run_counter"],
timestamp=timestamp_obs)
del crmap_gsimg
# Apply PRNU effect and output PRNU flat file:
if config["ins_effects"]["prnu_effect"] == True:
if self.logger is not None:
self.logger.info(" Applying PRNU effect")
else:
print(" Applying PRNU effect", flush=True)
prnu_img = effects.PRNU_Img(
xsize=self.npix_x,
ysize=self.npix_y,
sigma=0.01,
chip_utils.log_info(msg=" Applying PRNU effect", logger=self.logger)
img, prnu_img = chip_utils.add_PRNU(img=img, chip=self,
seed=int(config["random_seeds"]["seed_prnu"]+self.chipID))
img *= prnu_img
if config["output_setting"]["prnu_output"] == True:
prnu_img.write("%s/FlatImg_PRNU_%s.fits" % (chip_output.subdir,self.chipID))
if config["output_setting"]["flat_output"] == False:
del prnu_img
# Add dark current
# # Add dark current
# if config["ins_effects"]["add_dark"] == True:
# dark_noise = galsim.DeviateNoise(galsim.PoissonDeviate(rng_poisson, self.dark_noise*(exptime+0.5*self.readout_time)))
# img.addNoise(dark_noise)
# Add dark current & Poisson noise
InputDark = False
if config["ins_effects"]["add_dark"] == True:
dark_noise = galsim.DeviateNoise(galsim.PoissonDeviate(rng_poisson, self.dark_noise*(exptime+0.5*self.readout_time)))
img.addNoise(dark_noise)
if InputDark:
img = chip_utils.add_inputdark(img=img, chip=self, exptime=exptime)
else:
img, _ = chip_utils.add_poisson(img=img, chip=self, exptime=exptime, poisson_noise=poisson_noise)
else:
img, _ = chip_utils.add_poisson(img=img, chip=self, exptime=exptime, poisson_noise=poisson_noise, dark_noise=0.)
# Add diffusion & brighter-fatter effects
if config["ins_effects"]["bright_fatter"] == True:
img = chip_utils.add_brighter_fatter(img=img)
# Add Hot Pixels or/and Dead Pixels
rgbadpix = Generator(PCG64(int(SeedDefective+self.chipID)))
......@@ -517,38 +381,80 @@ class Chip(FocalPlane):
# Apply Nonlinearity on the chip image
if config["ins_effects"]["non_linear"] == True:
if self.logger is not None:
self.logger.info(" Applying Non-Linearity on the chip image")
else:
print(" Applying Non-Linearity on the chip image", flush=True)
chip_utils.log_info(msg=" Applying Non-Linearity on the chip image", logger=self.logger)
img = effects.NonLinearity(GSImage=img, beta1=5.e-7, beta2=0)
# Apply CCD Saturation & Blooming
if config["ins_effects"]["saturbloom"] == True:
if self.logger is not None:
self.logger.info(" Applying CCD Saturation & Blooming")
else:
print(" Applying CCD Saturation & Blooming")
chip_utils.log_info(msg=" Applying CCD Saturation & Blooming", logger=self.logger)
img = effects.SaturBloom(GSImage=img, nsect_x=1, nsect_y=1, fullwell=fullwell)
# Apply CTE Effect
###if config["ins_effects"]["cte_trail"] == True:
### chip_utils.log_info(msg=" Apply CTE Effect", logger=self.logger)
### img = effects.CTE_Effect(GSImage=img, threshold=27)
pre1 = self.prescan_x #27
over1= self.overscan_x #71
pre2 = self.prescan_y #0 #4
over2= self.overscan_y #84 #80
if config["ins_effects"]["cte_trail"] == True:
if self.logger is not None:
self.logger.info(" Apply CTE Effect")
else:
print(" Apply CTE Effect")
img = effects.CTE_Effect(GSImage=img, threshold=27)
chip_utils.log_info(msg=" Apply CTE Effect", logger=self.logger)
###img = effects.CTE_Effect(GSImage=img, threshold=27)
###CTI_modeling
### 2*8 -> 1*16 img-layout
img = chip_utils.formatOutput(GSImage=img)
self.nsecy = 1
self.nsecx = 16
img_arr = img.array
ny, nx = img_arr.shape
dx = int(nx/self.nsecx)
dy = int(ny/self.nsecy)
newimg = galsim.Image(nx, int(ny+over2), init_value=0)
for ichannel in range(16):
print('\n***add CTI effects: pointing-{:} chip-{:} channel-{:}***'.format(pointing_ID, self.chipID, ichannel+1))
#nx,ny,noverscan,nsp,nmax = 4608,4616,84,3,10
noverscan,nsp,nmax = over2,3,10
beta,w,c = 0.478,84700,0
t = np.array([0.74,7.7,37],dtype=np.float32)
rho_trap = np.array([0.6,1.6,1.4],dtype=np.float32)
trap_seeds = np.array([0,1000,10000],dtype=np.int32) + ichannel + self.chipID*16
release_seed = 50 + ichannel + pointing_ID*30 + self.chipID*16
newimg.array[:, 0+ichannel*dx:dx+ichannel*dx] = CTI_sim(img_arr[:, 0+ichannel*dx:dx+ichannel*dx],dx,dy,noverscan,nsp,nmax,beta,w,c,t,rho_trap,trap_seeds,release_seed)
newimg.wcs = img.wcs
del img
img = newimg
### 1*16 -> 2*8 img-layout
img = chip_utils.formatRevert(GSImage=img)
self.nsecy = 2
self.nsecx = 8
### prescan & overscan
if config["ins_effects"]["add_prescan"] == True:
chip_utils.log_info(msg=" Apply pre/over-scan", logger=self.logger)
if config["ins_effects"]["cte_trail"] == False:
img = chip_utils.AddPreScan(GSImage=img, pre1=pre1, pre2=pre2, over1=over1, over2=over2)
if config["ins_effects"]["cte_trail"] == True:
img = chip_utils.AddPreScan(GSImage=img, pre1=pre1, pre2=pre2, over1=over1, over2=0)
### 1*16 output
if config["ins_effects"]["format_output"] == True:
chip_utils.log_info(msg=" Apply 1*16 format", logger=self.logger)
img = chip_utils.formatOutput(GSImage=img)
self.nsecy = 1
self.nsecx = 16
# Add Bias level
if config["ins_effects"]["add_bias"] == True:
if self.logger is not None:
self.logger.info(" Adding Bias level and 16-channel non-uniformity")
else:
print(" Adding Bias level and 16-channel non-uniformity")
chip_utils.log_info(msg=" Adding Bias level and 16-channel non-uniformity", logger=self.logger)
if config["ins_effects"]["bias_16channel"] == True:
img = effects.AddBiasNonUniform16(img,
bias_level=float(self.bias_level),
nsecy = 2, nsecx=8,
nsecy = self.nsecy, nsecx=self.nsecx,
seed=SeedBiasNonuni+self.chipID,
logger=self.logger)
elif config["ins_effects"]["bias_16channel"] == False:
......@@ -562,14 +468,11 @@ class Chip(FocalPlane):
img.addNoise(readout_noise)
# Apply Gain & Quantization
if self.logger is not None:
self.logger.info(" Applying Gain (and 16 channel non-uniformity) & Quantization")
else:
print(" Applying Gain (and 16 channel non-uniformity) & Quantization", flush=True)
chip_utils.log_info(msg=" Applying Gain (and 16 channel non-uniformity) & Quantization", logger=self.logger)
if config["ins_effects"]["gain_16channel"] == True:
img, self.gain_channel = effects.ApplyGainNonUniform16(
img, gain=self.gain,
nsecy = 2, nsecx=8,
nsecy = self.nsecy, nsecx=self.nsecx,
seed=SeedGainNonuni+self.chipID,
logger=self.logger)
elif config["ins_effects"]["gain_16channel"] == False:
......@@ -590,12 +493,20 @@ class Chip(FocalPlane):
print(" Output N frame Bias files", flush=True)
NBias = int(config["output_setting"]["NBias"])
for i in range(NBias):
BiasCombImg, BiasTag = effects.MakeBiasNcomb(
self.npix_x, self.npix_y,
bias_level=float(self.bias_level),
ncombine=1, read_noise=self.read_noise, gain=1,
seed=SeedBiasNonuni+self.chipID,
logger=self.logger)
### BiasCombImg, BiasTag = effects.MakeBiasNcomb(
### self.npix_x, self.npix_y,
### bias_level=float(self.bias_level),
### ncombine=1, read_noise=self.read_noise, gain=1,
### seed=SeedBiasNonuni+self.chipID,
### logger=self.logger)
BiasCombImg = galsim.Image(self.npix_x, self.npix_y, init_value=0) ###
if config["ins_effects"]["add_bias"] == True:
biaslevel = self.bias_level
overscan = biaslevel-2
elif config["ins_effects"]["add_bias"] == False:
biaslevel = 0
overscan = 0
# Readout noise for Biases is not generated with random seeds. So readout noise for bias images can't be reproduced.
if config["ins_effects"]["cosmic_ray"] == True:
if config["ins_effects"]["cray_differ"] == True:
......@@ -610,6 +521,11 @@ class Chip(FocalPlane):
BiasCombImg += cr_map
del cr_map
# Apply Bad lines
if config["ins_effects"]["add_badcolumns"] == True:
BiasCombImg = effects.BadColumns(BiasCombImg-float(self.bias_level)+5, seed=SeedBadColumns, chipid=self.chipID, logger=self.logger) + float(self.bias_level)-5
# Non-Linearity for Bias
if config["ins_effects"]["non_linear"] == True:
if self.logger is not None:
......@@ -618,12 +534,43 @@ class Chip(FocalPlane):
print(" Applying Non-Linearity on the Bias image", flush=True)
BiasCombImg = effects.NonLinearity(GSImage=BiasCombImg, beta1=5.e-7, beta2=0)
# Apply Bad lines
if config["ins_effects"]["add_badcolumns"] == True:
BiasCombImg = effects.BadColumns(BiasCombImg-float(self.bias_level)+5, seed=SeedBadColumns, chipid=self.chipID, logger=self.logger) + float(self.bias_level)-5
###########################START
pre1 = self.prescan_x #27
over1= self.overscan_x #71
pre2 = self.prescan_y #0 #4
over2= self.overscan_y #84 #80
### prescan & overscan
if config["ins_effects"]["add_prescan"] == True:
chip_utils.log_info(msg=" Apply pre/over-scan", logger=self.logger)
BiasCombImg = chip_utils.AddPreScan(GSImage=BiasCombImg, pre1=pre1, pre2=pre2, over1=over1, over2=over2)
### 1*16 output
if config["ins_effects"]["format_output"] == True:
chip_utils.log_info(msg=" Apply 1*16 format", logger=self.logger)
BiasCombImg = chip_utils.formatOutput(GSImage=BiasCombImg)
self.nsecy = 1
self.nsecx = 16
###########################END
### Add Bias level
if config["ins_effects"]["add_bias"] == True:
if self.logger is not None:
self.logger.info(" Adding Bias level and 16-channel non-uniformity")
else:
print(" Adding Bias level and 16-channel non-uniformity")
BiasCombImg = effects.AddBiasNonUniform16(BiasCombImg,
bias_level=biaslevel,
nsecy = self.nsecy, nsecx=self.nsecx,
seed=SeedBiasNonuni+self.chipID,
logger=self.logger)
rng = galsim.UniformDeviate()
ncombine = 1
NoiseBias = galsim.GaussianNoise(rng=rng, sigma=self.read_noise*ncombine**0.5)
BiasCombImg.addNoise(NoiseBias)
BiasCombImg, self.gain_channel = effects.ApplyGainNonUniform16(BiasCombImg, gain=self.gain,
nsecy = 2, nsecx=8,
nsecy = self.nsecy, nsecx=self.nsecx,
seed=SeedGainNonuni+self.chipID,
logger=self.logger)
# BiasCombImg = effects.AddOverscan(
......@@ -633,12 +580,9 @@ class Chip(FocalPlane):
BiasCombImg.replaceNegative(replace_value=0)
BiasCombImg.quantize()
BiasCombImg = galsim.ImageUS(BiasCombImg)
# BiasCombImg.write("%s/BiasImg_%s_%s_%s.fits" % (chip_output.subdir, BiasTag, self.chipID, i+1))
# datetime_obs = datetime.utcfromtimestamp(timestamp_obs)
# date_obs = datetime_obs.strftime("%y%m%d")
# time_obs = datetime_obs.strftime("%H%M%S")
timestamp_obs += 10 * 60
self.outputCal(
chip_utils.outputCal(
chip=self,
img=BiasCombImg,
ra_cen=ra_cen,
dec_cen=dec_cen,
......@@ -647,6 +591,8 @@ class Chip(FocalPlane):
pointing_ID=pointing_ID,
output_dir=chip_output.subdir,
exptime=0.0,
project_cycle=config["project_cycle"],
run_counter=config["run_counter"],
timestamp=timestamp_obs)
del BiasCombImg
......@@ -689,6 +635,16 @@ class Chip(FocalPlane):
FlatCombImg += cr_map
del cr_map
# Add Hot Pixels or/and Dead Pixels
rgbadpix = Generator(PCG64(int(SeedDefective+self.chipID)))
badfraction = 5E-5*(rgbadpix.random()*0.5+0.7)
FlatCombImg = effects.DefectivePixels(FlatCombImg, IfHotPix=BoolHotPix, IfDeadPix=BoolDeadPix, fraction=badfraction, seed=SeedDefective+self.chipID, biaslevel=0)
# Apply Bad lines
if config["ins_effects"]["add_badcolumns"] == True:
FlatCombImg = effects.BadColumns(FlatCombImg, seed=SeedBadColumns, chipid=self.chipID, logger=self.logger)
if config["ins_effects"]["non_linear"] == True:
if self.logger is not None:
self.logger.info(" Applying Non-Linearity on the Flat image")
......@@ -696,17 +652,61 @@ class Chip(FocalPlane):
print(" Applying Non-Linearity on the Flat image", flush=True)
FlatCombImg = effects.NonLinearity(GSImage=FlatCombImg, beta1=5.e-7, beta2=0)
###if config["ins_effects"]["cte_trail"] == True:
### FlatCombImg = effects.CTE_Effect(GSImage=FlatCombImg, threshold=3)
###########################START
pre1 = self.prescan_x #27
over1= self.overscan_x #71
pre2 = self.prescan_y #0 #4
over2= self.overscan_y #84 #80
if config["ins_effects"]["cte_trail"] == True:
FlatCombImg = effects.CTE_Effect(GSImage=FlatCombImg, threshold=3)
chip_utils.log_info(msg=" Apply CTE Effect", logger=self.logger)
###img = effects.CTE_Effect(GSImage=img, threshold=27)
###CTI_modeling
### 2*8 -> 1*16 img-layout
FlatCombImg = chip_utils.formatOutput(GSImage=FlatCombImg)
self.nsecy = 1
self.nsecx = 16
img_arr = FlatCombImg.array
ny, nx = img_arr.shape
dx = int(nx/self.nsecx)
dy = int(ny/self.nsecy)
newimg = galsim.Image(nx, int(ny+over2), init_value=0)
for ichannel in range(16):
print('\n***add CTI effects: pointing-{:} chip-{:} channel-{:}***'.format(pointing_ID, self.chipID, ichannel+1))
#nx,ny,noverscan,nsp,nmax = 4608,4616,84,3,10
noverscan,nsp,nmax = over2,3,10
beta,w,c = 0.478,84700,0
t = np.array([0.74,7.7,37],dtype=np.float32)
rho_trap = np.array([0.6,1.6,1.4],dtype=np.float32)
trap_seeds = np.array([0,1000,10000],dtype=np.int32) + ichannel + self.chipID*16
release_seed = 50 + ichannel + pointing_ID*30 + self.chipID*16
newimg.array[:, 0+ichannel*dx:dx+ichannel*dx] = CTI_sim(img_arr[:, 0+ichannel*dx:dx+ichannel*dx],dx,dy,noverscan,nsp,nmax,beta,w,c,t,rho_trap,trap_seeds,release_seed)
newimg.wcs = FlatCombImg.wcs
del FlatCombImg
FlatCombImg = newimg
### 1*16 -> 2*8 img-layout
FlatCombImg = chip_utils.formatRevert(GSImage=FlatCombImg)
self.nsecy = 2
self.nsecx = 8
# Add Hot Pixels or/and Dead Pixels
rgbadpix = Generator(PCG64(int(SeedDefective+self.chipID)))
badfraction = 5E-5*(rgbadpix.random()*0.5+0.7)
FlatCombImg = effects.DefectivePixels(FlatCombImg, IfHotPix=BoolHotPix, IfDeadPix=BoolDeadPix, fraction=badfraction, seed=SeedDefective+self.chipID, biaslevel=0)
### prescan & overscan
if config["ins_effects"]["add_prescan"] == True:
chip_utils.log_info(msg=" Apply pre/over-scan", logger=self.logger)
if config["ins_effects"]["cte_trail"] == False:
FlatCombImg = chip_utils.AddPreScan(GSImage=FlatCombImg, pre1=pre1, pre2=pre2, over1=over1, over2=over2)
if config["ins_effects"]["cte_trail"] == True:
FlatCombImg = chip_utils.AddPreScan(GSImage=FlatCombImg, pre1=pre1, pre2=pre2, over1=over1, over2=0)
# Apply Bad lines
if config["ins_effects"]["add_badcolumns"] == True:
FlatCombImg = effects.BadColumns(FlatCombImg, seed=SeedBadColumns, chipid=self.chipID, logger=self.logger)
### 1*16 output
if config["ins_effects"]["format_output"] == True:
chip_utils.log_info(msg=" Apply 1*16 format", logger=self.logger)
FlatCombImg = chip_utils.formatOutput(GSImage=FlatCombImg)
self.nsecy = 1
self.nsecx = 16
###########################END
# Add Bias level
if config["ins_effects"]["add_bias"] == True:
......@@ -717,7 +717,7 @@ class Chip(FocalPlane):
# img += float(config["ins_effects"]["bias_level"])
FlatCombImg = effects.AddBiasNonUniform16(FlatCombImg,
bias_level=biaslevel,
nsecy = 2, nsecx=8,
nsecy = self.nsecy, nsecx=self.nsecx,
seed=SeedBiasNonuni+self.chipID,
logger=self.logger)
......@@ -729,19 +729,16 @@ class Chip(FocalPlane):
FlatCombImg.addNoise(readout_noise)
FlatCombImg, self.gain_channel = effects.ApplyGainNonUniform16(FlatCombImg, gain=self.gain,
nsecy = 2, nsecx=8,
nsecy = self.nsecy, nsecx=self.nsecx,
seed=SeedGainNonuni+self.chipID,
logger=self.logger)
# FlatCombImg = effects.AddOverscan(FlatCombImg, overscan=overscan, gain=self.gain, widthl=27, widthr=27, widtht=8, widthb=8)
FlatCombImg.replaceNegative(replace_value=0)
FlatCombImg.quantize()
FlatCombImg = galsim.ImageUS(FlatCombImg)
# FlatCombImg.write("%s/FlatImg_%s_%s_%s.fits" % (chip_output.subdir, FlatTag, self.chipID, i+1))
# datetime_obs = datetime.utcfromtimestamp(timestamp_obs)
# date_obs = datetime_obs.strftime("%y%m%d")
# time_obs = datetime_obs.strftime("%H%M%S")
timestamp_obs += 10 * 60
self.outputCal(
chip_utils.outputCal(
chip=self,
img=FlatCombImg,
ra_cen=ra_cen,
dec_cen=dec_cen,
......@@ -750,6 +747,8 @@ class Chip(FocalPlane):
pointing_ID=pointing_ID,
output_dir=chip_output.subdir,
exptime=self.flat_exptime,
project_cycle=config["project_cycle"],
run_counter=config["run_counter"],
timestamp=timestamp_obs)
del FlatCombImg, FlatSingle, prnu_img
......@@ -793,10 +792,21 @@ class Chip(FocalPlane):
cr_map[cr_map < 0] = 0
crmap_gsimg = galsim.Image(cr_map, dtype=np.uint16)
del cr_map
# datetime_obs = datetime.utcfromtimestamp(timestamp_obs)
# date_obs = datetime_obs.strftime("%y%m%d")
# time_obs = datetime_obs.strftime("%H%M%S")
self.outputCal(
###########################START
### prescan & overscan
if config["ins_effects"]["add_prescan"] == True:
chip_utils.log_info(msg=" Apply pre/over-scan", logger=self.logger)
crmap_gsimg = chip_utils.AddPreScan(GSImage=crmap_gsimg, pre1=pre1, pre2=pre2, over1=over1, over2=over2)
### 1*16 output
if config["ins_effects"]["format_output"] == True:
chip_utils.log_info(msg=" Apply 1*16 format", logger=self.logger)
crmap_gsimg = chip_utils.formatOutput(GSImage=crmap_gsimg)
self.nsecy = 1
self.nsecx = 16
###########################END
chip_utils.outputCal(
chip=self,
img=crmap_gsimg,
ra_cen=ra_cen,
dec_cen=dec_cen,
......@@ -805,9 +815,21 @@ class Chip(FocalPlane):
pointing_ID=pointing_ID,
output_dir=chip_output.subdir,
exptime=self.dark_exptime,
project_cycle=config["project_cycle"],
run_counter=config["run_counter"],
timestamp=timestamp_obs)
del crmap_gsimg
# Add Hot Pixels or/and Dead Pixels
rgbadpix = Generator(PCG64(int(SeedDefective+self.chipID)))
badfraction = 5E-5*(rgbadpix.random()*0.5+0.7)
DarkCombImg = effects.DefectivePixels(DarkCombImg, IfHotPix=BoolHotPix, IfDeadPix=BoolDeadPix, fraction=badfraction, seed=SeedDefective+self.chipID, biaslevel=0)
# Apply Bad lines
if config["ins_effects"]["add_badcolumns"] == True:
DarkCombImg = effects.BadColumns(DarkCombImg, seed=SeedBadColumns, chipid=self.chipID, logger=self.logger)
# Non-Linearity for Dark
if config["ins_effects"]["non_linear"] == True:
if self.logger is not None:
......@@ -816,17 +838,61 @@ class Chip(FocalPlane):
print(" Applying Non-Linearity on the Dark image", flush=True)
DarkCombImg = effects.NonLinearity(GSImage=DarkCombImg, beta1=5.e-7, beta2=0)
###if config["ins_effects"]["cte_trail"] == True:
### DarkCombImg = effects.CTE_Effect(GSImage=DarkCombImg, threshold=3)
###########################START
pre1 = self.prescan_x #27
over1= self.overscan_x #71
pre2 = self.prescan_y #0 #4
over2= self.overscan_y #84 #80
if config["ins_effects"]["cte_trail"] == True:
DarkCombImg = effects.CTE_Effect(GSImage=DarkCombImg, threshold=3)
chip_utils.log_info(msg=" Apply CTE Effect", logger=self.logger)
###img = effects.CTE_Effect(GSImage=img, threshold=27)
###CTI_modeling
### 2*8 -> 1*16 img-layout
DarkCombImg = chip_utils.formatOutput(GSImage=DarkCombImg)
self.nsecy = 1
self.nsecx = 16
img_arr = DarkCombImg.array
ny, nx = img_arr.shape
dx = int(nx/self.nsecx)
dy = int(ny/self.nsecy)
newimg = galsim.Image(nx, int(ny+over2), init_value=0)
for ichannel in range(16):
print('\n***add CTI effects: pointing-{:} chip-{:} channel-{:}***'.format(pointing_ID, self.chipID, ichannel+1))
#nx,ny,noverscan,nsp,nmax = 4608,4616,84,3,10
noverscan,nsp,nmax = over2,3,10
beta,w,c = 0.478,84700,0
t = np.array([0.74,7.7,37],dtype=np.float32)
rho_trap = np.array([0.6,1.6,1.4],dtype=np.float32)
trap_seeds = np.array([0,1000,10000],dtype=np.int32) + ichannel + self.chipID*16
release_seed = 50 + ichannel + pointing_ID*30 + self.chipID*16
newimg.array[:, 0+ichannel*dx:dx+ichannel*dx] = CTI_sim(img_arr[:, 0+ichannel*dx:dx+ichannel*dx],dx,dy,noverscan,nsp,nmax,beta,w,c,t,rho_trap,trap_seeds,release_seed)
newimg.wcs = DarkCombImg.wcs
del DarkCombImg
DarkCombImg = newimg
# Add Hot Pixels or/and Dead Pixels
rgbadpix = Generator(PCG64(int(SeedDefective+self.chipID)))
badfraction = 5E-5*(rgbadpix.random()*0.5+0.7)
DarkCombImg = effects.DefectivePixels(DarkCombImg, IfHotPix=BoolHotPix, IfDeadPix=BoolDeadPix, fraction=badfraction, seed=SeedDefective+self.chipID, biaslevel=0)
### 1*16 -> 2*8 img-layout
DarkCombImg = chip_utils.formatRevert(GSImage=DarkCombImg)
self.nsecy = 2
self.nsecx = 8
# Apply Bad lines
if config["ins_effects"]["add_badcolumns"] == True:
DarkCombImg = effects.BadColumns(DarkCombImg, seed=SeedBadColumns, chipid=self.chipID, logger=self.logger)
### prescan & overscan
if config["ins_effects"]["add_prescan"] == True:
chip_utils.log_info(msg=" Apply pre/over-scan", logger=self.logger)
if config["ins_effects"]["cte_trail"] == False:
DarkCombImg = chip_utils.AddPreScan(GSImage=DarkCombImg, pre1=pre1, pre2=pre2, over1=over1, over2=over2)
if config["ins_effects"]["cte_trail"] == True:
DarkCombImg = chip_utils.AddPreScan(GSImage=DarkCombImg, pre1=pre1, pre2=pre2, over1=over1, over2=0)
### 1*16 output
if config["ins_effects"]["format_output"] == True:
chip_utils.log_info(msg=" Apply 1*16 format", logger=self.logger)
DarkCombImg = chip_utils.formatOutput(GSImage=DarkCombImg)
self.nsecy = 1
self.nsecx = 16
###########################END
# Add Bias level
if config["ins_effects"]["add_bias"] == True:
......@@ -837,7 +903,7 @@ class Chip(FocalPlane):
# img += float(config["ins_effects"]["bias_level"])
DarkCombImg = effects.AddBiasNonUniform16(DarkCombImg,
bias_level=biaslevel,
nsecy = 2, nsecx=8,
nsecy = self.nsecy, nsecx=self.nsecx,
seed=SeedBiasNonuni+self.chipID,
logger=self.logger)
......@@ -850,7 +916,7 @@ class Chip(FocalPlane):
DarkCombImg, self.gain_channel = effects.ApplyGainNonUniform16(
DarkCombImg, gain=self.gain,
nsecy = 2, nsecx=8,
nsecy = self.nsecy, nsecx=self.nsecx,
seed=SeedGainNonuni+self.chipID,
logger=self.logger)
# DarkCombImg = effects.AddOverscan(
......@@ -860,12 +926,9 @@ class Chip(FocalPlane):
DarkCombImg.replaceNegative(replace_value=0)
DarkCombImg.quantize()
DarkCombImg = galsim.ImageUS(DarkCombImg)
# DarkCombImg.write("%s/DarkImg_%s_%s_%s.fits" % (chip_output.subdir, DarkTag, self.chipID, i+1))
# datetime_obs = datetime.utcfromtimestamp(timestamp_obs)
# date_obs = datetime_obs.strftime("%y%m%d")
# time_obs = datetime_obs.strftime("%H%M%S")
timestamp_obs += 10 * 60
self.outputCal(
chip_utils.outputCal(
chip=self,
img=DarkCombImg,
ra_cen=ra_cen,
dec_cen=dec_cen,
......@@ -874,6 +937,8 @@ class Chip(FocalPlane):
pointing_ID=pointing_ID,
output_dir=chip_output.subdir,
exptime=self.dark_exptime,
project_cycle=config["project_cycle"],
run_counter=config["run_counter"],
timestamp = timestamp_obs)
del DarkCombImg
# img = galsim.ImageUS(img)
......@@ -894,19 +959,3 @@ class Chip(FocalPlane):
# del sub_img
return img
def loadSLSFLATCUBE(self, flat_fn='flat_cube.fits'):
from astropy.io import fits
try:
with pkg_resources.files('ObservationSim.Instrument.data').joinpath(flat_fn) as data_path:
flat_fits = fits.open(data_path, ignore_missing_simple=True)
except AttributeError:
with pkg_resources.path('ObservationSim.Instrument.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
import os
import galsim
import ctypes
import numpy as np
from astropy.io import fits
from datetime import datetime
from ObservationSim.Instrument.Chip import Effects as effects
from ObservationSim.Config.Header import generatePrimaryHeader, generateExtensionHeader
try:
import importlib.resources as pkg_resources
except ImportError:
# Try backported to PY<37 'importlib_resources'
import importlib_resources as pkg_resources
def log_info(msg, logger=None):
if logger:
logger.info(msg)
else:
print(msg, flush=True)
def getChipSLSGratingID(chipID):
gratingID = ['','']
if chipID == 1: gratingID = ['GI2', 'GI1']
if chipID == 2: gratingID = ['GV4', 'GV3']
if chipID == 3: gratingID = ['GU2', 'GU1']
if chipID == 4: gratingID = ['GU4', 'GU3']
if chipID == 5: gratingID = ['GV2', 'GV1']
if chipID == 10: 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
def getChipSLSConf(chipID):
confFile = ''
if chipID == 1: confFile = ['CSST_GI2.conf', 'CSST_GI1.conf']
if chipID == 2: confFile = ['CSST_GV4.conf', 'CSST_GV3.conf']
if chipID == 3: confFile = ['CSST_GU2.conf', 'CSST_GU1.conf']
if chipID == 4: confFile = ['CSST_GU4.conf', 'CSST_GU3.conf']
if chipID == 5: confFile = ['CSST_GV2.conf', 'CSST_GV1.conf']
if chipID == 10: 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
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):
img_type = pointing.pointing_type
img_type_code = pointing.pointing_type_code
h_prim = generatePrimaryHeader(
xlen=chip.npix_x,
ylen=chip.npix_y,
pointing_id = pointing.obs_id,
pointing_type_code = img_type_code,
ra=pointing.ra,
dec=pointing.dec,
pixel_scale=chip.pix_scale,
time_pt = pointing.timestamp,
exptime=pointing.exp_time,
im_type=img_type,
sat_pos=[pointing.sat_x, pointing.sat_y, pointing.sat_z],
sat_vel=[pointing.sat_vx, pointing.sat_vy, pointing.sat_vz],
project_cycle=project_cycle,
run_counter=run_counter,
chip_name=str(chip.chipID).rjust(2, '0'))
h_ext = generateExtensionHeader(
chip=chip,
xlen=chip.npix_x,
ylen=chip.npix_y,
ra=pointing.ra,
dec=pointing.dec,
pa=pointing.img_pa.deg,
gain=chip.gain,
readout=chip.read_noise,
dark=chip.dark_noise,
saturation=90000,
pixel_scale=chip.pix_scale,
pixel_size=chip.pix_size,
xcen=chip.x_cen,
ycen=chip.y_cen,
extName=img_type,
timestamp=pointing.timestamp,
exptime=pointing.exp_time,
readoutTime=chip.readout_time,
t_shutter_open=pointing.t_shutter_open,
t_shutter_close=pointing.t_shutter_close)
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'):
h_prim, h_ext = generateHeader(
chip=chip,
pointing=pointing,
img_type=img_type,
img_type_code=img_type_code,
project_cycle=project_cycle,
run_counter=run_counter)
hdu1 = fits.PrimaryHDU(header=h_prim)
hdu1.add_checksum()
hdu1.header.comments['CHECKSUM'] = 'HDU checksum'
hdu1.header.comments['DATASUM'] = 'data unit checksum'
hdu2 = fits.ImageHDU(img.array, header=h_ext)
hdu2.add_checksum()
hdu2.header.comments['XTENSION'] = 'extension type'
hdu2.header.comments['CHECKSUM'] = 'HDU checksum'
hdu2.header.comments['DATASUM'] = 'data unit checksum'
hdu1 = fits.HDUList([hdu1, hdu2])
fname = os.path.join(output_dir, h_prim['FILENAME']+'.fits')
hdu1.writeto(fname, output_verify='ignore', overwrite=True)
def add_sky_background(img, filt, exptime, sky_map=None, tel=None):
# Add sky background
if sky_map is None:
sky_map = filt.getSkyNoise(exptime=exptime)
sky_map = sky_map * np.ones_like(img.array)
sky_map = galsim.Image(array=sky_map)
# Apply Poisson noise to the sky map
# # (NOTE): only for photometric chips if it utilizes the photon shooting to draw stamps
# if self.survey_type == "photometric":
# sky_map.addNoise(poisson_noise)
elif img.array.shape != sky_map.shape:
raise ValueError("The shape img and sky_map must be equal.")
elif tel is not None: # If sky_map is given in flux
sky_map = sky_map * tel.pupil_area * exptime
img += sky_map
return img, sky_map
def get_flat(img, seed):
flat_img = effects.MakeFlatSmooth(
GSBounds=img.bounds,
seed=seed)
flat_normal = flat_img / np.mean(flat_img.array)
return flat_img, flat_normal
def add_cosmic_rays(img, chip, exptime=150, seed=0):
cr_map, cr_event_num = effects.produceCR_Map(
xLen=chip.npix_x, yLen=chip.npix_y,
exTime=exptime+0.5*chip.readout_time,
cr_pixelRatio=0.003*(exptime+0.5*chip.readout_time)/600.,
gain=chip.gain,
attachedSizes=chip.attachedSizes,
seed=seed) # seed: obj-imaging:+0; bias:+1; dark:+2; flat:+3;
img += cr_map
cr_map[cr_map > 65535] = 65535
cr_map[cr_map < 0] = 0
crmap_gsimg = galsim.Image(cr_map, dtype=np.uint16)
del cr_map
return img, crmap_gsimg, cr_event_num
def add_PRNU(img, chip, seed=0):
prnu_img = effects.PRNU_Img(
xsize=chip.npix_x,
ysize=chip.npix_y,
sigma=0.01,
seed=seed)
img *= prnu_img
return img, prnu_img
def get_poisson(seed=0, sky_level=0.):
rng_poisson = galsim.BaseDeviate(seed)
poisson_noise = galsim.PoissonNoise(rng_poisson, sky_level=sky_level)
return rng_poisson, poisson_noise
def get_base_img(img, chip, read_noise, readout_time, dark_noise, exptime=150., InputDark=None):
if InputDark == None:
# 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)
base_img1 = base_level * np.ones_like(img.array)
else:
base_img1 = np.zeros_like(img.array)
ny = int(chip.npix_y/2)
nx = chip.npix_x
arr = np.arange(ny).reshape(ny, 1)
arr = np.broadcast_to(arr, (ny, nx))
base_img2 = np.zeros_like(img.array)
base_img2[:ny, :] = arr
base_img2[ny:, :] = arr[::-1,:]
base_img2[:,:] = base_img2[:,:]*(readout_time/ny)*dark_noise
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):
if poisson_noise is None:
_, poisson_noise = get_poisson(seed=seed, sky_level=sky_level)
read_noise = chip.read_noise
if dark_noise is None:
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)
img += base_img
img.addNoise(poisson_noise)
# img -= read_noise**2
if InputDark != None:
hdu = fits.open(InputDark) ##"Instrument/data/dark/dark_1000s_example_0.fits"
img += hdu[0].data/hdu[0].header['exptime']*exptime
hdu.close()
return img, base_img
def add_brighter_fatter(img):
#Inital dynamic lib
try:
with pkg_resources.files('ObservationSim.Instrument.Chip.libBF').joinpath("libmoduleBF.so") as lib_path:
lib_bf = ctypes.CDLL(lib_path)
except AttributeError:
with pkg_resources.path('ObservationSim.Instrument.Chip.libBF', "libmoduleBF.so") as 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]
# Set bit flag
bit_flag = 1
bit_flag = bit_flag | (1 << 2)
nx, ny = img.array.shape
nn = nx * ny
arr_ima= (ctypes.c_float*nn)()
arr_imc= (ctypes.c_float*nn)()
arr_ima[:]= img.array.reshape(nn)
arr_imc[:]= np.zeros(nn)
lib_bf.addEffects(nx, ny, arr_ima, arr_imc, bit_flag)
img.array[:, :] = np.reshape(arr_imc, [nx, ny])
del arr_ima, arr_imc
return img
"""
def add_inputdark(img, chip, exptime):
fname = "/share/home/weichengliang/CSST_git/test_new_sim/csst-simulation/ObservationSim/Instrument/data/dark/dark_1000s_example_0.fits"
hdu = fits.open(fname)
#ny, nx = img.array.shape
#inputdark = np.zeros([ny, nx])
img.array[:, :] += hdu[0].data/hdu[0].header['exptime']*exptime
hdu.close()
del inputdark
return img
"""
def AddPreScan(GSImage, pre1=27, pre2=4, over1=71, over2=80, nsecy = 2, nsecx=8):
img= GSImage.array
ny, nx = img.shape
dx = int(nx/nsecx)
dy = int(ny/nsecy)
imgt=np.zeros([int(nsecy*nsecx), int(ny/nsecy+pre2+over2), int(nx/nsecx+pre1+over1)])
for iy in range(nsecy):
for ix in range(nsecx):
if iy % 2 == 0:
tx = ix
else:
tx = (nsecx-1)-ix
ty = iy
chunkidx = int(tx+ty*nsecx) #chunk1-[1,2,3,4], chunk2-[5,6,7,8], chunk3-[9,10,11,12], chunk4-[13,14,15,16]
imgtemp = np.zeros([int(ny/nsecy+pre2+over2), int(nx/nsecx+pre1+over1)])
if int(chunkidx/4) == 0:
imgtemp[pre2:pre2+dy, pre1:pre1+dx] = img[iy*dy:(iy+1)*dy, ix*dx:(ix+1)*dx]
imgt[chunkidx, :, :] = imgtemp
if int(chunkidx/4) == 1:
imgtemp[pre2:pre2+dy, over1:over1+dx] = img[iy*dy:(iy+1)*dy, ix*dx:(ix+1)*dx]
imgt[chunkidx, :, :] = imgtemp #[:, ::-1]
if int(chunkidx/4) == 2:
imgtemp[over2:over2+dy, over1:over1+dx] = img[iy*dy:(iy+1)*dy, ix*dx:(ix+1)*dx]
imgt[chunkidx, :, :] = imgtemp #[::-1, ::-1]
if int(chunkidx/4) == 3:
imgtemp[over2:over2+dy, pre1:pre1+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]
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.array[:, :] = np.concatenate([imgtx1, imgtx2]) #vstack chunk(1,2) & chunk(4,3)
newimg.wcs = GSImage.wcs
return newimg
def AddPreScanFO(GSImage, pre1=27, pre2=4, over1=71, over2=80, nsecy = 1, nsecx=16):
img= GSImage.array
ny, nx = img.shape
dx = int(nx/nsecx)
dy = int(ny/nsecy)
newimg = galsim.Image(int(nx+(pre1+over1)*nsecx), int(ny+(pre2+over2)*nsecy), init_value=0)
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.wcs = GSImage.wcs
return newimg
def formatOutput(GSImage, nsecy = 2, nsecx=8):
img = GSImage.array
ny, nx = img.shape
dx = int(nx/nsecx)
dy = int(ny/nsecy)
imgt = np.zeros([int(nsecx*nsecy), dy, dx])
for iy in range(nsecy):
for ix in range(nsecx):
if iy % 2 == 0:
tx = ix
else:
tx = (nsecx-1)-ix
ty = iy
chunkidx = int(tx+ty*nsecx)
if int(chunkidx/4) == 0:
imgt[chunkidx, :, :] = img[iy*dy:(iy+1)*dy, ix*dx:(ix+1)*dx]
if int(chunkidx/4) == 1:
imgt[chunkidx, :, :] = img[iy*dy:(iy+1)*dy, ix*dx:(ix+1)*dx]
if int(chunkidx/4) == 2:
imgt[chunkidx, :, :] = img[iy*dy:(iy+1)*dy, ix*dx:(ix+1)*dx]
if int(chunkidx/4) == 3:
imgt[chunkidx, :, :] = img[iy*dy:(iy+1)*dy, ix*dx:(ix+1)*dx]
imgttx0 = np.hstack(imgt[ 0:4:, :, :])
imgttx1 = np.hstack(imgt[ 4:8:, :, ::-1])
imgttx2 = np.hstack(imgt[8:12:, ::-1, ::-1])
imgttx3 = np.hstack(imgt[12:16:,::-1, :])
newimg = galsim.Image(int(dx*nsecx*nsecy), dy, init_value=0)
newimg.array[:, :] = np.hstack([imgttx0, imgttx1, imgttx2, imgttx3])
return newimg
def formatRevert(GSImage, nsecy = 1, nsecx=16):
img = GSImage.array
ny, nx = img.shape
dx = int(nx/nsecx)
dy = int(ny/nsecy)
newimg = galsim.Image(int(dx*8), int(dy*2), init_value=0)
for ix in range(0,4):
tx = ix
newimg.array[0:dy, 0+tx*dx:dx+tx*dx] = img[:, 0+ix*dx:dx+ix*dx]
for ix in range(4,8):
tx = ix
newimg.array[0:dy, 0+tx*dx:dx+tx*dx] = img[:, 0+ix*dx:dx+ix*dx][:, ::-1]
for ix in range(8,12):
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]
for ix in range(12,16):
tx = 7-(ix-8)
newimg.array[0+dy:dy+dy, 0+tx*dx:dx+tx*dx] = img[:, 0+ix*dx:dx+ix*dx][::-1, :]
return newimg
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "nrutil.h"
#define ISSETBITFLAG(x,b) ((x) & (1 << (b)))
#define ADD_DIFFUSION 1
#define ADD_BF_FILTER 2
float linearInterp(float xp, float x0, float y0, float x1, float y1);
void addEffects(int ngx_ima, int ngy_ima, float *arr_ima, float *arr_imc, int bit_flag)
{
int nx, ny, i,j,k,ks;
int it,jt,itt,jtt;
int diffuidx[26][2],diffuN,ilow,ih,im,dim[3];
float diffua[5][5],cdiffu[26],**bfa;
double mvar,mcov,tmp,ma,mb,mc;
char fname[100];
nx = ngx_ima; //input-image size
ny = ngy_ima;
//0. init. original image with an input array (arr_ima)
//1. Adding diffusion effect.
if(ISSETBITFLAG(bit_flag, ADD_DIFFUSION))
{
printf("adding diffusion.....\n");
printf("ERR: no diffusion filter ...");
exit(0);
}
//2. Adding BF effect
if(ISSETBITFLAG(bit_flag, ADD_BF_FILTER))
{
printf("Adding BF effect...\n");
//setup BF correlation fliter
float neX;
float neP1 = 50000;
float bfaP1[9]={0.9707182, 0.002143905, 0.004131103, 0.001149542, 0.0005501739, 0.0005469659, 0.0003726081, 0.0003795207, 0.0001633302};
float neP2 = 10000;
float bfaP2[9]={0.9945288, 0.0003041936, 0.0007539311, 0.0002424675, 0.0001226098, 0.00009308617, 0.00008027447, 0.00006309676, 0.00006400052};
bfa=matrix(-2,2,-2,2);
// smooth with the BF filter
for(i=0;i<nx;i++)for(j=0;j<ny;j++) arr_imc[j+i*ny]=0;
for(i=0;i<nx;i++)
{
for(j=0;j<ny;j++)
{
//rescale BF filter with the local pix value
neX = arr_ima[j+i*ny];
if(neX >= 10000)
{
bfa[0][0]=0; //linearInterp(neX, neP1, bfaP1[0], neP2, bfaP2[0]); //0;
bfa[0][1]=bfa[0][-1]=linearInterp(neX, neP1, bfaP1[1], neP2, bfaP2[1]); //0.01575;
bfa[-1][0]=bfa[1][0]=linearInterp(neX, neP1, bfaP1[2], neP2, bfaP2[2]); //0.00652;
bfa[-1][-1]=bfa[1][1]=bfa[-1][1]=bfa[1][-1]=linearInterp(neX, neP1, bfaP1[3], neP2, bfaP2[3]); //0.00335;
bfa[0][-2]=bfa[0][2]=linearInterp(neX, neP1, bfaP1[4], neP2, bfaP2[4]);
bfa[-2][0]=bfa[2][0]=linearInterp(neX, neP1, bfaP1[5], neP2, bfaP2[5]); //0.00118;
bfa[-2][-1]=bfa[-2][1]=bfa[2][1]=bfa[2][-1]=linearInterp(neX, neP1, bfaP1[6], neP2, bfaP2[6]);
bfa[-1][-2]=bfa[1][2]=bfa[-1][2]=bfa[1][-2]=linearInterp(neX, neP1, bfaP1[7], neP2, bfaP2[7]); //0.00083;
bfa[-2][-2]=bfa[-2][2]=bfa[2][-2]=bfa[2][2]=linearInterp(neX, neP1, bfaP1[8], neP2, bfaP2[8]); //0.00043;
}
else
{
neX=10000;
bfa[0][0]=0;
bfa[0][1]=bfa[0][-1]=bfaP2[1];
bfa[-1][0]=bfa[1][0]=bfaP2[2];
bfa[-1][-1]=bfa[1][1]=bfa[-1][1]=bfa[1][-1]=bfaP2[3];
bfa[0][-2]=bfa[0][2]=bfaP2[4];
bfa[-2][0]=bfa[2][0]=bfaP2[5];
bfa[-2][-1]=bfa[-2][1]=bfa[2][1]=bfa[2][-1]=bfaP2[6];
bfa[-1][-2]=bfa[1][2]=bfa[-1][2]=bfa[1][-2]=bfaP2[7];
bfa[-2][-2]=bfa[-2][2]=bfa[2][-2]=bfa[2][2]=bfaP2[8];
}
tmp = 0;
for(it=-2;it<=2;it++)
for(jt=-2;jt<=2;jt++)
{
bfa[it][jt] = bfa[it][jt]/neX*arr_ima[j+i*ny];
tmp += bfa[it][jt];
}
bfa[0][0]=1.-tmp;
// assign electrons according to the BF filter bfat
for(it=-2;it<=2;it++)
{
for(jt=-2;jt<=2;jt++)
{
itt=i+it;
jtt=j+jt;
if(itt>=0 && jtt>=0 && itt<nx && jtt<ny)
//c0[itt][jtt]+=bfa[it][jt]*b[i][j];
arr_imc[jtt+itt*ny] += bfa[it][jt]*arr_ima[j+i*ny];
}
}
}
}
free_matrix(bfa,-2,2,-2,2);
}
else
{
for(i=0;i<nx;i++) for(j=0;j<ny;j++) arr_imc[j+i*ny]=arr_ima[j+i*ny]; ////for ADD_BF False
}
}
float linearInterp(float xp, float x0, float y0, float x1, float y1)
{
float yp;
yp = y0 + ((y1-y0)/(x1-x0)) * (xp - x0);
return yp;
}
#if defined(__STDC__) || defined(ANSI) || defined(NRANSI) /* ANSI */
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#define NR_END 1
#define FREE_ARG char*
void nrerror(char error_text[])
/* Numerical Recipes standard error handler */
{
fprintf(stderr,"Numerical Recipes run-time error...\n");
fprintf(stderr,"%s\n",error_text);
fprintf(stderr,"...now exiting to system...\n");
exit(1);
}
float *vector(long nl, long nh)
/* allocate a float vector with subscript range v[nl..nh] */
{
float *v;
v=(float *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(float)));
if (!v) nrerror("allocation failure in vector()");
return v-nl+NR_END;
}
int *ivector(long nl, long nh)
/* allocate an int vector with subscript range v[nl..nh] */
{
int *v;
v=(int *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(int)));
if (!v) nrerror("allocation failure in ivector()");
return v-nl+NR_END;
}
unsigned char *cvector(long nl, long nh)
/* allocate an unsigned char vector with subscript range v[nl..nh] */
{
unsigned char *v;
v=(unsigned char *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(unsigned char)));
if (!v) nrerror("allocation failure in cvector()");
return v-nl+NR_END;
}
long *lvector(long nl, long nh)
/* allocate an long vector with subscript range v[nl..nh] */
{
long *v;
v=(long *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(long)));
if (!v) nrerror("allocation failure in lvector()");
return v-nl+NR_END;
}
double *dvector(long nl, long nh)
/* allocate a double vector with subscript range v[nl..nh] */
{
double *v;
v=(double *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(double)));
if (!v) nrerror("allocation failure in dvector()");
return v-nl+NR_END;
}
float **matrix(long nrl, long nrh, long ncl, long nch)
/* allocate a float matrix with subscript range m[nrl..nrh][ncl..nch] */
{
long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
float **m;
/* allocate pointers to rows */
m=(float **) malloc((size_t)((nrow+NR_END)*sizeof(float*)));
if (!m) nrerror("allocation failure 1 in matrix()");
m += NR_END;
m -= nrl;
/* allocate rows and set pointers to them */
m[nrl]=(float *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(float)));
if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
m[nrl] += NR_END;
m[nrl] -= ncl;
for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
/* return pointer to array of pointers to rows */
return m;
}
double **dmatrix(long nrl, long nrh, long ncl, long nch)
/* allocate a double matrix with subscript range m[nrl..nrh][ncl..nch] */
{
long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
double **m;
/* allocate pointers to rows */
m=(double **) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
if (!m) nrerror("allocation failure 1 in matrix()");
m += NR_END;
m -= nrl;
/* allocate rows and set pointers to them */
m[nrl]=(double *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
m[nrl] += NR_END;
m[nrl] -= ncl;
for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
/* return pointer to array of pointers to rows */
return m;
}
int **imatrix(long nrl, long nrh, long ncl, long nch)
/* allocate a int matrix with subscript range m[nrl..nrh][ncl..nch] */
{
long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
int **m;
/* allocate pointers to rows */
m=(int **) malloc((size_t)((nrow+NR_END)*sizeof(int*)));
if (!m) nrerror("allocation failure 1 in matrix()");
m += NR_END;
m -= nrl;
/* allocate rows and set pointers to them */
m[nrl]=(int *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(int)));
if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
m[nrl] += NR_END;
m[nrl] -= ncl;
for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
/* return pointer to array of pointers to rows */
return m;
}
float **submatrix(float **a, long oldrl, long oldrh, long oldcl, long oldch,
long newrl, long newcl)
/* point a submatrix [newrl..][newcl..] to a[oldrl..oldrh][oldcl..oldch] */
{
long i,j,nrow=oldrh-oldrl+1,ncol=oldcl-newcl;
float **m;
/* allocate array of pointers to rows */
m=(float **) malloc((size_t) ((nrow+NR_END)*sizeof(float*)));
if (!m) nrerror("allocation failure in submatrix()");
m += NR_END;
m -= newrl;
/* set pointers to rows */
for(i=oldrl,j=newrl;i<=oldrh;i++,j++) m[j]=a[i]+ncol;
/* return pointer to array of pointers to rows */
return m;
}
float **convert_matrix(float *a, long nrl, long nrh, long ncl, long nch)
/* allocate a float matrix m[nrl..nrh][ncl..nch] that points to the matrix
declared in the standard C manner as a[nrow][ncol], where nrow=nrh-nrl+1
and ncol=nch-ncl+1. The routine should be called with the address
&a[0][0] as the first argument. */
{
long i,j,nrow=nrh-nrl+1,ncol=nch-ncl+1;
float **m;
/* allocate pointers to rows */
m=(float **) malloc((size_t) ((nrow+NR_END)*sizeof(float*)));
if (!m) nrerror("allocation failure in convert_matrix()");
m += NR_END;
m -= nrl;
/* set pointers to rows */
m[nrl]=a-ncl;
for(i=1,j=nrl+1;i<nrow;i++,j++) m[j]=m[j-1]+ncol;
/* return pointer to array of pointers to rows */
return m;
}
float ***f3tensor(long nrl, long nrh, long ncl, long nch, long ndl, long ndh)
/* allocate a float 3tensor with range t[nrl..nrh][ncl..nch][ndl..ndh] */
{
long i,j,nrow=nrh-nrl+1,ncol=nch-ncl+1,ndep=ndh-ndl+1;
float ***t;
/* allocate pointers to pointers to rows */
t=(float ***) malloc((size_t)((nrow+NR_END)*sizeof(float**)));
if (!t) nrerror("allocation failure 1 in f3tensor()");
t += NR_END;
t -= nrl;
/* allocate pointers to rows and set pointers to them */
t[nrl]=(float **) malloc((size_t)((nrow*ncol+NR_END)*sizeof(float*)));
if (!t[nrl]) nrerror("allocation failure 2 in f3tensor()");
t[nrl] += NR_END;
t[nrl] -= ncl;
/* allocate rows and set pointers to them */
t[nrl][ncl]=(float *) malloc((size_t)((nrow*ncol*ndep+NR_END)*sizeof(float)));
if (!t[nrl][ncl]) nrerror("allocation failure 3 in f3tensor()");
t[nrl][ncl] += NR_END;
t[nrl][ncl] -= ndl;
for(j=ncl+1;j<=nch;j++) t[nrl][j]=t[nrl][j-1]+ndep;
for(i=nrl+1;i<=nrh;i++) {
t[i]=t[i-1]+ncol;
t[i][ncl]=t[i-1][ncl]+ncol*ndep;
for(j=ncl+1;j<=nch;j++) t[i][j]=t[i][j-1]+ndep;
}
/* return pointer to array of pointers to rows */
return t;
}
double ***d3tensor(long nrl, long nrh, long ncl, long nch, long ndl, long ndh)
/* allocate a double 3tensor with range t[nrl..nrh][ncl..nch][ndl..ndh] */
{
long i,j,nrow=nrh-nrl+1,ncol=nch-ncl+1,ndep=ndh-ndl+1;
double ***t;
/* allocate pointers to pointers to rows */
t=(double ***) malloc((size_t)((nrow+NR_END)*sizeof(double**)));
if (!t) nrerror("allocation failure 1 in f3tensor()");
t += NR_END;
t -= nrl;
/* allocate pointers to rows and set pointers to them */
t[nrl]=(double **) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double*)));
if (!t[nrl]) nrerror("allocation failure 2 in f3tensor()");
t[nrl] += NR_END;
t[nrl] -= ncl;
/* allocate rows and set pointers to them */
t[nrl][ncl]=(double *) malloc((size_t)((nrow*ncol*ndep+NR_END)*sizeof(double)));
if (!t[nrl][ncl]) nrerror("allocation failure 3 in f3tensor()");
t[nrl][ncl] += NR_END;
t[nrl][ncl] -= ndl;
for(j=ncl+1;j<=nch;j++) t[nrl][j]=t[nrl][j-1]+ndep;
for(i=nrl+1;i<=nrh;i++) {
t[i]=t[i-1]+ncol;
t[i][ncl]=t[i-1][ncl]+ncol*ndep;
for(j=ncl+1;j<=nch;j++) t[i][j]=t[i][j-1]+ndep;
}
/* return pointer to array of pointers to rows */
return t;
}
char **cmatrix(long nrl, long nrh, long ncl, long nch)
/* allocate a int matrix with subscript range m[nrl..nrh][ncl..nch] */
{
long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
char **m;
/* allocate pointers to rows */
m=(char **) malloc((size_t)((nrow+NR_END)*sizeof(char*)));
if (!m) nrerror("allocation failure 1 in matrix()");
m += NR_END;
m -= nrl;
/* allocate rows and set pointers to them */
m[nrl]=(char *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(char)));
if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
m[nrl] += NR_END;
m[nrl] -= ncl;
for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
/* return pointer to array of pointers to rows */
return m;
}
void free_vector(float *v, long nl, long nh)
/* free a float vector allocated with vector() */
{
free((FREE_ARG) (v+nl-NR_END));
}
void free_ivector(int *v, long nl, long nh)
/* free an int vector allocated with ivector() */
{
free((FREE_ARG) (v+nl-NR_END));
}
void free_cvector(unsigned char *v, long nl, long nh)
/* free an unsigned char vector allocated with cvector() */
{
free((FREE_ARG) (v+nl-NR_END));
}
void free_lvector(long *v, long nl, long nh)
/* free an long vector allocated with lvector() */
{
free((FREE_ARG) (v+nl-NR_END));
}
void free_dvector(double *v, long nl, long nh)
/* free a double vector allocated with dvector() */
{
free((FREE_ARG) (v+nl-NR_END));
}
void free_matrix(float **m, long nrl, long nrh, long ncl, long nch)
/* free a float matrix allocated by matrix() */
{
free((FREE_ARG) (m[nrl]+ncl-NR_END));
free((FREE_ARG) (m+nrl-NR_END));
}
void free_dmatrix(double **m, long nrl, long nrh, long ncl, long nch)
/* free a double matrix allocated by dmatrix() */
{
free((FREE_ARG) (m[nrl]+ncl-NR_END));
free((FREE_ARG) (m+nrl-NR_END));
}
void free_imatrix(int **m, long nrl, long nrh, long ncl, long nch)
/* free an int matrix allocated by imatrix() */
{
free((FREE_ARG) (m[nrl]+ncl-NR_END));
free((FREE_ARG) (m+nrl-NR_END));
}
void free_submatrix(float **b, long nrl, long nrh, long ncl, long nch)
/* free a submatrix allocated by submatrix() */
{
free((FREE_ARG) (b+nrl-NR_END));
}
void free_convert_matrix(float **b, long nrl, long nrh, long ncl, long nch)
/* free a matrix allocated by convert_matrix() */
{
free((FREE_ARG) (b+nrl-NR_END));
}
void free_f3tensor(float ***t, long nrl, long nrh, long ncl, long nch,
long ndl, long ndh)
/* free a float f3tensor allocated by f3tensor() */
{
free((FREE_ARG) (t[nrl][ncl]+ndl-NR_END));
free((FREE_ARG) (t[nrl]+ncl-NR_END));
free((FREE_ARG) (t+nrl-NR_END));
}
void free_d3tensor(double ***t, long nrl, long nrh, long ncl, long nch,
long ndl, long ndh)
/* free a double f3tensor allocated by f3tensor() */
{
free((FREE_ARG) (t[nrl][ncl]+ndl-NR_END));
free((FREE_ARG) (t[nrl]+ncl-NR_END));
free((FREE_ARG) (t+nrl-NR_END));
}
void free_cmatrix(char **m, long nrl, long nrh, long ncl, long nch)
/* free a character matrix allocated by matrix() */
{
free((FREE_ARG) (m[nrl]+ncl-NR_END));
free((FREE_ARG) (m+nrl-NR_END));
}
#else /* ANSI */
/* traditional - K&R */
#include <stdio.h>
#define NR_END 1
#define FREE_ARG char*
void nrerror(error_text)
char error_text[];
/* Numerical Recipes standard error handler */
{
void exit();
fprintf(stderr,"Numerical Recipes run-time error...\n");
fprintf(stderr,"%s\n",error_text);
fprintf(stderr,"...now exiting to system...\n");
exit(1);
}
float *vector(nl,nh)
long nh,nl;
/* allocate a float vector with subscript range v[nl..nh] */
{
float *v;
v=(float *)malloc((unsigned int) ((nh-nl+1+NR_END)*sizeof(float)));
if (!v) nrerror("allocation failure in vector()");
return v-nl+NR_END;
}
int *ivector(nl,nh)
long nh,nl;
/* allocate an int vector with subscript range v[nl..nh] */
{
int *v;
v=(int *)malloc((unsigned int) ((nh-nl+1+NR_END)*sizeof(int)));
if (!v) nrerror("allocation failure in ivector()");
return v-nl+NR_END;
}
unsigned char *cvector(nl,nh)
long nh,nl;
/* allocate an unsigned char vector with subscript range v[nl..nh] */
{
unsigned char *v;
v=(unsigned char *)malloc((unsigned int) ((nh-nl+1+NR_END)*sizeof(unsigned char)));
if (!v) nrerror("allocation failure in cvector()");
return v-nl+NR_END;
}
long *lvector(nl,nh)
long nh,nl;
/* allocate an unsigned long vector with subscript range v[nl..nh] */
{
long *v;
v=(long *)malloc((int) ((nh-nl+1+NR_END)*sizeof(long)));
if (!v) nrerror("allocation failure in lvector()");
return v-nl+NR_END;
}
double *dvector(nl,nh)
long nh,nl;
/* allocate a double vector with subscript range v[nl..nh] */
{
double *v;
v=(double *)malloc((unsigned int) ((nh-nl+1+NR_END)*sizeof(double)));
if (!v) nrerror("allocation failure in dvector()");
return v-nl+NR_END;
}
float **matrix(nrl,nrh,ncl,nch)
long nch,ncl,nrh,nrl;
/* allocate a float matrix with subscript range m[nrl..nrh][ncl..nch] */
{
long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
float **m;
/* allocate pointers to rows */
m=(float **) malloc((unsigned int)((nrow+NR_END)*sizeof(float*)));
if (!m) nrerror("allocation failure 1 in matrix()");
m += NR_END;
m -= nrl;
/* allocate rows and set pointers to them */
m[nrl]=(float *) malloc((unsigned int)((nrow*ncol+NR_END)*sizeof(float)));
if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
m[nrl] += NR_END;
m[nrl] -= ncl;
for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
/* return pointer to array of pointers to rows */
return m;
}
double **dmatrix(nrl,nrh,ncl,nch)
long nch,ncl,nrh,nrl;
/* allocate a double matrix with subscript range m[nrl..nrh][ncl..nch] */
{
long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
double **m;
/* allocate pointers to rows */
m=(double **) malloc((unsigned int)((nrow+NR_END)*sizeof(double*)));
if (!m) nrerror("allocation failure 1 in matrix()");
m += NR_END;
m -= nrl;
/* allocate rows and set pointers to them */
m[nrl]=(double *) malloc((unsigned int)((nrow*ncol+NR_END)*sizeof(double)));
if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
m[nrl] += NR_END;
m[nrl] -= ncl;
for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
/* return pointer to array of pointers to rows */
return m;
}
int **imatrix(nrl,nrh,ncl,nch)
long nch,ncl,nrh,nrl;
/* allocate a int matrix with subscript range m[nrl..nrh][ncl..nch] */
{
long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
int **m;
/* allocate pointers to rows */
m=(int **) malloc((unsigned int)((nrow+NR_END)*sizeof(int*)));
if (!m) nrerror("allocation failure 1 in matrix()");
m += NR_END;
m -= nrl;
/* allocate rows and set pointers to them */
m[nrl]=(int *) malloc((unsigned int)((nrow*ncol+NR_END)*sizeof(int)));
if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
m[nrl] += NR_END;
m[nrl] -= ncl;
for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
/* return pointer to array of pointers to rows */
return m;
}
float **submatrix(a,oldrl,oldrh,oldcl,oldch,newrl,newcl)
float **a;
long newcl,newrl,oldch,oldcl,oldrh,oldrl;
/* point a submatrix [newrl..][newcl..] to a[oldrl..oldrh][oldcl..oldch] */
{
long i,j,nrow=oldrh-oldrl+1,ncol=oldcl-newcl;
float **m;
/* allocate array of pointers to rows */
m=(float **) malloc((unsigned int) ((nrow+NR_END)*sizeof(float*)));
if (!m) nrerror("allocation failure in submatrix()");
m += NR_END;
m -= newrl;
/* set pointers to rows */
for(i=oldrl,j=newrl;i<=oldrh;i++,j++) m[j]=a[i]+ncol;
/* return pointer to array of pointers to rows */
return m;
}
float **convert_matrix(a,nrl,nrh,ncl,nch)
float *a;
long nch,ncl,nrh,nrl;
/* allocate a float matrix m[nrl..nrh][ncl..nch] that points to the matrix
declared in the standard C manner as a[nrow][ncol], where nrow=nrh-nrl+1
and ncol=nch-ncl+1. The routine should be called with the address
&a[0][0] as the first argument. */
{
long i,j,nrow=nrh-nrl+1,ncol=nch-ncl+1;
float **m;
/* allocate pointers to rows */
m=(float **) malloc((unsigned int) ((nrow+NR_END)*sizeof(float*)));
if (!m) nrerror("allocation failure in convert_matrix()");
m += NR_END;
m -= nrl;
/* set pointers to rows */
m[nrl]=a-ncl;
for(i=1,j=nrl+1;i<nrow;i++,j++) m[j]=m[j-1]+ncol;
/* return pointer to array of pointers to rows */
return m;
}
double ***d3tensor(nrl,nrh,ncl,nch,ndl,ndh)
long nch,ncl,ndh,ndl,nrh,nrl;
/* allocate a float 3tensor with range t[nrl..nrh][ncl..nch][ndl..ndh] */
{
long i,j,nrow=nrh-nrl+1,ncol=nch-ncl+1,ndep=ndh-ndl+1;
double ***t;
/* allocate pointers to pointers to rows */
t=(double ***) malloc((unsigned int)((nrow+NR_END)*sizeof(double**)));
if (!t) nrerror("allocation failure 1 in f3tensor()");
t += NR_END;
t -= nrl;
/* allocate pointers to rows and set pointers to them */
t[nrl]=(double **) malloc((unsigned int)((nrow*ncol+NR_END)*sizeof(double*)));
if (!t[nrl]) nrerror("allocation failure 2 in f3tensor()");
t[nrl] += NR_END;
t[nrl] -= ncl;
/* allocate rows and set pointers to them */
t[nrl][ncl]=(double *) malloc((unsigned int)((nrow*ncol*ndep+NR_END)*sizeof(double)));
if (!t[nrl][ncl]) nrerror("allocation failure 3 in f3tensor()");
t[nrl][ncl] += NR_END;
t[nrl][ncl] -= ndl;
for(j=ncl+1;j<=nch;j++) t[nrl][j]=t[nrl][j-1]+ndep;
for(i=nrl+1;i<=nrh;i++) {
t[i]=t[i-1]+ncol;
t[i][ncl]=t[i-1][ncl]+ncol*ndep;
for(j=ncl+1;j<=nch;j++) t[i][j]=t[i][j-1]+ndep;
}
/* return pointer to array of pointers to rows */
return t;
}
float ***f3tensor(nrl,nrh,ncl,nch,ndl,ndh)
long nch,ncl,ndh,ndl,nrh,nrl;
/* allocate a float 3tensor with range t[nrl..nrh][ncl..nch][ndl..ndh] */
{
long i,j,nrow=nrh-nrl+1,ncol=nch-ncl+1,ndep=ndh-ndl+1;
float ***t;
/* allocate pointers to pointers to rows */
t=(float ***) malloc((unsigned int)((nrow+NR_END)*sizeof(float**)));
if (!t) nrerror("allocation failure 1 in f3tensor()");
t += NR_END;
t -= nrl;
/* allocate pointers to rows and set pointers to them */
t[nrl]=(float **) malloc((unsigned int)((nrow*ncol+NR_END)*sizeof(float*)));
if (!t[nrl]) nrerror("allocation failure 2 in f3tensor()");
t[nrl] += NR_END;
t[nrl] -= ncl;
/* allocate rows and set pointers to them */
t[nrl][ncl]=(float *) malloc((unsigned int)((nrow*ncol*ndep+NR_END)*sizeof(float)));
if (!t[nrl][ncl]) nrerror("allocation failure 3 in f3tensor()");
t[nrl][ncl] += NR_END;
t[nrl][ncl] -= ndl;
for(j=ncl+1;j<=nch;j++) t[nrl][j]=t[nrl][j-1]+ndep;
for(i=nrl+1;i<=nrh;i++) {
t[i]=t[i-1]+ncol;
t[i][ncl]=t[i-1][ncl]+ncol*ndep;
for(j=ncl+1;j<=nch;j++) t[i][j]=t[i][j-1]+ndep;
}
/* return pointer to array of pointers to rows */
return t;
}
char **cmatrix(nrl,nrh,ncl,nch)
long nch,ncl,nrh,nrl;
/* allocate a int matrix with subscript range m[nrl..nrh][ncl..nch] */
{
long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
char **m;
/* allocate pointers to rows */
m=(char **) malloc((unsigned int)((nrow+NR_END)*sizeof(char*)));
if (!m) nrerror("allocation failure 1 in matrix()");
m += NR_END;
m -= nrl;
/* allocate rows and set pointers to them */
m[nrl]=(char *) malloc((unsigned int)((nrow*ncol+NR_END)*sizeof(char)));
if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
m[nrl] += NR_END;
m[nrl] -= ncl;
for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
/* return pointer to array of pointers to rows */
return m;
}
void free_vector(v,nl,nh)
float *v;
long nh,nl;
/* free a float vector allocated with vector() */
{
free((FREE_ARG) (v+nl-NR_END));
}
void free_ivector(v,nl,nh)
int *v;
long nh,nl;
/* free an int vector allocated with ivector() */
{
free((FREE_ARG) (v+nl-NR_END));
}
void free_cvector(v,nl,nh)
long nh,nl;
unsigned char *v;
/* free an unsigned char vector allocated with cvector() */
{
free((FREE_ARG) (v+nl-NR_END));
}
void free_lvector(v,nl,nh)
long nh,nl;
long *v;
/* free an long vector allocated with lvector() */
{
free((FREE_ARG) (v+nl-NR_END));
}
void free_dvector(v,nl,nh)
double *v;
long nh,nl;
/* free a double vector allocated with dvector() */
{
free((FREE_ARG) (v+nl-NR_END));
}
void free_matrix(m,nrl,nrh,ncl,nch)
float **m;
long nch,ncl,nrh,nrl;
/* free a float matrix allocated by matrix() */
{
free((FREE_ARG) (m[nrl]+ncl-NR_END));
free((FREE_ARG) (m+nrl-NR_END));
}
void free_dmatrix(m,nrl,nrh,ncl,nch)
double **m;
long nch,ncl,nrh,nrl;
/* free a double matrix allocated by dmatrix() */
{
free((FREE_ARG) (m[nrl]+ncl-NR_END));
free((FREE_ARG) (m+nrl-NR_END));
}
void free_imatrix(m,nrl,nrh,ncl,nch)
int **m;
long nch,ncl,nrh,nrl;
/* free an int matrix allocated by imatrix() */
{
free((FREE_ARG) (m[nrl]+ncl-NR_END));
free((FREE_ARG) (m+nrl-NR_END));
}
void free_submatrix(b,nrl,nrh,ncl,nch)
float **b;
long nch,ncl,nrh,nrl;
/* free a submatrix allocated by submatrix() */
{
free((FREE_ARG) (b+nrl-NR_END));
}
void free_convert_matrix(b,nrl,nrh,ncl,nch)
float **b;
long nch,ncl,nrh,nrl;
/* free a matrix allocated by convert_matrix() */
{
free((FREE_ARG) (b+nrl-NR_END));
}
void free_f3tensor(t,nrl,nrh,ncl,nch,ndl,ndh)
float ***t;
long nch,ncl,ndh,ndl,nrh,nrl;
/* free a float f3tensor allocated by f3tensor() */
{
free((FREE_ARG) (t[nrl][ncl]+ndl-NR_END));
free((FREE_ARG) (t[nrl]+ncl-NR_END));
free((FREE_ARG) (t+nrl-NR_END));
}
void free_d3tensor(t,nrl,nrh,ncl,nch,ndl,ndh)
double ***t;
long nch,ncl,ndh,ndl,nrh,nrl;
/* free a float f3tensor allocated by f3tensor() */
{
free((FREE_ARG) (t[nrl][ncl]+ndl-NR_END));
free((FREE_ARG) (t[nrl]+ncl-NR_END));
free((FREE_ARG) (t+nrl-NR_END));
}
void free_cmatrix(m,nrl,nrh,ncl,nch)
char **m;
long nch,ncl,nrh,nrl;
/* free a double matrix allocated by dmatrix() */
{
free((FREE_ARG) (m[nrl]+ncl-NR_END));
free((FREE_ARG) (m+nrl-NR_END));
}
#endif /* ANSI */
/* CAUTION: This is the ANSI C (only) version of the Numerical Recipes
utility file nrutil.h. Do not confuse this file with the same-named
file nrutil.h that may be supplied in a 'misc' subdirectory.
*That* file is the one from the book, and contains both ANSI and
traditional K&R versions, along with #ifdef macros to select the
correct version. *This* file contains only ANSI C. */
#ifndef _NR_UTILS_H_
#define _NR_UTILS_H_
static float sqrarg;
#define SQR(a) ((sqrarg=(a)) == 0.0 ? 0.0 : sqrarg*sqrarg)
static double dsqrarg;
#define DSQR(a) ((dsqrarg=(a)) == 0.0 ? 0.0 : dsqrarg*dsqrarg)
static double dmaxarg1,dmaxarg2;
#define DMAX(a,b) (dmaxarg1=(a),dmaxarg2=(b),(dmaxarg1) > (dmaxarg2) ?\
(dmaxarg1) : (dmaxarg2))
static double dminarg1,dminarg2;
#define DMIN(a,b) (dminarg1=(a),dminarg2=(b),(dminarg1) < (dminarg2) ?\
(dminarg1) : (dminarg2))
static float maxarg1,maxarg2;
#define FMAX(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1) > (maxarg2) ?\
(maxarg1) : (maxarg2))
static float minarg1,minarg2;
#define FMIN(a,b) (minarg1=(a),minarg2=(b),(minarg1) < (minarg2) ?\
(minarg1) : (minarg2))
static long lmaxarg1,lmaxarg2;
#define LMAX(a,b) (lmaxarg1=(a),lmaxarg2=(b),(lmaxarg1) > (lmaxarg2) ?\
(lmaxarg1) : (lmaxarg2))
static long lminarg1,lminarg2;
#define LMIN(a,b) (lminarg1=(a),lminarg2=(b),(lminarg1) < (lminarg2) ?\
(lminarg1) : (lminarg2))
static int imaxarg1,imaxarg2;
#define IMAX(a,b) (imaxarg1=(a),imaxarg2=(b),(imaxarg1) > (imaxarg2) ?\
(imaxarg1) : (imaxarg2))
static int iminarg1,iminarg2;
#define IMIN(a,b) (iminarg1=(a),iminarg2=(b),(iminarg1) < (iminarg2) ?\
(iminarg1) : (iminarg2))
#define SIGN(a,b) ((b) >= 0.0 ? fabs(a) : -fabs(a))
void nrerror(char error_text[]);
float *vector(long nl, long nh);
int *ivector(long nl, long nh);
unsigned char *cvector(long nl, long nh);
long *lvector(long nl, long nh);
double *dvector(long nl, long nh);
float **matrix(long nrl, long nrh, long ncl, long nch);
double **dmatrix(long nrl, long nrh, long ncl, long nch);
int **imatrix(long nrl, long nrh, long ncl, long nch);
float **submatrix(float **a, long oldrl, long oldrh, long oldcl, long oldch,
long newrl, long newcl);
float **convert_matrix(float *a, long nrl, long nrh, long ncl, long nch);
float ***f3tensor(long nrl, long nrh, long ncl, long nch, long ndl, long ndh);
void free_vector(float *v, long nl, long nh);
void free_ivector(int *v, long nl, long nh);
void free_cvector(unsigned char *v, long nl, long nh);
void free_lvector(long *v, long nl, long nh);
void free_dvector(double *v, long nl, long nh);
void free_matrix(float **m, long nrl, long nrh, long ncl, long nch);
void free_dmatrix(double **m, long nrl, long nrh, long ncl, long nch);
void free_imatrix(int **m, long nrl, long nrh, long ncl, long nch);
void free_submatrix(float **b, long nrl, long nrh, long ncl, long nch);
void free_convert_matrix(float **b, long nrl, long nrh, long ncl, long nch);
void free_f3tensor(float ***t, long nrl, long nrh, long ncl, long nch,
long ndl, long ndh);
int ***i3tensor(long nrl, long nrh, long ncl, long nch, long ndl, long ndh);
void free_i3tensor(int ***t, long nrl, long nrh, long ncl, long nch,
long ndl, long ndh);
unsigned char ***b3tensor(long nrl, long nrh, long ncl, long nch, long ndl, long ndh);
void free_b3tensor(unsigned char ***t, long nrl, long nrh, long ncl, long nch,
long ndl, long ndh);
double ***d3tensor(long nrl, long nrh, long ncl, long nch, long ndl, long ndh);
void free_d3tensor(double ***t, long nrl, long nrh, long ncl, long nch,
long ndl, long ndh);
char **cmatrix(long nrl, long nrh, long ncl, long nch);
void free_cmatrix(char **m, long nrl, long nrh, long ncl, long nch);
#endif /* _NR_UTILS_H_ */
from ctypes import CDLL, POINTER, c_int, c_double,c_float,c_long,c_char_p
from numpy.ctypeslib import ndpointer
import numpy.ctypeslib as clb
import numpy as np
from astropy.io import fits
from scipy.stats import randint
from glob import glob
from datetime import datetime
import os
lib_path = os.path.dirname(os.path.realpath(__file__))
#lib_path += "/add_CTI.so"
lib_path += "/libmoduleCTI.so"
lib = CDLL(lib_path)
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),\
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.argtypes = [POINTER(c_int), c_int, c_int, c_int, c_int, POINTER(c_float), c_float, c_float, c_char_p]
def get_trap_map(seeds,nx,ny,nmax,rho_trap,beta,c,out_dir):
hsp_result = np.zeros(ny*nx*nmax)
nsp = len(rho_trap)
seeds1 = seeds.astype(np.int32)
seeds_p = np.ctypeslib.as_ctypes(seeds1)
rho_trap1 = rho_trap.astype(np.float32)
rho_trap_p = np.ctypeslib.as_ctypes(rho_trap1)
filename = (out_dir+"/trap.bin").encode('utf-8')
get_trap_h(seeds_p,c_int(int(nsp)),c_int(int(nx)),c_int(int(ny)),\
c_int(int(nmax)),rho_trap_p,c_float(beta),\
c_float(c),filename)
def bin2fits(bin_file,fits_dir,nsp,nx,ny,nmax):
data = np.fromfile(bin_file,dtype=np.float32)
data = data.reshape(nx,nsp,ny,nmax).transpose(1,3,2,0)
for i in range(nsp):
print("transfering trap type "+str(i+1))
datai = data[i]
ntrap = datai[0,:,:]
for j in range(nmax-1):
h = datai[j+1,:,:]
h[np.where(ntrap<j+1)] = 0
datai[j+1,:,:] = h
fits.writeto(fits_dir+"/trap_"+str(i+1)+".fits",datai,overwrite=True)
'''
def numpy_matrix_to_int_pointer(arr):
int_pointer_array = (POINTER(c_int)*arr.shape[0])()
for i in range(arr.shape[0]):
arr1 = np.array(arr[i].copy().tolist(),dtype=np.int32)
int_pointer_array[i] = np.ctypeslib.as_ctypes(arr1)
return int_pointer_array
def pointer_to_numpy_matrix(arr_pointer,row,col):
arr = np.zeros((row,col))
for i in range(row):
for j in range(col):
arr[i,j] = arr_pointer[i][j]
return arr
def CTI_sim(im,nx,ny,noverscan,nsp,nmax,beta,w,c,t,rho_trap,trap_seeds,release_seed=0):
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)
ntotal = ny+noverscan
beta_c,w_c,c_c = c_float(beta),c_float(w),c_float(c)
t_p = np.ctypeslib.as_ctypes(t)
rho_trap_p = np.ctypeslib.as_ctypes(rho_trap)
image_p = numpy_matrix_to_int_pointer(image)
trap_seeds1 = trap_seeds.astype(np.int32)
trap_seeds_p = np.ctypeslib.as_ctypes(trap_seeds1)
release_seed_c = c_int(release_seed)
image_cti = np.zeros((nx,ntotal))
image_cti = image_cti.astype(np.int32)
image_cti_p = numpy_matrix_to_int_pointer(image_cti)
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)
print(datetime.now())
image_cti_result = np.zeros((nx,ntotal))
for i in range(nx):
for j in range(ntotal):
image_cti_result[i,j] = image_cti_p[i][j]
return image_cti_result.T
"""
if __name__ =='__main__':
nx,ny,noverscan,nsp,nmax = 4608,4616,84,3,10
ntotal = 4700
beta,w,c = 0.478,84700,0
t = np.array([0.74,7.7,37],dtype=np.float32)
rho_trap = np.array([0.6,1.6,1.4],dtype=np.float32)
trap_seeds = np.array([0,100,1000],dtype=np.int32)
release_seed = 500
image = fits.getdata("inputdata/image.fits").astype(np.int32)
get_trap_map(trap_seeds,nx,ny,nmax,rho_trap,beta,c,".")
bin2fits("trap.bin",".",nsp,nx,ny,nmax)
image_cti = CTI_sim(image,nx,ny,noverscan,nsp,nmax,beta,w,c,t,rho_trap,trap_seeds,release_seed)
fits.writeto("output/image_CTI.fits",data=image_cti,overwrite=True)
"""
#include objects here:
objects = src/add_CTI1.o src/nrutil.o src/ran1.o src/ran2.o src/poidev.o src/gammln.o src/gasdev.o src/sort.o src/creattraps.o
add_CTI.so: $(objects)
gcc -shared -fPIC -std=c99 -o $@ $(objects) -lm
# general compilation rules
.SUFFIXES: .c .o
.c.o:
cc -c $< -O3 -shared -fPIC -std=c99 -o $@
.PHONY : clean
clean:
rm -f src/*.o add_CTI.so
CTI_lgl_v0.3/
add_CTIfinal.c >> add_CTI.c
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