Skip to content

Commit

Permalink
Merge pull request #87 from eslavich/eslavich-fits-array-casting
Browse files Browse the repository at this point in the history
Support casting of FITS_rec with unsigned integer column
  • Loading branch information
eslavich authored Nov 18, 2021
2 parents 413485e + b237652 commit 293ec94
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
- Add schema feature to forward deprecated model attributes to
a new location. [#86]

- Support casting of FITS_rec tables with unsigned integer columns. [#87]

0.3.0 (2021-09-03)
==================

Expand Down
7 changes: 5 additions & 2 deletions src/stdatamodels/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,13 @@ def gentle_asarray(a, dtype):
def _safe_asanyarray(a, dtype):
if isinstance(a, fits.fitsrec.FITS_rec):
if any(c.bzero is not None for c in a.columns):
# Due to an issue in astropy, it's not safe to convert
# Due to an issue in astropy, it's not safe to directly cast
# a FITS_rec with a pseudo-unsigned column.
# See https://github.com/astropy/astropy/issues/12112
raise ValueError("Cannot convert FITS_rec dtype")
result = np.zeros(a.shape, dtype=dtype)
for old_col, new_col in zip(a.dtype.names, result.dtype.names):
result[new_col] = a[old_col]
return result

return np.asanyarray(a, dtype=dtype)

Expand Down
10 changes: 6 additions & 4 deletions tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ def test_gentle_asarray_fits_rec_pseudo_unsigned(tmp_path):
assert result["col1"][3] == 4

# This tests the case where a table with a pseudo unsigned integer column
# is opened from a FITS file and needs to be cast. Due to a bug in astropy
# this isn't safe so we expect an exception.
# is opened from a FITS file and needs to be cast. This requires special
# handling on our end to dodge the bug.
file_path = tmp_path / "test.fits"

data = np.array([(0,)], dtype=[("col1", np.uint16)])
Expand All @@ -98,8 +98,10 @@ def test_gentle_asarray_fits_rec_pseudo_unsigned(tmp_path):
hdul.writeto(file_path)

with fits.open(file_path) as hdul:
with pytest.raises(ValueError, match="Cannot convert FITS_rec dtype"):
util.gentle_asarray(hdul[-1].data, dtype=[("col1", np.uint32)])
result = util.gentle_asarray(hdul[-1].data, dtype=[("col1", np.uint32)])
# Without the fix, the value in the array would be 128 due to bzero
# shift being applied twice.
assert result[0][0] == 0


def test_gentle_asarray_nested_array():
Expand Down

0 comments on commit 293ec94

Please sign in to comment.