[AstroPy] Pywcs: Creation, access to properties

Michael Droettboom mdroe at stsci.edu
Fri Apr 29 12:36:57 EDT 2011

On 04/29/2011 11:04 AM, Ole Streicher wrote:
> Hello,
> I have two questions about pywcs:
> First is, how do I create a pywcs.WCS object not from a file, but with
> certain parameters? The way I do it now is first to create a header with
> the required keywords, and then use the pywcs.WCS constructor, but this
> seems to me a bit indirect.
Have you seen this?

> The second question is on how do I access some parameters in a robust
> way? For example, I want to get the instrument resolution in arcsec out
> of the image. Currently, I implemented it like
> import numpy
> import pywcs
> def get_resolution(wcs, units = None)
>       c = numpy.sqrt(numpy.sum((wcs.wcs.cd if wcs.wcs.has_cd()
>                                 else wcs.wcs.pc) ** 2, axis = 0))
>       if units is None:
>         return c
>       else
>         return [
>            pywcs.UnitConverter(from_unit, to_unit).convert(cc)
>            if to_unit is not None else cc
>            for from_unit, to_unit, cc in zip(wcs.wcs.cunit, units, c)
>            ]
> but this looks a bit weird, and I feel that I still did not took all
> possible cases to construct the WCS. Is there a simpler way?
Are you trying to determine the size of the output frame?  The WCS 
object has a calcFootprint method:


By calculating the length of the edges, one could approximate the size 
in arcsec of the image.  I say approximate because the output frame is 
not necessarily rectangular.

The cd and pc matrices are just affine transformations that don't 
necessarily have anything to do with the image size.  I see that 
checking for either the cd or pc matrices is annoying.  (I recently saw 
some other code that had to do this, too).  Wcslib under the hood, in 
fact, converts everything to pc/cdelt form canonically -- but pywcs only 
provides access to one at a time because it then it becomes unclear 
which values to use if one or the other is changed.  I think it would be 
handy to add a read-only "get_pc" and "get_cdelt" functions, though, so 
that one could always get the canonical form of the various alternative 
representations.  I'll try to add this to the SVN repository today.


> This question arises since I often have to implement something like
> "give me the pixel position just 1 arcsec upwards (in CCD orientation)
> from the image center".
> Best regards
> Ole
> _______________________________________________
> AstroPy mailing list
> AstroPy at scipy.org
> http://mail.scipy.org/mailman/listinfo/astropy

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

More information about the AstroPy mailing list