Commit 29ac5666 authored by Fang Yuedong's avatar Fang Yuedong
Browse files

Baseline

parents
import os
class ChipOutput(object):
def __init__(self, config, focal_plane, chip, filt, imgKey0="", imgKey1="", imgKey2="", exptime=150., mjdTime="", ra_cen=None, dec_cen=None, pointing_ID='0', subdir="./", prefix=""):
self.focal_plane = focal_plane
self.chip = chip
self.filt = filt
self.imgKey0 = imgKey0
self.imgKey1 = imgKey1
self.imgKey2 = imgKey2
self.exptime = exptime
self.mjdTime = mjdTime
if (ra_cen is not None) and (dec_cen is not None):
self.ra_cen = ra_cen
self.dec_cen = dec_cen
else:
self.ra_cen = config["ra_center"]
self.dec_cen = config["dec_center"]
exp_name = imgKey0 + "_%s_%s.fits"
self.chipLabel = focal_plane.getChipLabel(chip.chipID)
self.img_name = prefix + exp_name%(self.chipLabel, filt.filter_type)
# self.cat_name = self.img_name[:-5] + ".cat"
self.cat_name = 'MSC_' + config["date_obs"] + config["time_obs"] + "_" + str(pointing_ID).rjust(7, '0') + "_" + self.chipLabel.rjust(2,'0') + ".cat"
self.subdir = subdir
# hdr1 = "#ID ID_chip filter xImage yImage ra dec z mag flag SNR "
hdr1 = "#ID ID_chip filter xImage yImage ra dec z mag flag "
hdr2 = "thetaR bfrac hlr_disk hlr_bulge e1_disk e2_disk e1_bulge e2_bulge e1_total e2_total"
hdr3 = "e1PSF e2PSF e1 e2 g1 g2 e1OBS e2OBS"
hdr4 = "sed_type av redden "
hdr5 = "star_model teff logg feh\n"
# fmt1 = "%10d %4d %5s %10.3f %10.3f %15.6f %15.6f %7.4f %8.4f %2d %9.2f "
fmt1 = "%10d %4d %5s %10.3f %10.3f %15.6f %15.6f %7.4f %8.4f %2d "
fmt2 = "%8.4f %8.4f %8.4f %8.4f %8.4f %8.4f %8.4f %8.4f %8.4f %8.4f"
fmt3 = "%8.4f %8.4f %8.4f %8.4f %8.4f %8.4f %8.4f %8.4f"
fmt4 = "%2d %8.4f %8.4f "
fmt5 = "%10s %8.4f %8.4f %8.4f\n"
self.hdr = hdr1 + hdr2 + hdr3 + hdr4 + hdr5
self.fmt = fmt1 + fmt2 + fmt3 + fmt4 + fmt5
self.cat = open(os.path.join(self.subdir, self.cat_name), "w")
self.cat.write(self.hdr)
def updateHDR(self, hdr):
hdrNew = [{"name":"RDNOISE", "value":self.chip.read_noise, "comment":"read noise in e-/pixel"},
{"name":"DARK", "value":self.chip.dark_noise, "comment":"Dark noise (e-/pixel/s)"},
{"name":"EXPTIME", "value":self.exptime, "comment":"exposure time in second"},
{"name":"GAIN", "value":self.chip.gain, "comment":"CCD gain in e-/ADU"},
{"name":"SATURATE","value":65535.0, "comment":"saturation level"},
{"name":"CCDCHIP", "value":int(self.chipLabel), "comment":"chip ID in the CCD mosaic"},
{"name":"FILTER", "value":self.filt.filter_type, "comment":"filter name"},
{"name":"MJD-OBS", "value":self.mjdTime, "comment":"Modified Julian Date (MJD) of observation"},
{"name":"DATE-OBS","value":self.imgKey1, "comment":"Date of observation"},
{"name":"EQUINOX", "value":2000.0},
{"name":"RADECSYS","value":"ICRS"},
{"name":"RA", "value":self.ra_cen, "comment":"telescope pointing center"},
{"name":"DEC", "value":self.dec_cen, "comment":"telescope pointing center"},
{"name":"OBJECT", "value":"CSS-OS"},
{"name":"WCSDIM", "value":2.0, "comment":"WCS Dimensionality"},
{"name":"EXTNAME", "value":"IM1", "comment":"Extension name"},
{"name":"BSCALE", "value":1.0},
{"name":"BZERO", "value":0.0},
{"name":"OBSID", "value":self.imgKey0, "comment":"Observation ID"},
{"name":"CCDNAME", "value":"ccd"+self.chipLabel,"comment":"CCD name"},
{"name":"RSPEED", "value":10.0, "comment":"Read speed"},
{"name":"CHIPTEMP","value":-100.0, "comment":"Chip temperature"},
{"name":"DATASEC", "value":"1:%d,1:%d"%(self.chip.npix_x,self.chip.npix_y), "comment":"Data section"},
{"name":"CCDSUM", "value":self.chip.npix_x*self.chip.npix_y, "comment":"CCD pixel summing"},
{"name":"NSUM", "value":self.chip.npix_x*self.chip.npix_y, "comment":"CCD pixel summing"},
{"name":"AUTHOR", "value":"CSST-Sim Group"},
{"name":"GROUP", "value":"Weak Lensing Working Group for CSST"}]
for item in hdrNew:
hdr.add_record(item)
return hdr
# def cat_add_obj(self, obj, pos_img, snr, pos_shear, g1, g2):
def cat_add_obj(self, obj, pos_img, pos_shear, g1, g2):
ximg = pos_img.x - self.chip.bound.xmin + 1.0
yimg = pos_img.y - self.chip.bound.ymin + 1.0
e1, e2, g1, g2, e1OBS, e2OBS = obj.getObservedEll(g1, g2)
if obj.type == 'galaxy':
line = self.fmt%(obj.id, int(self.chipLabel), self.filt.filter_type, ximg, yimg, obj.ra, obj.dec, obj.z, obj.getMagFilter(self.filt), obj.param["star"], obj.thetaR, obj.bfrac, obj.hlr_disk, obj.hlr_bulge,
obj.e1_disk, obj.e2_disk, obj.e1_bulge, obj.e2_bulge, obj.e1_total, obj.e2_total,
pos_shear.g1, pos_shear.g2, e1, e2, g1, g2, e1OBS, e2OBS, obj.sed_type, obj.param['av'], obj.param['redden'], 'n', 0, 0, 0)
elif obj.type == "quasar":
line = self.fmt % (obj.id, int(self.chipLabel), self.filt.filter_type, ximg, yimg, obj.ra, obj.dec, obj.z,
obj.getMagFilter(self.filt), obj.param["star"], 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0,
pos_shear.g1, pos_shear.g2, e1, e2, g1, g2, e1OBS, e2OBS, obj.sed_type, obj.param['av'], obj.param['redden'], 'n', 0.0, 0.0, 0.0)
else:
line = self.fmt%(obj.id, int(self.chipLabel), self.filt.filter_type, ximg, yimg, obj.ra, obj.dec, obj.z, obj.getMagFilter(self.filt), obj.param["star"], 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
pos_shear.g1, pos_shear.g2, e1, e2, g1, g2, e1OBS, e2OBS, 0, 0.0, 0.0, obj.param['model_tag'], obj.param['teff'], obj.param['logg'],obj.param['feh'])
# line = self.fmt%(obj.id, int(self.chipLabel), self.filt.filter_type, ximg, yimg, obj.ra, obj.dec, obj.z, obj.getMagFilter(self.filt), obj.param["star"], pos_shear.g1, pos_shear.g2, e1, e2, g1, g2, e1OBS, e2OBS)
self.cat.write(line)
\ No newline at end of file
import galsim
import os
from astropy.time import Time as asTime
def ConfigDir(cat_dir=None, work_dir=None, data_dir=None, config_file_path=None):
path_dict = {}
# Working directory
if work_dir == None:
dirname, _ = os.path.split(os.path.abspath(__file__))
path_dict["work_dir"] = "/".join(dirname.split("/")[:-1]) + "/"
else:
path_dict["work_dir"] = work_dir
# Configuration file
if config_file_path is not None:
path_dict["config_file"] = config_file_path
else:
path_dict["config_file"] = os.path.join(path_dict["work_dir"], "ObservationSim.cfg")
# Output directories
# path_dict["output_fig_dir"] = os.path.join(path_dict["work_dir"], "figure/")
# if not os.path.exists(path_dict["output_fig_dir"]):
# os.system("mkdir %s"%path_dict["output_fig_dir"])
# path_dict["output_cat_dir"] = os.path.join(path_dict["work_dir"], "scat/")
# if not os.path.exists(path_dict["output_cat_dir"]):
# os.system("mkdir %s"%path_dict["output_cat_dir"])
path_dict["output_img_dir"] = os.path.join(path_dict["work_dir"], "simg/")
if not os.path.exists(path_dict["output_img_dir"]):
os.system("mkdir %s"%path_dict["output_img_dir"])
# Data directory
if data_dir == None:
path_dict["data_dir"] =os.path.join(path_dict["work_dir"], "data/")
else:
path_dict["data_dir"] = data_dir
# Data sub-catalogs
# Object catalog direcotry
# path_dict["cat_dir"] = os.path.join(path_dict["data_dir"], "catalog_points_7degree2/", cat_dir)
path_dict["cat_dir"] = os.path.join(path_dict["data_dir"], "Catalog_20210126")
# PSF data directory
path_dict["psf_dir"] = os.path.join(path_dict["data_dir"], "csstPSFdata/CSSOS_psf_20210108/CSST_psf_ciomp_2p5um_cycle3_ccr90_proc")
# SED catalog directory
path_dict["SED_dir"] = os.path.join(path_dict["data_dir"], "imageSims/Catalog/SEDObject")
path_dict["template_dir"] = path_dict["data_dir"] + "Templates/"
# Directories/files for instrument parameters, e.g. efficiency curves.
path_dict["filter_dir"] = os.path.join(path_dict["data_dir"], "Filters")
path_dict["ccd_dir"] = os.path.join(path_dict["data_dir"], "Filter_CCD_Mirror/ccd")
path_dict["mirror_file"] = os.path.join(path_dict["data_dir"], "Filter_CCD_Mirror/mirror_ccdnote.txt")
# Cosmic-ray data directory:
path_dict["CRdata_dir"] = os.path.join(path_dict["data_dir"], "CRdata")
# Slitless spectroscopy realted
path_dict["sls_dir"] = os.path.join(path_dict["data_dir"], "CONF/")
path_dict["normalize_dir"] = os.path.join(path_dict["data_dir"], "normalize_filter/")
return path_dict
def ReadConfig(config_filename):
"""Read in a configuration file and return the corresponding dict(s).
Parameters:
config_filename: The name of the configuration file to read.
Returns:
(list) of config dicts
"""
config = {}
config_file = open(config_filename).readlines()
nlines = len(config_file)
for i in range(nlines):
row = config_file[i].split()
if len(row) <= 1: continue # blank row
if not "#" in row:
if len(row) == 2:
key, val = row[0:2]
config.update({key:val})
else:
print("!! Something is wrong with parameter '%s'."%row[0])
return
elif row.index("#") == 2:
key, val = row[0:2]
config.update({key:val})
elif row.index("#") == 0:
continue # annotation
else:
print("!! Something is wrong with parameter '%s'."%row[0])
return
config = ParseConfig(config)
return config
def ParseConfig(config):
"""Parse the config values to the right type
Parameters:
config: raw config dict
Returns:
Parsed config dict
"""
config["ra_center"] = float(config["ra_center"])
config["dec_center"] = float(config["dec_center"])
config["psf_rcont"] = config["psf_rcont"].split(",")
config["psfRa"] = float(config["psf_rcont"][0])
config["psfCont"] = float(config["psf_rcont"][1])
config["image_rot"] = float(config["image_rot"])*galsim.degrees
config["sigma_spin"] = float(config["sigma_spin"])
config["reduced_g1"] = float(config["reduced_g1"])
config["reduced_g2"] = float(config["reduced_g2"])
config["rotateEll"] = float(config["rotateEll"])
config["reEll"] = int(config["rotateEll"]/45.0)
if config["reEll"]==0: config["reIndex"] = "P"
if config["reEll"]==1: config["reIndex"] = "X"
if config["reEll"]==2: config["reIndex"] = "N"
if config["reEll"]==3: config["reIndex"] = "Y"
config["seed_flat"] = int(config["seed_flat"])
config["seed_prnu"] = int(config["seed_prnu"])
config["seed_star"] = int(config["seed_star"])
config["seed_gal"] = int(config["seed_gal"])
config["seed_Av"] = int(config["seed_Av"])
config["bias_level"] = int(config["bias_level"])
config["df_strength"] = float(config["df_strength"])
return config
\ No newline at end of file
"""
generate image header
"""
import numpy as np
from astropy.io import fits
import astropy.wcs as pywcs
from collections import OrderedDict
from scipy import math
import random
import os
import sys
def chara2digit(char):
""" Function to judge and convert characters to digitals
Parameters
----------
"""
try:
float(char) # for int, long and float
except ValueError:
pass
return char
else:
data = float(char)
return data
def read_header_parameter(filename='global_header.param'):
""" Function to read the header parameters
Parameters
----------
"""
name = []
value = []
description = []
for line in open(filename):
line = line.strip("\n")
arr = line.split('|')
# csvReader = csv.reader(csvDataFile)
# for arr in csvReader:
name.append(arr[0])
# print(arr[0],arr[1])
value.append(chara2digit(arr[1]))
description.append(arr[2])
# 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)])
cd_rot = np.dot(mat, cd)
return cd_rot
def Header_extention(xlen = 9216, ylen = 9232, gain = 1.0, readout = 5.0, dark = 0.02,saturation=90000, row_num = 1, col_num = 1):
""" Creat an image frame for CCST with multiple extensions
Parameters
----------
"""
flag_ltm_x = [0,1,-1,1,-1]
flag_ltm_y = [0,1,1,-1,-1]
flag_ltv_x = [0,0,1,0,1]
flag_ltv_y = [0,0,0,1,1]
detector_size_x = int(xlen)
detector_size_y = int(ylen)
data_x = str(int(detector_size_x))
data_y = str(int(detector_size_y))
data_sec = '[1:'+data_x+',1:'+data_y+']'
e_header_fn = os.path.split(os.path.realpath(__file__))[0] + '/extension_header.param'
name, value, description = read_header_parameter(e_header_fn)
f = open(os.path.split(os.path.realpath(__file__))[0] + '/filter.lst')
s = f.readline()
s = s.strip("\n")
filters = s.split(' ')
s = f.readline()
s = s.strip("\n")
filterID = s.split()
s = f.readline()
s = s.strip("\n")
CCDID = s.split()
k = (row_num-1)*6+col_num
h_iter = 0
for n1,v1,d1 in zip(name, value, description):
if n1=='EXTNAME':
value[h_iter] = 'RAW,'+CCDID[k-1].rjust(2,'0')
if n1=='CCDNAME':
value[h_iter] = 'ccd' + CCDID[k-1].rjust(2,'0')
if n1=='AMPNAME':
value[h_iter] = 'ccd' + CCDID[k-1].rjust(2,'0') + ':A'
if n1=='GAIN':
value[h_iter] = gain
if n1=='RDNOISE':
value[h_iter] = readout
if n1=='SATURATE':
value[h_iter] = saturation
if n1=='CCDCHIP':
value[h_iter] = 'ccd' + CCDID[k-1].rjust(2,'0')
if n1=='CCDLABEL':
value[h_iter] = filters[k-1] + '-' + filterID[k-1]
if n1=='DATASEC':
value[h_iter] = data_sec
h_iter = h_iter + 1
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 = 60, dec = -40, pa = -23.433,psize = 0.074, row_num = 1, col_num = 1):
""" Creat a wcs frame for CCST with multiple extensions
Parameters
----------
"""
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]
x_num = 6
y_num = 5
detector_num = x_num*y_num
detector_size_x = xlen
detector_size_y = ylen
gap_y = gapy
gap_x = [gapx1,gapx2]
ra_ref = ra
dec_ref = dec
pa_aper = pa
pixel_size = psize
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
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]]])
# frame_array = np.empty((5,6),dtype=np.float64)
# print(x_center,y_center)
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
# print(i,j,x_ref,y_ref,ra_ref,dec_ref)
r_dat = OrderedDict()
# name = []
# value = []
# description = []
for k in range(1,2):
cd = np.array([[ pixel_size, 0], [0, pixel_size]])/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['EQUINOX'] = 2000.0
r_dat['WCSDIM'] = 2.0
r_dat['CTYPE1'] = 'RA---TAN'
r_dat['CTYPE2'] = 'DEC--TAN'
r_dat['CRVAL1'] = ra_ref
r_dat['CRVAL2'] = dec_ref
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]
return r_dat
# name = ['EQUINOX',
# 'WCSDIM',
# 'CTYPE1',
# 'CTYPE2',
# 'CRVAL1',
# 'CRVAL2',
# 'CRPIX1',
# 'CRPIX2',
# 'CD1_1',
# 'CD1_2',
# 'CD2_1',
# 'CD2_2']
# value = [2000.0,
# 2.0,
# 'RA---TAN',
# 'DEC--TAN',
# ra_ref,
# dec_ref,
# flag_ext_x[k]*((x_ref+flag_ext_x[k]*detector_size_x/2)-x_center),
# flag_ext_y[k]*((y_ref+flag_ext_y[k]*detector_size_y/2)-y_center),
# cd_rot[0,0],
# cd_rot[0,1],
# cd_rot[1,0],
# cd_rot[1,1]]
# description = ['Equinox of WCS',
# 'WCS Dimensionality',
# 'Coordinate type',
# 'Coordinate typ',
# 'Coordinate reference value',
# 'Coordinate reference value',
# 'Coordinate reference pixel',
# 'Coordinate reference pixel',
# 'Coordinate matrix',
# 'Coordinate matrix',
# 'Coordinate matrix',
# 'Coordinate matrix']
# return name, value, description
def generatePrimaryHeader(xlen = 9216, ylen = 9232, pointNum = '1', ra = 60, dec = -40, psize = 0.074, row_num = 1, col_num = 1, date='200930', time_obs='120000'):
# 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)
g_header_fn = os.path.split(os.path.realpath(__file__))[0] + '/global_header.param'
name, value, description = read_header_parameter(g_header_fn)
f = open(os.path.split(os.path.realpath(__file__))[0] + '/filter.lst')
s = f.readline()
s = s.strip("\n")
filters = s.split(' ')
s = f.readline()
s = s.strip("\n")
filterID = s.split()
s = f.readline()
s = s.strip("\n")
CCDID = s.split()
h_prim = fits.Header()
for i in range(len(name)):
if(name[i]=='FILENAME'):
# value[i] = 'CSST_' + date + '_' +time_obs + '_' + pointNum.rjust(6,'0') + '_' +ccdnum.rjust(2,'0')+'_raw'
# value[i] = 'CSST_' + date + '_' +time_obs + '_' + pointNum.rjust(6,'0') + '_' +CCDID[k-1].rjust(2,'0')+'_raw'
value[i] = 'MSC_' + date + time_obs + '_' + pointNum.rjust(7,'0') + '_' +CCDID[k-1].rjust(2,'0')+'_raw'
if(name[i]=='DETSIZE'):
value[i] = '[1:' + str(int(xlen)) + ',1:'+ str(int(ylen)) + ']'
if(name[i]=='PIXSCAL1'):
value[i] = str(psize)
if(name[i]=='PIXSCAL2'):
value[i] = str(psize)
h_prim[name[i]] = (value[i],description[i])
h_prim.add_comment('==================================================================',after='FILETYPE')
h_prim.add_comment('Target information')
h_prim.add_comment('==================================================================')
h_prim.add_comment('==================================================================',after='EQUINOX')
h_prim.add_comment('Exposure information')
h_prim.add_comment('==================================================================')
h_prim.add_comment('==================================================================',after='MJDEND')
h_prim.add_comment('Telescope information')
h_prim.add_comment('==================================================================')
h_prim.add_comment('==================================================================',after='REFFRAME')
h_prim.add_comment('Detector information')
h_prim.add_comment('==================================================================')
h_prim.add_comment('==================================================================',after='DETETEMP')
h_prim.add_comment('Other information')
h_prim.add_comment('==================================================================')
return h_prim
def generateExtensionHeader(xlen = 9216, ylen = 9232,ra = 60, dec = -40, pa = -23.433, gain = 1.0, readout = 5.0, dark = 0.02, saturation=90000, psize = 0.074, row_num = 1, col_num = 1):
h_ext = fits.Header()
for i in range(1,2):
# NAXIS1:Number of pixels per row; NAXIS2:Number of rows
h_ext['NAXIS1'] = xlen
h_ext['NAXIS2'] = ylen
name, value, description = Header_extention(xlen = xlen, ylen = ylen, gain = gain, readout = readout, dark = dark, saturation=saturation, row_num = row_num, col_num = col_num)
for j in range(len(name)):
# print(name[j],value[j],description[j])
h_ext[name[j]] = (value[j],description[j])
header_wcs = WCS_def(xlen = xlen, ylen = ylen, gapy = 898.0, gapx1 = 534, gapx2 = 1309, ra = ra, dec = dec, pa = pa ,psize = psize, row_num = row_num, col_num = col_num)
for k,v in header_wcs.items():
h_ext.set(k,v)
# for j in range(len(name)):
# h_ext[name[j]] = (value[j],description[j])
h_ext.add_comment('==================================================================',after='OBSID')
h_ext.add_comment('Readout information')
h_ext.add_comment('==================================================================')
h_ext.add_comment('==================================================================',after='CHIPTEMP')
h_ext.add_comment('Chip information')
h_ext.add_comment('==================================================================')
h_ext.add_comment('==================================================================',after='TRIMSEC')
h_ext.add_comment('WCS information')
h_ext.add_comment('==================================================================')
h_ext.add_comment('==================================================================',after='CD2_2')
h_ext.add_comment('Other information')
h_ext.add_comment('==================================================================')
return h_ext
def main(argv):
xlen = int(argv[1])
ylen = int(argv[2])
pointingNum = argv[3]
ra = float(argv[4])
dec = float(argv[5])
pSize = float(argv[6])
ccd_row_num = int(argv[7])
ccd_col_num = int(argv[8])
pa_aper = float(argv[9])
gain = float(argv[10])
readout = float(argv[11])
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_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)
hdul = fits.HDUList([hdu1,hdu2])
hdul.writeto(h_prim['FILENAME']+'.fits',output_verify='ignore')
# if __name__ == "__main__":
# main(sys.argv)
"""
generate image header
"""
import numpy as np
from astropy.io import fits
import astropy.wcs as pywcs
from scipy import math
import random
import os
import sys
def chara2digit(char):
""" Function to judge and convert characters to digitals
Parameters
----------
"""
try:
float(char) # for int, long and float
except ValueError:
pass
return char
else:
data = float(char)
return data
def read_header_parameter(filename='global_header.param'):
""" Function to read the header parameters
Parameters
----------
"""
name = []
value = []
description = []
for line in open(filename):
line = line.strip("\n")
arr = line.split('|')
# csvReader = csv.reader(csvDataFile)
# for arr in csvReader:
name.append(arr[0])
value.append(chara2digit(arr[1]))
description.append(arr[2])
# 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)])
cd_rot = np.dot(mat, cd)
return cd_rot
def Header_extention(xlen = 9232, ylen = 9216, gain = 1.0, readout = 5.0, dark = 0.02,saturation=90000, row_num = 1, col_num = 1):
""" Creat an image frame for CCST with multiple extensions
Parameters
----------
"""
flag_ltm_x = [0,1,-1,1,-1]
flag_ltm_y = [0,1,1,-1,-1]
flag_ltv_x = [0,0,1,0,1]
flag_ltv_y = [0,0,0,1,1]
detector_size_x = int(xlen)
detector_size_y = int(ylen)
data_x = str(int(detector_size_x))
data_y = str(int(detector_size_y))
data_sec = '[1:'+data_x+',1:'+data_y+']'
name = []
value = []
description = []
for k in range(1,2):
# f = open("extension"+str(k)+"_image.param","w")
j = row_num
i = col_num
ccdnum = str((j-1)*5+i)
name = ['EXTNAME',
'BSCALE',
'BZERO',
'OBSID',
'CCDNAME',
'AMPNAME',
'GAIN',
'RDNOISE',
'DARK',
'SATURATE',
'RSPEED',
'CHIPTEMP',
'CCDCHIP',
'DATASEC',
'CCDSUM',
'NSUM',
'LTM1_1',
'LTM2_2',
'LTV1',
'LTV2',
'ATM1_1',
'ATM2_2',
'ATV1',
'ATV2',
'DTV1',
'DTV2',
'DTM1_1',
'DTM2_2']
value = ['IM'+str(k),
1.0,
0.0,
'CSST.20200101T000000',
'ccd' + ccdnum.rjust(2,'0'),
'ccd' + ccdnum.rjust(2,'0') + ':'+str(k),
gain,
readout,
dark,
saturation,
10.0,
-100.0,
'ccd' + ccdnum.rjust(2,'0'),
data_sec,
'1 1',
'1 1',
flag_ltm_x[k],
flag_ltm_y[k],
flag_ltv_x[k]*(detector_size_x-20*2+1),
flag_ltv_y[k]*(detector_size_y+1),
flag_ltm_x[k],
flag_ltm_y[k],
flag_ltv_x[k]*(detector_size_x-20*2+1),
flag_ltv_y[k]*(detector_size_y+1),
0,
0,
1,
1]
description = ['Extension name',
' ',
' ',
'Observation ID',
'CCD name',
'Amplifier name',
'Gain (e-/ADU)',
'Readout noise (e-/pixel)',
'Dark noise (e-/pixel/s)',
'Saturation (e-)',
'Read speed',
'Chip temperature',
'CCD chip ID',
'Data section',
'CCD pixel summing',
'CCD pixel summing',
'CCD to image transformation',
'CCD to image transformation',
'CCD to image transformation',
'CCD to image transformation',
'CCD to amplifier transformation',
'CCD to amplifier transformation',
'CCD to amplifier transformation',
'CCD to amplifier transformation',
'CCD to detector transformatio',
'CCD to detector transformatio',
'CCD to detector transformatio',
'CCD to detector transformatio']
return name, value, description
##9232 9216 898 534 1309 60 -40 -23.4333
def WCS_def(xlen = 9232, ylen = 9216, gapx = 898.0, gapy1 = 534, gapy2 = 1309, ra = 60, dec = -40, pa = -23.433,psize = 0.074, row_num = 1, col_num = 1):
""" Creat a wcs frame for CCST with multiple extensions
Parameters
----------
"""
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]
x_num = 5
y_num = 6
detector_num = x_num*y_num
detector_size_x = xlen
detector_size_y = ylen
gap_x = gapx
gap_y = [gapy1,gapy2]
ra_ref = ra
dec_ref = dec
pa_aper = pa
pixel_size = psize
gap_y1_num = 3
gap_y2_num = 2
x_center = (detector_size_x*x_num+gap_x*(x_num-1))/2
y_center = (detector_size_y*y_num+gap_y[0]*gap_y1_num+gap_y[1]*gap_y2_num)/2
gap_y_map = np.array([[0,0,0,0,0],[gap_y[0],gap_y[1],gap_y[1],gap_y[1],gap_y[1]],[gap_y[1],gap_y[0],gap_y[0],gap_y[0],gap_y[0]],[gap_y[0],gap_y[0],gap_y[0],gap_y[0],gap_y[0]],[gap_y[0],gap_y[0],gap_y[0],gap_y[0],gap_y[1]],[gap_y[1],gap_y[1],gap_y[1],gap_y[1],gap_y[0]]])
frame_array = np.empty((5,6),dtype=np.float64)
# print(x_center,y_center)
j = row_num
i = col_num
ccdnum = str((j-1)*5+i)
x_ref, y_ref = (detector_size_x+gap_x)*i-gap_x-detector_size_x/2, detector_size_y*j + sum(gap_y_map[0:j,i-1]) - detector_size_y/2
# print(i,j,x_ref,y_ref,ra_ref,dec_ref)
name = []
value = []
description = []
for k in range(1,2):
cd = np.array([[ pixel_size, 0], [0, pixel_size]])/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")
name = ['EQUINOX',
'WCSDIM',
'CTYPE1',
'CTYPE2',
'CRVAL1',
'CRVAL2',
'CRPIX1',
'CRPIX2',
'CD1_1',
'CD1_2',
'CD2_1',
'CD2_2']
value = [2000.0,
2.0,
'RA---TAN',
'DEC--TAN',
ra_ref,
dec_ref,
flag_ext_x[k]*((x_ref+flag_ext_x[k]*detector_size_x/2)-x_center),
flag_ext_y[k]*((y_ref+flag_ext_y[k]*detector_size_y/2)-y_center),
cd_rot[0,0],
cd_rot[0,1],
cd_rot[1,0],
cd_rot[1,1]]
description = ['Equinox of WCS',
'WCS Dimensionality',
'Coordinate type',
'Coordinate typ',
'Coordinate reference value',
'Coordinate reference value',
'Coordinate reference pixel',
'Coordinate reference pixel',
'Coordinate matrix',
'Coordinate matrix',
'Coordinate matrix',
'Coordinate matrix']
return name, value, description
def generatePrimaryHeader(xlen = 9232, ylen = 9216,pointNum = '1', ra = 60, dec = -40, psize = 0.074, row_num = 1, col_num = 1):
# array_size1, array_size2, flux, sigma = int(argv[1]), int(argv[2]), 1000.0, 5.0
filerParm_fn = os.path.split(os.path.realpath(__file__))[0] + '/filter.lst'
f = open(filerParm_fn)
s = f.readline()
s = s.strip("\n")
filter = s.split(' ')
k = (row_num-1)*5+col_num
ccdnum = str(k)
g_header_fn = os.path.split(os.path.realpath(__file__))[0] + '/global_header.param'
name, value, description = read_header_parameter(g_header_fn)
h_prim = fits.Header()
date = '200930'
time_obs = '120000'
for i in range(len(name)):
if(name[i]=='FILTER'):
value[i] = filter[k-1]
if(name[i]=='FILENAME'):
value[i] = 'CSST_' + date + '_' +time_obs + '_' + pointNum.rjust(6,'0') + '_' +ccdnum.rjust(2,'0')+'_raw'
if(name[i]=='DETSIZE'):
value[i] = '[1:' + str(int(xlen)) + ',1:'+ str(int(ylen)) + ']'
if(name[i]=='PIXSCAL1'):
value[i] = str(psize)
if(name[i]=='PIXSCAL2'):
value[i] = str(psize)
h_prim[name[i]] = (value[i],description[i])
h_prim.add_comment('==================================================================',after='FILETYPE')
h_prim.add_comment('Target information')
h_prim.add_comment('==================================================================')
h_prim.add_comment('==================================================================',after='EQUINOX')
h_prim.add_comment('Exposure information')
h_prim.add_comment('==================================================================')
h_prim.add_comment('==================================================================',after='MJDEND')
h_prim.add_comment('Telescope information')
h_prim.add_comment('==================================================================')
h_prim.add_comment('==================================================================',after='REFFRAME')
h_prim.add_comment('Detector information')
h_prim.add_comment('==================================================================')
h_prim.add_comment('==================================================================',after='FILTER')
h_prim.add_comment('Other information')
h_prim.add_comment('==================================================================')
return h_prim
def generateExtensionHeader(xlen = 9232, ylen = 9216,ra = 60, dec = -40, pa = -23.433, gain = 1.0, readout = 5.0, dark = 0.02, saturation=90000, psize = 0.074, row_num = 1, col_num = 1):
h_ext = fits.Header()
for i in range(1,2):
# NAXIS1:Number of pixels per row; NAXIS2:Number of rows
h_ext['NAXIS1'] = xlen
h_ext['NAXIS2'] = ylen
name, value, description = Header_extention(xlen = xlen, ylen = ylen, gain = gain, readout = readout, dark = dark, saturation=saturation, row_num = row_num, col_num = col_num)
for j in range(len(name)):
h_ext[name[j]] = (value[j],description[j])
name, value, description = WCS_def(xlen = xlen, ylen = ylen, gapx = 898.0, gapy1 = 534, gapy2 = 1309, ra = ra, dec = dec, pa = pa ,psize = psize, row_num = row_num, col_num = col_num)
for j in range(len(name)):
h_ext[name[j]] = (value[j],description[j])
h_ext.add_comment('==================================================================',after='OBSID')
h_ext.add_comment('Readout information')
h_ext.add_comment('==================================================================')
h_ext.add_comment('==================================================================',after='CHIPTEMP')
h_ext.add_comment('Chip information')
h_ext.add_comment('==================================================================')
h_ext.add_comment('==================================================================',after='DTM2_2')
h_ext.add_comment('WCS information')
h_ext.add_comment('==================================================================')
return h_ext
def main(argv):
xlen = int(argv[1])
ylen = int(argv[2])
pointingNum = argv[3]
ra = float(argv[4])
dec = float(argv[5])
pSize = float(argv[6])
ccd_row_num = int(argv[7])
ccd_col_num = int(argv[8])
pa_aper = float(argv[9])
gain = float(argv[10])
readout = float(argv[11])
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_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)
hdul = fits.HDUList([hdu1,hdu2])
hdul.writeto(h_prim['FILENAME']+'.fits',output_verify='ignore')
# if __name__ == "__main__":
# main(sys.argv)
from .ImageHeader import generatePrimaryHeader
from .ImageHeader import generateExtensionHeader
\ No newline at end of file
EXTNAME|IM1|Extension name
EXTVER|1|The ID number
BSCALE|1|
BZERO|8|
OBSID|CSST.20200101T000000|Observation ID
CCDNAME|CCD02|CCD name
AMPNAME|ccd02:B|Amplifier name
GAIN|1.00|Gain (e/ADU)
RDNOISE|5.00|Readout noise (e)
SATURATE|90000.0|Saturation (ADU)
RSPEED|10.0|Read speed (in MHz)
CHIPTEMP|-100.0|Chip temperature (in K)
CCDCHIP|1|CCD chip ID
CCDLABEL|GI-1|CCD chip label
HOSCAN|2000|Horizontal overscan width, per readout channel
VOSCAN|2000|Virtical overscan height, per readout channel
CCDSUM|1 1|CCD pixel summing
CCDSEC|[1:9216:9232]|CCD section
AMPSEC|[1:9216:9232]|Amplifier section
DATASEC|[1:9216,1: 9232]|Data section
DETSEC|[1:9216,1:9232]|Detector section
BIASSEC|[9216:9217,9232:9234]|Bias section
TRIMSEC|[1:9216,1:9232]|Trim section
WCSDIM|2|WCS dimensionality
EQUINOX|2000|Epoch (year)
CTYPE1|RA---TPV|Coordinate type
CTYPE2|DEC---TPV|Coordinate type
CRVAL1|1.00|Coordinate reference value
CRVAL2|1.00|Coordinate reference value
CRPIX1|1.00|Coordinate reference pixel
CRPIX2|1.00|Coordinate reference pixel
CD1_1|1|Coordinate matrix
CD2_1|0|Coordinate matrix
CD1_2|0|Coordinate matrix
CD2_2|1|Coordinate matrix
CHECKSUM|C65|SHA256 checksum of global headers
\ No newline at end of file
GV GI y z y GI GU r u NUV i GV GU g NUV NUV g GU GV i NUV u r GU GI y z y GI GV
3 3 3 1 1 1 3 2 2 1 1 1 4 2 3 2 1 1 4 2 4 1 1 2 4 2 2 4 2 2
26 21 16 11 6 1 27 22 17 12 7 2 28 23 18 13 8 3 29 24 19 14 9 4 30 25 20 15 10 5
DATE|2020-01-01|Date this file was written
FILENAME|CSST_01_999999|Name of file
FILETYPE|zero|Type of data
EXPNUM|99999999|Exposure sequence number
TARGNAME|CSST_|Observation title
RADECSYS|FK5|Default coordinate system
RADECEQ|2000.0|Default equinox
RA|00:00:00.00|RA of observation (hr)
DEC|+00:00:00.00|Dec of observation (deg)
EQUINOX|2000|Epoch(year)
TIMESYS|UT|Time system
DATE-OBS|2020-01-01|UTC date of start of observation
TIME-OBS|00:00:00|UTC time of start of observation
EXPTIME|0.000|Exposure time (sec)
MJD-OBS|58849.00000|MJD of observation start
MJDSTART|58849.00000|MJD of observation start
MJDEND|58849.00000|MJD of observation end
ST|16:32:58|Sidereal time
TELESCOP|CSST|Telescope name
TELRADEC|ICRS|Telescope coordinate system
TELEQUIN|2000|Equinox of tel coords
TELRA|00:00:00.00|RA of telescope (hr)
TELDEC|00:00:00.00|Dec of telescope (deg)
TELFOCUS|-9999|Telescope focus
SUNANGLE|30.00|Angle between sun and direction
MOONANGL|30.00|Angle between moon and direction
SUN_ALT|30.00|Sun altitude
REFFRAME|GSC1|guide star catalog version
INSTRUME|MBI|Instrument used to acquire data
DETSIZE|[1:9232,1:9216]|Detector size
NUMDETE|1|Number of detectors
NUMAMPS|4|Number of amplifiers
PIXSCAL1|0.074|Pixel scale for axis 1 (arcsec/pixel)
PIXSCAL2|0.074|Pixel scale for axis 2 (arcsec/pixel)
SHUTSTAT|closed|Shutter status
DETETEMP|-100.00|Detector temperature
CHECHSUM|abcdefg|checksum of global headers
ITL-HEAD|OK|ITL Header flag
N-AMPS-X|2|Number of amplifiers in X
N-AMPS-Y|2|Number of amplifiers in Y
from .Config import ConfigDir, ReadConfig, ParseConfig
from .ChipOutput import ChipOutput
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
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