如何从头开始生成拟合文件 [英] How to generate a fits file from the beginning

查看:79
本文介绍了如何从头开始生成拟合文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这篇文章中,他们解释了如何从ascii文件生成fits文件.但是,我也想知道如何将头文件和数据定义为fits文件.(将ASCII表转换为FITS图像)

In this post, they explain how to generate a fits file from ascii file. However, I also would like to know how to define header and data into fits file. (Converting ASCII Table to FITS image)

例如,当我用astropy调用光谱拟合文件(从望远镜下载)时,可以分别调用数据和标头.

For example, when I call a spectral fits file with astropy (which is downloaded from a telescope), I can call data and header separately.

IE

  In [1]:hdu = fits.open('observation.fits', memmap=True)

  In [2]:header = hdu[0].header

  In [3]:header 
   Out [3]: 
   SIMPLE  =                    T / conforms to FITS standard                      
   BITPIX  =                     8                                                 
   NAXIS   =                     1
   NAXIS1  =                     47356                      
   EXTEND  =                    T                                                  
   DATE    = 'date' / file creation date (YYYY-MM-DDThh:mm:ss UT)   
   ORIGIN  = 'XXX     '           / European Southern Observatory                  
   TELESCOP= 'XXX'         / ESO Telescope Name                             
   INSTRUME= 'Instrument'           / Instrument used.                               
   OBJECT  = 'ABC '           / Original target.                               
   RA      =           30.4993 / xx:xx:xx.x RA (J2000) pointing                 
   DEC     =            -20.0009 / xx:xx:xx.x DEC (J2000) pointing               

   CTYPE1  = 'WAVE    '           / wavelength axis in nm                          
   CRPIX1  =                   0. / Reference pixel in z                           
   CRVAL1  =     298.903594970703 / central wavelength                             
   CDELT1  =   0.0199999995529652 / nm per pixel                                   
   CUNIT1  = 'nm      '           / spectral unit                                  

  ..
   bla bla
  ..                                                                               
   END                                                                             




   In [3]:data = hdu[0].data
   In [4]:data 
   Out [4]:array([  1000,   1001,   1002, ...,
     5.18091546e-13,   4.99434453e-13,   4.91280864e-13])


   Lets assume, I have data like below

   WAVE FLUX
   1000 2.02e-12
   1001 3.03e-12
   1002 4.04e-12
  ..
   bla bla
  ..

因此,我想用自己的数据(带有自己的标头)生成光谱拟合文件.

So, I'd like to generate a spectral fits file with my own data (with its own header).

最小问题:现在让我们假设,我正确地生成了光谱拟合文件,但是我意识到我忘了在X轴上取WAVE值的对数(1000、1001、1002,...).如何在不触摸Y轴的FLUX值(2.02e-12、3.03e-13、4.04e-13)的情况下做到这一点?

Mini question : Now lets assume, I generate spectral fits file correctly, but I realised that I forgot to take logarithm of WAVE values in X axis (1000, 1001, 1002, ....) . How can I do that without touching FLUX values of Y-axis (2.02e-12, 3.03e-13, 4.04e-13) ?

推荐答案

FITS文件被组织为一个或多个HDU(标头数据单元),顾名思义,其由一个数据对象(通常是一个对象的单个数组)组成.观察,尽管有时其他类似表),以及与该数据一起出现的元数据的标头.

FITS files are organized as one or more HDUs (Header Data Units) consisting, as the name suggests, as one data object (generally, a single array for an observation, though sometimes something else like a table), and the header of metadata that goes with that data.

要从头开始创建文件(尤其是图像),最简单的方法是直接创建 ImageHDU 对象:

To create a file from scratch, especially an image, the simplest way is to directly create an ImageHDU object:

>>> from astropy.io import fits
>>> hdu = fits.ImageHDU()

就像从现有文件中读取HDU一样,此HDU具有一个(通常为空)标题,以及一个空数据属性,您可以将其分配给:

Just as with an HDU read from an existing file, this HDU has a (mostly empty) header, and an empty data attribute that you can then assign to:

>>> hdu.data = np.array(<some numpy array>)
>>> hdu.header['TELESCOP'] = 'Gemini'

满意时,可以使用以下命令将HDU写出到文件中:

When you're satisfied you can write the HDU out to a file with:

>>> hdu.writeto('filename.fits')

(注意:您将看到的许多文档都演示了一个更复杂的过程,该过程创建一个 HDUList 对象,将HDU附加到HDU列表,然后编写完整的HDU列表.仅在创建多扩展FITS文件时才需要.对于单个HDU,可以直接使用 hdu.writeto ,并且该框架将处理其他结构细节.)

(Note: A lot of the documentation you'll see demonstrates a more complex process of creating an HDUList object, appending the HDU to the HDU list, and then writing the full HDU list. This is only necessary if you're creating a multi-extension FITS file. For a single HDU, you can use hdu.writeto directly and the framework will handle the other structural details.)

通常,您不需要操纵描述数据本身格式的标头,因为标头是自动的,不应用手触摸(FITS具有将有关数据结构的信息与实际元数据混合在一起的缺点).您可以在此处查看有关如何处理FITS数据的更多示例: http://docs.astropy.org/en/stable/generation/examples/index.html#astropy-io

In general you don't need to manipulate the headers that describe the format of the data itself--that is automatic and should not be touched by hand (FITS has the unfortunate misfeature of mixing information about data structure with actual metadata). You can see more examples on how to manipulate FITS data here: http://docs.astropy.org/en/stable/generated/examples/index.html#astropy-io

您的其他问题与操纵图像的WCS(世界坐标系)有关,尤其是对于光谱数据而言,这可能并非易事.我会问一个单独的问题,其中包含有关您希望完成的工作的更多详细信息.

Your other question pertains to manipulating the WCS (World Coordinate System) of the image, and in particular for spectral data this can be non-trivial. I would ask a separate question about that with more details about what you hope to accomplish.

这篇关于如何从头开始生成拟合文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆