Commit 7588d2f149507b9a89a384bdccdd15e8dcd9133a

Authored by Simon Bonnefoy
Committed by Jürgen Knödlseder
1 parent ae09a3d4

new example to extract significance from the output file of ctfindvar

examples/show_variability_evolution.py 0 → 100755
  1 +#! /usr/bin/env python
  2 +# ==========================================================================
  3 +# Shows butterfly diagram created with ctbutterfly
  4 +#
  5 +# Copyright (C) 2014-2017 Simon Bonnefoy
  6 +#
  7 +# This program is free software: you can redistribute it and/or modify
  8 +# it under the terms of the GNU General Public License as published by
  9 +# the Free Software Foundation, either version 3 of the License, or
  10 +# (at your option) any later version.
  11 +#
  12 +# This program is distributed in the hope that it will be useful,
  13 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  14 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15 +# GNU General Public License for more details.
  16 +#
  17 +# You should have received a copy of the GNU General Public License
  18 +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  19 +#
  20 +# ==========================================================================
  21 +import sys
  22 +import gammalib
  23 +import cscripts
  24 +import numpy as np
  25 +try:
  26 + import matplotlib.pyplot as plt
  27 + plt.figure()
  28 + plt.close()
  29 +except (ImportError, RuntimeError):
  30 + print('This script needs the "matplotlib" module')
  31 + sys.exit()
  32 +
  33 +
  34 +# ========================== #
  35 +# Plot variability evolution #
  36 +# ========================== #
  37 +def plot_variability_evolution(filename, plotfile):
  38 + """
  39 + Plot variability evolution
  40 +
  41 + Parameters
  42 + ----------
  43 + filename : str
  44 + Name of spectrum FITS file
  45 + plotfile : str
  46 + Plot file name
  47 + """
  48 + # Read variability file
  49 + fits = gammalib.GFits(filename)
  50 + table = fits.table(1)
  51 + c_gti_start = table['TSTART']
  52 + c_gti_stop = table['TSTOP']
  53 + c_sig = table['SOURCE']
  54 +
  55 + #convert the c_sig object to list
  56 + table_c_sig=[]
  57 + table_c_gti=[]
  58 +
  59 + for k in c_sig:
  60 + table_c_sig.append(k)
  61 + for j,k in zip(c_gti_start, c_gti_stop):
  62 + table_c_gti.append(0.5*(j+k))
  63 +
  64 + #Initializing the plots
  65 + axarr=[]
  66 + f = plt.figure(figsize=(12,7))
  67 + ax1 = f.add_subplot(121)
  68 + ax2 = f.add_subplot(122)
  69 + axarr.append(ax1)
  70 + axarr.append(ax2)
  71 +
  72 + axarr[0].plot(table_c_gti, table_c_sig)
  73 + axarr[0].set_title('Significance evolution')
  74 +
  75 + axarr[1].hist(table_c_sig, 20, range=[min(table_c_sig), max(table_c_sig)], histtype='stepfilled', facecolor='g', alpha=0.75)
  76 + axarr[1].set_title('Significance distribution')
  77 +
  78 + #IT SEEMS THAT THE LOG SCALE IS NOT WORKING WELL !!!!!!
  79 + #axarr[1].set_yscale('log')
  80 +
  81 + plt.show()
  82 +
  83 +# ========================== #
  84 +# Show variability evolution #
  85 +# ========================== #
  86 +def show_variability_evolution():
  87 + """
  88 + Show variability
  89 + """
  90 + # Set usage string
  91 + usage = 'show_variability_evolution.py [-p plotfile] [file]'
  92 +
  93 + # Set default options
  94 + options = [{'option': '-p', 'value': ''}]
  95 +
  96 + # Get arguments and options from command line arguments
  97 + args, options = cscripts.ioutils.get_args_options(options, usage)
  98 +
  99 + # Extract script parameters from options
  100 + plotfile = options[0]['value']
  101 + #hdu = int(options[1]['value'])
  102 +
  103 + # Show variability evolution
  104 + plot_variability_evolution(args[0], plotfile)
  105 +
  106 + # Return
  107 + return
  108 +
  109 +
  110 +# ======================== #
  111 +# Main routine entry point #
  112 +# ======================== #
  113 +if __name__ == '__main__':
  114 +
  115 + # Show variability evolution
  116 + show_variability_evolution()
... ...