-
Notifications
You must be signed in to change notification settings - Fork 0
/
DavesAstropyUtils.py
243 lines (218 loc) · 8.4 KB
/
DavesAstropyUtils.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#!/usr/bin/env python3
""" Various utilities for dealing with astropy
A set of unrelated utility functions related to astropy
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>.
"""
__author__ = "Dave Strickland"
__copyright__ = "Copyright 2018, Dave Strickland"
__date__ = "2018/02/23"
__deprecated__ = False
__email__ = "[email protected]"
__license__ = "GPLv3"
__version__ = "0.2.0"
def convert_greek_unicode_symbol(anInputStr):
"""Replaces unicode greek symbols with the ASCII textual name
of that symbol.
Also replaces some problematic unicode characters that are not
recognized by the Simbad ID service. Note that unicode characters
and their hex codes can be looked up on the web, e.g. at
https://www.fileformat.info/info/unicode/char/search.htm or
https://unicodelookup.com/, if you know what you're looking
for. To find what unicode character is
in some text use http://www.babelstone.co.uk/Unicode/whatisit.html
"""
# Greek alphabet mapping from https://gist.github.com/beniwohli/765262
greek_alphabet = {
u'\u0391': 'Alpha',
u'\u0392': 'Beta',
u'\u0393': 'Gamma',
u'\u0394': 'Delta',
u'\u0395': 'Epsilon',
u'\u0396': 'Zeta',
u'\u0397': 'Eta',
u'\u0398': 'Theta',
u'\u0399': 'Iota',
u'\u039A': 'Kappa',
u'\u039B': 'Lamda',
u'\u039C': 'Mu',
u'\u039D': 'Nu',
u'\u039E': 'Xi',
u'\u039F': 'Omicron',
u'\u03A0': 'Pi',
u'\u03A1': 'Rho',
u'\u03A3': 'Sigma',
u'\u03A4': 'Tau',
u'\u03A5': 'Upsilon',
u'\u03A6': 'Phi',
u'\u03A7': 'Chi',
u'\u03A8': 'Psi',
u'\u03A9': 'Omega',
u'\u03B1': 'alpha',
u'\u03B2': 'beta',
u'\u03B3': 'gamma',
u'\u03B4': 'delta',
u'\u03B5': 'epsilon',
u'\u03B6': 'zeta',
u'\u03B7': 'eta',
u'\u03B8': 'theta',
u'\u03B9': 'iota',
u'\u03BA': 'kappa',
u'\u03BB': 'lamda',
u'\u03BC': 'mu',
u'\u03BD': 'nu',
u'\u03BE': 'xi',
u'\u03BF': 'omicron',
u'\u03C0': 'pi',
u'\u03C1': 'rho',
u'\u03C3': 'sigma',
u'\u03C4': 'tau',
u'\u03C5': 'upsilon',
u'\u03C6': 'phi',
u'\u03C7': 'chi',
u'\u03C8': 'psi',
u'\u03C9': 'omega'}
# Unicode characters Simbad can't handle
tricky_unicode = {
u'\u2019': "'" # U+2019 single right quote
}
# iterate over input string character by character
outputList = []
for c in anInputStr:
if c in greek_alphabet:
out = greek_alphabet[c]
#print('found {} in alphabet, replace with {}'.format(c, out))
outputList.append(out)
elif c in tricky_unicode:
out = tricky_unicode[c]
outputList.append(out)
else:
outputList.append(c)
outputStr = ''.join(outputList).strip()
return outputStr
def read_table(input_file, p_verbose=False):
"""Attempts to read the file into an astropy Table object.
This function attempts to determine the file type based on the file
name, and then calls the correct reader function.
"""
import sys
import os.path
# Don't have to worry about race conditions for this type of work
if not os.path.isfile(input_file):
print('Error: Input file {} not found'.format(input_file))
sys.exit(3)
try:
if 'txt' in input_file or 'csv' in input_file:
p_data = read_ascii(input_file, p_verbose)
elif 'html' in input_file:
p_data = read_html(input_file, p_verbose)
elif 'fits' in input_file:
p_data = read_fits(input_file, p_verbose)
else:
print('Error: Unexpected file format for input file {}'.format(input_file))
sys.exit(1)
except:
print('Error: Failed to correctly read {}'.format(input_file))
sys.exit(2)
if p_verbose:
print('Read {} row tables from {}'.format(len(p_data), input_file))
return p_data
def read_ascii(input_txt_file, p_verbose=False):
"""Reads data from a comma separated txt/csv file, returning an astropy Tables object
The format of the input txt/csv file shoould be one object per line,
with the first non-commented line being the Table column heading.
Commas should be used as delimiters.
"""
from astropy.table import Table
if p_verbose:
print('Reading data table from {}'.format(input_txt_file))
p_data = Table.read(input_txt_file,
format='ascii.basic',
delimiter=',')
if p_verbose:
print(p_data.info)
return p_data
def read_html(input_html_table, p_verbose=False):
"""Reads data from a cleanly formatted HTML table, returning an astropy Tables object"""
from astropy.table import Table
if p_verbose:
print('Reading data table from {}'.format(input_html_table))
p_data = Table.read(input_html_table,
format='ascii.html')
if p_verbose:
print(p_data.info)
return p_data
def read_fits(input_html_table, p_verbose=False):
"""Reads data from a FITS or gzipped FITS file, returning an astropy Tables object"""
from astropy.table import Table
if p_verbose:
print('Reading data table from {}'.format(input_html_table))
p_data = Table.read(input_html_table,
format='fits')
if p_verbose:
print(p_data.info)
return p_data
def write_table(atable, an_output_file, a_css_style):
"""Writes the astropy Table to disk using a format determined
from the file name itself.
"""
if '.fits' in an_output_file:
write_to_fits(atable, an_output_file)
elif '.html' in an_output_file:
write_to_html(atable, an_output_file, a_css_style)
else:
print('Error: Unexpected file format for output table')
print(' File name: {}'.format(an_output_file))
print(' Expecting file name suffix to include either "fits" or "html"')
print(' Nothing will be written now...')
return
def write_to_fits(atable, an_output_file):
"""Write an astropy table to a fits file
"""
atable.write(an_output_file, format='fits', overwrite=True)
return
def write_to_html(atable, an_output_file, a_css_style=None):
"""Writes out an astropy Table to HTML while applying a CSS style to it."""
from astropy.table import Table
if a_css_style is None:
a_css_style = 'darkTable.css'
# This reads the css and applies it within the table
with open(a_css_style, 'r') as css_file:
p_css_str = css_file.read()
# Need to extract actual name of table style used in the CSS. Assuming
# its the first and only one.
first = p_css_str.split(None, 1)[0]
p_style = first.split('.')[1]
p_html_dict = {'css': p_css_str,
'table_class': p_style}
# For some reason the include_names and exclude_names options listed
# in the astropy documentation don't work.
atable.write(an_output_file,
format='ascii.html',
overwrite=True,
htmldict=p_html_dict)
print('Wrote formatted table to {} using CSS style {}'.format(an_output_file, p_style))
return
def read_star_aliases(star_alias_csv_file):
"""Creates a dictionary of problematic user star names and the
names that Simbad will recognize, used by SimbadStarQuery.
"""
import csv
star_alias_dict = {}
with open(star_alias_csv_file, 'r') as csvfile:
csvreader = csv.reader(csvfile, quotechar='"', delimiter=',', quoting=csv.QUOTE_ALL, skipinitialspace=True)
# skip first line as its a header
next(csvreader)
for row in csvreader:
if len(row) < 2:
print('Warning: row from {} contains less than two items. Skipping it.'.format(star_alias_csv_file))
else:
star_alias_dict[row[0]] = row[1]
return star_alias_dict