Commit ef8c4609 authored by Fang Yuedong's avatar Fang Yuedong
Browse files

modified fits header etc.

parent 3f932427
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=""):
def __init__(self, config, focal_plane, chip, filt, imgKey0="", imgKey1="", imgKey2="", exptime=150., mjdTime="", ra_cen=None, dec_cen=None, pointing_type='MS', pointing_ID='0', subdir="./", prefix=""):
self.focal_plane = focal_plane
self.chip = chip
self.filt = filt
......@@ -25,18 +25,19 @@ class ChipOutput(object):
# 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"
hdr2 = "thetaR bfrac hlr_disk hlr_bulge e1_disk e2_disk e1_bulge e2_bulge "
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"
fmt2 = "%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
if pointing_type == 'MS':
self.cat = open(os.path.join(self.subdir, self.cat_name), "w")
self.cat.write(self.hdr)
......@@ -79,15 +80,14 @@ class ChipOutput(object):
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,
obj.e1_disk, obj.e2_disk, obj.e1_bulge, obj.e2_bulge,
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,
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,
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)
......@@ -24,9 +24,9 @@ def ConfigDir(cat_dir=None, work_dir=None, data_dir=None, config_file_path=None)
# 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"])
# 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:
......
......@@ -233,7 +233,7 @@ def WCS_def(xlen = 9216, ylen = 9232, gapy = 898.0, gapx1 = 534, gapx2 = 1309, r
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', im_type = 'MS'):
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', im_type = 'MS', exptime=150.):
# array_size1, array_size2, flux, sigma = int(argv[1]), int(argv[2]), 1000.0, 5.0
......@@ -262,12 +262,19 @@ def generatePrimaryHeader(xlen = 9216, ylen = 9232, pointNum = '1', ra = 60, dec
h_prim['FILENAME'] = 'MSC_' + im_type + '_' + date + time_obs + '_1' + pointNum.rjust(8,'0') + '_' +CCDID[k-1].rjust(2,'0')+'_raw'
h_prim['DATE'] = '20'+date[0:2]+'-' + date[2:4]+'-'+date[4:6]
h_prim['TIME'] = time_obs[0:2]+':'+time_obs[2:4]+':'+time_obs[4:6]
h_prim['DATE-OBS'] = '20'+date[0:2]+'-' + date[2:4]+'-'+date[4:6]
h_prim['TIME-OBS'] = time_obs[0:2]+':'+time_obs[2:4]+':'+time_obs[4:6]
h_prim['DETECTOR'] = 'CCD'+CCDID[k-1].rjust(2,'0')
h_prim['RA_OBJ'] = ra
h_prim['DEC_OBJ'] = dec
h_prim['OBJECT'] = '1'+ pointNum.rjust(8,'0')
h_prim['OBSID'] = '1'+ pointNum.rjust(8,'0')
h_prim['TELFOCUS'] = 'f/14'
h_prim['EXPTIME'] = exptime
# Define file types
file_type = {'MS':'sci', 'CLB':'zero', 'CLD':'dark', 'CLF':'flat', 'CRS':'cosmic_ray', 'CRD':'cosmic_ray'}
h_prim['FILETYPE'] = file_type[im_type]
co = coord.SkyCoord(ra, dec, unit='deg')
......@@ -277,13 +284,14 @@ def generatePrimaryHeader(xlen = 9216, ylen = 9232, pointNum = '1', ra = 60, dec
h_prim['RA_NOM'] = ra_hms
h_prim['DEC_NOM'] = dec_hms
h_prim['PIXSCAL1'] = str(psize)
h_prim['PIXSCAL2'] = str(psize)
h_prim['PIXSCAL1'] = psize
h_prim['PIXSCAL2'] = psize
ttt = h_prim['DATE'] + 'T' + h_prim['TIME']
tstart = Time(ttt)
h_prim['EXPSTART'] = round(tstart.mjd, 5)
tend = Time(tstart.cxcsec + h_prim['EXPTIME'], format="cxcsec")
# tend = Time(tstart.cxcsec + h_prim['EXPTIME'], format="cxcsec")
tend = Time(tstart.mjd + h_prim['EXPTIME']/86400., format="mjd")
h_prim['EXPEND'] = round(tend.mjd, 5)
return h_prim
......@@ -315,6 +323,7 @@ def generateExtensionHeader(xlen = 9216, ylen = 9232,ra = 60, dec = -40, pa = -2
h_ext['GAIN1'] = gain
h_ext['RDNOISE1'] = readout
h_ext['CCDCHIP'] = 'ccd' + CCDID[k-1].rjust(2,'0')
h_ext['POS_ANG'] = pa
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)
......
......@@ -5,6 +5,9 @@ import Instrument.Chip.Effects as effects
from Instrument.FocalPlane import FocalPlane
from astropy.table import Table
from numpy.random import Generator, PCG64
from Config.Header import generatePrimaryHeader, generateExtensionHeader
from astropy.io import fits
from datetime import datetime
class Chip(FocalPlane):
def __init__(self, chipID, ccdEffCurve_dir, CRdata_dir, normalize_dir=None, sls_dir=None, config=None, treering_func=None):
......@@ -33,6 +36,10 @@ class Chip(FocalPlane):
self.bias_level = float(config["bias_level"])
self.overscan = 1000
self.exptime = 150 # second
# self.dark_exptime = 300 # second
# self.flat_exptime = 150 # second
self.dark_exptime = float(config['dark_exptime'])
self.flat_exptime = float(config['flat_exptime'])
# A chip ID must be assigned
self.chipID = int(chipID)
......@@ -297,7 +304,54 @@ class Chip(FocalPlane):
if self.chipID == 30: confFile = ['CSST_GI8.conf', 'CSST_GI7.conf']
return confFile
def addEffects(self, config, img, chip_output, filt, exptime=150., pointing_ID=0):
def generateHeader(self, ra_cen, dec_cen, img_rot, im_type, pointing_ID, date_obs, time_obs, exptime=150.):
h_prim = generatePrimaryHeader(
xlen=self.npix_x,
ylen=self.npix_y,
pointNum = str(pointing_ID),
ra=ra_cen,
dec=dec_cen,
psize=self.pix_scale,
row_num=self.rowID,
col_num=self.colID,
date=date_obs,
time_obs=time_obs,
im_type = im_type,
exptime=exptime
)
h_ext = generateExtensionHeader(
xlen=self.npix_x,
ylen=self.npix_y,
ra=ra_cen,
dec=dec_cen,
pa=img_rot.deg,
gain=self.gain,
readout=self.read_noise,
dark=self.dark_noise,
saturation=90000,
psize=self.pix_scale,
row_num=self.rowID,
col_num=self.colID,
extName='raw')
return h_prim, h_ext
def outputCal(self, img, ra_cen, dec_cen, img_rot, im_type, pointing_ID, date_obs, time_obs, output_dir, exptime=150.):
h_prim, h_ext = self.generateHeader(
ra_cen=ra_cen,
dec_cen=dec_cen,
img_rot=img_rot,
im_type=im_type,
pointing_ID=pointing_ID,
date_obs=date_obs,
time_obs=time_obs,
exptime=exptime)
hdu1 = fits.PrimaryHDU(header=h_prim)
hdu2 = fits.ImageHDU(img.array, header=h_ext)
hdu1 = fits.HDUList([hdu1, hdu2])
fname = os.path.join(output_dir, h_prim['FILENAME']+'.fits')
hdu1.writeto(fname, output_verify='ignore', overwrite=True)
def addEffects(self, config, img, chip_output, filt, ra_cen, dec_cen, img_rot, exptime=150., pointing_ID=0, timestamp_obs=1621915200, pointing_type='MS'):
SeedGainNonuni=int(config["seed_gainNonUniform"])
SeedBiasNonuni=int(config["seed_biasNonUniform"])
SeedRnNonuni = int(config["seed_rnNonUniform"])
......@@ -356,7 +410,7 @@ class Chip(FocalPlane):
img += self.dark_noise*exptime
# Add cosmic-rays
if config["cosmic_ray"].lower() == "y":
if config["cosmic_ray"].lower() == "y" and pointing_type=='MS':
print(" Adding Cosmic-Ray", flush=True)
cr_map = effects.produceCR_Map(
xLen=self.npix_x, yLen=self.npix_y,
......@@ -364,13 +418,28 @@ class Chip(FocalPlane):
cr_pixelRatio=0.003,
gain=self.gain,
attachedSizes=self.attachedSizes,
seed=SeedCosmicRay+pointing_ID*30+self.chipID)
seed=SeedCosmicRay+pointing_ID*30+self.chipID) # seed: obj-imaging:+0; bias:+1; dark:+2; flat:+3;
img += cr_map
cr_map[cr_map > 65535] = 65535
cr_map[cr_map < 0] = 0
crmap_gsimg = galsim.Image(cr_map, dtype=np.uint16)
del cr_map
# crmap_gsimg.write("%s/CosmicRay_%s_1.fits" % (chip_output.subdir, self.chipID))
crmap_gsimg.write("%s/CosmicRay_%s.fits" % (chip_output.subdir, self.chipID))
# crmap_gsimg.write("%s/CosmicRay_%s.fits" % (chip_output.subdir, self.chipID))
datetime_obs = datetime.fromtimestamp(timestamp_obs)
date_obs = datetime_obs.strftime("%y%m%d")
time_obs = datetime_obs.strftime("%H%M%S")
self.outputCal(
img=crmap_gsimg,
ra_cen=ra_cen,
dec_cen=dec_cen,
img_rot=img_rot,
im_type='CRS',
pointing_ID=pointing_ID,
date_obs=date_obs,
time_obs=time_obs,
output_dir=chip_output.subdir,
exptime=150.)
del crmap_gsimg
# Add Bias level
......@@ -383,7 +452,7 @@ class Chip(FocalPlane):
seed=SeedBiasNonuni+self.chipID)
# Bias output
if config["bias_output"].lower() == "y":
if config["bias_output"].lower() == "y" and pointing_type=='CAL':
print(" Output N frame Bias files", flush=True)
NBias = int(config["NBias"])
for i in range(NBias):
......@@ -392,6 +461,18 @@ class Chip(FocalPlane):
bias_level=float(config["bias_level"]),
ncombine=1, read_noise=self.read_noise, gain=1,
seed=SeedBiasNonuni+self.chipID)
if config["cosmic_ray"].lower() == "y":
if config["cray_differ"].lower() == "y":
cr_map = effects.produceCR_Map(
xLen=self.npix_x, yLen=self.npix_y,
exTime=0.01,
cr_pixelRatio=0.003*0.01/self.exptime,
gain=self.gain,
attachedSizes=self.attachedSizes,
seed=SeedCosmicRay+pointing_ID*30+self.chipID+1)
# seed: obj-imaging:+0; bias:+1; dark:+2; flat:+3;
BiasCombImg += cr_map
del cr_map
# Non-Linearity for Bias
if config["non_linear"].lower() == "y":
......@@ -408,11 +489,26 @@ class Chip(FocalPlane):
BiasCombImg.replaceNegative(replace_value=0)
BiasCombImg.quantize()
BiasCombImg = galsim.ImageUS(BiasCombImg)
BiasCombImg.write("%s/BiasImg_%s_%s_%s.fits" % (chip_output.subdir, BiasTag, self.chipID, i+1))
# BiasCombImg.write("%s/BiasImg_%s_%s_%s.fits" % (chip_output.subdir, BiasTag, self.chipID, i+1))
datetime_obs = datetime.fromtimestamp(timestamp_obs)
date_obs = datetime_obs.strftime("%y%m%d")
time_obs = datetime_obs.strftime("%H%M%S")
timestamp_obs += 5 * 60
self.outputCal(
img=BiasCombImg,
ra_cen=ra_cen,
dec_cen=dec_cen,
img_rot=img_rot,
im_type='CLB',
pointing_ID=pointing_ID,
date_obs=date_obs,
time_obs=time_obs,
output_dir=chip_output.subdir,
exptime=0.0)
del BiasCombImg
# Export combined (ncombine, Vignetting + PRNU) & single vignetting flat-field file
if config["flat_output"].lower() == "y":
if config["flat_output"].lower() == "y" and pointing_type=='CAL':
print(" Output N frame Flat-Field files", flush=True)
NFlat = int(config["NFlat"])
if config["add_bias"].lower() == "y":
......@@ -421,7 +517,7 @@ class Chip(FocalPlane):
elif config["add_bias"].lower() == "n":
biaslevel = 0
overscan = 0
darklevel = self.dark_noise*self.exptime
darklevel = self.dark_noise*self.flat_exptime
for i in range(NFlat):
FlatSingle = flat_img * prnu_img + darklevel
FlatCombImg, FlatTag = effects.MakeFlatNcomb(
......@@ -434,7 +530,17 @@ class Chip(FocalPlane):
seed_bias=SeedDefective+self.chipID
)
if config["cosmic_ray"].lower() == "y":
if config["cray_differ"].lower() == "y":
cr_map = effects.produceCR_Map(
xLen=self.npix_x, yLen=self.npix_y,
exTime=self.flat_exptime,
cr_pixelRatio=0.003*self.flat_exptime/self.exptime,
gain=self.gain,
attachedSizes=self.attachedSizes,
seed=SeedCosmicRay+pointing_ID*30+self.chipID+3)
# seed: obj-imaging:+0; bias:+1; dark:+2; flat:+3;
FlatCombImg += cr_map
del cr_map
if config["non_linear"].lower() == "y":
print(" Applying Non-Linearity on the Flat image", flush=True)
......@@ -459,7 +565,23 @@ class Chip(FocalPlane):
FlatCombImg.replaceNegative(replace_value=0)
FlatCombImg.quantize()
FlatCombImg = galsim.ImageUS(FlatCombImg)
FlatCombImg.write("%s/FlatImg_%s_%s_%s.fits" % (chip_output.subdir, FlatTag, self.chipID, i+1))
# FlatCombImg.write("%s/FlatImg_%s_%s_%s.fits" % (chip_output.subdir, FlatTag, self.chipID, i+1))
datetime_obs = datetime.fromtimestamp(timestamp_obs)
date_obs = datetime_obs.strftime("%y%m%d")
time_obs = datetime_obs.strftime("%H%M%S")
timestamp_obs += 5 * 60
self.outputCal(
img=FlatCombImg,
ra_cen=ra_cen,
dec_cen=dec_cen,
img_rot=img_rot,
im_type='CLF',
pointing_ID=pointing_ID,
date_obs=date_obs,
time_obs=time_obs,
output_dir=chip_output.subdir,
exptime=self.flat_exptime)
del FlatCombImg, FlatSingle, prnu_img
# flat_img.replaceNegative(replace_value=0)
# flat_img.quantize()
......@@ -467,7 +589,7 @@ class Chip(FocalPlane):
del flat_img
# Export Dark current images
if config["dark_output"].lower() == "y":
if config["dark_output"].lower() == "y" and pointing_type=='CAL':
print(" Output N frame Dark Current files", flush=True)
NDark = int(config["NDark"])
if config["add_bias"].lower() == "y":
......@@ -479,11 +601,39 @@ class Chip(FocalPlane):
for i in range(NDark):
DarkCombImg, DarkTag = effects.MakeDarkNcomb(
self.npix_x, self.npix_y,
overscan=overscan, bias_level=biaslevel, darkpsec=0.02, exptime=150,
overscan=overscan, bias_level=biaslevel, darkpsec=0.02, exptime=self.dark_exptime,
ncombine=1, read_noise=self.read_noise,
gain=1, seed_bias=SeedBiasNonuni+self.chipID)
if config["cosmic_ray"].lower() == "y":
if config["cray_differ"].lower() == "y":
cr_map = effects.produceCR_Map(
xLen=self.npix_x, yLen=self.npix_y,
exTime=self.dark_exptime,
cr_pixelRatio=0.003*self.dark_exptime/self.exptime,
gain=self.gain,
attachedSizes=self.attachedSizes,
seed=SeedCosmicRay+pointing_ID*30+self.chipID+2)
# seed: obj-imaging:+0; bias:+1; dark:+2; flat:+3;
DarkCombImg += cr_map
cr_map[cr_map > 65535] = 65535
cr_map[cr_map < 0] = 0
crmap_gsimg = galsim.Image(cr_map, dtype=np.uint16)
del cr_map
datetime_obs = datetime.fromtimestamp(timestamp_obs)
date_obs = datetime_obs.strftime("%y%m%d")
time_obs = datetime_obs.strftime("%H%M%S")
self.outputCal(
img=crmap_gsimg,
ra_cen=ra_cen,
dec_cen=dec_cen,
img_rot=img_rot,
im_type='CRD',
pointing_ID=pointing_ID,
date_obs=date_obs,
time_obs=time_obs,
output_dir=chip_output.subdir,
exptime=self.dark_exptime)
del crmap_gsimg
# Non-Linearity for Dark
if config["non_linear"].lower() == "y":
......@@ -513,13 +663,24 @@ class Chip(FocalPlane):
DarkCombImg.replaceNegative(replace_value=0)
DarkCombImg.quantize()
DarkCombImg = galsim.ImageUS(DarkCombImg)
DarkCombImg.write("%s/DarkImg_%s_%s_%s.fits" % (chip_output.subdir, DarkTag, self.chipID, i+1))
# DarkCombImg.write("%s/DarkImg_%s_%s_%s.fits" % (chip_output.subdir, DarkTag, self.chipID, i+1))
datetime_obs = datetime.fromtimestamp(timestamp_obs)
date_obs = datetime_obs.strftime("%y%m%d")
time_obs = datetime_obs.strftime("%H%M%S")
timestamp_obs += 5 * 60
self.outputCal(
img=DarkCombImg,
ra_cen=ra_cen,
dec_cen=dec_cen,
img_rot=img_rot,
im_type='CLD',
pointing_ID=pointing_ID,
date_obs=date_obs,
time_obs=time_obs,
output_dir=chip_output.subdir,
exptime=self.dark_exptime)
del DarkCombImg
# garbage collection of cosmic-ray array
if config["cosmic_ray"].lower() == "y":
del cr_map
# Apply Nonlinearity on the chip image
if config["non_linear"].lower() == "y":
print(" Applying Non-Linearity on the chip image", flush=True)
......
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