Fits文件

来自Shiyin's note
跳到导航 跳到搜索

python中的读写

详见 Astropy.io.fits

astro_lib中的fits文件读写

mrdfits 和 mwrfits

mrdfits都入fits文件,存为一个结构数组,结构的tag从header得到。比较简易方便。

a = mrdfits('TEST.FITS', 0, header)

mwrfits将一个结构数组写成fits文件,header根据结构的tag名自动生成.

mwrfits,a,'TEST2.fit',/create
  • 默认的写入在HDU1,不是PRIMARY HDU
  • 默认的数组写入的是BINARY格式。可以用/silent变成ASCII格式。
  • 如果已有fits文件,那么不用/create,则将生成一个HDU附加在原来文件之后。

readfits 和 writefits

data = READFITS( Filename, Header)
WRITEFITS, filename, data , header,

这一组命令将fits文件分成data(数组)和header(字符串数组)处理,相对没有mrdfits 和 mwrfits方便,但是有较多的配套命令可修改fits文件。

头文件相关命令

  • mkhdr,hdr,fltarr(512,512),/ima,/extend ;生成一个512x512图像的最小的头文件
  • sxaddpar,hdr,'CRVAL1',256,'center pixel value' ;增加一个头文件描述

astro_lib中的fits文件的修改

ASCII 表格的修改

修改之前,需要readfits都入data和header

  • FTADDCOL: Add a new column to a FITS ASCII table
  • FTPUT: Update or add data to a field in a FITS ASCII table array
  • FTAB_EXT Extract specified columns of a FITS table extension into IDL vectors

FTDELCOL Delete specified column from a FITS ASCII table data array

BINARY 表格的修改

BINARY表格的修改(如删除一列)相对较为复杂。但是可以较为方便的构造

例:Write a binary table 'sample.fits' giving 43 X,Y positions and a 21 x 21 PSF at each position:

(1) First, create sample values

    x = findgen(43) & y = findgen(43)+1 & psf = randomn(seed,21,21,43)

(2) Create primary header, write it to disk, and make extension header

     fxhmake,header,/initialize,/extend,/date
     fxwrite,'sample.fits',header
     fxbhmake,header,43,'TESTEXT','Test binary table extension'

(3) Fill extension header with desired column names

     fxbaddcol,1,header,x[0],'X'             ;Use first element in each array
     fxbaddcol,2,header,y[0],'Y'             ;to determine column properties
     fxbaddcol,3,header,psf[*,*,0],'PSF'

(4) Write extension header to FITS file

     fxbcreate,unit,'sample.fits',header

(5) Use FXBWRITM to write all data to the extension in a single call

     fxbwritm,unit,['X','Y','PSF'], x, y, psf
     fxbfinish,unit                 ;Close the file

其他

fitsdir

列出某个目录下的所有fits文件(利用了file_search)的头文件的基本信息。可以用Kewords关键字指定输出你感兴趣的头信息。这些信息可以直接输出(textout=filename)到一个文件。