Add logical_as_bytes parameter to preserve NULL values with logical columns in FITS#19374
Add logical_as_bytes parameter to preserve NULL values with logical columns in FITS#19374astrofrog wants to merge 1 commit intoastropy:mainfrom
Conversation
…l columns
The FITS standard specifies that logical columns can contain 'T' (True),
'F' (False), or 0x00 (NULL/undefined). Previously, astropy converted all
logical columns to numpy bool, which cannot represent the NULL state -
NULL values were silently converted to False.
This change adds a `logical_as_bytes` parameter (analogous to the existing
`character_as_bytes`) that can be passed to `fits.open()`. When True,
logical columns are returned as int8 arrays containing the raw byte values:
- 84 (ord('T')) for True
- 70 (ord('F')) for False
- 0 for NULL
Additionally, a warning is now issued when NULL values are present and
logical_as_bytes=False (the default), alerting users that NULL information
is being lost.
|
Thank you for your contribution to Astropy! 🌌 This checklist is meant to remind the package maintainers who will review this pull request of some common things to look for.
|
|
As a side note, if we go with this approach we can also make it so that |
|
Ok so interestingly this shows that at least one of our existing test files (tb.fits) actually contains such a column (which is then causing a warning in another test which is changed to an exception). |
|
Thanks for diving into this issue @astrofrog :) |
|
Also the same solution should be used for VLA columns (#18755), it can be done in another PR but just mentioning it to remind that the chosen approach should be compatible. |
This is an experimental fix for #18815
Essentially it:
logical_as_byteswhich is analogous tocharacter_as_bytesand whenTruewill return logical columns as byteslogical_as_byteskeyword argument:We can of course try and find another solution, but I thought it would be worth trying this out since it's not too complex in terms of lines changed (which includes the new test) and mirrors how data is read in with CFITSIO (see #18815 (comment)). There's also precedent for this kind of solution (
character_as_bytes) and it is backward-compatible.