[AstroPy] AstroPy Digest, Vol 207, Issue 2

Hessman, Frederic fhessma at uni-goettingen.de
Mon Apr 29 03:37:06 EDT 2024


Just an update on plotting images on pre-configured WCS, in this case a fake FK5 image on a galactic coordinate grid.

Thomas' idea of using reproject works great: just need to add

    from reproject import reproject_interp                   # OR reproject_adaptive FOR ANTI-ALIASING
    array,footprint = reproject_interp (hdu,gal_hdr)     # hdu=ORIGINAL (RA,DEC) IMAGE

and pyplot.imshow() will give you the correct coordinate positioning.  After reading the WCS docs again, I could easily manipulate the formatting of the axes, but the formatting method

	ax.coords[0].set_major_formatter ('%.3f')          # SUPPOSED TO BE THE SAME AS 'd.ddd'

(see https://docs.astropy.org/en/stable/visualization/wcsaxes/ticks_labels_grid.html) results in an error : if you'd like to try it out, I'd be happy to send the updated script.

One thing that doesn't work is the initial positioning of the image: whereas the galactic coordinates of my fake object @ RA,DEC=(00:11:22,33:44:55) are (113.469,-28.386), the plot is centered at around the major tics at (113.460,-28.390); no problem, one can shove the image to the center by hand, but one wonders why it's necessary, given that the positioning of the image should be defined by the coordinate axis WCS which was centered on the object and the image was placed correctly within both WCS.

It still seems that the behaviour of pyplot & WCS using the syntax

    fk5_to_gal = ax.get_transform(wcs)
    ax.imshow (data,origin='lower',interpolation='nearest',transform=fk5_to_gal)

that correcting rotates the image data but incorrectly creates fake data to turn a rotated image outline into a non-rotated image outline is doing something incorrectly.  This would be a much more straight-forward route if it worked.

Thanks for the great help, as always!

Rick

> On 24 Apr 2024, at 18:00, astropy-request at python.org wrote:
> 
> Send AstroPy mailing list submissions to
> 	astropy at python.org
> 
> To subscribe or unsubscribe via the World Wide Web, visit
> 	https://mail.python.org/mailman/listinfo/astropy
> or, via email, send a message with subject or body 'help' to
> 	astropy-request at python.org
> 
> You can reach the person managing the list at
> 	astropy-owner at python.org
> 
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of AstroPy digest..."
> 
> 
> Today's Topics:
> 
>   1. Re: how to display an image on an different WCS
>      (BOCH Thomas (OBAS))
>   2. Re: how to display an image on an different WCS
>      (j.koot at airbusds.nl)
> 
> 
> ----------------------------------------------------------------------
> 
> Message: 1
> Date: Tue, 23 Apr 2024 17:52:38 +0200 (CEST)
> From: "BOCH Thomas (OBAS)" <thomas.boch at astro.unistra.fr>
> To: Astronomical Python mailing list <astropy at python.org>
> Subject: Re: [AstroPy] how to display an image on an different WCS
> Message-ID:
> 	<682287989.7081986.1713887558704.JavaMail.zimbra at unistra.fr>
> Content-Type: text/plain; charset=utf-8
> 
> Hi Frederic,
> 
> If I understand correctly your use case, you will need to reproject your image into the new WCS. reproject ( https://pypi.org/project/reproject/ ) is a good candidate for the task.
> 
> Cheers,
> Thomas
> 
> ----- Mail d?origine -----
> De: Hessman, Frederic <fhessma at uni-goettingen.de>
> ?: Astronomical Python mailing list <astropy at python.org>
> Envoy?: Tue, 23 Apr 2024 17:14:45 +0200 (CEST)
> Objet: [AstroPy] how to display an image on an different WCS
> 
> There are tutorials and plenty of examples showing how to put coordinates onto a plot of an image given the image's WCS, but what about the other way around:  define the coordinate system of the plot and then place the image in that frame?
> 
> Let's say one has a 101x101 image with a flat blob on it and an outer frame (so one can easily see rotations) and whose pixels are in an unrotated FK5 frame, centered at (00:11:22,-33:44:55) : the accompanying script will demo all of this.
> 
> I've got a WCS for the image, called "wcs"
> 
> CTYPE : 'RA---TAN' 'DEC--TAN'
> CRVAL : 2.8416666666666663 33.74861111111111
> CRPIX : 51.0 51.0
> PC1_1 PC1_2  : 1.0 0.0
> PC2_1 PC2_2  : 0.0 1.0
> CDELT : 0.000277777777777777 0.000277777777777777
> NAXIS : 101  101
> 
> I want to display the image where the galactic coordinates are the main axes, so I've created a galactic WCS from scratch at the same central position, called "gal_wcs".
> 
> CTYPE : 'GLON-TAN' 'GLAT-TAN'
> CRVAL : 113.46911966371806 -28.38577360534605
> CRPIX : 51.0 51.0
> PC1_1 PC1_2  : 1.0 0.0
> PC2_1 PC2_2  : 0.0 1.0
> CDELT : 0.000277777777777777 0.000277777777777777
> NAXIS : 101  101
> 
> Naively, I'd expect to be able to do it this way :
> 
> fig = plt.figure(figsize=(4,4))
> 
> ax = plt.subplot (projection=gal_wcs, label='galactic')
> ax.coords.grid (True, color='gray',ls='solid')
> ax.coords[0].set_axislabel (r'Galactic Longitude')
> ax.coords[1].set_axislabel (r'Galactic Latitude')
> 
> overlay = ax.get_coords_overlay ('fk5')
> overlay.grid (True, color='gray',ls='dotted')
> overlay[0].set_axislabel (r'Right Ascension (J2000)')
> overlay[1].set_axislabel (r'Declination (J2000)')
> 
> fk5_2_gal = ax.get_transform(wcs) # TRANSFORMATION FROM wcs=FK5 TO ax=galactic
> ax.imshow (data,origin='lower',interpolation='nearest',transform=fk5_to_gal)
> 
> but this doesn't work: the image contents are rotated but matplotlib creates data outside of the frame to maintain an un-rotated area!  ?
> 
> In order to see what's happening, one can increase the size of the coordinate space, e.g.
> 
> plt.xlim (right=0-0.2*nx,left=nx-1+0.2*nx)
> plt.ylim (top=0-0.2*ny,bottom=ny-1+0.2*ny)
> 
> but this doesn't really change anything: one sees the displayed image is not rotated, just the contents, and one sees the area of data created by matplotlib outside of the rotated image.  The area used is not the same in the two cases, so it's not simply matplotlib filling in the area defined by gal_wcs.
> 
> Where am I confused?  Can someone suggest an example I've missed?  Running python 3.9.11, astropy 6.0.0, matplotlib 3.5.1 on Darwin 12.7.4 / Monterey.
> 
> Rick
> 
> 
> 
> 
> 
> 
> ------------------------------
> 
> Message: 2
> Date: Tue, 23 Apr 2024 17:15:25 +0200
> From: j.koot at airbusds.nl
> To: Astronomical Python mailing list <astropy at python.org>
> Subject: Re: [AstroPy] how to display an image on an different WCS
> Message-ID: <E1rzHrh-0032WM-0U at smtp.airbusds.nl>
> 
> Dear reader,
> 
> Please note that your email might not reach me.
> 
> Airbus Netherlands has migrated to Airbus' company-wide email platform.
> This means that from now on my email address is sjaak.koot at airbus.com.
> I kindly request you to update my contact details accordingly. Thank you
> very much in advance.
> 
> Kind regards,
> Sjaak Koot
> 
> 
> ------------------------------
> 
> Subject: Digest Footer
> 
> _______________________________________________
> AstroPy mailing list
> AstroPy at python.org
> https://mail.python.org/mailman/listinfo/astropy
> 
> 
> ------------------------------
> 
> End of AstroPy Digest, Vol 207, Issue 2
> ***************************************



More information about the AstroPy mailing list