Skip to content

Commit

Permalink
JP-3494: write tweakreg source catalogs into user-specified output_dir (
Browse files Browse the repository at this point in the history
#8386)

Co-authored-by: Howard Bushouse <[email protected]>
  • Loading branch information
emolter and hbushouse committed Apr 2, 2024
1 parent 704239b commit 804743f
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 18 deletions.
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ ramp_fitting
to use uint16 instead of uint8, in order to avoid potential
overflow/wraparound problems. [#8377]

tweakreg
--------

- Output source catalog file now respects ``output_dir`` parameter. [#8386]


1.14.0 (2024-03-29)
===================
Expand Down
27 changes: 27 additions & 0 deletions jwst/tweakreg/tests/test_tweakreg.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,24 @@

import asdf
from astropy.modeling.models import Shift
from astropy.table import Table
import pytest

from jwst.tweakreg import tweakreg_step
from jwst.tweakreg import tweakreg_catalog
from stdatamodels.jwst.datamodels import ImageModel


@pytest.fixture
def dummy_source_catalog():

columns = ['id', 'xcentroid', 'ycentroid', 'flux']
catalog = Table(names=columns, dtype=(int, float, float, float))
catalog.add_row([1, 100.0, 100.0, 100.0])

return catalog


@pytest.mark.parametrize("offset, is_good", [(1 / 3600, True), (11 / 3600, False)])
def test_is_wcs_correction_small(offset, is_good):
path = os.path.join(os.path.dirname(__file__), "mosaic_long_i2d_gwcs.asdf")
Expand Down Expand Up @@ -63,3 +74,19 @@ def test_expected_failure_bad_starfinder():
model = ImageModel()
with pytest.raises(ValueError):
tweakreg_catalog.make_tweakreg_catalog(model, 5.0, bkg_boxsize=400, starfinder='bad_value')


def test_write_catalog(dummy_source_catalog, tmp_cwd):
'''
Covers an issue where catalog write did not respect self.output_dir
'''

OUTDIR = 'outdir'
model = ImageModel()
step = tweakreg_step.TweakRegStep()
os.mkdir(OUTDIR)
step.output_dir = OUTDIR
expected_outfile = os.path.join(OUTDIR, 'catalog.ecsv')
step._write_catalog(model, dummy_source_catalog, 'catalog.ecsv')

assert os.path.exists(expected_outfile)
72 changes: 54 additions & 18 deletions jwst/tweakreg/tweakreg_step.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,23 +243,7 @@ def process(self, input):
.format(len(catalog), filename))

if new_cat and self.save_catalogs:
catalog_filename = filename.replace(
'.fits', '_cat.{}'.format(self.catalog_format)
)
if self.catalog_format == 'ecsv':
fmt = 'ascii.ecsv'
elif self.catalog_format == 'fits':
# NOTE: The catalog must not contain any 'None' values.
# FITS will also not clobber existing files.
fmt = 'fits'
else:
raise ValueError(
'\'catalog_format\' must be "ecsv" or "fits".'
)
catalog.write(catalog_filename, format=fmt, overwrite=True)
self.log.info('Wrote source catalog: {}'
.format(catalog_filename))
image_model.meta.tweakreg_catalog = catalog_filename
image_model = self._write_catalog(image_model, catalog, filename)

# Temporarily attach catalog to the image model so that it follows
# the grouping by exposure, to be removed after use below
Expand Down Expand Up @@ -412,7 +396,10 @@ def process(self, input):
# whatever convention is determined by the JWST Cal Working
# Group.
if self.save_abs_catalog:
output_name = 'fit_{}_ref.ecsv'.format(self.abs_refcat.lower())
if self.output_dir is None:
output_name = 'fit_{}_ref.ecsv'.format(self.abs_refcat.lower())
else:
output_name = path.join(self.output_dir, 'fit_{}_ref.ecsv'.format(self.abs_refcat.lower()))
else:
output_name = None

Expand Down Expand Up @@ -524,6 +511,55 @@ def process(self, input):
)

return images


def _write_catalog(self, image_model, catalog, filename):
'''
Determine output filename for catalog based on outfile for step
and output dir, then write catalog to file.
Parameters
----------
image_model : jwst.datamodels.ImageModel
Image model containing the source catalog.
catalog : astropy.table.Table
Table containing the source catalog.
filename : str
Output filename for step
Returns
-------
image_model : jwst.datamodels.ImageModel
Image model with updated catalog information.
'''

catalog_filename = str(filename).replace(
'.fits', '_cat.{}'.format(self.catalog_format)
)
if self.catalog_format == 'ecsv':
fmt = 'ascii.ecsv'
elif self.catalog_format == 'fits':
# NOTE: The catalog must not contain any 'None' values.
# FITS will also not clobber existing files.
fmt = 'fits'
else:
raise ValueError(
'\'catalog_format\' must be "ecsv" or "fits".'
)
if self.output_dir is None:
catalog.write(catalog_filename, format=fmt, overwrite=True)
else:
catalog.write(
path.join(self.output_dir, catalog_filename),
format=fmt,
overwrite=True
)
self.log.info('Wrote source catalog: {}'
.format(catalog_filename))
image_model.meta.tweakreg_catalog = catalog_filename

return image_model


def _is_wcs_correction_small(self, wcs, twcs):
"""Check that the newly tweaked wcs hasn't gone off the rails"""
Expand Down

0 comments on commit 804743f

Please sign in to comment.