“Astropy.io.fits”的版本间差异

来自Shiyin's note
跳到导航 跳到搜索
(创建页面,内容为“ >>> hdulist = fits.open('input.fits') >>> hdulist.info() ==header== *each element of an HDUList is an HDU object with .header and .data attributes, which can be…”)
 
无编辑摘要
 
(未显示同一用户的2个中间版本)
第5行: 第5行:


==header==
==header==
>>> hdr=hdulist[0].header
*each element of an HDUList is an HDU object with .header and .data attributes, which can be used to access the header and data portions of the HDU.
>> list(hdr.keys())
>>> hdulist[0].header['targname']
>>> hdr['targname']
>>> header[:2] #只看前面两个
>>> hdr.set('observer', 'Edwin Hubble') #添加header



*To see the entire header as it appears in the FITS file (with the END card and padding stripped), simply enter the header object by itself, or print(repr(header)):
* FITS标准使用的特定结构关键字集随HDU类型的不同而不同,还有许多其他保留的关键字,例如用于数据缩放的关键字,或用于表的列属性的关键字,如FITS标准中所述。其中大多数都可以通过Column或HDU对象的属性访问,例如用HDU .name设置EXTVER的 EXTNAME或HDU.ver。结构关键字将作为通用操作的结果进行检查和/或更新。例如,关键字NAXIS*从数据形状(.data.shape)中设置,BITPIX从数据类型(.data.dtype)中设置,调用HDU的验证方法(PrimaryHDU.verify()),一些关键字可以自动修复。
*It’s also possible to view a slice of the header:

>>> header[:2]
===comment/hitory===
*Another way to either update an existing card or append a new one is to use the Header.set() method:
>>> hdr['history'] = 'I updated this file 2/26/09'
>>> prihdr.set('observer', 'Edwin Hubble')
>>> hdr['comment'] = 'Edwin Hubble really knew his stuff'
>>> hdr['comment'] = 'I like using HST observations'
>>> hdr['history']
I updated this file 2/26/09
>>> hdr['comment']
Edwin Hubble really knew his stuff
I like using HST observations
>>> hdr['comment'][1] = 'I like using JWST observations'
>>> hdr['comment']
Edwin Hubble really knew his stuff
I like using JWST observations


==fits table 的读与写==
==fits table 的读与写==
*写
from astropy.table import Table
t=Table([Bage,fzbin,Age_weight2],names=('Age','z','weights'))
t.write('SFH_LMC3_out.fits',format='fits')
*读
*读
hdu = fits.open('SFH_LMC3_out.fits')
hdu = fits.open('SFH_LMC3_out.fits')
hdu.info() #看看哪些hdu
hdu.info() #看看哪些hdu
data=hdu[1].data
data=hdu[1].data
cols=hdu[1].columns
data.columns #看看有那些列
data.columns #看看有那些列
data.field(0) #读取第一列,也可以拿到列的名字,然后data['tag']读取
Bage=data. Field('Age')
data[0] #第一行
* 修改columns中某个keyword的值
columns = fits.ColDefs(hdul[1].columns)
clumns1.change_attrib(col_name='DATA',attrib='format',new_value='8388608B')
*写
from astropy.table import Table
t=Table([Bage,fzbin,Age_weight2],names=('Age','z','weights'))
t.write('SFH_LMC3_out.fits',format='fits')
*更新
:可以使用**HDUList.flush()方法将自open()**以来所做的所有更改写回原始文件。**close()**方法将对以更新模式打开的FITS文件执行相同的操作:
with fits.open('original.fits', mode='update') as hdul:
# Change something in hdul.
hdul.flush()

==fits image读写==
n = np.arange(100.0)
hdu = fits.PrimaryHDU(n)
hdu.writeto('new2.fits')
hdu.writeto('new2.fits')
*如果超过一个hdu
>>> hdul = fits.HDUList([hdu])
>>> hdul.writeto('new1.fits')



==fits文件打开过多错误==
==fits文件打开过多错误==
第41行: 第75行:
:*del hdu.data
:*del hdu.data
:*hdu = fits.open(fits_name,memmap=False) #打开fits文件数目比较多的情况下
:*hdu = fits.open(fits_name,memmap=False) #打开fits文件数目比较多的情况下

==fits image读写==
n = np.arange(100.0)
hdu = fits.PrimaryHDU(n)
hdu.writeto('new2.fits')
hdu.writeto('new2.fits')
*如果超过一个hdu
>>> hdul = fits.HDUList([hdu])
>>> hdul.writeto('new1.fits')

2023年7月9日 (日) 01:47的最新版本


>>> hdulist = fits.open('input.fits')
>>> hdulist.info()

header

>>> hdr=hdulist[0].header
>> list(hdr.keys())
>>> hdr['targname']
>>> header[:2] #只看前面两个
>>> hdr.set('observer', 'Edwin Hubble') #添加header


  • FITS标准使用的特定结构关键字集随HDU类型的不同而不同,还有许多其他保留的关键字,例如用于数据缩放的关键字,或用于表的列属性的关键字,如FITS标准中所述。其中大多数都可以通过Column或HDU对象的属性访问,例如用HDU .name设置EXTVER的 EXTNAME或HDU.ver。结构关键字将作为通用操作的结果进行检查和/或更新。例如,关键字NAXIS*从数据形状(.data.shape)中设置,BITPIX从数据类型(.data.dtype)中设置,调用HDU的验证方法(PrimaryHDU.verify()),一些关键字可以自动修复。

comment/hitory

>>> hdr['history'] = 'I updated this file 2/26/09'
>>> hdr['comment'] = 'Edwin Hubble really knew his stuff'
>>> hdr['comment'] = 'I like using HST observations'
>>> hdr['history']
I updated this file 2/26/09
>>> hdr['comment']
Edwin Hubble really knew his stuff
I like using HST observations
>>> hdr['comment'][1] = 'I like using JWST observations'
>>> hdr['comment']
Edwin Hubble really knew his stuff
I like using JWST observations

fits table 的读与写

hdu = fits.open('SFH_LMC3_out.fits')
hdu.info() #看看哪些hdu
data=hdu[1].data
cols=hdu[1].columns
data.columns #看看有那些列
data.field(0) #读取第一列,也可以拿到列的名字,然后data['tag']读取
data[0] #第一行
  • 修改columns中某个keyword的值
columns = fits.ColDefs(hdul[1].columns)
clumns1.change_attrib(col_name='DATA',attrib='format',new_value='8388608B')
from astropy.table import Table
t=Table([Bage,fzbin,Age_weight2],names=('Age','z','weights'))
t.write('SFH_LMC3_out.fits',format='fits')
  • 更新
可以使用**HDUList.flush()方法将自open()**以来所做的所有更改写回原始文件。**close()**方法将对以更新模式打开的FITS文件执行相同的操作:
with fits.open('original.fits', mode='update') as hdul:
   # Change something in hdul.
   hdul.flush()

fits image读写

n = np.arange(100.0) 
hdu = fits.PrimaryHDU(n)
hdu.writeto('new2.fits')
hdu.writeto('new2.fits')
  • 如果超过一个hdu
>>> hdul = fits.HDUList([hdu])
>>> hdul.writeto('new1.fits')


fits文件打开过多错误

  • [1]
  • fits.close()
  • 可能还根系统设置有关
ulimit -a
ulimit -n 100000
vim /etc/security/limits.conf
  • 把python的软限制编程硬限制,参见 [2],下面方法貌似也不管用
import resource
soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE)
resource.setrlimit(resource.RLIMIT_NOFILE, (hard, hard))
  • lsof -p 进程id > openfiles.log命令,获得当前占用句柄的全部详情进行分析
  • 实测这两个效果不好(python 3.8)
  • del hdu.data
  • hdu = fits.open(fits_name,memmap=False) #打开fits文件数目比较多的情况下