Skip to content

replace non-ASCII characters in header - #2887

Closed
pkubanek wants to merge 1 commit into
astropy:masterfrom
pkubanek:master
Closed

pkubanek wants to merge 1 commit into
astropy:masterfrom
pkubanek:master

Conversation

@pkubanek

Copy link
Copy Markdown

Fixed problems with non-ASCII (e.g. UTF-8) characters in FITS header. This is a real issue with e.g. Astrometry.net, which puts into HISTORY local name of its build - and if the build was done on e.g. Czech locale, one will see UTF-8 characters in header (such as čt, which is for čtvrtek, Thursday). Such FITS headers open fine in Python and are probably valid, but due to header encoding does not work with wcs.

@astrofrog

Copy link
Copy Markdown
Member

@pkubanek - technically speaking, I think the FITS standard requires ASCII headers (see http://www.aanda.org/articles/aa/pdf/2010/16/aa15362-10.pdf, Section 3.2).But @embray should comment on this.

@pkubanek

Copy link
Copy Markdown
Author

I know FITS standard would be more strict, and I will urge my friends at Astrometry.net to drop local day names from HISTORY keyword. But so far this is the first case when we hit problems with our FITS - other tools and libraries, including IRAF, cftsio and ds9, works with them fine. I think this change will not cause any other problem, and will align WCS package with what is de-facto accepted. I can provide test FITS frame to support my claims.

@embray

embray commented Aug 26, 2014

Copy link
Copy Markdown
Member

FWIW, currently PyFITS/astropy.io.fits will choke on non-ASCII characters in the header (last I checked). I've been meaning to fix that for a while--the header parsing already has code to handle invalid characters, but the loop that actually reads through FITS files and determines the beginnings and ends of headers is broken in that respect. So maybe that should be handled at the io.fits level rather than anywhere in the WCS module.

That said, Astrometry.net should definitely be given a talking to about this.

@embray

embray commented Aug 26, 2014

Copy link
Copy Markdown
Member

@pkubanek Also, you wrote

Such FITS headers open fine in Python and are probably valid

I'm surprised they open, given my comments that non-ASCII headers don't generally work. Maybe I fixed that after all and forgot that I did? :) In any case they're definitely not valid regardless of whether they happen to work. Could you send me a link to download a sample file that exhibits this problem?

@pkubanek

Copy link
Copy Markdown
Author

Proof file: http://lascaux.asu.cas.cz/~petr/broken_wcs.fits

Code:

import astropy.wcs
import astropy.io.fits

f = astropy.io.fits.open('broken_wcs.fits')

print f[0].header['HISTORY']

w = astropy.wcs.WCS('broken_wcs.fits')
ra,dec = w.all_pix2world(100,200,0)
print ra,dec

@embray

embray commented Sep 2, 2014

Copy link
Copy Markdown
Member

Ah yes, now I remember--this does read on Python 2 but not on Python 3 due to currently somewhat unsophisticated handling of encodings. That should be fixed, but in the fits module, not the wcs module.

@embray

embray commented Sep 2, 2014

Copy link
Copy Markdown
Member

Although, on the other hand, as PyFITS does try to display non-ASCII characters (so that they can be corrected, if nothing else), maybe this patch is still a good idea just in case, if the wcslib code will blow up otherwise.

@mdboom

mdboom commented Sep 2, 2014

Copy link
Copy Markdown
Contributor

Maybe we do what this patch does, but additionally display a warning if the header contains non-ascii characters?

@pllim

pllim commented Dec 2, 2016

Copy link
Copy Markdown
Member

I am digging through old FITS PRs. Is this still necessary?

@embray

embray commented Mar 30, 2017

Copy link
Copy Markdown
Member

I think it's still relevant but the issue needs to be fixed on the fits.Header end of things.

@Cadair

Cadair commented Mar 30, 2017

Copy link
Copy Markdown
Member

I have done horrible work arounds for some solar data where there are unicode chars in the comments of a header (I know I know), so I would like a nicer way of dealing with this situation.

@embray

embray commented Mar 31, 2017

Copy link
Copy Markdown
Member

It's probably a relatively easy fix. If nothing else the header parser needs to catch non-ASCII bytes and either replace them with a replacement character (e.g. '?' and/or raise an error--perhaps depending on a setting).

@saimn

saimn commented May 15, 2017

Copy link
Copy Markdown
Contributor

Opened #6074 to keep track of this issue, and closing as the fix has to be done in io.fits.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants