Commit 6078791f authored by Fang Yuedong's avatar Fang Yuedong
Browse files

first commit of user-customizable catalog version

parent 1635e47d
import numpy as np import numpy as np
import os import os
from datetime import datetime from scipy.interpolate import InterpolatedUnivariateSpline, interp1d
from scipy.interpolate import InterpolatedUnivariateSpline, UnivariateSpline, interp1d
from scipy import interpolate from scipy import interpolate
from astropy.table import Table from astropy.table import Table
import galsim import galsim
vc_A = 2.99792458e+18 # speed of light: A/s VC_A = 2.99792458e+18 # speed of light: A/s
vc_m = 2.99792458e+8 # speed of light: m/s VC_M = 2.99792458e+8 # speed of light: m/s
h_Plank = 6.626196e-27 # Plank constant: erg s H_PLANK = 6.626196e-27 # Plank constant: erg s
def magToFlux(mag): def magToFlux(mag):
""" """
...@@ -132,13 +131,13 @@ def tflux(filt, sed, redshift=0.0, av=0.0, redden=0): ...@@ -132,13 +131,13 @@ def tflux(filt, sed, redshift=0.0, av=0.0, redden=0):
sedxx = (sw.copy(), sf.copy()) sedxx = (sw.copy(), sf.copy())
sw = vc_A/sw sw = VC_A/sw
sf = sf*(vc_A/sw**2) # convert flux unit to erg/s/cm^s/Hz sf = sf*(VC_A/sw**2) # convert flux unit to erg/s/cm^s/Hz
sw, sf = sw[::-1], sf[::-1] sw, sf = sw[::-1], sf[::-1]
sfun = interp1d(sw, sf, kind='linear') sfun = interp1d(sw, sf, kind='linear')
fwave, fresp = filt[:,0], filt[:,1] fwave, fresp = filt[:,0], filt[:,1]
fwave = vc_A/fwave fwave = VC_A/fwave
fwave, fresp = fwave[::-1], fresp[::-1] fwave, fresp = fwave[::-1], fresp[::-1]
tflux = sfun(fwave) tflux = sfun(fwave)
...@@ -337,8 +336,7 @@ def reddening(sw, sf, av=0.0, model=0): ...@@ -337,8 +336,7 @@ def reddening(sw, sf, av=0.0, model=0):
flux = sf*10**(-0.4*A_lambda) flux = sf*10**(-0.4*A_lambda)
else: else:
print("!!! Please select a proper reddening model") raise ValueError("!!! Please select a proper reddening model")
sys.exit(0)
return flux return flux
......
...@@ -4,7 +4,7 @@ import os ...@@ -4,7 +4,7 @@ import os
from ObservationSim.Config import ConfigDir, ChipOutput from ObservationSim.Config import ConfigDir, ChipOutput
from ObservationSim.Config.Header import generatePrimaryHeader, generateExtensionHeader from ObservationSim.Config.Header import generatePrimaryHeader, generateExtensionHeader
from ObservationSim.Instrument import Telescope, Filter, FilterParam, FocalPlane, Chip from ObservationSim.Instrument import Telescope, Filter, FilterParam, FocalPlane, Chip
from ObservationSim.MockObject import Catalog, MockObject, Star, Galaxy, Quasar, calculateSkyMap_split_g from ObservationSim.MockObject import calculateSkyMap_split_g
from ObservationSim.PSF import PSFGauss, FieldDistortion, PSFInterp from ObservationSim.PSF import PSFGauss, FieldDistortion, PSFInterp
from ObservationSim._util import getShearFiled, makeSubDir_PointingList from ObservationSim._util import getShearFiled, makeSubDir_PointingList
...@@ -17,7 +17,7 @@ import logging ...@@ -17,7 +17,7 @@ import logging
import psutil import psutil
class Observation(object): class Observation(object):
def __init__(self, config, work_dir=None, data_dir=None): def __init__(self, config, Catalog, work_dir=None, data_dir=None):
self.path_dict = ConfigDir(config=config, work_dir=work_dir, data_dir=data_dir) self.path_dict = ConfigDir(config=config, work_dir=work_dir, data_dir=data_dir)
# self.config = ReadConfig(self.path_dict["config_file"]) # self.config = ReadConfig(self.path_dict["config_file"])
self.config = config self.config = config
...@@ -26,6 +26,7 @@ class Observation(object): ...@@ -26,6 +26,7 @@ class Observation(object):
self.filter_param = FilterParam(filter_dir=self.path_dict["filter_dir"]) # Currently the default values are hard coded in self.filter_param = FilterParam(filter_dir=self.path_dict["filter_dir"]) # Currently the default values are hard coded in
self.chip_list = [] self.chip_list = []
self.filter_list = [] self.filter_list = []
self.Catalog = Catalog
# if we want to apply field distortion? # if we want to apply field distortion?
if self.config["ins_effects"]["field_dist"] == True: if self.config["ins_effects"]["field_dist"] == True:
...@@ -85,7 +86,7 @@ class Observation(object): ...@@ -85,7 +86,7 @@ class Observation(object):
if pointing_type == 'MS': if pointing_type == 'MS':
# Load catalogues and templates # Load catalogues and templates
self.cat = Catalog(config=self.config, chip=chip, cat_dir=cat_dir, sed_dir=sed_dir) self.cat = self.Catalog(config=self.config, chip=chip, cat_dir=cat_dir, sed_dir=sed_dir)
self.nobj = len(self.cat.objs) self.nobj = len(self.cat.objs)
# Loop over objects # Loop over objects
...@@ -93,42 +94,29 @@ class Observation(object): ...@@ -93,42 +94,29 @@ class Observation(object):
bright_obj = 0 bright_obj = 0
dim_obj = 0 dim_obj = 0
for j in range(self.nobj): for j in range(self.nobj):
# if j >= 20: # if j >= 100:
# break # break
obj = self.cat.objs[j] obj = self.cat.objs[j]
if obj.type == 'star' and self.config["galaxy_only"]:
continue
elif obj.type == 'galaxy' and self.config["star_only"]:
continue
elif obj.type == 'quasar' and self.config["star_only"]:
continue
# Load SED # load SED
if obj.type == 'star': try:
normF = chip.normF_star sed_data = self.cat.load_sed(obj)
if self.config["galaxy_only"]: norm_filt = self.cat.load_norm_filt(obj)
continue obj.sed, obj.param["mag_%s"%filt.filter_type] = self.cat.convert_sed(
try: mag=obj.param["mag_use_normal"],
obj.load_SED( sed=sed_data,
survey_type=chip.survey_type, target_filt=filt,
normFilter=normF, norm_filt=norm_filt,
target_filt=filt, )
sed_lib=self.cat.tempSED_star) except Exception as e:
except Exception as e: print(e)
print(e) continue
continue
elif obj.type == 'galaxy': # or obj.type == quasar
normF = chip.normF_galaxy
if self.config["star_only"]:
continue
obj.load_SED(
survey_type=chip.survey_type,
sed_templates=self.cat.tempSed_gal,
normFilter=normF,
target_filt=filt)
elif obj.type == 'quasar':
normF = chip.normF_galaxy
if self.config["star_only"]:
continue
obj.load_SED(
survey_type=chip.survey_type,
sed_templates=self.cat.tempSed_gal,
normFilter=normF,
target_filt=filt)
# Exclude very bright/dim objects (for now) # Exclude very bright/dim objects (for now)
if filt.is_too_bright(mag=obj.getMagFilter(filt)): if filt.is_too_bright(mag=obj.getMagFilter(filt)):
...@@ -156,6 +144,10 @@ class Observation(object): ...@@ -156,6 +144,10 @@ class Observation(object):
except: except:
print("failed to load external shear.") print("failed to load external shear.")
pass pass
elif self.config["shear_setting"]["shear_type"] == "catalog":
pass
else:
raise ValueError("Unknown shear input")
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:
...@@ -191,7 +183,9 @@ class Observation(object): ...@@ -191,7 +183,9 @@ class Observation(object):
g1=g1, g1=g1,
g2=g2, g2=g2,
exptime=exptime, exptime=exptime,
normFilter=normF) # normFilter=normF,
normFilter=norm_filt,
)
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)
...@@ -274,53 +268,6 @@ class Observation(object): ...@@ -274,53 +268,6 @@ class Observation(object):
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)
def runExposure(self, ra_cen=None, dec_cen=None, timestamp_obs=1621915200, pointing_ID=0, pointing_type='MS', img_rot=None, exptime=150., shear_cat_file=None, chips=None):
if (ra_cen == None) or (dec_cen == None):
ra_cen = self.config["obs_setting"]["ra_center"]
dec_cen = self.config["obs_setting"]["dec_center"]
if img_rot == None:
img_rot = self.config["obs_setting"]["image_rot"]
sub_img_dir, prefix = makeSubDir_PointingList(path_dict=self.path_dict, config=self.config, pointing_ID=pointing_ID)
# Loop over chips
for i in range(len(self.chip_list)):
chip = self.chip_list[i]
filt = self.filter_list[i]
# Only run a particular set of chips
if chips is not None:
if chip.chipID not in chips:
continue
# Prepare output files
chip_output = ChipOutput(
config=self.config,
focal_plane=self.focal_plane,
chip=chip,
filt=filt,
exptime=exptime,
pointing_type=pointing_type,
pointing_ID=pointing_ID,
subdir=sub_img_dir,
prefix=prefix)
self.runOneChip(
chip=chip,
filt=filt,
chip_output=chip_output,
pointing_ID = pointing_ID,
ra_cen=ra_cen,
dec_cen=dec_cen,
img_rot=img_rot,
exptime=exptime,
timestamp_obs=timestamp_obs,
pointing_type=pointing_type,
cat_dir=self.path_dict["cat_dir"])
print("finished running chip#%d..."%(chip.chipID), flush=True)
def runExposure_MPI_PointingList(self, ra_cen=None, dec_cen=None, pRange=None, timestamp_obs=np.array([1621915200]), pointing_type=np.array(['MS']), img_rot=None, exptime=150., shear_cat_file=None, chips=None, use_mpi=False): def runExposure_MPI_PointingList(self, ra_cen=None, dec_cen=None, pRange=None, timestamp_obs=np.array([1621915200]), pointing_type=np.array(['MS']), img_rot=None, exptime=150., shear_cat_file=None, chips=None, use_mpi=False):
if use_mpi: if use_mpi:
comm = MPI.COMM_WORLD comm = MPI.COMM_WORLD
......
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