簡體   English   中英

如何修剪.fits圖像並保持世界坐標以便在不尋常的Python中進行繪制?

[英]How do I trim a .fits image and keep world coordinates for plotting in astropy Python?

這個問題困擾了我一段時間。 我正在嘗試處理.fits文件形式的大量數據(大約11000x9000像素)。 我需要做的是為天空中的許多對象創建一個“放大”的RA / Dec坐標圖(最好使用astropy.wcs),其輪廓來自一個fits文件,而灰度來自另一個fits文件(或來自另一個fits文件的熱圖)。

我的問題是,每當我從圖像中切片數據(到我感興趣的區域)時,我都會失去與天空坐標的關聯。 這意味着切片的圖像不在正確的位置。

我改編自繁瑣的文檔中的示例,以節省您的數據麻煩 注意:我希望輪廓覆蓋的區域比圖像大,無論解決方案是對兩個數據都適用

RH圖中的“圖像”應居中!

這是我遇到麻煩的代碼:

from matplotlib import pyplot as plt
from astropy.io import fits
from astropy.wcs import WCS
from astropy.utils.data import download_file
import numpy as np

fits_file = 'http://data.astropy.org/tutorials/FITS-images/HorseHead.fits'
image_file = download_file(fits_file, cache=True)
hdu = fits.open(image_file)[0]
wmap = WCS(hdu.header)
data = hdu.data

fig = plt.figure()
ax1 = fig.add_subplot(121, projection=wmap)
ax2 = fig.add_subplot(122, projection=wmap)
# Scale input image
bottom, top = 0., 12000.
data = (((top - bottom) * (data - data.min())) / (data.max() - data.min())) + bottom


'''First plot'''
ax1.imshow(data, origin='lower', cmap='gist_heat_r')

# Now plot contours
xcont = np.arange(np.size(data, axis=1))
ycont = np.arange(np.size(data, axis=0))
colors = ['forestgreen','green', 'limegreen']
levels = [2000., 7000., 11800.]

ax1.contour(xcont, ycont, data, colors=colors, levels=levels, linewidths=0.5, smooth=16)
ax1.set_xlabel('RA')
ax1.set_ylabel('Dec')
ax1.set_title('Full image')

''' Second plot ''' 
datacut = data[250:650, 250:650]
ax2.imshow(datacut, origin='lower', cmap=cmap)
ax2.contour(xcont, ycont, data, colors=colors, levels=levels, linewidths=0.5, smooth=16)

ax2.set_xlabel('RA')
ax2.set_ylabel('')
ax2.set_title('Sliced image')
plt.show()

我嘗試使用切片的WCS坐標來解決此問題,但是我不確定是否可以將其傳遞到任何地方!

pixcoords = wcs.wcs_pix2world(zip(*[range(250,650),range(250,650)]),1)

好消息是:您也可以簡單地astropy.WCS ,這使得您的任務相對瑣碎:

...

wmapcut = wmap[250:650, 250:650] # sliced here
datacut = data[250:650, 250:650]
ax2 = fig.add_subplot(122, projection=wmapcut) # use sliced wcs as projection
ax2.imshow(datacut, origin='lower', cmap='gist_heat_r')
# contour has to be sliced as well
ax2.contour(np.arange(datacut.shape[0]), np.arange(datacut.shape[1]), datacut, 
            colors=colors, levels=levels, linewidths=0.5, smooth=16)
...

在此處輸入圖片說明

如果文件具有不同的WCS,則可能需要進行一些重新投影(例如,參見reproject

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM