# Rotating DP0.2 \`exposures\` with WCS

**URL:** <https://www.rubin.community/t/rotating-dp0-2-exposures-with-wcs/10085>\
**Category:** Support\
**Tags:** dp0\
**Created:** [May 20, 2025, 2:00pm UTC](https://www.rubin.community/t/rotating-dp0-2-exposures-with-wcs/10085 "2025-05-20T14:00:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![altavilla](https://sea2.discourse-cdn.com/flex002/user_avatar/www.rubin.community/altavilla/32/3148_2.png) [@altavilla](https://www.rubin.community/u/altavilla)\
**Post date:** [May 20, 2025, 2:00pm UTC](https://www.rubin.community/t/rotating-dp0-2-exposures-with-wcs/10085/1 "2025-05-20T14:00:53Z")

</div>

Hi, I have a doubt related to this topic, so I’ll revive it. I read the previous answers and the DP02\_03a\_Image\_Display\_and\_Manipulation notebook.  
I wanted to rotate the images to have North up and East to the left (so RA along x-axis and DEC along y-axis, with RA increasing to the left and Dec increasing towards the top).  
I found a function to rotate images in DP02\_14\_Injecting\_Synthetic\_Sources (a modified version of this [code](https://github.com/lsst/atmospec/blob/1e7d6e8e5655cc13d71b21ba866001e6d49ee04e/python/lsst/atmospec/utils.py#L259-L301)).  
I successfully rotated the image and the coordinates grid as expected, but the coordinates of the grid seem to be wrong.  
I attach the original frame (with a marker on a big galaxy)

 ![original](https://us1.discourse-cdn.com/flex002/uploads/rubin/original/2X/7/723f5760d1c9a851949427071fbc3098d4499f4c.png)  
the rotated one  
 ![rotated_1](https://us1.discourse-cdn.com/flex002/uploads/rubin/original/2X/d/d54531362cdebaae688c85ed9aed683ec694c4b4.png)  
and an image showing the two frames side by side with the marker that does not fall on the galaxy in the rotated images (but falls at the right galaxy coordinates).  
 ![rotated_2](https://us1.discourse-cdn.com/flex002/uploads/rubin/original/2X/5/558ca4d47bdcc0f2b2c54e0749349a9303a32bef.png)  
Maybe the function I used rotates in pixel but does not handle wcs?  
Or maybe I’m missing something or I’m doing something wrong…  
Thanks for any suggestion.

This is the function I used:

```auto
def rotate_exposure(exp, n_degrees):
    """Rotate an exposure by nDegrees clockwise.

    Parameters
    ----------
    exp : `lsst.afw.image.exposure.Exposure`
        The exposure to rotate
    n_degrees : `float`
        Number of degrees clockwise to rotate by

    Returns
    -------
    rotated_exp : `lsst.afw.image.exposure.Exposure`
        A copy of the input exposure, rotated by nDegrees
    """
    n_degrees = n_degrees % 360

    wcs = exp.getWcs()

    warper = afwMath.Warper('lanczos4')

    affine_rot_transform = geom.AffineTransform.makeRotation(n_degrees*geom.degrees)
    transform_p2top2 = afwGeom.makeTransform(affine_rot_transform)
    rotated_wcs = afwGeom.makeModifiedWcs(transform_p2top2, wcs, False)

    rotated_exp = warper.warpExposure(rotated_wcs, exp)
    return rotated_exp

```

---

<div class="post-metadata">

**Author:** ![plazas](https://sea2.discourse-cdn.com/flex002/user_avatar/www.rubin.community/plazas/32/867_2.png) [@plazas](https://www.rubin.community/u/plazas)\
**Post date:** [May 20, 2025, 6:15pm UTC](https://www.rubin.community/t/rotating-dp0-2-exposures-with-wcs/10085/2 "2025-05-20T18:15:37Z")

</div>

Thank you, @altavilla, for your questions! I’ve moved this to a new topic under the `support` category (original topic where this was posted: [Calexps displayed with imshow seem offset from WCS center](http://www.rubin.community/t/calexps-displayed-with-imshow-seem-offset-from-wcs-center/9475)). Thank you for sharing the function you used. To reproduce the error, would you mind sharing all the code you used to produce the images you posted, including the part that calls this function? Additionally, could you please share the LSST Science Pipelines version you were using when spawning the notebook in the `data.lsst.cloud` RSP? Thanks!

---

<div class="post-metadata">

**Author:** ![altavilla](https://sea2.discourse-cdn.com/flex002/user_avatar/www.rubin.community/altavilla/32/3148_2.png) [@altavilla](https://www.rubin.community/u/altavilla)\
**Post date:** [May 21, 2025, 6:59am UTC](https://www.rubin.community/t/rotating-dp0-2-exposures-with-wcs/10085/3 "2025-05-21T06:59:19Z")

</div>

Hi, I used the RSP selecting the following options:

- Recommended (Weekly 2025\_17)
- Large (4.0 CPU, 16Gi RAM)  
In attachment the notebook I created, maybe there is a mistake…  
[testRotation.ipynb](https://www.rubin.community/uploads/short-url/408bZhRaH1OCYhNRalM0Jram63s.ipynb) (3.0 MB)  
Thanks for the reply!

---

<div class="post-metadata">

**Author:** ![altavilla](https://sea2.discourse-cdn.com/flex002/user_avatar/www.rubin.community/altavilla/32/3148_2.png) [@altavilla](https://www.rubin.community/u/altavilla)\
**Post date:** [May 22, 2025, 10:28am UTC](https://www.rubin.community/t/rotating-dp0-2-exposures-with-wcs/10085/4 "2025-05-22T10:28:26Z")

</div>

Hi, indeed there were some mistakes in my notebook, here the revised version and result  
[testRotationCorr.ipynb](https://www.rubin.community/uploads/short-url/wph2CjV2muujk5bD6EfQek1iUvs.ipynb) (897.7 KB)

 ![original_corr](https://us1.discourse-cdn.com/flex002/uploads/rubin/original/2X/b/b77d7c8f63026099e928ca57d6da9e203b59d150.png)

 ![rotated_1corr](https://us1.discourse-cdn.com/flex002/uploads/rubin/original/2X/b/bb32878fd408dbddab6bbc1349d99610332d89ee.png)  
Sorry for bothering

---

<div class="post-metadata">

**Author:** ![plazas](https://sea2.discourse-cdn.com/flex002/user_avatar/www.rubin.community/plazas/32/867_2.png) [@plazas](https://www.rubin.community/u/plazas)\
**Post date:** [May 22, 2025, 7:31pm UTC](https://www.rubin.community/t/rotating-dp0-2-exposures-with-wcs/10085/5 "2025-05-22T19:31:14Z")

</div>

No problem at all! It seems that the addition of `extent=image_rotated_extent` in the last call to `imshow` helped fix the issue. The `extent` keyword should always be used. I’ll mark your post as the solution (thank you for sharing your corrected code!), but feel free to open a new topic in the `support` category if you have any other questions.
