[PYTHON] astropy: fits file

How to open / save / add fits files with astropy (astronomical Python package).

Data input/output with astropy


#from astrolibpy.utils.idlsave import idlsave 

from astropy import table
from astropy.table import Table 
from astropy.table import Column
from astropy.io import fits

#####################
# method (1)
#####################

# read
table1 = Table.read('table1_old.fits')

# save 
data_  = [ table1['source_id'], table1['radial_velocity'] ]
names_ = ('source_id', 'radial_velocity')

dtype_ = ( 'i8', 'f8' )

table2 = Table(data_, 
               names=names_, 
               #meta={'name': 'first table'},
               dtype=dtype_)

table2.write('table2_new.fits', overwrite=True)

#####################
# method (2)
#####################

# read
hdu_list = fits.open('table1_old.fits', memmap=True)
data_input = hdu_list[1].data

# save
table2 = Table(data_input)
table2.write('table2_new.fits', overwrite=True)


dtype : ['f8', 'i4', 'S2' ] etc.


Add line to fits file


table2 = Table(data_input)
table2.write('table2_new.fits', overwrite=True)


col_G0 = Column(name='phot_g_mean_mag_0', data=G0)
col_bp0= Column(name='phot_bp_mean_mag_0',data=bp0)
col_rp0= Column(name='phot_rp_mean_mag_0',data=rp0)


table_2.add_columns([col_G0,
                     col_bp0,
                     col_rp0])

table_2.write('table_2_combined.fits', overwrite=True)


Recommended Posts

astropy: fits file
File matching
File creation
Read file
File operations