astropy/astropy — pull request #1854 by mdboom (2013-12-02 18:46:59+00:00)
Title: Issues with WCS fixing

If I save the following header to a file ``test.hdr``:

```
SIMPLE = T /Standard FITS
BITPIX = -32 /Floating point (32 bit)
NAXIS = 4
NAXIS1 = 400
NAXIS2 = 400
NAXIS3 = 1
NAXIS4 = 1
BSCALE = 1.000000000000E+00 /PHYSICAL = PIXEL*BSCALE + BZERO
BZERO = 0.000000000000E+00
EQUINOX = 2.000000000000E+03
LONPOLE = 1.800000000000E+02
LATPOLE = -1.220463888890E+01
CTYPE1 = 'RA---SIN'
CRVAL1 = 9.831781727830E+02
CDELT1 = -5.555555555556E-05
CRPIX1 = 2.010000000000E+02
CUNIT1 = 'deg '
CTYPE2 = 'DEC--SIN'
CRVAL2 = -3.339919992992E+01
CDELT2 = 5.555555555556E-05
CRPIX2 = 2.010000000000E+02
CUNIT2 = 'deg '
CTYPE3 = 'VELO-LSR'
CRVAL3 = -0.000000000000E+00
CDELT3 = -7.293888888828E+05
CRPIX3 = 1.000000000000E+00
CUNIT3 = 'M/S '
CTYPE4 = 'STOKES '
CRVAL4 = 1.000000000000E+00
CDELT4 = 1.000000000000E+00
CRPIX4 = 1.000000000000E+00
CUNIT4 = ' '
```

then try and parse it, I see a number of issues:

```
In [1]: from astropy.io import fits

In [2]: h = fits.Header.fromtextfile('test.hdr')

In [3]: from astropy.wcs import WCS

In [4]: w =WCS(h)
WARNING: FITSFixedWarning: 'celfix' made the change 'In CUNIT2 : Mismatched units type 'charge': have 'm/S', want 'm/s''. [astropy.wcs.wcs]
WARNING: FITSFixedWarning: 'spcfix' made the change 'Changed CTYPE3 from 'VELO-LSR' to 'VOPT', and SPECSYS to 'LSRK''. [astropy.wcs.wcs]
```

Now the warning is correct that M/S is invalid since it should really be m/s. But the error message mentions CUNIT2 when it should be CUNIT3. After reading the file, the unit has been fixed to ``m/S``, but it really should be ``m/s``:

```
In [6]: w.wcs.cunit
Out[6]: ['deg', 'deg', 'm/S', '']
```

Since CUNIT3 indicates it is a velocity, is there a way it could figure out that m/s makes more sense than meters per siemens?

Finally, I see the following error:

```
In [7]: w.to_header()
ERROR: RuntimeError: Unknown error occurred. Something is seriously wrong. [astropy.wcs.wcs]
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
in ()
----> 1 w.to_header()

/Volumes/Raptor/Library/Python/3.3/lib/python/site-packages/astropy-0.3.dev6466-py3.3-macosx-10.8-x86_64.egg/astropy/wcs/wcs.py in to_header(self, relax, key)
1624
1625 if self.wcs is not None:
-> 1626 header_string = self.wcs.to_header(relax)
1627 header = fits.Header.fromstring(header_string)
1628 else:

RuntimeError: Unknown error occurred. Something is seriously wrong.
```

cc @mdboom