0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

astropy: fits file

Last updated at Posted at 2020-05-15

astropy (天文系 Python package) でfitsファイルを open / save / add するやり方。

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)


Change column name

table3 = Table(data_input)
print(table3.columns)
# 
table3['E'].name = 'energy'

0
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?