-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimbadIdent.py
More file actions
381 lines (344 loc) · 14.7 KB
/
Copy pathSimbadIdent.py
File metadata and controls
381 lines (344 loc) · 14.7 KB
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#!/usr/bin/env python3
"""A simple web-scraper of stellar information using the Simbad service.
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/>.
"""
import os
import numpy as np
from astropy import units as u
from astropy.coordinates import SkyCoord
__author__ = "Dave Strickland"
__copyright__ = "Copyright 2018, Dave Strickland"
__date__ = "2018/02/23"
__deprecated__ = True
__email__ = "dave.strickland@gmail.com"
__license__ = "GPLv3"
__version__ = "0.1"
class SimbadIdent:
"""Parses data associated with a Simbad Ident query
Deprecated. Original developed before I noticed astroquery
could do this. This method may be less resource intensive than
using astroquery, and can work offline too (when the files have
already been downloaded).
"""
def __init__(self, user_ident, usecached=True):
self.user_ident = user_ident
self.rawfile = self.get_simbdad_id(self.user_ident, usecached)
self.data = []
self.identifiers = {}
self.read_data(self.rawfile)
self.parse_identifiers()
self.parse_coordinates()
self.parse_data()
def __str__(self):
# Limitation. This only works if the object is fully constructed.
#return 'SimbadIdent: user_ident={}, datafile={}, Data={}'.format(self.user_ident, self.rawfile, self.data)
return 'SimbadIdent: user_ident={}, datafile={}, coord={}, identifiers={}, pm={}, plx={}, spec_type={}, magB={}, magV={}'.format(self.user_ident,
self.rawfile,
self.coord,
self.identifiers,
self.pm,
self.plx,
self.spec_type,
self.magB,
self.magV)
def parse_coordinates(self):
"""Extracts ICRS coordinates, uncertainty (mas), and reference"""
# This is handled slightly differently to parse_data because we
# want to do some specialized processing on the coordinates using
# astropy's SkyCoord class.
self.coord = None
val_indices = slice(1,7)
err_indices = slice(10,13)
ref_indices = 13
for line in self.data:
if 'Coordinates(ICRS' in line:
tmp = line.split(' ')
tmp = list(filter(bool, tmp)) # remove empty strings
if (len(tmp) > 7):
coord_str = ' '.join(tmp[val_indices])
sc = SkyCoord(coord_str, unit=(u.hourangle, u.deg), frame='icrs')
##vals = sc.to_string('hmsdms', precision=1)
# returns a list, so just get first element
ra_deg = sc.ra.degree
dec_deg = sc.dec.degree
# sc.ra.to_string returns a list, so just get first element
ra_str = sc.ra.to_string(u.hour, sep=':', precision=1)
dec_str = sc.dec.to_string(u.deg, sep=':', precision=1)
else:
ra_deg = None
dec_deg = None
ra_str = None
dec_str = None
# error measurements
##if (len(tmp) > 12):
## errs = tmp[err_indices]
## if isinstance(errs, list):
## errs = ' '.join(errs).strip('[]') # get rid of simbad's []
## else:
## errs = errs.strip('[]')
## # Catch some common cases where there really arent uncertainties known
## if '~' in errs:
## errs = None
##else:
## errs = None
# reference bibcode
if (len(tmp) > 13):
refs = tmp[ref_indices]
if isinstance(refs, list):
refs = ' '.join(vals)
if '~' in refs:
refs = None
else:
refs = None
setattr(self, 'coord', {'ra_str': ra_str,
'dec_str': dec_str,
'ra_deg': ra_deg,
'dec_deg': dec_deg,
'ref': refs})
break
return
def parse_data(self):
"""Extracts data such as:
- B, V magnitudes,
- proper motion,
- parallax
- spectral type.
Data values, errors and reference ADS bibcode are all extracted
and stored in a dict.
"""
# this is a brute force method that is slightly inefficient as
# it repeatedly iterates over self.data, but it does get the job done with
# minimum lines of code.
self.data_name_dict = {'pm': 'Proper motions',
'plx': 'Parallax',
'spec_type': 'Spectral type',
'magB': 'Flux B',
'magV': 'Flux V'}
self.val_indices = {'pm': slice(2,4),
'plx': 1,
'spec_type': 2,
'magB': 3,
'magV': 3}
self.err_indices = {'pm': slice(4,6),
'plx': 2,
'spec_type': 3,
'magB': 4,
'magV': 4}
self.ref_indices = {'pm': 8,
'plx': 4,
'spec_type': 4,
'magB': 6,
'magV': 6}
self.float_conv = {'pm': False,
'plx': True,
'spec_type': False,
'magB': True,
'magV': True}
self.col_name_dict = {'pm': 'pm',
'plx': 'plx',
'spec_type': 'spec_type',
'magB': 'magB',
'magV': 'magV'}
self.measurement_units = {'pm': 'mas mas',
'plx': 'mas',
'spec_type': '',
'magB': 'mag',
'magV': 'mag'}
for data_name in self.data_name_dict.keys():
# set up default in case it doesn't exist
setattr(self, data_name, {'val': None, 'err': None, 'ref': None})
for line in self.data:
# data_name_dict(data_name) is the search string
search_string = self.data_name_dict[data_name]
if search_string in line:
tmp = line.split(' ')
tmp = list(filter(bool, tmp)) # remove empty strings
vals = tmp[self.val_indices[data_name]]
errs = tmp[self.err_indices[data_name]]
refs = tmp[self.ref_indices[data_name]]
# create a string if we have a list, skip otherwise
if isinstance(vals, list):
vals = ' '.join(vals)
if '~' in vals:
vals = None
if isinstance(errs, list):
errs = ' '.join(errs).strip('[]') # get rid of simbad's []
else:
errs = errs.strip('[]')
if '~' in errs:
errs = None
if isinstance(refs, list):
refs = ' '.join(vals)
if '~' in refs:
refs = None
# floating point conversion?
if self.float_conv[data_name]:
if vals is not None:
vals = float(vals)
else:
vals = float('nan')
if errs is not None:
errs = float(errs)
else:
errs = float('nan')
#print(data_name, {'val': vals, 'err': errs, 'ref': refs})
setattr(self, data_name, {'val': vals, 'err': errs, 'ref': refs})
break
return
def parse_identifiers(self):
"""Parse the primary (Simbad) object identifier as well as
secondary identifiers of interest to us such as WDS, SAO, HD,
and the common name.
"""
# Get Simbad's own primary identifier
obj_index = 5 # TODO do dynamically
tmp = self.data[obj_index].replace('Object ','').split('---')
simbad_ident = tmp[0].strip()
self.identifiers['MAIN_ID'] = simbad_ident
[l_start, l_end] = self.find_identifier_section()
for identifier in ['WDS', 'SAO', 'HIP', 'NAME', 'HD']:
identifier_found= False
for i in range(l_start, l_end+1):
line = self.data[i].strip()
if identifier in line:
tmp = line.split(' ')
tmp = list(filter(bool, tmp)) # remove empty strings
idx = tmp.index(identifier)
self.identifiers[identifier] = tmp[idx+1]
identifier_found = True
break # break out of line loop
if not identifier_found:
self.identifiers[identifier] = None
# parse secondary identifiers we're interested in.
return
def get_astropy_table_defn(self):
"""Returns column name, dtype and units lists for the data in this
object that can be used to define an astropy Table object to
store the data returned by get_astropy_table_row().
"""
# unfortunately we can't make this truly self describing.
##col_names = ['user_ident']
##col_dtypes = ['U16']
##col_units = ['']
for key in self.identifiers:
col_names.append(key)
col_dtypes.append('U20')
col_units.append('')
# for the following we have the value, error and reference to store.
col_names.append('RA_icrs')
col_names.append('DEC_icrs')
col_names.append('RA_icrs_deg')
col_names.append('DEC_icrs_deg')
col_names.append('RADEC_bibcode')
col_dtypes.append('U13') # ra
col_dtypes.append('U13') # dec
col_dtypes.append(np.float64) # ra
col_dtypes.append(np.float64) # dec
col_dtypes.append('U24') # ref
col_units.append('h:m:s')
col_units.append('d:m:s')
col_units.append('deg')
col_units.append('deg')
col_units.append('bibcode')
for data_name in self.data_name_dict.keys():
vname = self.col_name_dict[data_name]
ename = 'e_{}'.format(vname)
rname = 'ref_{}'.format(vname)
col_names.append(vname)
col_names.append(ename)
col_names.append(rname)
if self.float_conv[data_name]:
col_dtypes.append(np.double) # val
col_dtypes.append(np.double) # err
else:
col_dtypes.append('U16') # val
col_dtypes.append('U16') # err
col_dtypes.append('U24') # ref
col_units.append(self.measurement_units[data_name])
col_units.append(self.measurement_units[data_name])
col_units.append('bibcode')
return [col_names, col_dtypes, col_units]
def get_astropy_table_row(self):
"""Returns the data stored in this object as a list in the
same format as given by get_astropy_table_defn. This list
can be used to add a row to an astropy Table object using its
add_row() method.
"""
p_list = [self.user_ident]
for key in self.identifiers:
p_list.append(self.identifiers[key])
p_list.append(self.coord['ra_str'])
p_list.append(self.coord['dec_str'])
p_list.append(self.coord['ra_deg'])
p_list.append(self.coord['dec_deg'])
p_list.append(self.coord['ref'])
for data_name in self.data_name_dict.keys():
data_dict = getattr(self, data_name)
p_list.append(data_dict['val'])
p_list.append(data_dict['err'])
p_list.append(data_dict['ref'])
return p_list
def get_simbdad_id(self, aSimbadIdentifier, usecached=True):
"""Query Simbad by Identifier, storing the output as a file in
subdirectory Simbad.
If usecached is True, then don't perform the internet query if we
have the file on disk already.
"""
import os
import urllib
import pathlib
import time
# store files in subdirectory of current dir
odir = 'Simbad'
if not os.path.isdir(odir):
pathlib.Path(odir).mkdir(parents=True, exist_ok=True)
print('Created {} to store Simbad data in'.format(odir))
ofile = '{}/{}.simbad_id'.format(odir, aSimbadIdentifier.replace(' ', '_'))
ofile_exists = os.path.isfile(ofile)
if ofile_exists and usecached:
print(' For "{}" using cached file {}'.format(aSimbadIdentifier, ofile))
else:
print(' Querying Simbad for "{}"'.format(aSimbadIdentifier))
time.sleep(1.5) # to avoid hammering Simbad with many queries.
# Have to replace spaces with %20
simbad_url = 'http://simbad.u-strasbg.fr/simbad/sim-id?output.format=ASCII&Ident={}&obj.bibsel=off&obj.messel=off&obj.notesel=off'.format(aSimbadIdentifier.replace(' ', '%20'))
try:
response = urllib.request.urlopen(simbad_url, timeout = 5)
content = response.read()
f = open(ofile, 'wb')
f.write(content)
f.close()
except urllib.error.URLError as e:
print(' Error querying Simbad: ',type(e))
print(' Original URL used: {}'.format(simbad_url))
ofile = None
return ofile
def find_identifier_section(self):
l_start = None
l_end = None
for (i, line) in enumerate(self.data):
if 'Identifiers' in line:
l_start = i + 1
if '====================' in line:
l_end = i
return [l_start, l_end]
def read_data(self, datafile):
if os.path.isfile(datafile):
with open(datafile, 'r') as f:
self.data = f.readlines()
else:
raise ValueError('Cannot find file {}'.format(datafile))
# strip off returns in lines
# Note: This is a inplace operation, and also avoids the
# need for map or lambda.
for idx, line in enumerate(self.data):
self.data[idx] = line.strip()
return