-
Notifications
You must be signed in to change notification settings - Fork 0
/
NHighPrecisionObjects.py
executable file
·197 lines (132 loc) · 4.64 KB
/
NHighPrecisionObjects.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
#!/usr/bin/env python
# encoding: utf-8
from Config import *
import pickle
import sys
import numpy as np
import tables
from scipy.interpolate import interp1d
#from srw.NOMADParser import NOMADParser
from NOMADFields import NOMADFieldsParser
import matplotlib.pyplot as plt
import BesanconParser
exptimes, crosspoints, satpoints = pickle.load(open("precisiondata.cpickle"))
interpcross = interp1d(exptimes, crosspoints, kind='linear')
interpsat = interp1d(exptimes, satpoints, kind='linear')
def rangeAtExptime(t):
'''
Returns the range in which a target is observed
at 1mmag for a given exposure time.
Returns:
* lowest magnitude
* highest magnitude
'''
return float(interpsat(t)), float(interpcross(t))
class DataStore(object):
"""
Holds the data and provides
unified IO for it
"""
def __init__(self):
"""@todo: to be defined """
self.fields = {
1: (60, -45),
2: (12 * 15, -45),
3: (20 * 15, -45),
}
self.data = None
self.currentField = None
def setField(self, field):
self.currentField = field
self.fetch()
def visible(self):
'''
Returns the magnitudes of all of the visible
mag < 17 objects
'''
return self.data[self.data < 17]
def highPrecision(self, e):
'''
Returns all of the magnitudes of the
high precision objects
'''
hpRange = rangeAtExptime(e)
return self.data[(self.data > hpRange[0]) & (self.data <= hpRange[1])]
def nVisible(self):
'''
Returns the number of visible objects
'''
#return len(self.visible())
return self.visible().size
def nHighPrecision(self, e):
'''
Returns the number of high precision objects
'''
#return len(self.highPrecision(e))
return self.highPrecision(e).size
def percentage(self, t):
return float(self.nHighPrecision(t)) * 100. / float(self.nVisible())
def fetch(self):
raise NotImplementedError()
def close(self):
self.parser.close()
class NOMADDataStore(DataStore):
"""
Data store for the NOMAD database
"""
def __init__(self):
"""@todo: to be defined """
super(NOMADDataStore, self).__init__()
self.parser = NOMADFieldsParser()
def fetch(self):
self.data = self.parser.getTable("/fields", "field%d" % self.currentField).cols.vmagnitude[:]
self.data = self.data[self.data != 0]
class BesanconDataStore(DataStore):
"""
Data store for the Besancon database
"""
def __init__(self, restr=None):
"""@todo: to be defined """
super(BesanconDataStore, self).__init__()
self.restr = restr
def fetch(self):
self.parser = BesanconParser.BesanconParser()
node = self.parser.getTable("/fields", "field%d" % self.currentField)
if self.restr:
self.data = np.array([row['imagnitude'] for row in node.where(self.restr)])
else:
self.data = node.cols.imagnitude[:]
if __name__ == '__main__':
exptimes = np.linspace(5, 90, 50)
#exptimes = [5,]
visibleMags = {}
fields = [1, 2, 3]
linestyles = ['-', '--', ':']
# Select only the main sequence objects
ModelRestrictions = "((typ == 5) | (typ == 6) | (typ == 7)) & (cl == 5)"
fig = plt.figure()
profileAx = fig.add_subplot(111)
with tables.open_file('out.h5', 'w') as outfile:
outfile.create_array('/', 'exptime', exptimes)
for parser in [{'name': 'NOMAD', 'parser': NOMADDataStore()},]:
#{'name': 'Besancon', 'parser': BesanconDataStore(ModelRestrictions)}]:
print(parser['name'])
for i, field in enumerate(fields):
print("\tField %d" % field)
parser['parser'].setField(field)
visibleMags[parser['name']] = parser['parser'].visible()
if parser['name'] == "NOMAD":
ls = "-"
elif parser['name'] == "Besancon":
ls = "--"
data = [parser['parser'].percentage(e) for e in exptimes]
profileAx.plot(exptimes, data, label="%s %d" % (parser['name'], field),
color='k', ls=linestyles[i])
outfile.create_array('/', 'field{0:d}'.format(field), data)
parser['parser'].close()
profileAx.set_xlabel("Exposure time / s")
profileAx.set_ylabel("Percentage of high precision stars / %")
if len(sys.argv) > 1:
plt.savefig(sys.argv[1])
else:
plt.show()