Commit 3b71cf337afc47c4a8a9df5a539471d8a976332f

Authored by Jürgen Knödlseder
1 parent 9b6e9d0c

Use csspec in the mak_spectrum.py example script (#1504).

cscripts/csspec.py
... ... @@ -43,7 +43,8 @@ class csspec(ctools.cscript):
43 43 self.version = "1.0.0"
44 44  
45 45 # Initialise some members
46   - self.obs = None
  46 + self.obs = None
  47 + self.fits = None
47 48  
48 49 # Initialise some members
49 50 if len(argv) > 0 and isinstance(argv[0],gammalib.GObservations):
... ... @@ -107,8 +108,8 @@ class csspec(ctools.cscript):
107 108 pars.append(gammalib.GApplicationPar("psfcube","f","a","NONE","","","PSF cube file (only needed for stacked analysis)"))
108 109 pars.append(gammalib.GApplicationPar("bkgcube","s","a","NONE","","","Background cube file (only needed for stacked analysis)"))
109 110 pars.append(gammalib.GApplicationPar("caldb","s","a","prod2","","","Calibration database"))
110   - pars.append(gammalib.GApplicationPar("edisp","b","h","no","","","Apply energy dispersion?"))
111 111 pars.append(gammalib.GApplicationPar("irf","s","a","South_50h","","","Instrument response function"))
  112 + pars.append(gammalib.GApplicationPar("edisp","b","h","no","","","Apply energy dispersion?"))
112 113 pars.append(gammalib.GApplicationPar("emin","r","h","0.1","","","Lower energy limit for spectral points(TeV)"))
113 114 pars.append(gammalib.GApplicationPar("emax","r","h","100.0","","","Upper energy limit for spectral points(TeV)"))
114 115 pars.append(gammalib.GApplicationPar("enumbins","i","a","20","","","Number of spectral points"))
... ... @@ -198,6 +199,13 @@ class csspec(ctools.cscript):
198 199 # Return
199 200 return
200 201  
  202 + def spectrum(self):
  203 + """
  204 + Return spectrum as FITS object.
  205 + """
  206 + # Return
  207 + return self.fits
  208 +
201 209 def execute(self):
202 210 """
203 211 Execute the script.
... ...
examples/make_spectrum.py
... ... @@ -2,7 +2,7 @@
2 2 # ==========================================================================
3 3 # This script shows how to make a spectrum using obsutils.
4 4 #
5   -# Copyright (C) 2014 Juergen Knoedlseder
  5 +# Copyright (C) 2014-2015 Juergen Knoedlseder
6 6 #
7 7 # This program is free software: you can redistribute it and/or modify
8 8 # it under the terms of the GNU General Public License as published by
... ... @@ -18,9 +18,9 @@
18 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19 #
20 20 # ==========================================================================
21   -import gammalib
  21 +#import gammalib
22 22 import ctools
23   -from cscripts import obsutils
  23 +import cscripts
24 24 try:
25 25 import matplotlib.pyplot as plt
26 26 has_matplotlib = True
... ... @@ -46,6 +46,7 @@ def make_spectrum():
46 46 tstop = 1800.0
47 47 emin = 0.1
48 48 emax = 100.0
  49 + enumbins = 10
49 50  
50 51 # Simulate events
51 52 sim = ctools.ctobssim()
... ... @@ -62,14 +63,28 @@ def make_spectrum():
62 63 sim.run()
63 64  
64 65 # Generate an energy binning
65   - e_min = gammalib.GEnergy(emin, "TeV")
66   - e_max = gammalib.GEnergy(emax, "TeV")
67   - ebounds = gammalib.GEbounds(10, e_min, e_max)
  66 + #e_min = gammalib.GEnergy(emin, "TeV")
  67 + #e_max = gammalib.GEnergy(emax, "TeV")
  68 + #ebounds = gammalib.GEbounds(10, e_min, e_max)
68 69  
69   - # Generate spectral points
70   - spectrum = obsutils.spectrum(sim.obs(), "Crab", ebounds)
  70 + # Setup csspec run
  71 + spec = cscripts.csspec(sim.obs())
  72 + spec["srcname"] = "Crab"
  73 + spec["outfile"] = "spectrum.fits"
  74 + spec["expcube"] = "NONE"
  75 + spec["psfcube"] = "NONE"
  76 + spec["bkgcube"] = "NONE"
  77 + spec["edisp"] = False
  78 + spec["emin"] = emin
  79 + spec["emax"] = emax
  80 + spec["enumbins"] = enumbins
  81 + spec["binned"] = False
  82 + spec.run()
71 83  
72   - # Return spectrum
  84 + # Get copy of spectrum
  85 + spectrum = spec.spectrum().copy()
  86 +
  87 + # Return
73 88 return spectrum
74 89  
75 90  
... ... @@ -80,21 +95,66 @@ def plot_spectrum(spectrum):
80 95 """
81 96 Plot spectrum.
82 97 """
  98 + # Read spectrum file
  99 + table = spectrum.table(1)
  100 + c_energy = table["Energy"]
  101 + c_ed = table["ed_Energy"]
  102 + c_eu = table["eu_Energy"]
  103 + c_flux = table["Flux"]
  104 + c_eflux = table["e_Flux"]
  105 + c_ts = table["TS"]
  106 + c_upper = table["UpperLimit"]
  107 +
  108 + # Initialise arrays to be filled
  109 + energies = []
  110 + flux = []
  111 + ed_engs = []
  112 + eu_engs = []
  113 + e_flux = []
  114 + ul_energies = []
  115 + ul_ed_engs = []
  116 + ul_eu_engs = []
  117 + ul_flux = []
  118 +
  119 + # Loop over rows of the file
  120 + nrows = table.nrows()
  121 + for row in range(nrows):
  122 +
  123 + # Get TS
  124 + ts = c_ts.real(row)
  125 + flx = c_flux.real(row)
  126 + e_flx = c_eflux.real(row)
  127 +
  128 + # Switch
  129 + if ts > 9.0 and e_flx < flx:
  130 +
  131 + # Add information
  132 + energies.append(c_energy.real(row))
  133 + flux.append(c_flux.real(row))
  134 + ed_engs.append(c_ed.real(row))
  135 + eu_engs.append(c_eu.real(row))
  136 + e_flux.append(c_eflux.real(row))
  137 +
  138 + #
  139 + else:
  140 +
  141 + # Add information
  142 + ul_energies.append(c_energy.real(row))
  143 + ul_flux.append(c_upper.real(row))
  144 + ul_ed_engs.append(c_ed.real(row))
  145 + ul_eu_engs.append(c_eu.real(row))
  146 +
83 147 # Create figure
84   - plt.figure(1)
  148 + plt.figure()
85 149 plt.title("Crab spectrum")
86 150  
87   - # Plot spectrum
88   - plt.loglog(spectrum['energy']['value'],
89   - spectrum['flux']['value'], 'ro', label='Crab')
90   - plt.errorbar(spectrum['energy']['value'],
91   - spectrum['flux']['value'],
92   - spectrum['flux']['ed_value'], ecolor='r')
93   -
94   - # Put labels
95   - plt.xlabel("Energy ("+spectrum['energy']['unit']+")")
96   - plt.ylabel("Flux ("+spectrum['flux']['unit']+")")
97   - plt.legend(loc="lower left")
  151 + # Plot the spectrum
  152 + plt.loglog()
  153 + plt.grid()
  154 + plt.errorbar(energies, flux, yerr=e_flux, xerr=[ed_engs, eu_engs], fmt='ro')
  155 + plt.errorbar(ul_energies, ul_flux, xerr=[ul_ed_engs, ul_eu_engs], yerr=1.0e-11, uplims=True, fmt='ro')
  156 + plt.xlabel("Energy (TeV)")
  157 + plt.ylabel(r"dN/dE (erg cm$^{-2}$ s$^{-1}$)")
98 158  
99 159 # Show spectrum
100 160 plt.show()
... ...