生成的FITS文件WCS不生效,SAO DS9仅显示像素坐标求助
生成带WCS的FITS文件后,DS9鼠标悬停仅显示像素坐标而非天球坐标
我生成了包含随机恒星的FITS文件,文件头已添加WCS信息,但在SAO DS9中打开时,鼠标悬停只显示x、y像素坐标,看不到对应的天球坐标。以下是生成文件的Python脚本和DS9中的显示现象:
import numpy as np from pathlib import Path import matplotlib.pyplot as plt import pandas as pd import os from astropy.wcs import WCS from astropy import units as u from astropy.io import fits from astropy.visualization import AsinhStretch, LogStretch from astropy.visualization.mpl_normalize import ImageNormalize from convenience_functions import show_image from photutils.datasets import make_gaussian_sources_image, make_random_gaussians_table from astropy.nddata import Cutout2D def add_wcs(cntrpix,cntrval,cdelt,ctype, radesys): #zhdrbb.remove('ZODIL') #CREATE WCS KEYWORDS w = WCS(naxis=2) w.wcs.crpix = cntrpix w.wcs.crval = cntrval w.wcs.cdelt = cdelt w.wcs.ctype = ctype w.wcs.radesys = radesys w.wcs.equinox = 2000 w.wcs.set_pv([(2, 1, 45.0)]) w.wcs.cunit = [u.deg, u.deg] header = w.to_header() return header def stars(image, number, mag_min, mag_max): """ Add some stars to the image. """ # Most of the code below is a direct copy/paste from # https://photutils.readthedocs.io/en/stable/_modules/photutils/datasets/make.html#make_100gaussians_image flux_min = 10**mag_min flux_max = 10**mag_max flux_range = [flux_min, flux_max] y_max, x_max = image.shape xmean_range = [0.01 * x_max, 0.99 * x_max] ymean_range = [0.01 * y_max, 0.99 * y_max] xstddev_range = [4, 4] ystddev_range = [4, 4] params = dict([('flux', flux_range), ('x_mean', xmean_range), ('y_mean', ymean_range), ('x_stddev', xstddev_range), ('y_stddev', ystddev_range), ('theta', [0, 2*np.pi])]) sources = make_random_gaussians_table(number, params, seed=int(random.random()*1e4)) star_im = make_gaussian_sources_image(image.shape, sources) return sources, star_im RA_0, RA_F = 30, 70 DEC_0, DEC_F = -20,20 MAG_0, MAG_F = 3, 8 N_STARS = 250 D_RA = np.abs(RA_0 - RA_F) D_DEC = np.abs(DEC_0 - DEC_F) GRID_LENGTH = int(2e3) # N PIXELS PER SIDE # WCS STUFF CRPIX = (int(GRID_LENGTH / 2), int(GRID_LENGTH / 2)) CRVAL = (RA_0 + D_RA / 2., DEC_0 + D_DEC / 2.) cdelt_x = D_RA / GRID_LENGTH cdelt_y = D_DEC / GRID_LENGTH CDELT = (cdelt_x, cdelt_y) CTYPE = ["RA-TAN", "DEC-TAN"] RADESYS = 'ICRS' base_pix_full_image = np.zeros((GRID_LENGTH,GRID_LENGTH)) star_data, star_cat_img = stars(base_pix_full_image, N_STARS, MAG_0, MAG_F) # WCS wcs_header = add_wcs(CRPIX, CRVAL, CDELT, CTYPE, RADESYS) hdr = fits.Header() hdu_cat = fits.PrimaryHDU(header=wcs_header,data=star_cat_img) # WRITE FITS PIXEL FILE hdu_cat.writeto(fits_pixel_file, overwrite=True)
截图显示DS9窗口底部状态栏仅展示
x: xxx y: xxx的像素坐标,无RA/DEC等天球坐标信息。
排查建议
检查WCS关键字完整性
用fits.getheader(fits_pixel_file)导出文件头,确认核心WCS关键字存在且格式正确:CRPIX1/CRPIX2:需匹配图像中心像素值CRVAL1/CRVAL2:对应设置的天球中心坐标CDELT1/CDELT2:每像素对应的角度值(单位应为度)CTYPE1/CTYPE2:必须为RA-TAN和DEC-TAN
注意:Astropy生成的CUNIT1/CUNIT2关键字若设置非度单位,可能导致DS9解析失败,可尝试删除该关键字后重新保存文件。
移除不必要的PV扭曲参数
代码中w.wcs.set_pv([(2, 1, 45.0)])添加了DEC轴的扭曲项,对于无畸变的模拟图像,该参数可能干扰DS9的WCS解析。注释掉这一行,重新生成文件测试。调整DS9坐标显示设置
在DS9菜单栏选择Frame -> Coordinate System,确认已切换至ICRS(与代码中RADESYS='ICRS'匹配),而非Image模式。也可通过快捷键Ctrl+W快速切换坐标显示类型。修复代码依赖问题
stars函数中使用了random.random()但未导入random模块,运行时会抛出异常,导致生成的FITS文件可能不完整。需在代码开头添加import random。验证WCS有效性
用Astropy自带工具验证WCS转换是否正常:from astropy.io import fits from astropy.wcs import WCS hdul = fits.open(fits_pixel_file) wcs = WCS(hdul[0].header) print(wcs) # 测试中心像素转天球坐标 pix_center = (GRID_LENGTH/2, GRID_LENGTH/2) sky_center = wcs.pixel_to_world(*pix_center) print(f"中心像素对应天球坐标:{sky_center}")若转换结果正确,说明WCS本身无问题,需重点检查DS9的设置。
内容的提问来源于stack exchange,提问作者anakha272
相关产品推荐
相关产品推荐

