Skip to content

Commit

Permalink
Wrap reading FITS file in context manager.
Browse files Browse the repository at this point in the history
Both for the HDU reading itself
and to catch FITSFixedWarning which happens for all RomanDESCSims Roman images.
  • Loading branch information
wmwv committed Jul 19, 2024
1 parent af26ba7 commit de7c5f5
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions python/RomanDESCForwardModelLightcurves.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import os
import re
from typing import Optional
import warnings

import astrophot as ap
import astropy.units as u
Expand All @@ -64,7 +65,7 @@
from astropy.coordinates import SkyCoord
from astropy.io import fits
from astropy.table import Table, join
from astropy.wcs import WCS
from astropy.wcs import FITSFixedWarning, WCS


class Config:
Expand Down Expand Up @@ -326,28 +327,38 @@ def make_target(
psf_size: float, width of the PSF
do_mask: Use mask.
"""
hdu = fits.open(image_filepath)
header = hdu[0].header # Primary header
img = hdu[hdu_idx["image"]].data # Image HDU
var = hdu[hdu_idx["variance"]].data # Variance HDU

# The RomanDESCSims Roman files have a misformatted DATE-OBS
# (" " instead of "T" separator)
# so we build the targets in a context manager to suppress the warning
# messages for things that were automatically fixed when reading the file.
with warnings.catch_warnings():
warnings.simplefilter("ignore", category=FITSFixedWarning)
with fits.open(image_filepath) as hdu:
header = hdu[0].header # Primary header
img = hdu[hdu_idx["image"]].data # Image HDU
# The WCS is stored in the image header not the primary header.
# So grab that here.
img_header = hdu[hdu_idx["image"]].header # Image HDU Header
var = hdu[hdu_idx["variance"]].data # Variance HDU

if do_mask:
# We need to read the informative mask with a bad-value mask.
# E.g., for an LSST Science Pipelines mask, mask values
# don't mean bad, they just indicate something about the pixel
# E.g., one flag is that that pixel is part of a footprint
# of a valid object and we don't want to mask those!
informative_mask = hdu[hdu_idx["mask"]].data # Mask
bad_pixel_mask = informative_mask & bad_pixel_bitmask

wcs = WCS(img_header)
band = header["FILTER"]
sca = header["SCA_NUM"]

zp_band = {"H158": 32.603}

if do_mask:
# But need to translate the informative mask with a bad-pixel mask.
# E.g., for an LSST Science Pipelines mask, one of the mask values
# is that that pixel is part of a footprint of a valid object
# We don't want to mask those!
informative_mask = hdu[hdu_idx["mask"]].data # Mask
bad_pixel_mask = informative_mask & bad_pixel_bitmask

if zeropoint is None:
zeropoint = zp_band[band] # + 2.5 * np.log10(header["EXPTIME"])

wcs = WCS(hdu[hdu_idx["image"]].header)
x, y = wcs.world_to_pixel(coord)

psf = get_roman_psf(band, sca, x, y)
Expand Down Expand Up @@ -376,8 +387,6 @@ def make_target(
# This needs to be added to truth file "mag" to get calibrated mag
target.header.sim_zptmag = header["ZPTMAG"]

hdu.close()

return target


Expand Down

0 comments on commit de7c5f5

Please sign in to comment.