欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

aplpy画连续谱图

程序员文章站 2022-03-21 08:29:08
...
import aplpy
from astropy.io import fits
import matplotlib.pyplot as plt
import numpy as np
import os

inputfits=['sim1_full.fits','sim1_910k.fits','sim2_full.fits','sim2_910k.fits','sim3_full.fits','sim3_910k.fits']
outputfits=['sim1_full_aplpy.fits','sim1_910_aplpy.fits','sim2_full_aplpy.fits','sim2_910_aplpy.fits','sim3_full_aplpy.fits','sim3_910k_aplpy.fits']

##casa 生成的fits文件无法直接被aplpy读取绘图,需要对头文件进行修改。
##主要修改方法就是删除头文件中的第三和第四轴的头文件信息,只保留前两个轴的信息。
def make_new_fits(i):
    #change header:
    f = fits.open(inputfits[i])
    hdr = f[0].header
    hdr.set('NAXIS',2)
    hdr.remove('NAXIS3')                                             
    hdr.remove('NAXIS4')                                                                          
    hdr.remove('PC1_1')                                             
    hdr.remove('PC2_1')                                             
    hdr.remove('PC3_1')                                             
    hdr.remove('PC4_1')                                             
    hdr.remove('PC1_2')                                             
    hdr.remove('PC2_2')                                             
    hdr.remove('PC3_2')                                             
    hdr.remove('PC4_2')                                             
    hdr.remove('PC1_3')                                             
    hdr.remove('PC2_3')                                             
    hdr.remove('PC3_3')                                             
    hdr.remove('PC4_3')                                             
    hdr.remove('PC1_4')                                             
    hdr.remove('PC2_4')                                             
    hdr.remove('PC3_4')                                             
    hdr.remove('PC4_4')                                                                                  
    hdr.remove('CTYPE3')                                             
    hdr.remove('CRVAL3')                                             
    hdr.remove('CDELT3')                                             
    hdr.remove('CRPIX3')                                             
    hdr.remove('CUNIT3')                                             
    hdr.remove('CTYPE4')                                             
    hdr.remove('CRVAL4')                                             
    hdr.remove('CDELT4')                                             
    hdr.remove('CRPIX4')                                             
    hdr.remove('CUNIT4')   
    #make new fits:
    hdu = fits.PrimaryHDU(f[0].data[0][0],header=hdr)
    path_exists = os.path.exists(outputfits[i])
    #print(path_exists)
    if path_exists==True:
        os.remove(outputfits[i])
        
    hdu.writeto(outputfits[i])
    
make_new_fits(1)

def subplot():
    fig = plt.figure(figsize=(21,14))
    f1 = aplpy.FITSFigure(outputfits[0],figure=fig,subplot=(2,3,1))
    f1.recenter(290.916875,14.51819444444,radius=0.0002)
    f1.show_colorscale(vmin=0.0,vmax=0.05)
    f1.add_beam()
    f1.add_colorbar()
    f1.show_contour(outputfits[0],colors='White',levels=[0.007,0.01,0.02,0.03,0.04])
    f1.add_label(0.5, 0.90, 'without embedded sources\n full uvrange', relative=True,size=20,color='White')
    
    f2 = aplpy.FITSFigure(outputfits[1],figure=fig,subplot=(2,3,4))
    f2.recenter(290.916875,14.51819444444,radius=0.0002)
    f2.show_colorscale(vmin=0.0,vmax=0.002)
    f2.add_beam()
    f2.add_colorbar()
    f2.show_contour(outputfits[1],colors='White',levels=[0.007,0.01,0.02,0.03,0.04])
    f2.add_label(0.5, 0.9, 'without embedded sources\n uvrange > 910k$\lambda$', relative=True,size=20,color='White')
    
    f3 = aplpy.FITSFigure(outputfits[2],figure=fig,subplot=(2,3,2))
    f3.recenter(290.916875,14.51819444444,radius=0.0002)
    f3.show_colorscale(vmin=0.0,vmax=0.05)
    f3.add_beam()
    f3.add_colorbar()
    f3.tick_labels.hide_y()
    f3.show_contour(outputfits[2],colors='White',levels=[0.007,0.01,0.02,0.03,0.04])
    f3.axis_labels.hide()
    f3.add_label(0.5, 0.90, 'with 3 point sources\n full uvrange', relative=True,size=20,color='White')
    
    f4 = aplpy.FITSFigure(outputfits[3],figure=fig,subplot=(2,3,5))
    f4.recenter(290.916875,14.51819444444,radius=0.0002)
    f4.show_colorscale(vmin=0.0,vmax=0.002)
    f4.add_beam()
    f4.add_colorbar()
    f4.show_contour(outputfits[3],colors='White',levels=[0.0003,0.0005,0.0007])
    f4.axis_labels.hide()
    f4.tick_labels.hide_y()
    f4.add_label(0.5, 0.90, 'with 3 point sources\n uvrange > 910k$\lambda$', relative=True,size=20,color='White')
    
    f5 = aplpy.FITSFigure(outputfits[4],figure=fig,subplot=(2,3,3))
    f5.recenter(290.916875,14.51819444444,radius=0.0002)
    f5.show_colorscale(vmin=0.0,vmax=0.05)
    f5.add_beam()
    f5.add_colorbar()
    f5.axis_labels.hide()
    f5.tick_labels.hide_y()
    f5.show_contour(outputfits[4],colors='White',levels=[0.007,0.01,0.02,0.03,0.04])
    f5.add_label(0.5, 0.9, 'with 3 compact sources\n full uvrange', relative=True,size=20,color='White')
    
    f6 = aplpy.FITSFigure(outputfits[5],figure=fig,subplot=(2,3,6))
    f6.recenter(290.916875,14.51819444444,radius=0.0002)
    f6.show_colorscale(vmin=0.0,vmax=0.002)
    f6.add_beam()
    f6.add_colorbar()
    f6.axis_labels.hide()
    f6.tick_labels.hide_y()
    f6.show_contour(outputfits[5],colors='White',levels=[0.0009,0.0015,0.0021])
    f6.add_label(0.5, 0.9, 'with 3 compact sources\n uvrange > 910k$\lambda$', relative=True,size=20,color='White')
    
    fig.savefig('simulation.pdf')
subplot()

绘制结果如下:
aplpy画连续谱图

相关标签: 天文软件