-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RCAL-862 Refactor L3 Injection #132
Merged
PaulHuwe
merged 11 commits into
spacetelescope:main
from
PaulHuwe:RCAL-862_RefactorL3Injection
Aug 6, 2024
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
8dbcd8d
Initial save.
PaulHuwe 9e3e98b
Moved L3 tests to test_l3
PaulHuwe 0d3e83c
Save
PaulHuwe e1de7e3
Save
PaulHuwe 6536f8e
Initial PR
PaulHuwe eab8041
Merge branch 'main' of https://github.com/spacetelescope/romanisim in…
PaulHuwe 65898b9
Addressed PR comments and added flake8 fixes.
PaulHuwe 04baff1
Idea
PaulHuwe 657c83b
Integrated changes from L3 integration as well as addressed PR comments.
PaulHuwe 8f6f527
Addressed PR comments and rebased.
PaulHuwe d4af8fd
Removed extraneous comment.
PaulHuwe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
@@ -1,80 +1,104 @@ | ||
"""Function for Level 3-like images. | ||
"""Roman WFI simulator functions for Level 3 mosaics. | ||
|
||
Based on galsim's implementation of Roman image simulation. Uses galsim Roman modules | ||
for most of the real work. | ||
""" | ||
|
||
import numpy as np | ||
import math | ||
import astropy.time | ||
from astropy import table | ||
import galsim | ||
from . import log | ||
from . import image, wcs, psf | ||
|
||
|
||
def add_objects_to_l3(l3_mos, source_cat, rng=None, seed=None): | ||
from galsim import roman | ||
|
||
from . import parameters | ||
from . import util | ||
import romanisim.wcs | ||
import romanisim.l1 | ||
import romanisim.bandpass | ||
import romanisim.psf | ||
import romanisim.image | ||
import romanisim.persistence | ||
from romanisim import log | ||
import roman_datamodels.maker_utils as maker_utils | ||
import roman_datamodels.datamodels as rdm | ||
from roman_datamodels.stnode import WfiMosaic | ||
|
||
|
||
def add_objects_to_l3(l3_mos, source_cat, exptimes, xpos=None, ypos=None, coords=None, cps_conv=1.0, unit_factor=1.0, | ||
filter_name=None, coords_unit='rad', wcs=None, psf=None, rng=None, seed=None): | ||
"""Add objects to a Level 3 mosaic | ||
|
||
Parameters | ||
---------- | ||
l3_mos : MosaicModel | ||
l3_mos : MosaicModel or galsim.Image | ||
Mosaic of images | ||
source_cat : list | ||
List of catalog objects to add to l3_mos | ||
exptimes : list | ||
Exposure times to scale back to rate units | ||
xpos, ypos : array_like | ||
x & y positions of sources (pixel) at which sources should be added | ||
coords : array_like | ||
ra & dec positions of sources (coords_unit) at which sources should be added | ||
cps_conv : float | ||
Factor to convert data to cps | ||
unit_factor: float | ||
Factor to convert counts data to MJy / sr | ||
coords_unit : string | ||
units of coords | ||
wcs : galsim.GSFitsWCS | ||
WCS corresponding to image | ||
psf : galsim.Profile | ||
PSF for image | ||
rng : galsim.BaseDeviate | ||
random number generator to use | ||
seed : int | ||
seed to use for random number generator | ||
|
||
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 | ||
if filter_name is None: | ||
filter_name = l3_mos.meta.basic.optical_element | ||
|
||
# Generate WCS (if needed) | ||
if wcs is None: | ||
wcs = romanisim.wcs.get_mosaic_wcs(l3_mos.meta, shape=l3_mos.data.shape) | ||
|
||
# Create PSF (if needed) | ||
if psf is None: | ||
psf = romanisim.psf.make_psf(filter_name=filter_name, sca=parameters.default_sca, chromatic=False, webbpsf=True) | ||
|
||
# Create Image canvas to add objects to | ||
if isinstance(l3_mos, (rdm.MosaicModel, WfiMosaic)): | ||
sourcecountsall = galsim.ImageF(l3_mos.data.value, wcs=wcs, xmin=0, ymin=0) | ||
else: | ||
sourcecountsall = l3_mos | ||
|
||
# Create position arrays (if needed) | ||
if any(pos is None for pos in [xpos, ypos]): | ||
# Create coordinates (if needed) | ||
if coords is None: | ||
coords = np.array([[o.sky_pos.ra.rad, o.sky_pos.dec.rad] | ||
for o in source_cat]) | ||
coords_unit = 'rad' | ||
# Generate x,y positions for sources | ||
xpos, ypos = sourcecountsall.wcs.radecToxy(coords[:, 0], coords[:, 1], coords_unit) | ||
|
||
# Add sources to the original mosaic data array | ||
romanisim.image.add_objects_to_image(sourcecountsall, source_cat, xpos=xpos, ypos=ypos, | ||
psf=psf, flux_to_counts_factor=[xpt * cps_conv for xpt in exptimes], | ||
convtimes=[xpt / unit_factor for xpt in exptimes], | ||
bandpass=[filter_name], filter_name=filter_name, | ||
rng=rng, seed=seed) | ||
|
||
# Save array with added sources | ||
if isinstance(l3_mos, (rdm.MosaicModel, WfiMosaic)): | ||
l3_mos.data = sourcecountsall.array * l3_mos.data.unit | ||
|
||
# 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably around here need a:
if add_noise:
stamp.addNoise(galsim.PoissonNoise(rng))
to get around that issue we were discussing that noise is only added in photon-shooting / chromatic mode by default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was saving that for RCAL-863.