Commit 45682570 authored by Zhang Xin's avatar Zhang Xin
Browse files

pep8

parent d10eb6a8
Pipeline #7114 failed with stage
in 0 seconds
......@@ -336,7 +336,7 @@ class SpecDisperser(object):
orders = {"A": "1st", "B": "0st", "C": "2st", "D": "-1st", "E": "-2st"}
sens_file_name = conffile[0:-5] + \
"_sensitivity_" + orders[beam] + ".fits"
if not os.path.exists(sens_file_name) == True:
if os.path.exists(sens_file_name) if False:
senstivity_out = Table(
array([w, sens]).T, names=("WAVELENGTH", "SENSITIVITY"))
senstivity_out.write(sens_file_name, format="fits")
......
......@@ -8,16 +8,16 @@ import numpy
extensions = [
Extension("disperse_c.interp", ["disperse_c/interp.pyx"],
include_dirs = [numpy.get_include()],
include_dirs=[numpy.get_include()],
libraries=["m"]),
Extension("disperse_c.disperse", ["disperse_c/disperse.pyx"],
include_dirs = [numpy.get_include()],
include_dirs=[numpy.get_include()],
libraries=["m"]),
]
setup(
name = "slssim_disperse",
ext_modules = cythonize(extensions),
name="slssim_disperse",
ext_modules=cythonize(extensions),
)
......@@ -435,14 +435,15 @@ class PSFInterpSLS(PSFModel):
# PSF_int_trans[ids_szero] = 0
# print(PSF_int_trans[ids_szero].shape[0],PSF_int_trans.shape)
PSF_int_trans = PSF_int_trans/np.sum(PSF_int_trans)
###DEBGU
ids_szero = PSF_int_trans<0
# DEBGU
ids_szero = PSF_int_trans < 0
n01 = PSF_int_trans[ids_szero].shape[0]
n1 = np.sum(np.isinf(PSF_int_trans))
n2 = np.sum(np.isnan(PSF_int_trans))
if n1>0 or n2>0:
print("DEBUG: PSFInterpSLS, inf:%d, nan:%d, 0 num:%d"%(n1, n2, n01))
if n1 > 0 or n2 > 0:
print("DEBUG: PSFInterpSLS, inf:%d, nan:%d, 0 num:%d" %
(n1, n2, n01))
####
# from astropy.io import fits
......@@ -537,19 +538,20 @@ class PSFInterpSLS(PSFModel):
sumImg = np.sum(cutImg.array)
tmp_img = cutImg*0
for j in np.arange(npc):
X_ = np.hstack((pos_p[:,1].flatten()[:, None], pos_p[:,0].flatten()[:, None]),dtype=np.float32)
X_ = np.hstack((pos_p[:, 1].flatten()[:, None], pos_p[:, 0].flatten()[
:, None]), dtype=np.float32)
Z_ = (pc_coeff[j].astype(np.float32)).flatten()
# print(pc_coeff[j].shape[0], pos_p[:,1].shape[0], pos_p[:,0].shape[0])
cx_len = int(chip.npix_x)
cy_len = int(chip.npix_y)
n_x = np.arange(0, cx_len, 1, dtype = int)
n_y = np.arange(0, cy_len, 1, dtype = int)
n_x = np.arange(0, cx_len, 1, dtype=int)
n_y = np.arange(0, cy_len, 1, dtype=int)
M, N = np.meshgrid(n_x, n_y)
# t1=datetime.datetime.now()
# U = interpolate.griddata(X_, Z_, (M[0:cy_len, 0:cx_len],N[0:cy_len, 0:cx_len]),
# method='nearest',fill_value=1.0)
b_img = galsim.Image(cx_len, cy_len)
b_img.setOrigin(0,0)
b_img.setOrigin(0, 0)
bounds = cutImg.bounds & b_img.bounds
if bounds.area() == 0:
continue
......@@ -574,8 +576,8 @@ class PSFInterpSLS(PSFModel):
ye = bounds.ymax+1
xs = bounds.xmin
xe = bounds.xmax+1
U = interpolate.griddata(X_, Z_, (M[ys:ye, xs:xe],N[ys:ye, xs:xe]),
method='nearest',fill_value=1.0)
U = interpolate.griddata(X_, Z_, (M[ys:ye, xs:xe], N[ys:ye, xs:xe]),
method='nearest', fill_value=1.0)
# t2=datetime.datetime.now()
# print("time interpolate:", t2-t1)
......@@ -586,47 +588,47 @@ class PSFInterpSLS(PSFModel):
img_tmp = cutImg
img_tmp[bounds] = img_tmp[bounds]*U
psf = pcs[:, j].reshape(m_size, m_size)
tmp_img = tmp_img + signal.fftconvolve(img_tmp.array, psf, mode='same', axes=None)
tmp_img = tmp_img + \
signal.fftconvolve(img_tmp.array, psf, mode='same', axes=None)
# t3=datetime.datetime.now()
# print("time convole:", t3-t2)
del U
del img_tmp
if np.sum(tmp_img.array)==0:
if np.sum(tmp_img.array) == 0:
tmp_img = cutImg
else:
tmp_img = tmp_img/np.sum(tmp_img.array)*sumImg
return tmp_img
def convolveFullImgWithPCAPSF(self, chip, folding_threshold=5.e-3):
keys_L1= chip_utils.getChipSLSGratingID(chip.chipID)
keys_L1 = chip_utils.getChipSLSGratingID(chip.chipID)
# keys_L2 = ['order-2','order-1','order0','order1','order2']
keys_L2 = ['order0','order1']
keys_L3 = ['w1','w2','w3','w4']
keys_L2 = ['order0', 'order1']
keys_L3 = ['w1', 'w2', 'w3', 'w4']
npca = 10
x_start = chip.x_cen/chip.pix_size - chip.npix_x / 2.
y_start = chip.y_cen/chip.pix_size - chip.npix_y / 2.
for i,gt in enumerate(keys_L1):
for i, gt in enumerate(keys_L1):
psfCo = self.grating1_data
if i > 0:
psfCo = self.grating2_data
for od in keys_L2:
psfCo_L2 = psfCo['order1']
if od in ['order-2','order-1','order0','order2']:
if od in ['order-2', 'order-1', 'order0', 'order2']:
psfCo_L2 = psfCo['order0']
for w in keys_L3:
img = chip.img_stack[gt][od][w]
pcs = psfCo_L2['band'+w[1]]['band_data'][0].data
pos_p = psfCo_L2['band'+w[1]]['band_data'][1].data/chip.pix_size - np.array([y_start, x_start])
pos_p = psfCo_L2['band'+w[1]]['band_data'][1].data / \
chip.pix_size - np.array([y_start, x_start])
pc_coeff = psfCo_L2['band'+w[1]]['band_data'][2].data
# print("DEBUG-----------",np.max(pos_p[:,1]),np.min(pos_p[:,1]), np.max(pos_p[:,0]),np.min(pos_p[:,0]))
sum_img = np.sum(img.array)
# coeff_mat = np.zeros([npca, chip.npix_y, chip.npix_x])
# for m in np.arange(chip.npix_y):
# for n in np.arange(chip.npix_x):
......@@ -660,37 +662,41 @@ class PSFInterpSLS(PSFModel):
# coeff_mat[:, m, n] = coeff_int
m_size = int(pcs.shape[0]**0.5)
tmp_img = np.zeros_like(img.array,dtype=np.float32)
tmp_img = np.zeros_like(img.array, dtype=np.float32)
for j in np.arange(npca):
print(gt, od, w, j)
X_ = np.hstack((pos_p[:,1].flatten()[:, None], pos_p[:,0].flatten()[:, None]),dtype=np.float32)
X_ = np.hstack((pos_p[:, 1].flatten()[:, None], pos_p[:, 0].flatten()[
:, None]), dtype=np.float32)
Z_ = (pc_coeff[j].astype(np.float32)).flatten()
# print(pc_coeff[j].shape[0], pos_p[:,1].shape[0], pos_p[:,0].shape[0])
sub_size = 4
cx_len = int(chip.npix_x/sub_size)
cy_len = int(chip.npix_y/sub_size)
n_x = np.arange(0, chip.npix_x, sub_size, dtype = int)
n_y = np.arange(0, chip.npix_y, sub_size, dtype = int)
n_x = np.arange(0, chip.npix_x, sub_size, dtype=int)
n_y = np.arange(0, chip.npix_y, sub_size, dtype=int)
M, N = np.meshgrid(n_x, n_y)
t1=datetime.datetime.now()
t1 = datetime.datetime.now()
# U = interpolate.griddata(X_, Z_, (M[0:cy_len, 0:cx_len],N[0:cy_len, 0:cx_len]),
# method='nearest',fill_value=1.0)
U1 = interpolate.griddata(X_, Z_, (M, N),
method='nearest',fill_value=1.0)
method='nearest', fill_value=1.0)
U = np.zeros_like(chip.img.array, dtype=np.float32)
for mi in np.arange(cy_len):
for mj in np.arange(cx_len):
U[mi*sub_size:(mi+1)*sub_size, mj*sub_size:(mj+1)*sub_size]=U1[mi,mj]
t2=datetime.datetime.now()
U[mi*sub_size:(mi+1)*sub_size, mj *
sub_size:(mj+1)*sub_size] = U1[mi, mj]
t2 = datetime.datetime.now()
print("time interpolate:", t2-t1)
img_tmp = img.array*U
psf = pcs[:, j].reshape(m_size, m_size)
tmp_img = tmp_img + signal.fftconvolve(img_tmp, psf, mode='same', axes=None)
tmp_img = tmp_img + \
signal.fftconvolve(
img_tmp, psf, mode='same', axes=None)
t3=datetime.datetime.now()
t3 = datetime.datetime.now()
print("time convole:", t3-t2)
del U
del U1
......
......@@ -25,6 +25,7 @@ import galsim
import math
# from numba import jit
class Chip(object):
def __init__(self, chipID):
self.chipID = chipID
......@@ -43,7 +44,7 @@ class Chip(object):
self.npix_y = 9232
self.pix_scale = 0.074
def getTanWCS(self, ra, dec, img_rot, pix_scale = None, xcen=None, ycen=None, logger=None):
def getTanWCS(self, ra, dec, img_rot, pix_scale=None, xcen=None, ycen=None, logger=None):
""" Get the WCS of the image mosaic using Gnomonic/TAN projection
Parameter:
......@@ -57,7 +58,8 @@ class Chip(object):
WCS of the focal plane
"""
if logger is not None:
logger.info(" Construct the wcs of the entire image mosaic using Gnomonic/TAN projection")
logger.info(
" Construct the wcs of the entire image mosaic using Gnomonic/TAN projection")
if (xcen == None) or (ycen == None):
xcen = self.cen_pix_x
ycen = self.cen_pix_y
......@@ -78,7 +80,8 @@ class Chip(object):
# dvdx = -np.cos(img_rot.rad) * pix_scale
# dvdy = +np.sin(img_rot.rad) * pix_scale
moscen = galsim.PositionD(x=xcen, y=ycen)
sky_center = galsim.CelestialCoord(ra=ra*galsim.degrees, dec=dec*galsim.degrees)
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)
......@@ -99,8 +102,6 @@ class Chip(object):
A galsim BoundsD object
"""
chipID = self.chipID
rowID, colID = self.getChipRowCol(chipID)
......@@ -125,36 +126,41 @@ class Chip(object):
return galsim.PositionD(xcen, ycen)
def transRaDec2D(ra, dec):
x1 = np.cos(dec / 57.2957795) * np.cos(ra / 57.2957795);
y1 = np.cos(dec / 57.2957795) * np.sin(ra / 57.2957795);
z1 = np.sin(dec / 57.2957795);
x1 = np.cos(dec / 57.2957795) * np.cos(ra / 57.2957795)
y1 = np.cos(dec / 57.2957795) * np.sin(ra / 57.2957795)
z1 = np.sin(dec / 57.2957795)
return np.array([x1, y1, z1])
def getobsPA(ra, dec):
l1 = np.array([0,0,1])
l1 = np.array([0, 0, 1])
l2 = transRaDec2D(ra, dec)
polar_ec = coord.SkyCoord(0*u.degree, 90*u.degree,frame='barycentrictrueecliptic')
polar_ec = coord.SkyCoord(0*u.degree, 90*u.degree,
frame='barycentrictrueecliptic')
polar_eq = polar_ec.transform_to('icrs')
# print(polar_eq.ra.value,polar_eq.dec.value)
polar_d = transRaDec2D(polar_eq.ra.value, polar_eq.dec.value)
l1l2cross = np.cross(l2,l1)
pdl2cross = np.cross(l2,polar_d)
angle = math.acos(np.dot(l1l2cross,pdl2cross)/(np.linalg.norm(l1l2cross)*np.linalg.norm(pdl2cross)))
l1l2cross = np.cross(l2, l1)
pdl2cross = np.cross(l2, polar_d)
angle = math.acos(np.dot(l1l2cross, pdl2cross) /
(np.linalg.norm(l1l2cross)*np.linalg.norm(pdl2cross)))
angle = (angle)/math.pi*180
angle = angle + 90
if (ra<90 or ra> 270):
angle=-angle
if (ra < 90 or ra > 270):
angle = -angle
return angle
# @jit()
def getSelectPointingList(center = [60,-40], radius = 2):
points = np.loadtxt('sky.dat')
center = center#ra dec
def getSelectPointingList(center=[60, -40], radius=2):
points = np.loadtxt('sky.dat')
center = center # ra dec
radius = radius # degree
radii_dec = 1
......@@ -163,7 +169,8 @@ def getSelectPointingList(center = [60,-40], radius = 2):
if radii_ra > 180:
radii_ra = 180
c_eclip = coord.SkyCoord(points[:,2]*u.degree, points[:,1]*u.degree,frame='barycentrictrueecliptic')
c_eclip = coord.SkyCoord(
points[:, 2]*u.degree, points[:, 1]*u.degree, frame='barycentrictrueecliptic')
c_equtor = c_eclip.transform_to('icrs')
# print(np.min((c_equtor.ra*u.degree).value), np.max((c_equtor.ra*u.degree).value))
......@@ -174,13 +181,15 @@ def getSelectPointingList(center = [60,-40], radius = 2):
ra_range_lo = center[0]-radii_ra
ra_range_hi = center[0]+radii_ra
if ra_range_lo< 0:
ids1 = ((c_equtor.ra*u.degree).value<ra_range_hi) | ((c_equtor.ra*u.degree).value>360+ra_range_lo)
if ra_range_lo < 0:
ids1 = ((c_equtor.ra*u.degree).value <
ra_range_hi) | ((c_equtor.ra*u.degree).value > 360+ra_range_lo)
elif ra_range_hi > 360:
ids1 = ((c_equtor.ra*u.degree).value>ra_range_lo) | ((c_equtor.ra*u.degree).value<ra_range_hi-360)
ids1 = ((c_equtor.ra*u.degree).value >
ra_range_lo) | ((c_equtor.ra*u.degree).value < ra_range_hi-360)
else:
ids1 = ((c_equtor.ra*u.degree).value > ra_range_lo) & ((c_equtor.ra*u.degree).value < ra_range_hi)
ids1 = ((c_equtor.ra*u.degree).value >
ra_range_lo) & ((c_equtor.ra*u.degree).value < ra_range_hi)
dec_range_lo = center[1]-radii_dec
if center[1]-radii_dec < -90:
......@@ -198,25 +207,24 @@ def getSelectPointingList(center = [60,-40], radius = 2):
p_result = np.zeros([num, 5])
i = 0
for p,p_ in zip(points[ids1][ids3][ids4],c_equtor[ids1][ids3][ids4]):
for p, p_ in zip(points[ids1][ids3][ids4], c_equtor[ids1][ids3][ids4]):
ra = (p_.ra*u.degree).value
dec = (p_.dec*u.degree).value
# print(ra, dec)
lon = p[2]
lat = p[1]
p_result[i,0] = ra
p_result[i,1] = dec
p_result[i,2] = lon
p_result[i,3] = lat
p_result[i,4] = getobsPA(ra, dec) + 90
p_result[i, 0] = ra
p_result[i, 1] = dec
p_result[i, 2] = lon
p_result[i, 3] = lat
p_result[i, 4] = getobsPA(ra, dec) + 90
i = i + 1
return p_result
def findPointingbyChipID(chipID = 8, ra = 60., dec = -40.):
def findPointingbyChipID(chipID=8, ra=60., dec=-40.):
"""_summary_
Args:
......@@ -228,7 +236,7 @@ def findPointingbyChipID(chipID = 8, ra = 60., dec = -40.):
_type_: [ra, dec, rotation angle]
"""
chip_center = [ra, dec]
p_list = getSelectPointingList(center = chip_center)
p_list = getSelectPointingList(center=chip_center)
pchip = Chip(chipID)
p_num = p_list.shape[0]
......@@ -238,10 +246,10 @@ def findPointingbyChipID(chipID = 8, ra = 60., dec = -40.):
r_ra = ra
r_dec = dec
r_rot = 0.
for i in np.arange(0,p_num,1):
ra_n = p_list[i,0]
dec_n = p_list[i,1]
rot = p_list[i,4]*galsim.degrees
for i in np.arange(0, p_num, 1):
ra_n = p_list[i, 0]
dec_n = p_list[i, 1]
rot = p_list[i, 4]*galsim.degrees
chip_wcs = pchip.getTanWCS(ra_n, dec_n, rot)
c_center = pchip.getChipCenter()
......@@ -259,13 +267,12 @@ def findPointingbyChipID(chipID = 8, ra = 60., dec = -40.):
r_rot = rot.deg
if min_d == max_value:
print("RA:%f,Dec:%f不在指向范围内,请于巡天规划序列比对!!!!!"%(ra, dec))
print("RA:%f,Dec:%f不在指向范围内,请于巡天规划序列比对!!!!!" % (ra, dec))
return [r_ra, r_dec , r_rot]
return [r_ra, r_dec, r_rot]
if __name__ == "__main__":
tchip, tra, tdec = 13, 60., -40.
pointing = findPointingbyChipID(chipID=tchip, ra=tra, dec=tdec)
print("[ra_center, dec_center, image_rot]: ", pointing)
from pylab import *
import math, sys, numpy as np
import math
import sys
import numpy as np
import astropy.coordinates as coord
from astropy.coordinates import SkyCoord
from astropy import wcs, units as u
......@@ -14,8 +16,9 @@ def transRaDec2D(ra, dec):
z1 = np.sin(dec / 57.2957795)
return np.array([x1, y1, z1])
def ecl2radec(lon_ecl, lat_ecl):
## convert from ecliptic coordinates to equatorial coordinates
# convert from ecliptic coordinates to equatorial coordinates
c_ecl = SkyCoord(
lon=lon_ecl * u.degree, lat=lat_ecl * u.degree, frame="barycentrictrueecliptic"
)
......@@ -25,18 +28,19 @@ def ecl2radec(lon_ecl, lat_ecl):
def radec2ecl(ra, dec):
## convert from equatorial coordinates to ecliptic coordinates
# convert from equatorial coordinates to ecliptic coordinates
c_eq = SkyCoord(ra=ra * u.degree, dec=dec * u.degree, frame="icrs")
c_ecl = c_eq.transform_to("barycentrictrueecliptic")
lon_ecl, lat_ecl = c_ecl.lon.degree, c_ecl.lat.degree
return lon_ecl, lat_ecl
def cal_FoVcenter_1P_equatorial(ra_FieldCenter, dec_FieldCenter, chipID = 1, pa = -23.5):
### [ra_FieldCenter, dec_FieldCenter] is the center ra, dec of calibration fileds, such as: NEP, NGC 6397, etc.
### [ra_ChipCenter, dec_ChipCenter] is the center ra, dec of the Chip center.
### [ra_PointCenter, dec_PointCenter] is the telescope pointing center.
## Calculate PA angle
def cal_FoVcenter_1P_equatorial(ra_FieldCenter, dec_FieldCenter, chipID=1, pa=-23.5):
# [ra_FieldCenter, dec_FieldCenter] is the center ra, dec of calibration fileds, such as: NEP, NGC 6397, etc.
# [ra_ChipCenter, dec_ChipCenter] is the center ra, dec of the Chip center.
# [ra_PointCenter, dec_PointCenter] is the telescope pointing center.
# Calculate PA angle
chip = Chip(chipID)
h_ext = ImageHeader.generateExtensionHeader(
......@@ -75,17 +79,18 @@ def cal_FoVcenter_1P_equatorial(ra_FieldCenter, dec_FieldCenter, chipID = 1, pa
return ra_PointCenter, dec_PointCenter, lon_ecl_PointCenter, lat_ecl_PointCenter
def cal_FoVcenter_1P_ecliptic(lon_ecl_FieldCenter, lat_ecl_FieldCenter, chipID = 1, pa = -23.5):
### [ra_FieldCenter, dec_FieldCenter] is the center ra, dec of calibration fileds, such as: NEP, NGC 6397, etc.
### [ra_ChipCenter, dec_ChipCenter] is the center ra, dec of the Chip center.
### [ra_PointCenter, dec_PointCenter] is the telescope pointing center.
def cal_FoVcenter_1P_ecliptic(lon_ecl_FieldCenter, lat_ecl_FieldCenter, chipID=1, pa=-23.5):
# [ra_FieldCenter, dec_FieldCenter] is the center ra, dec of calibration fileds, such as: NEP, NGC 6397, etc.
# [ra_ChipCenter, dec_ChipCenter] is the center ra, dec of the Chip center.
# [ra_PointCenter, dec_PointCenter] is the telescope pointing center.
ra_FieldCenter, dec_FieldCenter = ecl2radec(
lon_ecl_FieldCenter, lat_ecl_FieldCenter
)
## Calculate PA angle
# Calculate PA angle
chip = Chip(chipID)
h_ext = ImageHeader.generateExtensionHeader(
......@@ -124,7 +129,8 @@ def cal_FoVcenter_1P_ecliptic(lon_ecl_FieldCenter, lat_ecl_FieldCenter, chipID =
return ra_PointCenter, dec_PointCenter, lon_ecl_PointCenter, lat_ecl_PointCenter
def getChipCenterRaDec(chipID = 1, p_ra = 60., p_dec = -40.):
def getChipCenterRaDec(chipID=1, p_ra=60., p_dec=-40.):
chip = Chip(chipID)
h_ext = ImageHeader.generateExtensionHeader(
......@@ -149,17 +155,18 @@ def getChipCenterRaDec(chipID = 1, p_ra = 60., p_dec = -40.):
RA_chip, Dec_chip = world_point[0][0], world_point[0][1]
return RA_chip, Dec_chip
if __name__ == '__main__':
ra_input, dec_input = 270.00000, 66.56000 # NEP
pa = 23.5
# chipid = 2
for chipid in np.arange(1,31,1):
for chipid in np.arange(1, 31, 1):
ra, dec, lon_ecl, lat_ecl = cal_FoVcenter_1P_equatorial(
ra_input, dec_input,chipID=chipid, pa=pa)
ra_input, dec_input, chipID=chipid, pa=pa)
print("chip id is %d, chip center [ra,dec] is [%f, %f], pointing center calculated [ra,dec] is [%f, %f]"%(chipid, ra_input, dec_input, ra, dec))
#for check the result
print("chip id is %d, chip center [ra,dec] is [%f, %f], pointing center calculated [ra,dec] is [%f, %f]" % (
chipid, ra_input, dec_input, ra, dec))
# for check the result
# testRA, testDec = getChipCenterRaDec(chipID = chipid, p_ra = ra, p_dec = dec)
# print(ra_input-testRA, dec_input-testDec)
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