Commit 38e2c452 authored by Fang Yuedong's avatar Fang Yuedong
Browse files

Merge branch 'master' into 'current_stable_for_tests'

for tests before release

See merge request !23
parents e0f2b9f7 1c35c0e2
import os
import logging
import ObservationSim.Config._util as _util
from ObservationSim.Config.Header import generatePrimaryHeader
import observation_sim.config._util as _util
from observation_sim.config.header import generatePrimaryHeader
class ChipOutput(object):
......
......@@ -5,11 +5,12 @@ import galsim
import numpy as np
from astropy.time import Time
from ObservationSim.Config._util import get_obs_id
import ObservationSim.Instrument._util as _util
from observation_sim.config._util import get_obs_id
import observation_sim.instruments._util as _util
class Pointing(object):
def __init__(self, id=0, ra=0., dec=0., img_pa=0., timestamp=1621915200, sat_x=0., sat_y=0., sat_z=0., sun_x=0., sun_y=0., sun_z=0., sat_vx=0., sat_vy=0., sat_vz=0., exp_time=150., pointing_type='SCI', pointing_type_code='101', pointing_id = '00000001', obs_config_file=None, t_shutter_open = 1.3, t_shutter_close = 1.3):
def __init__(self, id=0, ra=0., dec=0., img_pa=0., timestamp=1621915200, sat_x=0., sat_y=0., sat_z=0., sun_x=0., sun_y=0., sun_z=0., sat_vx=0., sat_vy=0., sat_vz=0., exp_time=150., pointing_type='SCI', pointing_type_code='101', pointing_id='00000001', obs_config_file=None, t_shutter_open=1.3, t_shutter_close=1.3):
self.id = id
self.ra = ra
self.dec = dec
......@@ -41,7 +42,6 @@ class Pointing(object):
if self.obs_param["obs_id"]:
self.obs_id = str(self.obs_param["obs_id"])
def get_full_depth_exptime(self, filter_type):
if self.survey_field_type == 'WIDE':
if filter_type in _util.SPEC_FILTERS:
......@@ -64,7 +64,6 @@ class Pointing(object):
else:
return max(150., self.exp_time) # [TODO] for FGS
def read_pointing_columns(self, columns, id=0, t=1621915200, pointing_type='SCI'):
self.id = id
col_len = len(columns)
......@@ -108,7 +107,8 @@ class Pointing(object):
self.timestamp = t
def make_output_pointing_dir(self, overall_config, copy_obs_config=False):
run_dir = os.path.join(overall_config["work_dir"], overall_config["run_name"])
run_dir = os.path.join(
overall_config["work_dir"], overall_config["run_name"])
if not os.path.exists(run_dir):
try:
os.makedirs(run_dir, exist_ok=True)
......@@ -119,7 +119,7 @@ class Pointing(object):
project_cycle=overall_config["project_cycle"],
run_counter=overall_config["run_counter"],
pointing_id=self.obs_id,
pointing_type_code = self.pointing_type_code)
pointing_type_code=self.pointing_type_code)
self.output_dir = os.path.join(run_dir, self.output_prefix)
if not os.path.exists(self.output_dir):
try:
......@@ -127,10 +127,10 @@ class Pointing(object):
except OSError:
pass
if copy_obs_config and self.obs_config_file:
obs_config_output_path = os.path.join(self.output_dir, os.path.basename(self.obs_config_file))
obs_config_output_path = os.path.join(
self.output_dir, os.path.basename(self.obs_config_file))
if not os.path.exists(obs_config_output_path):
try:
shutil.copy(self.obs_config_file, self.output_dir)
except OSError:
pass
def get_obs_id(img_type='SCI', project_cycle=6, run_counter=0, pointing_id='00000001',pointing_type_code='101'):
def get_obs_id(img_type='SCI', project_cycle=6, run_counter=0, pointing_id='00000001', pointing_type_code='101'):
# obs_type = {'SCI': '01', 'BIAS': '03', 'DARK': '07', 'FLAT': '11', 'CRS': '98', 'CRD': '99'}
# obs_type = {'SCIE': '01', 'BIAS': '03', 'DARK': '07', 'FLAT': '11', 'CRS': '98', 'CRD': '99', 'CAL': '01'}
obs_id = pointing_type_code + str(int(project_cycle)).rjust(2, '0') + str(int(run_counter)) + pointing_id
obs_id = pointing_type_code + \
str(int(project_cycle)).rjust(2, '0') + \
str(int(run_counter)) + pointing_id
return obs_id
# def get_obs_id(img_type='SCI', project_cycle=6, run_counter=0, pointing_num=0):
# # obs_type = {'SCI': '01', 'BIAS': '03', 'DARK': '07', 'FLAT': '11', 'CRS': '98', 'CRD': '99'}
# obs_type = {'SCIE': '01', 'BIAS': '03', 'DARK': '07', 'FLAT': '11', 'CRS': '98', 'CRD': '99', 'CAL': '01'}
# obs_id = '1'+ obs_type[img_type] + str(int(project_cycle)).rjust(2, '0') + str(int(run_counter)) + str(pointing_num).rjust(8,'0')
# return obs_id
def get_file_type(img_type='SCI'):
file_type = {'SCI':'SCI', 'BIAS':'BIAS', 'DARK':'DARK', 'FLAT':'FLAT', 'CRS':'CRS', 'CRD':'CRD','CALS':'CALS','CALF':'CALF'}
file_type = {'SCI': 'SCI', 'BIAS': 'BIAS', 'DARK': 'DARK', 'FLAT': 'FLAT',
'CRS': 'CRS', 'CRD': 'CRD', 'CALS': 'CALS', 'CALF': 'CALF'}
return file_type[img_type]
......@@ -17,13 +17,14 @@ from astropy.coordinates import SkyCoord
from astropy.wcs.utils import fit_wcs_from_points
from astropy.time import Time
from astropy import wcs
from ObservationSim.Config._util import get_obs_id, get_file_type
from observation_sim.config._util import get_obs_id, get_file_type
from datetime import datetime, timezone
# import socket
import platform
import toml
def chara2digit(char):
""" Function to judge and convert characters to digitals
......@@ -66,6 +67,7 @@ def read_header_parameter(filename='global_header.param'):
# print(value)
return name, value, description
def rotate_CD_matrix(cd, pa_aper):
"""Rotate CD matrix
......@@ -90,17 +92,18 @@ def rotate_CD_matrix(cd, pa_aper):
"""
rad = np.deg2rad(-pa_aper)
mat = np.zeros((2,2))
mat[0,:] = np.array([np.cos(rad),-np.sin(rad)])
mat[1,:] = np.array([np.sin(rad),np.cos(rad)])
mat = np.zeros((2, 2))
mat[0, :] = np.array([np.cos(rad), -np.sin(rad)])
mat[1, :] = np.array([np.sin(rad), np.cos(rad)])
cd_rot = np.dot(mat, cd)
return cd_rot
def calcaluteSLSRotSkyCoor(pix_xy = None,rot_angle = 1, xlen = 9216, ylen = 9232, w = None):
def calcaluteSLSRotSkyCoor(pix_xy=None, rot_angle=1, xlen=9216, ylen=9232, w=None):
rad = np.deg2rad(rot_angle)
mat = np.zeros((2,2))
mat[0,:] = np.array([np.cos(rad),-np.sin(rad)])
mat[1,:] = np.array([np.sin(rad),np.cos(rad)])
mat = np.zeros((2, 2))
mat[0, :] = np.array([np.cos(rad), -np.sin(rad)])
mat[1, :] = np.array([np.sin(rad), np.cos(rad)])
center = np.array([xlen/2, ylen/2])
rot_pix = np.dot(mat, pix_xy-center) + center
skyCoor = w.wcs_pix2world(np.array([rot_pix]), 1)
......@@ -172,10 +175,9 @@ def calcaluteSLSRotSkyCoor(pix_xy = None,rot_angle = 1, xlen = 9216, ylen = 9232
# return name, value, description
##9232 9216 898 534 1309 60 -40 -23.4333
def WCS_def(xlen = 9216, ylen = 9232, gapy = 898.0, gapx1 = 534, gapx2 = 1309, ra_ref = 60, dec_ref = -40, pa = -23.433, pixel_scale = 0.074, pixel_size=1e-2,
rotate_chip=0., filter = 'GI', row_num = None, col_num = None, xcen = None, ycen = None):
# 9232 9216 898 534 1309 60 -40 -23.4333
def WCS_def(xlen=9216, ylen=9232, gapy=898.0, gapx1=534, gapx2=1309, ra_ref=60, dec_ref=-40, pa=-23.433, pixel_scale=0.074, pixel_size=1e-2,
rotate_chip=0., filter='GI', row_num=None, col_num=None, xcen=None, ycen=None):
""" Creat a wcs frame for CCST with multiple extensions
Parameters
......@@ -192,8 +194,8 @@ def WCS_def(xlen = 9216, ylen = 9232, gapy = 898.0, gapx1 = 534, gapx2 = 1309, r
flag_x = [0, 1, -1, 1, -1]
flag_y = [0, 1, 1, -1, -1]
flag_ext_x = [0,-1,1,-1,1]
flag_ext_y = [0,-1,-1,1,1]
flag_ext_x = [0, -1, 1, -1, 1]
flag_ext_y = [0, -1, -1, 1, 1]
pa_aper = pa
if (row_num is not None) and (col_num is not None):
......@@ -205,35 +207,42 @@ def WCS_def(xlen = 9216, ylen = 9232, gapy = 898.0, gapx1 = 534, gapx2 = 1309, r
detector_size_x = xlen
detector_size_y = ylen
gap_y = gapy
gap_x = [gapx1,gapx2]
gap_x = [gapx1, gapx2]
gap_x1_num = 3
gap_x2_num = 2
y_center = (detector_size_y*y_num+gap_y*(y_num-1))/2
x_center = (detector_size_x*x_num+gap_x[0]*gap_x1_num+gap_x[1]*gap_x2_num)/2
x_center = (detector_size_x*x_num +
gap_x[0]*gap_x1_num+gap_x[1]*gap_x2_num)/2
gap_x_map = np.array([[0,0,0,0,0],[gap_x[0],gap_x[1],gap_x[1],gap_x[1],gap_x[1]],[gap_x[1],gap_x[0],gap_x[0],gap_x[0],gap_x[0]],[gap_x[0],gap_x[0],gap_x[0],gap_x[0],gap_x[0]],[gap_x[0],gap_x[0],gap_x[0],gap_x[0],gap_x[1]],[gap_x[1],gap_x[1],gap_x[1],gap_x[1],gap_x[0]]])
gap_x_map = np.array([[0, 0, 0, 0, 0], [gap_x[0], gap_x[1], gap_x[1], gap_x[1], gap_x[1]], [gap_x[1], gap_x[0], gap_x[0], gap_x[0], gap_x[0]], [
gap_x[0], gap_x[0], gap_x[0], gap_x[0], gap_x[0]], [gap_x[0], gap_x[0], gap_x[0], gap_x[0], gap_x[1]], [gap_x[1], gap_x[1], gap_x[1], gap_x[1], gap_x[0]]])
j = row_num
i = col_num
# ccdnum = str((j-1)*5+i)
x_ref, y_ref = detector_size_x*i + sum(gap_x_map[0:i,j-1]) - detector_size_x/2. , (detector_size_y+gap_y)*j-gap_y-detector_size_y/2
x_ref, y_ref = detector_size_x*i + \
sum(gap_x_map[0:i, j-1]) - detector_size_x / \
2., (detector_size_y+gap_y)*j-gap_y-detector_size_y/2
for k in range(1,2):
for k in range(1, 2):
cd = np.array([[ pixel_scale, 0], [0, pixel_scale]])/3600.*flag_x[k]
cd = np.array([[pixel_scale, 0], [0, pixel_scale]]) / \
3600.*flag_x[k]
cd_rot = rotate_CD_matrix(cd, pa_aper)
# f = open("CCD"+ccdnum.rjust(2,'0')+"_extension"+str(k)+"_wcs.param","w")
r_dat['CRPIX1'] = flag_ext_x[k]*((x_ref+flag_ext_x[k]*detector_size_x/2)-x_center)
r_dat['CRPIX2'] = flag_ext_y[k]*((y_ref+flag_ext_y[k]*detector_size_y/2)-y_center)
r_dat['CD1_1'] = cd_rot[0,0]
r_dat['CD1_2'] = cd_rot[0,1]
r_dat['CD2_1'] = cd_rot[1,0]
r_dat['CD2_2'] = cd_rot[1,1]
r_dat['CRPIX1'] = flag_ext_x[k] * \
((x_ref+flag_ext_x[k]*detector_size_x/2)-x_center)
r_dat['CRPIX2'] = flag_ext_y[k] * \
((y_ref+flag_ext_y[k]*detector_size_y/2)-y_center)
r_dat['CD1_1'] = cd_rot[0, 0]
r_dat['CD1_2'] = cd_rot[0, 1]
r_dat['CD2_1'] = cd_rot[1, 0]
r_dat['CD2_2'] = cd_rot[1, 1]
if filter in ['GU', 'GV', 'GI']:
......@@ -265,7 +274,8 @@ def WCS_def(xlen = 9216, ylen = 9232, gapy = 898.0, gapx1 = 534, gapx2 = 1309, r
y_pixs[i_pix] = y
pix_coor = np.array([x, y])
sc1 = calcaluteSLSRotSkyCoor(pix_xy=pix_coor, rot_angle=sls_rot, xlen=xlen, ylen=ylen, w=w)
sc1 = calcaluteSLSRotSkyCoor(
pix_xy=pix_coor, rot_angle=sls_rot, xlen=xlen, ylen=ylen, w=w)
# print(sc1[0,0],sc1[0,1])
sky_coors.append((sc1[0, 0], sc1[0, 1]))
......@@ -294,12 +304,12 @@ def WCS_def(xlen = 9216, ylen = 9232, gapy = 898.0, gapx1 = 534, gapx2 = 1309, r
r_dat['CRPIX2'] = -y1
# cd = np.array([[ pixel_scale, 0], [0, pixel_scale]])/3600.*flag_x[1]
cd = np.array([[ pixel_scale, 0], [0, -pixel_scale]])/3600.
cd = np.array([[pixel_scale, 0], [0, -pixel_scale]])/3600.
cd_rot = rotate_CD_matrix(cd, pa_aper)
r_dat['CD1_1'] = cd_rot[0,0]
r_dat['CD1_2'] = cd_rot[0,1]
r_dat['CD2_1'] = cd_rot[1,0]
r_dat['CD2_2'] = cd_rot[1,1]
r_dat['CD1_1'] = cd_rot[0, 0]
r_dat['CD1_2'] = cd_rot[0, 1]
r_dat['CD2_1'] = cd_rot[1, 0]
r_dat['CD2_2'] = cd_rot[1, 1]
w = wcs.WCS(naxis=2)
w.wcs.crpix = [r_dat['CRPIX1'], r_dat['CRPIX2']]
......@@ -320,10 +330,12 @@ def WCS_def(xlen = 9216, ylen = 9232, gapy = 898.0, gapx1 = 534, gapx2 = 1309, r
x_pixs[i_pix] = x
y_pixs[i_pix] = y
pix_coor = np.array([x, y])
sc1 = calcaluteSLSRotSkyCoor(pix_xy=pix_coor, rot_angle=rotate_chip, xlen=xlen, ylen=ylen, w=w)
sc1 = calcaluteSLSRotSkyCoor(
pix_xy=pix_coor, rot_angle=rotate_chip, xlen=xlen, ylen=ylen, w=w)
sky_coors.append((sc1[0, 0], sc1[0, 1]))
wcs_new = fit_wcs_from_points(xy=np.array([x_pixs, y_pixs]),
world_coords=SkyCoord(sky_coors, frame="icrs", unit="deg"),
world_coords=SkyCoord(
sky_coors, frame="icrs", unit="deg"),
projection='TAN')
r_dat['CD1_1'] = wcs_new.wcs.cd[0, 0]
r_dat['CD1_2'] = wcs_new.wcs.cd[0, 1]
......@@ -336,30 +348,31 @@ def WCS_def(xlen = 9216, ylen = 9232, gapy = 898.0, gapx1 = 534, gapx2 = 1309, r
r_dat['CRVAL2'] = wcs_new.wcs.crval[1]
else:
raise ValueError('In function WCS_def(): Either (row_num, col_num) or (xcen, ycen, pixel_size) should be given')
raise ValueError(
'In function WCS_def(): Either (row_num, col_num) or (xcen, ycen, pixel_size) should be given')
return r_dat
#TODO project_cycle is temporary, is not in header defined, delete in future
def generatePrimaryHeader(xlen = 9216, ylen = 9232, pointing_id = '00000001', pointing_type_code='101', ra = 60, dec = -40, pixel_scale = 0.074, time_pt = None, im_type = 'SCI', exptime=150., sat_pos = [0.,0.,0.], sat_vel = [0., 0., 0.], project_cycle=6, run_counter=0, chip_name="01"):
# TODO project_cycle is temporary, is not in header defined, delete in future
def generatePrimaryHeader(xlen=9216, ylen=9232, pointing_id='00000001', pointing_type_code='101', ra=60, dec=-40, pixel_scale=0.074, time_pt=None, im_type='SCI', exptime=150., sat_pos=[0., 0., 0.], sat_vel=[0., 0., 0.], project_cycle=6, run_counter=0, chip_name="01"):
# array_size1, array_size2, flux, sigma = int(argv[1]), int(argv[2]), 1000.0, 5.0
# k = (row_num-1)*6+col_num
# ccdnum = str(k)
datetime_obs = datetime.utcfromtimestamp(time_pt)
datetime_obs = datetime_obs.replace(tzinfo=timezone.utc)
# print(datetime_obs.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-5])
datetime_obs = datetime.utcfromtimestamp(np.round(datetime_obs.timestamp(), 1))
datetime_obs = datetime.utcfromtimestamp(
np.round(datetime_obs.timestamp(), 1))
# print(datetime_obs.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-5])
# date_obs = datetime_obs.strftime("%y%m%d")
# time_obs = datetime_obs.strftime("%H%M%S%f")[:-5]
g_header_fn = os.path.split(os.path.realpath(__file__))[0] + '/csst_msc_l0_ms.fits'
g_header_fn = os.path.split(os.path.realpath(__file__))[
0] + '/csst_msc_l0_ms.fits'
f = open(os.path.split(os.path.realpath(__file__))[0] + '/filter.lst')
s = f.readline()
s = s.strip("\n")
......@@ -386,7 +399,6 @@ def generatePrimaryHeader(xlen = 9216, ylen = 9232, pointing_id = '00000001', po
# h_card = fits.card.Card(header_toml[h_key][key]['key'],header_toml[h_key][key]['example'],header_toml[h_key][key]['comment'])
# h_prim.append(h_card)
# h_prim['PIXSIZE1'] = xlen
# h_prim['PIXSIZE2'] = ylen
......@@ -404,7 +416,8 @@ def generatePrimaryHeader(xlen = 9216, ylen = 9232, pointing_id = '00000001', po
# # OBS_id = '1'+ obs_type[im_type] + str(int(project_cycle)) + pointNum.rjust(7,'0')
# OBS_id = '1'+ obs_type[im_type] + str(int(project_cycle)) + str(int(run_counter)).rjust(2, '0') + pointNum.rjust(5,'0')
OBS_id = get_obs_id(img_type=im_type, project_cycle=project_cycle, run_counter=run_counter, pointing_id=pointing_id, pointing_type_code = pointing_type_code)
OBS_id = get_obs_id(img_type=im_type, project_cycle=project_cycle, run_counter=run_counter,
pointing_id=pointing_id, pointing_type_code=pointing_type_code)
# h_prim['OBJECT'] = str(int(project_cycle)) + pointNum.rjust(7, '0')
h_prim['OBJECT'] = pointing_id
......@@ -420,8 +433,10 @@ def generatePrimaryHeader(xlen = 9216, ylen = 9232, pointing_id = '00000001', po
co = coord.SkyCoord(ra, dec, unit='deg')
ra_hms = format(co.ra.hms.h, '02.0f') + format(co.ra.hms.m, '02.0f') + format(co.ra.hms.s, '02.1f')
dec_hms = format(co.dec.dms.d, '02.0f') + format(abs(co.dec.dms.m), '02.0f') + format(abs(co.dec.dms.s), '02.0f')
ra_hms = format(co.ra.hms.h, '02.0f') + format(co.ra.hms.m,
'02.0f') + format(co.ra.hms.s, '02.1f')
dec_hms = format(co.dec.dms.d, '02.0f') + format(abs(co.dec.dms.m),
'02.0f') + format(abs(co.dec.dms.s), '02.0f')
if dec >= 0:
h_prim['TARGET'] = ra_hms + '+' + dec_hms
else:
......@@ -435,8 +450,6 @@ def generatePrimaryHeader(xlen = 9216, ylen = 9232, pointing_id = '00000001', po
h_prim['RA_PNT1'] = ra
h_prim['DEC_PNT1'] = dec
# h_prim['PIXSCAL1'] = pixel_scale
# h_prim['PIXSCAL2'] = pixel_scale
......@@ -452,11 +465,13 @@ def generatePrimaryHeader(xlen = 9216, ylen = 9232, pointing_id = '00000001', po
# file_start_time = '20' + date[0:6] + time_obs[0:6]
file_start_time = datetime_obs.strftime("%Y%m%d%H%M%S")
end_time_str = str(tend.datetime)
file_end_time = end_time_str[0:4] + end_time_str[5:7]+end_time_str[8:10] + end_time_str[11:13] + end_time_str[14:16] + end_time_str[17:19]
file_end_time = end_time_str[0:4] + end_time_str[5:7]+end_time_str[8:10] + \
end_time_str[11:13] + end_time_str[14:16] + end_time_str[17:19]
# h_prim['FILENAME'] = 'CSST_MSC_MS_' + im_type + '_' + file_start_time + '_' + file_end_time + '_' + OBS_id + '_' + CCDID[
# k - 1].rjust(2, '0') + '_L0_V01'
h_prim['FILENAME'] = 'CSST_MSC_MS_' + h_prim['FILETYPE'] + '_' + file_start_time + '_' + file_end_time + '_' + OBS_id + '_' + chip_name + '_L0_V01'
h_prim['FILENAME'] = 'CSST_MSC_MS_' + h_prim['FILETYPE'] + '_' + \
file_start_time + '_' + file_end_time + \
'_' + OBS_id + '_' + chip_name + '_L0_V01'
h_prim['POSI0_X'] = sat_pos[0]
h_prim['POSI0_Y'] = sat_pos[1]
......@@ -473,15 +488,19 @@ def generatePrimaryHeader(xlen = 9216, ylen = 9232, pointing_id = '00000001', po
# h_prim['SIM_VER'] = (get_distribution("CSSTSim").version, "Version of CSST MSC simulation software")
currentDateAndTime = datetime.now()
compute_name = platform.node()
h_prim['FITSSWV'] = get_distribution("csst_msc_sim").version +'_' + currentDateAndTime.strftime("%Y%m%d") + '_' +compute_name
h_prim['EPOCH'] = round((Time(h_prim['EXPSTART'], format='mjd', scale='tcb')).jyear, 1)
h_prim['FITSSWV'] = get_distribution(
"csst_msc_sim").version + '_' + currentDateAndTime.strftime("%Y%m%d") + '_' + compute_name
h_prim['EPOCH'] = round(
(Time(h_prim['EXPSTART'], format='mjd', scale='tcb')).jyear, 1)
return h_prim
def generateExtensionHeader(chip, xlen = 9216, ylen = 9232,ra = 60, dec = -40, pa = -23.433, gain = 1.0, readout = 5.0, dark = 0.02, saturation=90000, pixel_scale = 0.074, pixel_size=1e-2,
extName='SCIE', row_num = None, col_num = None, xcen=None, ycen=None, timestamp = 1621915200,exptime = 150., readoutTime = 40., t_shutter_open = 1.3, t_shutter_close = 1.3):
e_header_fn = os.path.split(os.path.realpath(__file__))[0] + '/csst_msc_l0_ms.fits'
def generateExtensionHeader(chip, xlen=9216, ylen=9232, ra=60, dec=-40, pa=-23.433, gain=1.0, readout=5.0, dark=0.02, saturation=90000, pixel_scale=0.074, pixel_size=1e-2,
extName='SCIE', row_num=None, col_num=None, xcen=None, ycen=None, timestamp=1621915200, exptime=150., readoutTime=40., t_shutter_open=1.3, t_shutter_close=1.3):
e_header_fn = os.path.split(os.path.realpath(__file__))[
0] + '/csst_msc_l0_ms.fits'
f = open(os.path.split(os.path.realpath(__file__))[0] + '/filter.lst')
s = f.readline()
s = s.strip("\n")
......@@ -562,28 +581,35 @@ def generateExtensionHeader(chip, xlen = 9216, ylen = 9232,ra = 60, dec = -40, p
t_shutter_os = tstart
t_shutter_oe = Time(tstart.mjd + t_shutter_open / 86400., format="mjd")
t_shutter_co = Time(tstart.mjd + exptime / 86400., format="mjd")
t_shutter_ce = Time(tstart.mjd + (exptime + t_shutter_close) / 86400., format="mjd")
t_shutter_os1 = datetime.utcfromtimestamp(np.round(datetime.utcfromtimestamp(t_shutter_os.unix).replace(tzinfo=timezone.utc).timestamp(), 1))
t_shutter_ce = Time(
tstart.mjd + (exptime + t_shutter_close) / 86400., format="mjd")
t_shutter_os1 = datetime.utcfromtimestamp(np.round(datetime.utcfromtimestamp(
t_shutter_os.unix).replace(tzinfo=timezone.utc).timestamp(), 1))
h_ext['SHTOPEN0'] = t_shutter_os1.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-5]
t_shutter_oe1 = datetime.utcfromtimestamp(np.round(datetime.utcfromtimestamp(t_shutter_oe.unix).replace(tzinfo=timezone.utc).timestamp(), 1))
t_shutter_oe1 = datetime.utcfromtimestamp(np.round(datetime.utcfromtimestamp(
t_shutter_oe.unix).replace(tzinfo=timezone.utc).timestamp(), 1))
h_ext['SHTOPEN1'] = t_shutter_oe1.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-5]
t_shutter_co1 = datetime.utcfromtimestamp(np.round(datetime.utcfromtimestamp(t_shutter_co.unix).replace(tzinfo=timezone.utc).timestamp(), 1))
t_shutter_co1 = datetime.utcfromtimestamp(np.round(datetime.utcfromtimestamp(
t_shutter_co.unix).replace(tzinfo=timezone.utc).timestamp(), 1))
h_ext['SHTCLOS0'] = t_shutter_co1.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-5]
t_shutter_ce1 = datetime.utcfromtimestamp(np.round(datetime.utcfromtimestamp(t_shutter_ce.unix).replace(tzinfo=timezone.utc).timestamp(), 1))
t_shutter_ce1 = datetime.utcfromtimestamp(np.round(datetime.utcfromtimestamp(
t_shutter_ce.unix).replace(tzinfo=timezone.utc).timestamp(), 1))
h_ext['SHTCLOS1'] = t_shutter_ce1.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-5]
tstart_read = Time(tstart.mjd + exptime / 86400., format="mjd")
tend_read = Time(tstart.mjd + (exptime + readoutTime) / 86400., format="mjd")
tend_read = Time(tstart.mjd + (exptime + readoutTime) /
86400., format="mjd")
# tstart1=tstart.datetime.replace(microsecond=round(tstart.datetime.microsecond, -5))
tstart1 = datetime.utcfromtimestamp(np.round(datetime.utcfromtimestamp(tstart_read.unix).replace(tzinfo=timezone.utc).timestamp(), 1))
tstart1 = datetime.utcfromtimestamp(np.round(datetime.utcfromtimestamp(
tstart_read.unix).replace(tzinfo=timezone.utc).timestamp(), 1))
h_ext['ROTIME0'] = tstart1.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-5]
# tend_read1 = tend_read.datetime.replace(microsecond=round(tend_read.datetime.microsecond, -5))
tend_read1 = datetime.utcfromtimestamp(np.round(datetime.utcfromtimestamp(tend_read.unix).replace(tzinfo=timezone.utc).timestamp(), 1))
tend_read1 = datetime.utcfromtimestamp(np.round(datetime.utcfromtimestamp(
tend_read.unix).replace(tzinfo=timezone.utc).timestamp(), 1))
h_ext['ROTIME1'] = tend_read1.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-5]
# h_ext['POS_ANG'] = pa
header_wcs = WCS_def(xlen=xlen, ylen=ylen, gapy=898.0, gapx1=534, gapx2=1309, ra_ref=ra, dec_ref=dec, pa=pa, pixel_scale=pixel_scale, pixel_size=pixel_size,
rotate_chip=chip.rotate_angle, filter = h_ext['FILTER'], row_num=row_num, col_num=col_num, xcen = xcen, ycen = ycen)
rotate_chip=chip.rotate_angle, filter=h_ext['FILTER'], row_num=row_num, col_num=col_num, xcen=xcen, ycen=ycen)
h_ext['CRPIX1'] = header_wcs['CRPIX1']
h_ext['CRPIX2'] = header_wcs['CRPIX2']
......@@ -604,7 +630,6 @@ def generateExtensionHeader(chip, xlen = 9216, ylen = 9232,ra = 60, dec = -40, p
return h_ext
def main(argv):
xlen = int(argv[1])
......@@ -621,17 +646,17 @@ def main(argv):
dark = float(argv[12])
fw = float(argv[13])
h_prim = generatePrimaryHeader(xlen=xlen, ylen=ylen, ra=ra, dec=dec, psize=pSize,
row_num=ccd_row_num, col_num=ccd_col_num, pointNum=pointingNum)
h_prim = generatePrimaryHeader(xlen = xlen, ylen = ylen,ra = ra, dec = dec, psize = pSize, row_num = ccd_row_num, col_num = ccd_col_num, pointNum = pointingNum)
h_ext = generateExtensionHeader(xlen = xlen, ylen = ylen,ra = ra, dec = dec, pa = pa_aper, gain = gain, readout = readout, dark = dark, saturation=fw, psize = pSize, row_num = ccd_row_num, col_num = ccd_col_num)
h_ext = generateExtensionHeader(xlen=xlen, ylen=ylen, ra=ra, dec=dec, pa=pa_aper, gain=gain,
readout=readout, dark=dark, saturation=fw, psize=pSize, row_num=ccd_row_num, col_num=ccd_col_num)
hdu1 = fits.PrimaryHDU(header=h_prim)
hdu2 = fits.ImageHDU(np.zeros([ylen,xlen]),header = h_ext)
hdu2 = fits.ImageHDU(np.zeros([ylen, xlen]), header=h_ext)
hdul = fits.HDUList([hdu1,hdu2])
hdul = fits.HDUList([hdu1, hdu2])
hdul.writeto(h_prim['FILENAME']+'.fits',output_verify='ignore')
hdul.writeto(h_prim['FILENAME']+'.fits', output_verify='ignore')
# if __name__ == "__main__":
# main(sys.argv)
......@@ -4,9 +4,9 @@ import os
import numpy as np
import gc
import ObservationSim.Instrument._util as _util
from ObservationSim.Instrument.FilterParam import FilterParam
from ObservationSim.Straylight import Straylight
import observation_sim.instruments._util as _util
from observation_sim.instruments.FilterParam import FilterParam
from observation_sim.sky_background import Straylight
try:
import importlib.resources as pkg_resources
......@@ -14,6 +14,7 @@ except ImportError:
# Try backported to PY<37 'importlib_resources'
import importlib_resources as pkg_resources
class Filter(object):
def __init__(self, filter_id, filter_type, filter_param, ccd_bandpass=None):
self.filter_id = filter_id
......@@ -52,54 +53,64 @@ class Filter(object):
def _get_bandpasses(self, filter_dir=None, unit='A'):
if self.filter_id < 7: # Photometric
try:
with pkg_resources.files('ObservationSim.Instrument.data.filters').joinpath(self.filter_type.lower() + '.txt') as filter_file:
self.filter_bandpass = galsim.Bandpass(str(filter_file), wave_type=unit)
with pkg_resources.files('observation_sim.instruments.data.filters').joinpath(self.filter_type.lower() + '.txt') as filter_file:
self.filter_bandpass = galsim.Bandpass(
str(filter_file), wave_type=unit)
except AttributeError:
with pkg_resources.path('ObservationSim.Instrument.data.filters', self.filter_type.lower() + '.txt') as filter_file:
self.filter_bandpass = galsim.Bandpass(str(filter_file), wave_type=unit)
with pkg_resources.path('observation_sim.instruments.data.filters', self.filter_type.lower() + '.txt') as filter_file:
self.filter_bandpass = galsim.Bandpass(
str(filter_file), wave_type=unit)
try:
with pkg_resources.files('ObservationSim.Instrument.data.throughputs').joinpath(self.filter_type.lower() + '_throughput.txt') as filter_file:
bandpass_full = galsim.Bandpass(str(filter_file), wave_type=unit)
with pkg_resources.files('observation_sim.instruments.data.throughputs').joinpath(self.filter_type.lower() + '_throughput.txt') as filter_file:
bandpass_full = galsim.Bandpass(
str(filter_file), wave_type=unit)
except AttributeError:
with pkg_resources.path('ObservationSim.Instrument.data.throughputs', self.filter_type.lower() + '_throughput.txt') as filter_file:
bandpass_full = galsim.Bandpass(str(filter_file), wave_type=unit)
with pkg_resources.path('observation_sim.instruments.data.throughputs', self.filter_type.lower() + '_throughput.txt') as filter_file:
bandpass_full = galsim.Bandpass(
str(filter_file), wave_type=unit)
# bandpass_full = bandpass_full * self.ccd_bandpass
# Get sub-bandpasses
bandpass_sub_list = []
try:
with pkg_resources.files('ObservationSim.Instrument.data.filters').joinpath(self.filter_type.lower() + "_sub.list") as wave_bin_file:
with pkg_resources.files('observation_sim.instruments.data.filters').joinpath(self.filter_type.lower() + "_sub.list") as wave_bin_file:
wave_points = open(wave_bin_file).read().splitlines()
except AttributeError:
with pkg_resources.path('ObservationSim.Instrument.data.filters', self.filter_type.lower() + "_sub.list") as wave_bin_file:
with pkg_resources.path('observation_sim.instruments.data.filters', self.filter_type.lower() + "_sub.list") as wave_bin_file:
wave_points = open(wave_bin_file).read().splitlines()
for i in range(2, len(wave_points), 2):
blim = max(float(wave_points[i-2])*0.1, bandpass_full.blue_limit)
blim = max(float(wave_points[i-2])
* 0.1, bandpass_full.blue_limit)
rlim = min(float(wave_points[i])*0.1, bandpass_full.red_limit)
if blim >= rlim:
continue
bandpass = bandpass_full.truncate(blue_limit=blim, red_limit=rlim)
bandpass = bandpass_full.truncate(
blue_limit=blim, red_limit=rlim)
bandpass_sub_list.append(bandpass)
# print("num of sub-bandpasses for filter#%d(%s) = "%(self.filter_id, self.filter_type), len(bandpass_sub_list), flush=True)
else: # Spectroscopic
sls_lamb = np.linspace(self.blue_limit, self.red_limit, 100)
sls_flux = np.ones_like(sls_lamb)
con_spec = galsim.LookupTable(sls_lamb, sls_flux, interpolant='nearest')
con_spec = galsim.LookupTable(
sls_lamb, sls_flux, interpolant='nearest')
bandpass_full = galsim.Bandpass(con_spec, wave_type=unit)
bandpass_sub_list = []
try:
with pkg_resources.files('ObservationSim.Instrument.data.filters').joinpath(self.filter_type.lower() + "_sub.list") as wave_bin_file:
with pkg_resources.files('observation_sim.instruments.data.filters').joinpath(self.filter_type.lower() + "_sub.list") as wave_bin_file:
wave_points = open(wave_bin_file).read().splitlines()
except AttributeError:
with pkg_resources.path('ObservationSim.Instrument.data.filters', self.filter_type.lower() + "_sub.list") as wave_bin_file:
with pkg_resources.path('observation_sim.instruments.data.filters', self.filter_type.lower() + "_sub.list") as wave_bin_file:
wave_points = open(wave_bin_file).read().splitlines()
for i in range(2, len(wave_points), 2):
blim = max(float(wave_points[i - 2]) * 0.1, bandpass_full.blue_limit)
rlim = min(float(wave_points[i]) * 0.1, bandpass_full.red_limit)
blim = max(float(wave_points[i - 2])
* 0.1, bandpass_full.blue_limit)
rlim = min(float(wave_points[i])
* 0.1, bandpass_full.red_limit)
if blim >= rlim:
continue
bandpass = bandpass_full.truncate(blue_limit=blim, red_limit=rlim)
bandpass = bandpass_full.truncate(
blue_limit=blim, red_limit=rlim)
bandpass_sub_list.append(bandpass)
# print("num of sub-bandpasses for filter#%d(%s) = " % (self.filter_id, self.filter_type), len(bandpass_sub_list), flush=True)
......@@ -111,17 +122,20 @@ class Filter(object):
def getSkyNoise(self, exptime, gain=1.):
return self.sky_background * exptime / gain
def setFilterStrayLightPixel(self,jtime = 2460843., sat_pos = np.array([0,0,0]), pointing_radec = np.array([0,0]), sun_pos = np.array([0,0,0])):
sl = Straylight(jtime=jtime, sat_pos=sat_pos, pointing_radec=pointing_radec,sun_pos=sun_pos)
def setFilterStrayLightPixel(self, jtime=2460843., sat_pos=np.array([0, 0, 0]), pointing_radec=np.array([0, 0]), sun_pos=np.array([0, 0, 0])):
sl = Straylight(jtime=jtime, sat_pos=sat_pos,
pointing_radec=pointing_radec, sun_pos=sun_pos)
if self.filter_type in _util.SPEC_FILTERS:
s_pix, spec = sl.calculateStrayLightGrating(grating = self.filter_type.upper())
if s_pix>0.8:
s_pix, spec = sl.calculateStrayLightGrating(
grating=self.filter_type.upper())
if s_pix > 0.8:
s_pix = 0.8
self.sky_background = s_pix
self.zodical_spec = spec
elif self.filter_type.lower() in [x.lower for x in _util.PHOT_FILTERS]:
s_pix = sl.calculateStrayLightFilter(filter=self.filter_type.lower())
if s_pix>1:
s_pix = sl.calculateStrayLightFilter(
filter=self.filter_type.lower())
if s_pix > 1:
s_pix = 1
self.sky_background = s_pix
self.zodical_spec = None
......@@ -144,6 +158,8 @@ class Filter(object):
full_well = 90000
throughput_file = self.filter_type.lower() + '_throughput.txt'
self.mag_limiting, self.mag_saturation = _util.calculateLimitMag(psf_fwhm=psf_fwhm, pixelSize=pix_scale, throughputFn=throughput_file, readout=5.0, skyFn=skyFn, darknoise=dark_noise, exTime=exptime, fw=full_well)
self.mag_limiting, self.mag_saturation = _util.calculateLimitMag(
psf_fwhm=psf_fwhm, pixelSize=pix_scale, throughputFn=throughput_file, readout=5.0, skyFn=skyFn, darknoise=dark_noise, exTime=exptime, fw=full_well)
print("for filter %s: mag_limiting: %.3f, mag_saturation: %.3f"%(self.filter_type, self.mag_limiting, self.mag_saturation))
print("for filter %s: mag_limiting: %.3f, mag_saturation: %.3f" %
(self.filter_type, self.mag_limiting, self.mag_saturation))
import galsim
import numpy as np
class FilterParam(object):
def __init__(self, filter_dir=None, filter_param=None):
self.param = self._filtParam(filter_param)
......@@ -33,7 +34,8 @@ class FilterParam(object):
"i": [7653.2, 1588.1, 6760.0, 8550.0, 0.4960, 0.212, 16.7, 25.9],
"z": [9600.6, 2490.5, 8240.0, 11000.0, 0.2000, 0.123, 15.7, 25.2],
"y": [10051.0, 1590.6, 9130.0, 11000.0, 0.0960, 0.037, 14.4, 24.4],
"FGS": [5000.0, 8000.0, 3000.0, 11000.0, 0.6500, 0.164, 0., 30.], # [TODO]
# [TODO]
"FGS": [5000.0, 8000.0, 3000.0, 11000.0, 0.6500, 0.164, 0., 30.],
"GU": [0.0, 0.0, 2550.0, 4200.0, 1.0, 0.037, 14.0, 26.0],
"GV": [0.0, 0.0, 4000.0, 6500.0, 1.0, 0.037, 14.0, 26.0],
......@@ -65,9 +67,10 @@ class FilterParam(object):
ifiltn = filtdir+itype+".dat"
iblim = filtParams[itype][2]
irlim = filtParams[itype][3]
ifilt = galsim.Bandpass(ifiltn,wave_type=unit,blue_limit=iblim,red_limit=irlim)
ifilt = galsim.Bandpass(
ifiltn, wave_type=unit, blue_limit=iblim, red_limit=irlim)
wave = ifilt.wave_list
ifilt = ifilt.func(wave)
filts[itype] = np.transpose(np.array([wave*10.0,ifilt]))
filts[itype] = np.transpose(np.array([wave*10.0, ifilt]))
return filtype, filts
......@@ -40,18 +40,6 @@ class FocalPlane(object):
for i in range(1, 31):
self.ignore_chips.append(i)
# if config is not None:
# self.nchip_x = config["nchip_x"]
# self.nchip_y = config["nchip_y"]
# self.npix_tot_x = config["npix_tot_x"]
# self.npix_tot_y = config["npix_tot_y"]
# self.npix_gap_x = config["npix_gap_x"]
# self.npix_gap_y = config["npix_gap_y"]
# if "chipLabelIDs" in config:
# self.chipLabelIDs = config["chipLabelIDs"]
# if "bad_chips" in config:
# self.bad_chips = config["bad_chips"]
# else:
self.nchip_x = 6
self.nchip_y = 5
self.npix_tot_x = 59516
......@@ -95,10 +83,6 @@ class FocalPlane(object):
if (xcen == None) or (ycen == None):
xcen = self.cen_pix_x
ycen = self.cen_pix_y
# 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
dudx = -np.cos(img_rot.rad) * pix_scale
dudy = +np.sin(img_rot.rad) * pix_scale
......
import numpy as np
try:
import importlib.resources as pkg_resources
except ImportError:
# Try backported to PY<37 'importlib_resources'
import importlib_resources as pkg_resources
class Telescope(object):
def __init__(self, param=None, optEffCurve_path=None):
self.diameter = 2.0 # in unit of meter
if param is not None:
self.diameter = param["diameter"]
self.pupil_area = np.pi * (0.5 * self.diameter)**2
if optEffCurve_path is not None:
self.efficiency = self._get_efficiency(optEffCurve_path)
else:
try:
with pkg_resources.files('observation_sim.instruments.data').joinpath('mirror_ccdnote.txt') as optEffCurve_path:
self.efficiency = self._get_efficiency(optEffCurve_path)
except AttributeError:
with pkg_resources.path('observation_sim.instruments.data', 'mirror_ccdnote.txt') as optEffCurve_path:
self.efficiency = self._get_efficiency(optEffCurve_path)
def _get_efficiency(self, effCurve_path):
""" Read in the efficiency of optics
for each band
Parameters:
effCurve_path: the path for efficiency file
Returns:
opticsEff: a dictionary of efficiency (a scalar) for each band
"""
f = open(effCurve_path, 'r')
for _ in range(2):
header = f.readline()
iline = 0
opticsEff = {}
for line in f:
line = line.strip()
columns = line.split()
opticsEff[str(columns[0])] = float(columns[2])
f.close()
return opticsEff
......@@ -2,4 +2,4 @@ from .Telescope import Telescope
from .Filter import Filter
from .FilterParam import FilterParam
from .FocalPlane import FocalPlane
from .Chip import Chip
\ No newline at end of file
from .chip import Chip
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