#!/usr/bin/python # vim: tabstop=3 shiftwidth=3 softtabstop=3 expandtab nonumber textwidth=0 wrapmargin=0 wrap """ fits2txt.py - reads in FITS data for the IRTF Extended Spectral Library and convert the FITS BINARY TABLE data to text. Files are provide via the command line. Example: fits2txt.py HD013783.fits # converts 1 file. fits2txt.py *.fits # converts all the FITS files. """ from __future__ import print_function # standard python import sys import os import getopt from astropy.io import fits #------------------------------------------------------------------ # usage() #------------------------------------------------------------------ def usage(): print (__doc__) #------------------------------------------------------------------ def fits2text( fname_fits ): fname_txt = os.path.splitext(fname_fits)[0]+'.txt' # data is in FIT BINARY TABLES hdul = fits.open( fname_fits ) data = hdul[1].data # gets the data, should be 3 colunms cols = hdul[1].columns # gets the column information print("converting %s to %s... %d x %d" %( fname_fits, fname_txt, len(cols.names), len(data) ) ); f = open( fname_txt, "w+" ) # include the filename as a comment f.write( "#\n") f.write( "# %s\n"%(fname_txt)) f.write( "#\n") # prints the header... adding '# ' to show these are comments in the text file. f.write( "# ") # 1st comment f.write( hdul[1].header.tostring(sep='\n# ', padding=False) ) f.write( "#\n") # print the column names f.write( "#%14s %15s %15s \n"%( cols.names[0], cols.names[1], cols.names[2]) ) # print the data for d in data: f.write( "%15.12F %15.12e %15.12e \n"%(d[0], d[1], d[2])); f.close() #------------------------------------------------------------------ # main() - #------------------------------------------------------------------ def main( argv ): # # parse command line args # debug = False try: opts, args = getopt.getopt(argv, "vh", ["help" ]) except getopt.GetoptError: usage() sys.exit(-1) try: for opt, arg in opts: if opt == "-h": sys.exit(0) elif opt == "-v": debug = True elif opt == '--help': sys.exit(0) # --help automatically prints doc string except: usage() sys.exit(-1) if debug: print( " debug=", debug) # assume the remain arguments are filesname filelist = args for f in filelist: fits2text( f ) return 0 # now run main sys.exit( main( sys.argv[1:] )) ##### EOF