[AstroPy] [pywcs] Unit question

Michael Droettboom mdroe at stsci.edu
Mon Apr 4 09:45:15 EDT 2011

On 04/04/2011 08:27 AM, Ole Streicher wrote:
> Dear James,
> Am 01.04.2011 18:30, schrieb James Turner:
>> Oh, sorry, that probably didn't make sense; it couldn't convert to m
>> it didn't know what it was. Should think longer before posting... I
>> did notice that there's a units class in pywcs (under API
>> documentation), so maybe that will help?
> This helps a bit, thank you. However, my problem is that I want to put
> not just the wavelength there but coordinates for all dimensions.
> So, I have a loop like
> for i, s in enumerate(wcs.wcs_pix2sky([origin], 1)[0]):
>      conv = pywcs.UnitConverter(want = h.header['CUNIT%i' % (i+1)])
>      h.header['CRVAL%i' % (i+1)] = conv.convert(s)
>      h.header['CRPIX%i' % (i+1)] = 1
> The Problem here is now that wcs.UnitConverter wants the "have" unit,
> which is the of the wcs.wcs_sky2pix() output. How do I canonically get
> the measurement units of the coordinates returned by wcs_sky2pix()?
I'm having a little trouble following what you are trying to do.

In WCS, the spectral axes values in the file (in pixel space) are in 
undefined arbitrary units, and the WCS defines a translation from that 
to a standard unit (of which many are supported) defined in CUNITi.  So 
there's no way to really canonically get the measurement units of the 
coordinates returned by wcs_sky2pix.  To get the units returned by 
wcs_pix2sky, use wcs.wcs.units[wcs.wcs.spec].  If you want to get the 
original values back, you can run them through wcs_sky2pix, or just copy 
them from the original array.

In the above example, the "have" argument should be the input unit, and 
here you are passing the output of pix2sky, so it's in sky space.  That 
unit can be obtained with wcs.wcs.unit[wcs.wcs.spec].  Then your "want" 
unit can be whatever canonical unit you want to standardize on.

I hope this makes sense.  Feel free to attach your input FITS file so we 
can experiment further and get a sense of what you're trying to do.


Michael Droettboom
Science Software Branch
Space Telescope Science Institute
Baltimore, Maryland, USA

More information about the AstroPy mailing list