-
Notifications
You must be signed in to change notification settings - Fork 0
/
listjoin_FITS_produce_txt_table.py
executable file
·182 lines (106 loc) · 5.34 KB
/
listjoin_FITS_produce_txt_table.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# I created this script in order to append several FITS tables. Right down here there are a couple of examples of whatever can be done+
# with FITS tables. In the end, I also print the table to a text file (which could even have a LaTex format!!!)
# coding: utf-8
import numpy as np
import scipy as scipy
import scipy.stats as stats
import astropy.stats as astats
import numpy.random as random
from astropy.table import Table
from astropy.io import ascii
import astropy.io.fits as fits
from astropy.table import Table
import pyfits
import os
from os import walk
import glob
from tabulate import tabulate
import atpy
import operator
def sort_table(table, cols):
# sort a table by multiple columns
# table: a list of lists (or tuple of tuples) where each inner list represents a row
# cols: a list (or tuple) specifying the column numbers to sort by
# e.g. (1,0) would sort by column 1, then by column 0
for col in reversed(cols):
table = sorted(table, key=operator.itemgetter(col))
return table
# SELECT RECORDS FROM FITS TABLE
# In the next example, assuming the table’s second field having the name ‘magnitude’,
# an output table containing all the records of magnitude > 5 from the input table is generated:
# t = pyfits.open('table.fits')
# tbdata = t[1].data
# mask = tbdata.['magnitude'] > 5
# newtbdata = tbdata[mask]
# hdu = pyfits.BinTableHDU(data=newtbdata)
# hdu.writeto('newtable.fits',clobber=True) # clobber=True allows to overwrite the existing FITS table
#t1 = pyfits.open('table1.fits')
#t2 = pyfits.open('table2.fits')
# MERGE FITS TABLES
# columns = t1[1].columns+t2[1].columns
# hdu = pyfits.BinTableHDU.from_columns(columns)
# hdu.writeto('newtable.fits',clobber=True) # clobber=True allows to overwrite the existing FITS table
# APPEND FITS TABLES
# This cannot be done with cat *.fits > newtable.fits in the terminal!!!!!!
# ls *.fits > list.lst would create a file with all the names
#nrows1 = t1[1].data.shape[0]
#nrows2 = t2[1].data.shape[0]
#nrows = nrows1 + nrows2
#hdu = pyfits.BinTableHDU.from_columns(t1[1].columns, nrows=nrows)
#for colname in t1[1].columns.names:
# hdu.data[colname][nrows1:] = t2[1].data[colname]
#hdu.writeto('newtable.fits',clobber=True) # clobber=True allows to overwrite the existing FITS table
# COMBINE ALL THE FITS FILES PRESENT IN THE CURRENT DIRECTORY
path=os.getcwd()
title="newtable"
# Find all the .fits files in the current directory
filelist=[]
for file in os.listdir(path):
if file.endswith(".fits"):
if file!=title+".fits": # Allows to overwrite the final table and not to append it to the other ones
filelist.append(file)
filelist=sorted(filelist) # Sorts the files by name, given that Python reads them in a random way
# Open the tables
tables=[]
rows=[]
#columns=[]
for i in range(len(filelist)):
tables.append(i)
rows.append(i)
#columns.append(i)
tables[i] = pyfits.open(filelist[i])
rows[i] = tables[i][1].data.shape[0]
#columns[i]=tables[i][1].columns
nrows=sum(rows)
hdu = pyfits.BinTableHDU.from_columns(tables[0][1].columns, nrows=nrows)
for colname in tables[0][1].columns.names:
for i in range(len(tables)):
hdu.data[colname][i] = tables[i][1].data[0][colname]
hdu.writeto(title+".fits",clobber=True) # clobber=True allows to overwrite the existing FITS table
# Print the final table to a .txt file and open it to write the header belonging to the previous tables and then overwrite it as a FITS again
tablefinal=pyfits.open(title+".fits")
tablefortext=Table.read(title+".fits",format='fits')
tableh=fits.open(filelist[0]) # In order to get the header for the final FITS table
prihdrr = tableh[0].header # I wrote the units as a comment when I created the individual tables. Header from one of them!
tbhdu = pyfits.BinTableHDU.from_columns(tablefinal[1].columns, nrows=nrows)
prihdr = pyfits.Header()
prihdr['COMMENT']=str(prihdrr['COMMENT'][0])+str(prihdrr['COMMENT'][1]) # It was divided into two. Weird. I am joining both elements
prihdu = pyfits.PrimaryHDU(header=prihdr)
tbhdulist = pyfits.HDUList([prihdu, tbhdu])
tbhdulist.writeto(title+".fits",clobber=True) #clobber=True allows to overwrite the existing FITS table
# prihdr['COMMENT'] was ivided into two after writing the table...
f=open(title+".txt","w")
print >>f, tabulate(tablefortext,headers=[str(i) for i in tables[0][1].columns.names])
# See the options of Pypi tabulate, the package which is being used. It allows to write a LaTex table, e.g. (tablefmt="latex")
print >>f,'\n'+'#'+str(prihdrr['COMMENT'][0])+str(prihdrr['COMMENT'][1]) # I include a comment with the units in the end of the table
f.close()
tabletext=Table.read(title+".txt",format='ascii')
tabletext_definitive=sort_table(tabletext,(0,1,2)) # Sort the table by column 0, then by column 1 and finally by column 2
g=open(title+".txt","w")
print >>g, tabulate(tabletext_definitive,headers=[str(i) for i in tables[0][1].columns.names,numalign="right"]) # Plain text
print >>g,'\n'+'#'+str(prihdrr['COMMENT'][0])+str(prihdrr['COMMENT'][1]) # I include a comment with the units in the end of the table
g.close()
h=open(title+"_latex.txt","w")
print >>h, tabulate(tabletext_definitive,headers=[str(i) for i in tables[0][1].columns.names],tablefmt="latex") # LaTex format
print >>h,'\n'+'#'+str(prihdrr['COMMENT'][0])+str(prihdrr['COMMENT'][1]) # I include a comment with the units in the end of the table
h.close()