方正42
Universe, Life and Everything
Toggle navigation
方正42
主页
Technical Details
天马行空
读书笔记
Arxiv读读读
About Me
归档
标签
【Tips】How to change column format of FITS file?
无
2023-11-27 14:33:50
48
0
0
fangzheng42
# Problem and Demand - In a standard SPECTRA file of FITS format, there is a column of source `COUNTS` in extension. - Default data type of `COUNTS` is `1I`, meaning *interger*. - **manually** change the data type of `COUNTS` to *float*. # Try - directly assign a float array - **failed** - the array will be reformatted to *interger* - change keyword `TFORM2` in header - **failed** - *TFORM2* cannot be manually changed # Solution - with `astropy`, using `fits.Columns` - `filename_raw` is the raw spectra fits file - `counts_new` is the new counts array with *float* type ```python from astropy.io import fits from astrioy.table import Table hdu_raw = fits.open(filename_raw) hd_pri = hdu_raw[0].header # extract primary header hd_spec = hdu_raw[1].header # extract spectra header da_spec = hdu_raw[1].data # extract data columns col_chan = fits.Column(name='CHANNEL',format='1I',array=da_spec['CHANNEL']) # unchanged col_cts_new = fits.Column(name='COUNTS',format='1D',unit='count',array=test_counts) # '1D' for double precision, instead of originial '1I' for interger col_bin_lo = fits.Column(name='BIN_LO',format='1D',unit='angstrom',array=da_spec['BIN_LO']) col_bin_hi = fits.Column(name='BIN_HI',format='1D',unit='angstrom',array=da_spec['BIN_HI']) #generate new extension new_hdu0 = fits.PrimaryHDU(header = hd_pri) new_hdu1 = fits.BinTableHDU.from_columns(columns=[col_chan,col_cts_new,col_bin_lo,col_bin_hi], header = hd_spec) new_hdu_tot = fits.HDUList([new_hdu0, new_hdu1]) new_hdu_tot.writeto(filename_new, overwrite = True) ``` # Notes - Then you will get a fits file with `COUNTS` in type of *float*, and can also be accepted by *Xspec*
上一篇: 无
下一篇:
2022-W01
0
赞
48 人读过
新浪微博
微信
腾讯微博
QQ空间
人人网
提交评论
立即登录
, 发表评论.
没有帐号?
立即注册
0
条评论
More...
文档导航
没有帐号? 立即注册