pl_simulations_30GeV.py

Acharyya Atreya, 03/04/2020 10:18 AM

Download (7.03 KB)

 
1
import numpy
2
import os, sys
3
import random
4
#import matplotlib.pyplot as plt
5
from matplotlib import rc
6
import gammalib
7
import ctools
8
import cscripts
9

    
10

    
11
data=numpy.genfromtxt('pl_4lac_with_redshift_v2.dat', dtype='string')
12

    
13

    
14
Source_Name=[]
15
RAJ2000=[]
16
DECJ2000=[]
17
Redshift=[]
18
Pivot_Energy=[]
19
PL_Flux_Density=[]#units ph / (cm2 MeV s)
20
PL_Index=[]
21

    
22
for i in range(1,len(data)):
23
    if(0<float(data[i][3])<4):
24
        Source_Name.append(data[i][0])
25
        RAJ2000.append(float(data[i][1]))
26
        DECJ2000.append(float(data[i][2]))
27
        Redshift.append(float(data[i][3]))
28
        Pivot_Energy.append(float(data[i][4]))
29
        PL_Flux_Density.append(float(data[i][5]))
30
        PL_Index.append(float(data[i][6]))
31

    
32

    
33

    
34
caldb = 'prod3b-v2'
35
tmin = 0.0
36
tmax= 72000
37
emin  = 0.03  # TeV
38
emax  = 199 # TeV
39

    
40
irf_used=[]
41
decs=[]
42

    
43

    
44
cta_north_lat=28.71
45
cta_south_lat=-24.63
46
alt_cul_north=[]
47
alt_cul_south=[]
48

    
49

    
50

    
51

    
52
for i in range(len(DECJ2000)):
53
    ra=RAJ2000[i]
54
    dec=DECJ2000[i]
55
    decs.append(dec)
56
    source=Source_Name[i]
57
    alt_cul_north.append(90-numpy.abs(cta_north_lat-dec))
58
    alt_cul_south.append(90-numpy.abs(cta_south_lat-dec))
59

    
60

    
61
zenith_n=[]
62
zenith_s=[]
63

    
64

    
65
for i in range(len(alt_cul_north)):
66
    zenith_n.append(90-alt_cul_north[i])
67
    
68
for i in range(len(alt_cul_south)):
69
    zenith_s.append(90-alt_cul_south[i])
70

    
71

    
72
'''
73
final_ra=[]
74
final_dec=[]
75

76
final_ra_rad=[]
77
final_dec_rad=[]
78

79

80
ra2=[]
81

82

83
for i in range(len(RAJ2000)):
84
    ra2.append(RAJ2000[i])
85
    
86
    
87
for i in range(len(ra2)):
88
    if(ra2[i]>180):
89
        ra2[i]=ra2[i]-360
90

91

92
for i in range(len(ra2)):
93
    final_ra_rad.append((ra2[i])*0.0174533)
94
    final_dec_rad.append((DECJ2000[i])*0.0174533)
95

96

97
for i in range(len(final_ra)):
98
    final_ra_rad.append((final_ra[i])*0.0174533)
99
    final_dec_rad.append((final_dec[i])*0.0174533)
100

101

102

103

104
fig = plt.figure(figsize=(20,16))
105
ax = fig.add_subplot(111, projection="mollweide")
106
cm = plt.cm.get_cmap('jet')
107
plt.grid(True)
108
for i in range(len(final_ra_rad)):
109
    if(0<zenith_n[i]<90):
110
        SC =ax.scatter(final_ra_rad[i], final_dec_rad[i], c=zenith_n[i],cmap=cm,vmin=0, vmax=100)
111
cbar = plt.colorbar(SC, shrink=1., pad=0.05)
112
cbar.ax.tick_params(labelsize=20)
113
cbar.set_label('Zenith Angle', fontsize=20)
114
plt.title('CTA N')
115
plt.savefig('Plots/CTA_N_zenith.png',dpi=100)
116
#plt.show()
117

118

119

120
fig = plt.figure(figsize=(20,16))
121
ax = fig.add_subplot(111, projection="mollweide")
122
cm = plt.cm.get_cmap('jet')
123
plt.grid(True)
124
for i in range(len(final_ra_rad)):
125
    if(0<zenith_s[i]<90):
126
        SC =ax.scatter(final_ra_rad[i], final_dec_rad[i], c=zenith_s[i],cmap=cm,vmin=0, vmax=100)
127
cbar = plt.colorbar(SC, shrink=1., pad=0.05)
128
cbar.ax.tick_params(labelsize=20)
129
cbar.set_label('Zenith Angle', fontsize=20)
130
plt.title('CTA S')
131
plt.savefig('Plots/CTA_S_zenith.png',dpi=100)
132
'''
133

    
134
north_irf=[]
135
south_irf=[]
136

    
137

    
138
for i in range(len(zenith_n)):
139
    if(0<zenith_n[i]<25):
140
        north_irf.append('North_z20_50h')
141
    if(25<zenith_n[i]<45):
142
        north_irf.append('North_z40_50h')
143
    if(45<zenith_n[i]<65):
144
        north_irf.append('North_z60_50h')
145
    else:
146
        north_irf.append('North_z20_50h')
147
    
148
for i in range(len(zenith_s)):
149
    if(0<zenith_s[i]<25):
150
        south_irf.append('South_z20_50h')
151
    if(25<zenith_s[i]<45):
152
        south_irf.append('South_z40_50h')
153
    if(45<zenith_s[i]<65):
154
        south_irf.append('South_z60_50h')
155
    else:
156
        south_irf.append('South_z20_50h')
157

    
158

    
159
for i in range(826,len(Source_Name)):
160
    irf_n=north_irf[i]
161
    irf_s=south_irf[i]
162

    
163
    irf_n_30='North_z20_50h'
164
    irf_s_30='South_z20_50h'
165

    
166
    ra=RAJ2000[i]
167
    dec=DECJ2000[i]
168

    
169
    source=Source_Name[i]
170
    print source,ra,dec,irf_n,irf_s
171
    for j in range(1):
172
    #s1=random.randint(1,1000)
173
        evfile = 'PL_events_file_20hr/%s_events_n30GeV_%i.fits'%(source,j)
174
        obssim = ctools.ctobssim()
175
        obssim['ra']        = ra
176
        obssim['dec']       = dec
177
        obssim['rad']       = 3.0
178
        obssim['tmin']      = tmin#'2020-01-01T00:00:00'
179
        obssim['tmax']      = tmax
180
    #obssim['seed'] = s1 # Any integer value you like
181
        obssim['emin']      = emin
182
        obssim['emax']      = emax
183
        obssim['caldb']     = caldb
184
        obssim['irf']       = irf_n_30
185
        obssim['nthreads']=0
186
        obssim['chatter']=2
187
    #obssim['clobber']='yes'
188
        obssim['mode']='ql'
189
        obssim['logfile']='ctobssim_n_%s.log'%(source)
190
        obssim['inmodel']   = 'PL_input_model/input_loglaw_pl_%s.xml'%(source)
191
        obssim['outevents'] = evfile
192
        obssim.execute()
193

    
194
    for j in range(1):
195
    #s1=random.randint(1,1000)
196
        evfile = 'PL_events_file_20hr/%s_events_s30GeV_%i.fits'%(source,j)
197
        obssim = ctools.ctobssim()
198
        obssim['ra']        = ra
199
        obssim['dec']       = dec
200
        obssim['rad']       = 3.0
201
        obssim['tmin']      = tmin#'2020-01-01T00:00:00'
202
        obssim['tmax']      = tmax
203
    #obssim['seed'] = s1 # Any integer value you like
204
        obssim['emin']      = emin
205
        obssim['emax']      = emax
206
        obssim['caldb']     = caldb
207
        obssim['irf']       = irf_s_30
208
        obssim['inmodel']   = 'PL_input_model/input_loglaw_pl_%s.xml'%(source)
209
        obssim['nthreads']=0
210
        obssim['chatter']=2
211
    #obssim['clobber']='yes'
212
        obssim['mode']='ql'
213
        obssim['logfile']='ctobssim_s_%s.log'%(source)
214
        obssim['outevents'] = evfile
215
        obssim.execute()
216
    
217
    
218
        
219
        
220
        
221
        like = ctools.ctlike()
222
        evfile = 'PL_events_file_20hr/%s_events_n30GeV_%i.fits'%(source,j)
223
        like['inobs']   = evfile
224
        like['caldb']   = caldb
225
        like['irf']     = irf_n_30
226
        like['inmodel'] = 'PL_input_model/input_loglaw_pl_%s.xml'%(source)
227
        like['outcovmat']='NONE'
228
        like['statistic']='DEFAULT'
229
        like['like_accuracy']= 0.005
230
        like['max_iter']=50
231
        like['logfile']='ctlike_n_%s.log'%(source)
232
        like['outmodel'] = 'Fit_results_20hr/results_n30_%s_%s.xml'%(source,j)
233
        like.execute()
234
        
235
        
236
    
237

    
238
        
239
        
240
        import re
241
        import sys
242
        import os
243
        import glob
244
        
245
        orig_stdout = sys.stdout
246
        f = open('Fit_results_20hr/results_n30_%s_%s.txt'%(source,j),'w')
247
        sys.stdout = f
248
        print(like.obs().models()[0])
249
        #print s1
250
        sys.stdout = orig_stdout
251
        f.close()
252
        
253
        
254
        
255
        like = ctools.ctlike()
256
        evfile = 'PL_events_file_20hr/%s_events_s30GeV_%i.fits'%(source,j)
257
        like['inobs']   = evfile
258
        like['caldb']   = caldb
259
        like['irf']     = irf_s_30
260
        like['inmodel'] = 'PL_input_model/input_loglaw_pl_%s.xml'%(source)
261
        like['outcovmat']='NONE'
262
        like['statistic']='DEFAULT'
263
        like['like_accuracy']= 0.005
264
        like['max_iter']=50
265
        like['logfile']='ctlike_s_%s.log'%(source)
266
        like['outmodel'] = 'Fit_results_20hr/results_s30_%s_%s.xml'%(source,j)
267
        like.execute()
268
        
269
        
270
        
271

    
272
        
273
        orig_stdout = sys.stdout
274
        f = open('Fit_results_20hr/results_s30_%s_%s.txt'%(source,j),'w')
275
        sys.stdout = f
276
        print(like.obs().models()[0])
277
        #print s1
278
        
279
        sys.stdout = orig_stdout
280
        f.close()
281