esheldon / fitsio

A python package for FITS input/output wrapping cfitsio
GNU General Public License v2.0
133 stars 57 forks source link

write_keys fails to write comments #90

Closed kadrlica closed 8 years ago

kadrlica commented 8 years ago

With fitsio 0.9.7 I've been trying to copy a header from one hdu to another and I've noticed that the comments are not preserved. Looking here, I think the problem is that the value of a COMMENT record is blank. For example:

{'card': 'COMMENT 160627_28/iff1025c8.fits ut160627_28/iff1026c8.fits',
 'comment': '160627_28/iff1025c8.fits ut160627_28/iff1026c8.fits',
 'name': 'COMMENT',
 'value': '',
 'value_orig': ''}
esheldon commented 8 years ago

did you also try git master?

On 7/2/16, Alex Drlica-Wagner notifications@github.com wrote:

With fitsio 0.9.7 I've been trying to copy a header from one hdu to another and I've noticed that the comments are not preserved. Looking here, I think the problem is that the value of a COMMENT record is blank. For example:

{'card': 'COMMENT 160627_28/iff1025c8.fits ut160627_28/iff1026c8.fits',
 'comment': '160627_28/iff1025c8.fits ut160627_28/iff1026c8.fits',
 'name': 'COMMENT',
 'value': '',
 'value_orig': ''}

You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHub: https://github.com/esheldon/fitsio/issues/90

Erin Scott Sheldon Brookhaven National Laboratory erin dot sheldon at gmail dot com

kadrlica commented 8 years ago

I just tried with the current master and I have the same issue:

> git describe
v0.9.8rc2-19-g2b1e8fa

> python
>>> import fitsio
>>> fitsio.__version__
'0.9.8'
>>> fits = fitsio.FITS('bpm/iffbpmc1.fits')
>>> fits[0].read_header()

SIMPLE  =                    T / file does conform to FITS standard
BITPIX  =                   16 / number of bits per data pixel
NAXIS   =                    2 / number of data axes
NAXIS1  =                 2048 / length of data axis 1
NAXIS2  =                 4096 / length of data axis 2
EXTEND  =                    T / FITS dataset may contain extensions
COMMENT   FITS (Flexible Image Transport System) format is defined in 'Astronomy
COMMENT   and Astrophysics', volume 376, page 359; bibcode: 2001A&A...376..359H
CHIP    =                    1
BIT0    = 'EDGE    '
BIT1    = 'WACKY   '
BIT2    = 'GROW    '
BIT3    = 'FILL    '
SOFTWARE= 'imacs-mkbpm 0.1.0+0.gc0b6647.dirty'
COMMENT imacs-mkbpm --wacky-nsig 5 --wacky-npix 2 --grow-nsig 2.7 --edge-npix 0
COMMENT --bottom-npix 4 -m med/iffmedc1.fits -o bpm/iffbpmc1.fits ut160627_28/if
COMMENT f1022c1.fits ut160627_28/iff1023c1.fits ut160627_28/iff1024c1.fits ut160
COMMENT 627_28/iff1025c1.fits ut160627_28/iff1026c1.fits
>>> h = fits[0].read_header()
>>> d = fits[0].read()
>>> out = fitsio.FITS('tmp.fits','rw')
>>> out.write(d,header=h)
>>> out.close()
>>> fitsio.read_header('tmp.fits')

SIMPLE  =                    T / file does conform to FITS standard
BITPIX  =                   16 / number of bits per data pixel
NAXIS   =                    2 / number of data axes
NAXIS1  =                 2048 / length of data axis 1
NAXIS2  =                 4096 / length of data axis 2
EXTEND  =                    T / FITS dataset may contain extensions
COMMENT   FITS (Flexible Image Transport System) format is defined in 'Astronomy
COMMENT   and Astrophysics', volume 376, page 359; bibcode: 2001A&A...376..359H
CHIP    =                    1
BIT0    = 'EDGE    '
BIT1    = 'WACKY   '
BIT2    = 'GROW    '
BIT3    = 'FILL    '
SOFTWARE= 'imacs-mkbpm 0.1.0+0.gc0b6647.dirty'

I've opened PR #91 with my proposed fix. It has been working in my script, but I haven't tested it much more thoroughly.

esheldon commented 8 years ago

This was actually a bug when converting the original header entries.

When reading the original header cards, I extract some basic info for convenience, along with the original card string, into a dict. But when creating the FITSHDR I should have properly done the conversion from the original card_string rather than just an update, only copied over the information I extracted.

Please pull from master and give it a try.

esheldon commented 8 years ago

Did the bug fix resolve the issue?

kadrlica commented 8 years ago

The comments are written, but there is an extra space inserted at the beginning. This causes the comments to exceed the maximum number of characters per line and increases the number of comment cards.

Original:

COMMENT imacs-mkbpm --wacky-nsig 5 --wacky-npix 2 --grow-nsig 2.7 --edge-npix 0
COMMENT --bottom-npix 4 -m med/iffmedc7.fits -o bpm/iffbpmc7.fits ut160627_28/if
COMMENT f1022c7.fits ut160627_28/iff1023c7.fits ut160627_28/iff1024c7.fits ut160
COMMENT 627_28/iff1025c7.fits ut160627_28/iff1026c7.fits

Copied:

COMMENT  imacs-mkbpm --wacky-nsig 5 --wacky-npix 2 --grow-nsig 2.7 --edge-npix 0
COMMENT  --bottom-npix 4 -m med/iffmedc7.fits -o bpm/iffbpmc7.fits ut160627_28/i
COMMENT f
COMMENT  f1022c7.fits ut160627_28/iff1023c7.fits ut160627_28/iff1024c7.fits ut16
COMMENT 0
COMMENT  627_28/iff1025c7.fits ut160627_28/iff1026c7.fits
esheldon commented 8 years ago

I pushed a fix for this

kadrlica commented 8 years ago

Thanks, looks like that worked.

kadrlica commented 8 years ago

I think this issue can be closed.