如何在不调整大小和不修改颜色的情况下查看此.fits图像?

时间:2018-03-20 02:35:36

标签: python python-3.x python-imaging-library fits aplpy

我正在尝试从'.fits'文件打开全彩色图像。但是,将其与相应的'.gif'图像进行比较时,似乎有关于其颜色和大小的错误。

如何以适当的尺寸查看真彩色图像?

例如,可以选择'.fits'文件和相应的'.gif'文件located at the top of this webpage。我的示例代码使用APLPY模块,如下所示。

def from_fits_to_image(color_scheme, rloc, rname='synop_Ml_0.2104', rext='.fits', cmap=None):
    """ 
    color_scheme : 'rgb', 'grayscale', or 'false color'; color scheme of image to be shown
    rloc         : type <str>; location of file to be read
    rname        : type <str>; name of file to be read
    rext         : type <str>; extension of file to be read
    cmap         : None or type <str>; colormap
    """
    rpath = rloc + rname + rext
    if color_scheme == 'rgb':
        pic = aplpy.FITSFigure(rpath)
        # pic.show_rgb(alt_filename) # what filename is supposed to go here?
    else:
        pic = aplpy.FITSFigure(rpath)
        if color_scheme == 'grayscale':
            pic.show_grayscale()
        elif color_scheme == 'false color':
            if cmap is None:
                pic.show_colorscale()
            else:
                pic.show_colorscale(cmap=cmap)
    # plt.savefig(...)
    plt.show()

只要提供正确的rloc(已下载的'.fits'文件的位置)和color_scheme,就会运行上述代码。

调用下面的函数将显示正确尺寸的空图。为了使它非空,我必须提供另一个现有的文件名,但我不清楚究竟应该是什么。

from_fits_to_image(color_scheme='rgb', rloc=rloc) 

enter image description here

下面的每个函数调用都会显示一个已调整为小的图。虽然color_scheme='grayscale'似乎可以正确地着色图,但其他方法无法正确地着色图像。

from_fits_to_image('grayscale', rloc=rloc)

enter image description here

from_fits_to_image('false color', rloc=rloc)

enter image description here

from_fits_to_image('false color', rloc=rloc, cmap='plasma')

enter image description here

为了进行比较,'.gif'图片位于下方。理想情况下,输出看起来与下图完全相同。

修改

我尝试过使用astropyPILpyfits失败了。任何帮助将不胜感激。

enter image description here

编辑2:

以下是使用fits中的astropy.io的结果。

from astropy.io import fits

def reada(rloc, rname='synop_Ml_0.1998', rext='.fits'):
    """ """
    rpath = rloc + rname + rext
    # hdu_list = fits.open(rpath)
    # hdu_list.info()
    pic = fits.getdata(rpath)
    plt.imshow(pic)
    plt.show()

reada(rloc=rloc)

我曾与vminvmax kwargs玩过,但没有成功。此外,即使使用pyfits,使用pyfits.open(rpath, uint=True, do_not_scale_image_data=True)打开文件也会导致以下错误:

TypeError: Image data can not convert to float

enter image description here

1 个答案:

答案 0 :(得分:2)

从gif来看,似乎图像是假彩色图像,这是选择正确的彩色图表的问题。我不能说python中是否有与您链接的颜色映射相同的颜色映射,但我们可以找到非常接近的颜色映射,重新创建映像中的所有功能:

fig = aplpy.FITSFigure('synop_Ml_0.2104.fits')
fig.show_colorscale(vmin=-60, vmax=60, cmap='hot')

显示以下内容(注意aplpy不理解此坐标系,因此它以像素坐标绘制图形): enter image description here

问题的第二部分比较棘手,我无法完全回答。首先,您需要将Carrington时间转换为经度,将纬度转换为度数,然后绘制轴标签的新值,而不是旧的,或者作为寄生轴与旧的一起绘制(您可以参考寄生虫)轴示例here)。

现在看起来Carrington时间只是自1853年11月9日以来旋转的度数,并且x轴精确地跨越360,所以我假设转换只是偏移757079.95,左边的x轴值边缘。我们可以通过查看地图的像素跨度如何与坐标跨度相对应来对世界坐标进行仔细检查:

In [88]: fig._wcs.wcs_pix2world(fig._ax1.get_xlim(), fig._ax1.get_ylim(), origin=1)
Out[88]: [array([757439.95, 757079.95]), array([-1.,  1.])]

xaxis边缘的值757079.95和757439.95的差异恰好是360度。

那么我们可以使用一些matplotlib技巧来手动偏移坐标值以强制它们从零到360并让xaxis与你的gif图像匹配:

# using hidden attributes is non-pythonic but aplpy does not leave us other options
ax = fig._ax1
x_range = ax.get_xlim()
new_ticklabels = np.arange(60, 361, 60)
new_tick_positions = new_ticklabels / 360. * x_range[1] + x_range[0]
ax.set_xticks(new_tick_positions)
ax.set_xticklabels(new_ticklabels)
fig.axis_labels.set_xtext('Carrington Longitude')

enter image description here

请记住,aplpy是一个用于绘制天体坐标而非太阳坐标的图书馆,因此让轴转换为正常工作可能是一个相当痛苦的过程。另一种方法,也许是更好的方法,是用sunpy绘制拟合文件,用于太阳物理学的python library。但是,我从来没有使用它,它似乎为这个特殊的拟合文件抛出错误。看起来您需要修改适合文件的标题以正确读取坐标。如果您想使用该库,也许您可​​以与sunpy社区联系?