-
Notifications
You must be signed in to change notification settings - Fork 0
/
listbincnts.py
369 lines (222 loc) · 6.5 KB
/
listbincnts.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
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
# listbincnts.py
# Sam Connolly 27/06/2013
#===============================================================================
# bin data according in an ascii file of column data, such that
# each bin has a minimum total from one column, ordered according to another
# giving the bin of each data point as a LIST. Also, a minwidth is available.
# UNEVEN BINS.
#===============================================================================
# Import packages
import pylab as plt
#================ PARAMETERS ===================================================
# read variables
header = 1 # number of header lines to ignore
bincolumn = 6 # column to bin along
exposurecolumn = 3 # exposure time column
mincount = True # minimum total for some value in each bin?
mincolumn = 12
binmin = 1000
minnum = False # minimum number of points in each bin?
countmin = 5
minwidth = True # minimum bin width?
widthmin = 0.025
# File routes
route = "/disks/raid/raid1/xray/raid/sdc1g08/NetData/Ngc4395/"\
+ "lightcurve/raw/"
# file name
infilename = "NGC4395_lcurve_gti_2-10keV.txt"
# quality check?
check = True
# histogram of binning?
hist = True
# Save output?
save = True
savefile = "binsout.dat"
outroute = "/disks/raid/raid1/xray/raid/sdc1g08/NetData/Ngc4395/"\
+ "spectra/gtihardbin/"
print outroute
# Highlighted lightcurve?
lc = True
timecolumn = 2
labels = True
#==================== Load data ================================================
# create file routes
location = route+infilename
savelocation = outroute+savefile
# read data intoarray
start = 0
infile= open(location, 'r')
num = 1
for line in infile:
linedata = line.split()
if start == header:
columns = len(linedata)
data = [[] for x in range(columns + 1)]
if start >= header:
if check == False:
linedata[10] = 'a1'
if linedata[10] == 'a1':
for column in range(columns):
if len(linedata) == columns:
try:
data[column].append(float(linedata[column]))
except ValueError:
data[column].append(linedata[column])
data[-1].append(num)
num += 1
start += 1
infile.close()
bincolumn -= 1
mincolumn -= 1
timecolumn -= 1
exposurecolumn -= 1
if minwidth == False: # turn off minimum width if required
widthmin = 0
if minnum == False:
countmin = 0
#========================= Sort according to bincolumn =========================
# create array of location indices for sorted list
start = True
for index in range(len(data[0])):
if start == True:
sortindex = [index]
start = False
else:
i = 0
if data[bincolumn][index] < data[bincolumn][sortindex[-1]]:
while data[bincolumn][index] > data[bincolumn][sortindex[i]]:
i += 1
sortindex.insert(i,index)
else:
sortindex.append(index)
#======================== Bin ==================================================
bins = []
end = False
index = 0
total = 0
nbin = 0
nbinned = 0
this = [[],[],0]
for index in range(len(sortindex)):
this[0].append(sortindex[index]) # add index to bin
this[1].append(data[-1][sortindex[index]]) # add actual index to bin
nbinned += 1
total += data[mincolumn][sortindex[index]] # add counts
if mincount == False:
if (data[bincolumn][this[0][-1]] - \
data[bincolumn][this[0][0]]) >= widthmin:
if len(this[0]) >= countmin:
this[2] = total
bins.append(this)
nbin += 1
this = [[],[],0]
total = 0
if mincount == True:
if total > binmin: # change bin at sufficient counts
if (data[bincolumn][this[0][-1]] - \
data[bincolumn][this[0][0]]) >= widthmin:
if len(this[0]) >= countmin:
this[2] = total
bins.append(this)
nbin += 1
this = [[],[],0]
total = 0
this[2] = total
print bins[-1]
if this[2] > binmin and mincount == True and mincount == False:
bins.append(this)
nbin += 1
elif len(this[0]) > binmin and mincount == False and mincount == True:
bins.append(this)
nbin += 1
elif len(this[0]) > binmin and this[2] > binmin \
and mincount == True and mincount == True:
bins.append(this)
nbin += 1
else:
for p in range(len(this[0])):
bins[-1][0].append(this[0][p])
bins[-1][1].append(this[1][p])
bins[-1][2] += this[2]
print bins[-1]
#======================== print & save output ==================================
totcnts = 0
if save == True:
out = open(savelocation,'w')
for b in range(len(bins)):
low = data[bincolumn][bins[b][0][0]]
try:
high = data[bincolumn][bins[b+1][0][0]]
except IndexError:
high = data[bincolumn][bins[b][0][-1]]
mid = (low+high)/2.0
cnts = bins[b][2]
totcnts += cnts
print low, " >= x > ", high, " ==> ",mid, "counts:", str(cnts), " :\n"
if save == True:
out.write(str(low) + " >=x> " + str(high) + " :\n")
output = ''
for index in bins[b][1]:
output = output + str(index) + '\t'
print output, "\n"
if save == True:
out.write(output+"\n")
print "Number of binned points:", nbinned
print "Number of bins: ", len(bins)
print "Total counts: ", totcnts
print "Total exposure time: "
if save == True:
out.write("number of bins: " + str(len(bins)))
out.close()
print "Saved as:" + savefile + "in:" + outroute
# plots
nplots = 0
if hist:
nplots += 1
if lc:
nplots += 1
if nplots == 1:
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
if nplots == 2:
fig = plt.figure()
# histogram
if hist:
if nplots == 2:
ax = fig.add_subplot(1,2,1)
edges = []
counts = []
widths = []
for b in range(len(bins)):
edges.append(data[bincolumn][bins[b][0][0]])
counts.append(bins[b][2])
try:
widths.append(data[bincolumn][bins[b+1][0][0]]-\
data[bincolumn][bins[b][0][0]])
except IndexError:
widths.append(data[bincolumn][bins[b][0][-1]]-\
data[bincolumn][bins[b][0][0]])
plt.bar(edges,counts,widths)
# highlighted lightcurve
if lc:
if nplots == 2:
ax = fig.add_subplot(1,2,2)
plt.scatter(data[timecolumn],data[bincolumn])
for b in range(len(bins)):
try:
plt.axhspan(data[bincolumn][bins[b][0][0]], \
data[bincolumn][bins[b+1][0][0]],alpha = 0.3)
except IndexError:
plt.axhspan(data[bincolumn][bins[b][0][0]], \
data[bincolumn][bins[b][0][-1]],alpha = 0.3)
if labels:
for index in range(len(data[0])):
plt.annotate(data[-1][index],
xy = (data[timecolumn][index],data[bincolumn][index]),
xytext = (-20,20),
textcoords = 'offset points', ha = 'right', va = 'bottom',
bbox = dict(boxstyle = 'round,pad=0.5', fc = 'blue',
alpha = 0.5), arrowprops = dict(arrowstyle = '->',
connectionstyle = 'arc3,rad=0'))
if nplots > 0:
plt.show()