1
|
import numpy
|
2
|
import os, sys
|
3
|
import random
|
4
|
import matplotlib.pyplot as plt
|
5
|
from matplotlib import rc
|
6
|
|
7
|
import gammalib
|
8
|
import ctools
|
9
|
import cscripts
|
10
|
|
11
|
|
12
|
data=numpy.genfromtxt('pl_4lac_with_redshift_v2.dat', dtype='string')
|
13
|
|
14
|
|
15
|
Source_Name=[]
|
16
|
RAJ2000=[]
|
17
|
DECJ2000=[]
|
18
|
Redshift=[]
|
19
|
Pivot_Energy=[]
|
20
|
PL_Flux_Density=[]
|
21
|
PL_Index=[]
|
22
|
|
23
|
for i in range(1,len(data)):
|
24
|
if(0<float(data[i][3])<4):
|
25
|
Source_Name.append(data[i][0])
|
26
|
RAJ2000.append(float(data[i][1]))
|
27
|
DECJ2000.append(float(data[i][2]))
|
28
|
Redshift.append(float(data[i][3]))
|
29
|
Pivot_Energy.append(float(data[i][4]))
|
30
|
PL_Flux_Density.append(float(data[i][5]))
|
31
|
PL_Index.append(float(data[i][6]))
|
32
|
|
33
|
|
34
|
|
35
|
caldb = 'prod3b-v2'
|
36
|
tmin = 0.0
|
37
|
tmax= 72000
|
38
|
emin = 1.0
|
39
|
emax = 199
|
40
|
|
41
|
irf_used=[]
|
42
|
decs=[]
|
43
|
|
44
|
|
45
|
cta_north_lat=28.71
|
46
|
cta_south_lat=-24.63
|
47
|
alt_cul_north=[]
|
48
|
alt_cul_south=[]
|
49
|
|
50
|
north_irf=[]
|
51
|
south_irf=[]
|
52
|
|
53
|
|
54
|
for i in range(len(DECJ2000)):
|
55
|
ra=RAJ2000[i]
|
56
|
dec=DECJ2000[i]
|
57
|
decs.append(dec)
|
58
|
source=Source_Name[i]
|
59
|
alt_cul_north.append(90-numpy.abs(cta_north_lat-dec))
|
60
|
alt_cul_south.append(90-numpy.abs(cta_south_lat-dec))
|
61
|
|
62
|
|
63
|
|
64
|
|
65
|
|
66
|
for i in range(len(decs)):
|
67
|
if(alt_cul_north[i]<25):
|
68
|
north_irf.append('North_z20_50h')
|
69
|
if(25<alt_cul_north[i]<45):
|
70
|
north_irf.append('North_z40_50h')
|
71
|
if(45<alt_cul_north[i]<65):
|
72
|
north_irf.append('North_z60_50h')
|
73
|
if(65<alt_cul_north[i]<90):
|
74
|
north_irf.append('North_z60_50h')
|
75
|
if(alt_cul_south[i]<25):
|
76
|
south_irf.append('South_z20_50h')
|
77
|
if(25<alt_cul_south[i]<45):
|
78
|
south_irf.append('South_z40_50h')
|
79
|
if(45<alt_cul_south[i]<65):
|
80
|
south_irf.append('South_z60_50h')
|
81
|
if(65<alt_cul_south[i]<90):
|
82
|
south_irf.append('South_z60_50h')
|
83
|
|
84
|
|
85
|
|
86
|
|
87
|
|
88
|
|
89
|
'''
|
90
|
plt.figure(figsize=(16,12))
|
91
|
plt.scatter(north_irf,decs)
|
92
|
plt.xticks(fontsize=14, rotation=0)
|
93
|
plt.yticks(fontsize=14, rotation=0)
|
94
|
plt.ylabel('Declination')
|
95
|
plt.xlabel('North IRF used')
|
96
|
plt.savefig('north_irf.png',dpi=100)
|
97
|
|
98
|
|
99
|
plt.figure(figsize=(16,12))
|
100
|
plt.scatter(south_irf,decs)
|
101
|
plt.ylabel('Declination')
|
102
|
plt.xticks(fontsize=14, rotation=0)
|
103
|
plt.yticks(fontsize=14, rotation=0)
|
104
|
|
105
|
plt.xlabel('South IRF used')
|
106
|
plt.savefig('south_irf.png',dpi=100)
|
107
|
|
108
|
|
109
|
|
110
|
for i in range(125,len(Source_Name)):
|
111
|
if(Source_Name[i]=='4FGL_J0157.7-4614'):
|
112
|
print i
|
113
|
|
114
|
'''
|
115
|
for i in range(len(Source_Name)):
|
116
|
|
117
|
irf_n=north_irf[i]
|
118
|
irf_s=south_irf[i]
|
119
|
|
120
|
irf_n_30='North_z20_50h'
|
121
|
irf_s_30='South_z20_50h'
|
122
|
|
123
|
ra=RAJ2000[i]
|
124
|
dec=DECJ2000[i]
|
125
|
|
126
|
source=Source_Name[i]
|
127
|
print source,ra,dec
|
128
|
for j in range(1):
|
129
|
|
130
|
evfile = '/Volumes/mac_ext/egal_pop/PL_events_file_50hr/%s_events_n1000GeV_%i.fits'%(source,j)
|
131
|
obssim = ctools.ctobssim()
|
132
|
obssim['ra'] = ra
|
133
|
obssim['dec'] = dec
|
134
|
obssim['rad'] = 3.0
|
135
|
obssim['tmin'] = tmin
|
136
|
obssim['tmax'] = tmax
|
137
|
|
138
|
obssim['emin'] = emin
|
139
|
obssim['emax'] = emax
|
140
|
obssim['caldb'] = caldb
|
141
|
obssim['irf'] = irf_n
|
142
|
obssim['nthreads']=0
|
143
|
obssim['chatter']=2
|
144
|
|
145
|
obssim['mode']='ql'
|
146
|
obssim['logfile']='ctobssim_n_%s.log'%(source)
|
147
|
obssim['inmodel'] = '/Volumes/mac_ext/egal_pop/PL_input_model/input_loglaw_pl_%s.xml'%(source)
|
148
|
obssim['outevents'] = evfile
|
149
|
obssim.execute()
|
150
|
|
151
|
for j in range(1):
|
152
|
|
153
|
evfile = '/Volumes/mac_ext/egal_pop/PL_events_file_50hr/%s_events_s1000GeV_%i.fits'%(source,j)
|
154
|
obssim = ctools.ctobssim()
|
155
|
obssim['ra'] = ra
|
156
|
obssim['dec'] = dec
|
157
|
obssim['rad'] = 3.0
|
158
|
obssim['tmin'] = tmin
|
159
|
obssim['tmax'] = tmax
|
160
|
|
161
|
obssim['emin'] = emin
|
162
|
obssim['emax'] = emax
|
163
|
obssim['caldb'] = caldb
|
164
|
obssim['irf'] = irf_s
|
165
|
obssim['inmodel'] = '/Volumes/mac_ext/egal_pop/PL_input_model/input_loglaw_pl_%s.xml'%(source)
|
166
|
obssim['nthreads']=0
|
167
|
obssim['chatter']=2
|
168
|
|
169
|
obssim['mode']='ql'
|
170
|
obssim['logfile']='ctobssim_s_%s.log'%(source)
|
171
|
obssim['outevents'] = evfile
|
172
|
obssim.execute()
|
173
|
|
174
|
|
175
|
|
176
|
|
177
|
like = ctools.ctlike()
|
178
|
evfile = '/Volumes/mac_ext/egal_pop/PL_events_file_50hr/%s_events_n1000GeV_%i.fits'%(source,j)
|
179
|
like['inobs'] = evfile
|
180
|
like['caldb'] = caldb
|
181
|
like['irf'] = irf_n
|
182
|
like['inmodel'] = '/Volumes/mac_ext/egal_pop/PL_input_model/input_loglaw_pl_%s.xml'%(source)
|
183
|
like['outcovmat']='NONE'
|
184
|
like['statistic']='DEFAULT'
|
185
|
like['like_accuracy']= 0.005
|
186
|
like['max_iter']=50
|
187
|
like['logfile']='ctlike_n_%s.log'%(source)
|
188
|
like['outmodel'] = '/Volumes/mac_ext/egal_pop/Fit_results_50hr/results_n1000_%s_%s.xml'%(source,j)
|
189
|
like.execute()
|
190
|
|
191
|
|
192
|
|
193
|
|
194
|
|
195
|
|
196
|
import re
|
197
|
import sys
|
198
|
import os
|
199
|
import glob
|
200
|
|
201
|
orig_stdout = sys.stdout
|
202
|
f = open('/Volumes/mac_ext/egal_pop/Fit_results_50hr/results_n1000_%s_%s.txt'%(source,j),'w')
|
203
|
sys.stdout = f
|
204
|
print(like.obs().models()[0])
|
205
|
|
206
|
sys.stdout = orig_stdout
|
207
|
f.close()
|
208
|
|
209
|
|
210
|
|
211
|
like = ctools.ctlike()
|
212
|
evfile = '/Volumes/mac_ext/egal_pop/PL_events_file_50hr/%s_events_s1000GeV_%i.fits'%(source,j)
|
213
|
like['inobs'] = evfile
|
214
|
like['caldb'] = caldb
|
215
|
like['irf'] = irf_s
|
216
|
like['inmodel'] = '/Volumes/mac_ext/egal_pop/PL_input_model/input_loglaw_pl_%s.xml'%(source)
|
217
|
like['outcovmat']='NONE'
|
218
|
like['statistic']='DEFAULT'
|
219
|
like['like_accuracy']= 0.005
|
220
|
like['max_iter']=50
|
221
|
like['logfile']='ctlike_s_%s.log'%(source)
|
222
|
like['outmodel'] = '/Volumes/mac_ext/egal_pop/Fit_results_50hr/results_s1000_%s_%s.xml'%(source,j)
|
223
|
like.execute()
|
224
|
|
225
|
|
226
|
|
227
|
|
228
|
|
229
|
orig_stdout = sys.stdout
|
230
|
f = open('/Volumes/mac_ext/egal_pop/Fit_results_50hr/results_s1000_%s_%s.txt'%(source,j),'w')
|
231
|
sys.stdout = f
|
232
|
print(like.obs().models()[0])
|
233
|
|
234
|
|
235
|
sys.stdout = orig_stdout
|
236
|
f.close()
|
237
|
|
238
|
|
239
|
|
240
|
|
241
|
'''
|
242
|
|
243
|
butterfly = '/Volumes/mac_ext/egal_pop/PL_obtained_spectra/butterfly_%s_n30GeV.txt'%(source) # text file where the butterfly plot is stored
|
244
|
bttflmk = ctools.ctbutterfly()
|
245
|
bttflmk['inobs'] = '/Volumes/mac_ext/egal_pop/PL_events_file/%s_events_n_%i.fits'%(source,j)
|
246
|
bttflmk['inmodel'] = '/Volumes/mac_ext/egal_pop/Fit_results/results_n30_%s_%s.xml'%(source,j)
|
247
|
bttflmk['srcname'] = '%s'%(source)
|
248
|
bttflmk['emin'] = emin
|
249
|
bttflmk['emax'] = emax
|
250
|
bttflmk['caldb'] = caldb
|
251
|
bttflmk['irf'] = irf_n_30
|
252
|
#bttflmk['statistic'] = 'WSTAT'
|
253
|
bttflmk['outfile'] = butterfly
|
254
|
bttflmk.execute()
|
255
|
|
256
|
|
257
|
|
258
|
|
259
|
butterfly = '/Volumes/mac_ext/egal_pop/PL_obtained_spectra/butterfly_%s_s30GeV.txt'%(source) # text file where the butterfly plot is stored
|
260
|
bttflmk = ctools.ctbutterfly()
|
261
|
bttflmk['inobs'] = '/Volumes/mac_ext/egal_pop/PL_events_file/%s_events_s_%i.fits'%(source,j)
|
262
|
bttflmk['inmodel'] = '/Volumes/mac_ext/egal_pop/Fit_results/results_s30_%s_%s.xml'%(source,j)
|
263
|
bttflmk['srcname'] = '%s'%(source)
|
264
|
bttflmk['emin'] = emin
|
265
|
bttflmk['emax'] = emax
|
266
|
bttflmk['caldb'] = caldb
|
267
|
bttflmk['irf'] = irf_s_30
|
268
|
#bttflmk['statistic'] = 'WSTAT'
|
269
|
bttflmk['outfile'] = butterfly
|
270
|
bttflmk.execute()
|
271
|
|
272
|
sed = "/Volumes/mac_ext/egal_pop/PL_obtained_spectra/sed%s_n30GeV.fits"%(source)
|
273
|
sedmkr = cscripts.csspec()
|
274
|
sedmkr['inobs'] = '/Volumes/mac_ext/egal_pop/PL_events_file/%s_events_n_%i.fits'%(source,j)
|
275
|
sedmkr['inmodel'] = '/Volumes/mac_ext/egal_pop/Fit_results/results_n30_%s_%s.xml'%(source,j)
|
276
|
sedmkr['srcname'] = '%s'%(source)
|
277
|
sedmkr['outfile'] = sed
|
278
|
#sedmkr['statistic'] = 'WSTAT'
|
279
|
sedmkr['method'] = "AUTO"
|
280
|
sedmkr['emin'] = emin
|
281
|
sedmkr['emax'] = emax
|
282
|
sedmkr['caldb'] = caldb
|
283
|
sedmkr['irf'] = irf_n_30
|
284
|
sedmkr['enumbins'] = 10
|
285
|
sedmkr['ebinalg'] = 'LOG'
|
286
|
sedmkr.execute()
|
287
|
|
288
|
|
289
|
|
290
|
sed = "/Volumes/mac_ext/egal_pop/PL_obtained_spectra/sed%s_s30GeV.fits"%(source)
|
291
|
sedmkr = cscripts.csspec()
|
292
|
sedmkr['inobs'] = '/Volumes/mac_ext/egal_pop/PL_events_file/%s_events_s_%i.fits'%(source,j)
|
293
|
sedmkr['inmodel'] = '/Volumes/mac_ext/egal_pop/Fit_results/results_s30_%s_%s.xml'%(source,j)
|
294
|
sedmkr['srcname'] = '%s'%(source)
|
295
|
sedmkr['outfile'] = sed
|
296
|
#sedmkr['statistic'] = 'WSTAT'
|
297
|
sedmkr['method'] = "AUTO"
|
298
|
sedmkr['emin'] = emin
|
299
|
sedmkr['emax'] = emax
|
300
|
sedmkr['caldb'] = caldb
|
301
|
sedmkr['irf'] = irf_s_30
|
302
|
sedmkr['enumbins'] = 10
|
303
|
sedmkr['ebinalg'] = 'LOG'
|
304
|
sedmkr.execute()
|
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
|
#print (like.obs().models()[0])
|
330
|
|
331
|
|
332
|
|
333
|
|
334
|
|
335
|
text_file = open('results_%s_0.03_%s.txt'%(source_name,s))
|
336
|
#print text_file
|
337
|
lines = text_file.readlines()
|
338
|
#print lines
|
339
|
if (numpy.float(re.findall("\d+\.\d+", lines[3])[0])>10):
|
340
|
ts.append(numpy.float(re.findall("\d+\.\d+", lines[3])[0]))
|
341
|
|
342
|
print ts
|
343
|
|
344
|
plt.hist(ts, bins=10)
|
345
|
plt.xlabel('TS')
|
346
|
plt.ylabel('Number')
|
347
|
plt.xlim(numpy.min(ts),numpy.max(ts)+10)
|
348
|
plt.savefig('ts_hist.png')
|
349
|
#plt.show()
|
350
|
|
351
|
maxt=0
|
352
|
for t in range(len(ts)):
|
353
|
if (ts[t]==numpy.max(ts)):
|
354
|
maxt= t
|
355
|
|
356
|
s=maxt
|
357
|
|
358
|
|
359
|
|
360
|
|
361
|
|
362
|
|
363
|
|
364
|
def plot_butterfly(filename,ax,color,label):
|
365
|
#this snippet is extracted from
|
366
|
#$CTOOLS/examples/show_butterfly.py
|
367
|
|
368
|
# Read butterfly file
|
369
|
csv = gammalib.GCsv(filename)
|
370
|
|
371
|
# Initialise arrays to be filled
|
372
|
butterfly_x = []
|
373
|
butterfly_y = []
|
374
|
line_x = []
|
375
|
line_y = []
|
376
|
|
377
|
# Loop over rows of the file
|
378
|
nrows = csv.nrows()
|
379
|
for row in range(nrows):
|
380
|
|
381
|
# Get conversion coefficient
|
382
|
conv = csv.real(row,0) * csv.real(row,0) * gammalib.MeV2erg
|
383
|
|
384
|
# Compute upper edge of confidence band
|
385
|
butterfly_x.append(csv.real(row,0)/1.0e6) # TeV
|
386
|
butterfly_y.append(csv.real(row,2)*conv)
|
387
|
|
388
|
# Set line values
|
389
|
line_x.append(csv.real(row,0)/1.0e6) # TeV
|
390
|
line_y.append(csv.real(row,1)*conv)
|
391
|
|
392
|
# Loop over the rows backwards to compute the lower edge of the
|
393
|
# confidence band
|
394
|
for row in range(nrows):
|
395
|
index = nrows - 1 - row
|
396
|
conv = csv.real(index,0) * csv.real(index,0) * gammalib.MeV2erg
|
397
|
butterfly_x.append(csv.real(index,0)/1.0e6)
|
398
|
low_error = max(csv.real(index,3)*conv, 1e-26)
|
399
|
butterfly_y.append(low_error)
|
400
|
|
401
|
# Plot
|
402
|
ax.plot(line_x,line_y,color=color,ls='--',alpha=0.5,label=label)
|
403
|
#ax.fill(butterfly_x,butterfly_y,color=color,alpha=0.2)
|
404
|
|
405
|
def plot_sed(filename,ax,color,ts_thresh=9):
|
406
|
#this snippet is extracted from
|
407
|
#$CTOOLS/examples/show_spectrum.py
|
408
|
|
409
|
fits = gammalib.GFits(filename)
|
410
|
table = fits.table(1)
|
411
|
c_energy = table['Energy']
|
412
|
c_ed = table['ed_Energy']
|
413
|
c_eu = table['eu_Energy']
|
414
|
c_flux = table['Flux']
|
415
|
c_eflux = table['e_Flux']
|
416
|
c_ts = table['TS']
|
417
|
c_upper = table['UpperLimit']
|
418
|
|
419
|
# Initialise arrays to be filled
|
420
|
energies = []
|
421
|
flux = []
|
422
|
ed_engs = []
|
423
|
eu_engs = []
|
424
|
e_flux = []
|
425
|
ul_energies = []
|
426
|
ul_ed_engs = []
|
427
|
ul_eu_engs = []
|
428
|
ul_flux = []
|
429
|
|
430
|
# Loop over rows of the file
|
431
|
nrows = table.nrows()
|
432
|
for row in range(nrows):
|
433
|
|
434
|
# Get Test Statistic, flux and flux error
|
435
|
ts = c_ts.real(row)
|
436
|
flx = c_flux.real(row)
|
437
|
e_flx = c_eflux.real(row)
|
438
|
|
439
|
# If Test Statistic is larger than threshold and flux error is smaller than
|
440
|
# flux then append flux plots ...
|
441
|
if ts > ts_thresh and e_flx < flx:
|
442
|
energies.append(c_energy.real(row))
|
443
|
flux.append(c_flux.real(row))
|
444
|
ed_engs.append(c_ed.real(row))
|
445
|
eu_engs.append(c_eu.real(row))
|
446
|
e_flux.append(c_eflux.real(row))
|
447
|
|
448
|
# ... otherwise append upper limit
|
449
|
else:
|
450
|
ul_energies.append(c_energy.real(row))
|
451
|
ul_flux.append(c_upper.real(row))
|
452
|
ul_ed_engs.append(c_ed.real(row))
|
453
|
ul_eu_engs.append(c_eu.real(row))
|
454
|
|
455
|
# Set upper limit errors
|
456
|
yerr = [0.6 * x for x in ul_flux]
|
457
|
|
458
|
# Plot
|
459
|
ax.errorbar(energies, flux, yerr=e_flux, xerr=[ed_engs, eu_engs],
|
460
|
fmt='o'+color,alpha=0.4)
|
461
|
ax.errorbar(ul_energies, ul_flux, xerr=[ul_ed_engs, ul_eu_engs],
|
462
|
yerr=yerr, uplims=True, fmt='o'+color,alpha=0.4)
|
463
|
|
464
|
|
465
|
|
466
|
fig6 = plt.figure()
|
467
|
ax = plt.subplot()
|
468
|
|
469
|
# define axes properties
|
470
|
ax.set_xscale('log')
|
471
|
ax.set_yscale('log')
|
472
|
ax.set_xlabel('Energy (TeV)')
|
473
|
ax.set_ylabel(r'E$^2$ $\times$ dN/dE (erg cm$^{-2}$ s$^{-1}$)')
|
474
|
|
475
|
# plot results
|
476
|
plot_butterfly(butterfly,ax,color='b',label='%s'%(source_name))
|
477
|
plot_sed(sed,ax,color='g')
|
478
|
|
479
|
|
480
|
|
481
|
# legend
|
482
|
ax.legend(loc='best')
|
483
|
pylab.xlim([0.03,30])
|
484
|
#pylab.ylim([10**(-10),10**(-9)])
|
485
|
plt.savefig('butterfly_%s_0.03_%s.png'%(source_name,s))
|
486
|
#plt.show()
|
487
|
|
488
|
ulimit=ctools.ctulimit()
|
489
|
ulimit['inobs'] = evfile
|
490
|
ulimit['emin'] = emin
|
491
|
ulimit['emax'] = emax
|
492
|
ulimit['caldb'] = caldb
|
493
|
ulimit['irf'] = irf
|
494
|
ulimit['srcname'] = '%s'%(source_name)
|
495
|
ulimit['inmodel'] = 'input_loglaw.xml'
|
496
|
ulimit.execute()
|
497
|
|
498
|
'''
|