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

modified fits header etc.

parent 3f932427
...@@ -4,8 +4,8 @@ from Instrument import Telescope, Filter, FilterParam, FocalPlane, Chip ...@@ -4,8 +4,8 @@ from Instrument import Telescope, Filter, FilterParam, FocalPlane, Chip
from MockObject import Catalog, MockObject, Star, Galaxy, Quasar, calculateSkyMap_split_g from MockObject import Catalog, MockObject, Star, Galaxy, Quasar, calculateSkyMap_split_g
from PSF import PSFGauss, PSFInterp, FieldDistortion from PSF import PSFGauss, PSFInterp, FieldDistortion
from _util import makeSubDir, getShearFiled, makeSubDir_PointingList from _util import makeSubDir, getShearFiled, makeSubDir_PointingList
from astropy.time import Time as asTime
from astropy.io import fits from astropy.io import fits
from datetime import datetime
import numpy as np import numpy as np
import mpi4py.MPI as MPI import mpi4py.MPI as MPI
import galsim import galsim
...@@ -47,7 +47,7 @@ class Observation(object): ...@@ -47,7 +47,7 @@ class Observation(object):
self.g1_field, self.g2_field, self.nshear = getShearFiled(config=self.config) self.g1_field, self.g2_field, self.nshear = getShearFiled(config=self.config)
def runOneChip(self, chip, filt, chip_output, wcs_fp=None, psf_model=None, pointing_ID=0, ra_cen=None, dec_cen=None, img_rot=None, exptime=150., input_cat_name=None, shear_cat_file=None, cat_dir=None, sed_dir=None): def runOneChip(self, chip, filt, chip_output, wcs_fp=None, psf_model=None, pointing_ID=0, ra_cen=None, dec_cen=None, img_rot=None, exptime=150., timestamp_obs=1621915200, pointing_type='MS', input_cat_name=None, shear_cat_file=None, cat_dir=None, sed_dir=None):
if (ra_cen is None) or (dec_cen is None): if (ra_cen is None) or (dec_cen is None):
ra_cen = self.config["ra_center"] ra_cen = self.config["ra_center"]
...@@ -80,169 +80,189 @@ class Observation(object): ...@@ -80,169 +80,189 @@ class Observation(object):
skyfile = os.path.join(self.path_dict["data_dir"], 'skybackground/sky_emiss_hubble_50_50_A.dat') skyfile = os.path.join(self.path_dict["data_dir"], 'skybackground/sky_emiss_hubble_50_50_A.dat')
sky_map = calculateSkyMap_split_g(xLen=chip.npix_x, yLen=chip.npix_y, blueLimit=filt.blue_limit, redLimit=filt.red_limit, skyfn=skyfile, conf=chip.sls_conf, pixelSize=chip.pix_scale, isAlongY=0) sky_map = calculateSkyMap_split_g(xLen=chip.npix_x, yLen=chip.npix_y, blueLimit=filt.blue_limit, redLimit=filt.red_limit, skyfn=skyfile, conf=chip.sls_conf, pixelSize=chip.pix_scale, isAlongY=0)
# Load catalogues and templates if pointing_type == 'MS':
self.cat = Catalog(config=self.config, chip=chip, cat_dir=cat_dir, sed_dir=sed_dir, pRa=ra_cen, pDec=dec_cen, rotation=img_rot, template_dir=self.path_dict["template_dir"]) # Load catalogues and templates
self.nobj = len(self.cat.objs) self.cat = Catalog(config=self.config, chip=chip, cat_dir=cat_dir, sed_dir=sed_dir, pRa=ra_cen, pDec=dec_cen, rotation=img_rot, template_dir=self.path_dict["template_dir"])
self.nobj = len(self.cat.objs)
# Loop over objects
missed_obj = 0 # Loop over objects
bright_obj = 0 missed_obj = 0
dim_obj = 0 bright_obj = 0
for j in range(self.nobj): dim_obj = 0
# if j >= 20: for j in range(self.nobj):
# break # if j >= 20:
obj = self.cat.objs[j] # break
obj = self.cat.objs[j]
# Load SED
if obj.type == 'star': # Load SED
normF = chip.normF_star if obj.type == 'star':
try: normF = chip.normF_star
try:
obj.load_SED(
survey_type=chip.survey_type,
normFilter=normF,
target_filt=filt,
sed_lib=self.cat.tempSED_star)
except Exception as e:
print(e)
continue
elif obj.type == 'galaxy': # or obj.type == quasar
normF = chip.normF_galaxy
obj.load_SED( obj.load_SED(
sed_path=sed_dir,
survey_type=chip.survey_type, survey_type=chip.survey_type,
sed_templates=self.cat.tempSed_gal,
normFilter=normF, normFilter=normF,
target_filt=filt, target_filt=filt)
sed_lib=self.cat.tempSED_star) elif obj.type == 'quasar':
except Exception as e: normF = chip.normF_galaxy
print(e) obj.load_SED(
continue sed_path=sed_dir,
elif obj.type == 'galaxy': # or obj.type == quasar survey_type=chip.survey_type,
normF = chip.normF_galaxy sed_templates=self.cat.tempSed_gal,
obj.load_SED( normFilter=normF,
sed_path=sed_dir, target_filt=filt)
survey_type=chip.survey_type,
sed_templates=self.cat.tempSed_gal, # Exclude very bright/dim objects (for now)
normFilter=normF, if filt.is_too_bright(mag=obj.getMagFilter(filt)):
target_filt=filt) # print("obj too birght!!", flush=True)
elif obj.type == 'quasar': if obj.type != 'galaxy':
normF = chip.normF_galaxy bright_obj += 1
obj.load_SED( obj.unload_SED()
sed_path=sed_dir, continue
survey_type=chip.survey_type, if filt.is_too_dim(mag=obj.getMagFilter(filt)):
sed_templates=self.cat.tempSed_gal, # print("obj too dim!!", flush=True)
normFilter=normF, dim_obj += 1
target_filt=filt)
# Exclude very bright/dim objects (for now)
if filt.is_too_bright(mag=obj.getMagFilter(filt)):
# print("obj too birght!!", flush=True)
if obj.type != 'galaxy':
bright_obj += 1
obj.unload_SED() obj.unload_SED()
# print(obj.getMagFilter(filt))
continue continue
if filt.is_too_dim(mag=obj.getMagFilter(filt)):
# print("obj too dim!!", flush=True)
dim_obj += 1
obj.unload_SED()
# print(obj.getMagFilter(filt))
continue
if self.config["shear_method"] == "constant": if self.config["shear_method"] == "constant":
if obj.type == 'star': if obj.type == 'star':
g1, g2 = 0, 0 g1, g2 = 0, 0
else: else:
g1, g2 = self.g1_field, self.g2_field g1, g2 = self.g1_field, self.g2_field
elif self.config["shear_method"] == "extra": elif self.config["shear_method"] == "extra":
# TODO: every object with individual shear from input catalog(s) # TODO: every object with individual shear from input catalog(s)
g1, g2 = self.g1_field[j], self.g2_field[j] g1, g2 = self.g1_field[j], self.g2_field[j]
pos_img, offset, local_wcs = obj.getPosImg_Offset_WCS(img=chip.img, fdmodel=self.fd_model, chip=chip, verbose=False) pos_img, offset, local_wcs = obj.getPosImg_Offset_WCS(img=chip.img, fdmodel=self.fd_model, chip=chip, verbose=False)
if pos_img.x == -1 or pos_img.y == -1: if pos_img.x == -1 or pos_img.y == -1:
# Exclude object which is outside the chip area (after field distortion) # Exclude object which is outside the chip area (after field distortion)
# print("obj missed!!") # print("obj missed!!")
missed_obj += 1 missed_obj += 1
obj.unload_SED() obj.unload_SED()
continue continue
# Draw object & update output catalog # Draw object & update output catalog
try: try:
if chip.survey_type == "photometric": if chip.survey_type == "photometric":
isUpdated, pos_shear = obj.drawObj_multiband( isUpdated, pos_shear = obj.drawObj_multiband(
tel=self.tel, tel=self.tel,
pos_img=pos_img, pos_img=pos_img,
psf_model=psf_model, psf_model=psf_model,
bandpass_list=filt.bandpass_sub_list, bandpass_list=filt.bandpass_sub_list,
filt=filt, filt=filt,
chip=chip, chip=chip,
g1=g1, g1=g1,
g2=g2, g2=g2,
exptime=exptime) exptime=exptime)
elif chip.survey_type == "spectroscopic": elif chip.survey_type == "spectroscopic":
isUpdated, pos_shear = obj.drawObj_slitless( isUpdated, pos_shear = obj.drawObj_slitless(
tel=self.tel, tel=self.tel,
pos_img=pos_img, pos_img=pos_img,
psf_model=psf_model, psf_model=psf_model,
bandpass_list=filt.bandpass_sub_list, bandpass_list=filt.bandpass_sub_list,
filt=filt, filt=filt,
chip=chip, chip=chip,
g1=g1, g1=g1,
g2=g2, g2=g2,
exptime=exptime, exptime=exptime,
normFilter=normF) normFilter=normF)
if isUpdated: if isUpdated:
# TODO: add up stats # TODO: add up stats
chip_output.cat_add_obj(obj, pos_img, pos_shear, g1, g2) chip_output.cat_add_obj(obj, pos_img, pos_shear, g1, g2)
pass
else:
# print("object omitted", flush=True)
continue
except Exception as e:
print(e)
pass pass
else: # Unload SED:
# print("object omitted", flush=True) obj.unload_SED()
continue del obj
except Exception as e:
print(e)
pass
# Unload SED:
obj.unload_SED()
del obj
del psf_model del psf_model
del self.cat del self.cat
print("check running:1: pointing-{:} chip-{:} pid-{:} memory-{:6.2}GB".format(pointing_ID, chip.chipID, os.getpid(), (psutil.Process(os.getpid()).memory_info().rss / 1024 / 1024 / 1024) ), flush=True) print("check running:1: pointing-{:} chip-{:} pid-{:} memory-{:6.2}GB".format(pointing_ID, chip.chipID, os.getpid(), (psutil.Process(os.getpid()).memory_info().rss / 1024 / 1024 / 1024) ), flush=True)
# Detector Effects # Detector Effects
# =========================================================== # ===========================================================
chip.img = chip.addNoise(config=self.config, tel=self.tel, filt=filt, img=chip.img, sky_map=sky_map) chip.img = chip.addNoise(config=self.config, tel=self.tel, filt=filt, img=chip.img, sky_map=sky_map)
chip.img = chip.addEffects(config=self.config, img=chip.img, chip_output=chip_output, filt=filt, pointing_ID=pointing_ID) # chip.img = chip.addEffects(config=self.config, img=chip.img, chip_output=chip_output, filt=filt, pointing_ID=pointing_ID)
# whether to output zero, dark, flat calibration images.
chip.img = chip.addEffects(
config=self.config,
img=chip.img,
chip_output=chip_output,
filt=filt,
ra_cen=ra_cen,
dec_cen=dec_cen,
img_rot=img_rot,
pointing_ID=pointing_ID,
timestamp_obs=timestamp_obs,
pointing_type=pointing_type)
h_prim = generatePrimaryHeader( if pointing_type == 'MS':
xlen=chip.npix_x, datetime_obs = datetime.fromtimestamp(timestamp_obs)
ylen=chip.npix_y, date_obs = datetime_obs.strftime("%y%m%d")
pointNum = str(pointing_ID), time_obs = datetime_obs.strftime("%H%M%S")
ra=ra_cen, h_prim = generatePrimaryHeader(
dec=dec_cen, xlen=chip.npix_x,
psize=chip.pix_scale, ylen=chip.npix_y,
row_num=chip.rowID, pointNum = str(pointing_ID),
col_num=chip.colID, ra=ra_cen,
date=self.config["date_obs"], dec=dec_cen,
time_obs=self.config["time_obs"], psize=chip.pix_scale,
im_type = 'MS') row_num=chip.rowID,
h_ext = generateExtensionHeader( col_num=chip.colID,
xlen=chip.npix_x, # date=self.config["date_obs"],
ylen=chip.npix_y, # time_obs=self.config["time_obs"],
ra=ra_cen, date=date_obs,
dec=dec_cen, time_obs=time_obs,
pa=img_rot.deg, im_type='MS')
gain=chip.gain, h_ext = generateExtensionHeader(
readout=chip.read_noise, xlen=chip.npix_x,
dark=chip.dark_noise, ylen=chip.npix_y,
saturation=90000, ra=ra_cen,
psize=chip.pix_scale, dec=dec_cen,
row_num=chip.rowID, pa=img_rot.deg,
col_num=chip.colID, gain=chip.gain,
extName='SCI') readout=chip.read_noise,
chip.img = galsim.Image(chip.img.array, dtype=np.uint16) dark=chip.dark_noise,
hdu1 = fits.PrimaryHDU(header=h_prim) saturation=90000,
hdu2 = fits.ImageHDU(chip.img.array, header=h_ext) psize=chip.pix_scale,
hdu1 = fits.HDUList([hdu1, hdu2]) row_num=chip.rowID,
fname = os.path.join(chip_output.subdir, h_prim['FILENAME'] + '.fits') col_num=chip.colID,
hdu1.writeto(fname, output_verify='ignore', overwrite=True) extName='raw')
chip.img = galsim.Image(chip.img.array, dtype=np.uint16)
hdu1 = fits.PrimaryHDU(header=h_prim)
hdu2 = fits.ImageHDU(chip.img.array, header=h_ext)
hdu1 = fits.HDUList([hdu1, hdu2])
fname = os.path.join(chip_output.subdir, h_prim['FILENAME'] + '.fits')
hdu1.writeto(fname, output_verify='ignore', overwrite=True)
print("# objects that are too bright %d out of %d"%(bright_obj, self.nobj))
print("# objects that are too dim %d out of %d"%(dim_obj, self.nobj))
print("# objects that are missed %d out of %d"%(missed_obj, self.nobj))
del chip.img del chip.img
print("check running:2: pointing-{:} chip-{:} pid-{:} memory-{:6.2}GB".format(pointing_ID, chip.chipID, os.getpid(), (psutil.Process(os.getpid()).memory_info().rss / 1024 / 1024 / 1024) ), flush=True) print("check running:2: pointing-{:} chip-{:} pid-{:} memory-{:6.2}GB".format(pointing_ID, chip.chipID, os.getpid(), (psutil.Process(os.getpid()).memory_info().rss / 1024 / 1024 / 1024) ), flush=True)
print("# objects that are too bright %d out of %d"%(bright_obj, self.nobj))
print("# objects that are too dim %d out of %d"%(dim_obj, self.nobj))
print("# objects that are missed %d out of %d"%(missed_obj, self.nobj))
def runExposure(self, ra_cen=None, dec_cen=None, pointing_ID=0, img_rot=None, exptime=150., input_cat_name=None, shear_cat_file=None, oneChip=None): def runExposure(self, ra_cen=None, dec_cen=None, timestamp_obs=1621915200, pointing_ID=0, pointing_type='MS', img_rot=None, exptime=150., input_cat_name=None, shear_cat_file=None, oneChip=None):
if (ra_cen == None) or (dec_cen == None): if (ra_cen == None) or (dec_cen == None):
ra_cen = self.config["ra_center"] ra_cen = self.config["ra_center"]
...@@ -269,6 +289,7 @@ class Observation(object): ...@@ -269,6 +289,7 @@ class Observation(object):
chip=chip, chip=chip,
filt=filt, filt=filt,
exptime=exptime, exptime=exptime,
pointing_type=pointing_type,
pointing_ID=pointing_ID, pointing_ID=pointing_ID,
subdir=sub_img_dir, subdir=sub_img_dir,
prefix=prefix) prefix=prefix)
...@@ -282,11 +303,13 @@ class Observation(object): ...@@ -282,11 +303,13 @@ class Observation(object):
dec_cen=dec_cen, dec_cen=dec_cen,
img_rot=img_rot, img_rot=img_rot,
exptime=exptime, exptime=exptime,
timestamp_obs=timestamp_obs,
pointing_type=pointing_type,
cat_dir=self.path_dict["cat_dir"], cat_dir=self.path_dict["cat_dir"],
sed_dir=self.path_dict["SED_dir"]) sed_dir=self.path_dict["SED_dir"])
print("finished running chip#%d..."%(chip.chipID), flush=True) print("finished running chip#%d..."%(chip.chipID), flush=True)
def runExposure_MPI_PointingList(self, ra_cen=None, dec_cen=None, pRange=None, img_rot=None, exptime=150., input_cat_name=None, shear_cat_file=None): def runExposure_MPI_PointingList(self, ra_cen=None, dec_cen=None, pRange=None, timestamp_obs=[1621915200], pointing_type=['MS'], img_rot=None, exptime=150., input_cat_name=None, shear_cat_file=None):
comm = MPI.COMM_WORLD comm = MPI.COMM_WORLD
ind_thread = comm.Get_rank() ind_thread = comm.Get_rank()
num_thread = comm.Get_size() num_thread = comm.Get_size()
...@@ -302,6 +325,11 @@ class Observation(object): ...@@ -302,6 +325,11 @@ class Observation(object):
else: else:
pStart = 0 pStart = 0
# TEMP
if len(timestamp_obs) == 1:
timestamp_obs *= len(pRange)
pointing_type *= len(pRange)
for ipoint in range(len(ra_cen)): for ipoint in range(len(ra_cen)):
for ichip in range(nchips_per_fp): for ichip in range(nchips_per_fp):
i = ipoint*nchips_per_fp + ichip i = ipoint*nchips_per_fp + ichip
...@@ -322,6 +350,7 @@ class Observation(object): ...@@ -322,6 +350,7 @@ class Observation(object):
chip=chip, chip=chip,
filt=filt, filt=filt,
exptime=exptime, exptime=exptime,
pointing_type=pointing_type[ipoint],
pointing_ID=pointing_ID, pointing_ID=pointing_ID,
subdir=sub_img_dir, subdir=sub_img_dir,
prefix=prefix) prefix=prefix)
...@@ -333,7 +362,9 @@ class Observation(object): ...@@ -333,7 +362,9 @@ class Observation(object):
ra_cen=ra_cen[ipoint], ra_cen=ra_cen[ipoint],
dec_cen=dec_cen[ipoint], dec_cen=dec_cen[ipoint],
img_rot=img_rot, img_rot=img_rot,
exptime=exptime, exptime=exptime,
timestamp_obs=timestamp_obs[ipoint],
pointing_type=pointing_type[ipoint],
cat_dir=self.path_dict["cat_dir"], cat_dir=self.path_dict["cat_dir"],
sed_dir=self.path_dict["SED_dir"]) sed_dir=self.path_dict["SED_dir"])
print("finished running chip#%d..."%(chip.chipID), flush=True) print("finished running chip#%d..."%(chip.chipID), flush=True)
\ No newline at end of file
import os import os
import numpy as np import numpy as np
from Config import ReadConfig from Config import ReadConfig
from datetime import datetime
work_dir = "/public/home/fangyuedong/CSST_new_framework/test/" work_dir = "/public/home/fangyuedong/CSST/test/"
data_dir = "/data/simudata/CSSOSDataProductsSims/data/" data_dir = "/data/simudata/CSSOSDataProductsSims/data/"
config_file = os.path.join(work_dir, "ObservationSim.cfg") config_file = os.path.join(work_dir, "ObservationSim.cfg")
...@@ -25,5 +26,23 @@ f.close() ...@@ -25,5 +26,23 @@ f.close()
pRA = np.array(pRA) pRA = np.array(pRA)
pDEC = np.array(pDEC) pDEC = np.array(pDEC)
# Create calibration pointings
ncal = 1 # Define the number of calibration pointings
pointing_type = ['MS']*len(pRA)
pRA = np.append(pRA[:ncal], pRA)
pDEC = np.append(pDEC[:ncal], pDEC)
pointing_type = ['CAL']*ncal + pointing_type
# Calculate starting time(s)
t0 = datetime(2021, 5, 25, 12, 0, 0)
t = datetime.timestamp(t0)
timestamp_obs = []
for i in range(len(pointing_type)):
timestamp_obs.append(t)
if pointing_type[i] == 'CAL':
t += 3 * 5 * 60 # 3 calibration exposure
elif pointing_type[i] == 'MS':
t += 5 * 60
# Define the range of pointing list # Define the range of pointing list
pRange = range(0, 3) pRange = range(0, 2)
\ No newline at end of file \ No newline at end of file
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