Commit 95f81f91 authored by GZhao's avatar GZhao
Browse files

v2.0beta update

parent b7f8c4fa
Pipeline #4357 failed with stage
in 0 seconds
python D:\workdir\Project\csst_cpic_sim\csst_cpic_sim\main.py $args
\ No newline at end of file
[run]
branch = True
source = CpicImgSim
import unittest
from CpicImgSim.camera import EMCCD
from csst_cpic_sim.camera import CpicVisEmccd
import numpy as np
class TestEMCCD(unittest.TestCase):
def test_emccd_normal(self):
emccd = EMCCD()
emccd.switch['flat'] = True
emccd.switch['dark'] = True
emccd.switch['stripe'] = True
emccd.switch['cic'] = True
emccd.switch['cte'] = True
emccd.switch['badcolumn'] = True
emccd.switch['nonlinear'] = True
emccd.switch['cosmicray'] = True
emccd.switch['blooming'] = True
image_focal = np.zeros(emccd.flat_shape) + 1000
emccd = CpicVisEmccd()
image_focal = np.zeros(emccd.flat_shape) + 10
iamge_cosmic_ray = np.zeros(emccd.dark_shape)
emgain = 10
expt = 10
image = emccd.readout(image_focal, emgain, expt, iamge_cosmic_ray)
em_set = 200
expt = 0
image = emccd.readout(image_focal, em_set, expt, iamge_cosmic_ray)
self.assertEqual(image.shape[0], emccd.image_shape[0])
self.assertEqual(image.shape[1], emccd.image_shape[1])
self.assertEqual(image.shape[0], emccd.bias_shape[0])
self.assertEqual(image.shape[1], emccd.bias_shape[1])
def test_emccd_allclose(self):
emccd = EMCCD()
emccd.switch['flat'] = False
emccd.switch['dark'] = False
emccd.switch['stripe'] = False
emccd.switch['cic'] = False
emccd.switch['cte'] = False
emccd.switch['badcolumn'] = False
emccd.switch['nonlinear'] = False
emccd.switch['cosmicray'] = False
emccd.switch['blooming'] = False
config = {
'switch': {'shutter': False},
'dark': 1e-2,
'cic': 0.2,
'flat': np.zeros((1024, 1024)),
'ccd_temp': -200
}
image_focal = np.zeros(emccd.flat_shape) + 1000
emgain = 10
emccd = CpicVisEmccd(config)
for key in emccd.switch:
emccd.switch[key] = False
image_focal = np.zeros(emccd.flat_shape) + 10
em_set = 1024
expt = 10
image = emccd.readout(image_focal, emgain, expt)
image = emccd.readout(image_focal, em_set, expt)
self.assertEqual(image.shape[0], emccd.image_shape[0])
self.assertEqual(image.shape[1], emccd.image_shape[1])
self.assertEqual(image.shape[0], emccd.bias_shape[0])
self.assertEqual(image.shape[1], emccd.bias_shape[1])
def test_emccd_blooming(self):
import numpy as np
emccd = EMCCD()
emccd.switch['flat'] = True
emccd.switch['dark'] = True,
emccd.switch['stripe'] = True
emccd.switch['cic'] = True
emccd.switch['cte'] = True
emccd.switch['badcolumn'] = True
emccd.switch['nonlinear'] = True
emccd.switch['cosmicray'] = True
emccd.switch['blooming'] = True
emccd = CpicVisEmccd()
image_focal = np.zeros(emccd.flat_shape) + 1000
image_focal[100:200, 100:200] = 500_000
image_focal[100:200, 100:200] = 100_000
iamge_cosmic_ray = np.zeros(emccd.dark_shape)
emgain = 10
expt = 10
image = emccd.readout(image_focal, emgain, expt, iamge_cosmic_ray)
self.assertEqual(image.shape[0], emccd.image_shape[0])
self.assertEqual(image.shape[1], emccd.image_shape[1])
emgain = 800
expt = 1
emccd.ccd_temp = -100
emccd.time_syn(10, readout=False)
image = emccd.readout(image_focal, None, expt, iamge_cosmic_ray, emgain=emgain)
self.assertEqual(image.shape[0], emccd.bias_shape[0])
self.assertEqual(image.shape[1], emccd.bias_shape[1])
def test_em_fix_fun(self):
emccd = CpicVisEmccd()
emgain = emccd.em_fix_fuc_fit(-5)
self.assertEqual(emgain, 0.0)
def test_emccd_update(self):
emccd = CpicVisEmccd()
emccd.ccd_temp = -100
emgain = emccd.emgain_set(1024, ccd_temp=None, self_update=False )
self.assertAlmostEqual(emgain, 1.23, places=2)
if __name__ == '__main__':
# unittest.main()
from CpicImgSim.camera import CPIC_VIS_EMCCD
emccd = CPIC_VIS_EMCCD()
emccd.emgain_fun(1023, -30)
unittest.main()
# from CpicImgSim.camera import CPIC_VIS_EMCCD
# emccd = CPIC_VIS_EMCCD()
# emccd.emgain_fun(1023, -30)
bias_images = []
for _ in range(10):
bias = emccd.bias_frame(show=True)
bias_images.append(bias)
bias_images = np.array(bias_images)
from astropy.io import fits
fits.writeto("bias.fits", bias_images)
# bias_images = []
# for _ in range(10):
# bias = emccd.bias_frame(show=True)
# bias_images.append(bias)
# bias_images = np.array(bias_images)
# from astropy.io import fits
# fits.writeto("bias.fits", bias_images)
import unittest
from CpicImgSim.camera import CosmicRayFrameMaker, sky_frame_maker
from CpicImgSim.target import star_photlam
from CpicImgSim.optics import filter_throughput
from csst_cpic_sim.camera import CosmicRayFrameMaker, sky_frame_maker
from csst_cpic_sim.target import star_photlam
from csst_cpic_sim.optics import filter_throughput
import numpy as np
......
import unittest
from CpicImgSim import observation_simulation, quick_run
# from CpicImgSim import observation_simulation, quick_run
from csst_cpic_sim.main import main, quick_run_v2, observation_simulation_from_config
from csst_cpic_sim.config import config
import os
test_dir = os.path.dirname(__file__)
cases = os.path.join(test_dir, 'testcases')
output = os.path.join(test_dir, 'test_output')
config['output'] = output
from unittest.mock import patch
a = []
import io
import shutil
def clear_output():
shutil.rmtree(output)
os.mkdir(output)
class TestMain(unittest.TestCase):
def test_main(self):
target_example = {
'name': 'test',
'cstar': {
'magnitude': 0,
'ra': '120d',
'dec': '40d',
'distance': 10,
'sptype': 'F0III',
},
# 'stars': [
# {
# 'magnitude': 15,
# 'ra': '120d0m5s',
# 'dec': '40d00m3s',
# 'sptype': 'G0III',
# 'isblackbody': True
# },
# ],
# 'planets': [
# {
# 'radius': 20,
# 'pangle': 60,
# 'separation': 0.7,
# 'phase_angle': 90,
# }
# ]
}
quick_run('', 10, 'f661', 1, 1, 30)
quick_run('*2.4/10(3,5)/15(-4,2)', 21, 'f661', 1, 1, 30)
# normal target
observation_simulation(
target=target_example,
skybg=21,
expt=1,
nframe=1,
band='f661',
emgain=30,
obsid=51012345678,
def test_help(self):
main(argv = None)
def test_quick_run_cmdline(self):
clear_output()
main([
'quickrun',
'demo_0_20',
'0', '700', '1',
'-r', '30',
'-s', '19',
'-ec',
'-o', output
])
file = os.listdir(output)
self.assertEqual(len(file), 1)
self.assertEqual(file[0][:9], 'demo_0_20')
def test_quick_run_func(self):
clear_output()
quick_run_v2(
'', 'f661', 0, 20, 1, skybg=None, camera_effect=False, output=output
)
file = os.listdir(output)
self.assertEqual(len(file), 1)
self.assertEqual(file[0][:5], 'blank')
def test_full_run_func(self):
clear_output()
with open(cases+'/run_config.yaml', 'r') as fid:
text = fid.read()
text = text.replace('<output>', output)
with open(output+'/run_config_new.yaml', 'w') as fid:
fid.write(text)
observation_simulation_from_config(
os.path.join(cases, '05_sci.yaml'),
os.path.join(output, 'run_config_new.yaml')
)
# bias-gain
observation_simulation(
target={},
skybg=None,
expt=1,
nframe=1,
band='f661',
emgain=30,
obsid=50112345678,
file = os.listdir(output)
self.assertEqual(len(file), 4)
clear_output()
observation_simulation_from_config(
os.path.join(cases, '05_sci.yaml'),
None
)
file = os.listdir(output)
self.assertEqual(len(file), 1)
self.assertEqual(file[0][:5], 'SCI')
def test_full_run_cmdline(self):
clear_output()
main(['run', os.path.join(cases, '05_sci.yaml')])
file = os.listdir(output)
self.assertEqual(len(file), 1)
self.assertEqual(file[0][:5], 'SCI')
# def test_main(self):
# target_example = {
# 'name': 'test',
# 'cstar': {
# 'magnitude': 0,
# 'ra': '120d',
# 'dec': '40d',
# 'distance': 10,
# 'sptype': 'F0III',
# },
# }
# quick_run('', 10, 'f661', 1, 1, 30)
# quick_run('*2.4/10(3,5)/15(-4,2)', 21, 'f661', 1, 1, 30)
# # normal target
# observation_simulation(
# target=target_example,
# skybg=21,
# expt=1,
# nframe=1,
# band='f661',
# emgain=30,
# obsid=51012345678,
# )
# # bias-gain
# observation_simulation(
# target={},
# skybg=None,
# expt=1,
# nframe=1,
# band='f661',
# emgain=30,
# obsid=50112345678,
# )
if __name__ == '__main__':
......
import os
import unittest
from CpicImgSim.target import _sptype2num, hybrid_albedo_spectrum, star_photlam, spectrum_generator
from CpicImgSim.target import AlbedoCat, bcc_spectrum, planet_contrast, extract_target_x_y, TargetOjbect
from CpicImgSim.config import S
from math import pi
import numpy as np
import unittest
from csst_cpic_sim.target import _sptype2num, hybrid_albedo_spectrum, star_photlam
from csst_cpic_sim.target import spectrum_generator, target_file_load, detect_template_path
from csst_cpic_sim.target import AlbedoCat, bcc_spectrum, planet_contrast, extract_target_x_y, TargetOjbect
from csst_cpic_sim.config import S
tests_folder = os.path.dirname(os.path.abspath(__file__))
class TestTarget(unittest.TestCase):
def test_target_object(self):
d_cstar= {
......@@ -18,11 +19,11 @@ class TestTarget(unittest.TestCase):
'sptype': 'F0III',
}
cstar = TargetOjbect(d_cstar)
self.assertEqual(cstar.sp_model == 'star')
self.assertEqual(cstar.sp_model, 'star')
d_cstar['is_blackbody'] = True
d_cstar['sp_model'] = 'blackbody'
cstar = TargetOjbect(d_cstar)
self.assertEqual(cstar.sp_model == 'blackbody')
self.assertEqual(cstar.sp_model, 'blackbody')
d_planet = {
'radius': 2,
......@@ -31,20 +32,16 @@ class TestTarget(unittest.TestCase):
'coe_r': 0.7,
'separation': 0.5,
'phase_angle': 90,
'sp_model': 'hybrid_planet'
}
old_planet = TargetOjbect(d_planet, cstar = cstar, sp_model='planet')
self.assertEqual(old_planet.sp_model == 'hybrid_planet')
old_planet = TargetOjbect(d_planet, cstar = cstar)
self.assertEqual(old_planet.sp_model, 'hybrid_planet')
d_planet['sp_model'] = 'bcc_planet'
d_planet['coe_cloud'] = 1
d_planet['coe_metal'] = 0
old_planet = TargetOjbect(d_planet, cstar = cstar, sp_model='planet')
self.assertEqual(old_planet.sp_model == 'bcc_planet')
self.assertRaises(Warning, TargetOjbect, d_cstar, cstar = cstar, sp_model='planet')
def test_image_input(self):
old_planet = TargetOjbect(d_planet, cstar = cstar)
self.assertEqual(old_planet.sp_model, 'bcc_planet')
def test_bcc_class(self):
......@@ -133,62 +130,106 @@ class TestTarget(unittest.TestCase):
self.assertRaises(ValueError, extract_target_x_y,
dict(ra='120d', dec='40d'))
def test_spectrum_generator(self):
target_example = {
'cstar': {
'magnitude': 5,
'ra': '120d',
'dec': '40d',
'distance': 10,
'sptype': 'F0III',
},
'stars': [
{
'magnitude': 5,
'ra': '120.0001d',
'dec': '40.0001d',
'sptype': 'G0III',
'isblackbody': True
},
{
'magnitude': 2,
'ra': '120.0001d',
'dec': '40.0001d',
'sptype': 'G0II',
'isblackbody': True
},
{
'magnitude': 2,
'ra': '120.0001d',
'dec': '40.0001d',
'sptype': 'G0',
'isblackbody': True
},
],
'planets': [
{
'radius': 2,
'pangle': 60,
'separation': 0.5,
'phase_angle': 90,
},
{
'radius': 2,
'pangle': 60,
'separation': 0.5,
'phase_angle': 90,
'model': 'bcc_planet',
'coe_c': 1,
'coe_m': 0,
}
]
}
def test_detect_template_path(self):
for f in os.listdir():
if os.path.isfile(f):
break
self_file = detect_template_path(f)
self.assertEqual(os.path.basename(self_file), f)
self.assertEqual(os.path.dirname(self_file), os.getcwd())
target = detect_template_path('demo_5_25.yaml')
self.assertEqual(os.path.basename(target), 'demo_5_25.yaml')
self.assertRaises(FileExistsError, detect_template_path, 'demo_5_35.yaml')
def test_template_file_load(self):
t0 = target_file_load({0:0})
self.assertEqual(t0[0], 0)
t_error = target_file_load(['1'])
self.assertEqual(t_error, {})
t1 = target_file_load('')
self.assertEqual(t1, {})
t1 = target_file_load(' ')
self.assertEqual(t1, {})
t2 = target_file_load('*5')
self.assertEqual(t2['cstar']['magnitude'], 5)
self.assertEqual(t2['cstar']['sp_model'], 'reference')
t3 = target_file_load('*5/25(0.7,-0.7)')
self.assertEqual(t3['cstar']['magnitude'], 5)
self.assertEqual(t3['cstar']['sp_model'], 'reference')
self.assertEqual(len(t3['objects']), 1)
obj = t3['objects'][0]
self.assertEqual(obj['sp_model'], 'reference')
self.assertEqual(obj['magnitude'], 25)
self.assertEqual(obj['separation'], np.sqrt(0.7**2*2))
t3 = target_file_load('*5/25(0.7;-0.7)')
self.assertEqual(t3['cstar']['magnitude'], 5)
self.assertEqual(t3['cstar']['sp_model'], 'reference')
self.assertEqual(len(t3['objects']), 0)
t4 = target_file_load('demo_0_20')
self.assertEqual(t4['cstar']['magnitude'], 0)
self.assertEqual(t4['cstar']['sp_model'], 'reference')
self.assertEqual(len(t4['objects']), 1)
obj = t4['objects'][0]
self.assertEqual(obj['sp_model'], 'reference')
self.assertEqual(obj['magnitude'], 20)
t4 = target_file_load('demo_0_20.yaml')
self.assertEqual(t4['cstar']['magnitude'], 0)
self.assertEqual(t4['cstar']['sp_model'], 'reference')
self.assertEqual(len(t4['objects']), 1)
obj = t4['objects'][0]
self.assertEqual(obj['sp_model'], 'reference')
self.assertEqual(obj['magnitude'], 20)
t6 = target_file_load('file not exist')
self.assertEqual(t6, {})
def test_example_targets(self):
target1 = target_file_load('demo_0_20.yaml')
spectrums = spectrum_generator(target1)
self.assertEqual(len(spectrums), 2)
for sp in spectrums:
self.assertEqual(f"{sp[2].waveunits}", 'angstrom')
self.assertEqual(f"{sp[2].fluxunits}", 'photlam')
bcc = target_file_load('demo_bcc')
spectrums = spectrum_generator(bcc)
self.assertEqual(len(spectrums), 2)
del bcc['cstar']['distance']
self.assertRaises(ValueError, spectrum_generator, bcc)
tp = target_file_load('demo_planet_temp')
spectrums = spectrum_generator(tp)
self.assertEqual(len(spectrums), 2)
disk = target_file_load('demo_disk')
contrast = disk['objects'][0]['contrast']
spectrums = spectrum_generator(disk)
self.assertEqual(len(spectrums), 2)
self.assertIsNotNone(spectrums[1][3])
diff = spectrums[0][2] * contrast - spectrums[1][2]
self.assertTrue(diff.flux.mean() < 1)
template_star = target_file_load('demo_std_star')
spectrums = spectrum_generator(template_star)
self.assertEqual(len(spectrums), 1)
sps = spectrum_generator(target_example)
num_ouput = len([sp for sp in sps if sp is not None])
num_input = len(target_example['stars']) + \
len(target_example['planets']) + 1
self.assertEqual(num_ouput, num_input)
if __name__ == '__main__':
......
band: f661
emgain: 50
emset: -1
expt: 20
nframe: 2
obsid: '40100000005'
rotation: 30
shift:
- 0
- 0
skybg: 20.31
target:
cstar:
dec: 84.2875d
distance: 3.22
magnitude: 3.72
ra: -80.468889d
sptype: F0III
objects:
- coe_cloud: 2
coe_metal: 0
pangle: 45
phase_angle: 90
radius: 2
separation: 0.65
sp_model: bcc_planet
- magnitude: 20
pangle: 135
separation: 0.95
sp_model: blackbody
sptype: F0III
time: '2024-05-01T00:45:12.190274'
gnc_info:
SUNANGL0: 85.00895743
MOONANG0: 93.1528588
TEL_ALT0: 68
POS_ANG0: 30.0
POSI0_X: 17
POSI0_Y: 932.076
POSI0_Z: 85
VELO0_X: 78
VELO0_Y: 93
VELO0_Z: 85
EULER0_1: 64
EULER0_2: 94
EULER0_3: 45
RA_PNT0: 84.2875
DEC_PNT0: 57
CABEND: '2024-05-01T00:11:32.190274'
CABSTART: '2024-05-01T00:11:12.190274'
SUNANGL1: 90.00895743
MOONANG1: 98.1528588
TEL_ALT1: 73
POS_ANG1: 35.0
POSI1_X: 22
POSI1_Y: 937.076
POSI1_Z: 90
VELO1_X: 83
VELO1_Y: 98
VELO1_Z: 90
EULER1_1: 69
EULER1_2: 99
EULER1_3: 50
RA_PNT1: 84.2875
DEC_PNT1: 57
camera:
- {}
- bias_level_std: 0
cic: 0.2
dark: 1.0e-3
heat_speed: 0
switch:
badcolumn: false
bias_ci: false
bias_hp: false
bias_shift: false
bias_vp: false
cosmicray: false
cte: false
flat: false
nonlinear: false
shutter: false
- bias_level_std: 0
heat_speed: 0
cic: 0
dark: 0
max_adu: 65535
readout_noise: 0
switch:
badcolumn: false
bias_ci: false
bias_hp: false
bias_shift: false
bias_vp: false
blooming: false
cic: false
cosmicray: false
cte: false
dark: false
em_blooming: false
flat: false
nonlinear: false
shutter: false
csst_format: true
nsample: 5
check_fits_header: true
output: <output>
camera:
- {}
- bias_level_std: 0
cic: 0.2
dark: 1.0e-3
heat_speed: 0
switch:
badcolumn: false
bias_ci: false
bias_hp: false
bias_shift: false
bias_vp: false
cosmicray: false
cte: false
flat: false
nonlinear: false
shutter: false
- bias_level_std: 0
heat_speed: 0
cic: 0
dark: 0
max_adu: 65535
readout_noise: 0
switch:
badcolumn: false
bias_ci: false
bias_hp: false
bias_shift: false
bias_vp: false
blooming: false
cic: false
cosmicray: false
cte: false
dark: false
em_blooming: false
flat: false
nonlinear: false
shutter: false
csst_format: true
nsample: 5
check_fits_header: true
output: D:\workdir\Project\cpic-img-sim\tests\test_output
import unittest
from unittest import mock
from CpicImgSim.io import obsid_parser, primary_hdu, frame_header, save_fits_simple
import CpicImgSim.io as io
from csst_cpic_sim.io import obsid_parser, primary_hdu, frame_header, save_fits_simple
import csst_cpic_sim.io as io
from astropy.io import fits
import numpy as np
import yaml
......
......@@ -2,9 +2,9 @@ import unittest
import time
from CpicImgSim.target import star_photlam
from CpicImgSim.optics import make_focus_image, focal_mask, filter_throughput, ideal_focus_image
from CpicImgSim.config import which_focalplane, S
from csst_cpic_sim.target import star_photlam
from csst_cpic_sim.optics import make_focus_image, focal_mask, filter_throughput, ideal_focus_image
from csst_cpic_sim.config import which_focalplane, S
import numpy as np
from astropy.io import fits
......@@ -165,8 +165,8 @@ if __name__ == '__main__':
psf = psf / psf.sum()
return psf
from CpicImgSim.optics import convolve_psf
img_final = convolve_psf('f661', targets, cov_psf_func)
from CpicImgSim.optics import focal_convolve
img_final = focal_convolve('f661', targets, cov_psf_func)
fits.writeto('cov.fits', img_final, overwrite=True)
......
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