1# This file is part of the Astrometry.net suite.
2# Licensed under a 3-clause BSD style license - see LICENSE
3from __future__ import print_function
4import simplejson
5import re
6
7lines = open('brightstars-data.c').readlines()
8l = ''.join(lines[3:-2]).replace('{','[').replace('}',']')
9
10def replace_unicode(match):
11    c1 = match.group(1)
12    c2 = match.group(2)
13    #s = eval('\\x%s\\x%s'
14    s = '"\\x%s\\x%s"' % (c1, c2)
15    #print 's', s
16    s = eval(s)
17    #print 's', s
18    d = s.decode('utf8')
19    return d + ' '
20
21l = re.sub(r'\\x(..)\\x(..)""', replace_unicode, l)
22#l = re.sub(r'\\x(..)\\x(..)""', r'\u\1\2 ', l)
23#l = re.sub(r'\\x(..)\\x(..)""', r'\x\1\x\2 ', l)
24#l = l.decode('utf8')
25
26l = '[' + l + '0 ]'
27print(l)
28
29j = simplejson.loads(l)
30j = j[:-1]
31print(j)
32
33nm, nm2, rr, dd = [],[],[],[]
34vmag = []
35for n1,n2,r,d,mag in j:
36    #print 'n1', n1
37    j = simplejson.dumps(n1)
38    #print 'json:', j
39    #j = str(j)
40    #print '  ->', j
41    nm.append(j.replace('"', ''))
42    nm2.append(str(n2))
43    vmag.append(float(mag))
44    rr.append(r)
45    dd.append(d)
46
47from astrometry.util.fits import *
48import numpy as np
49
50T = tabledata()
51T.name1 = np.array(nm)
52T.name2 = np.array(nm2)
53T.vmag = np.array(vmag)
54T.ra = np.array(rr)
55T.dec = np.array(dd)
56T.about()
57T.writeto('brightstars.fits')
58