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

fix has_quote detection to handle free-form string values in header cards #421

Merged
merged 9 commits into from
Feb 20, 2025
14 changes: 12 additions & 2 deletions fitsio/fitsio_pywrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -4444,8 +4444,18 @@ PyFITSObject_read_header(struct PyFITSObject* self, PyObject* args) {
is_string_value=0;
}
} else {
int j;
has_equals = (card[8] == '=') ? 1 : 0;
has_quote = (card[10] == '\'') ? 1 : 0;
has_quote = 0;
// Look for 0 or more space characters followed by a quote character.
for (j=10; j<FLEN_CARD; j++) {
if (card[j] == '\'') {
has_quote = 1;
break;
} else if (card[j] != ' ') {
break;
}
}
if (has_equals && has_quote) {
is_string_value=1;
} else {
Expand Down Expand Up @@ -4511,7 +4521,7 @@ PyFITSObject_read_header(struct PyFITSObject* self, PyObject* args) {

} else {

// if its a stringwe just store it.
// if it's a string we just store it.
if (is_string_value) {
convert_to_ascii(longstr);
add_string_to_dict(dict,"value",longstr);
Expand Down
17 changes: 17 additions & 0 deletions fitsio/tests/test_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,23 @@
from ..hdu.base import INVALID_HDR_CHARS


def test_free_form_string():
with tempfile.TemporaryDirectory() as tmpdir:
fname = os.path.join(tmpdir, 'test.fits')
with open(fname, 'w') as f:
s = ("SIMPLE = T / Standard FITS " + # noqa
"BITPIX = 16 / number of bits per data pixel " + # noqa
"NAXIS = 0 / number of data axes " + # noqa
"EXTEND = T / File contains extensions " + # noqa
"PHOTREF = 'previous MegaCam' / Source: cum.photcat " + # noqa
"EXTRA = 7 / need another line following PHOTREF " + # noqa
"END " # noqa
)
f.write(s + ' ' * (2880-len(s)))
hdr = read_header(fname)
assert hdr['PHOTREF'] == 'previous MegaCam'


def test_add_delete_and_update_records():
# Build a FITSHDR from a few records (no need to write on disk)
# Record names have to be in upper case to match with FITSHDR.add_record
Expand Down
Loading