Skip to content

Commit

Permalink
Merge pull request #218 from gbrammer/miri-time-dependent
Browse files Browse the repository at this point in the history
Compute time-dependent photometric correction for MIRI
  • Loading branch information
gbrammer authored Apr 19, 2024
2 parents d0ddfc4 + 9e1beb5 commit 06a349c
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 11 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/publish-to-pypi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Build sdist
run: |
pip install build
python -m build --sdist
- uses: actions/upload-artifact@v3
- uses: actions/upload-artifact@v4
with:
path: dist/*.tar.gz

Expand All @@ -56,7 +56,7 @@ jobs:
github.event.ref_type == 'tag' &&
github.ref == 'refs/heads/${{ github.event.repository.default_branch }}'
steps:
- uses: actions/download-artifact@v3
- uses: actions/download-artifact@v4
with:
name: artifact
path: dist
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/python-package-with-jwst.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ jobs:
python-version: ['3.9', '3.10']
os: [ubuntu-latest]
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install package
Expand Down
12 changes: 6 additions & 6 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ jobs:
python-version: [3.8, 3.9, '3.10']
os: [ubuntu-latest]
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Build check
Expand All @@ -35,9 +35,9 @@ jobs:
python-version: ['3.10']
os: [ubuntu-latest]
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install package
Expand All @@ -54,11 +54,11 @@ jobs:
python-version: [3.8, 3.9, '3.10']
os: [ubuntu-latest]
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install package
Expand Down
116 changes: 116 additions & 0 deletions grizli/jwst_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2589,6 +2589,8 @@ def get_crds_zeropoint(instrument='NIRCAM', detector='NRCALONG', filter='F444W',
Get ``photmjsr`` photometric zeropoint for a partiular JWST instrument imaging
mode
To-do: add MIRI time-dependence
Parameters
----------
instrument : str
Expand Down Expand Up @@ -3366,3 +3368,117 @@ def compute_siaf_pa_offset(c1, c2, c2_pa=202.9918, swap_coordinates=True, verbos
print(f'Catalog offset: {dx[0].to(u.arcsec):5.2f} {dx[1].to(u.arcsec):5.2f} new APA: {c2_pa + dphi:.5f}')

return c2_pa + dphi, dphi


def get_miri_photmjsr(file=None, filter='F770W', subarray='FULL', mjd=60153.23, photom_file='jwst_miri_photom_0201.fits', verbose=True):
"""
Get time-dependent MIRI photometry values
Parameters
----------
file : str
Image filename
filter : str
MIRI filter name
subarray : str
Detector subarray used
mjd : float
Observation epoch
photom_file : str
CRDS ``photom`` reference file name
verbose : bool
messaging
Returns
-------
photmjsr : float
Photometric scaling
photom_corr : float
Time-dependent correction for the filter and observation epoch
"""
import astropy.io.fits as pyfits
import jwst.datamodels

if file is not None:
with pyfits.open(file) as im:
h = im[0].header
filter = h['FILTER']
mjd = h['EXPSTART']
try:
subarray = h['SUBARRAY']
except KeyError:
pass

try:
from jwst.photom.miri_imager import time_corr_photom
except ImportError:
# msg = 'Failed to import `jwst.photom.miri_imager` to include time-dependence'
# utils.log_comment(utils.LOGFILE, msg, verbose=verbose)

time_corr_photom = time_corr_photom_copy

PATH = os.path.join(os.getenv('CRDS_PATH'), 'references', 'jwst', 'miri')
local_file = os.path.join(PATH, photom_file)
remote_file = 'https://jwst-crds.stsci.edu/unchecked_get/references/jwst/'
remote_file += 'jwst_miri_photom_0201.fits'

use_path = local_file if os.path.exists(local_file) else remote_file

with jwst.datamodels.open(use_path) as ref:

test = ref.phot_table['filter'] == filter
test &= ref.phot_table['subarray'] == subarray
if test.sum() == 0:
msg = f'Row not found in {photom_file} for {filter} / {subarray}'
utils.log_comment(utils.LOGFILE, msg, verbose=verbose)

return np.nan, None

row = np.where(test)[0][0]
photmjsr = ref.phot_table['photmjsr'][row]

try:
photom_corr = time_corr_photom(ref.timecoeff[row], mjd)
except:
photom_corr = 0.


return (photmjsr, photom_corr)


def time_corr_photom_copy(param, t):
"""
Short Summary
--------------
Time dependent PHOTOM function.
The model parameters are amplitude, tau, t0. t0 is the reference day
from which the time-dependent parameters were derived. This function will return
a correction to apply to the PHOTOM value at a given MJD.
N.B.: copied from [jwst.photom.miri_imager](https://github.com/spacetelescope/jwst/blob/master/jwst/photom/miri_imager.py#L9)
Parameters
----------
param : numpy array
Set of parameters for the PHOTOM value
t : int
Modified Julian Day (MJD) of the observation
Returns
-------
corr: float
The time-dependent correction to the photmjsr term.
"""

amplitude, tau, t0 = param["amplitude"], param["tau"], param["t0"]
corr = amplitude * np.exp(-(t - t0)/tau)

return corr
32 changes: 32 additions & 0 deletions grizli/tests/test_jwst.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,38 @@ def test_filter_info(self):
info = jwst_utils.get_jwst_filter_info(header)
assert(info['name'] == 'F560W')
assert(np.allclose(info['pivot'], 5.632612))


def test_miri_photom(self):

try:
import jwst
except ImportError:
return None

photmjsr, corr = jwst_utils.get_miri_photmjsr(
file=None,
filter='F770W',
subarray='FULL',
mjd=60153.23,
photom_file='jwst_miri_photom_0201.fits',
verbose=True,
)

assert np.allclose([photmjsr], 0.25890106)
assert np.allclose([corr], 0.)

# Missing filter
photmjsr, corr = jwst_utils.get_miri_photmjsr(
file=None,
filter='xF770W',
subarray='FULL',
mjd=60153.23,
photom_file='jwst_miri_photom_0201.fits',
verbose=True,
)

assert np.isnan(photmjsr)


class TestJWSTFittingTools:
Expand Down

0 comments on commit 06a349c

Please sign in to comment.