-
Notifications
You must be signed in to change notification settings - Fork 0
/
binaries.py
191 lines (159 loc) · 6.94 KB
/
binaries.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
#! /usr/bin/env python3
"""
Physical and orbital properties of the known gamma-ray binaries
and some X-ray binaries.
"""
import os as _os
import configparser as _configparser
from astropy import units as u
from astropy import coordinates
from astropy import time
from binaries.src.parameters import Parameter
__author__ = "Benito Marcote"
__copyright__ = "Copyright 2014, Benito Marcote"
__credits__ = ["Benito Marcote"]
__license__ = "GPL"
__version__ = "2.0.1"
__maintainer__ = "Benito Marcote"
__email__ = "[email protected]"
__status__ = "Development"
class System(object):
def __init__(self, name, coordinates, **kwargs):
self._name = name
self._coordinates = coordinates
for key, value in kwargs.items():
setattr(self, key, value)
@property
def name(self):
return self._name
@property
def coordinates(self):
return self._coordinates
@coordinates.setter
def coordinates(self, coordinates):
self._coordinates = coordinates
def __repr__(self):
return '<Sources.binaries.System: '+self.name+'>'
def read_sources_from_inifiles(path):
_config = _configparser.ConfigParser()
_binaries = {}
# Import all the known objects
_files = _os.listdir(path)
for a_file in _files:
if a_file[-4:] == '.ini':
_config.read(path+'/'+a_file)
#if is_python27:
# _config = _config.__dict__['_sections']
#
params = {}
source_name = ''
abrv_name = ''
for a_section in _config.sections():
if a_section == 'source_name':
source_name = _config[a_section]['name']
abrv_name = _config[a_section]['abrv']
continue
name = a_section
desc = _config[a_section].get('description', '')
ref = _config[a_section].get('reference', '')
val = _config[a_section].get('value')
err = _config[a_section].get('error', None) # Change this
has_error = _config[a_section].getboolean('has_error', fallback=True)
try:
val = float(val)
err = [float(i) for i in err.split(',')]
#err = float(err)
if _config.has_option(a_section, 'unit') and (name != 'ra' or name != 'dec'):
val = val*eval(_config[a_section]['unit'])
if err != None:
# Can be two values: -err1, +err2 or just one +-err
for i in range(len(err)):
err[i] = err[i]*eval(_config[a_section]['unit'])
# If there is only one value for the error, then ocnvert to a quantity.
if len(err) == 1:
err = err[0]
except (TypeError, ValueError):
# The values are strings
pass
params[name] = Parameter(name, desc, val, has_error, err, ref)
ra = params['ra']
dec = params['dec']
params.pop('ra')
params.pop('dec')
# ra and dec are coordinates
ra_units = eval(_config['ra']['unit'])
dec_units = eval(_config['dec']['unit'])
distance = params['distance']
if 'mu_alpha_cos_delta' in params: # It has proper motions defined!
pm_ra_cos_dec = params['mu_alpha_cos_delta']
pm_dec = params['mu_delta']
if 'ref_epoch' in params:
ref_epoch = time.Time(params['ref_epoch'].value, format='mjd')
else:
print('WARNING: Epoch J2000.0 is assumed for {}.'.format(source_name))
ref_epoch = time.Time(2000.0, format='decimalyear')
coord_val = coordinates.SkyCoord(ra=ra.value, dec=dec.value, unit=(ra_units,dec_units),
distance=distance.value, pm_ra_cosdec=pm_ra_cos_dec.value,
pm_dec=pm_dec.value, obstime=ref_epoch)
coord_err = coordinates.SkyCoord(ra=ra.error, dec=dec.error, unit=(ra_units, dec_units),
distance=distance.error)
#pm_dec=pm_dec.value,
#pm_ra_cosdec=pm_ra_cos_dec.value, obstime=ref_epoch)
else:
coord_val = coordinates.SkyCoord(ra=ra.value, dec=dec.value, unit=(ra_units,dec_units),
distance=distance.value)
coord_err = coordinates.SkyCoord(ra=ra.error, dec=dec.error, unit=(ra_units, dec_units),
distance=distance.value)
ref = ra.reference.strip()
if ref != dec.reference.strip():
ref = ','.join([ref, dec.reference.strip()])
coord = Parameter('Coordinates', ra.description+'\n'+dec.description, coord_val, True,
coord_err, ref)
_binaries[abrv_name] = System(source_name, coord, **params)
return _binaries
class Binaries(object):
"""
The initialized object contains all the known sources with its properties as attributes.
"""
def __init__(self):
path = _os.path.abspath(__file__)
dir_path = _os.path.dirname(path)
self._binaries = read_sources_from_inifiles(dir_path + '/data/')
for key in self._binaries.keys():
#print(key)
setattr(self, key, self._binaries[key])
def list_binaries(self):
for key in self._binaries.keys():
print(self._binaries[key].name)
def return_name_binaries(self):
names = []
for key in self._binaries.keys():
names.append(self._binaries[key].name)
return names
def get_binaries_as_dict(self):
d = {}
for key in self._binaries.keys():
d[self._binaries[key].name] = self._binaries[key]
return d
def get_binary(self, name):
for key in self._binaries:
if self._binaries[key].name == name:
return self._binaries[key]
# No binary found
raise KeyError
def reload(self):
"""
Reload all the binaries.
Read again the existing init files to load all the known systems.
The new object has all the sytems and their parameters updated.
"""
# First remove all the current attributes
for key in _binaries.keys():
delattr(self, key)
# Read again the init files and put the new attributes
path = _os.path.abspath(__file__)
dir_path = _os.path.dirname(path)
self._binaries = read_sources_from_inifiles(dir_path + '/data/')
for key in _binaries.keys():
print(key)
setattr(self, key, self._binaries[key])