Blame view

Modules/Timing.py 4.79 KB
f5a75019   Thomas Fitoussi   Rewrite Analysis....
1
from numpy import histogram, log10, shape, arange, pi, convolve, r_
7030f150   Thomas Fitoussi   Full reorganisati...
2
from matplotlib.pyplot import figure, show
f5a75019   Thomas Fitoussi   Rewrite Analysis....
3
from Read import ReadTime, ReadTiming, ReadExtraFile, ReadProfile, ReadEnergy, ReadGeneration
f4246390   Thomas Fitoussi   Improve angular d...
4
from Constants import yr, degre
65135318   Thomas Fitoussi   Reset modules
5
from Analytic import Analytic_delay_vs_theta
7030f150   Thomas Fitoussi   Full reorganisati...
6

074c1729   Thomas Fitoussi   Analysis.py used ...
7
def timing(time,weight,nbBins=100):
f4246390   Thomas Fitoussi   Improve angular d...
8
9
10
11
12
13
   '''
      Compute flux versus time delay
      Input: directory name
      Input (optional): generation (default = all)
      Output: time delay (sec), flux
   '''
a44230b7   Thomas Fitoussi   Improve approxima...
14
15
16
17
   cond = time>0
   time = time[cond]
   time=log10(time)
   weight=weight[cond]
7030f150   Thomas Fitoussi   Full reorganisati...
18

a44230b7   Thomas Fitoussi   Improve approxima...
19
20
21
   #dN,dt=histogram(time,nbBins,range=[-1,1e17],weights=weight)
   dN,dt=histogram(time,nbBins,range=[-1,17],weights=weight)
   dt = 10**dt
7030f150   Thomas Fitoussi   Full reorganisati...
22
23
   timecenter=(dt[1:nbBins+1]+dt[0:nbBins])/2
   binSize=dt[1:nbBins+1]-dt[0:nbBins]
a44230b7   Thomas Fitoussi   Improve approxima...
24
   dNdt=dN/binSize *timecenter
7030f150   Thomas Fitoussi   Full reorganisati...
25

f5a75019   Thomas Fitoussi   Rewrite Analysis....
26
   return timecenter, dNdt#/max(dNdt)
7030f150   Thomas Fitoussi   Full reorganisati...
27

f5a75019   Thomas Fitoussi   Rewrite Analysis....
28
def drawTiming(files,plot_gen=False,plot_energy_range=False):
f4246390   Thomas Fitoussi   Improve angular d...
29
30
31
   '''
      Plot flux versus time delay
      Input: list of directories
f4246390   Thomas Fitoussi   Improve angular d...
32
33
      Output: graph of flux versus time delay 
   '''
7030f150   Thomas Fitoussi   Full reorganisati...
34
35
36
   fig = figure()
   ax = fig.add_subplot(111)
   for fileId in files:
f5a75019   Thomas Fitoussi   Rewrite Analysis....
37
38
39
40
41
42
      delta_t,dNdt = ReadTiming(fileId,[0,1])
      p=ax.plot(delta_t,dNdt,linestyle="steps-mid")

      if plot_energy_range:
         labels = ["MeV band","GeV band","TeV band"]
         i = 2
a44230b7   Thomas Fitoussi   Improve approxima...
43
         for n in arange(0,3,1):
a44230b7   Thomas Fitoussi   Improve approxima...
44
            delta_t,dNdt = ReadTiming(fileId,[0,i])
f5a75019   Thomas Fitoussi   Rewrite Analysis....
45
            ax.plot(delta_t,dNdt,linestyle="steps-mid",label=labels[n])
a44230b7   Thomas Fitoussi   Improve approxima...
46
            i+=1
f5a75019   Thomas Fitoussi   Rewrite Analysis....
47
48
49
50
51

      if plot_gen:
         generation = ReadGeneration(fileId,[0])
         i=5
         for gen in generation:
a44230b7   Thomas Fitoussi   Improve approxima...
52
53
54
            delta_t,dNdt = ReadTiming(fileId,[0,i])
            ax.plot(delta_t,dNdt,linestyle="steps-mid",label="gen=%.0f"%gen)
            i+=1
f4246390   Thomas Fitoussi   Improve angular d...
55
56

   ax.set_xscale('log')
7030f150   Thomas Fitoussi   Full reorganisati...
57
58
59
   ax.set_yscale('log')
   ax.grid(b=True,which='major')
   ax.legend(loc="best")
f4246390   Thomas Fitoussi   Improve angular d...
60
   ax.set_xlabel("Time delay [s]")
a44230b7   Thomas Fitoussi   Improve approxima...
61
62
63
64
65
66
67
68
   ax.set_ylabel("$t.dN/dt$ [arbitrary units]")

   show()

def drawConvolveTiming(files):
   fig = figure()
   ax = fig.add_subplot(111)
   for fileId in files:
f5a75019   Thomas Fitoussi   Rewrite Analysis....
69
70
71
72
73
74
75
76
77
78
79
80
81
      delta_t,dNdt1 = ReadTiming(fileId,[0,2]) # MeV band
      delta_t,dNdt2 = ReadTiming(fileId,[0,3]) # GeV band
      delta_t,dNdt3 = ReadTiming(fileId,[0,4]) # TeV band
      conv1 = convolve(dNdt1,dNdt2,'same')
      conv2 = convolve(dNdt2,dNdt3,'same')
      conv3 = convolve(dNdt1,dNdt3,'same')
      ax.plot(delta_t,conv1/max(conv1),linestyle="steps-mid",label="MeV - GeV band")
      ax.plot(delta_t,conv2/max(conv2),linestyle="steps-mid",label="GeV - TeV band")
      ax.plot(delta_t,conv3/max(conv3),linestyle="steps-mid",label="MeV - TeV band")
      print fileId, " ====================================="
      print "   MeV - GeV band: ",delta_t[r_[True, conv1[1:] > conv1[:-1]] & r_[conv1[:-1] > conv1[1:], True]] 
      print "   GeV - TeV band: ",delta_t[r_[True, conv2[1:] > conv2[:-1]] & r_[conv2[:-1] > conv2[1:], True]]
      print "   MeV - TeV band: ",delta_t[r_[True, conv3[1:] > conv3[:-1]] & r_[conv3[:-1] > conv3[1:], True]]
a44230b7   Thomas Fitoussi   Improve approxima...
82
83
84
85
86
87
88

   ax.set_xscale('log')
   ax.set_yscale('log')
   ax.grid(b=True,which='major')
   ax.legend(loc="best")
   ax.set_xlabel("Time delay [s]")
   ax.set_ylabel("cross correlation [arbitrary units]")
7030f150   Thomas Fitoussi   Full reorganisati...
89
90

   show()
f4246390   Thomas Fitoussi   Improve angular d...
91

a44230b7   Thomas Fitoussi   Improve approxima...
92
93
94
95
96
97
98
99
100
101
def delay_vs_theta(theta,delay,nbBins=100):
   cond= (theta!=0)
   theta=log10(theta[cond])
   dt,dtheta=histogram(theta,nbBins,weights=delay[cond])
   dN,dtheta=histogram(theta,nbBins)
   dtheta=10**dtheta
   thetacenter=(dtheta[1:nbBins+1]+dtheta[0:nbBins])/2
   dt=dt/dN
   return dt, thetacenter

f4246390   Thomas Fitoussi   Improve angular d...
102
103
104
105
106
107
108
109
def drawDelay_vs_angle(fileId):
   '''
      Plot angle versus time delay, generation by generation
      Input: directory name
      Output: graph of angle versus time delay, generation by generation
   '''
   fig = figure()
   ax = fig.add_subplot(111)
fb95bd3d   Thomas Fitoussi   Add time delay ve...
110
111
   nbBins = 100

f5a75019   Thomas Fitoussi   Rewrite Analysis....
112
113
   generation, theta = ReadExtraFile(fileId,cols=[3,4])
   theta *= degre
6633967c   Thomas Fitoussi   Correction on the...
114
115
   energy = ReadEnergy(fileId)
   delay  = ReadTime(fileId)
65135318   Thomas Fitoussi   Reset modules
116

a44230b7   Thomas Fitoussi   Improve approxima...
117
118
   Emin = [1e-3,1e0,1e3] #GeV
   Emax = [1e0,1e3,1e5] #GeV
f5a75019   Thomas Fitoussi   Rewrite Analysis....
119
120
121
122
123
124
125
126
127
128
129
130
131

   for gen in list(set(generation)):
      cond = generation==gen
      ax.plot(theta[cond],delay[cond],",",label="gen = %.0f"%gen)
   #for n in arange(0,3,1):
   #   cond= (energy>Emin[n]) & (energy<Emax[n])
   #   if Emin[n]==1e-3:
   #      label="MeV band"
   #   if Emin[n]==1e0:
   #      label="GeV band"
   #   if Emin[n]==1e3:
   #      label="TeV band"
   #   ax.plot(theta[cond],delay[cond],".",label=label)
65135318   Thomas Fitoussi   Reset modules
132

a44230b7   Thomas Fitoussi   Improve approxima...
133
134
   dt,angle=delay_vs_theta(theta,delay,nbBins)
   ax.plot(angle,dt,color='m',linestyle="steps-mid",linewidth=2)
fb95bd3d   Thomas Fitoussi   Add time delay ve...
135

65135318   Thomas Fitoussi   Reset modules
136
137
138
139
140
   thmin = 1.e-8
   thmax = pi/2.
   nth = 1000
   th = thmin*(thmax/thmin)**(arange(nth)/(nth-1.))
   delay_fit = Analytic_delay_vs_theta(th,fileId)
a44230b7   Thomas Fitoussi   Improve approxima...
141
   ax.plot(th*degre,delay_fit,'--k',linewidth=2)
fb95bd3d   Thomas Fitoussi   Add time delay ve...
142
143
144
145
146

   ax.set_xscale('log')
   ax.set_yscale('log')
   ax.grid(b=True,which='major')
   ax.legend(loc="best")
a44230b7   Thomas Fitoussi   Improve approxima...
147
   ax.set_xlabel("$\\theta$ [deg]")
fb95bd3d   Thomas Fitoussi   Add time delay ve...
148
149
150
   ax.set_ylabel("Time delay [s]")

   show()