[AstroPy] Cutout2D with weird formatting

Eric Jensen ejensen1 at swarthmore.edu
Mon Mar 29 21:32:06 EDT 2021


Hi Dominik, 

You should be able to do this by modifying the FITS header to remove the references to the third and fourth dimensions. 

Adding these lines to your code should do the trick, before you create the WCS from the header: 

# Remove length-1 dimensions: 
the_map = np.squeeze(hdul[0].data)

# Get rid of references to third and fourth
# dimensions in the FITS header: 
the_header['NAXIS'] = 2
for f in (['CTYPE', 'CRVAL', 'CDELT', 
           'CRPIX', 'CUNIT', 'NAXIS']):
    for i in ['3', '4']:
        del(the_header[f + i])
wcs = WCS(header=the_header)


Eric


> On Mar 25, 2021, at 11:44 AM, Dominik Rhiem <hg.dude.hg at gmail.com> wrote:
> 
> Dear all,
> 
> I am using FITS maps produced by a program called PyBDSF used for source detection and removal, and I want to make cutouts from these maps. However, the formatting of these maps is a bit weird and I am having trouble with making Cutout2D work with it. I have attached the fits file. Some sample code:
> hdul = fits.open('test.fits')
> the_map = hdul[0].data
> the_header = hdul[0].header
> position = (the_header['CRPIX1'], the_header['CRPIX2']) #I want the cutout to be centred on the map
> size = 400 * u.arcsec #the cutout should have a total extent of 400 arcsec in x and y direction
> wcs = WCS(header=the_header)
> cutout = Cutout2D(the_map, position, size, wcs)
> 
> Which results in:
> ValueError: "large_array_shape" and "small_array_shape" must have the same number of dimensions.
> Importantly, the array has a shape of (1, 1, 600, 600), i.e. the actual 2D data array is found via:
> the_map = the_map[0][0]
> which is reflected in the wcs. If I try to use that definition of the array for the cutout, I get this error:
> ValueError: operands could not be broadcast together with shapes (4,) (2,) (4,)
> 
> How can I solve either of these issues?
> 
> Sincerely,
> Dominik
> 
> <test.fits>_______________________________________________
> AstroPy mailing list
> AstroPy at python.org
> https://mail.python.org/mailman/listinfo/astropy

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4388 bytes
Desc: not available
URL: <https://mail.python.org/pipermail/astropy/attachments/20210329/af7d7448/attachment.bin>


More information about the AstroPy mailing list