Commit aad290d7073c24042ab2ef7185e82042acd96def

Authored by Jürgen Knödlseder
1 parent 5cec33c1

Adapt filepath and make Python 3 compatible.

inst/cta/test/test_response_table.py
1 #! /usr/bin/env python 1 #! /usr/bin/env python
2 - 2 +# ==========================================================================
  3 +# This script displays the CTA PSF using a response table.
  4 +#
  5 +# Requires:
  6 +# - matplotlib
  7 +# - numpy
  8 +#
  9 +# Copyright (C) 2012 Juergen Knoedlseder
  10 +#
  11 +# This program is free software: you can redistribute it and/or modify
  12 +# it under the terms of the GNU General Public License as published by
  13 +# the Free Software Foundation, either version 3 of the License, or
  14 +# (at your option) any later version.
  15 +#
  16 +# This program is distributed in the hope that it will be useful,
  17 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  18 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19 +# GNU General Public License for more details.
  20 +#
  21 +# You should have received a copy of the GNU General Public License
  22 +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  23 +#
  24 +# ==========================================================================
3 from gammalib import * 25 from gammalib import *
4 from math import * 26 from math import *
5 import os 27 import os
6 import sys 28 import sys
  29 +import numpy as np
  30 +import matplotlib.pyplot as plt
  31 +import matplotlib.cm as cm
7 32
8 33
9 -# ======================= #  
10 -# Test CTA response table #  
11 -# ======================= #  
12 -def cta_response_table(index=1): 34 +# ====================== #
  35 +# Display response table #
  36 +# ====================== #
  37 +def display_response_table(index=1):
13 """ 38 """
  39 + Display CTA response table.
  40 +
  41 + Keywords:
  42 + index - Response parameter to display (starting from 0)
14 """ 43 """
15 - # Import modules for plotting  
16 - import numpy as np  
17 - import matplotlib.pyplot as plt  
18 - import matplotlib.cm as cm  
19 -  
20 - #  
21 - t = GCTAResponseTable()  
22 - print t  
23 -  
24 - #  
25 - fits = GFits('/Users/jurgen/cta/dc1/psf/psf.fits') 44 + # Load PSF into response table
  45 + fits = GFits('../caldb/data/cta/e/bcf/000001/irf_test.fits')
26 table = fits.table('POINT SPREAD FUNCTION') 46 table = fits.table('POINT SPREAD FUNCTION')
27 - psf = GCTAResponseTable(table)  
28 - print psf 47 + psf = GCTAResponseTable(table)
  48 + print(psf)
29 49
30 # Set limits for 2D plot 50 # Set limits for 2D plot
31 - emin = 0.05  
32 - emax = 80.0 51 + emin = 0.05
  52 + emax = 80.0
33 thetamin = 0.0 53 thetamin = 0.0
34 thetamax = 2.5 54 thetamax = 2.5
35 - neng = 100  
36 - ntheta = 100  
37 - logemin = log10(emin)  
38 - logemax = log10(emax)  
39 - dlogeng = (logemax - logemin) / (neng - 1)  
40 - dtheta = (thetamax - thetamin) / (ntheta - 1) 55 + neng = 100
  56 + ntheta = 100
  57 + logemin = log10(emin)
  58 + logemax = log10(emax)
  59 + dlogeng = (logemax - logemin) / (neng - 1)
  60 + dtheta = (thetamax - thetamin) / (ntheta - 1)
41 61
42 # Fill image. Note that X and Y are inversed with respect to 62 # Fill image. Note that X and Y are inversed with respect to
43 # traditional orientation 63 # traditional orientation
@@ -45,9 +65,8 @@ def cta_response_table(index=1): @@ -45,9 +65,8 @@ def cta_response_table(index=1):
45 for ieng in range(neng): 65 for ieng in range(neng):
46 eng = pow(10.0, logemin + ieng * dlogeng) 66 eng = pow(10.0, logemin + ieng * dlogeng)
47 for itheta in range(ntheta): 67 for itheta in range(ntheta):
48 - theta = thetamin + itheta * dtheta  
49 - pars = psf(eng, theta)  
50 - # print eng, theta, pars[index] 68 + theta = thetamin + itheta * dtheta
  69 + pars = psf(eng, theta)
51 image[itheta, ieng] = pars[index] 70 image[itheta, ieng] = pars[index]
52 71
53 def format_coord(x, y): 72 def format_coord(x, y):
@@ -61,7 +80,7 @@ def cta_response_table(index=1): @@ -61,7 +80,7 @@ def cta_response_table(index=1):
61 80
62 # Show image 81 # Show image
63 fig = plt.figure() 82 fig = plt.figure()
64 - ax = fig.add_subplot(111) 83 + ax = fig.add_subplot(111)
65 ax.imshow(image, cmap=cm.jet, interpolation='nearest', origin='lower') 84 ax.imshow(image, cmap=cm.jet, interpolation='nearest', origin='lower')
66 ax.format_coord = format_coord 85 ax.format_coord = format_coord
67 plt.show() 86 plt.show()
@@ -78,14 +97,10 @@ if __name__ == &#39;__main__&#39;: @@ -78,14 +97,10 @@ if __name__ == &#39;__main__&#39;:
78 Perform testing. 97 Perform testing.
79 """ 98 """
80 # Dump result 99 # Dump result
81 - print  
82 - print "********************************"  
83 - print "* CTA Python interface testing *"  
84 - print "********************************"  
85 -  
86 - # Initialise success counter  
87 - tests = 0  
88 - success = 0 100 + print("")
  101 + print("******************************")
  102 + print("* Display CTA response table *")
  103 + print("******************************")
89 104
90 - # Perform tests  
91 - cta_response_table() 105 + # Display response table
  106 + display_response_table()