Commit 36189a3e authored by JX's avatar JX 😵
Browse files

Merge remote-tracking branch 'origin/develop'

parents dd26d370 27646bc4
Pipeline #4509 passed with stage
in 0 seconds
......@@ -4,7 +4,8 @@ from datetime import datetime
import argparse
from astropy.time import Time
from ObservationSim.Config import Pointing
from observation_sim.config import Pointing
def parse_args():
'''
......@@ -12,19 +13,25 @@ def parse_args():
can be set in the .yaml config file as well.
'''
parser = argparse.ArgumentParser()
parser.add_argument('--config_file', type=str, required=True, help='.yaml config file for simulation settings.')
parser.add_argument('--catalog', type=str, help='name of the catalog interface class to be loaded.')
parser.add_argument('-c', '--config_dir', type=str, help='Directory that houses the .yaml config file.')
parser.add_argument('-d', '--data_dir', type=str, help='Directory that houses the input data.')
parser.add_argument('-w', '--work_dir', type=str, help='The path for output.')
parser.add_argument('--config_file', type=str, required=True,
help='.yaml config file for simulation settings.')
parser.add_argument('--catalog', type=str,
help='name of the catalog interface class to be loaded.')
parser.add_argument('-c', '--config_dir', type=str,
help='Directory that houses the .yaml config file.')
parser.add_argument('-d', '--data_dir', type=str,
help='Directory that houses the input data.')
parser.add_argument('-w', '--work_dir', type=str,
help='The path for output.')
return parser.parse_args()
def generate_pointing_list(config, pointing_filename=None, data_dir=None):
pointing_list = []
# Only valid when the pointing list does not contain time stamp column
t0 = datetime(2021, 5, 25, 12, 0, 0)
delta_t = 10. # Time elapsed between exposures (minutes)
delta_t = 10. # Time elapsed between exposures (minutes)
# Calculate starting time(s) for CAL exposures
# NOTE: temporary implementation
......@@ -36,7 +43,7 @@ def generate_pointing_list(config, pointing_filename=None, data_dir=None):
obs_config_file = config['obs_setting']["obs_config_file"]
else:
obs_config_file = None
# if pointing_filename and data_dir:
if pointing_filename:
if data_dir:
......@@ -48,7 +55,7 @@ def generate_pointing_list(config, pointing_filename=None, data_dir=None):
# header = f.readline()
iline = 0
for line in f:
if len(line.strip()) == 0 or line[0]=='#':
if len(line.strip()) == 0 or line[0] == '#':
continue
if run_pointings and iline not in run_pointings:
iline += 1
......@@ -77,12 +84,13 @@ def generate_pointing_list(config, pointing_filename=None, data_dir=None):
exp_time=exp_time,
pointing_type='SCI',
obs_config_file=obs_config_file
)
)
t += delta_t * 60.
pointing_list.append(pointing)
ipoint += 1
return pointing_list
def make_run_dirs(work_dir, run_name, pointing_list):
if not os.path.exists(work_dir):
try:
......@@ -97,6 +105,7 @@ def make_run_dirs(work_dir, run_name, pointing_list):
pass
return imgDir
def make_output_pointing_dir(path_dict, config, pointing_ID=0):
imgDir = os.path.join(path_dict["work_dir"], config["run_name"])
if not os.path.exists(imgDir):
......@@ -113,6 +122,7 @@ def make_output_pointing_dir(path_dict, config, pointing_ID=0):
pass
return subImgdir, prefix
def get_shear_field(config):
if not config["shear_setting"]["shear_type"] in ["constant", "catalog"]:
raise ValueError("Please set a right 'shear_method' parameter.")
......@@ -125,4 +135,4 @@ def get_shear_field(config):
else:
g1, g2 = 0., 0.
nshear = 0
return g1, g2, nshear
\ No newline at end of file
return g1, g2, nshear
......@@ -7,28 +7,32 @@ except ImportError:
# Try backported to PY<37 'importlib_resources'
import importlib_resources as pkg_resources
def checkInputList(input_list, n):
if not isinstance(input_list, list):
raise TypeError("Input type is not list!", input_list)
for i in input_list:
if type(i) != type(1.1):
if type(i) != type(1):
raise TypeError("Input list's element is not float or int!", input_list)
raise TypeError(
"Input list's element is not float or int!", input_list)
if len(input_list) != n:
raise RuntimeError("Length of input list is not equal to stars' number!", input_list)
raise RuntimeError(
"Length of input list is not equal to stars' number!", input_list)
def on_orbit_obs_position(input_ra_list, input_dec_list, input_pmra_list, input_pmdec_list, input_rv_list, input_parallax_list, input_nstars, input_x, input_y, input_z, input_vx, input_vy, input_vz, input_epoch, input_date_str, input_time_str, lib_path=None):
#Check input parameters
# Check input parameters
if not isinstance(input_nstars, int):
raise TypeError("Parameter 7 is not int!", input_nstars)
checkInputList(input_ra_list, input_nstars)
checkInputList(input_dec_list, input_nstars)
checkInputList(input_pmra_list, input_nstars)
checkInputList(input_pmdec_list, input_nstars)
checkInputList(input_rv_list, input_nstars)
checkInputList(input_parallax_list, input_nstars)
if not isinstance(input_x, float):
raise TypeError("Parameter 8 is not double!", input_x)
if not isinstance(input_y, float):
......@@ -41,69 +45,77 @@ def on_orbit_obs_position(input_ra_list, input_dec_list, input_pmra_list, input_
raise TypeError("Parameter 12 is not double!", input_vy)
if not isinstance(input_vz, float):
raise TypeError("Parameter 13 is not double!", input_vz)
#Convert km -> m
input_x = input_x*1000.0
input_y = input_y*1000.0
# Convert km -> m
input_x = input_x*1000.0
input_y = input_y*1000.0
input_z = input_z*1000.0
input_vx = input_vx*1000.0
input_vy = input_vy*1000.0
input_vz = input_vz*1000.0
input_vz = input_vz*1000.0
if not isinstance(input_date_str, str):
raise TypeError("Parameter 15 is not string!", input_date_str)
else:
input_date_str = input_date_str.strip()
if not (input_date_str[4]=="-" and input_date_str[7]=="-"):
if not (input_date_str[4] == "-" and input_date_str[7] == "-"):
raise TypeError("Parameter 15 format error (1)!", input_date_str)
else:
tmp = input_date_str.split("-")
if len(tmp) != 3:
raise TypeError("Parameter 15 format error (2)!", input_date_str)
raise TypeError(
"Parameter 15 format error (2)!", input_date_str)
input_year = int(tmp[0])
input_month = int(tmp[1])
input_day = int(tmp[2])
if not (input_year>=1900 and input_year<=2100):
raise TypeError("Parameter 15 year range error [1900 ~ 2100]!", input_year)
if not (input_month>=1 and input_month<=12):
raise TypeError("Parameter 15 month range error [1 ~ 12]!", input_month)
if not (input_day>=1 and input_day<=31):
raise TypeError("Parameter 15 day range error [1 ~ 31]!", input_day)
if not (input_year >= 1900 and input_year <= 2100):
raise TypeError(
"Parameter 15 year range error [1900 ~ 2100]!", input_year)
if not (input_month >= 1 and input_month <= 12):
raise TypeError(
"Parameter 15 month range error [1 ~ 12]!", input_month)
if not (input_day >= 1 and input_day <= 31):
raise TypeError(
"Parameter 15 day range error [1 ~ 31]!", input_day)
if not isinstance(input_time_str, str):
raise TypeError("Parameter 16 is not string!", input_time_str)
else:
input_time_str = input_time_str.strip()
if not (input_time_str[2]==":" and input_time_str[5]==":"):
if not (input_time_str[2] == ":" and input_time_str[5] == ":"):
raise TypeError("Parameter 16 format error (1)!", input_time_str)
else:
tmp = input_time_str.split(":")
if len(tmp) != 3:
raise TypeError("Parameter 16 format error (2)!", input_time_str)
raise TypeError(
"Parameter 16 format error (2)!", input_time_str)
input_hour = int(tmp[0])
input_minute = int(tmp[1])
input_second = float(tmp[2])
if not (input_hour>=0 and input_hour<=23):
raise TypeError("Parameter 16 hour range error [0 ~ 23]!", input_hour)
if not (input_minute>=0 and input_minute<=59):
raise TypeError("Parameter 16 minute range error [0 ~ 59]!", input_minute)
if not (input_second>=0 and input_second<60.0):
raise TypeError("Parameter 16 second range error [0 ~ 60)!", input_second)
#Inital dynamic lib
if not (input_hour >= 0 and input_hour <= 23):
raise TypeError(
"Parameter 16 hour range error [0 ~ 23]!", input_hour)
if not (input_minute >= 0 and input_minute <= 59):
raise TypeError(
"Parameter 16 minute range error [0 ~ 59]!", input_minute)
if not (input_second >= 0 and input_second < 60.0):
raise TypeError(
"Parameter 16 second range error [0 ~ 60)!", input_second)
# Inital dynamic lib
try:
with pkg_resources.files('ObservationSim.Astrometry.lib').joinpath("libshao.so") as lib_path:
with pkg_resources.files('observation_sim.astrometry.lib').joinpath("libshao.so") as lib_path:
shao = cdll.LoadLibrary(lib_path)
except AttributeError:
with pkg_resources.path('ObservationSim.Astrometry.lib', "libshao.so") as lib_path:
with pkg_resources.path('observation_sim.astrometry.lib', "libshao.so") as lib_path:
shao = cdll.LoadLibrary(lib_path)
shao.onOrbitObs.restype = c_int
d3 = c_double * 3
shao.onOrbitObs.argtypes = [c_double, c_double, c_double, c_double, c_double, c_double, \
c_int, c_int, c_int, c_int, c_int, c_double, \
c_double, POINTER(d3), POINTER(d3), \
c_int, c_int, c_int, c_int, c_int, c_double, \
POINTER(c_double), POINTER(c_double) ]
shao.onOrbitObs.argtypes = [c_double, c_double, c_double, c_double, c_double, c_double,
c_int, c_int, c_int, c_int, c_int, c_double,
c_double, POINTER(d3), POINTER(d3),
c_int, c_int, c_int, c_int, c_int, c_double,
POINTER(c_double), POINTER(c_double)]
output_ra_list = list()
output_dec_list = list()
for i in range(input_nstars):
......@@ -118,17 +130,17 @@ def on_orbit_obs_position(input_ra_list, input_dec_list, input_pmra_list, input_
v3 = d3(input_vx, input_vy, input_vz)
input_year_c = c_int(input_year)
input_month_c = c_int(input_month)
input_day_c = c_int(input_day)
input_day_c = c_int(input_day)
input_hour_c = c_int(input_hour)
input_minute_c = c_int(input_minute)
input_second_c = c_double(input_second)
input_second_c = c_double(input_second)
DAT = c_double(37.0)
output_ra = c_double(0.0)
output_dec = c_double(0.0)
rs = shao.onOrbitObs(input_ra, input_dec, input_parallax, input_pmra, input_pmdec, input_rv, \
input_year_c, input_month_c, input_day_c, input_hour_c, input_minute_c, input_second_c, \
DAT, byref(p3), byref(v3), \
input_year_c, input_month_c, input_day_c, input_hour_c, input_minute_c, input_second_c, \
rs = shao.onOrbitObs(input_ra, input_dec, input_parallax, input_pmra, input_pmdec, input_rv,
input_year_c, input_month_c, input_day_c, input_hour_c, input_minute_c, input_second_c,
DAT, byref(p3), byref(v3),
input_year_c, input_month_c, input_day_c, input_hour_c, input_minute_c, input_second_c,
byref(output_ra), byref(output_dec))
if rs != 0:
raise RuntimeError("Calculate error!")
......
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
......@@ -23,7 +24,7 @@ class Pointing(object):
self.pointing_type_code = pointing_type_code
self.obs_id = pointing_id
self.survey_field_type = 'WIDE'
self.jdt = 0.
self.jdt = 0.
self.obs_config_file = obs_config_file
self.t_shutter_open = t_shutter_open
self.t_shutter_close = t_shutter_close
......@@ -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:
......@@ -52,7 +52,7 @@ class Pointing(object):
elif filter_type.lower() in ['u', 'g', 'r', 'i', 'z']:
return 150. * 2
else:
return max(150., self.exp_time) # [TODO] for FGS
return max(150., self.exp_time) # [TODO] for FGS
elif self.survey_field_type == 'DEEP':
if filter_type in _util.SPEC_FILTERS:
return 250. * 4 * 4
......@@ -62,8 +62,7 @@ class Pointing(object):
elif filter_type.lower() in ['u', 'g', 'r', 'i', 'z']:
return 250. * 2 * 4
else:
return max(150., self.exp_time) # [TODO] for FGS
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
......@@ -91,7 +90,7 @@ class Pointing(object):
# [TODO] Can also define other survey types
if is_deep != -1.0:
self.survey_field_type = "DEEP"
if not self.obs_config_file:
self.obs_config_file = str(columns[20])
with open(self.obs_config_file, "r") as stream:
......@@ -99,7 +98,7 @@ class Pointing(object):
self.obs_param = yaml.safe_load(stream)
except yaml.YAMLError as exc:
print(exc)
self.pointing_type_code = columns[21][0:3]
self.obs_id = columns[21][3:]
......@@ -108,18 +107,19 @@ 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)
except OSError:
pass
self.output_prefix = get_obs_id(
img_type=self.pointing_type,
project_cycle=overall_config["project_cycle"],
run_counter=overall_config["run_counter"],
pointing_id=self.obs_id,
pointing_type_code = self.pointing_type_code)
img_type=self.pointing_type,
project_cycle=overall_config["project_cycle"],
run_counter=overall_config["run_counter"],
pointing_id=self.obs_id,
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'}
return file_type[img_type]
\ No newline at end of file
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
......@@ -33,7 +34,7 @@ def chara2digit(char):
"""
try:
float(char) # for int, long and float
float(char) # for int, long and float
except ValueError:
pass
return char
......@@ -66,41 +67,43 @@ def read_header_parameter(filename='global_header.param'):
# print(value)
return name, value, description
def rotate_CD_matrix(cd, pa_aper):
"""Rotate CD matrix
Parameters
----------
cd: (2,2) array
CD matrix
pa_aper: float
Position angle, in degrees E from N, of y axis of the detector
Returns
-------
cd_rot: (2,2) array
Rotated CD matrix
Comments
--------
`astropy.wcs.WCS.rotateCD` doesn't work for non-square pixels in that it
doesn't preserve the pixel scale! The bug seems to come from the fact
that `rotateCD` assumes a transposed version of its own CD matrix.
"""
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
......@@ -189,11 +191,11 @@ def WCS_def(xlen = 9216, ylen = 9232, gapy = 898.0, gapx1 = 534, gapx2 = 1309, r
r_dat['CTYPE2'] = 'DEC--TAN'
r_dat['CRVAL1'] = ra_ref
r_dat['CRVAL2'] = dec_ref
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
for k in range(1,2):
cd = np.array([[ pixel_scale, 0], [0, pixel_scale]])/3600.*flag_x[k]
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):
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,12 +274,13 @@ 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]))
wcs_new = fit_wcs_from_points(xy=np.array([x_pixs, y_pixs]),
world_coords=SkyCoord(sky_coors, frame="icrs", unit="deg"), projection='TAN')
world_coords=SkyCoord(sky_coors, frame="icrs", unit="deg"), projection='TAN')
# print(wcs_new)
# test_center = wcs_new.wcs_pix2world(np.array([[xlen / 2, ylen / 2]]), 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"),
wcs_new = fit_wcs_from_points(xy=np.array([x_pixs, y_pixs]),
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')
return r_dat
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,14 +416,15 @@ 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
h_prim['OBSID'] = OBS_id
# h_prim['TELFOCUS'] = 'f/14'
h_prim['EXPTIME'] = exptime
# # Define file types
# file_type = {'SCI':'SCIE', 'BIAS':'BIAS', 'DARK':'DARK', 'FLAT':'FLAT', 'CRS':'CRS', 'CRD':'CRD','CALS':'CALS','CALF':'CALF'}
# h_prim['FILETYPE'] = file_type[im_type]
......@@ -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]
......@@ -467,21 +482,25 @@ def generatePrimaryHeader(xlen = 9216, ylen = 9232, pointing_id = '00000001', po
h_prim['VELO0_Z'] = sat_vel[2]
# h_prim['RA_PNT0'] = ra_hms
# h_prim['DEC_PNT0'] = dec_hms
# Get version of CSSTSim Package
from pkg_resources import get_distribution
# 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)
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)
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)
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