Commit 4afd1181 authored by Fang Yuedong's avatar Fang Yuedong
Browse files

add renamed files

parent 4c9c940a
This source diff could not be displayed because it is too large. You can view the blob instead.
30000 15000 7000 3140 1610 985 640 439.106795917950 332.953759944373 275 227.040718244473 184.215493793075 150 126.619219993047 111.295375994437 100 88.7046240055627 73.3807800069534 50
20000 12000 6000 2940 1540 945 625 434.136797758554 329.872405260036 271 223.547334192282 182.480458923860 150 127.340532138986 111.872425711189 100 88.1275742888112 72.6594678610140 50
16000 8000 4740 2470 1370 865 590 424.924281880506 325.703844589420 264 216.192536238306 177.535146441613 148 127.046313907011 112.081495570053 100 87.6962822077246 72.0647972041002 50.0000000000000
11500 6780 3440 1860 1110 755 525 379.404487437312 298.581196755993 251 210.819373149455 174.952207929139 146 125.577279224129 111.350712268192 100 88.2048432873637 72.6449429980935 50
6400 4480 2410 1410 910 635 454 340.751783794961 276.748855303552 237 201.241198619320 168.117212939671 141 122.135273579611 109.263774419244 99 87.9584478029781 72.7536153092783 50
3840 2830 1730 1100 749 545 410 320.344426947347 262.847445764680 223 188.949910583017 159.472507970269 136 119.263338127992 107.188448280172 97 85.9226628309394 71.1811063164520 50
2480 1870 1220 845 615 467 365 293.396992658036 243.591181463075 207 176.533897785140 151.075388060617 131 116.245193979181 104.996155183345 95 84.0038448166554 69.7548060208193 50
1650 1270 910 680 510 397 320 264.610999197860 224.242861979717 193 166.095447800706 143.176731474096 125 111.822024414131 101.902063975749 93 82.8757138020283 69.2890866969798 50
1180 940 700 530 416 338 282 239.383076216900 206.105287077371 179 155.528255539422 135.661786794464 120 108.689086810597 100.062380559589 92 82.3820638848555 69.0886909671804 50
910 730 555 442 356 292 250 217.177087096913 189.491057165516 166 145.936910467127 129.234403723928 116 106.038233997541 97.9416983091442 90 80.5027461353003 67.7395437802365 50
727.458461874842 590.813948067450 458.193770946183 380.572133876131 310.356532547090 256.488736025072 222.994441816550 196.671226703445 173.815072589959 153.993110811782 136.873310808416 122.526994434613 111.126321648939 102.546129456415 95.4719630478941 88.2920446606834 79.3945965320918 67.1678408994278 50
597.965786285821 501.989953285938 394.904124012041 330.741575203306 272.426631477206 229.377054599373 200.722233330368 178.298184971317 159.500061954463 143.183118953515 128.502453511432 115.812534648182 105.767674252986 98.6162295599359 92.9827391826220 87.0857870795079 79.1439572090571 67.3758335297333 50
505 442 352 292 243 209 183 162.948572168708 147.332365312119 134 121.348535356634 109.968784724353 101 95.0646938121630 90.7184217163971 86 78.9482449502696 67.6019728545037 50
434.185639242502 392.917767644504 318.376629582089 263.460345936839 222.213128168793 193.706429839947 169.485130042169 151.204192642909 137.846384427846 126.706631884706 115.748672194896 105.611485314551 97.6028618934907 92.4866702185601 88.8510891247191 84.7403750839550 78.1987845682555 67.2705740496079 50
379.733278070694 351.223716015657 291.034402908743 242.720597293047 207.589653411575 181.900455075790 159.198062706301 142.411627057649 130.568767913886 120.897501975771 111.291656158755 102.308298237053 95.1703061995698 90.5932607902020 87.2635537728093 83.3602796662423 77.0625329893514 66.5494082609872 50
338 317 269 227 196 172 151 135.608650155770 124.774226094307 116 107.379198451166 99.3648656018433 93 88.8745560395662 85.7663114983196 82 75.9003551683471 65.7921106271005 50
305.576758932926 289.996462836015 251.299710725463 213.919815709849 184.976139962921 162.759618689212 143.916889866626 129.942315002235 119.830751582977 111.550732019764 103.559236179535 96.2670337533281 90.5733418882097 86.9591551723739 84.2325779585342 80.7834920405337 75.0017792122157 65.2773212674234 50
279.989990320293 268.633701169951 236.958264352989 202.709368957841 174.696236357976 154.280384356725 137.633663992575 124.996787950500 115.479466152946 107.523169439697 99.9646100690791 93.2214659735947 88.1066565042906 85.0296921673207 82.7864480892672 79.7693905518192 74.3709858366659 64.9837002254965 50
259 251 225 193 166 147 132 120.465513676140 111.554773867063 104 96.8743867494711 90.6150297791503 86 83.4073096963458 81.6147366459656 79 73.9408189095900 64.8149125258764 50
240.766584285053 235.503788418843 214.572675664209 184.452983816359 159.437867535078 141.218058366167 126.921384565401 116.141066332417 107.932382590812 101.016365962164 94.4576243640234 88.6950668978497 84.5111779610779 82.2885391283921 80.8101206834713 78.4589900872436 73.6182148006372 64.6708622845800 50
225.047501388876 221.834071897130 205.317724864488 176.849334282997 154.403178934298 136.681711201015 122.526540582659 112.212537983324 104.653215400233 98.4172276728472 92.4433471106159 87.1508159029366 83.3289937091939 81.3714666204164 80.0887264542075 77.8954914598142 73.2064798864838 64.4364099834633 50
212 210 197 170 150 133 119 108.968150170540 101.799499174001 96 90.4505707007701 85.5319895790017 82 80.2295670263797 79.0725425099927 77 72.4830130455629 63.9926551958425 50
201.690237260197 200.025420525649 189.446623654739 163.786767543232 145.568686193063 129.849629327603 116.434608908807 106.594275830766 99.4340907887345 93.6430633908398 88.2383060221020 83.5493421751065 80.2337981193304 78.5974046590145 77.5383107374556 75.6027698327141 71.3370354228506 63.2873609859257 50
193.820004124204 191.992974204493 182.733799152023 158.374775391654 141.394751608534 127.177956024402 114.556469973086 104.869893480803 97.5423671128237 91.5486606096547 86.1255446666051 81.5577937981954 78.3921834737485 76.8539016661414 75.8817863624661 74.0930880533685 70.1050572294944 62.5349443814896 50
188 186 177 154 138 125 113 103.472133032474 96.0903350126002 90 84.5712734023673 80.0733060196831 77 75.5390696543528 74.6534779457045 73 69.2354109431844 62.0164859012028 50
183.842837044530 182.024159108189 172.344533420539 150.807353418120 135.768869174150 123.346387286385 111.525364984557 102.194863876581 95.0804127903208 89.2261952892916 83.9273482154446 79.4828215244692 76.4425184504893 75.0392867782103 74.2377524946620 72.6854861374556 69.0300582442024 61.9190393525135 50
180.968778410604 179.564401655269 168.710041490384 148.577490076315 134.536333150083 122.310168405082 110.395727673146 101.298913319835 94.6606644919416 89.2399817403860 84.1171019637652 79.6572074513917 76.5467168402564 75.1263973841518 74.3544108040747 72.8432674378230 69.2054776231947 62.0535516979877 50
179 178 166 147 134 122 110 101.161848147921 95.0155655993437 90 84.9567367232255 80.3383120527731 77 75.4186123665846 74.5571121154899 73 69.3317767733990 62.1369431889709 50
177.716192339462 176.914980078633 164.212279700143 145.946781747252 133.985466739967 122.542599304634 110.686153375186 102.107114286080 96.2902104428977 91.4521554519937 86.2753467707504 81.3049172606660 77.5516836342678 75.6227629219776 74.5104734257943 72.8034337656115 69.0902625613226 61.9595784328209 50
177.527104455602 176.712382137394 163.722331182737 146.018968006791 134.830286161505 124.136925910825 112.638397112107 104.241674217759 98.4721687469323 93.4914124288013 87.9535951873799 82.4835397260425 78.1987269430036 75.8001614550468 74.3229427532328 72.3856940471913 68.6070385465521 61.6055994609451 50
179 178 165 148 137 127 116 107.618369565958 101.509629084266 96 89.8850372551827 83.8376297796048 79 76.1007406725702 74.2139258713895 72 68.1194074619438 61.2325926607631 50
182.588689893918 181.204809466168 168.324866297409 152.368482555836 140.770745347464 131.186919198783 120.730021514699 112.131318192577 105.251463102523 98.8170347545853 91.9445480061505 85.3102631039541 79.9803766420691 76.6271017220435 74.3467174753257 71.8415195408390 67.8138035575069 60.9658651642528 50
188.293174137357 186.030514239601 173.215448593483 158.383674933557 145.661040722416 136.105090914582 126.050683878426 117.039742915109 109.149274748558 101.609183359224 93.9322814719671 86.7630167892887 81.0287457580997 77.2925195138329 74.6601679460091 71.8732156326712 67.6731871518618 60.8016070816238 50
196 192 179 165 151 141 131 121.444142789946 112.555351044031 104 95.6297114342393 88.0370932777062 82 77.9629373990224 75.0370165858846 72 67.6296500807821 60.7037292676442 50
205.595714751532 198.636152427066 185.006773628739 171.171880621998 156.116737055362 145.117131307640 134.615982788241 124.445016873482 114.821979010604 105.613038862998 96.8183116745740 88.9736950113039 82.7490320196781 78.5042987286960 75.3600026973967 72.1267843673288 67.6152841800409 60.6361425770818 50
216.966865661637 205.461857200499 190.564022591480 175.853739666387 160.340365763647 147.701969690105 135.936645151968 125.142864222108 115.301445669936 106.071854134304 97.2795559745780 89.4140244321794 83.1307344690420 78.7825468539377 75.5118655829902 72.1584804591610 67.5621812854119 60.5627578647043 50
230 212 195 178 163 148 134 122.638183892218 113.346038043688 105 96.7949181158581 89.1992839824302 83 78.6636251258314 75.3753445451096 72 67.4024332326682 60.4474859852797 50
\ No newline at end of file
from ObservationSim.ObservationSim import Observation
from ObservationSim._util import parse_args, make_run_dirs, generate_pointing_list
from observation_sim.ObservationSim import Observation
from observation_sim._util import parse_args, make_run_dirs, generate_pointing_list
from importlib.metadata import version
import os
import yaml
......@@ -18,7 +18,7 @@ def run_sim():
Parameters
----------
Catalog : Class
a catalog class which is inherited from ObservationSim.MockObject.CatalogBase
a catalog class which is inherited from observation_sim.mock_objects.CatalogBase
Returns
----------
......@@ -94,7 +94,7 @@ def run_sim():
# Initialize the simulation
if args.catalog is not None:
catalog_module = importlib.import_module('Catalog.'+args.catalog)
catalog_module = importlib.import_module('catalog.'+args.catalog)
obs = Observation(config=config, Catalog=catalog_module.Catalog,
work_dir=config['work_dir'], data_dir=config['data_dir'])
else:
......
......@@ -39,26 +39,26 @@ class build_ext(build_ext):
extensions = [
Extension("ObservationSim.MockObject.SpecDisperser.disperse_c.interp", ["ObservationSim/MockObject/SpecDisperser/disperse_c/interp.pyx"],
Extension("observation_sim.mock_objects.SpecDisperser.disperse_c.interp", ["observation_sim/mock_objects/SpecDisperser/disperse_c/interp.pyx"],
include_dirs=[numpy.get_include()],
libraries=["m"]),
Extension("ObservationSim.MockObject.SpecDisperser.disperse_c.disperse", ["ObservationSim/MockObject/SpecDisperser/disperse_c/disperse.pyx"],
Extension("observation_sim.mock_objects.SpecDisperser.disperse_c.disperse", ["observation_sim/mock_objects/SpecDisperser/disperse_c/disperse.pyx"],
include_dirs=[numpy.get_include()],
libraries=["m"]),
]
df_module = [CTypes('ObservationSim.Instrument.Chip.libBF.libmoduleBF',
['ObservationSim/Instrument/Chip/libBF/diffusion_X1.c',
'ObservationSim/Instrument/Chip/libBF/nrutil.c'],
df_module = [CTypes('observation_sim.instruments.chip.libBF.libmoduleBF',
['observation_sim/instruments/chip/libBF/diffusion_X1.c',
'observation_sim/instruments/chip/libBF/nrutil.c'],
include_dirs=[
'ObservationSim/Instrument/Chip/libBF/', '/usr/include']
'observation_sim/instruments/chip/libBF/', '/usr/include']
)]
cti_module = [CTypes('ObservationSim.Instrument.Chip.libCTI.libmoduleCTI',
['ObservationSim/Instrument/Chip/libCTI/src/add_CTI.c', 'ObservationSim/Instrument/Chip/libCTI/src/nrutil.c', 'ObservationSim/Instrument/Chip/libCTI/src/ran1.c', 'ObservationSim/Instrument/Chip/libCTI/src/ran2.c', 'ObservationSim/Instrument/Chip/libCTI/src/poidev.c',
'ObservationSim/Instrument/Chip/libCTI/src/gammln.c', 'ObservationSim/Instrument/Chip/libCTI/src/gasdev.c', 'ObservationSim/Instrument/Chip/libCTI/src/sort.c', 'ObservationSim/Instrument/Chip/libCTI/src/creattraps.c'],
cti_module = [CTypes('observation_sim.instruments.chip.libCTI.libmoduleCTI',
['observation_sim/instruments/chip/libCTI/src/add_CTI.c', 'observation_sim/instruments/chip/libCTI/src/nrutil.c', 'observation_sim/instruments/chip/libCTI/src/ran1.c', 'observation_sim/instruments/chip/libCTI/src/ran2.c', 'observation_sim/instruments/chip/libCTI/src/poidev.c',
'observation_sim/instruments/chip/libCTI/src/gammln.c', 'observation_sim/instruments/chip/libCTI/src/gasdev.c', 'observation_sim/instruments/chip/libCTI/src/sort.c', 'observation_sim/instruments/chip/libCTI/src/creattraps.c'],
include_dirs=[
'ObservationSim/Instrument/Chip/libCTI/src/', '/usr/include']
'observation_sim/instruments/chip/libCTI/src/', '/usr/include']
)]
......@@ -93,23 +93,23 @@ setup(name='csst_msc_sim',
# ],
package_data={
'ObservationSim.Astrometry.lib': ['libshao.so'],
'ObservationSim.Instrument.Chip.libBF': ['libmoduleBF.so'],
'ObservationSim.Instrument.Chip.libCTI': ['libmoduleCTI.so'],
'ObservationSim.MockObject.data': ['*.dat'],
'ObservationSim.MockObject.data.led': ['*.fits'],
'ObservationSim.Instrument.data': ['*.txt', '*.dat', '*.json'],
'ObservationSim.Instrument.data.field_distortion': ['*.pickle'],
'ObservationSim.Instrument.data.ccd': ['*.txt', '*.json'],
'ObservationSim.Instrument.data.filters': ['*.txt', '*.list', '*.dat'],
'ObservationSim.Instrument.data.throughputs': ['*.txt', '*.dat'],
'ObservationSim.Instrument.data.sls_conf': ['*.conf', '*.fits'],
# 'ObservationSim.Instrument.data.flatCube': ['*.fits'],
'Catalog.data': ['*.fits', '*.so'],
'ObservationSim.Config.Header': ['*.fits', '*.lst'],
'ObservationSim.Straylight.data': ['*.dat'],
'ObservationSim.Straylight.data.sky': ['*.dat'],
'ObservationSim.Straylight.lib': ['*'],
'observation_sim.astrometry.lib': ['libshao.so'],
'observation_sim.instruments.chip.libBF': ['libmoduleBF.so'],
'observation_sim.instruments.chip.libCTI': ['libmoduleCTI.so'],
'observation_sim.mock_objects.data': ['*.dat'],
'observation_sim.mock_objects.data.led': ['*.fits'],
'observation_sim.instruments.data': ['*.txt', '*.dat', '*.json'],
'observation_sim.instruments.data.field_distortion': ['*.pickle'],
'observation_sim.instruments.data.ccd': ['*.txt', '*.json'],
'observation_sim.instruments.data.filters': ['*.txt', '*.list', '*.dat'],
'observation_sim.instruments.data.throughputs': ['*.txt', '*.dat'],
'observation_sim.instruments.data.sls_conf': ['*.conf', '*.fits'],
# 'observation_sim.Instrument.data.flatCube': ['*.fits'],
'catalog.data': ['*.fits', '*.so'],
'observation_sim.config.header': ['*.fits', '*.lst'],
'observation_sim.sky_background.data': ['*.dat'],
'observation_sim.sky_background.data.sky': ['*.dat'],
'observation_sim.sky_background.lib': ['*'],
},
python_requires=">=3.11", # Python版本要求
install_requires=requirements,
......
import unittest
import sys,os,math
import sys
import os
import math
from itertools import islice
import numpy as np
import copy
......@@ -9,10 +11,9 @@ import galsim
import yaml
from astropy.io import fits
from ObservationSim.Instrument import Chip, Filter, FilterParam, FocalPlane
from ObservationSim.Instrument.Chip import ChipUtils as chip_utils
#from ObservationSim.sim_steps import add_brighter_fatter_CTE
from ObservationSim.Instrument.Chip.libCTI.CTI_modeling import CTI_sim
from observation_sim.instruments import Chip, Filter, FilterParam, FocalPlane
from observation_sim.instruments.chip import chip_util
from observation_sim.instruments.chip.libCTI.CTI_modeling import CTI_sim
try:
import importlib.resources as pkg_resources
......@@ -23,14 +24,15 @@ except ImportError:
### test FUNCTION --- START ###
def add_brighter_fatter(img):
#Inital dynamic lib
# Inital dynamic lib
try:
with pkg_resources.files('ObservationSim.Instrument.Chip.libBF').joinpath("libmoduleBF.so") as lib_path:
with pkg_resources.files('observation_sim.instruments.chip.libBF').joinpath("libmoduleBF.so") as lib_path:
lib_bf = ctypes.CDLL(lib_path)
except AttributeError:
with pkg_resources.path('ObservationSim.Instrument.Chip.libBF', "libmoduleBF.so") as lib_path:
with pkg_resources.path('observation_sim.instruments.chip.libBF', "libmoduleBF.so") as lib_path:
lib_bf = ctypes.CDLL(lib_path)
lib_bf.addEffects.argtypes = [ctypes.c_int, ctypes.c_int, ctypes.POINTER(ctypes.c_float), ctypes.POINTER(ctypes.c_float), ctypes.c_int]
lib_bf.addEffects.argtypes = [ctypes.c_int, ctypes.c_int, ctypes.POINTER(
ctypes.c_float), ctypes.POINTER(ctypes.c_float), ctypes.c_int]
# Set bit flag
bit_flag = 1
......@@ -38,11 +40,11 @@ def add_brighter_fatter(img):
nx, ny = img.array.shape
nn = nx * ny
arr_ima= (ctypes.c_float*nn)()
arr_imc= (ctypes.c_float*nn)()
arr_ima = (ctypes.c_float*nn)()
arr_imc = (ctypes.c_float*nn)()
arr_ima[:]= img.array.reshape(nn)
arr_imc[:]= np.zeros(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])
......@@ -50,20 +52,24 @@ def add_brighter_fatter(img):
return img
### test FUNCTION --- END ###
def defineCCD(iccd, config_file):
with open(config_file, "r") as stream:
try:
config = yaml.safe_load(stream)
#for key, value in config.items():
# for key, value in config.items():
# print (key + " : " + str(value))
except yaml.YAMLError as exc:
print(exc)
chip = Chip(chipID=iccd, config=config)
chip.img = galsim.ImageF(400, 200) #galsim.ImageF(chip.npix_x, chip.npix_y)
# galsim.ImageF(chip.npix_x, chip.npix_y)
chip.img = galsim.ImageF(400, 200)
focal_plane = FocalPlane(chip_list=[iccd])
chip.img.wcs= focal_plane.getTanWCS(192.8595, 27.1283, -113.4333*galsim.degrees, chip.pix_scale)
chip.img.wcs = focal_plane.getTanWCS(
192.8595, 27.1283, -113.4333*galsim.degrees, chip.pix_scale)
return chip
def defineFilt(chip):
filter_param = FilterParam()
filter_id, filter_type = chip.getChipFilter()
......@@ -79,11 +85,11 @@ def defineFilt(chip):
class detModule_coverage(unittest.TestCase):
def __init__(self, methodName='runTest'):
super(detModule_coverage, self).__init__(methodName)
##self.dataPath = "/public/home/chengliang/CSSOSDataProductsSims/csst-simulation/tests/UNIT_TEST_DATA" ##os.path.join(os.getenv('UNIT_TEST_DATA_ROOT'), 'csst_fz_gc1')
self.dataPath = os.path.join(os.getenv('UNIT_TEST_DATA_ROOT'), 'csst_msc_sim/csst_fz_msc')
# self.dataPath = "/public/home/chengliang/CSSOSDataProductsSims/csst-simulation/tests/UNIT_TEST_DATA" ##os.path.join(os.getenv('UNIT_TEST_DATA_ROOT'), 'csst_fz_gc1')
self.dataPath = os.path.join(
os.getenv('UNIT_TEST_DATA_ROOT'), 'csst_msc_sim/csst_fz_msc')
self.iccd = 1
def test_add_brighter_fatter(self):
config_file = os.path.join(self.dataPath, 'config_test.yaml')
chip = defineCCD(self.iccd, config_file)
......@@ -91,32 +97,31 @@ class detModule_coverage(unittest.TestCase):
print(chip.chipID)
print(chip.cen_pix_x, chip.cen_pix_y)
#objA-lowSFB
# objA-lowSFB
obj = galsim.Gaussian(sigma=0.2, flux=1000)
arr = obj.drawImage(nx=64, ny=64, scale=0.074).array
chip.img.array[(100-32):(100+32),(200-32):(200+32)] = arr[:,:]
chip.img.array[(100-32):(100+32), (200-32):(200+32)] = arr[:, :]
img_old = copy.deepcopy(chip.img)
img_new = add_brighter_fatter(img=chip.img)
arr1= img_old.array
arr2= img_new.array
arr1 = img_old.array
arr2 = img_new.array
deltaA_max = np.max(np.abs(arr2-arr1))
print('deltaA-max:', np.max(np.abs(arr2-arr1)))
print('deltaA-min:', np.min(np.abs(arr2-arr1)))
print('deltaA-max:', np.max(np.abs(arr2-arr1)))
print('deltaA-min:', np.min(np.abs(arr2-arr1)))
#objB-highSFB
# objB-highSFB
obj = galsim.Gaussian(sigma=0.2, flux=10000)
arr = obj.drawImage(nx=64, ny=64, scale=0.074).array
chip.img.array[(100-32):(100+32),(200-32):(200+32)] = arr[:,:]
chip.img.array[(100-32):(100+32), (200-32):(200+32)] = arr[:, :]
img_old = copy.deepcopy(chip.img)
img_new = add_brighter_fatter(img=chip.img)
arr3= img_old.array
arr4= img_new.array
arr3 = img_old.array
arr4 = img_new.array
deltaB_max = np.max(np.abs(arr4-arr3))
print('deltaB-max:', np.max(np.abs(arr4-arr3)))
print('deltaB-min:', np.min(np.abs(arr4-arr3)))
self.assertTrue( deltaB_max > deltaA_max )
print('deltaB-max:', np.max(np.abs(arr4-arr3)))
print('deltaB-min:', np.min(np.abs(arr4-arr3)))
self.assertTrue(deltaB_max > deltaA_max)
def test_apply_CTE(self):
config_file = os.path.join(self.dataPath, 'config_test.yaml')
......@@ -126,18 +131,21 @@ class detModule_coverage(unittest.TestCase):
print(chip.cen_pix_x, chip.cen_pix_y)
print(" Apply CTE Effect")
nx,ny,noverscan,nsp,nmax = 4608,4616,84,3,10
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)
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(os.path.join(self.dataPath, "testCTE_image_before.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(os.path.join(self.dataPath, "testCTE_image_after.fits"),data=image_cti,overwrite=True)
image = fits.getdata(os.path.join(
self.dataPath, "testCTE_image_before.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(os.path.join(
self.dataPath, "testCTE_image_after.fits"), data=image_cti, overwrite=True)
if __name__ == '__main__':
......
import unittest
import sys,os,math
import sys
import os
import math
from itertools import islice
import numpy as np
import galsim
import yaml
from ObservationSim.Instrument import Chip, Filter, FilterParam, FocalPlane
from ObservationSim.PSF.PSFInterp import PSFInterp
from observation_sim.instruments import Chip, Filter, FilterParam, FocalPlane
from observation_sim.PSF.PSFInterp import PSFInterp
def defineCCD(iccd, config_file):
with open(config_file, "r") as stream:
try:
config = yaml.safe_load(stream)
#for key, value in config.items():
# for key, value in config.items():
# print (key + " : " + str(value))
except yaml.YAMLError as exc:
print(exc)
chip = Chip(chipID=iccd, config=config)
chip.img = galsim.ImageF(chip.npix_x, chip.npix_y)
focal_plane = FocalPlane(chip_list=[iccd])
chip.img.wcs= focal_plane.getTanWCS(192.8595, 27.1283, -113.4333*galsim.degrees, chip.pix_scale)
chip.img.wcs = focal_plane.getTanWCS(
192.8595, 27.1283, -113.4333*galsim.degrees, chip.pix_scale)
return chip
def defineFilt(chip):
filter_param = FilterParam()
filter_id, filter_type = chip.getChipFilter()
......@@ -39,7 +43,8 @@ def defineFilt(chip):
class PSFInterpModule_coverage(unittest.TestCase):
def __init__(self, methodName='runTest'):
super(PSFInterpModule_coverage, self).__init__(methodName)
self.dataPath = os.path.join(os.getenv('UNIT_TEST_DATA_ROOT'), 'csst_msc_sim/csst_fz_msc')
self.dataPath = os.path.join(
os.getenv('UNIT_TEST_DATA_ROOT'), 'csst_msc_sim/csst_fz_msc')
self.iccd = 8
def test_loadPSFSet(self):
......@@ -48,23 +53,28 @@ class PSFInterpModule_coverage(unittest.TestCase):
bandpass = defineFilt(chip)
print(chip.chipID)
print(chip.cen_pix_x, chip.cen_pix_y)
pathTemp = self.dataPath #"/public/share/yangxuliu/CSSOSDataProductsSims/psfCube/set1_dynamic/"
psfModel= PSFInterp(chip, npsf=900, PSF_data_file=pathTemp, PSF_data_prefix="", HocBuild=True, LOG_DEBUG=True)
x, y = 4096, 4096 #imgPos[iobj, :] # try get the PSF at some location (1234, 1234) on the chip
# "/public/share/yangxuliu/CSSOSDataProductsSims/psfCube/set1_dynamic/"
pathTemp = self.dataPath
psfModel = PSFInterp(chip, npsf=900, PSF_data_file=pathTemp,
PSF_data_prefix="", HocBuild=True, LOG_DEBUG=True)
# imgPos[iobj, :] # try get the PSF at some location (1234, 1234) on the chip
x, y = 4096, 4096
x = x+chip.bound.xmin
y = y+chip.bound.ymin
pos_img = galsim.PositionD(x, y)
psf,_ = psfModel.get_PSF(chip=chip, pos_img=pos_img, bandpass=0, galsimGSObject=True)
psfA = psfModel.get_PSF(chip=chip, pos_img=pos_img, bandpass=bandpass[0], galsimGSObject=False)
psfB = psfModel.get_PSF(chip=chip, pos_img=pos_img, findNeighMode='hoclistFind', bandpass=bandpass[0], galsimGSObject=False)
self.assertTrue( psf != None )
self.assertTrue( np.max(np.abs(psfA-psfB))<1e-6 )
psf, _ = psfModel.get_PSF(
chip=chip, pos_img=pos_img, bandpass=0, galsimGSObject=True)
psfA = psfModel.get_PSF(
chip=chip, pos_img=pos_img, bandpass=bandpass[0], galsimGSObject=False)
psfB = psfModel.get_PSF(
chip=chip, pos_img=pos_img, findNeighMode='hoclistFind', bandpass=bandpass[0], galsimGSObject=False)
self.assertTrue(psf != None)
self.assertTrue(np.max(np.abs(psfA-psfB)) < 1e-6)
if __name__ == '__main__':
unittest.main()
#
#need add environment parameter UNIT_TEST_DATA_ROOT, link to "testData/"
#linx and mac can run as follow, need modify the name of file directory
#export UNIT_TEST_DATA_ROOT=/Users/zhangxin/Work/SlitlessSim/CSST_SIM/CSST_develop/csst-simulation/tests/testData
# need add environment parameter UNIT_TEST_DATA_ROOT, link to "testData/"
# linx and mac can run as follow, need modify the name of file directory
# export UNIT_TEST_DATA_ROOT=/Users/zhangxin/Work/SlitlessSim/CSST_SIM/CSST_develop/csst-simulation/tests/testData
#
import unittest
from ObservationSim.MockObject.SpecDisperser import rotate90, SpecDisperser
from observation_sim.mock_objects.SpecDisperser import rotate90, SpecDisperser
from ObservationSim.Config import ChipOutput
from ObservationSim.Instrument import Telescope, Chip, FilterParam, Filter, FocalPlane
from ObservationSim.MockObject import MockObject, Star
from ObservationSim.PSF import PSFGauss
from observation_sim.config import ChipOutput
from observation_sim.instruments import Telescope, Chip, FilterParam, Filter, FocalPlane
from observation_sim.mock_objects import MockObject, Star
from observation_sim.PSF import PSFGauss
import numpy as np
import galsim
......@@ -20,36 +20,37 @@ import matplotlib.pyplot as plt
from lmfit.models import LinearModel, GaussianModel
from ObservationSim.Config.Header import generateExtensionHeader
from observation_sim.config.header import generateExtensionHeader
import math
import yaml
import os
def getAngle132(x1=0, y1=0, z1=0, x2=0, y2=0, z2=0, x3=0, y3=0, z3=0):
cosValue = 0;
angle = 0;
cosValue = 0
angle = 0
x11 = x1 - x3;
y11 = y1 - y3;
z11 = z1 - z3;
x11 = x1 - x3
y11 = y1 - y3
z11 = z1 - z3
x22 = x2 - x3;
y22 = y2 - y3;
z22 = z2 - z3;
x22 = x2 - x3
y22 = y2 - y3
z22 = z2 - z3
tt = np.sqrt((x11 * x11 + y11 * y11 + z11 * z11) * (x22 * x22 + y22 * y22 + z22 * z22));
tt = np.sqrt((x11 * x11 + y11 * y11 + z11 * z11)
* (x22 * x22 + y22 * y22 + z22 * z22))
if (tt == 0):
return 0;
return 0
cosValue = (x11 * x22 + y11 * y22 + z11 * z22) / tt;
cosValue = (x11 * x22 + y11 * y22 + z11 * z22) / tt
if (cosValue > 1):
cosValue = 1;
cosValue = 1
if (cosValue < -1):
cosValue = -1;
angle = math.acos(cosValue);
return angle * 360 / (2 * math.pi);
cosValue = -1
angle = math.acos(cosValue)
return angle * 360 / (2 * math.pi)
def fit_SingleGauss(xX, yX, contmX, iHa0):
......@@ -71,26 +72,35 @@ def fit_SingleGauss(xX, yX, contmX, iHa0):
# print outX.params['g_center']
outX.fit_report(min_correl=0.25)
# print(outX.fit_report(min_correl=0.25))
line_slopeX = float(outX.fit_report(min_correl=0.25).split('line_slope:')[1].split('+/-')[0]) * contmX
line_slopeX = float(outX.fit_report(min_correl=0.25).split(
'line_slope:')[1].split('+/-')[0]) * contmX
err_line_slopeX = float(
outX.fit_report(min_correl=0.25).split('line_slope:')[1].split('+/-')[1].split('(')[0]) * contmX
line_interceptX = float(outX.fit_report(min_correl=0.25).split('line_intercept:')[1].split('+/-')[0]) * contmX
line_interceptX = float(outX.fit_report(min_correl=0.25).split(
'line_intercept:')[1].split('+/-')[0]) * contmX
err_line_interceptX = float(
outX.fit_report(min_correl=0.25).split('line_intercept:')[1].split('+/-')[1].split('(')[0]) * contmX
sigmaX = float(outX.fit_report(min_correl=0.25).split('g_sigma:')[1].split('+/-')[0])
err_sigmaX = float(outX.fit_report(min_correl=0.25).split('g_sigma:')[1].split('+/-')[1].split('(')[0])
sigmaX = float(outX.fit_report(min_correl=0.25).split(
'g_sigma:')[1].split('+/-')[0])
err_sigmaX = float(outX.fit_report(min_correl=0.25).split(
'g_sigma:')[1].split('+/-')[1].split('(')[0])
fwhmX = float(outX.fit_report(min_correl=0.25).split('g_fwhm:')[1].split('+/-')[0])
err_fwhmX = float(outX.fit_report(min_correl=0.25).split('g_fwhm:')[1].split('+/-')[1].split('(')[0])
fwhmX = float(outX.fit_report(min_correl=0.25).split(
'g_fwhm:')[1].split('+/-')[0])
err_fwhmX = float(outX.fit_report(min_correl=0.25).split(
'g_fwhm:')[1].split('+/-')[1].split('(')[0])
centerX = float(outX.fit_report(min_correl=0.25).split('g_center:')[1].split('+/-')[0])
err_centerX = float(outX.fit_report(min_correl=0.25).split('g_center:')[1].split('+/-')[1].split('(')[0])
centerX = float(outX.fit_report(min_correl=0.25).split(
'g_center:')[1].split('+/-')[0])
err_centerX = float(outX.fit_report(min_correl=0.25).split(
'g_center:')[1].split('+/-')[1].split('(')[0])
return sigmaX, err_sigmaX, fwhmX, err_fwhmX, centerX, err_centerX
def produceObj(x,y,chip, ra, dec, pa):
def produceObj(x, y, chip, ra, dec, pa):
pos_img = galsim.PositionD(x, y)
param = {}
......@@ -104,22 +114,22 @@ def produceObj(x,y,chip, ra, dec, pa):
obj = Star(param)
header_wcs = generateExtensionHeader(chip,
xlen=chip.npix_x,
ylen=chip.npix_y,
ra=ra,
dec=dec,
pa=pa,
gain=chip.gain,
readout=chip.read_noise,
dark=chip.dark_noise,
saturation=90000,
row_num=chip.rowID,
col_num=chip.colID,
pixel_scale=chip.pix_scale,
pixel_size=chip.pix_size,
xcen=chip.x_cen,
ycen=chip.y_cen,
extName='SCI')
xlen=chip.npix_x,
ylen=chip.npix_y,
ra=ra,
dec=dec,
pa=pa,
gain=chip.gain,
readout=chip.read_noise,
dark=chip.dark_noise,
saturation=90000,
row_num=chip.rowID,
col_num=chip.colID,
pixel_scale=chip.pix_scale,
pixel_size=chip.pix_size,
xcen=chip.x_cen,
ycen=chip.y_cen,
extName='SCI')
chip_wcs = galsim.FitsWCS(header=header_wcs)
param["ra"] = chip_wcs.posToWorld(pos_img).ra.deg
......@@ -143,50 +153,56 @@ def produceObj(x,y,chip, ra, dec, pa):
class TestSpecDisperse(unittest.TestCase):
def __init__(self, methodName='runTest'):
super(TestSpecDisperse,self).__init__(methodName)
super(TestSpecDisperse, self).__init__(methodName)
self.filePath('csst_msc_sim/test_sls_and_straylight')
# self.conff = conff
# self.throughputf = throughputf
def filePath(self, file_name):
fn = os.path.join(os.getenv('UNIT_TEST_DATA_ROOT'), file_name)
self.conff= os.path.join(fn, 'CSST_GI2.conf')
self.throughputf= os.path.join(fn, 'GI.Throughput.1st.fits')
self.conff = os.path.join(fn, 'CSST_GI2.conf')
self.throughputf = os.path.join(fn, 'GI.Throughput.1st.fits')
self.testDir = fn
self.outDataFn = os.path.join(fn,'output')
self.outDataFn = os.path.join(fn, 'output')
if os.path.isdir(self.outDataFn):
pass
else:
os.mkdir(self.outDataFn)
def test_rotate901(self):
m = np.array([[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15],[16,17,18,19,20],[21,22,23,24,25]])
m1 = np.array([[21,16,11,6,1],[22,17,12,7,2],[23,18,13,8,3],[24,19,14,9,4],[25,20,15,10,5]])
m2 = np.array([[5,10,15,20,25],[4,9,14,19,24],[3,8,13,18,23],[2,7,12,17,22],[1,6,11,16,21]])
m = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [
16, 17, 18, 19, 20], [21, 22, 23, 24, 25]])
m1 = np.array([[21, 16, 11, 6, 1], [22, 17, 12, 7, 2], [
23, 18, 13, 8, 3], [24, 19, 14, 9, 4], [25, 20, 15, 10, 5]])
m2 = np.array([[5, 10, 15, 20, 25], [4, 9, 14, 19, 24], [
3, 8, 13, 18, 23], [2, 7, 12, 17, 22], [1, 6, 11, 16, 21]])
xc = 2
yc = 2
isClockwise = 0
m1, xc1, yc1 = rotate90(array_orig=m, xc=xc, yc=yc, isClockwise=isClockwise)
m1, xc1, yc1 = rotate90(array_orig=m, xc=xc,
yc=yc, isClockwise=isClockwise)
self.assertTrue(xc1-xc == 0)
self.assertTrue(yc1-yc == 0)
self.assertTrue(np.sum(m-m1) == 0)
def test_rotate902(self):
m = np.array([[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15],[16,17,18,19,20],[21,22,23,24,25]])
m1 = np.array([[21,16,11,6,1],[22,17,12,7,2],[23,18,13,8,3],[24,19,14,9,4],[25,20,15,10,5]])
m2 = np.array([[5,10,15,20,25],[4,9,14,19,24],[3,8,13,18,23],[2,7,12,17,22],[1,6,11,16,21]])
m = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [
16, 17, 18, 19, 20], [21, 22, 23, 24, 25]])
m1 = np.array([[21, 16, 11, 6, 1], [22, 17, 12, 7, 2], [
23, 18, 13, 8, 3], [24, 19, 14, 9, 4], [25, 20, 15, 10, 5]])
m2 = np.array([[5, 10, 15, 20, 25], [4, 9, 14, 19, 24], [
3, 8, 13, 18, 23], [2, 7, 12, 17, 22], [1, 6, 11, 16, 21]])
xc = 2
yc = 2
isClockwise =1
m1, xc1, yc1 = rotate90(array_orig=m, xc=xc, yc=yc, isClockwise=isClockwise)
isClockwise = 1
m1, xc1, yc1 = rotate90(array_orig=m, xc=xc,
yc=yc, isClockwise=isClockwise)
self.assertTrue(xc1-xc == 0)
self.assertTrue(yc1-yc == 0)
self.assertTrue(np.sum(m-m2) == 0)
def test_Specdistperse1(self):
star = galsim.Gaussian(fwhm=0.39)
......@@ -236,7 +252,8 @@ class TestSpecDisperse(unittest.TestCase):
ids = wave_pix < 9700
ids1 = wave_pix[ids] > 6500
print('Spec disperse flux test')
self.assertTrue(np.mean((wave_flux[ids][ids1] - sed_i(wave_pix[ids][ids1]))/sed_i(wave_pix[ids][ids1]))<0.004)
self.assertTrue(np.mean(
(wave_flux[ids][ids1] - sed_i(wave_pix[ids][ids1]))/sed_i(wave_pix[ids][ids1])) < 0.004)
# plt.figure()
# plt.plot(wave_pix, wave_flux)
# plt.plot(sed['WAVELENGTH'], sed['FLUX'])
......@@ -299,14 +316,17 @@ class TestSpecDisperse(unittest.TestCase):
input_em_lam = 6600
ids = wave_pix < input_em_lam+200
ids1 = wave_pix[ids] > input_em_lam-200
deltLamda_pix = (max(wave_pix[ids][ids1]) - min(wave_pix[ids][ids1])) / (wave_pix[ids][ids1].shape[0] - 1)
_, _, fwhmx, fwhmx_err, center, center_err = fit_SingleGauss(wave_pix[ids][ids1], wave_flux[ids][ids1], 1.0, 6600)
deltLamda_pix = (max(
wave_pix[ids][ids1]) - min(wave_pix[ids][ids1])) / (wave_pix[ids][ids1].shape[0] - 1)
_, _, fwhmx, fwhmx_err, center, center_err = fit_SingleGauss(
wave_pix[ids][ids1], wave_flux[ids][ids1], 1.0, 6600)
print('Emission line position and shape test')
self.assertTrue(input_em_lam-center < deltLamda_pix)
# print(fwhmx/deltLamda_pix*pix_scale - psf_fwhm)
self.assertTrue(fwhmx/deltLamda_pix*pix_scale - psf_fwhm < np.abs(0.02))
self.assertTrue(fwhmx/deltLamda_pix*pix_scale -
psf_fwhm < np.abs(0.02))
# print('error is ',np.mean((wave_flux[ids][ids1] - sed_i(wave_pix[ids][ids1]))/sed_i(wave_pix[ids][ids1])))
# self.assertTrue(np.mean((wave_flux[ids][ids1] - sed_i(wave_pix[ids][ids1]))/sed_i(wave_pix[ids][ids1]))<0.004)
# plt.figure()
......@@ -356,7 +376,6 @@ class TestSpecDisperse(unittest.TestCase):
for i in range(sh[1]):
spec_pix[i] = sum(Aimg[:, i])
wave_flux = np.zeros(wave_pix.shape[0])
for i in np.arange(1, wave_pix.shape[0] - 1):
w = wave_pix[i]
......@@ -414,14 +433,12 @@ class TestSpecDisperse(unittest.TestCase):
plt.legend(['one spec', 'split in 8000 A'])
plt.show()
def test_double_disperse(self):
# work_dir = "/public/home/fangyuedong/CSST_unittest/CSST/test/"
# data_dir = "/Volumes/Extreme SSD/SimData/"
# data_dir = "/data/simudata/CSSOSDataProductsSims/data/"
configFn = os.path.join(self.testDir, 'config_C6.yaml')
normFilterFn = os.path.join(self.testDir, 'SLOAN_SDSS.g.fits')
normFilterFn = os.path.join(self.testDir, 'SLOAN_SDSS.g.fits')
norm_star = Table.read(normFilterFn)
with open(configFn, "r") as stream:
try:
......@@ -431,9 +448,9 @@ class TestSpecDisperse(unittest.TestCase):
except yaml.YAMLError as exc:
print(exc)
filter_param = FilterParam()
focal_plane = FocalPlane(survey_type=config["obs_setting"]["survey_type"])
focal_plane = FocalPlane(
survey_type=config["obs_setting"]["survey_type"])
chip = Chip(1, config=config)
filter_id, filter_type = chip.getChipFilter()
filt = Filter(filter_id=filter_id, filter_type=filter_type, filter_param=filter_param,
......@@ -442,13 +459,14 @@ class TestSpecDisperse(unittest.TestCase):
psf_model = PSFGauss(chip=chip)
wcs_fp = focal_plane.getTanWCS(float(config["obs_setting"]["ra_center"]), float(config["obs_setting"]["dec_center"]), float(config["obs_setting"]["image_rot"]) * galsim.degrees, chip.pix_scale)
wcs_fp = focal_plane.getTanWCS(float(config["obs_setting"]["ra_center"]), float(
config["obs_setting"]["dec_center"]), float(config["obs_setting"]["image_rot"]) * galsim.degrees, chip.pix_scale)
chip.img = galsim.ImageF(chip.npix_x, chip.npix_y)
chip.img.setOrigin(chip.bound.xmin, chip.bound.ymin)
chip.img.wcs = wcs_fp
obj, pos_img = produceObj(2000,4500, chip,float(config["obs_setting"]["ra_center"]), float(config["obs_setting"]["dec_center"]), float(config["obs_setting"]["image_rot"]))
obj, pos_img = produceObj(2000, 4500, chip, float(config["obs_setting"]["ra_center"]), float(
config["obs_setting"]["dec_center"]), float(config["obs_setting"]["image_rot"]))
# print(pos_img,chip.pix_scale)
obj.drawObj_slitless(
tel=tel,
......@@ -462,7 +480,8 @@ class TestSpecDisperse(unittest.TestCase):
exptime=150,
normFilter=norm_star)
obj, pos_img = produceObj(3685, 6500, chip,float(config["obs_setting"]["ra_center"]), float(config["obs_setting"]["dec_center"]), float(config["obs_setting"]["image_rot"]))
obj, pos_img = produceObj(3685, 6500, chip, float(config["obs_setting"]["ra_center"]), float(
config["obs_setting"]["dec_center"]), float(config["obs_setting"]["image_rot"]))
obj.drawObj_slitless(
tel=tel,
pos_img=pos_img,
......@@ -475,7 +494,8 @@ class TestSpecDisperse(unittest.TestCase):
exptime=150,
normFilter=norm_star)
obj, pos_img = produceObj(5000, 2500, chip, float(config["obs_setting"]["ra_center"]), float(config["obs_setting"]["dec_center"]), float(config["obs_setting"]["image_rot"]))
obj, pos_img = produceObj(5000, 2500, chip, float(config["obs_setting"]["ra_center"]), float(
config["obs_setting"]["dec_center"]), float(config["obs_setting"]["image_rot"]))
obj.drawObj_slitless(
tel=tel,
pos_img=pos_img,
......@@ -490,7 +510,8 @@ class TestSpecDisperse(unittest.TestCase):
print('Spec double disperse test')
from astropy.io import fits
fits.writeto(os.path.join(self.outDataFn,'test_sls_doubleDisp.fits'),chip.img.array, overwrite = True)
fits.writeto(os.path.join(
self.outDataFn, 'test_sls_doubleDisp.fits'), chip.img.array, overwrite=True)
# plt.figure()
# plt.imshow(chip.img.array)
......@@ -498,7 +519,7 @@ class TestSpecDisperse(unittest.TestCase):
def test_SLSImage_rotation(self):
from astropy.wcs import WCS
configFn = os.path.join(self.testDir,'config_C6.yaml')
configFn = os.path.join(self.testDir, 'config_C6.yaml')
with open(configFn, "r") as stream:
try:
......@@ -509,71 +530,71 @@ class TestSpecDisperse(unittest.TestCase):
print(exc)
chip = Chip(1, config=config)
ra=float(config["obs_setting"]["ra_center"])
dec=float(config["obs_setting"]["dec_center"])
pa=float(config["obs_setting"]["image_rot"])
ra = float(config["obs_setting"]["ra_center"])
dec = float(config["obs_setting"]["dec_center"])
pa = float(config["obs_setting"]["image_rot"])
chip.rotate_angle = 0
header_wcs1 = generateExtensionHeader(chip,
xlen=chip.npix_x,
ylen=chip.npix_y,
ra=ra,
dec=dec,
pa=pa,
gain=chip.gain,
readout=chip.read_noise,
dark=chip.dark_noise,
saturation=90000,
pixel_scale=chip.pix_scale,
row_num=chip.rowID,
col_num=chip.colID,
extName='raw')
xlen=chip.npix_x,
ylen=chip.npix_y,
ra=ra,
dec=dec,
pa=pa,
gain=chip.gain,
readout=chip.read_noise,
dark=chip.dark_noise,
saturation=90000,
pixel_scale=chip.pix_scale,
row_num=chip.rowID,
col_num=chip.colID,
extName='raw')
center = np.array([chip.npix_x / 2, chip.npix_y / 2])
h_wcs1 = WCS(header_wcs1)
x1, y1 = center + [100,0]
sky_1 = h_wcs1.pixel_to_world(x1,y1)
x1, y1 = center + [100, 0]
sky_1 = h_wcs1.pixel_to_world(x1, y1)
chip = Chip(1, config=config)
rot_angle = 1
chip.rotate_angle = rot_angle
header_wcs2 = generateExtensionHeader(chip,
xlen=chip.npix_x,
ylen=chip.npix_y,
ra=ra,
dec=dec,
pa=pa,
gain=chip.gain,
readout=chip.read_noise,
dark=chip.dark_noise,
saturation=90000,
pixel_scale=chip.pix_scale,
row_num=chip.rowID,
col_num=chip.colID,
extName='raw')
xlen=chip.npix_x,
ylen=chip.npix_y,
ra=ra,
dec=dec,
pa=pa,
gain=chip.gain,
readout=chip.read_noise,
dark=chip.dark_noise,
saturation=90000,
pixel_scale=chip.pix_scale,
row_num=chip.rowID,
col_num=chip.colID,
extName='raw')
h_wcs2 = WCS(header_wcs2)
x2, y2 = h_wcs2.world_to_pixel(sky_1)
angle = getAngle132(x1,y1,0,x2,y2,0,center[0],center[1],0)
angle = getAngle132(x1, y1, 0, x2, y2, 0, center[0], center[1], 0)
# print("rotation angle:" ,rot_angle ,chip.rotate_angle, angle)
# self.assertTrue(rot_angle - angle < np.abs(0.001))
rot_angle = 10
chip.rotate_angle = rot_angle
chip.rotate_angle = rot_angle
header_wcs2 = generateExtensionHeader(chip,
xlen=chip.npix_x,
ylen=chip.npix_y,
ra=ra,
dec=dec,
pa=pa,
gain=chip.gain,
readout=chip.read_noise,
dark=chip.dark_noise,
saturation=90000,
pixel_scale=chip.pix_scale,
row_num=chip.rowID,
col_num=chip.colID,
extName='raw')
xlen=chip.npix_x,
ylen=chip.npix_y,
ra=ra,
dec=dec,
pa=pa,
gain=chip.gain,
readout=chip.read_noise,
dark=chip.dark_noise,
saturation=90000,
pixel_scale=chip.pix_scale,
row_num=chip.rowID,
col_num=chip.colID,
extName='raw')
h_wcs2 = WCS(header_wcs2)
x2, y2 = h_wcs2.world_to_pixel(sky_1)
......@@ -584,19 +605,19 @@ class TestSpecDisperse(unittest.TestCase):
rot_angle = 50
chip.rotate_angle = rot_angle
header_wcs2 = generateExtensionHeader(chip,
xlen=chip.npix_x,
ylen=chip.npix_y,
ra=ra,
dec=dec,
pa=pa,
gain=chip.gain,
readout=chip.read_noise,
dark=chip.dark_noise,
saturation=90000,
pixel_scale=chip.pix_scale,
row_num=chip.rowID,
col_num=chip.colID,
extName='raw')
xlen=chip.npix_x,
ylen=chip.npix_y,
ra=ra,
dec=dec,
pa=pa,
gain=chip.gain,
readout=chip.read_noise,
dark=chip.dark_noise,
saturation=90000,
pixel_scale=chip.pix_scale,
row_num=chip.rowID,
col_num=chip.colID,
extName='raw')
h_wcs2 = WCS(header_wcs2)
x2, y2 = h_wcs2.world_to_pixel(sky_1)
......@@ -604,7 +625,6 @@ class TestSpecDisperse(unittest.TestCase):
# print(rot_angle - angle)
self.assertTrue(rot_angle - angle < np.abs(0.001))
chip = Chip(27, config=config)
ra = float(config["obs_setting"]["ra_center"])
......@@ -612,19 +632,19 @@ class TestSpecDisperse(unittest.TestCase):
pa = float(config["obs_setting"]["image_rot"])
chip.rotate_angle = 0
header_wcs1 = generateExtensionHeader(chip,
xlen=chip.npix_x,
ylen=chip.npix_y,
ra=ra,
dec=dec,
pa=pa,
gain=chip.gain,
readout=chip.read_noise,
dark=chip.dark_noise,
saturation=90000,
pixel_scale=chip.pix_scale,
row_num=chip.rowID,
col_num=chip.colID,
extName='raw')
xlen=chip.npix_x,
ylen=chip.npix_y,
ra=ra,
dec=dec,
pa=pa,
gain=chip.gain,
readout=chip.read_noise,
dark=chip.dark_noise,
saturation=90000,
pixel_scale=chip.pix_scale,
row_num=chip.rowID,
col_num=chip.colID,
extName='raw')
center = np.array([chip.npix_x / 2, chip.npix_y / 2])
h_wcs1 = WCS(header_wcs1)
......@@ -634,19 +654,19 @@ class TestSpecDisperse(unittest.TestCase):
rot_angle = 1
chip.rotate_angle = rot_angle
header_wcs2 = generateExtensionHeader(chip,
xlen=chip.npix_x,
ylen=chip.npix_y,
ra=ra,
dec=dec,
pa=pa,
gain=chip.gain,
readout=chip.read_noise,
dark=chip.dark_noise,
saturation=90000,
pixel_scale=chip.pix_scale,
row_num=chip.rowID,
col_num=chip.colID,
extName='raw')
xlen=chip.npix_x,
ylen=chip.npix_y,
ra=ra,
dec=dec,
pa=pa,
gain=chip.gain,
readout=chip.read_noise,
dark=chip.dark_noise,
saturation=90000,
pixel_scale=chip.pix_scale,
row_num=chip.rowID,
col_num=chip.colID,
extName='raw')
h_wcs2 = WCS(header_wcs2)
x2, y2 = h_wcs2.world_to_pixel(sky_1)
......@@ -657,19 +677,19 @@ class TestSpecDisperse(unittest.TestCase):
rot_angle = 10
chip.rotate_angle = rot_angle
header_wcs2 = generateExtensionHeader(chip,
xlen=chip.npix_x,
ylen=chip.npix_y,
ra=ra,
dec=dec,
pa=pa,
gain=chip.gain,
readout=chip.read_noise,
dark=chip.dark_noise,
saturation=90000,
pixel_scale=chip.pix_scale,
row_num=chip.rowID,
col_num=chip.colID,
extName='raw')
xlen=chip.npix_x,
ylen=chip.npix_y,
ra=ra,
dec=dec,
pa=pa,
gain=chip.gain,
readout=chip.read_noise,
dark=chip.dark_noise,
saturation=90000,
pixel_scale=chip.pix_scale,
row_num=chip.rowID,
col_num=chip.colID,
extName='raw')
h_wcs2 = WCS(header_wcs2)
x2, y2 = h_wcs2.world_to_pixel(sky_1)
......@@ -680,19 +700,19 @@ class TestSpecDisperse(unittest.TestCase):
rot_angle = 50
chip.rotate_angle = rot_angle
header_wcs2 = generateExtensionHeader(chip,
xlen=chip.npix_x,
ylen=chip.npix_y,
ra=ra,
dec=dec,
pa=pa,
gain=chip.gain,
readout=chip.read_noise,
dark=chip.dark_noise,
saturation=90000,
pixel_scale=chip.pix_scale,
row_num=chip.rowID,
col_num=chip.colID,
extName='raw')
xlen=chip.npix_x,
ylen=chip.npix_y,
ra=ra,
dec=dec,
pa=pa,
gain=chip.gain,
readout=chip.read_noise,
dark=chip.dark_noise,
saturation=90000,
pixel_scale=chip.pix_scale,
row_num=chip.rowID,
col_num=chip.colID,
extName='raw')
h_wcs2 = WCS(header_wcs2)
x2, y2 = h_wcs2.world_to_pixel(sky_1)
......@@ -701,11 +721,9 @@ class TestSpecDisperse(unittest.TestCase):
self.assertTrue(rot_angle - angle < np.abs(0.001))
if __name__ == '__main__':
os.environ['UNIT_TEST_DATA_ROOT']="/Users/zhangxin/Work/SlitlessSim/CSST_SIM/CSST_develop/csst-simulation/tests/testData"
os.environ['UNIT_TEST_DATA_ROOT'] = "/Users/zhangxin/Work/SlitlessSim/CSST_SIM/CSST_develop/csst-simulation/tests/testData"
testDir = os.getenv('UNIT_TEST_DATA_ROOT')
# conff= os.path.join(testDir, 'CSST_GI2.conf')
# throughputf= os.path.join(testDir, 'GI.Throughput.1st.fits')
......@@ -723,4 +741,4 @@ if __name__ == '__main__':
unittest.TextTestRunner(verbosity=2).run(suit)
# runner = unittest.TextTestRunner()
# runner.run(suit)
\ No newline at end of file
# runner.run(suit)
#
#need add environment parameter UNIT_TEST_DATA_ROOT, link to "testData/"
#linx and mac can run as follow, need modify the name of file directory
#export UNIT_TEST_DATA_ROOT=/Users/zhangxin/Work/SlitlessSim/CSST_SIM/CSST_develop/csst-simulation/tests/testData
# need add environment parameter UNIT_TEST_DATA_ROOT, link to "testData/"
# linx and mac can run as follow, need modify the name of file directory
# export UNIT_TEST_DATA_ROOT=/Users/zhangxin/Work/SlitlessSim/CSST_SIM/CSST_develop/csst-simulation/tests/testData
#
import unittest
from ObservationSim.Straylight import Straylight
from observation_sim.sky_background import Straylight
import numpy as np
import math
......@@ -17,8 +17,10 @@ import matplotlib.pyplot as plt
import os
hubbleAverZodiacal = {'nuv':0.0035,'u':0.0163,'g':0.1109,'r':0.1471,'i':0.1568,'z':0.0953,'y':0.0283}
hubbleAverEarthShine = {'nuv':0.00024,'u':0.0051,'g':0.0506,'r':0.0591,'i':0.0568,'z':0.0315,'y':0.0090}
hubbleAverZodiacal = {'nuv': 0.0035, 'u': 0.0163, 'g': 0.1109,
'r': 0.1471, 'i': 0.1568, 'z': 0.0953, 'y': 0.0283}
hubbleAverEarthShine = {'nuv': 0.00024, 'u': 0.0051, 'g': 0.0506,
'r': 0.0591, 'i': 0.0568, 'z': 0.0315, 'y': 0.0090}
# def transRaDec2D(ra, dec):
# x1 = np.cos(dec / 57.2957795) * np.cos(ra / 57.2957795);
......@@ -28,63 +30,69 @@ hubbleAverEarthShine = {'nuv':0.00024,'u':0.0051,'g':0.0506,'r':0.0591,'i':0.056
def getAngle132(x1=0, y1=0, z1=0, x2=0, y2=0, z2=0, x3=0, y3=0, z3=0):
cosValue = 0;
angle = 0;
cosValue = 0
angle = 0
x11 = x1 - x3;
y11 = y1 - y3;
z11 = z1 - z3;
x11 = x1 - x3
y11 = y1 - y3
z11 = z1 - z3
x22 = x2 - x3;
y22 = y2 - y3;
z22 = z2 - z3;
x22 = x2 - x3
y22 = y2 - y3
z22 = z2 - z3
tt = np.sqrt((x11 * x11 + y11 * y11 + z11 * z11) * (x22 * x22 + y22 * y22 + z22 * z22));
tt = np.sqrt((x11 * x11 + y11 * y11 + z11 * z11)
* (x22 * x22 + y22 * y22 + z22 * z22))
if (tt == 0):
return 0;
return 0
cosValue = (x11 * x22 + y11 * y22 + z11 * z22) / tt;
cosValue = (x11 * x22 + y11 * y22 + z11 * z22) / tt
if (cosValue > 1):
cosValue = 1;
cosValue = 1
if (cosValue < -1):
cosValue = -1;
angle = math.acos(cosValue);
return angle * 360 / (2 * math.pi);
cosValue = -1
angle = math.acos(cosValue)
return angle * 360 / (2 * math.pi)
def calculateAnglePwithEarth(sat = np.array([0,0,0]), pointing = np.array([0,0,0]), sun = np.array([0,0,0])):
def calculateAnglePwithEarth(sat=np.array([0, 0, 0]), pointing=np.array([0, 0, 0]), sun=np.array([0, 0, 0])):
modSat = np.sqrt(sat[0]*sat[0] + sat[1]*sat[1]+sat[2]*sat[2])
modPoint = np.sqrt(pointing[0]*pointing[0] + pointing[1]*pointing[1] + pointing[2]*pointing[2])
withLocalZenithAngle = (pointing[0] * sat[0] + pointing[1] * sat[1] + pointing[2] * sat[2]) / (modPoint*modSat)
modPoint = np.sqrt(pointing[0]*pointing[0] +
pointing[1]*pointing[1] + pointing[2]*pointing[2])
withLocalZenithAngle = (
pointing[0] * sat[0] + pointing[1] * sat[1] + pointing[2] * sat[2]) / (modPoint*modSat)
innerM_sat_sun = sat[0] * sun[0] + sat[1] * sun[1] + sat[2] * sun[2]
cosAngle = innerM_sat_sun / (modSat * cons.au.value/1000)
isInSunSide = 1
if (cosAngle < -0.3385737): #cos109.79
isInSunSide = -1;
if (cosAngle < -0.3385737): # cos109.79
isInSunSide = -1
elif cosAngle >= -0.3385737 and cosAngle <= 0.3385737:
isInSunSide = 0;
isInSunSide = 0
return math.acos(withLocalZenithAngle)*180/math.pi, isInSunSide
return math.acos(withLocalZenithAngle)*180/math.pi,isInSunSide
class TestStraylight(unittest.TestCase):
def __init__(self,methodName='runTest', filter = 'i', grating = "GI"):
super(TestStraylight,self).__init__(methodName)
def __init__(self, methodName='runTest', filter='i', grating="GI"):
super(TestStraylight, self).__init__(methodName)
# print(file_name)
# fn = os.path.join(os.getenv('UNIT_TEST_DATA_ROOT'), file_name)
# self.pointingData = np.loadtxt(os.path.join(fn, 'Straylight_test.dat'), dtype=np.double)
self.filePath('csst_msc_sim/test_sls_and_straylight')
self.filter = filter
self.grating = grating
def filePath(self, file_name):
fn = os.path.join(os.getenv('UNIT_TEST_DATA_ROOT'), file_name)
self.pointingData = np.loadtxt(os.path.join(fn, 'Straylight_test.dat'), dtype=np.double)
self.pointingData = np.loadtxt(os.path.join(
fn, 'Straylight_test.dat'), dtype=np.double)
def test_EarthShineFilter(self):
d_sh = self.pointingData.shape
sl_e_pix = np.zeros([d_sh[0],3],dtype=np.double)
sl_e_pix = np.zeros([d_sh[0], 3], dtype=np.double)
for i in np.arange(d_sh[0]):
# if i > 50:
......@@ -92,17 +100,19 @@ class TestStraylight(unittest.TestCase):
ju = self.pointingData[i, 5]
# pointing = transRaDec2D(self.pointingData[i, 0], self.pointingData[i, 1])
# print(ju, pointing, surveylist[i,3:9])
sl = Straylight(jtime=ju, sat_pos=self.pointingData[i, 6:9], pointing_radec=np.array([self.pointingData[i, 0], self.pointingData[i, 1]]),sun_pos=self.pointingData[i,9:12])
sl = Straylight(jtime=ju, sat_pos=self.pointingData[i, 6:9], pointing_radec=np.array(
[self.pointingData[i, 0], self.pointingData[i, 1]]), sun_pos=self.pointingData[i, 9:12])
e1, py = sl.calculateEarthShineFilter(filter=self.filter)
earthZenithAngle, isInSunSide = calculateAnglePwithEarth(sat=self.pointingData[i, 6:9], pointing= sl.pointing, sun=self.pointingData[i,9:12])
earthZenithAngle, isInSunSide = calculateAnglePwithEarth(
sat=self.pointingData[i, 6:9], pointing=sl.pointing, sun=self.pointingData[i, 9:12])
# e2, _ = sl.calculateZodiacalFilter2(filter='i', sun_pos=sl.sun_pos)
# e3 = sl.calculateStarLightFilter(filter='i', pointYaxis=py)
# e_all = sl.calculateStrayLightFilter(filter='i')
# s_pix, spec = sl.calculateStrayLightGrating(grating='GI')
sl_e_pix[i,0] = e1
sl_e_pix[i, 0] = e1
sl_e_pix[i, 1] = earthZenithAngle
sl_e_pix[i, 2] = isInSunSide
median = np.median(sl_e_pix[:,0])
median = np.median(sl_e_pix[:, 0])
print(' average Earthshine %s: %e' % (self.filter, median))
self.assertTrue(median-hubbleAverEarthShine[self.filter] < 0.1)
plt.figure()
......@@ -117,27 +127,29 @@ class TestStraylight(unittest.TestCase):
def test_ZodiacalFilter(self):
d_sh = self.pointingData.shape
sl_e_pix = np.zeros([d_sh[0],2],dtype=np.double)
sl_e_pix = np.zeros([d_sh[0], 2], dtype=np.double)
for i in np.arange(d_sh[0]):
ju = self.pointingData[i, 5]
sl = Straylight(jtime=ju, sat_pos=self.pointingData[i, 6:9], pointing_radec=np.array([self.pointingData[i, 0], self.pointingData[i, 1]]),sun_pos=self.pointingData[i,9:12])
e1, _ = sl.calculateZodiacalFilter2(filter=self.filter, sun_pos=sl.sun_pos)
sl_e_pix[i,0] = e1
sl_e_pix[i,1] = getAngle132(x1=self.pointingData[i,9], y1=self.pointingData[i,10], z1=self.pointingData[i,11], x2=sl.pointing[0],
y2=sl.pointing[1], z2=sl.pointing[2], x3=0, y3=0, z3=0)
sl = Straylight(jtime=ju, sat_pos=self.pointingData[i, 6:9], pointing_radec=np.array(
[self.pointingData[i, 0], self.pointingData[i, 1]]), sun_pos=self.pointingData[i, 9:12])
e1, _ = sl.calculateZodiacalFilter2(
filter=self.filter, sun_pos=sl.sun_pos)
sl_e_pix[i, 0] = e1
sl_e_pix[i, 1] = getAngle132(x1=self.pointingData[i, 9], y1=self.pointingData[i, 10], z1=self.pointingData[i, 11], x2=sl.pointing[0],
y2=sl.pointing[1], z2=sl.pointing[2], x3=0, y3=0, z3=0)
plt.figure()
plt.plot(sl_e_pix[:, 0], sl_e_pix[:, 1], 'r.')
plt.xlabel('straylight-zodiacal(e-/pixel/s)')
plt.ylabel('Angle between pointing and sun(degree)')
plt.show()
median = np.median(sl_e_pix[:,0])
median = np.median(sl_e_pix[:, 0])
print(' average Zodiacal %s: %f' % (self.filter, median))
self.assertTrue(median-hubbleAverZodiacal[self.filter] < 0.1)
def test_StarFilter(self):
d_sh = self.pointingData.shape
sl_e_pix = np.zeros(d_sh[0],dtype=np.double)
sl_e_pix = np.zeros(d_sh[0], dtype=np.double)
tnum = 10
for i in np.arange(tnum):
......@@ -146,20 +158,21 @@ class TestStraylight(unittest.TestCase):
ju = self.pointingData[i, 5]
# pointing = transRaDec2D(self.pointingData[i, 0], self.pointingData[i, 1])
# print(ju, pointing, surveylist[i,3:9])
sl = Straylight(jtime=ju, sat_pos=self.pointingData[i, 6:9], pointing_radec=np.array([self.pointingData[i, 0], self.pointingData[i, 1]]),sun_pos=self.pointingData[i,9:12])
sl = Straylight(jtime=ju, sat_pos=self.pointingData[i, 6:9], pointing_radec=np.array(
[self.pointingData[i, 0], self.pointingData[i, 1]]), sun_pos=self.pointingData[i, 9:12])
e1, py = sl.calculateEarthShineFilter(filter=self.filter)
# e2, _ = sl.calculateZodiacalFilter2(filter='i', sun_pos=sl.sun_pos)
e3 = sl.calculateStarLightFilter(filter=self.filter, pointYaxis=py)
# e_all = sl.calculateStrayLightFilter(filter='i')
# s_pix, spec = sl.calculateStrayLightGrating(grating='GI')
sl_e_pix[i] = e3
median = np.median(sl_e_pix[0:tnum])
median = np.median(sl_e_pix[0:tnum])
print(' average Earthshine %s: %e' % (self.filter, median))
self.assertTrue(median-hubbleAverEarthShine[self.filter] < 0.2)
def test_GratingStraylight(self):
d_sh = self.pointingData.shape
sl_e_pix = np.zeros(d_sh[0],dtype=np.double)
sl_e_pix = np.zeros(d_sh[0], dtype=np.double)
tnum = 10
for i in np.arange(tnum):
......@@ -168,7 +181,8 @@ class TestStraylight(unittest.TestCase):
ju = self.pointingData[i, 5]
# pointing = transRaDec2D(self.pointingData[i, 0], self.pointingData[i, 1])
# print(ju, pointing, surveylist[i,3:9])
sl = Straylight(jtime=ju, sat_pos=self.pointingData[i, 6:9], pointing_radec=np.array([self.pointingData[i, 0], self.pointingData[i, 1]]),sun_pos=self.pointingData[i,9:12])
sl = Straylight(jtime=ju, sat_pos=self.pointingData[i, 6:9], pointing_radec=np.array(
[self.pointingData[i, 0], self.pointingData[i, 1]]), sun_pos=self.pointingData[i, 9:12])
# e1, py = sl.calculateEarthShineFilter(filter=self.filter)
# e2, _ = sl.calculateZodiacalFilter2(filter='i', sun_pos=sl.sun_pos)
# e3 = sl.calculateStarLightFilter(filter=self.filter, pointYaxis=py)
......@@ -179,18 +193,15 @@ class TestStraylight(unittest.TestCase):
plt.plot(spec['WAVELENGTH'], spec['FLUX'], 'r')
plt.xlabel('WAVELENGTH')
plt.ylabel('F$\lambda$(erg/s/cm2/A/arcsec2)')
plt.xlim(2000,10000)
plt.xlim(2000, 10000)
plt.show()
median = np.median(sl_e_pix[0:tnum])
median = np.median(sl_e_pix[0:tnum])
print(' average Earthshine %s: %e' % (self.grating, median))
self.assertTrue(median < 0.8)
if __name__ == '__main__':
os.environ['UNIT_TEST_DATA_ROOT']="/Users/zhangxin/Work/SlitlessSim/CSST_SIM/CSST_develop/csst-simulation/tests/testData"
os.environ['UNIT_TEST_DATA_ROOT'] = "/Users/zhangxin/Work/SlitlessSim/CSST_SIM/CSST_develop/csst-simulation/tests/testData"
# suit = unittest.TestSuite()
# case1 = TestStraylight('test_EarthShineFilter', filter = 'i')
......@@ -201,4 +212,4 @@ if __name__ == '__main__':
# suit.addTest(case3)
# case4 = TestStraylight('test_GratingStraylight', grating = 'GI')
# suit.addTest(case4)
# unittest.TextTestRunner(verbosity=2).run(suit)
\ No newline at end of file
# unittest.TextTestRunner(verbosity=2).run(suit)
......@@ -4,7 +4,7 @@ import sys
from astropy.time import Time
from datetime import datetime
from ObservationSim.Astrometry.Astrometry_util import on_orbit_obs_position
from observation_sim.astrometry.Astrometry_util import on_orbit_obs_position
class TestAstrometry(unittest.TestCase):
......
import unittest
import sys,os,math
import sys
import os
import math
from itertools import islice
import numpy as np
import galsim
import yaml
from ObservationSim.Instrument import Chip, Filter, FilterParam, FocalPlane
#from ObservationSim.Instrument.Chip import ChipUtils as chip_utils
from observation_sim.instruments import Chip, Filter, FilterParam, FocalPlane
### test FUNCTION --- START ###
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+0.5*readout_time)
base_level = dark_noise*(exptime)
base_img1 = base_level * np.ones_like(img.array)
else:
......@@ -25,8 +28,8 @@ def get_base_img(img, chip, read_noise, readout_time, dark_noise, exptime=150.,
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
base_img2[ny:, :] = arr[::-1, :]
base_img2[:, :] = base_img2[:, :]*(readout_time/ny)*dark_noise
return base_img1+base_img2
### test FUNCTION --- END ###
......@@ -35,16 +38,18 @@ def defineCCD(iccd, config_file):
with open(config_file, "r") as stream:
try:
config = yaml.safe_load(stream)
#for key, value in config.items():
# for key, value in config.items():
# print (key + " : " + str(value))
except yaml.YAMLError as exc:
print(exc)
chip = Chip(chipID=iccd, config=config)
chip.img = galsim.ImageF(chip.npix_x, chip.npix_y)
focal_plane = FocalPlane(chip_list=[iccd])
chip.img.wcs= focal_plane.getTanWCS(192.8595, 27.1283, -113.4333*galsim.degrees, chip.pix_scale)
chip.img.wcs = focal_plane.getTanWCS(
192.8595, 27.1283, -113.4333*galsim.degrees, chip.pix_scale)
return chip
def defineFilt(chip):
filter_param = FilterParam()
filter_id, filter_type = chip.getChipFilter()
......@@ -60,7 +65,8 @@ def defineFilt(chip):
class detModule_coverage(unittest.TestCase):
def __init__(self, methodName='runTest'):
super(detModule_coverage, self).__init__(methodName)
self.dataPath = os.path.join(os.getenv('UNIT_TEST_DATA_ROOT'), 'csst_msc_sim/csst_fz_msc')
self.dataPath = os.path.join(
os.getenv('UNIT_TEST_DATA_ROOT'), 'csst_msc_sim/csst_fz_msc')
self.iccd = 1
def test_add_dark(self):
......@@ -70,16 +76,19 @@ class detModule_coverage(unittest.TestCase):
print(chip.chipID)
print(chip.cen_pix_x, chip.cen_pix_y)
exptime=150.
base_img = get_base_img(img=chip.img, chip=chip, read_noise=chip.read_noise, readout_time=chip.readout_time, dark_noise=chip.dark_noise, exptime=exptime, InputDark=None)
exptime = 150.
base_img = get_base_img(img=chip.img, chip=chip, read_noise=chip.read_noise,
readout_time=chip.readout_time, dark_noise=chip.dark_noise, exptime=exptime, InputDark=None)
ny = int(chip.npix_y/2)
self.assertTrue( np.abs(np.max(base_img) - (exptime*chip.dark_noise+(ny-1)*(chip.readout_time/ny)*chip.dark_noise )) < 1e-6 )
self.assertTrue( np.min(base_img) == 3 )
self.assertTrue(np.abs(np.max(base_img) - (exptime*chip.dark_noise +
(ny-1)*(chip.readout_time/ny)*chip.dark_noise)) < 1e-6)
self.assertTrue(np.min(base_img) == 3)
base_img = get_base_img(img=chip.img, chip=chip, read_noise=chip.read_noise, readout_time=chip.readout_time, dark_noise=chip.dark_noise, exptime=150., InputDark="testTag")
self.assertTrue( np.abs(np.max(base_img) - ((ny-1)*(chip.readout_time/ny)*chip.dark_noise )) < 1e-6 )
base_img = get_base_img(img=chip.img, chip=chip, read_noise=chip.read_noise,
readout_time=chip.readout_time, dark_noise=chip.dark_noise, exptime=150., InputDark="testTag")
self.assertTrue(np.abs(np.max(base_img) - ((ny-1) *
(chip.readout_time/ny)*chip.dark_noise)) < 1e-6)
if __name__ == '__main__':
......
import unittest
import numpy as np
from ObservationSim.Instrument.Chip import Effects
from observation_sim.instruments.chip import effects
import galsim
import matplotlib.pyplot as plt
import os,sys,math,copy
import os
import sys
import math
import copy
from numpy.random import Generator, PCG64
import warnings
from astropy.io import fits
......@@ -13,20 +16,20 @@ warnings.filterwarnings("ignore", '.*Numba.*',)
width = 9216
height = 9232
class DetTest(unittest.TestCase):
def __init__(self, methodName='runTest'):
super(DetTest,self).__init__(methodName)
super(DetTest, self).__init__(methodName)
self.filePath('csst_msc_sim/test_sls_and_straylight')
def filePath(self, file_name):
self.datafn = os.path.join(os.getenv('UNIT_TEST_DATA_ROOT'), file_name)
self.outDataFn = os.path.join(self.datafn,'output')
self.outDataFn = os.path.join(self.datafn, 'output')
if os.path.isdir(self.outDataFn):
pass
else:
os.mkdir(self.outDataFn)
def test_prnu(self):
'''
......@@ -35,13 +38,14 @@ class DetTest(unittest.TestCase):
print('PRNU Test:')
sigma = 0.01
seed = 20210911
prnuimg = Effects.PRNU_Img(width, height, sigma=sigma, seed=seed)
prnuimg = effects.PRNU_Img(width, height, sigma=sigma, seed=seed)
meanval, stdval = np.mean(prnuimg.array), np.std(prnuimg.array)
print(' Mean & STDDEV of PRNU image are %6.4f & %6.4f.' % (meanval, stdval))
print(' Mean & STDDEV of PRNU image are %6.4f & %6.4f.' %
(meanval, stdval))
print(' PRNU Image Array:')
print(' ',prnuimg.array)
self.assertTrue(np.abs(meanval-1)<1e-6)
self.assertTrue(np.abs(stdval-sigma)<0.002)
print(' ', prnuimg.array)
self.assertTrue(np.abs(meanval-1) < 1e-6)
self.assertTrue(np.abs(stdval-sigma) < 0.002)
print('\nUnit test for PRNU has been passed.')
del prnuimg
......@@ -50,15 +54,17 @@ class DetTest(unittest.TestCase):
Test add dark current to image. Expected result: an image with dark current 3.4 e- and noise=1.844 e-.
'''
rng_poisson = galsim.BaseDeviate(20210911)
dark_noise = galsim.DeviateNoise(galsim.PoissonDeviate(rng_poisson, 0.02*(150+0.5*40)))
img = galsim.Image(200,200,dtype=np.float32, init_value=0)
print('Initial Mean & STD = %6.3f & %6.3f' % (np.mean(img.array), np.std(img.array)))
dark_noise = galsim.DeviateNoise(
galsim.PoissonDeviate(rng_poisson, 0.02*(150+0.5*40)))
img = galsim.Image(200, 200, dtype=np.float32, init_value=0)
print('Initial Mean & STD = %6.3f & %6.3f' %
(np.mean(img.array), np.std(img.array)))
img.addNoise(dark_noise)
meanval = np.mean(img.array)
stdval = np.std(img.array)
print('Dark added Mean & STD = %6.3f & %6.3f' % (meanval, stdval))
self.assertTrue(np.abs(meanval-3.4)<0.05)
self.assertTrue(np.abs(stdval-1.844)<0.02)
self.assertTrue(np.abs(meanval-3.4) < 0.05)
self.assertTrue(np.abs(stdval-1.844) < 0.02)
print('\nUnit test for dark current has been passed.')
del img
......@@ -66,149 +72,161 @@ class DetTest(unittest.TestCase):
'''
Test saturation and bleeding. Expected result: an image with bleeding effect.
'''
img = galsim.Image(500,500,dtype=np.float32)
star = galsim.Gaussian(flux=60e5,fwhm=3)
img = star.drawImage(image=img,center=(150,200))
img = galsim.Image(500, 500, dtype=np.float32)
star = galsim.Gaussian(flux=60e5, fwhm=3)
img = star.drawImage(image=img, center=(150, 200))
# gal = galsim.Sersic(n=1, half_light_radius=3,flux=50e5)
# img = gal.drawImage(image=img,center=(350,300))
img.addNoise(galsim.GaussianNoise(sigma=7))
# plt.imshow(img.array)
# plt.show()
filename1 = os.path.join(self.outDataFn,'test_satu_initimg.fits')
filename1 = os.path.join(self.outDataFn, 'test_satu_initimg.fits')
img.write(filename1)
newimg = Effects.SaturBloom(img, fullwell=9e4)
newimg = effects.SaturBloom(img, fullwell=9e4)
# plt.imshow(newimg.array)
# plt.show()
filename2 = os.path.join(self.outDataFn,'test_satu_bleedimg.fits')
filename2 = os.path.join(self.outDataFn, 'test_satu_bleedimg.fits')
newimg.write(filename2)
del img,newimg, star
del img, newimg, star
def test_nonlinear(self):
'''
Test non-linear effect. Expected result: an image with non-linearity effect.
'''
imgarr = np.arange(1,9e4,4).reshape((150,150))
imgarr = np.arange(1, 9e4, 4).reshape((150, 150))
img = galsim.Image(copy.deepcopy(imgarr))
filename1 = os.path.join(self.outDataFn,'test_nonlinear_initimg.fits')
filename1 = os.path.join(self.outDataFn, 'test_nonlinear_initimg.fits')
img.write(filename1)
newimg = Effects.NonLinearity(img, beta1=5E-7, beta2=0)
filename2 = os.path.join(self.outDataFn,'test_nonlinear_finalimg.fits')
newimg = effects.NonLinearity(img, beta1=5E-7, beta2=0)
filename2 = os.path.join(
self.outDataFn, 'test_nonlinear_finalimg.fits')
newimg.write(filename2)
plt.scatter(imgarr.flatten(), newimg.array.flatten(), s=2, alpha=0.5)
plt.plot([-1e3,9e4],[-1e3,9e4],color='black', lw=1, ls='--')
plt.plot([-1e3, 9e4], [-1e3, 9e4], color='black', lw=1, ls='--')
plt.xlabel('input (e-)')
plt.ylabel('output (e-)')
plt.savefig(os.path.join(self.outDataFn,'test_nonlinearity.png'), dpi=200)
plt.savefig(os.path.join(self.outDataFn,
'test_nonlinearity.png'), dpi=200)
plt.show()
del img,newimg,imgarr
del img, newimg, imgarr
def test_badpixel_HtrDtr(self):
img = galsim.Image(500,500,init_value=1000)
img = galsim.Image(500, 500, init_value=1000)
rgbadpix = Generator(PCG64(20210911))
badfraction = 5E-5*(rgbadpix.random()*0.5+0.7)
img = Effects.DefectivePixels(img, IfHotPix=True, IfDeadPix=True, fraction=badfraction, seed=20210911, biaslevel=0)
img.write(os.path.join(self.outDataFn,'test_badpixel_HtrDtr.fits'))
img = effects.DefectivePixels(
img, IfHotPix=True, IfDeadPix=True, fraction=badfraction, seed=20210911, biaslevel=0)
img.write(os.path.join(self.outDataFn, 'test_badpixel_HtrDtr.fits'))
del img
def test_badpixel_HfsDtr(self):
img = galsim.Image(500,500,init_value=1000)
img = galsim.Image(500, 500, init_value=1000)
rgbadpix = Generator(PCG64(20210911))
badfraction = 5E-5*(rgbadpix.random()*0.5+0.7)
img = Effects.DefectivePixels(img, IfHotPix=False, IfDeadPix=True, fraction=badfraction, seed=20210911, biaslevel=0)
img.write(os.path.join(self.outDataFn,'test_badpixel_HfsDtr.fits'))
img = effects.DefectivePixels(
img, IfHotPix=False, IfDeadPix=True, fraction=badfraction, seed=20210911, biaslevel=0)
img.write(os.path.join(self.outDataFn, 'test_badpixel_HfsDtr.fits'))
del img
def test_badpixel_HtrDfs(self):
img = galsim.Image(500,500,init_value=1000)
img = galsim.Image(500, 500, init_value=1000)
rgbadpix = Generator(PCG64(20210911))
badfraction = 5E-5*(rgbadpix.random()*0.5+0.7)
img = Effects.DefectivePixels(img, IfHotPix=True, IfDeadPix=False, fraction=badfraction, seed=20210911, biaslevel=0)
img.write(os.path.join(self.outDataFn,'test_badpixel_HtrDfs.fits'))
img = effects.DefectivePixels(
img, IfHotPix=True, IfDeadPix=False, fraction=badfraction, seed=20210911, biaslevel=0)
img.write(os.path.join(self.outDataFn, 'test_badpixel_HtrDfs.fits'))
del img
def test_badpixel_HfsDfs(self):
img = galsim.Image(500,500,init_value=1000)
img = galsim.Image(500, 500, init_value=1000)
rgbadpix = Generator(PCG64(20210911))
badfraction = 5E-5*(rgbadpix.random()*0.5+0.7)
img = Effects.DefectivePixels(img, IfHotPix=False, IfDeadPix=False, fraction=badfraction, seed=20210911, biaslevel=0)
img.write(os.path.join(self.outDataFn,'test_badpixel_HfsDfs.fits'))
img = effects.DefectivePixels(
img, IfHotPix=False, IfDeadPix=False, fraction=badfraction, seed=20210911, biaslevel=0)
img.write(os.path.join(self.outDataFn, 'test_badpixel_HfsDfs.fits'))
del img
def test_badlines(self):
img = galsim.Image(500,500,init_value=-1000)
img = galsim.Image(500, 500, init_value=-1000)
img.addNoise(galsim.GaussianNoise(sigma=7))
newimg = Effects.BadColumns(copy.deepcopy(img), seed=20210911)
newimg.write(os.path.join(self.outDataFn,'test_badlines.fits'))
del newimg,img
newimg = effects.BadColumns(copy.deepcopy(img), seed=20210911)
newimg.write(os.path.join(self.outDataFn, 'test_badlines.fits'))
del newimg, img
# def test_cte(self):
# img = galsim.Image(200,200,init_value=1000)
# img.array[50,80] = 1e4
# img.array[150,150] = 3e4
# newimgcol = Effects.CTE_Effect(copy.deepcopy(img),direction='column')
# newimgrow = Effects.CTE_Effect(copy.deepcopy(img),direction='row')
# newimgcol = effects.CTE_Effect(copy.deepcopy(img),direction='column')
# newimgrow = effects.CTE_Effect(copy.deepcopy(img),direction='row')
# newimgcol.write(os.path.join(self.outDataFn,'test_ctecol.fits'))
# newimgrow.write(os.path.join(self.outDataFn,'test_cterow.fits'))
# del img,newimgcol,newimgrow
def test_readnoise(self):
img = galsim.Image(200,200,init_value=1000)
img = galsim.Image(200, 200, init_value=1000)
seed = 20210911
rng_readout = galsim.BaseDeviate(seed)
readout_noise = galsim.GaussianNoise(rng=rng_readout, sigma=5)
img.addNoise(readout_noise)
img.write(os.path.join(self.outDataFn,'test_readnoise.fits'))
img.write(os.path.join(self.outDataFn, 'test_readnoise.fits'))
stdval = np.std(img.array)
self.assertTrue(np.abs(stdval-5)<0.01*5)
self.assertTrue(np.abs(stdval-5) < 0.01*5)
print('\nUnit test for readout noise has been passed.')
del img
def test_addbias(self):
img = galsim.Image(200,200,init_value=0)
img = Effects.AddBiasNonUniform16(img,bias_level=500, nsecy = 2, nsecx=8,seed=20210911)
img = galsim.Image(200, 200, init_value=0)
img = effects.AddBiasNonUniform16(
img, bias_level=500, nsecy=2, nsecx=8, seed=20210911)
img.write('./output/test_addbias.fits')
del img
def test_apply16gains(self):
img = galsim.Image(500,500,init_value=100)
img,_ = Effects.ApplyGainNonUniform16(img, gain=1.5, nsecy=2, nsecx=8, seed=202102)
img.write(os.path.join(self.outDataFn,'test_apply16gains.fits'))
img = galsim.Image(500, 500, init_value=100)
img, _ = effects.ApplyGainNonUniform16(
img, gain=1.5, nsecy=2, nsecx=8, seed=202102)
img.write(os.path.join(self.outDataFn, 'test_apply16gains.fits'))
rightedge = int(500/8)*8
print('gain=%6.2f' % 1.5)
meanimg = np.mean(img.array[:,:rightedge])
sigmaimg = np.std(img.array[:,:rightedge])
print('mean, sigma = %6.2f, %6.2f' % (meanimg,sigmaimg))
self.assertTrue(np.abs(meanimg-100/1.5)<1)
self.assertTrue(np.abs(sigmaimg/meanimg-0.01)<0.001)
meanimg = np.mean(img.array[:, :rightedge])
sigmaimg = np.std(img.array[:, :rightedge])
print('mean, sigma = %6.2f, %6.2f' % (meanimg, sigmaimg))
self.assertTrue(np.abs(meanimg-100/1.5) < 1)
self.assertTrue(np.abs(sigmaimg/meanimg-0.01) < 0.001)
print('\nUnit test for applying 16 channel gains has been passed.')
del img
def test_cosmicray(self):
attachedSizes = np.loadtxt(os.path.join(self.datafn,'wfc-cr-attachpixel.dat'))
cr_map,_ = Effects.produceCR_Map(
xLen=500, yLen=500, exTime=150+0.5*40,
cr_pixelRatio=0.003*(1+0.5*40/150),
gain=1, attachedSizes=attachedSizes, seed=20210911)
attachedSizes = np.loadtxt(os.path.join(
self.datafn, 'wfc-cr-attachpixel.dat'))
cr_map, _ = effects.produceCR_Map(
xLen=500, yLen=500, exTime=150+0.5*40,
cr_pixelRatio=0.003*(1+0.5*40/150),
gain=1, attachedSizes=attachedSizes, seed=20210911)
crimg = galsim.Image(cr_map)
crimg.write(os.path.join(self.outDataFn,'test_cosmicray.fits'))
del cr_map,crimg
crimg.write(os.path.join(self.outDataFn, 'test_cosmicray.fits'))
del cr_map, crimg
def test_shutter(self):
img = galsim.Image(5000,5000,init_value=1000)
shuttimg = Effects.ShutterEffectArr(img, t_exp=150, t_shutter=1.3, dist_bearing=735, dt=1E-3) # shutter effect normalized image for this chip
img = galsim.Image(5000, 5000, init_value=1000)
# shutter effect normalized image for this chip
shuttimg = effects.ShutterEffectArr(
img, t_exp=150, t_shutter=1.3, dist_bearing=735, dt=1E-3)
img *= shuttimg
img.write(os.path.join(self.outDataFn,'test_shutter.fits'))
img.write(os.path.join(self.outDataFn, 'test_shutter.fits'))
del img
def test_vignette(self):
img = galsim.Image(2000,2000,init_value=1000)
img = galsim.Image(2000, 2000, init_value=1000)
print(img.bounds)
# # img.bounds = galsim.BoundsI(1, width, 1, height)
img.setOrigin(10000,10000)
flat_img = Effects.MakeFlatSmooth(img.bounds,20210911)
img.setOrigin(10000, 10000)
flat_img = effects.MakeFlatSmooth(img.bounds, 20210911)
flat_normal = flat_img / np.mean(flat_img.array)
flat_normal.write(os.path.join(self.outDataFn,'test_vignette.fits'))
del flat_img,img,flat_normal
flat_normal.write(os.path.join(self.outDataFn, 'test_vignette.fits'))
del flat_img, img, flat_normal
if __name__ == '__main__':
unittest.main()
\ No newline at end of file
unittest.main()
import unittest
import os
import galsim
from ObservationSim.Instrument import FocalPlane, Chip
from observation_sim.instruments import FocalPlane, Chip
class TestFocalPlane(unittest.TestCase):
......
......@@ -15,12 +15,12 @@ import copy
from astropy.cosmology import FlatLambdaCDM
from astropy import constants
from astropy import units as U
from ObservationSim.MockObject._util import getObservedSED
from ObservationSim.MockObject import CatalogBase, Galaxy
from observation_sim.mock_objects._util import getObservedSED
from observation_sim.mock_objects import CatalogBase, Galaxy
from ObservationSim.Instrument import Chip, Filter, FilterParam, FocalPlane
from ObservationSim.PSF.PSFInterp import PSFInterp
from ObservationSim.MockObject._util import integrate_sed_bandpass, getNormFactorForSpecWithABMAG, getABMAG
from observation_sim.instruments import Chip, Filter, FilterParam, FocalPlane
from observation_sim.PSF.PSFInterp import PSFInterp
from observation_sim.mock_objects._util import integrate_sed_bandpass, getNormFactorForSpecWithABMAG, getABMAG
class Catalog(CatalogBase):
......
......@@ -6,8 +6,7 @@ import numpy as np
import galsim
import yaml
from ObservationSim.Instrument import Chip, Filter, FilterParam, FocalPlane
#from ObservationSim.Instrument.Chip import ChipUtils as chip_utils
from observation_sim.instruments import Chip, Filter, FilterParam, FocalPlane
### test FUNCTION --- START ###
def AddPreScan(GSImage, pre1=27, pre2=4, over1=71, over2=80, nsecy = 2, nsecx=8):
......
......@@ -14,17 +14,19 @@ chip_filename = 'chip_definition.json'
# "npix_y": 7680,
# "x_cen": -273.35, # [mm]
# "y_cen": 211.36, # [mm]
# "rotate_angle": 90. # [deg]
# "rotate_angle": 90. # [deg]
# }
# chip_list[chip_id] = chip_dict
def get_chip_row_col_main_fp(chip_id):
rowID = ((chip_id - 1) % 5) + 1
colID = 6 - ((chip_id - 1) // 5)
return rowID, colID
def get_chip_center_main_fp(chip_id, pixel_size=1e-2):
row, col = get_chip_row_col_main_fp(chip_id)
npix_x = 9216
npix_y = 9232
......@@ -39,16 +41,20 @@ def get_chip_center_main_fp(chip_id, pixel_size=1e-2):
xcen = (npix_x//2 + gx1//2) * xrem - (gx2-gx1)
if chip_id <= 5 or chip_id == 10:
xcen = (npix_x//2 + gx1//2) * xrem + (gx2-gx1)
# ylim of a given CCD chip
yrem = (row - 1) - nchip_y // 2
ycen = (npix_y + gy) * yrem
return xcen * pixel_size, ycen * pixel_size
def create_chip_dict_main_fp(chip_id, pixel_size=1e-2):
filter_list = ["GV", "GI", "y", "z", "y", "GI", "GU", "r", "u", "NUV", "i", "GV", "GU", "g", "NUV", "NUV", "g", "GU", "GV", "i", "NUV", "u", "r", "GU", "GI", "y", "z", "y", "GI", "GV"]
chip_label_list = [3,3,3,1,1,1,3,2,2,1,1,1,4,2,3,2,1,1,4,2,4,1,1,2,4,2,2,4,2,2]
chip_id_list = [26, 21, 16, 11, 6, 1, 27, 22, 17, 12, 7, 2, 28, 23, 18, 13, 8, 3, 29, 24, 19, 14, 9, 4, 30, 25, 20, 15, 10, 5]
filter_list = ["GV", "GI", "y", "z", "y", "GI", "GU", "r", "u", "NUV", "i", "GV", "GU", "g",
"NUV", "NUV", "g", "GU", "GV", "i", "NUV", "u", "r", "GU", "GI", "y", "z", "y", "GI", "GV"]
chip_label_list = [3, 3, 3, 1, 1, 1, 3, 2, 2, 1, 1, 1,
4, 2, 3, 2, 1, 1, 4, 2, 4, 1, 1, 2, 4, 2, 2, 4, 2, 2]
chip_id_list = [26, 21, 16, 11, 6, 1, 27, 22, 17, 12, 7, 2, 28,
23, 18, 13, 8, 3, 29, 24, 19, 14, 9, 4, 30, 25, 20, 15, 10, 5]
npix_x = 9216
npix_y = 9232
idx = chip_id_list.index(chip_id)
......@@ -63,10 +69,10 @@ def create_chip_dict_main_fp(chip_id, pixel_size=1e-2):
chip_dict = {
"chip_name": chip_name,
"pix_size": 1e-2, # [mm]
"pix_scale": 0.074, # [arcsec/pix]
"pix_scale": 0.074, # [arcsec/pix]
"npix_x": npix_x,
"npix_y": npix_y,
"x_cen": xcen, # [mm]
"x_cen": xcen, # [mm]
"y_cen": ycen, # [mm]
"rotate_angle": rotate_angle, # [deg]
"n_psf_samples": 900,
......@@ -80,6 +86,7 @@ def create_chip_dict_main_fp(chip_id, pixel_size=1e-2):
}
return chip_dict
def set_fgs_chips(filepath):
with open(filepath, "r") as f:
data = json.load(f)
......@@ -94,7 +101,7 @@ def set_fgs_chips(filepath):
data[chip_id]["full_well"] = 90000
with open(filepath, "w") as f:
json.dump(data, f, indent=4)
def add_main_fp(filepath):
for i in range(30):
......@@ -102,6 +109,7 @@ def add_main_fp(filepath):
chip_dict = create_chip_dict_main_fp(chip_id)
add_dict_to_json(filepath, str(chip_id), chip_dict)
def add_dict_to_json(filepath, key, value):
with open(filepath, 'r') as f:
data = json.load(f)
......@@ -109,8 +117,9 @@ def add_dict_to_json(filepath, key, value):
with open(filepath, "w") as f:
json.dump(data, f, indent=4)
if __name__=="__main__":
src = "../ObservationSim/Instrument/data/ccd/chip_definition.json"
if __name__ == "__main__":
src = "../observation_sim/instruments/data/ccd/chip_definition.json"
shutil.copy(src, chip_filename)
add_main_fp(chip_filename)
set_fgs_chips(chip_filename)
\ No newline at end of file
set_fgs_chips(chip_filename)
import os
import numpy as np
import observation_sim.PSF.PSFInterp as PSFInterp
from observation_sim.instruments import Chip, Filter, FilterParam
import yaml
import galsim
import astropy.io.fits as fitsio
# Setup PATH
SIMPATH = "/share/simudata/CSSOSDataProductsSims/data/CSSTSimImage_C8/testRun_FGS"
config_filename = SIMPATH+"/config_C6_fits.yaml"
cat_filename = SIMPATH+"/MSC_00000000/MSC_10106100000000_chip_40_filt_FGS.cat"
# Read cat file
catFn = open(cat_filename, "r")
line = catFn.readline()
print(cat_filename, '\n', line)
imgPos = []
chipID = -1
for line in catFn:
line = line.strip()
columns = line.split()
if chipID == -1:
chipID = int(columns[1])
else:
assert chipID == int(columns[1])
ximg = float(columns[3])
yimg = float(columns[4])
imgPos.append([ximg, yimg])
imgPos = np.array(imgPos)
nobj = imgPos.shape[0]
print('chipID, nobj::', chipID, nobj)
# Read config file
with open(config_filename, "r") as stream:
try:
config = yaml.safe_load(stream)
for key, value in config.items():
print(key + " : " + str(value))
except yaml.YAMLError as exc:
print(exc)
# Setup Chip
chip = Chip(chipID=chipID, config=config)
print('chip.bound::', chip.bound.xmin, chip.bound.xmax,
chip.bound.ymin, chip.bound.ymax)
for iobj in range(nobj):
print("\nget psf for iobj-", iobj, '\t', 'bandpass:', end=" ", flush=True)
# Setup Position on focalplane
# try get the PSF at some location (1234, 1234) on the chip
x, y = imgPos[iobj, :]
x = x+chip.bound.xmin
y = y+chip.bound.ymin
pos_img = galsim.PositionD(x, y)
# Setup sub-bandpass
# (There are 4 sub-bandpasses for each PSF sample)
filter_param = FilterParam()
filter_id, filter_type = chip.getChipFilter()
filt = Filter(
filter_id=filter_id,
filter_type=filter_type,
filter_param=filter_param,
ccd_bandpass=chip.effCurve)
bandpass_list = filt.bandpass_sub_list
for i in range(len(bandpass_list)):
print(i, end=" ", flush=True)
# say you want to access the PSF for the sub-bandpass at the blue end for that chip
bandpass = bandpass_list[i]
# Get corresponding PSF model
psf_model = PSFInterp(chip=chip, npsf=100,
PSF_data_file=config["psf_setting"]["psf_dir"])
psf = psf_model.get_PSF(
chip=chip, pos_img=pos_img, bandpass=bandpass, galsimGSObject=False)
if True:
fn = "psf_{:}.{:}.{:}.fits".format(chipID, iobj, i)
if fn != None:
if os.path.exists(fn):
os.remove(fn)
hdu = fitsio.PrimaryHDU()
hdu.data = psf
hdu.header.set('pixScale', 5)
hdu.writeto(fn)
# NAME:
# indexFits_hdf5
# PURPOSE:
# Return a healpix indexed catalog from a set of Fits
# CALLING:
# write_StampsIndex(dir_cat=dir_temp)
# INPUTS:
# dir_cat - the directory of Fits catalog, "<dir_cat>/stampCats/*.fits"
# OUTPUTS:
# <dir_cat>/stampCatsIndex.hdf5
# OPTIONAL:
# test_fits(nfits=100, dir_cat=None) - generate a set of Fits by galsim gaussian
# HISTORY:
# Written by Chengliang Wei, 13 Apr. 2023
# Included by csst-simulation, C.W. 25 Apr. 2023
#
#
import os
import numpy as np
import astropy.io.fits as fitsio
import h5py
import healpy
import galsim
def test_fits(nfits=100, dir_cat=None):
for ifits in range(nfits):
gal = galsim.Gaussian(sigma=np.random.uniform(0.2, 0.3)).shear(
g1=np.random.uniform(-0.5, 0.5), g2=np.random.uniform(-0.5, 0.5))
arr = gal.drawImage(nx=64, ny=64, scale=0.074).array
hdu = fitsio.PrimaryHDU()
hdu.data = arr
hdu.header.set('index', ifits)
hdu.header.set('ra', 60.+np.random.uniform(-0.2, 0.2))
hdu.header.set('dec', -40.+np.random.uniform(-0.2, 0.2))
hdu.header.set('mag_g', 22+np.random.uniform(-1, 1))
hdu.header.set('pixScale', 0.074)
fout = dir_cat+"stampCats/testStamp_{:}.fits".format(ifits)
if os.path.exists(fout):
os.remove(fout)
hdu.writeto(fout)
def write_StampsIndex(dir_cat=None, DEBUG=False):
MAXNUMBERINDEX = 10000
NSIDE = 128
fp = h5py.File(dir_cat+'stampCatsIndex.hdf5', 'w')
grp1 = fp.create_group('Stamps')
dataSet_Size = np.zeros(healpy.nside2npix(NSIDE), dtype=np.int64)
fitsList = os.listdir(dir_cat+'stampCats/') # 获取fits文件列表
for istamp in range(len(fitsList)):
print(istamp, ': ', fitsList[istamp], end='\r')
hdu = fitsio.open(dir_cat+"stampCats/"+fitsList[istamp])
tra = hdu[0].header['RA']
tdec = hdu[0].header['DEC']
healpixID = healpy.ang2pix(NSIDE, tra, tdec, nest=False, lonlat=True)
if not (str(healpixID) in grp1):
grp2 = grp1.create_group(str(healpixID))
else:
grp2 = grp1[str(healpixID)]
if not ('ra' in grp2):
dset_ra = grp2.create_dataset(
'ra', (0,), dtype='f16', maxshape=(MAXNUMBERINDEX, ))
dset_dec = grp2.create_dataset(
'dec', (0,), dtype='f16', maxshape=(MAXNUMBERINDEX, ))
dt = h5py.special_dtype(vlen=str)
dset_fn = grp2.create_dataset(
'filename', (0,), dtype=dt, maxshape=(MAXNUMBERINDEX, ))
else:
dset_ra = grp2['ra']
dset_dec = grp2['dec']
dset_fn = grp2['filename']
dataSet_Size[healpixID] = dataSet_Size[healpixID]+1
grp2['ra'].resize((dataSet_Size[healpixID],))
grp2['dec'].resize((dataSet_Size[healpixID],))
grp2['filename'].resize((dataSet_Size[healpixID],))
dset_ra[dataSet_Size[healpixID]-1] = tra
dset_dec[dataSet_Size[healpixID]-1] = tdec
dset_fn[dataSet_Size[healpixID]-1] = fitsList[istamp]
fp.close()
if DEBUG:
print('\n')
ff = h5py.File(dir_cat+"stampCatsIndex.hdf5", "r")
ss = 0
for kk in ff['Stamps'].keys():
print(kk, ff['Stamps'][kk]['ra'].size)
ss = ss+ff['Stamps'][kk]['ra'].size
print(ss)
if __name__ == '__main__':
dir_temp = "./Catalog_test/"
# test_fits(dir_cat=dir_temp)
write_StampsIndex(dir_cat=dir_temp)
# NOTE: This is a stand-alone function, meaning that you do not need
# to install the entire CSST image simulation pipeline.
# For a given object's coordinate (Ra, Dec), the function will predict
# the object's image position and corresponding filter in the focal plane
# under a specified CSST pointing centered at (rap, decp).
import galsim
import numpy as np
import argparse
import matplotlib.pyplot as plt
import os
import sys
def focalPlaneInf(ra_target, dec_target, ra_point, dec_point, image_rot=-113.4333, figout="zTargetOnCCD.pdf"):
"""
Input parameters:
ra_target : right ascension of the target/input object;
float, in unit of degrees;
dec_target: declination of the target/input object;
float, in unit of degrees;
ra_point : right ascension of telescope pointing center;
float, in unit of degrees;
dec_point : declination of telescope pointing center;
float, in unit of degrees;
image_rot : orientation of the camera with respect the sky;
float, in unit of degrees;
NOTE: image_rot=-113.4333 is the default value
in current CSST image simulation;
figout : location of the target object in the focal plane;
str
--------------------------------------------------------------
Usage:
0) specify the coordinate (ra_target, dec_target) of your target and
the pointing center (ra_point dec_point) of the telescope
1) open a terminal
2) type >> python TargetLocationCheck.py ra_target dec_target ra_point dec_point
or type >> python TargetLocationCheck.py ra_target dec_target ra_point dec_point -image_rot=floatNum
or type >> python TargetLocationCheck.py ra_target dec_target ra_point dec_point -image_rot=floatNum -figout=FigureName
"""
print("^_^ Input target coordinate: [Ra, Dec] = [%10.6f, %10.6f]" % (
ra_target, dec_target))
print("^_^ Input telescope pointing center: [Ra, Dec] = [%10.6f, %10.6f]" % (
ra_point, dec_point))
print("^_^ Input camera orientation: %12.6f degree(s)" % image_rot)
print(" ")
# load ccd parameters
xsize, ysize, xchip, ychip, xgap, ygap, xnchip, ynchip = ccdParam()
print("^_^ Pixel range of focal plane: x = [%5d, %5d], y = [%5d, %5d]" % (
-xsize/2, xsize/2, -ysize/2, ysize/2))
# wcs
wcs = getTanWCS(ra_point, dec_point, image_rot, pix_scale=0.074)
skyObj = galsim.CelestialCoord(
ra=ra_target*galsim.degrees, dec=dec_target*galsim.degrees)
pixObj = wcs.toImage(skyObj)
xpixObj = pixObj.x
ypixObj = pixObj.y
print("^_^ Image position of target: [xImage, yImage] = [%9.3f, %9.3f]" % (
xpixObj, ypixObj))
# first determine if the target is in the focal plane
xin = (xpixObj+xsize/2)*(xpixObj-xsize/2)
yin = (ypixObj+ysize/2)*(ypixObj-ysize/2)
if xin > 0 or yin > 0:
raise ValueError("!!! Input target is out of the focal plane")
# second determine the location of the target
trigger = False
for i in range(30):
ichip = i+1
ischip = str("0%d" % ichip)[-2:]
fId, fType = getChipFilter(ichip)
ix0, ix1, iy0, iy1 = getChipLim(ichip)
ixin = (xpixObj-ix0)*(xpixObj-ix1)
iyin = (ypixObj-iy0)*(ypixObj-iy1)
if ixin <= 0 and iyin <= 0:
trigger = True
idx = xpixObj - ix0
idy = ypixObj - iy0
print(" ---------------------------------------------")
print(" ** Target locates in CHIP#%s with filter %s **" %
(ischip, fType))
print(
" ** Target position in the chip: [x, y] = [%7.2f, %7.2f]" % (idx, idy))
print(" ---------------------------------------------")
break
if not trigger:
print("^|^ Target locates in CCD gap")
# show the figure
print(" Target on CCD layout is saved into %s" % figout)
ccdLayout(xpixObj, ypixObj, figout=figout)
return
def ccdParam():
xt, yt = 59516, 49752
x0, y0 = 9216, 9232
xgap, ygap = (534, 1309), 898
xnchip, ynchip = 6, 5
ccdSize = xt, yt, x0, y0, xgap, ygap, xnchip, ynchip
return ccdSize
def getTanWCS(ra, dec, img_rot, pix_scale=0.074):
"""
Get the WCS of the image mosaic using Gnomonic/TAN projection
Parameter:
ra, dec: float
(RA, Dec) of pointing of optical axis
img_rot: galsim Angle object
Rotation of image
pix_scale: float
Pixel size in unit of as/pix
Returns:
WCS of the focal plane
"""
xcen, ycen = 0, 0
img_rot = img_rot * galsim.degrees
dudx = -np.cos(img_rot.rad) * pix_scale
dudy = -np.sin(img_rot.rad) * pix_scale
dvdx = -np.sin(img_rot.rad) * pix_scale
dvdy = +np.cos(img_rot.rad) * pix_scale
moscen = galsim.PositionD(x=xcen, y=ycen)
sky_center = galsim.CelestialCoord(
ra=ra*galsim.degrees, dec=dec*galsim.degrees)
affine = galsim.AffineTransform(dudx, dudy, dvdx, dvdy, origin=moscen)
WCS = galsim.TanWCS(affine, sky_center, units=galsim.arcsec)
return WCS
def getChipFilter(chipID):
"""
Return the filter index and type for a given chip #(chipID)
"""
filter_type_list = ["nuv", "u", "g", "r", "i", "z", "y", "GU", "GV", "GI"]
# TODO: maybe a more elegent way other than hard coded?
# e.g. use something like a nested dict:
if chipID in [6, 15, 16, 25]:
filter_type = "y"
if chipID in [11, 20]:
filter_type = "z"
if chipID in [7, 24]:
filter_type = "i"
if chipID in [14, 17]:
filter_type = "u"
if chipID in [9, 22]:
filter_type = "r"
if chipID in [12, 13, 18, 19]:
filter_type = "nuv"
if chipID in [8, 23]:
filter_type = "g"
if chipID in [1, 10, 21, 30]:
filter_type = "GI"
if chipID in [2, 5, 26, 29]:
filter_type = "GV"
if chipID in [3, 4, 27, 28]:
filter_type = "GU"
filter_id = filter_type_list.index(filter_type)
return filter_id, filter_type
def getChipLim(chipID):
"""
Calculate the edges in pixel for a given CCD chip on the focal plane
NOTE: There are 5*4 CCD chips in the focus plane for photometric observation.
Parameters:
chipID: int
the index of the chip
Returns:
A galsim BoundsD object
"""
xt, yt, x0, y0, gx, gy, xnchip, ynchip = ccdParam()
gx1, gx2 = gx
rowID = ((chipID - 1) % 5) + 1
colID = 6 - ((chipID - 1) // 5)
# xlim of a given CCD chip
xrem = 2*(colID - 1) - (xnchip - 1)
xcen = (x0//2 + gx1//2) * xrem
if chipID >= 26 or chipID == 21:
xcen = (x0//2 + gx1//2) * xrem - (gx2-gx1)
if chipID <= 5 or chipID == 10:
xcen = (x0//2 + gx1//2) * xrem + (gx2-gx1)
nx0 = xcen - x0//2 + 1
nx1 = xcen + x0//2
# ylim of a given CCD chip
yrem = (rowID - 1) - ynchip // 2
ycen = (y0 + gy) * yrem
ny0 = ycen - y0//2 + 1
ny1 = ycen + y0//2
return nx0-1, nx1-1, ny0-1, ny1-1
def ccdLayout(xpixTar, ypixTar, figout="ccdLayout.pdf"):
fig = plt.figure(figsize=(10.0, 8.0))
ax = fig.add_axes([0.1, 0.1, 0.80, 0.80])
# plot the layout of the ccd distribution
for i in range(30):
ichip = i+1
fId, fType = getChipFilter(ichip)
ischip = str("0%d" % ichip)[-2:]
ix0, ix1, iy0, iy1 = getChipLim(ichip)
ax.plot([ix0, ix1], [iy0, iy0], "k-", linewidth=2.5)
ax.plot([ix0, ix1], [iy1, iy1], "k-", linewidth=2.5)
ax.plot([ix0, ix0], [iy0, iy1], "k-", linewidth=2.5)
ax.plot([ix1, ix1], [iy0, iy1], "k-", linewidth=2.5)
ax.text(ix0+500, iy0+1500, "%s#%s" %
(fType, ischip), fontsize=12, color="grey")
ax.plot(xpixTar, ypixTar, "r*", ms=12)
ax.set_xlabel("$X\,[\mathrm{pixels}]$", fontsize=20)
ax.set_ylabel("$Y\,[\mathrm{pixels}]$", fontsize=20)
ax.invert_yaxis()
ax.axis('off')
plt.savefig(figout)
def parseArguments():
# Create argument parser
parser = argparse.ArgumentParser()
# Positional arguments
parser.add_argument("ra_target", type=float)
parser.add_argument("dec_target", type=float)
parser.add_argument("ra_point", type=float)
parser.add_argument("dec_point", type=float)
# Optional arguments
parser.add_argument("-image_rot", type=float, default=-113.4333)
parser.add_argument("-figout", type=str, default="zTargetOnCCD.pdf")
# Parse arguments
args = parser.parse_args()
return args
if __name__ == "__main__":
# Parse the arguments
args = parseArguments()
# Run function
focalPlaneInf(args.ra_target, args.dec_target, args.ra_point,
args.dec_point, args.image_rot, args.figout)
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