1. What are fits files?
Fits Flexible Image Transport System is a data format used for data transfer and exchange between observatories around the world. It describes the definition of data and the general method of encoding data. Fits file names. Fits.
2. Fits file format
An FITS file contains multiple HDU (Primary header and data unit), as shown in the following figure. Each row contains one HDU.
Each HDU consists of header and data.
- Header: stores the description of the whole data and the description of each column of data (for example, TTYPE indicates the column name; TFORM represents the data type in the column, with each letter representing a data type.
- Data: Stores data, which can be one-dimensional, two-dimensional, and so on. As shown in the figure below, the data in DATA is a two-dimensional data, which stores information such as emission line intensity.
3. Fits file reading
We use the FITS file manipulation tool provided by ‘Astropy’ to read fits files. To start, import the FITS file:
from astropy.io import fits
Copy the code
3.1 Fits file reading and basic information
The fits. Open (path) function reads the fits file to obtain a list object containing multiple HDU. We can obtain the specified HDU by subscripting.
hdu_list = fits.open("./fits_data/spec-0266-51602-0012.fits")
Copy the code
Be sure to use it after readinghdu_list.close()
Method to close the file stream, otherwise it consumes a lot of memory.
Using the hdu_list.info() method, we can view some basic information about the current FITS file:
""" def demo01(): Hdu_list = fits. Open ("./fits_data/spec-0266-51602-0012. Fits ") print("fits file info :") hdu_list.info() hdu_list.close()Copy the code
Output:
Fits basic information: Filename: ./fits_data/spec-0266-51602-0012.fits No. Name Ver Type Cards Dimensions Format 0 PRIMARY 1 PrimaryHDU 122 () 1 COADD 1 2 SPECOBJ 1 BinTableHDU 262 1R x 126C [6A, 4A, 16A, 23,... , 5E] 3 SPZLINE 1 BinTableHDU 48 29R x 19C [J, J, J, 13... J, E, E]Copy the code
-
The first line filename: indicates the name of the fits file being read
-
Line 2: The title of the data below
No.
: indicates the hDU numbername
: Indicates the current HDU nameDimensions
: indicates the data dimension of data in the current HDURow x columnformat
: Data type, I for integer, J for long integer, E for floating point, D for double precision floating point, and A for character
3.2 Data manipulation by fits files
We get an HDU object by subscript from the HDU list object. Using hdu.header, you can view the current HDU header file
"" def demo02(): hdu_list = fits.open("./fits_data/spec-0266-51602-0012.fits") hdu = hdu_list[1] hdu_header = hdu.header print(hdu_header) hdu_list.close()Copy the code
We can get the data part of the HDU through.data. The return value is the type that is unique to the FITS file.
But we get a data object of type numpy. Ndarray by column. In this case, we can use NUMpy and PANDAS for data processing.
Def demo03(): Hdu_list = fits. Open ("./fits_data/spec-0266-51602-0012. Fits ") hdu = hdu_list[1] data = hdu. ",type(data)) print(data) flux = hdu.data["flux"] # equivalent to hdu.data.field(' flux ') print("\n HUD data ") ",type(flux)) print(flux) hdu_list.close()Copy the code
Output:
The data type of the HUD data part is: < a class 'astropy. IO. Fits. Fitsrec. FITS_rec' > [(8.7938795, 3.5803, 0.05449915, 0, 0, 1.2542306, 6.6273575, 6.400915) (15.133193, 3.5804, 0.04994194, 0, 0, 1.2539674, 6.350275, 6.7443757)... (16.940786, 3.9639, 0.42537764, 16, 16, 0.7964833, 4.486557, 12.947592)] The data type of hud data is: <class 'numpy. Ndarray '> [8.7938795 15.133193 10.525017... 12.286306 16.704823 16.940786]Copy the code
In addition, we can view the column names of all data sections through hdu.data.names
Def demo04(): hdu_list = fits.open("./fits_data/spec-0266-51602-0012.fits") hdu = hdu_list[1] names = hdu.data.names print("hud Print (names) hdu_list.close() print(names) hdu_list.close()Copy the code
Output:
Hud data part of the column name: [' flux ', 'loglam', 'ivar', 'and_mask', 'or_mask', 'wdisp', 'sky' and 'model']Copy the code