-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #120 from PaulHuwe/RCAL-789_L3SourceInjection
RCAL-789: Source Injection - Level 3
- Loading branch information
Showing
5 changed files
with
235 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
"""Function for Level 3-like images. | ||
""" | ||
|
||
import numpy as np | ||
import galsim | ||
from . import log | ||
from . import image, wcs, psf | ||
|
||
|
||
def add_objects_to_l3(l3_mos, source_cat, rng=None, seed=None): | ||
"""Add objects to a Level 3 mosaic | ||
Parameters | ||
---------- | ||
l3_mos : MosaicModel | ||
Mosaic of images | ||
source_cat : list | ||
List of catalog objects to add to l3_mos | ||
Returns | ||
------- | ||
None | ||
l3_mos is updated in place | ||
""" | ||
|
||
if rng is None and seed is None: | ||
seed = 143 | ||
log.warning( | ||
'No RNG set, constructing a new default RNG from default seed.') | ||
if rng is None: | ||
rng = galsim.UniformDeviate(seed) | ||
|
||
# Obtain optical element | ||
filter_name = l3_mos.meta.basic.optical_element | ||
|
||
# Generate WCS | ||
twcs = wcs.get_mosaic_wcs(l3_mos.meta) | ||
|
||
# Create PSF | ||
l3_psf = psf.make_psf(filter_name=filter_name, sca=2, chromatic=False, webbpsf=True) | ||
|
||
# Generate x,y positions for sources | ||
coords = np.array([[o.sky_pos.ra.rad, o.sky_pos.dec.rad] | ||
for o in source_cat]) | ||
sourcecountsall = galsim.ImageF(l3_mos.data.shape[0], l3_mos.data.shape[1], wcs=twcs, xmin=0, ymin=0) | ||
xpos, ypos = sourcecountsall.wcs._xy(coords[:, 0], coords[:, 1]) | ||
xpos_idx = [round(x) for x in xpos] | ||
ypos_idx = [round(y) for y in ypos] | ||
|
||
# Create overall scaling factor map | ||
Ct_all = (l3_mos.data.value / l3_mos.var_poisson) | ||
|
||
# Cycle over sources and add them to the mosaic | ||
for idx, (x, y) in enumerate(zip(xpos_idx, ypos_idx)): | ||
# Set scaling factor for injected sources | ||
# Flux / sigma_p^2 | ||
Ct = (l3_mos.data[x][y].value / l3_mos.var_poisson[x][y].value) | ||
|
||
# Create empty postage stamp galsim source image | ||
sourcecounts = galsim.ImageF(l3_mos.data.shape[0], l3_mos.data.shape[1], wcs=twcs, xmin=0, ymin=0) | ||
|
||
# Simulate source postage stamp | ||
image.add_objects_to_image(sourcecounts, [source_cat[idx]], xpos=[xpos[idx]], ypos=[ypos[idx]], | ||
psf=l3_psf, flux_to_counts_factor=Ct, bandpass=[filter_name], | ||
filter_name=filter_name, rng=rng) | ||
|
||
# Scale the source image back by its flux ratios | ||
sourcecounts /= Ct | ||
|
||
# Add sources to the original mosaic data array | ||
l3_mos.data = (l3_mos.data.value + sourcecounts.array) * l3_mos.data.unit | ||
|
||
# Note for the future - other noise sources (read and flat) need to be implemented | ||
|
||
# Set new poisson variance | ||
l3_mos.var_poisson = l3_mos.data.value / Ct_all | ||
|
||
# l3_mos is updated in place, so no return | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters