Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Wu Jin
CSST simulation
Commits
883b7a77
Commit
883b7a77
authored
Nov 29, 2021
by
Fang Yuedong
Browse files
bug fixed
parent
e82a5dcc
Changes
392
Show whitespace changes
Inline
Side-by-side
ObservationSim/Config/Config.py
View file @
883b7a77
...
...
@@ -23,6 +23,7 @@ def ConfigDir(config, work_dir=None, data_dir=None):
path_dict
[
"cat_dir"
]
=
os
.
path
.
join
(
path_dict
[
"data_dir"
],
config
[
"input_path"
][
"cat_dir"
])
# PSF data directory
path_dict
[
"psf_dir"
]
=
os
.
path
.
join
(
path_dict
[
"data_dir"
],
config
[
"psf_setting"
][
"psf_dir"
])
path_dict
[
"fd_path"
]
=
os
.
path
.
join
(
path_dict
[
"data_dir"
],
config
[
"psf_setting"
][
"fd_path"
])
# SED catalog directory
# TODO: SED_dir is deprecated
path_dict
[
"SED_dir"
]
=
os
.
path
.
join
(
path_dict
[
"data_dir"
],
"imageSims/Catalog/SEDObject"
)
...
...
ObservationSim/Instrument/Chip/Chip.py
View file @
883b7a77
...
...
@@ -65,7 +65,7 @@ class Chip(FocalPlane):
self
.
_getCRdata
()
# Define the sensor
if
config
[
"ins_effects"
][
"bright_fatter"
]
==
True
:
if
config
[
"ins_effects"
][
"bright_fatter"
]
==
True
and
self
.
survey_type
==
"photometric"
:
self
.
sensor
=
galsim
.
SiliconSensor
(
strength
=
config
[
"ins_effects"
][
"df_strength"
],
treering_func
=
treering_func
)
else
:
self
.
sensor
=
galsim
.
Sensor
()
...
...
ObservationSim/ObservationSim.py
View file @
883b7a77
...
...
@@ -26,7 +26,7 @@ class Observation(object):
# if we want to apply field distortion?
if
self
.
config
[
"ins_effects"
][
"field_dist"
]
==
True
:
self
.
fd_model
=
FieldDistortion
()
self
.
fd_model
=
FieldDistortion
(
fdModel_path
=
self
.
path_dict
[
"fd_path"
]
)
else
:
self
.
fd_model
=
None
...
...
@@ -59,7 +59,7 @@ class Observation(object):
if
self
.
config
[
"psf_setting"
][
"psf_model"
]
==
"Gauss"
:
psf_model
=
PSFGauss
(
chip
=
chip
)
elif
self
.
config
[
"psf_setting"
][
"psf_model"
]
==
"Interp"
:
psf_model
=
PSFInterp
(
chip
=
chip
)
psf_model
=
PSFInterp
(
chip
=
chip
,
PSF_data_file
=
self
.
path_dict
[
"psf_dir"
]
)
else
:
print
(
"unrecognized PSF model type!!"
,
flush
=
True
)
...
...
@@ -78,7 +78,7 @@ class Observation(object):
if
chip
.
survey_type
==
"photometric"
:
sky_map
=
None
elif
chip
.
survey_type
==
"spectroscopic"
:
sky_map
=
calculateSkyMap_split_g
(
xLen
=
chip
.
npix_x
,
yLen
=
chip
.
npix_y
,
blueLimit
=
filt
.
blue_limit
,
redLimit
=
filt
.
red_limit
,
skyfn
=
path_dict
[
"sky_file"
],
conf
=
chip
.
sls_conf
,
pixelSize
=
chip
.
pix_scale
,
isAlongY
=
0
)
sky_map
=
calculateSkyMap_split_g
(
xLen
=
chip
.
npix_x
,
yLen
=
chip
.
npix_y
,
blueLimit
=
filt
.
blue_limit
,
redLimit
=
filt
.
red_limit
,
skyfn
=
self
.
path_dict
[
"sky_file"
],
conf
=
chip
.
sls_conf
,
pixelSize
=
chip
.
pix_scale
,
isAlongY
=
0
)
if
pointing_type
==
'MS'
:
# Load catalogues and templates
...
...
ObservationSim/PSF/PSFInterp_deprecated/PSFInterp.py
deleted
100644 → 0
View file @
e82a5dcc
import
galsim
import
numpy
as
np
import
os
import
time
import
copy
# import psfConfig as mypy
import
PSF.PSFInterp.psfConfig
as
mypy
npsf
=
900
#***# 30*30
LOG_DEBUG
=
False
#***#
class
PSFInterp
(
object
):
# def __init__(self, PSF_data=None, params=None, PSF_data_file=None):
def
__init__
(
self
,
chip
,
PSF_data
=
None
,
PSF_data_file
=
None
,
sigSpin
=
0.
,
psfRa
=
0.15
):
"""
The PSF data matrix is either given by a object parameter or read in from a file.
Parameters:
PSF_data: The PSF data matrix object
params: Other parameters?
PSF_data_file: The file for PSF data matrix (optional).
"""
if
LOG_DEBUG
:
print
(
'==================================================='
)
print
(
'DEBUG: psf module for csstSim '
\
+
time
.
strftime
(
"(%Y-%m-%d %H:%M:%S)"
,
time
.
localtime
()),
flush
=
True
)
print
(
'==================================================='
)
self
.
sigSpin
=
sigSpin
self
.
sigGauss
=
psfRa
# 80% light radius
# iccd = 1 #***#
# iccd = chip.chipID
iccd
=
int
(
chip
.
getChipLabel
(
chipID
=
chip
.
chipID
))
if
PSF_data_file
==
None
:
PSF_data_file
=
'/data/simudata/CSSOSDataProductsSims/data/csstPSFdata/CSSOS_psf_ciomp_30X30'
self
.
nwave
=
self
.
_getPSFwave
(
iccd
,
PSF_data_file
)
if
LOG_DEBUG
:
print
(
'nwave-{:} on ccd-{:}::'
.
format
(
self
.
nwave
,
iccd
),
flush
=
True
)
self
.
PSF_data
=
self
.
_loadPSF
(
iccd
,
PSF_data_file
)
self
.
itpPSF_data
=
self
.
_preprocessPSF
()
if
LOG_DEBUG
:
print
(
'self.PSF_data & self.itpPSF_data ... ok'
,
flush
=
True
)
if
LOG_DEBUG
:
print
(
'Preparing self.[psfMat,cen_col,cen_row] for psfMaker ... '
,
end
=
''
,
flush
=
True
)
ngy
,
ngx
=
self
.
itpPSF_data
[
0
][
0
][
'psfMat'
].
shape
self
.
psfMat
=
np
.
zeros
([
self
.
nwave
,
npsf
,
ngy
,
ngx
])
self
.
cen_col
=
np
.
zeros
([
self
.
nwave
,
npsf
])
self
.
cen_row
=
np
.
zeros
([
self
.
nwave
,
npsf
])
for
iwave
in
range
(
self
.
nwave
):
for
ipsf
in
range
(
npsf
):
self
.
psfMat
[
iwave
,
ipsf
,
:,
:]
=
self
.
itpPSF_data
[
iwave
][
ipsf
][
'psfMat'
]
self
.
cen_col
[
iwave
,
ipsf
]
=
self
.
itpPSF_data
[
iwave
][
ipsf
][
'imgMaxPosx_ccd'
]
self
.
cen_row
[
iwave
,
ipsf
]
=
self
.
itpPSF_data
[
iwave
][
ipsf
][
'imgMaxPosy_ccd'
]
if
LOG_DEBUG
:
print
(
'ok'
,
flush
=
True
)
def
_getPSFwave
(
self
,
iccd
,
PSF_data_file
):
"""
Get # of sampling waves on iccd
Parameters:
iccd: The chip of i-th ccd
PSF_data_file: The file for PSF data matrix
Returns:
nwave: The number of the sampling waves
"""
strs
=
os
.
listdir
(
PSF_data_file
+
'/ccd{:}'
.
format
(
iccd
))
nwave
=
0
for
_
in
strs
:
if
'wave_'
in
_
:
nwave
+=
1
return
nwave
def
_loadPSF
(
self
,
iccd
,
PSF_data_file
):
"""
load psf-matrix on iccd
Parameters:
iccd: The chip of i-th ccd
PSF_data_file: The file for PSF data matrix
Returns:
psfSet: The matrix of the csst-psf
"""
psfSet
=
[]
for
ii
in
range
(
self
.
nwave
):
iwave
=
ii
+
1
if
LOG_DEBUG
:
print
(
'self._loadPSF: iwave::'
,
iwave
,
flush
=
True
)
psfWave
=
[]
for
jj
in
range
(
npsf
):
ipsf
=
jj
+
1
psfInfo
=
mypy
.
LoadPSF
(
iccd
,
iwave
,
ipsf
,
PSF_data_file
,
CalcPSFsize
=
False
,
InputMaxPixelPos
=
True
)
psfWave
.
append
(
psfInfo
)
psfSet
.
append
(
psfWave
)
if
LOG_DEBUG
:
print
(
'psfSet has been loaded:'
,
flush
=
True
)
print
(
'psfSet[iwave][ipsf][keys]:'
,
psfSet
[
0
][
0
].
keys
(),
flush
=
True
)
return
psfSet
def
_preprocessPSF
(
self
):
"""
Preprocessing psf-matrix
Parameters:
Returns:
itpPSF_data: The matrix of the preprocessed csst-psf
"""
itpPSF_data
=
copy
.
deepcopy
(
self
.
PSF_data
)
for
iwave
in
range
(
self
.
nwave
):
for
ipsf
in
range
(
npsf
):
psfMat
=
self
.
PSF_data
[
iwave
][
ipsf
][
'psfMat'
]
psfMatX
=
mypy
.
psfCentering
(
psfMat
,
CenteringMode
=
1
)
itpPSF_data
[
iwave
][
ipsf
][
'psfMat'
]
=
psfMatX
return
itpPSF_data
def
_findWave
(
self
,
bandpass
):
for
iwave
in
range
(
self
.
nwave
):
bandwave
=
self
.
PSF_data
[
iwave
][
0
][
'wavelength'
]
if
bandpass
.
blue_limit
<
bandwave
and
bandwave
<
bandpass
.
red_limit
:
return
iwave
return
-
1
def
get_PSF
(
self
,
chip
,
pos_img
,
bandpass
,
pixSize
=
0.037
,
galsimGSObject
=
True
):
"""
Get the PSF at a given image position
Parameters:
chip: A 'Chip' object representing the chip we want to extract PSF from.
pos_img: A 'galsim.Position' object representing the image position.
bandpass: A 'galsim.Bandpass' object representing the wavelength range.
pixSize: The pixels size of psf matrix
Returns:
PSF: A 'galsim.GSObject'.
"""
# iccd = 1 #***# #chip.chipID
# iccd = chip.chipID
iccd
=
int
(
chip
.
getChipLabel
(
chipID
=
chip
.
chipID
))
# iwave = 1 #***# #self.findWave(bandpass)
iwave
=
self
.
_findWave
(
bandpass
)
if
iwave
==
-
1
:
print
(
"!!!PSF bandpass does not match."
)
exit
()
PSFMat
=
self
.
psfMat
[
iwave
]
cen_col
=
self
.
cen_col
[
iwave
]
cen_row
=
self
.
cen_row
[
iwave
]
# print('shape:', cen_col.shape)
# px = pos_img[0]
# py = pos_img[1]
px
=
pos_img
.
x
py
=
pos_img
.
y
imPSF
=
mypy
.
psfMaker_IDW
(
px
,
py
,
PSFMat
,
cen_col
,
cen_row
,
IDWindex
=
2
,
OnlyNeighbors
=
True
)
if
galsimGSObject
:
img
=
galsim
.
ImageF
(
imPSF
,
scale
=
pixSize
)
self
.
psf
=
galsim
.
InterpolatedImage
(
img
)
dx
=
px
-
chip
.
cen_pix_x
dy
=
py
-
chip
.
cen_pix_y
return
self
.
PSFspin
(
x
=
dx
,
y
=
dy
)
return
imPSF
def
PSFspin
(
self
,
x
,
y
):
"""
The PSF profile at a given image position relative to the axis center
Parameters:
theta : spin angles in a given exposure in unit of [arcsecond]
dx, dy: relative position to the axis center in unit of [pixels]
Return:
Spinned PSF: g1, g2 and axis ratio 'a/b'
"""
a2Rad
=
np
.
pi
/
(
60.0
*
60.0
*
180.0
)
ff
=
self
.
sigGauss
*
0.107
*
(
1000.0
/
10.0
)
# in unit of [pixels]
rc
=
np
.
sqrt
(
x
*
x
+
y
*
y
)
cpix
=
rc
*
(
self
.
sigSpin
*
a2Rad
)
beta
=
(
np
.
arctan2
(
y
,
x
)
+
np
.
pi
/
2
)
ell
=
cpix
**
2
/
(
2.0
*
ff
**
2
+
cpix
**
2
)
#ell *= 10.0
qr
=
np
.
sqrt
((
1.0
+
ell
)
/
(
1.0
-
ell
))
#psfShape = galsim.Shear(e=ell, beta=beta)
#g1, g2 = psfShape.g1, psfShape.g2
#qr = np.sqrt((1.0+ell)/(1.0-ell))
#return ell, beta, qr
PSFshear
=
galsim
.
Shear
(
e
=
ell
,
beta
=
beta
*
galsim
.
radians
)
return
self
.
psf
.
shear
(
PSFshear
),
PSFshear
if
__name__
==
'__main__'
:
psfPath
=
'/data/simudata/CSSOSDataProductsSims/data/csstPSFdata/CSSOS_psf_ciomp_30X30'
#psfPath = '/data/simudata/CSSOSDataProductsSims/data/csstPSFdata/CSSOS_psf_ciomp'
psfCSST
=
PSFInterp
(
PSF_data_file
=
psfPath
)
iwave
=
1
ipsf
=
665
pos_img
=
[
psfCSST
.
cen_col
[
iwave
,
ipsf
],
psfCSST
.
cen_row
[
iwave
,
ipsf
]]
img
=
psfCSST
.
get_PSF
(
1
,
pos_img
,
iwave
,
galsimGSObject
=
False
)
#plot check-1
import
matplotlib.pyplot
as
plt
fig
=
plt
.
figure
(
figsize
=
(
18
,
5
))
ax
=
plt
.
subplot
(
1
,
3
,
1
)
plt
.
imshow
(
img
)
plt
.
colorbar
()
ax
=
plt
.
subplot
(
1
,
3
,
2
)
imgx
=
psfCSST
.
itpPSF_data
[
iwave
][
ipsf
][
'psfMat'
]
imgx
/=
np
.
sum
(
imgx
)
plt
.
imshow
(
imgx
)
plt
.
colorbar
()
ax
=
plt
.
subplot
(
1
,
3
,
3
)
plt
.
imshow
(
img
-
imgx
)
plt
.
colorbar
()
plt
.
savefig
(
'test/figs/test.jpg'
)
#plot check-2: 注意图像坐标和全局坐标
fig
=
plt
.
figure
(
figsize
=
(
8
,
8
),
dpi
=
200
)
img
=
psfCSST
.
PSF_data
[
iwave
][
ipsf
][
'psfMat'
]
npix
=
img
.
shape
[
0
]
dng
=
105
imgg
=
img
[
dng
:
-
dng
,
dng
:
-
dng
]
plt
.
imshow
(
imgg
)
imgX
=
psfCSST
.
PSF_data
[
iwave
][
ipsf
][
'image_x'
]
#in mm
imgY
=
psfCSST
.
PSF_data
[
iwave
][
ipsf
][
'image_y'
]
#in mm
deltX
=
psfCSST
.
PSF_data
[
iwave
][
ipsf
][
'centroid_x'
]
#in mm
deltY
=
psfCSST
.
PSF_data
[
iwave
][
ipsf
][
'centroid_y'
]
#in mm
maxX
=
psfCSST
.
PSF_data
[
iwave
][
ipsf
][
'max_x'
]
maxY
=
psfCSST
.
PSF_data
[
iwave
][
ipsf
][
'max_y'
]
cenPix_X
=
npix
/
2
+
deltX
/
0.005
cenPix_Y
=
npix
/
2
-
deltY
/
0.005
maxPix_X
=
npix
/
2
+
maxX
/
0.005
-
1
maxPix_Y
=
npix
/
2
-
maxY
/
0.005
-
1
plt
.
plot
([
cenPix_X
-
dng
],[
cenPix_Y
-
dng
],
'rx'
,
ms
=
20
)
plt
.
plot
([
maxPix_X
-
dng
],[
maxPix_Y
-
dng
],
'b+'
,
ms
=
20
)
from
scipy
import
ndimage
y
,
x
=
ndimage
.
center_of_mass
(
img
)
plt
.
plot
([
x
-
dng
],[
y
-
dng
],
'rx'
,
ms
=
10
,
mew
=
2
)
x
,
y
=
mypy
.
findMaxPix
(
img
)
plt
.
plot
([
x
-
dng
],[
y
-
dng
],
'b+'
,
ms
=
10
,
mew
=
2
)
plt
.
savefig
(
'test/figs/test.jpg'
)
ObservationSim/PSF/PSFInterp_deprecated/PSFInterp_v2/PSFConfig.py
deleted
100644 → 0
View file @
e82a5dcc
import
sys
from
itertools
import
islice
import
numpy
as
np
import
matplotlib.pyplot
as
plt
import
scipy.io
#from scipy.io import loadmat
from
scipy
import
ndimage
import
ctypes
import
galsim
from
PSF.PSFInterp.PSFUtil
import
*
###加载PSF信息###
def
LoadPSF
(
iccd
,
iwave
,
ipsf
,
psfPath
,
psfSampleSize
=
5
,
InputMaxPixelPos
=
False
,
PSFCentroidWgt
=
False
):
"""
load psf informations from psf matrix.
Parameters:
iccd (int): ccd number [1,30].
iwave(int): wave-index [1,4].
ipsf (int): psf number [1, 100].
psfPath (int): path to psf matrix
psfSampleSize (float-optional): psf size in microns.
InputMaxPixelPos(bool-optional): only True for 30*30 psf-matrix
Returns:
psfInfo (dirctionary)
"""
if
iccd
not
in
np
.
linspace
(
1
,
30
,
30
,
dtype
=
'int'
):
print
(
'Error - iccd should be in [1, 30].'
)
sys
.
exit
()
if
iwave
not
in
np
.
linspace
(
1
,
4
,
4
,
dtype
=
'int'
):
print
(
'Error - iwave should be in [1, 4].'
)
sys
.
exit
()
if
ipsf
not
in
np
.
linspace
(
1
,
900
,
900
,
dtype
=
'int'
):
print
(
'Error - ipsf should be in [1, 900].'
)
sys
.
exit
()
psfInfo
=
{}
fpath
=
psfPath
+
'/'
+
'ccd{:}'
.
format
(
iccd
)
+
'/'
+
'wave_{:}'
.
format
(
iwave
)
#获取ipsf矩阵
if
not
PSFCentroidWgt
:
##读取PSF原数据
fpathMat
=
fpath
+
'/'
+
'5_psf_array'
+
'/'
+
'psf_{:}.mat'
.
format
(
ipsf
)
data
=
scipy
.
io
.
loadmat
(
fpathMat
)
psfInfo
[
'psfMat'
]
=
data
[
'psf'
]
if
PSFCentroidWgt
:
##读取PSFCentroidWgt
ffpath
=
psfPath
+
'_proc/'
+
'ccd{:}'
.
format
(
iccd
)
+
'/'
+
'wave_{:}'
.
format
(
iwave
)
ffpathMat
=
ffpath
+
'/'
+
'5_psf_array'
+
'/'
+
'psf_{:}_centroidWgt.mat'
.
format
(
ipsf
)
data
=
scipy
.
io
.
loadmat
(
ffpathMat
)
psfInfo
[
'psfMat'
]
=
data
[
'psf'
]
#获取ipsf波长
fpathWave
=
fpath
+
'/'
+
'1_wavelength.txt'
f
=
open
(
fpathWave
,
'r'
)
wavelength
=
np
.
float
(
f
.
readline
())
f
.
close
()
psfInfo
[
'wavelength'
]
=
wavelength
#获取ipsf位置
fpathCoordinate
=
fpath
+
'/'
+
'4_PSF_coordinate.txt'
f
=
open
(
fpathCoordinate
,
'r'
)
header
=
f
.
readline
()
for
line
in
islice
(
f
,
ipsf
-
1
,
ipsf
):
line
=
line
.
strip
()
columns
=
line
.
split
()
f
.
close
()
icol
=
0
psfInfo
[
'field_x'
]
=
float
(
columns
[
icol
])
#deg, 视场采样位置
icol
+=
1
psfInfo
[
'field_y'
]
=
float
(
columns
[
icol
])
#deg
icol
+=
1
psfInfo
[
'centroid_x'
]
=
float
(
columns
[
icol
])
#mm, psf质心相对主光线的偏移量
icol
+=
1
psfInfo
[
'centroid_y'
]
=
float
(
columns
[
icol
])
#mm
icol
+=
1
if
InputMaxPixelPos
==
True
:
psfInfo
[
'max_x'
]
=
float
(
columns
[
icol
])
#mm, max pixel postion
icol
+=
1
psfInfo
[
'max_y'
]
=
float
(
columns
[
icol
])
#mm
icol
+=
1
psfInfo
[
'image_x'
]
=
float
(
columns
[
icol
])
#mm, 主光线位置
icol
+=
1
psfInfo
[
'image_y'
]
=
float
(
columns
[
icol
])
#mm
nrows
,
ncols
=
psfInfo
[
'psfMat'
].
shape
psfPos
=
psfPixelLayout
(
nrows
,
ncols
,
psfInfo
[
'image_y'
],
psfInfo
[
'image_x'
],
pixSizeInMicrons
=
5.0
)
imgMaxPix_x
,
imgMaxPix_y
=
findMaxPix
(
psfInfo
[
'psfMat'
])
psfInfo
[
'imgMaxPosx_ccd'
]
=
psfPos
[
0
,
imgMaxPix_y
,
imgMaxPix_x
]
#cx, psf最大值位置, in mm
psfInfo
[
'imgMaxPosy_ccd'
]
=
psfPos
[
1
,
imgMaxPix_y
,
imgMaxPix_x
]
#cy
if
PSFCentroidWgt
:
#if ipsf == 1:
#print('Check:', psfInfo['centroid_x'], psfInfo['centroid_y'], data['cx'][0][0], data['cy'][0][0])
psfInfo
[
'centroid_x'
]
=
data
[
'cx'
][
0
][
0
]
#mm, psfCentroidWgt质心相对主光线的偏移量
psfInfo
[
'centroid_y'
]
=
data
[
'cy'
][
0
][
0
]
#mm
return
psfInfo
###加载PSF矩阵信息###
def
LoadPSFset
(
iccd
,
iwave
,
npsf
,
psfPath
,
psfSampleSize
=
5
,
InputMaxPixelPos
=
False
,
PSFCentroidWgt
=
False
):
"""
load psfSet for interpolation
Parameters:
iccd, iwave, psfPath: # of ccd/wave and path for psfs
npsf: 100 or 900 for different psf matrixes
Returns:
PSFMat (numpy.array): images
cen_col, cen_row (numpy.array, numpy.array): position of psf center in the view field
"""
psfSet
=
[]
for
ipsf
in
range
(
1
,
npsf
+
1
):
psfInfo
=
LoadPSF
(
iccd
,
iwave
,
ipsf
,
psfPath
,
InputMaxPixelPos
=
InputMaxPixelPos
,
PSFCentroidWgt
=
PSFCentroidWgt
)
psfSet
.
append
(
psfInfo
)
ngy
,
ngx
=
psfSet
[
0
][
'psfMat'
].
shape
PSFMat
=
np
.
zeros
([
npsf
,
ngy
,
ngx
])
cen_col
=
np
.
zeros
(
npsf
)
cen_row
=
np
.
zeros
(
npsf
)
FieldPos
=
False
for
ipsf
in
range
(
npsf
):
iPSFMat
=
psfSet
[
ipsf
][
'psfMat'
]
PSFMat
[
ipsf
,
:,
:]
=
psfTailor
(
iPSFMat
,
apSizeInArcsec
=
2.5
)
if
FieldPos
==
True
:
cen_col
[
ipsf
]
=
psfSet
[
ipsf
][
'field_x'
]
#cx
cen_row
[
ipsf
]
=
psfSet
[
ipsf
][
'field_y'
]
#cy
if
FieldPos
==
False
:
cen_col
[
ipsf
]
=
psfSet
[
ipsf
][
'image_x'
]
+
psfSet
[
ipsf
][
'centroid_x'
]
#cx
cen_row
[
ipsf
]
=
psfSet
[
ipsf
][
'image_y'
]
+
psfSet
[
ipsf
][
'centroid_y'
]
#cy
#cen_col[ipsf] = psfSet[ipsf]['imgMaxPosx_ccd'] #cx --- to be updated
#cen_row[ipsf] = psfSet[ipsf]['imgMaxPosy_ccd'] #cy
#cen_col[ipsf] = psfSet[ipsf]['image_x']
#cen_row[ipsf] = psfSet[ipsf]['image_y']
return
psfSet
,
PSFMat
,
cen_col
,
cen_row
###插值PSF图像-IDW方法###
def
psfMaker_IDW
(
px
,
py
,
PSFMat
,
cen_col
,
cen_row
,
IDWindex
=
2
,
OnlyNeighbors
=
True
,
hoc
=
None
,
hoclist
=
None
,
PSFCentroidWgt
=
False
):
"""
psf interpolation by IDW
Parameters:
px, py (float, float): position of the target
PSFMat (numpy.array): image
cen_col, cen_row (numpy.array, numpy.array): potions of the psf centers
IDWindex (int-optional): the power index of IDW
OnlyNeighbors (bool-optional): only neighbors are used for psf interpolation
Returns:
psfMaker (numpy.array)
"""
minimum_psf_weight
=
1e-8
ref_col
=
px
ref_row
=
py
ngy
,
ngx
=
PSFMat
[
0
,
:,
:].
shape
npsf
=
PSFMat
[:,
:,
:].
shape
[
0
]
psfWeight
=
np
.
zeros
([
npsf
])
if
OnlyNeighbors
==
True
:
if
hoc
is
None
:
neigh
=
findNeighbors
(
px
,
py
,
cen_col
,
cen_row
,
dr
=
5.
,
dn
=
4
,
OnlyDistance
=
False
)
if
hoc
is
not
None
:
#hoc,hoclist = findNeighbors_hoclist(cen_col, cen_row)
neigh
=
findNeighbors_hoclist
(
cen_col
,
cen_row
,
tx
=
px
,
ty
=
py
,
dn
=
4
,
hoc
=
hoc
,
hoclist
=
hoclist
)
#print("neigh:", neigh)
neighFlag
=
np
.
zeros
(
npsf
)
neighFlag
[
neigh
]
=
1
for
ipsf
in
range
(
npsf
):
if
OnlyNeighbors
==
True
:
if
neighFlag
[
ipsf
]
!=
1
:
continue
dist
=
np
.
sqrt
((
ref_col
-
cen_col
[
ipsf
])
**
2
+
(
ref_row
-
cen_row
[
ipsf
])
**
2
)
if
IDWindex
==
1
:
psfWeight
[
ipsf
]
=
dist
if
IDWindex
==
2
:
psfWeight
[
ipsf
]
=
dist
**
2
if
IDWindex
==
3
:
psfWeight
[
ipsf
]
=
dist
**
3
if
IDWindex
==
4
:
psfWeight
[
ipsf
]
=
dist
**
4
psfWeight
[
ipsf
]
=
max
(
psfWeight
[
ipsf
],
minimum_psf_weight
)
psfWeight
[
ipsf
]
=
1.
/
psfWeight
[
ipsf
]
psfWeight
/=
np
.
sum
(
psfWeight
)
psfMaker
=
np
.
zeros
((
ngy
,
ngx
),
dtype
=
'float64'
)
for
ipsf
in
range
(
npsf
):
if
OnlyNeighbors
==
True
:
if
neighFlag
[
ipsf
]
!=
1
:
continue
iPSFMat
=
PSFMat
[
ipsf
,
:,
:].
copy
()
#if not PSFCentroidWgt:
# iPSFMat = psfCentering(iPSFMat, CenteringMode=2)
#iPSFMat = psfCentering_FFT(iPSFMat)
#if PSFCentroidWgt:
ipsfWeight
=
psfWeight
[
ipsf
]
psfMaker
+=
iPSFMat
*
ipsfWeight
#print("ipsf, ipsfWeight:", ipsf, ipsfWeight)
psfMaker
/=
np
.
nansum
(
psfMaker
)
return
psfMaker
###TEST###
if
__name__
==
'__main__'
:
iccd
=
1
iwave
=
1
ipsf
=
1
npsf
=
100
psfPath
=
'/data/simudata/CSSOSDataProductsSims/data/csstPSFdata/CSSOS_psf_ciomp'
a
,
b
,
c
,
d
=
LoadPSFset
(
iccd
,
iwave
,
npsf
,
psfPath
,
InputMaxPixelPos
=
False
)
psfSet
=
a
print
(
'psfSet has been loaded.'
)
print
(
'Usage: psfSet[i][keys]'
)
print
(
'psfSet.keys:'
,
psfSet
[
0
].
keys
())
print
(
c
[
0
:
10
])
print
(
d
[
0
:
10
])
ObservationSim/PSF/PSFInterp_deprecated/PSFInterp_v2/PSFInterp.py
deleted
100644 → 0
View file @
e82a5dcc
'''
PSF interpolation for CSST-Sim
NOTE: [iccd, iwave, ipsf] are counted from 1 to n, but [tccd, twave, tpsf] are counted from 0 to n-1
'''
import
galsim
import
numpy
as
np
import
os
import
time
import
copy
import
PSF.PSFInterp.PSFConfig
as
myConfig
import
PSF.PSFInterp.PSFUtil
as
myUtil
from
PSF.PSFModel
import
PSFModel
LOG_DEBUG
=
False
#***#
NPSF
=
900
#***# 30*30
iccdTest
=
1
#***#
class
PSFInterp
(
PSFModel
):
# def __init__(self, PSF_data=None, params=None, PSF_data_file=None):
def
__init__
(
self
,
chip
,
PSF_data
=
None
,
PSF_data_file
=
None
,
sigSpin
=
0.
,
psfRa
=
0.15
):
"""
The PSF data matrix is either given by a object parameter or read in from a file.
Parameters:
PSF_data: The PSF data matrix object
params: Other parameters?
PSF_data_file: The file for PSF data matrix (optional).
"""
if
LOG_DEBUG
:
print
(
'==================================================='
)
print
(
'DEBUG: psf module for csstSim '
\
+
time
.
strftime
(
"(%Y-%m-%d %H:%M:%S)"
,
time
.
localtime
()),
flush
=
True
)
print
(
'==================================================='
)
self
.
sigSpin
=
sigSpin
self
.
sigGauss
=
psfRa
# 80% light radius
self
.
iccd
=
int
(
chip
.
getChipLabel
(
chipID
=
chip
.
chipID
))
if
PSF_data_file
==
None
:
PSF_data_file
=
'/data/simudata/CSSOSDataProductsSims/data/csstPSFdata/CSSOS_psf_ciomp_30X30'
self
.
nwave
=
self
.
_getPSFwave
(
self
.
iccd
,
PSF_data_file
)
self
.
PSF_data
=
self
.
_loadPSF
(
self
.
iccd
,
PSF_data_file
)
if
LOG_DEBUG
:
print
(
'nwave-{:} on ccd-{:}::'
.
format
(
self
.
nwave
,
self
.
iccd
),
flush
=
True
)
print
(
'self.PSF_data ... ok'
,
flush
=
True
)
print
(
'Preparing self.[psfMat,cen_col,cen_row] for psfMaker ... '
,
end
=
''
,
flush
=
True
)
ngy
,
ngx
=
self
.
PSF_data
[
0
][
0
][
'psfMat'
].
shape
self
.
psfMat
=
np
.
zeros
([
self
.
nwave
,
NPSF
,
ngy
,
ngx
])
self
.
cen_col
=
np
.
zeros
([
self
.
nwave
,
NPSF
])
self
.
cen_row
=
np
.
zeros
([
self
.
nwave
,
NPSF
])
self
.
hoc
=
[]
self
.
hoclist
=
[]
for
twave
in
range
(
self
.
nwave
):
for
tpsf
in
range
(
NPSF
):
#psfMatX = myUtil.psfTailor(self.PSF_data[twave][tpsf]['psfMat'], apSizeInArcsec=2.5)
self
.
psfMat
[
twave
,
tpsf
,
:,
:]
=
self
.
PSF_data
[
twave
][
tpsf
][
'psfMat'
]
self
.
cen_col
[
twave
,
tpsf
]
=
self
.
PSF_data
[
twave
][
tpsf
][
'image_x'
]
+
self
.
PSF_data
[
twave
][
tpsf
][
'centroid_x'
]
self
.
cen_row
[
twave
,
tpsf
]
=
self
.
PSF_data
[
twave
][
tpsf
][
'image_y'
]
+
self
.
PSF_data
[
twave
][
tpsf
][
'centroid_y'
]
#hoclist on twave for neighborsFinding
hoc
,
hoclist
=
myUtil
.
findNeighbors_hoclist
(
self
.
cen_col
[
twave
],
self
.
cen_row
[
twave
])
self
.
hoc
.
append
(
hoc
)
self
.
hoclist
.
append
(
hoclist
)
if
LOG_DEBUG
:
print
(
'ok'
,
flush
=
True
)
def
_getPSFwave
(
self
,
iccd
,
PSF_data_file
):
"""
Get # of sampling waves on iccd
Parameters:
iccd: The chip of i-th ccd
PSF_data_file: The file for PSF data matrix
Returns:
nwave: The number of the sampling waves
"""
strs
=
os
.
listdir
(
PSF_data_file
+
'/ccd{:}'
.
format
(
iccd
))
nwave
=
0
for
_
in
strs
:
if
'wave_'
in
_
:
nwave
+=
1
return
nwave
def
_loadPSF
(
self
,
iccd
,
PSF_data_file
):
"""
load psf-matrix on iccd
Parameters:
iccd: The chip of i-th ccd
PSF_data_file: The file for PSF data matrix
Returns:
psfSet: The matrix of the csst-psf
"""
psfSet
=
[]
for
ii
in
range
(
self
.
nwave
):
iwave
=
ii
+
1
if
LOG_DEBUG
:
print
(
'self._loadPSF: iwave::'
,
iwave
,
flush
=
True
)
psfWave
=
[]
for
jj
in
range
(
NPSF
):
ipsf
=
jj
+
1
psfInfo
=
myConfig
.
LoadPSF
(
iccd
,
iwave
,
ipsf
,
PSF_data_file
,
InputMaxPixelPos
=
True
,
PSFCentroidWgt
=
True
)
psfWave
.
append
(
psfInfo
)
psfSet
.
append
(
psfWave
)
if
LOG_DEBUG
:
print
(
'psfSet has been loaded:'
,
flush
=
True
)
print
(
'psfSet[iwave][ipsf][keys]:'
,
psfSet
[
0
][
0
].
keys
(),
flush
=
True
)
return
psfSet
def
_preprocessPSF
(
self
):
"""
Preprocessing psf-matrix
Parameters:
Returns:
itpPSF_data: The matrix of the preprocessed csst-psf
"""
'''
#old version (discarded)
itpPSF_data = copy.deepcopy(self.PSF_data)
for twave in range(self.nwave):
for tpsf in range(NPSF):
psfMat = self.PSF_data[twave][tpsf]['psfMat']
psf_image_x = self.PSF_data[twave][tpsf]['image_x']
psf_image_y = self.PSF_data[twave][tpsf]['image_y']
#psfMatX= myUtil.psfCentering(psfMat, CenteringMode=1)
#itpPSF_data[twave][tpsf]['psfMat'] = psfMatX
#img, cxt, cyt = myUtil.psfCentering_wgt(psfMat, psf_image_x, psf_image_y)
#itpPSF_data[twave][tpsf]['psfMat'] = img
#itpPSF_data[twave][tpsf]['centroid_x'] = cxt
#itpPSF_data[twave][tpsf]['centroid_y'] = cyt
return itpPSF_data
'''
pass
def
_findWave
(
self
,
bandpass
):
for
twave
in
range
(
self
.
nwave
):
bandwave
=
self
.
PSF_data
[
twave
][
0
][
'wavelength'
]
if
bandpass
.
blue_limit
<
bandwave
and
bandwave
<
bandpass
.
red_limit
:
return
twave
return
-
1
def
get_PSF
(
self
,
chip
,
pos_img
,
bandpass
,
pixSize
=
0.037
,
galsimGSObject
=
True
,
folding_threshold
=
5.e-3
):
"""
Get the PSF at a given image position
Parameters:
chip: A 'Chip' object representing the chip we want to extract PSF from.
pos_img: A 'galsim.Position' object representing the image position.
bandpass: A 'galsim.Bandpass' object representing the wavelength range.
pixSize: The pixels size of psf matrix
Returns:
PSF: A 'galsim.GSObject'.
"""
assert
self
.
iccd
==
int
(
chip
.
getChipLabel
(
chipID
=
chip
.
chipID
)),
'ERROR: self.iccd != chip.label'
# twave = bandpass-1 #***# ??? #self.findWave(bandpass) ###twave=iwave-1 as that in NOTE
twave
=
self
.
_findWave
(
bandpass
)
if
twave
==
-
1
:
print
(
"!!!PSF bandpass does not match."
)
exit
()
PSFMat
=
self
.
psfMat
[
twave
]
cen_col
=
self
.
cen_col
[
twave
]
cen_row
=
self
.
cen_row
[
twave
]
# px = pos_img[0]
# py = pos_img[1]
px
=
(
pos_img
.
x
-
chip
.
cen_pix_x
)
*
0.01
py
=
(
pos_img
.
y
-
chip
.
cen_pix_y
)
*
0.01
imPSF
=
myConfig
.
psfMaker_IDW
(
px
,
py
,
PSFMat
,
cen_col
,
cen_row
,
IDWindex
=
2
,
OnlyNeighbors
=
True
,
hoc
=
self
.
hoc
[
twave
],
hoclist
=
self
.
hoclist
[
twave
],
PSFCentroidWgt
=
True
)
#imPSF = myConfig.psfMaker_IDW(px, py, PSFMat, cen_col, cen_row, IDWindex=2, OnlyNeighbors=True, PSFCentroidWgt=True)
if
galsimGSObject
:
img
=
galsim
.
ImageF
(
imPSF
,
scale
=
pixSize
)
gsp
=
galsim
.
GSParams
(
folding_threshold
=
folding_threshold
)
self
.
psf
=
galsim
.
InterpolatedImage
(
img
,
gsparams
=
gsp
)
# dx = px - chip.cen_pix_x
# dy = py - chip.cen_pix_y
return
self
.
PSFspin
(
x
=
px
/
0.01
,
y
=
py
/
0.01
)
return
imPSF
def
PSFspin
(
self
,
x
,
y
):
"""
The PSF profile at a given image position relative to the axis center
Parameters:
theta : spin angles in a given exposure in unit of [arcsecond]
dx, dy: relative position to the axis center in unit of [pixels]
Return:
Spinned PSF: g1, g2 and axis ratio 'a/b'
"""
a2Rad
=
np
.
pi
/
(
60.0
*
60.0
*
180.0
)
ff
=
self
.
sigGauss
*
0.107
*
(
1000.0
/
10.0
)
# in unit of [pixels]
rc
=
np
.
sqrt
(
x
*
x
+
y
*
y
)
cpix
=
rc
*
(
self
.
sigSpin
*
a2Rad
)
beta
=
(
np
.
arctan2
(
y
,
x
)
+
np
.
pi
/
2
)
ell
=
cpix
**
2
/
(
2.0
*
ff
**
2
+
cpix
**
2
)
qr
=
np
.
sqrt
((
1.0
+
ell
)
/
(
1.0
-
ell
))
PSFshear
=
galsim
.
Shear
(
e
=
ell
,
beta
=
beta
*
galsim
.
radians
)
return
self
.
psf
.
shear
(
PSFshear
),
PSFshear
def
testPSFInterp
():
import
time
import
matplotlib.pyplot
as
plt
iccd
=
iccdTest
#[1, 30] for test
iwave
=
1
#[1, 4] for test
npsfB
=
900
psfPathB
=
'/data/simudata/CSSOSDataProductsSims/data/csstPSFdata/CSSOS_psf_ciomp_30X30'
psfCSST
=
PSFInterp
(
PSF_data_file
=
psfPathB
)
### define PSF_data from 30*30
npsfA
=
100
psfPathA
=
'/data/simudata/CSSOSDataProductsSims/data/csstPSFdata/CSSOS_psf_ciomp'
psfSetA
,
PSFMatA
,
cen_colA
,
cen_rowA
=
myConfig
.
LoadPSFset
(
iccd
,
iwave
,
npsfA
,
psfPathA
,
InputMaxPixelPos
=
False
,
PSFCentroidWgt
=
True
)
###load test_data from 10*10
psf_sz
=
np
.
zeros
(
npsfA
)
psf_e1
=
np
.
zeros
(
npsfA
)
psf_e2
=
np
.
zeros
(
npsfA
)
psfMaker_sz
=
np
.
zeros
(
npsfA
)
psfMaker_e1
=
np
.
zeros
(
npsfA
)
psfMaker_e2
=
np
.
zeros
(
npsfA
)
runtimeInterp
=
0
starttime
=
time
.
time
()
for
ipsf
in
range
(
npsfA
):
print
(
'IDW-ipsf: {:4}/100'
.
format
(
ipsf
),
end
=
'
\r
'
,
flush
=
True
)
starttimeInterp
=
time
.
time
()
px
=
cen_colA
[
ipsf
]
py
=
cen_rowA
[
ipsf
]
pos_img
=
[
px
,
py
]
img
=
psfCSST
.
get_PSF
(
iccd
,
pos_img
,
iwave
,
galsimGSObject
=
False
)
###interpolate PSF at[px,py]
endtimeInterp
=
time
.
time
()
runtimeInterp
=
runtimeInterp
+
(
endtimeInterp
-
starttimeInterp
)
imx
=
psfSetA
[
ipsf
][
'psfMat'
]
imy
=
img
cenX
,
cenY
,
sz
,
e1
,
e2
,
REE80
=
myUtil
.
psfSizeCalculator
(
imx
,
CalcPSFcenter
=
True
,
SigRange
=
True
,
TailorScheme
=
2
)
psf_sz
[
ipsf
]
=
sz
psf_e1
[
ipsf
]
=
e1
psf_e2
[
ipsf
]
=
e2
cenX
,
cenY
,
sz
,
e1
,
e2
,
REE80
=
myUtil
.
psfSizeCalculator
(
imy
,
CalcPSFcenter
=
True
,
SigRange
=
True
,
TailorScheme
=
2
)
psfMaker_sz
[
ipsf
]
=
sz
psfMaker_e1
[
ipsf
]
=
e1
psfMaker_e2
[
ipsf
]
=
e2
endtime
=
time
.
time
()
print
(
'run time::'
,
endtime
-
starttime
,
runtimeInterp
)
if
True
:
ell_iccd
=
np
.
zeros
(
npsfA
)
ell_iccd_psfMaker
=
np
.
zeros
(
npsfA
)
fig
=
plt
.
figure
(
figsize
=
(
18
,
5
))
plt
.
subplots_adjust
(
wspace
=
0.1
,
hspace
=
0.1
)
ax
=
plt
.
subplot
(
1
,
3
,
1
)
for
ipsf
in
range
(
npsfA
):
imx
=
cen_colA
[
ipsf
]
imy
=
cen_rowA
[
ipsf
]
plt
.
plot
(
imx
,
imy
,
'b.'
)
ang
=
np
.
arctan2
(
psf_e2
[
ipsf
],
psf_e1
[
ipsf
])
/
2
ell
=
np
.
sqrt
(
psf_e1
[
ipsf
]
**
2
+
psf_e2
[
ipsf
]
**
2
)
ell_iccd
[
ipsf
]
=
ell
ell
*=
50
lcos
=
ell
*
np
.
cos
(
ang
)
lsin
=
ell
*
np
.
sin
(
ang
)
plt
.
plot
([
imx
-
lcos
,
imx
+
lcos
],[
imy
-
lsin
,
imy
+
lsin
],
'b'
,
lw
=
2
)
###########
ang
=
np
.
arctan2
(
psfMaker_e2
[
ipsf
],
psfMaker_e1
[
ipsf
])
/
2
ell
=
np
.
sqrt
(
psfMaker_e1
[
ipsf
]
**
2
+
psfMaker_e2
[
ipsf
]
**
2
)
ell_iccd_psfMaker
[
ipsf
]
=
ell
ell
*=
50
lcos
=
ell
*
np
.
cos
(
ang
)
lsin
=
ell
*
np
.
sin
(
ang
)
plt
.
plot
([
imx
-
lcos
,
imx
+
lcos
],[
imy
-
lsin
,
imy
+
lsin
],
'r'
,
lw
=
1
)
###########
plt
.
gca
().
set_aspect
(
1
)
ax
=
plt
.
subplot
(
1
,
3
,
2
)
tmp
=
plt
.
hist
(
ell_iccd
,
bins
=
8
,
color
=
'b'
,
alpha
=
0.5
)
tmp
=
plt
.
hist
(
ell_iccd_psfMaker
,
bins
=
8
,
color
=
'r'
,
alpha
=
0.5
)
plt
.
annotate
(
'iccd-{:} iwave-{:}'
.
format
(
iccd
,
iwave
),
(
0.55
,
0.85
),
xycoords
=
'axes fraction'
,
fontsize
=
15
)
plt
.
xlabel
(
'ell'
)
plt
.
ylabel
(
'PDF'
)
ax
=
plt
.
subplot
(
1
,
3
,
3
)
dsz
=
(
psfMaker_sz
-
psf_sz
)
/
psf_sz
dsz_hist
=
plt
.
hist
(
dsz
)
plt
.
xlabel
(
'dsz'
)
plt
.
savefig
(
'test/figs/testPSFInterp_30t10_iccd{:}_iwave{:}.pdf'
.
format
(
iccd
,
iwave
))
if
__name__
==
'__main__'
:
if
False
:
psfPath
=
'/data/simudata/CSSOSDataProductsSims/data/csstPSFdata/CSSOS_psf_ciomp_30X30'
#psfPath = '/data/simudata/CSSOSDataProductsSims/data/csstPSFdata/CSSOS_psf_ciomp'
psfCSST
=
PSFInterp
(
PSF_data_file
=
psfPath
)
iwave
=
1
ipsf
=
665
pos_img
=
[
psfCSST
.
cen_col
[
iwave
,
ipsf
],
psfCSST
.
cen_row
[
iwave
,
ipsf
]]
img
=
psfCSST
.
get_PSF
(
1
,
pos_img
,
iwave
,
galsimGSObject
=
False
)
print
(
'haha'
)
if
True
:
testPSFInterp
()
if
False
:
#old version (discarded)
#plot check-1
import
matplotlib.pyplot
as
plt
fig
=
plt
.
figure
(
figsize
=
(
18
,
5
))
ax
=
plt
.
subplot
(
1
,
3
,
1
)
plt
.
imshow
(
img
)
plt
.
colorbar
()
ax
=
plt
.
subplot
(
1
,
3
,
2
)
imgx
=
psfCSST
.
itpPSF_data
[
iwave
][
ipsf
][
'psfMat'
]
imgx
/=
np
.
sum
(
imgx
)
plt
.
imshow
(
imgx
)
plt
.
colorbar
()
ax
=
plt
.
subplot
(
1
,
3
,
3
)
plt
.
imshow
(
img
-
imgx
)
plt
.
colorbar
()
plt
.
savefig
(
'test/figs/test1.jpg'
)
if
False
:
#old version (discarded)
#plot check-2: 注意图像坐标和全局坐标
fig
=
plt
.
figure
(
figsize
=
(
8
,
8
),
dpi
=
200
)
img
=
psfCSST
.
PSF_data
[
iwave
][
ipsf
][
'psfMat'
]
npix
=
img
.
shape
[
0
]
dng
=
105
imgg
=
img
[
dng
:
-
dng
,
dng
:
-
dng
]
plt
.
imshow
(
imgg
)
imgX
=
psfCSST
.
PSF_data
[
iwave
][
ipsf
][
'image_x'
]
#in mm
imgY
=
psfCSST
.
PSF_data
[
iwave
][
ipsf
][
'image_y'
]
#in mm
deltX
=
psfCSST
.
PSF_data
[
iwave
][
ipsf
][
'centroid_x'
]
#in mm
deltY
=
psfCSST
.
PSF_data
[
iwave
][
ipsf
][
'centroid_y'
]
#in mm
maxX
=
psfCSST
.
PSF_data
[
iwave
][
ipsf
][
'max_x'
]
maxY
=
psfCSST
.
PSF_data
[
iwave
][
ipsf
][
'max_y'
]
cenPix_X
=
npix
/
2
+
deltX
/
0.005
cenPix_Y
=
npix
/
2
-
deltY
/
0.005
maxPix_X
=
npix
/
2
+
maxX
/
0.005
-
1
maxPix_Y
=
npix
/
2
-
maxY
/
0.005
-
1
plt
.
plot
([
cenPix_X
-
dng
],[
cenPix_Y
-
dng
],
'rx'
,
ms
=
20
)
plt
.
plot
([
maxPix_X
-
dng
],[
maxPix_Y
-
dng
],
'b+'
,
ms
=
20
)
from
scipy
import
ndimage
y
,
x
=
ndimage
.
center_of_mass
(
img
)
plt
.
plot
([
x
-
dng
],[
y
-
dng
],
'rx'
,
ms
=
10
,
mew
=
4
)
x
,
y
=
myUtil
.
findMaxPix
(
img
)
plt
.
plot
([
x
-
dng
],[
y
-
dng
],
'b+'
,
ms
=
10
,
mew
=
4
)
plt
.
savefig
(
'test/figs/test2.jpg'
)
ObservationSim/PSF/PSFInterp_deprecated/PSFInterp_v2/PSFProcess.py
deleted
100644 → 0
View file @
e82a5dcc
import
os
,
sys
import
numpy
as
np
import
scipy.io
import
mpi4py.MPI
as
MPI
sys
.
path
.
append
(
"/public/home/weichengliang/lnData/CSST_new_framwork/csstPSF"
)
import
PSFConfig
as
myConfig
import
PSFUtil
as
myUtil
def
mkdir
(
path
):
isExists
=
os
.
path
.
exists
(
path
)
if
not
isExists
:
os
.
mkdir
(
path
)
############################################
comm
=
MPI
.
COMM_WORLD
ThisTask
=
comm
.
Get_rank
()
NTasks
=
comm
.
Get_size
()
npsf
=
900
psfPath
=
'/data/simudata/CSSOSDataProductsSims/data/csstPSFdata/CSSOS_psf_ciomp_30X30'
#npsf = 100
#psfPath = '/data/simudata/CSSOSDataProductsSims/data/csstPSFdata/CSSOS_psf_ciomp'
npsfPerTasks
=
int
(
npsf
/
NTasks
)
iStart
=
0
+
npsfPerTasks
*
ThisTask
iEnd
=
npsfPerTasks
+
npsfPerTasks
*
ThisTask
if
ThisTask
==
NTasks
:
iEnd
=
npsf
for
iccd
in
range
(
1
,
31
):
iccdPath
=
psfPath
+
'_proc/ccd{:}'
.
format
(
iccd
)
if
ThisTask
==
0
:
mkdir
(
iccdPath
)
comm
.
barrier
()
for
iwave
in
range
(
1
,
5
):
iwavePath
=
iccdPath
+
'/wave_{:}'
.
format
(
iwave
)
if
ThisTask
==
0
:
mkdir
(
iwavePath
)
comm
.
barrier
()
psfMatPath
=
iwavePath
+
'/5_psf_array'
if
ThisTask
==
0
:
mkdir
(
psfMatPath
)
comm
.
barrier
()
for
ii
in
range
(
iStart
,
iEnd
):
ipsf
=
ii
+
1
if
ThisTask
==
0
:
print
(
'iccd-iwave-ipsf: {:4}{:4}{:4}'
.
format
(
iccd
,
iwave
,
ipsf
),
end
=
'
\r
'
,
flush
=
True
)
#if iccd != 1 or iwave !=1 or ipsf != 1:
# continue
ipsfOutput
=
psfMatPath
+
'/psf_{:}_centroidWgt.mat'
.
format
(
ipsf
)
#ipsfOutput = './psf_{:}_centroidWgt.mat'.format(ipsf)
psfInfo
=
myConfig
.
LoadPSF
(
iccd
,
iwave
,
ipsf
,
psfPath
,
InputMaxPixelPos
=
True
)
ipsfMat
=
psfInfo
[
'psfMat'
]
npix_y
,
npix_x
=
ipsfMat
.
shape
if
npsf
==
100
:
ncut
=
160
if
npsf
==
900
:
ncut
=
200
img
,
cx
,
cy
=
myUtil
.
centroidWgt
(
ipsfMat
,
nt
=
ncut
)
dcx
=
cx
-
npix_x
/
2
#pixel coords -> global coords
dcy
=-
(
cy
-
npix_y
/
2
)
#pixel coords -> global coords
dcx
*=
5e-3
#pixels -> mm
dcy
*=
5e-3
#pixels -> mm
nn
=
256
dn
=
int
((
nn
-
ncut
)
/
2
)
imgt
=
np
.
zeros
([
nn
,
nn
],
dtype
=
np
.
float32
)
imgt
[
dn
:
-
dn
,
dn
:
-
dn
]
=
img
scipy
.
io
.
savemat
(
ipsfOutput
,
{
'cx'
:
dcx
,
'cy'
:
dcy
,
'psf'
:
imgt
})
if
iccd
!=
1
or
iwave
!=
1
or
ipsf
!=
1
:
if
ThisTask
==
0
:
print
(
'CHECK::'
,
dcx
,
dcy
,
psfInfo
[
'centroid_x'
],
psfInfo
[
'centroid_y'
])
ObservationSim/PSF/PSFInterp_deprecated/PSFInterp_v2/PSFUtil.py
deleted
100644 → 0
View file @
e82a5dcc
import
sys
from
itertools
import
islice
import
numpy
as
np
#import matplotlib.pyplot as plt
import
scipy.io
#from scipy.io import loadmat
#import xlrd
from
scipy
import
ndimage
#from scipy.interpolate import RectBivariateSpline
import
scipy.spatial
as
spatial
#from astropy.modeling.models import Ellipse2D
#from astropy.coordinates import Angle
#import matplotlib.patches as mpatches
import
ctypes
#import galsim
###定义PSF像素的全局坐标###
def
psfPixelLayout
(
nrows
,
ncols
,
cenPosRow
,
cenPosCol
,
pixSizeInMicrons
=
5.0
):
"""
convert psf pixels to physical position
Parameters:
nrows, ncols (int, int): psf sampling with [nrows, ncols].
cenPosRow, cenPosCol (float, float): A physical position of the chief ray for a given psf.
pixSizeInMicrons (float-optional): The pixel size in microns from the psf sampling.
Returns:
psfPixelPos (numpy.array-float): [posx, posy] in mm for [irow, icol]
Notes:
1. show positions on ccd, but not position on image only [+/- dy]
"""
psfPixelPos
=
np
.
zeros
([
2
,
nrows
,
ncols
])
if
nrows
%
2
!=
0
:
sys
.
exit
()
if
ncols
%
2
!=
0
:
sys
.
exit
()
cenPix_row
=
nrows
/
2
+
1
#中心主光线对应pixle [由长光定义]
cenPix_col
=
ncols
/
2
+
1
for
irow
in
range
(
nrows
):
for
icol
in
range
(
ncols
):
delta_row
=
((
irow
+
1
)
-
cenPix_row
)
*
pixSizeInMicrons
*
1e-3
delta_col
=
((
icol
+
1
)
-
cenPix_col
)
*
pixSizeInMicrons
*
1e-3
psfPixelPos
[
0
,
irow
,
icol
]
=
cenPosCol
+
delta_col
psfPixelPos
[
1
,
irow
,
icol
]
=
cenPosRow
-
delta_row
#note-1:in CCD全局坐标
return
psfPixelPos
###查找最大pixel位置###
def
findMaxPix
(
img
):
"""
get the pixel position of the maximum-value
Parameters:
img (numpy.array-float): image
Returns:
imgMaxPix_x, imgMaxPix_y (int, int): pixel position in columns & rows
"""
maxIndx
=
np
.
argmax
(
img
)
maxIndx
=
np
.
unravel_index
(
maxIndx
,
np
.
array
(
img
).
shape
)
imgMaxPix_x
=
maxIndx
[
1
]
imgMaxPix_y
=
maxIndx
[
0
]
return
imgMaxPix_x
,
imgMaxPix_y
###查找neighbors位置###
def
findNeighbors
(
tx
,
ty
,
px
,
py
,
dr
=
0.1
,
dn
=
1
,
OnlyDistance
=
True
):
"""
find nearest neighbors by 2D-KDTree
Parameters:
tx, ty (float, float): a given position
px, py (numpy.array, numpy.array): position data for tree
dr (float-optional): distance
dn (int-optional): nearest-N
OnlyDistance (bool-optional): only use distance to find neighbors. Default: True
Returns:
dataq (numpy.array): index
"""
datax
=
px
datay
=
py
tree
=
spatial
.
KDTree
(
list
(
zip
(
datax
.
ravel
(),
datay
.
ravel
())))
dataq
=
[]
rr
=
dr
if
OnlyDistance
==
True
:
dataq
=
tree
.
query_ball_point
([
tx
,
ty
],
rr
)
if
OnlyDistance
==
False
:
while
len
(
dataq
)
<
dn
:
dataq
=
tree
.
query_ball_point
([
tx
,
ty
],
rr
)
rr
+=
dr
dd
=
np
.
hypot
(
datax
[
dataq
]
-
tx
,
datay
[
dataq
]
-
ty
)
ddSortindx
=
np
.
argsort
(
dd
)
dataq
=
np
.
array
(
dataq
)[
ddSortindx
[
0
:
dn
]]
return
dataq
###查找neighbors位置-hoclist###
def
hocBuild
(
partx
,
party
,
nhocx
,
nhocy
,
dhocx
,
dhocy
):
if
np
.
max
(
partx
)
>
nhocx
*
dhocx
:
print
(
'ERROR'
)
sys
.
exit
()
if
np
.
max
(
party
)
>
nhocy
*
dhocy
:
print
(
'ERROR'
)
sys
.
exit
()
npart
=
partx
.
size
hoclist
=
np
.
zeros
(
npart
,
dtype
=
np
.
int32
)
-
1
hoc
=
np
.
zeros
([
nhocy
,
nhocx
],
dtype
=
np
.
int32
)
-
1
for
ipart
in
range
(
npart
):
ix
=
int
(
partx
[
ipart
]
/
dhocx
)
iy
=
int
(
party
[
ipart
]
/
dhocy
)
hoclist
[
ipart
]
=
hoc
[
iy
,
ix
]
hoc
[
iy
,
ix
]
=
ipart
return
hoc
,
hoclist
def
hocFind
(
px
,
py
,
dhocx
,
dhocy
,
hoc
,
hoclist
):
ix
=
int
(
px
/
dhocx
)
iy
=
int
(
py
/
dhocy
)
neigh
=
[]
it
=
hoc
[
iy
,
ix
]
while
it
!=
-
1
:
neigh
.
append
(
it
)
it
=
hoclist
[
it
]
return
neigh
def
findNeighbors_hoclist
(
px
,
py
,
tx
=
None
,
ty
=
None
,
dn
=
4
,
hoc
=
None
,
hoclist
=
None
):
nhocy
=
nhocx
=
20
pxMin
=
np
.
min
(
px
)
pxMax
=
np
.
max
(
px
)
pyMin
=
np
.
min
(
py
)
pyMax
=
np
.
max
(
py
)
dhocx
=
(
pxMax
-
pxMin
)
/
(
nhocx
-
1
)
dhocy
=
(
pyMax
-
pyMin
)
/
(
nhocy
-
1
)
partx
=
px
-
pxMin
+
dhocx
/
2
party
=
py
-
pyMin
+
dhocy
/
2
if
hoc
is
None
:
hoc
,
hoclist
=
hocBuild
(
partx
,
party
,
nhocx
,
nhocy
,
dhocx
,
dhocy
)
return
hoc
,
hoclist
if
hoc
is
not
None
:
tx
=
tx
-
pxMin
+
dhocx
/
2
ty
=
ty
-
pyMin
+
dhocy
/
2
itx
=
int
(
tx
/
dhocx
)
ity
=
int
(
ty
/
dhocy
)
ps
=
[
-
1
,
0
,
1
]
neigh
=
[]
for
ii
in
range
(
3
):
for
jj
in
range
(
3
):
ix
=
itx
+
ps
[
ii
]
iy
=
ity
+
ps
[
jj
]
if
ix
<
0
:
continue
if
iy
<
0
:
continue
if
ix
>
nhocx
-
1
:
continue
if
iy
>
nhocy
-
1
:
continue
#neightt = myUtil.hocFind(ppx, ppy, dhocx, dhocy, hoc, hoclist)
it
=
hoc
[
iy
,
ix
]
while
it
!=
-
1
:
neigh
.
append
(
it
)
it
=
hoclist
[
it
]
#neigh.append(neightt)
#ll = [i for k in neigh for i in k]
if
dn
!=
-
1
:
ptx
=
np
.
array
(
partx
[
neigh
])
pty
=
np
.
array
(
party
[
neigh
])
dd
=
np
.
hypot
(
ptx
-
tx
,
pty
-
ty
)
idx
=
np
.
argsort
(
dd
)
neigh
=
np
.
array
(
neigh
)[
idx
[
0
:
dn
]]
return
neigh
###PSF中心对齐###
def
psfCentering
(
img
,
apSizeInArcsec
=
0.5
,
psfSampleSizeInMicrons
=
5
,
focalLengthInMeters
=
28
,
CenteringMode
=
1
):
"""
centering psf within an aperture
Parameters:
img (numpy.array): image
apSizeInArcsec (float-optional): aperture size in arcseconds.
psfSampleSizeInMicrons (float-optional): psf pixel size in microns.
focalLengthInMeters (float-optional): csst focal length im meters.
CenteringMode (int-optional): how to center psf images
Returns:
imgT (numpy.array)
"""
if
CenteringMode
==
1
:
imgMaxPix_x
,
imgMaxPix_y
=
findMaxPix
(
img
)
if
CenteringMode
==
2
:
y
,
x
=
ndimage
.
center_of_mass
(
img
)
#y-rows, x-cols
imgMaxPix_x
=
int
(
x
)
imgMaxPix_y
=
int
(
y
)
apSizeInMicrons
=
np
.
deg2rad
(
apSizeInArcsec
/
3600.
)
*
focalLengthInMeters
*
1e6
apSizeInPix
=
apSizeInMicrons
/
psfSampleSizeInMicrons
apSizeInPix
=
np
.
int
(
np
.
ceil
(
apSizeInPix
))
imgT
=
np
.
zeros_like
(
img
)
ngy
,
ngx
=
img
.
shape
cy
=
int
(
ngy
/
2
)
cx
=
int
(
ngx
/
2
)
imgT
[
cy
-
apSizeInPix
:
cy
+
apSizeInPix
+
1
,
cx
-
apSizeInPix
:
cx
+
apSizeInPix
+
1
]
=
\
img
[
imgMaxPix_y
-
apSizeInPix
:
imgMaxPix_y
+
apSizeInPix
+
1
,
imgMaxPix_x
-
apSizeInPix
:
imgMaxPix_x
+
apSizeInPix
+
1
]
return
imgT
###插值对齐-fft###
def
psfCentering_FFT
(
image
):
"""
centering psf within an aperture by FFT
"""
ny
,
nx
=
image
.
shape
py
,
px
=
ndimage
.
center_of_mass
(
image
)
dx
=
(
px
-
nx
/
2
)
dy
=
(
py
-
ny
/
2
)
k
=
np
.
zeros
((
nx
,
ny
,
2
),
dtype
=
float
)
fg
=
np
.
fft
.
fft2
(
image
)
ge
=
np
.
zeros_like
(
fg
)
inx
=
int
(
nx
/
2
)
jny
=
int
(
ny
/
2
)
#prepare for the phase multiply matrix
#left bottom
for
i
in
range
(
inx
+
1
):
for
j
in
range
(
jny
+
1
):
k
[
i
][
j
][
0
]
=
i
;
k
[
i
][
j
][
1
]
=
j
;
#top right
for
i
in
range
(
inx
-
1
):
for
j
in
range
(
jny
-
1
):
k
[
i
+
inx
+
1
][
j
+
jny
+
1
][
0
]
=
(
-
(
nx
/
2
-
1
)
+
i
)
k
[
i
+
inx
+
1
][
j
+
jny
+
1
][
1
]
=
(
-
(
ny
/
2
-
1
)
+
j
)
#bottom right
for
i
in
range
(
inx
+
1
):
for
j
in
range
(
jny
-
1
):
k
[
i
][
j
+
jny
+
1
][
0
]
=
i
k
[
i
][
j
+
jny
+
1
][
1
]
=
(
-
(
ny
/
2
-
1
)
+
j
)
#top left
for
i
in
range
(
inx
-
1
):
for
j
in
range
(
int
(
jny
+
1
)):
k
[
i
+
inx
+
1
][
j
][
0
]
=
(
-
(
nx
/
2
-
1
)
+
i
)
k
[
i
+
inx
+
1
][
j
][
1
]
=
j
for
i
in
range
(
nx
):
for
j
in
range
(
ny
):
ge
[
i
][
j
]
=
fg
[
i
][
j
]
*
np
.
exp
(
2.
*
np
.
pi
*
(
dx
*
k
[
i
][
j
][
0
]
/
nx
+
dy
*
k
[
i
][
j
][
1
]
/
ny
)
*
1j
)
get
=
np
.
fft
.
ifft2
(
ge
).
real
return
(
get
)
###图像叠加###
def
psfStack
(
*
psfMat
):
"""
stacked image from the different psfs
Parameters:
*psfMat (numpy.array): the different psfs for stacking
Returns:
img (numpy.array): image
"""
nn
=
len
(
psfMat
)
img
=
np
.
zeros_like
(
psfMat
[
0
])
for
ii
in
range
(
nn
):
img
+=
psfMat
[
ii
]
/
np
.
sum
(
psfMat
[
ii
])
img
/=
np
.
sum
(
img
)
return
img
###计算PSF椭率-接口###
def
psfSizeCalculator
(
psfMat
,
psfSampleSize
=
5
,
CalcPSFcenter
=
True
,
SigRange
=
True
,
TailorScheme
=
2
):
"""
calculate psf size & ellipticity
Parameters:
psfMat (numpy.array): image
psfSampleSize (float-optional): psf size in microns.
CalcPSFcenter (bool-optional): whether calculate psf center. Default: True
SigRange (bool-optional): whether use psf tailor. Default: False
TailorScheme (int-optional): which method for psf tailor. Default: 1
Returns:
cenX, cenY (float, float): the pixel position of the mass center
sz (float): psf size
e1, e2 (float, float): psf ellipticity
REE80 (float): radius of REE80 in arcseconds
"""
psfSampleSize
=
psfSampleSize
*
1e-3
#mm
REE80
=
-
1.0
##encircling 80% energy
if
SigRange
is
True
:
if
TailorScheme
==
1
:
psfMat
=
imSigRange
(
psfMat
,
fraction
=
0.80
)
psfInfo
[
'psfMat'
]
=
psfMat
#set on/off
if
TailorScheme
==
2
:
#img = psfTailor(psfMat, apSizeInArcsec=0.5)
imgX
,
REE80
=
psfEncircle
(
psfMat
)
#psfMat = img
REE80
=
REE80
[
0
]
if
CalcPSFcenter
is
True
:
img
=
psfMat
/
np
.
sum
(
psfMat
)
y
,
x
=
ndimage
.
center_of_mass
(
img
)
#y-rows, x-cols
cenX
=
x
cenY
=
y
if
CalcPSFcenter
is
False
:
cenPix_X
=
psfMat
.
shape
[
1
]
/
2
#90
cenPix_Y
=
psfMat
.
shape
[
0
]
/
2
#90
cenX
=
cenPix_X
+
psfInfo
[
'centroid_x'
]
/
psfSampleSize
cenY
=
cenPix_Y
-
psfInfo
[
'centroid_y'
]
/
psfSampleSize
pixSize
=
1
sz
,
e1
,
e2
=
psfSecondMoments
(
psfMat
,
cenX
,
cenY
,
pixSize
=
pixSize
)
return
cenX
,
cenY
,
sz
,
e1
,
e2
,
REE80
###计算PSF椭率###
def
psfSecondMoments
(
psfMat
,
cenX
,
cenY
,
pixSize
=
1
):
"""
estimate the psf ellipticity by the second moment of surface brightness
Parameters:
psfMat (numpy.array-float): image
cenX, cenY (float, float): pixel position of the psf center
pixSize (float-optional): pixel size
Returns:
sz (float): psf size
e1, e2 (float, float): psf ellipticity
"""
apr
=
0.5
#arcsec, 0.5角秒内测量
fl
=
28.
#meters
pxs
=
5.0
#microns
apr
=
np
.
deg2rad
(
apr
/
3600.
)
*
fl
*
1e6
apr
=
apr
/
pxs
apr
=
np
.
int
(
np
.
ceil
(
apr
))
I
=
psfMat
ncol
=
I
.
shape
[
1
]
nrow
=
I
.
shape
[
0
]
w
=
0.0
w11
=
0.0
w12
=
0.0
w22
=
0.0
for
icol
in
range
(
ncol
):
for
jrow
in
range
(
nrow
):
x
=
icol
*
pixSize
-
cenX
y
=
jrow
*
pixSize
-
cenY
rr
=
np
.
sqrt
(
x
*
x
+
y
*
y
)
wgt
=
0.0
if
rr
<=
apr
:
wgt
=
1.0
w
+=
I
[
jrow
,
icol
]
*
wgt
w11
+=
x
*
x
*
I
[
jrow
,
icol
]
*
wgt
w12
+=
x
*
y
*
I
[
jrow
,
icol
]
*
wgt
w22
+=
y
*
y
*
I
[
jrow
,
icol
]
*
wgt
w11
/=
w
w12
/=
w
w22
/=
w
sz
=
w11
+
w22
e1
=
(
w11
-
w22
)
/
sz
e2
=
2.0
*
w12
/
sz
return
sz
,
e1
,
e2
###计算REE80###
def
psfEncircle
(
img
,
fraction
=
0.8
,
psfSampleSizeInMicrons
=
5
,
focalLengthInMeters
=
28
):
"""
psf tailor within a given percentage.
Parameters:
img (numpy.array-float): image
fraction (float-optional): a percentage for psf tailor.
psfSampleSizeInMicrons (float-optional): psf pixel size in microns.
focalLengthInMeters (float-optional): csst focal length im meters.
Returns:
img*wgt (numpy.array-float): image
REE80 (float): radius of REE80 in arcseconds.
"""
#imgMaxPix_x, imgMaxPix_y = findMaxPix(img)
y
,
x
=
ndimage
.
center_of_mass
(
img
)
#y-rows, x-cols
imgMaxPix_x
=
int
(
x
)
imgMaxPix_y
=
int
(
y
)
im1
=
img
.
copy
()
im1size
=
im1
.
shape
dis
=
np
.
zeros_like
(
img
)
for
irow
in
range
(
im1size
[
0
]):
for
icol
in
range
(
im1size
[
1
]):
dx
=
icol
-
imgMaxPix_x
dy
=
irow
-
imgMaxPix_y
dis
[
irow
,
icol
]
=
np
.
hypot
(
dx
,
dy
)
nn
=
im1size
[
1
]
*
im1size
[
0
]
disX
=
dis
.
reshape
(
nn
)
disXsortId
=
np
.
argsort
(
disX
)
imgX
=
img
.
reshape
(
nn
)
imgY
=
imgX
[
disXsortId
]
psfFrac
=
np
.
cumsum
(
imgY
)
/
np
.
sum
(
imgY
)
ind
=
np
.
where
(
psfFrac
>
fraction
)[
0
][
0
]
wgt
=
np
.
ones_like
(
dis
)
wgt
[
np
.
where
(
dis
>
dis
[
np
.
where
(
img
==
imgY
[
ind
])])]
=
0
REE80
=
np
.
rad2deg
(
dis
[
np
.
where
(
img
==
imgY
[
ind
])]
*
psfSampleSizeInMicrons
*
1e-6
/
focalLengthInMeters
)
*
3600
return
img
*
wgt
,
REE80
###图像能量百分比查找###
def
imSigRange
(
img
,
fraction
=
0.80
):
"""
extract the image within x-percent (DISCARD)
Parameters:
img (numpy.array-float): image
fraction (float-optional): a percentage
Returns:
im1 (numpy.array-float): image
"""
im1
=
img
.
copy
()
im1size
=
im1
.
shape
im2
=
np
.
sort
(
im1
.
reshape
(
im1size
[
0
]
*
im1size
[
1
]))
im2
=
im2
[::
-
1
]
im3
=
np
.
cumsum
(
im2
)
/
np
.
sum
(
im2
)
loc
=
np
.
where
(
im3
>
fraction
)
#print(im3[loc[0][0]], im2[loc[0][0]])
im1
[
np
.
where
(
im1
<=
im2
[
loc
[
0
][
0
]])]
=
0
return
im1
###孔径内图像裁剪###
def
psfTailor
(
img
,
apSizeInArcsec
=
0.5
,
psfSampleSizeInMicrons
=
5
,
focalLengthInMeters
=
28
):
"""
psf tailor within a given aperture size
Parameters:
img (numpy.array-float): image
apSizeInArcsec (float-optional): aperture size in arcseconds.
psfSampleSizeInMicrons (float-optional): psf pixel size in microns.
focalLengthInMeters (float-optional): csst focal length im meters.
Returns:
imgT (numpy.array-float): image
"""
#imgMaxPix_x, imgMaxPix_y = findMaxPix(img)
y
,
x
=
ndimage
.
center_of_mass
(
img
)
#y-rows, x-cols
imgMaxPix_x
=
int
(
x
)
imgMaxPix_y
=
int
(
y
)
apSizeInMicrons
=
np
.
deg2rad
(
apSizeInArcsec
/
3600.
)
*
focalLengthInMeters
*
1e6
apSizeInPix
=
apSizeInMicrons
/
psfSampleSizeInMicrons
apSizeInPix
=
np
.
int
(
np
.
ceil
(
apSizeInPix
))
imgT
=
np
.
zeros_like
(
img
)
imgT
[
imgMaxPix_y
-
apSizeInPix
:
imgMaxPix_y
+
apSizeInPix
+
1
,
imgMaxPix_x
-
apSizeInPix
:
imgMaxPix_x
+
apSizeInPix
+
1
]
=
\
img
[
imgMaxPix_y
-
apSizeInPix
:
imgMaxPix_y
+
apSizeInPix
+
1
,
imgMaxPix_x
-
apSizeInPix
:
imgMaxPix_x
+
apSizeInPix
+
1
]
return
imgT
###centroid with a window###
def
centroidWgt
(
img
,
nt
=
160
):
#libCentroid = ctypes.CDLL('/public/home/weichengliang/lnData/CSST_new_framwork/csstPSF/libCentroid/libCentroid.so') # CDLL加载库
libCentroid
=
ctypes
.
CDLL
(
'./libCentroid/libCentroid.so'
)
# CDLL加载库
libCentroid
.
centroidWgt
.
argtypes
=
[
ctypes
.
POINTER
(
ctypes
.
c_float
),
ctypes
.
c_int
,
ctypes
.
c_int
,
ctypes
.
POINTER
(
ctypes
.
c_double
)]
libCentroid
.
imSplint
.
argtypes
=
[
ctypes
.
POINTER
(
ctypes
.
c_float
),
ctypes
.
c_int
,
ctypes
.
c_int
,
ctypes
.
POINTER
(
ctypes
.
c_double
),
ctypes
.
c_int
,
ctypes
.
c_int
,
ctypes
.
POINTER
(
ctypes
.
c_float
)]
imx
=
img
/
np
.
sum
(
img
)
ny
,
nx
=
imx
.
shape
#imx centroid
nn
=
nx
*
ny
arr
=
(
ctypes
.
c_float
*
nn
)()
arr
[:]
=
imx
.
reshape
(
nn
)
para
=
(
ctypes
.
c_double
*
10
)()
libCentroid
.
centroidWgt
(
arr
,
ny
,
nx
,
para
)
imx_cy
=
para
[
3
]
#irow
imx_cx
=
para
[
4
]
#icol
#imx -> imy
nxt
=
nyt
=
nt
nn
=
nxt
*
nyt
yat
=
(
ctypes
.
c_float
*
nn
)()
libCentroid
.
imSplint
(
arr
,
ny
,
nx
,
para
,
nxt
,
nyt
,
yat
)
imy
=
np
.
array
(
yat
[:]).
reshape
([
nxt
,
nyt
])
return
imy
,
imx_cx
,
imx_cy
'''
def psfCentering_wgt(ipsfMat, psf_image_x, psf_image_y, psfSampleSizeInMicrons=5.0):
img, cx, cy = centroidWgt(ipsfMat, nt=160)
nrows, ncols = ipsfMat.shape
cyt = (cy + nrows/2)*psfSampleSizeInMicrons*1e-3 +psf_image_y
cxt = (cx + ncols/2)*psfSampleSizeInMicrons*1e-3 +psf_image_x
return img, cxt, cyt
'''
ObservationSim/PSF/PSFInterp_deprecated/PSFInterp_v2/__init__.py
deleted
100644 → 0
View file @
e82a5dcc
from
.PSFInterp
import
PSFInterp
\ No newline at end of file
ObservationSim/PSF/PSFInterp_deprecated/PSFInterp_v2/__pycache__/PSFConfig.cpython-37.pyc
deleted
100644 → 0
View file @
e82a5dcc
File deleted
ObservationSim/PSF/PSFInterp_deprecated/PSFInterp_v2/__pycache__/PSFConfig.cpython-38.pyc
deleted
100644 → 0
View file @
e82a5dcc
File deleted
ObservationSim/PSF/PSFInterp_deprecated/PSFInterp_v2/__pycache__/PSFInterp.cpython-38.pyc
deleted
100644 → 0
View file @
e82a5dcc
File deleted
ObservationSim/PSF/PSFInterp_deprecated/PSFInterp_v2/__pycache__/PSFUtil.cpython-37.pyc
deleted
100644 → 0
View file @
e82a5dcc
File deleted
ObservationSim/PSF/PSFInterp_deprecated/PSFInterp_v2/__pycache__/PSFUtil.cpython-38.pyc
deleted
100644 → 0
View file @
e82a5dcc
File deleted
ObservationSim/PSF/PSFInterp_deprecated/PSFInterp_v2/__pycache__/__init__.cpython-38.pyc
deleted
100644 → 0
View file @
e82a5dcc
File deleted
ObservationSim/PSF/PSFInterp_deprecated/PSFInterp_v2/libCentroid/.DS_Store
deleted
100644 → 0
View file @
e82a5dcc
File deleted
ObservationSim/PSF/PSFInterp_deprecated/PSFInterp_v2/libCentroid/Makefile
deleted
100644 → 0
View file @
e82a5dcc
#OPTS += -D
CC
=
gcc
OPTIMIZE
=
-fPIC
-g
-O3
#-Wall -wd981 #-wd1419 -wd810
#GSLI = -I/home/alex/opt/gsl/include
#GSLL = -L/home/alex/opt/gsl/lib -lgsl -lgslcblas
#FFTWI = -I/home/alex/opt/fftw/include
#FFTWL = -L/home/alex/opt/fftw/lib -lfftw3 -lfftw3f
#HDF5I = -I/home/alex/opt/hdf5/include
#HDF5L = -L/home/alex/opt/hdf5/lib -lhdf5_hl -lhdf5
#FITSI = -I/home/alex/opt/cfitsio/include
#FITSL = -L/home/alex/opt/cfitsio/lib -lcfitsio
#EXTRACFLAGS =
#EXTRACLIB =
CLINK
=
$(CC)
CFLAGS
=
$(OPTIMIZE)
#
$(EXTRACFLAGS)
$(OPTS)
CLIB
=
-shared
-lm
#
$(EXTRACLIB)
OBJS
=
centroidWgt.o nrutil.o
EXEC
=
libCentroid.so
all
:
$(EXEC)
$(EXEC)
:
$(OBJS)
$(CLINK)
$(CFLAGS)
-o
$@
$(OBJS)
$(CLIB)
$(OBJS)
:
nrutil.h Makefile
.PHONY
:
clean
clean
:
rm
-f
*
.o
$(EXEC)
ObservationSim/PSF/PSFInterp_deprecated/PSFInterp_v2/libCentroid/centroidWgt.c
deleted
100644 → 0
View file @
e82a5dcc
#include
<math.h>
#include
<stdio.h>
#include
<stdlib.h>
#include
"nrutil.h"
//y is the input image, nx and ny are the pixels along x and y axis;
//yt and residu are the required matrix for the function
//para contains the needed parameters: centx=para[3], centy=para[4]
void
star_gaus
(
float
**
y
,
int
nx
,
int
ny
,
double
*
para
);
void
Interp_bicubic
(
int
nx
,
int
ny
,
float
**
a0
,
int
nbound
,
int
nxt
,
int
nyt
,
float
**
at
,
double
*
xcen
);
void
splie2
(
float
**
ya
,
int
m
,
int
n
,
float
**
y2a
);
void
spline
(
float
y
[],
int
n
,
float
yp1
,
float
ypn
,
float
y2
[]);
void
splint
(
float
ya
[],
float
y2a
[],
int
n
,
float
x
,
float
*
y
);
void
centroidWgt
(
float
*
y
,
int
nx
,
int
ny
,
double
*
para
)
{
int
i
,
j
,
k
;
float
**
yy
;
double
**
basef
;
double
**
coff
;
double
ccol
,
crow
;
yy
=
matrix
(
0
,
nx
-
1
,
0
,
ny
-
1
);
for
(
i
=
0
;
i
<
nx
;
i
++
)
for
(
j
=
0
;
j
<
ny
;
j
++
)
{
k
=
j
+
i
*
nx
;
yy
[
i
][
j
]
=
y
[
k
];
}
star_gaus
(
yy
,
nx
,
ny
,
para
);
//ccol = para[4];
//crow = para[3];
}
void
imSplint
(
float
*
y
,
int
nx
,
int
ny
,
double
*
para
,
int
nxt
,
int
nyt
,
float
*
yat
)
{
int
i
,
j
,
k
;
float
**
yy
;
double
**
basef
;
double
**
coff
;
double
ccol
,
crow
;
yy
=
matrix
(
0
,
nx
-
1
,
0
,
ny
-
1
);
for
(
i
=
0
;
i
<
nx
;
i
++
)
for
(
j
=
0
;
j
<
ny
;
j
++
)
{
k
=
j
+
i
*
nx
;
yy
[
i
][
j
]
=
y
[
k
];
}
crow
=
para
[
3
];
ccol
=
para
[
4
];
int
nbound
;
float
**
at
;
double
xcen
[
2
];
at
=
matrix
(
0
,
nxt
-
1
,
0
,
nyt
-
1
);
nbound
=
(
int
)((
nx
-
nxt
)
/
2
);
xcen
[
0
]
=
crow
;
xcen
[
1
]
=
ccol
;
Interp_bicubic
(
nx
,
ny
,
yy
,
nbound
,
nxt
,
nyt
,
at
,
xcen
);
for
(
i
=
0
;
i
<
nxt
;
i
++
)
for
(
j
=
0
;
j
<
nyt
;
j
++
)
{
k
=
j
+
i
*
nxt
;
yat
[
k
]
=
at
[
i
][
j
];
}
}
//void star_gaus(float **y,int nx,int ny,float **yt,float **residu,double *para)
void
star_gaus
(
float
**
y
,
int
nx
,
int
ny
,
double
*
para
)
{
void
gasfit_2D
(
double
**
x
,
double
*
y
,
int
np
,
double
*
para
,
double
bg0
,
int
fbg
);
double
**
xs
,
*
ys
,
ymax
;
int
i
,
j
,
np
,
npt
,
im
,
jm
,
**
xi
,
k
;
double
kc
,
bgc
,
sigmac
,
xc
,
yc
,
sigma2v
,
det
;
double
bg0
=
0
;
int
fbg
=
0
;
np
=
nx
*
ny
;
xs
=
dmatrix
(
0
,
np
-
1
,
0
,
1
);
ys
=
dvector
(
0
,
np
-
1
);
xi
=
imatrix
(
0
,
np
-
1
,
0
,
1
);
ymax
=
y
[
0
][
0
];
for
(
i
=
0
;
i
<
nx
;
i
++
)
for
(
j
=
0
;
j
<
ny
;
j
++
){
if
(
ymax
<
y
[
i
][
j
]){
ymax
=
y
[
i
][
j
];
im
=
i
;
jm
=
j
;}
}
int
cutPix
;
cutPix
=
23
;
npt
=
0
;
for
(
i
=-
cutPix
;
i
<=
cutPix
;
i
++
){
for
(
j
=-
cutPix
;
j
<=
cutPix
;
j
++
){
xs
[
npt
][
0
]
=
xi
[
npt
][
0
]
=
i
+
im
;
xs
[
npt
][
1
]
=
xi
[
npt
][
1
]
=
j
+
jm
;
ys
[
npt
]
=
y
[
im
+
i
][
jm
+
j
];
npt
++
;
}
}
gasfit_2D
(
xs
,
ys
,
npt
,
para
,
bg0
,
fbg
);
kc
=
para
[
0
];
sigmac
=
para
[
1
];
bgc
=
para
[
2
];
xc
=
para
[
3
];
yc
=
para
[
4
];
// printf("%e %e %e %e %e\n",kc,sigmac,bgc,xc,yc);
/*
sigma2v=-1./(2.*sigmac*sigmac);
for(i=0;i<nx;i++){
for(j=0;j<ny;j++){
det=DSQR(i-xc)+DSQR(j-yc);
yt[i][j]=kc*exp(det*sigma2v)+bgc;
residu[i][j]=y[i][j]-yt[i][j];
}
}
*/
free_dmatrix
(
xs
,
0
,
np
-
1
,
0
,
1
);
free_imatrix
(
xi
,
0
,
np
-
1
,
0
,
1
);
free_dvector
(
ys
,
0
,
np
-
1
);
}
void
gasfit_2D
(
double
**
x
,
double
*
y
,
int
np
,
double
*
para
,
double
bg0
,
int
fbg
)
{
void
search_2D
(
double
**
x
,
double
*
y
,
int
np
,
double
kc
,
double
kd
,
double
sigmac
,
double
sigmad
,
double
bgc
,
double
bgd
,
double
xc
,
double
xd
,
double
yc
,
double
yd
,
double
*
para
,
double
bg0
,
int
fbg
);
int
i
,
j
,
k
,
imax
=
0
,
isigma
;
double
ymax
,
ymin
,
kc
,
kd
,
sigmac
,
sigmad
,
bgc
,
bgd
,
ysigma
,
xc
,
yc
,
xd
,
yd
;
double
det
,
dett
;
ymin
=
ymax
=
y
[
imax
];
for
(
i
=
1
;
i
<
np
;
i
++
){
if
(
ymax
<
y
[
i
]){
imax
=
i
;
ymax
=
y
[
i
];}
if
(
ymin
>
y
[
i
])
ymin
=
y
[
i
];
}
kc
=
ymax
;
kd
=
ymax
/
12
.;
det
=
ysigma
=
kc
*
exp
(
-
0
.
5
);
for
(
i
=
0
;
i
<
np
;
i
++
){
dett
=
fabs
(
ysigma
-
y
[
i
]);
if
(
dett
<
det
&&
y
[
i
]
!=
kc
){
det
=
dett
;
isigma
=
i
;}
//if(dett<det){det=dett;isigma=i;}
}
xc
=
x
[
imax
][
0
];
yc
=
x
[
imax
][
1
];
sigmac
=
sqrt
(
DSQR
(
xc
-
x
[
isigma
][
0
])
+
DSQR
(
yc
-
x
[
isigma
][
1
]))
*
1
.;
xd
=
yd
=
sigmac
*
0
.
25
;
sigmad
=
0
.
25
*
sigmac
;
bgc
=
0
.;
bgd
=
fabs
(
ymin
);
for
(
i
=
0
;
i
<
4
;
i
++
){
search_2D
(
x
,
y
,
np
,
kc
,
kd
,
sigmac
,
sigmad
,
bgc
,
bgd
,
xc
,
xd
,
yc
,
yd
,
para
,
bg0
,
fbg
);
kd
*=
0
.
33
;
sigmad
*=
0
.
33
;
bgd
*=
0
.
33
;
xd
*=
0
.
33
;
yd
*=
0
.
33
;
kc
=
para
[
0
];
sigmac
=
para
[
1
];
bgc
=
para
[
2
];
xc
=
para
[
3
];
yc
=
para
[
4
];
}
if
(
fbg
==
0
)
para
[
2
]
=
bg0
;
}
void
search_2D
(
double
**
x
,
double
*
y
,
int
np
,
double
kc
,
double
kd
,
double
sigmac
,
double
sigmad
,
double
bgc
,
double
bgd
,
double
xc
,
double
xd
,
double
yc
,
double
yd
,
double
*
para
,
double
bg0
,
int
fbg
)
{
double
k
,
sigma
,
bg
,
k2
,
k20
,
sigma2v
,
det
,
xt
,
yt
;
int
i
,
j
,
l
,
m
,
p
,
q
;
sigma2v
=-
1
.
/
(
2
.
*
sigmac
*
sigmac
);
bg
=
bgc
;
k20
=
0
;
for
(
m
=
0
;
m
<
np
;
m
++
){
det
=
DSQR
(
x
[
m
][
0
]
-
xc
)
+
DSQR
(
x
[
m
][
1
]
-
yc
);
det
=
kc
*
exp
(
det
*
sigma2v
)
+
bgc
-
y
[
m
];
k20
+=
det
*
det
;
}
for
(
i
=-
4
;
i
<=
4
;
i
++
){
k
=
kc
+
i
*
kd
;
if
(
k
>
0
){
for
(
j
=-
4
;
j
<=
4
;
j
++
){
sigma
=
sigmac
+
j
*
sigmad
;
if
(
sigma
>
0
){
sigma2v
=-
1
.
/
(
2
.
*
sigma
*
sigma
);
for
(
p
=-
4
;
p
<=
4
;
p
++
){
xt
=
xc
+
p
*
xd
;
for
(
q
=-
4
;
q
<=
4
;
q
++
){
yt
=
yc
+
q
*
yd
;
k2
=
0
;
if
(
fbg
==
0
){
bg
=
bg0
;
for
(
m
=
0
;
m
<
np
;
m
++
){
det
=
DSQR
(
x
[
m
][
0
]
-
xt
)
+
DSQR
(
x
[
m
][
1
]
-
yt
);
det
=
k
*
exp
(
det
*
sigma2v
)
+
bg
-
y
[
m
];
k2
+=
det
*
det
;
}
}
else
{
for
(
l
=-
4
;
l
<=
4
;
l
++
){
bg
=
bgc
+
l
*
bgd
;
for
(
m
=
0
;
m
<
np
;
m
++
){
det
=
DSQR
(
x
[
m
][
0
]
-
xt
)
+
DSQR
(
x
[
m
][
1
]
-
yt
);
det
=
k
*
exp
(
det
*
sigma2v
)
+
bg
-
y
[
m
];
k2
+=
det
*
det
;
}
}
}
//printf("k20=%e k2=%e\n",k20,k2);
if
(
k2
<=
k20
){
k20
=
k2
;
para
[
5
]
=
k2
;
para
[
0
]
=
k
;
para
[
1
]
=
sigma
;
para
[
2
]
=
bg
;
para
[
3
]
=
xt
;
para
[
4
]
=
yt
;}
}
}
}
}
}
}
}
/////////////////////////////
//#include <math.h>
//#include <stdio.h>
//#include <stdlib.h>
//#include "nrutil.h"
//nx,ny is the pixels of x and y direction
//a0 is the input image in nx*ny
//nbound is the boundary limit
//nxt,nyt is out put image dimentions
//at is the output image and cen represent the required center
void
Interp_bicubic
(
int
nx
,
int
ny
,
float
**
a0
,
int
nbound
,
int
nxt
,
int
nyt
,
float
**
at
,
double
*
xcen
)
{
void
splie2
(
float
**
ya
,
int
m
,
int
n
,
float
**
y2a
);
void
spline
(
float
y
[],
int
n
,
float
yp1
,
float
ypn
,
float
y2
[]);
void
splint
(
float
ya
[],
float
y2a
[],
int
n
,
float
x
,
float
*
y
);
int
i
,
j
,
m
,
n
,
ic
,
jc
;
float
*
ytmp
,
*
yytmp
,
**
y2a
,
x1
,
x2
,
shift1
,
shift2
;
y2a
=
matrix
(
0
,
nx
,
0
,
ny
);
ytmp
=
vector
(
0
,
ny
);
yytmp
=
vector
(
0
,
ny
);
splie2
(
a0
,
nx
,
ny
,
y2a
);
ic
=
nx
*
0
.
5
;
jc
=
ny
*
0
.
5
;
shift1
=
xcen
[
0
]
-
ic
;
//-0.5;
shift2
=
xcen
[
1
]
-
jc
;
//-0.5;
if
(
fabs
(
shift1
)
>
nbound
||
fabs
(
shift2
)
>
nbound
){
printf
(
"bicubic shifts too much %e %e
\n
"
,
shift1
,
shift2
);
getchar
();
}
for
(
n
=
0
;
n
<
nyt
;
n
++
){
x2
=
n
+
nbound
+
shift2
;
for
(
i
=
0
;
i
<
nx
;
i
++
){
splint
(
a0
[
i
],
y2a
[
i
],
ny
,
x2
,
&
yytmp
[
i
]);
spline
(
yytmp
,
ny
,
1.0e30
,
1.0e30
,
ytmp
);
}
for
(
m
=
0
;
m
<
nxt
;
m
++
){
x1
=
m
+
nbound
+
shift1
;
splint
(
yytmp
,
ytmp
,
ny
,
x1
,
&
at
[
m
][
n
]);
}
}
free_vector
(
yytmp
,
0
,
ny
);
free_vector
(
ytmp
,
0
,
ny
);
free_matrix
(
y2a
,
0
,
nx
,
0
,
ny
);
}
void
splie2
(
float
**
ya
,
int
m
,
int
n
,
float
**
y2a
)
{
void
spline
(
float
y
[],
int
n
,
float
yp1
,
float
ypn
,
float
y2
[]);
int
j
;
for
(
j
=
0
;
j
<
m
;
j
++
)
spline
(
ya
[
j
],
n
,
1.0e30
,
1.0e30
,
y2a
[
j
]);
}
void
spline
(
float
y
[],
int
n
,
float
yp1
,
float
ypn
,
float
y2
[])
{
int
i
,
k
;
float
p
,
qn
,
sig
,
un
,
*
u
;
u
=
vector
(
0
,
n
-
1
);
if
(
yp1
>
0.99e30
)
y2
[
0
]
=
u
[
0
]
=
0
.
0
;
else
{
y2
[
0
]
=
-
0
.
5
;
u
[
0
]
=
3
.
0
*
(
y
[
1
]
-
y
[
0
]
-
yp1
);
}
sig
=
0
.
5
;
for
(
i
=
1
;
i
<=
n
-
2
;
i
++
)
{
p
=
sig
*
y2
[
i
-
1
]
+
2
.
0
;
y2
[
i
]
=
(
sig
-
1
.
0
)
/
p
;
u
[
i
]
=
(
y
[
i
+
1
]
-
2
.
*
y
[
i
]
+
y
[
i
-
1
]);
u
[
i
]
=
(
3
.
0
*
u
[
i
]
-
sig
*
u
[
i
-
1
])
/
p
;
}
if
(
ypn
>
0.99e30
)
qn
=
un
=
0
.
0
;
else
{
qn
=
0
.
5
;
un
=
3
.
0
*
(
ypn
-
y
[
n
-
1
]
+
y
[
n
-
2
]);
}
y2
[
n
-
1
]
=
(
un
-
qn
*
u
[
n
-
2
])
/
(
qn
*
y2
[
n
-
2
]
+
1
.
0
);
for
(
k
=
n
-
2
;
k
>=
0
;
k
--
)
y2
[
k
]
=
y2
[
k
]
*
y2
[
k
+
1
]
+
u
[
k
];
free_vector
(
u
,
0
,
n
-
1
);
}
void
splint
(
float
ya
[],
float
y2a
[],
int
n
,
float
x
,
float
*
y
)
{
void
nrerror
(
char
error_text
[]);
int
klo
,
khi
,
k
;
float
h
,
b
,
a
;
klo
=
x
;
if
(
klo
<
0
)
klo
=
0
;
if
(
klo
==
n
-
1
)
klo
=
n
-
2
;
khi
=
klo
+
1
;
if
(
klo
<
0
||
khi
>=
n
)
printf
(
"error:klo, khi, n: %d %d %d
\n
"
,
klo
,
khi
,
n
);
if
(
klo
<
0
||
khi
>=
n
)
nrerror
(
"Bad xa input to routine splint"
);
a
=
(
khi
-
x
);
b
=
(
x
-
klo
);
*
y
=
a
*
ya
[
klo
]
+
b
*
ya
[
khi
]
+
((
a
*
a
*
a
-
a
)
*
y2a
[
klo
]
+
(
b
*
b
*
b
-
b
)
*
y2a
[
khi
])
/
6
.
0
;
}
ObservationSim/PSF/PSFInterp_deprecated/PSFInterp_v2/libCentroid/centroidWgt.o
deleted
100644 → 0
View file @
e82a5dcc
File deleted
ObservationSim/PSF/PSFInterp_deprecated/PSFInterp_v2/libCentroid/libCentroid.so
deleted
100755 → 0
View file @
e82a5dcc
File deleted
Prev
1
2
3
4
5
…
20
Next
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment