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 os
from datetime import datetime
from scipy.interpolate import InterpolatedUnivariateSpline, UnivariateSpline, interp1d
from scipy.interpolate import InterpolatedUnivariateSpline, interp1d
from scipy import interpolate
from astropy.table import Table
import galsim
vc_A = 2.99792458e+18 # speed of light: A/s
vc_m = 2.99792458e+8 # speed of light: m/s
h_Plank = 6.626196e-27 # Plank constant: erg s
VC_A = 2.99792458e+18 # speed of light: A/s
VC_M = 2.99792458e+8 # speed of light: m/s
H_PLANK = 6.626196e-27 # Plank constant: erg s
def magToFlux(mag):
"""
......@@ -132,13 +131,13 @@ def tflux(filt, sed, redshift=0.0, av=0.0, redden=0):
sedxx = (sw.copy(), sf.copy())
sw = vc_A/sw
sf = sf*(vc_A/sw**2) # convert flux unit to erg/s/cm^s/Hz
sw = VC_A/sw
sf = sf*(VC_A/sw**2) # convert flux unit to erg/s/cm^s/Hz
sw, sf = sw[::-1], sf[::-1]
sfun = interp1d(sw, sf, kind='linear')
fwave, fresp = filt[:,0], filt[:,1]
fwave = vc_A/fwave
fwave = VC_A/fwave
fwave, fresp = fwave[::-1], fresp[::-1]
tflux = sfun(fwave)
......@@ -337,8 +336,7 @@ def reddening(sw, sf, av=0.0, model=0):
flux = sf*10**(-0.4*A_lambda)
else:
print("!!! Please select a proper reddening model")
sys.exit(0)
raise ValueError("!!! Please select a proper reddening model")
return flux
......
......@@ -4,7 +4,7 @@ import os
from ObservationSim.Config import ConfigDir, ChipOutput
from ObservationSim.Config.Header import generatePrimaryHeader, generateExtensionHeader
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._util import getShearFiled, makeSubDir_PointingList
......@@ -17,7 +17,7 @@ import logging
import psutil
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.config = ReadConfig(self.path_dict["config_file"])
self.config = config
......@@ -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.chip_list = []
self.filter_list = []
self.Catalog = Catalog
# if we want to apply field distortion?
if self.config["ins_effects"]["field_dist"] == True:
......@@ -85,7 +86,7 @@ class Observation(object):
if pointing_type == 'MS':
# 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)
# Loop over objects
......@@ -93,42 +94,29 @@ class Observation(object):
bright_obj = 0
dim_obj = 0
for j in range(self.nobj):
# if j >= 20:
# if j >= 100:
# break
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
if obj.type == 'star':
normF = chip.normF_star
if self.config["galaxy_only"]:
continue
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
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)
# load SED
try:
sed_data = self.cat.load_sed(obj)
norm_filt = self.cat.load_norm_filt(obj)
obj.sed, obj.param["mag_%s"%filt.filter_type] = self.cat.convert_sed(
mag=obj.param["mag_use_normal"],
sed=sed_data,
target_filt=filt,
norm_filt=norm_filt,
)
except Exception as e:
print(e)
continue
# Exclude very bright/dim objects (for now)
if filt.is_too_bright(mag=obj.getMagFilter(filt)):
......@@ -156,6 +144,10 @@ class Observation(object):
except:
print("failed to load external shear.")
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)
if pos_img.x == -1 or pos_img.y == -1:
......@@ -191,7 +183,9 @@ class Observation(object):
g1=g1,
g2=g2,
exptime=exptime,
normFilter=normF)
# normFilter=normF,
normFilter=norm_filt,
)
if isUpdated:
# TODO: add up stats
chip_output.cat_add_obj(obj, pos_img, pos_shear, g1, g2)
......@@ -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)
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):
if use_mpi:
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