Skip to content
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

Add cref #275

Merged
merged 4 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changes/275.maintenance.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added filling of CREF keyword (IRF axis order) in the output files
5 changes: 5 additions & 0 deletions pyirf/io/gadf.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ def create_aeff2d_hdu(
header["HDUCLAS3"] = "POINT-LIKE" if point_like else "FULL-ENCLOSURE"
header["HDUCLAS4"] = "AEFF_2D"
header["DATE"] = Time.now().utc.iso
header["CREF5"] = "(ENERG_LO:ENERG_HI,THETA_LO:THETA_HI)"
jsitarek marked this conversation as resolved.
Show resolved Hide resolved
_add_header_cards(header, **header_cards)

return BinTableHDU(aeff, header=header, name=extname)
Expand Down Expand Up @@ -133,6 +134,7 @@ def create_psf_table_hdu(
header["HDUCLAS3"] = "FULL-ENCLOSURE"
header["HDUCLAS4"] = "PSF_TABLE"
header["DATE"] = Time.now().utc.iso
header["CREF7"] = "(ENERG_LO:ENERG_HI,THETA_LO:THETA_HI,RAD_LO:RAD_HI)"
_add_header_cards(header, **header_cards)

return BinTableHDU(psf_, header=header, name=extname)
Expand Down Expand Up @@ -191,6 +193,7 @@ def create_energy_dispersion_hdu(
header["HDUCLAS3"] = "POINT-LIKE" if point_like else "FULL-ENCLOSURE"
header["HDUCLAS4"] = "EDISP_2D"
header["DATE"] = Time.now().utc.iso
header["CREF7"] = "(ENERG_LO:ENERG_HI,MIGRA_LO:MIGRA_HI,THETA_LO:THETA_HI)"
_add_header_cards(header, **header_cards)

return BinTableHDU(edisp, header=header, name=extname)
Expand Down Expand Up @@ -247,6 +250,7 @@ def create_background_2d_hdu(
header["HDUCLAS3"] = "FULL-ENCLOSURE"
header["HDUCLAS4"] = "BKG_2D"
header["DATE"] = Time.now().utc.iso
header["CREF5"] = "(ENERG_LO:ENERG_HI,THETA_LO:THETA_HI)"
_add_header_cards(header, **header_cards)

return BinTableHDU(bkg, header=header, name=extname)
Expand Down Expand Up @@ -300,6 +304,7 @@ def create_rad_max_hdu(
header["HDUCLAS3"] = "POINT-LIKE"
header["HDUCLAS4"] = "RAD_MAX_2D"
header["DATE"] = Time.now().utc.iso
header["CREF5"] = "(ENERG_LO:ENERG_HI,THETA_LO:THETA_HI)"
_add_header_cards(header, **header_cards)

return BinTableHDU(rad_max_table, header=header, name=extname)
29 changes: 29 additions & 0 deletions pyirf/io/tests/test_gadf.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,32 @@ def test_rad_max_schema(rad_max_hdu):

_, hdu = rad_max_hdu
RAD_MAX.validate_hdu(hdu)


def test_cref(aeff2d_hdus, edisp_hdus, psf_hdu, bg_hdu, rad_max_hdu):
for point_like in range (2):
hdus = [fits.PrimaryHDU(),
aeff2d_hdus[1][point_like],
edisp_hdus[1][point_like],
psf_hdu[1],
bg_hdu[1],
rad_max_hdu[1]]

with tempfile.NamedTemporaryFile(suffix='.fits') as f:
fits.HDUList(hdus).writeto(f.name)
readhdul = fits.open(f.name)
for hdu in readhdul[1:]: # skip Primary
names=hdu.data.columns.names
# the IRF is always in the last column
cref = hdu.header['CREF'+str(len(names))]
# drop parentheses and get the expected list of axes
cref = cref[1:-1].split(',')
# transposed due to different fits and numpy axis order
readshape=hdu.data[names[-1]].T.shape
# only one set of IRFs saved per test file
assert readshape[-1] == 1, "first axis size is not 1"

for i in range(len(readshape)-1):
for axis in cref[i].split(':'):
err="not matching shape in "+hdu.header['EXTNAME']+" at axis "+axis
assert readshape[i] == hdu.data[axis].shape[1], err
Loading