cta_make_nodes.py
3.18 KB
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
#! /usr/bin/env python
# ==========================================================================
# This Python script creates the node section of the NodeFunction using
# logarithmically spaced energy bins. The intensity scale is set to the
# HESS Crab intensity (assuming a power law).
#
# Copyright (C) 2012-2014 Juergen Knoedlseder
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# ==========================================================================
from gammalib import *
import sys
import math
# ============== #
# Write one node #
# ============== #
def write_node(file, energy, scale):
"""
Writes one node to XML file.
"""
# Convert to strings
str_energy = str(energy)
str_scale = str(scale)
# Write start tag
file.write(' <node>\n')
# Write energy
file.write(' <parameter scale="1e6" name="Energy"')
file.write(' min="'+str_energy+'" max="'+str_energy+'"')
file.write(' value="'+str_energy+'"')
file.write(' free="0"/>\n')
# Write intensity
file.write(' <parameter scale="'+str_scale+'" name="Intensity"')
file.write(' min="1e-5" max="1e5"')
file.write(' value="1.0"')
file.write(' free="1"/>\n')
# Write end tag
file.write(' </node>\n')
# Return
return
# ============ #
# Create nodes #
# ============ #
def create_nodes(emin, emax, enumbins):
"""
Create nodes (energies in TeV).
"""
# Open file
file = open("nodes.xml", "w")
# Set node boundaries
elogmin = math.log10(float(emin))
elogmax = math.log10(float(emax))
elogbin = (elogmax - elogmin)/(float(enumbins)-1.0)
# Fill arrays
for i in range(int(enumbins)):
# Compute energy
energy = math.pow(10.0, i*elogbin+elogmin)
# Compute scale (HESS Crab spectrum)
scale = 3.45e-17 * math.pow(energy, -2.63)
# Write node
write_node(file, energy, scale)
# Debug
#sys.stdout.write(energy)
#sys.stdout.write(math.pow(10.0, int(math.log10(scale))-1.0)±"\n")
# Close file
file.close()
# Return
return
# ======================= #
# Main script entry point #
# ======================= #
if __name__ == '__main__':
"""
Compute nodes.
"""
# Check command line
usage = "Usage: cta_make_nodes emin emax enumbins"
if len(sys.argv) < 3:
sys.stdout.write(usage+"\n")
sys.exit()
# Extract parameters
emin = sys.argv[1]
emax = sys.argv[2]
enumbins = sys.argv[3]
# Create nodes
create_nodes(emin, emax, enumbins)