simple fits image+wcs rotation

3,358 views
Skip to first unread message

Wolfgang Kerzendorf

unread,
Nov 29, 2012, 11:21:27 AM11/29/12
to astro...@googlegroups.com
Hey guys,

I'm standing before the age-old problem again: Is there an easy and nice way to rotate a fits image including its wcs. Jess Mink's wscstools promise a possibility to rotate, but in the end don't actually work. I played around with montage, which rotates the image, but doesn't seem to update the wcs.

Maybe pyAST can do it?

Cheers
Wolfgang

Tim Jenness

unread,
Nov 29, 2012, 1:44:54 PM11/29/12
to astro...@googlegroups.com, David Berry
AST can do it with astRebin so long as the input and output framesets are constructed (input just comes from the FITS header and you stack on a rotation). David can send you example code.

David Berry

unread,
Nov 29, 2012, 2:38:34 PM11/29/12
to astro...@googlegroups.com
On 29 November 2012 18:44, Tim Jenness <tim.j...@gmail.com> wrote:
> AST can do it with astRebin so long as the input and output framesets are
> constructed (input just comes from the FITS header and you stack on a
> rotation). David can send you example code.

Ouch - I didn't realise it was an "age-old problem" ! This is exactly
the sort of thing AST was written for, and all Starlink apps use it in
this way. E.g. the kappa:rotate command
(http://docs.jach.hawaii.edu/star/sun95.htx/node394.html). But you
probably don't want to to download the whole starlink software just to
rotate a FITS file.

Tim is correct that the Ast.rebin function could be used to rotate the
actual pixel values, and then you would need to correct the WCS.
Writing a complete rotation app would need an hour or two which I
don't have just at the moment. But I've written a small script that
will just correct the WCS after the rotation has been performed by
some other means:

https://gist.github.com/4171277

For example:

% rotate_wcs.py fred.fits 100 120 35

will rotate the WCS in the primary header of image "fred.fits" by 35
degrees about pixel (100,120) without changing the pixel values
themselves.

Sadly, whilst putting this together I came across a small bug in the
way pyast interacts with pyfits. I've fixed it, so to run the script
you need pyast V2.2 (available from github or pypi).

David

Wolfgang Kerzendorf

unread,
Nov 29, 2012, 3:06:18 PM11/29/12
to astro...@googlegroups.com
Thanks David for sharing this. I was, however, more looking for somethings that rotates both the WCS and image at the same time (well not rotating the wcs at all in regards to the image).
I think we have another killer-feature here that astropy can strife for. Having an image class that can rotate both wcs and the image with just saying image.rotate(45, center=None #then it just picks the image center).

DS9 must somehow do this internally (rotation of the wcs and resampling of the image), but I don't know what it uses.

Cheers
Wolfgang

Tim Jenness

unread,
Nov 29, 2012, 4:16:21 PM11/29/12
to astro...@googlegroups.com
Wolfgang,

   I think you misunderstand. AST can do it using the Ast.rebin routine and that's what the Starlink ROTATE command does. It's just that no wrapper exists to simplify it as a "take my FITS file, rotate, write out rotated FITS file" command in python. David couldn't generate you that example this evening.

David Berry

unread,
Nov 29, 2012, 6:03:30 PM11/29/12
to astro...@googlegroups.com
On 29 November 2012 20:06, Wolfgang Kerzendorf <wkerz...@gmail.com> wrote:
> Thanks David for sharing this. I was, however, more looking for somethings that rotates both the WCS and image at the same time (well not rotating the wcs at all in regards to the image).

It would be another 10 to 20 lines of code in the script to rotate the
pixel values themselves, in addition to the WCS. I thought you had
some existing application that rotated the pixels, but did not rotate
the WCS. So the script I sent was meant to be run after the pixel
rotation to correct the wcs.

Basically, the script needs a call to Ast.resample (probably better
than Ast.rebin) that uses the "totalmap" mapping from the existing
script to rotate the pixel values. And then some pyfits odds and ends
to write the rotated data array and the rotated WCS out to a new fits
file. I'll look into it in the morning.

> I think we have another killer-feature here that astropy can strife for.

I have clearly been living a sheltered life :-)

David

Thomas Robitaille

unread,
Nov 29, 2012, 6:13:51 PM11/29/12
to astropy-dev mailing list
Hi Wolfgang,

Just for info, Montage should indeed be able to do it. Were you trying
to use mRotate? If so, maybe a better way to do it would be to use
mProject? Just define the new header you want to project to, including
rotation, and mProject the image to it. If this doesn't work, can you
post an example of what you are trying?

Cheers,
Tom

Wolfgang Kerzendorf

unread,
Nov 29, 2012, 6:27:45 PM11/29/12
to astro...@googlegroups.com
Hi David, and Tom,

I have rotated my image and didn't need to do anything very fancy: aplpy's FITSFigure (north=True) does this (as tom probably noticed I made python-montage astropy aware ;-) ). I think I tried to use mRotate in the past which screwed up. There seem to be ways - but they are not as straight forward as I had hoped.

David, it is clear that you need to get out more. Rotating FITS with WCS is what kids do these days ;-)

Anyways, our future image class should either have a method which does .rotate(angle, centerx, centrey) or some function that does that. While I'm thinking about this I also want a shift(deltax, deltay). (crosses fingers) Any day now, right?


Cheers
Wolfgang

David Berry

unread,
Nov 30, 2012, 10:39:30 AM11/30/12
to astro...@googlegroups.com
On 29 November 2012 23:27, Wolfgang Kerzendorf <wkerz...@gmail.com> wrote:
> Hi David, and Tom,
>
> I have rotated my image and didn't need to do anything very fancy: aplpy's FITSFigure (north=True) does this (as tom probably noticed I made python-montage astropy aware ;-) ). I think I tried to use mRotate in the past which screwed up. There seem to be ways - but they are not as straight forward as I had hoped.
>
> David, it is clear that you need to get out more. Rotating FITS with WCS is what kids do these days ;-)

We've being doing it for 10-15 years in Starlink. My sheltered life
means I didn't realise others weren't doing it then as well!

> Anyways, our future image class should either have a method which does .rotate(angle, centerx, centrey) or some function that does that. While I'm thinking about this I also want a shift(deltax, deltay). (crosses fingers) Any day now, right?

A demo script is at

https://gist.github.com/4176461

will translate a FITS image and correct the WCS accordingly. I've also
modified the rotation script to rotate the pixel values as well as the
WCS. They are both basically the same. The only significant difference
is i the nature of the mapping used to do the resampling and WCS
correction.

Took me a bit longer than expected because I found some bugs in the
way pyast interacts with pyfits. So the new scripts need pyast V2.3.

David
Reply all
Reply to author
Forward
0 new messages