diff --git a/cscripts/ioutils.py b/cscripts/ioutils.py index 1c08dc5..825cba0 100755 --- a/cscripts/ioutils.py +++ b/cscripts/ioutils.py @@ -90,7 +90,7 @@ def get_args_options(options, usage): # If there are no command line arguments then show usage string and # exit if len(sys.argv) < 1: - print(usage) + print('Usage: %s' % usage) sys.exit() # First possible option is element 1 @@ -116,13 +116,13 @@ def get_args_options(options, usage): try: option['value'] = str(sys.argv[i]) except: - print(usage) + print('Usage: %s' % usage) sys.exit() # ... there is no following parameter, hence write out usage # and quite else: - print(usage) + print('Usage: %s' % usage) sys.exit() # We can break now as every option should only occur once diff --git a/examples/make_spectrum.py b/examples/make_spectrum.py index 938561e..b9734c8 100755 --- a/examples/make_spectrum.py +++ b/examples/make_spectrum.py @@ -25,9 +25,9 @@ try: import matplotlib.pyplot as plt plt.figure() plt.close() - has_matplotlib = True except: - has_matplotlib = False + print('This script needs the "matplotlib" module') + sys.exit() # =============== # @@ -94,7 +94,7 @@ def make_spectrum(datadir): # ============= # # Plot spectrum # # ============= # -def plot_spectrum(spectrum, file=False): +def plot_spectrum(spectrum, plotfile): """ Plot spectrum @@ -102,8 +102,8 @@ def plot_spectrum(spectrum, file=False): ---------- spectrum : `~gammalib.GFits` Spectrum FITS file - file : bool, optional - Plot into a file + plotfile : str + Plot filename """ # Extract columns from spectrum file table = spectrum.table(1) @@ -169,8 +169,8 @@ def plot_spectrum(spectrum, file=False): plt.ylabel(r'E$^{2}$dN/dE (erg cm$^{-2}$ s$^{-1}$)') # Show spectrum or save it into file - if file: - plt.savefig('example_spectrum.eps') + if len(plotfile) > 0: + plt.savefig(plotfile) else: plt.show() @@ -183,19 +183,22 @@ def plot_spectrum(spectrum, file=False): # ======================== # if __name__ == '__main__': - # Get optional arguments - if len(sys.argv) >= 2: - datadir = sys.argv[1] - else: - datadir = 'data' - if len(sys.argv) >= 3: - file = True - else: - file = False + # Set usage string + usage = 'make_spectrum.py [-d datadir] [-p plotfile]' + + # Set default options + options = [{'option': '-d', 'value': 'data'}, + {'option': '-p', 'value': ''}] + + # Get arguments and options from command line arguments + args, options = cscripts.ioutils.get_args_options(options, usage) + + # Extract script parameters from options + datadir = options[0]['value'] + plotfile = options[1]['value'] # Generate spectrum spectrum = make_spectrum(datadir) - # If matplotlib is installed then plot spectrum - if has_matplotlib: - plot_spectrum(spectrum, file=file) + # Plot spectrum + plot_spectrum(spectrum, plotfile) diff --git a/examples/show_butterfly.py b/examples/show_butterfly.py index a3539e7..33092cc 100755 --- a/examples/show_butterfly.py +++ b/examples/show_butterfly.py @@ -20,6 +20,7 @@ # ========================================================================== import sys import gammalib +import cscripts try: import matplotlib.pyplot as plt plt.figure() @@ -29,23 +30,22 @@ except: sys.exit() -# ============ # -# Script entry # -# ============ # -if __name__ == '__main__': - - # Check for given butterfly file - if len(sys.argv) < 2: - sys.exit('Usage: show_butterfly.py butterfly.txt [file]') - - # Check if plotting in file is requested - plotfile = '' - if len(sys.argv) == 3: - plotfile = sys.argv[2] +# ============== # +# Plot butterfly # +# ============== # +def plot_butterfly(filename, plotfile): + """ + Plot butterfly diagram - # Read given butterfly file - filename = sys.argv[1] - csv = gammalib.GCsv(filename) + Parameters + ---------- + filename : str + Butterfly CSV file + plotfile : str + Plot filename + """ + # Read butterfly file + csv = gammalib.GCsv(filename) # Initialise arrays to be filled butterfly_x = [] @@ -65,15 +65,13 @@ if __name__ == '__main__': line_x.append(csv.real(row,0)) line_y.append(csv.real(row,1)) - # Loop over the rows backwards to compute the lower edge - # of the confidence band + # Loop over the rows backwards to compute the lower edge of the + # confidence band for row in range(nrows): index = nrows - 1 - row butterfly_x.append(csv.real(index,0)) - low_error = csv.real(index,3) - if low_error < 1e-26: - low_error = 1e-26 - butterfly_y.append(low_error) + low_error = max(csv.real(index,3), 1e-26) + butterfly_y.append(low_error) # Plot the butterfly and spectral line plt.figure() @@ -90,3 +88,27 @@ if __name__ == '__main__': plt.savefig(plotfile) else: plt.show() + + # Return + return + + +# ======================== # +# Main routine entry point # +# ======================== # +if __name__ == '__main__': + + # Set usage string + usage = 'show_butterfly.py [-p plotfile] file' + + # Set default options + options = [{'option': '-p', 'value': ''}] + + # Get arguments and options from command line arguments + args, options = cscripts.ioutils.get_args_options(options, usage) + + # Extract script parameters from options + plotfile = options[0]['value'] + + # Plot butterfly diagram + plot_butterfly(args[0], plotfile) diff --git a/examples/show_model.py b/examples/show_model.py index 976cc7e..26d03f2 100755 --- a/examples/show_model.py +++ b/examples/show_model.py @@ -20,6 +20,7 @@ # ========================================================================== import sys import gammalib +import cscripts try: import matplotlib.pyplot as plt plt.figure() @@ -32,7 +33,7 @@ except: # ======================= # # Plot spectral component # # ======================= # -def plot_spectrum(model, emin=0.01, emax=100.0, enumbins=100, plotfile=''): +def plot_spectrum(model, plotfile, emin=0.01, emax=100.0, enumbins=100): """ Plot spectral model component @@ -40,14 +41,14 @@ def plot_spectrum(model, emin=0.01, emax=100.0, enumbins=100, plotfile=''): ---------- model : `~gammalib.GModel` Model + plotfile : str + Name of plot file emin : float, optional Minimum energy (TeV) emax : float, optional Maximum energy (TeV) enumbins : integer, optional Number of energy bins - plotfile : str, optional - Name of plot file """ # Get spectral component spectrum = model.spectral() @@ -57,18 +58,12 @@ def plot_spectrum(model, emin=0.01, emax=100.0, enumbins=100, plotfile=''): e_max = gammalib.GEnergy(emax, 'TeV') ebounds = gammalib.GEbounds(enumbins, e_min, e_max) - # Setup model plot + # Setup lists of x and y values x = [] y = [] - min = 1.0e30 - max = 0.0 for i in range(enumbins): energy = ebounds.elogmean(i) value = spectrum.eval(energy) - if value > max: - max = value - if value < min: - min = value x.append(energy.TeV()) y.append(value) @@ -80,7 +75,6 @@ def plot_spectrum(model, emin=0.01, emax=100.0, enumbins=100, plotfile=''): plt.loglog(x, y, color='red') plt.xlabel('Energy (TeV)') plt.ylabel(r'dN/dE (ph s$^{-1}$ cm$^{-2}$ MeV$^{-1}$)') - #plt.ylim([min,max]) # Show spectrum or save it into file if len(plotfile) > 0: @@ -92,31 +86,32 @@ def plot_spectrum(model, emin=0.01, emax=100.0, enumbins=100, plotfile=''): return -# ============= # -# Script entry # -# ============= # +# ======================== # +# Main routine entry point # +# ======================== # if __name__ == '__main__': - # Check for model filename - if len(sys.argv) < 2: - sys.exit('Usage: show_model.py model.xml [name] [file]') + # Set usage string + usage = 'show_model.py [-n name] [-p plotfile] file' - # Get optional model index - if len(sys.argv) >= 3: - name = sys.argv[2] - else: + # Set default options + options = [{'option': '-n', 'value': ''}, + {'option': '-p', 'value': ''}] + + # Get arguments and options from command line arguments + args, options = cscripts.ioutils.get_args_options(options, usage) + + # Extract script parameters from options + name = options[0]['value'] + plotfile = options[1]['value'] + if len(name) == 0: name = 0 - if len(sys.argv) == 4: - plotfile = sys.argv[3] - else: - plotfile = '' # Read models XML file - filename = sys.argv[1] - models = gammalib.GModels(filename) + models = gammalib.GModels(args[0]) # Extract relevant model model = models[name] # Plot spectrum - plot_spectrum(model, plotfile=plotfile) + plot_spectrum(model, plotfile) diff --git a/examples/show_obs.py b/examples/show_obs.py index c7d2953..9404e54 100755 --- a/examples/show_obs.py +++ b/examples/show_obs.py @@ -33,7 +33,7 @@ except: # ==================== # # Run csobsinfo script # # ==================== # -def run_csobsinfo(filename, ra=None, dec=None, debug=True): +def run_csobsinfo(filename, ra, dec, debug=True): """ Run csobsinfo script @@ -41,9 +41,9 @@ def run_csobsinfo(filename, ra=None, dec=None, debug=True): ---------- filename : str File name of observation definition XML file - ra : float, optional + ra : str Target Right Ascension (deg) - dec : float, optional + dec : str Target declination of pointing (deg) debug : bool, optional Switch on debugging in csobsinfo run @@ -62,9 +62,9 @@ def run_csobsinfo(filename, ra=None, dec=None, debug=True): info['debug'] = debug # Set offset - if ra != None and dec != None: - info['ra'] = ra - info['dec'] = dec + if ra != '' and dec != '': + info['ra'] = float(ra) + info['dec'] = float(dec) info['offset'] = True else: info['offset'] = False @@ -79,7 +79,7 @@ def run_csobsinfo(filename, ra=None, dec=None, debug=True): # ================ # # Plot information # # ================ # -def plot_information(info, plotfile=''): +def plot_information(info, plotfile): """ Plot information @@ -87,7 +87,7 @@ def plot_information(info, plotfile=''): ---------- info : `~cscripts.csobsinfo` csobsinfo instance - plotfile : str, optional + plotfile : str Plot filename """ # Retrieve observation info @@ -178,22 +178,24 @@ def plot_information(info, plotfile=''): # ======================== # if __name__ == '__main__': - # Print usage message if wrong number of arguments is provided - if len(sys.argv) < 2: - sys.exit('Usage: inspect_obs.py inobs.xml [ra] [dec] [file]') + # Set usage string + usage = 'show_model.py [-ra ra] [-dec dec] [-p plotfile] file' - # Extract optional parameters - plotfile = '' - ra = None - dec = None - if len(sys.argv) == 3: - plotfile = sys.argv[2] - elif len(sys.argv) == 4: - ra = float(sys.argv[2]) - dec = float(sys.argv[3]) + # Set default options + options = [{'option': '-ra', 'value': ''}, + {'option': '-dec', 'value': ''}, + {'option': '-p', 'value': ''}] + + # Get arguments and options from command line arguments + args, options = cscripts.ioutils.get_args_options(options, usage) + + # Extract script parameters from options + ra = options[0]['value'] + dec = options[1]['value'] + plotfile = options[2]['value'] # Run csobsinfo - info = run_csobsinfo(sys.argv[1], ra=ra, dec=dec) + info = run_csobsinfo(args[0], ra, dec) # Plot information - plot_information(info, plotfile=plotfile) + plot_information(info, plotfile) diff --git a/examples/show_pha.py b/examples/show_pha.py index 904ea9c..ec26885 100755 --- a/examples/show_pha.py +++ b/examples/show_pha.py @@ -1,6 +1,6 @@ #! /usr/bin/env python # ========================================================================== -# Show a spectrum in PHA format. +# Show a spectrum in PHA format # # Copyright (C) 2013-2016 Juergen Knoedlseder # @@ -21,6 +21,7 @@ import sys import math import gammalib +import cscripts try: import matplotlib.pyplot as plt plt.figure() @@ -33,7 +34,7 @@ except: # ================= # # Show PHA spectrum # # ================= # -def show_pha(pha, plotfile=''): +def show_pha(pha, plotfile): """ Show PHA spectrum @@ -41,7 +42,7 @@ def show_pha(pha, plotfile=''): ---------- spectrum : `~gammalib.GPha` PHA file - plotfile : str, optional + plotfile : str Plot file name """ # Create figure @@ -94,22 +95,20 @@ def show_pha(pha, plotfile=''): # ======================== # if __name__ == '__main__': - # Print usage information - usage = 'Usage: show_pha.py filename [file]' - if len(sys.argv) < 2: - print(usage) - sys.exit() + # Set usage string + usage = 'show_pha.py [-p plotfile] file' - # Check if plotting in file is requested - plotfile = '' - if len(sys.argv) == 3: - plotfile = sys.argv[2] + # Set default options + options = [{'option': '-p', 'value': ''}] - # Extract parameters - filename = sys.argv[1] + # Get arguments and options from command line arguments + args, options = cscripts.ioutils.get_args_options(options, usage) + + # Extract script parameters from options + plotfile = options[0]['value'] # Load PHA spectrum - pha = gammalib.GPha(filename) + pha = gammalib.GPha(args[0]) # Show PHA spectrum - show_pha(pha, plotfile=plotfile) + show_pha(pha, plotfile) diff --git a/examples/show_pull_evolution.py b/examples/show_pull_evolution.py index 06bace2..f0e60fe 100755 --- a/examples/show_pull_evolution.py +++ b/examples/show_pull_evolution.py @@ -20,6 +20,7 @@ # ========================================================================== import sys import csv +import cscripts try: import matplotlib.pyplot as plt plt.figure() @@ -83,26 +84,22 @@ def read_pull(filename, parname): return a -# ======================== # -# Main routine entry point # -# ======================== # -if __name__ == '__main__': - - # Print usage information - usage = 'Usage: show_pull_evolution.py filename parname [file]' - if len(sys.argv) < 3: - print(usage) - sys.exit() - - # Check if plotting in file is requested - plotfile = '' - if len(sys.argv) == 4: - plotfile = sys.argv[3] - - # Extract parameters - filename = sys.argv[1] - parname = sys.argv[2] +# =================== # +# Plot pull evolution # +# =================== # +def plot_pull_evolution(filename, parname, plotfile): + """ + Plot pull evolution + Parameters + ---------- + filename : str + Pull filename + parname : str + Parameter name + plotfile : str + Plot filename + """ # Read values from CSV file values = read_pull(filename, parname) @@ -134,3 +131,27 @@ if __name__ == '__main__': plt.savefig(plotfile) else: plt.show() + + # Return + return + + +# ======================== # +# Main routine entry point # +# ======================== # +if __name__ == '__main__': + + # Set usage string + usage = 'show_pull_evolution.py [-p plotfile] file parameter' + + # Set default options + options = [{'option': '-p', 'value': ''}] + + # Get arguments and options from command line arguments + args, options = cscripts.ioutils.get_args_options(options, usage) + + # Extract script parameters from options + plotfile = options[0]['value'] + + # Plot pull evolution + plot_pull_evolution(args[0], args[1], plotfile) diff --git a/examples/show_pull_histogram.py b/examples/show_pull_histogram.py index 7a8521e..462c7df 100755 --- a/examples/show_pull_histogram.py +++ b/examples/show_pull_histogram.py @@ -20,6 +20,7 @@ # ========================================================================== import sys import csv +import cscripts try: import matplotlib.pyplot as plt import matplotlib.mlab as mlab @@ -84,29 +85,24 @@ def read_pull(filename, parname): return a -# ======================== # -# Main routine entry point # -# ======================== # -if __name__ == '__main__': - - # Print usage information - usage = 'Usage: show_pull_histogram.py filename parname [bins] [file]' - if len(sys.argv) < 3: - print(usage) - sys.exit() - - # Extract parameters - filename = sys.argv[1] - parname = sys.argv[2] - if len(sys.argv) > 3: - nbins = int(sys.argv[3]) - else: - nbins = 50 - if len(sys.argv) == 5: - plotfile = sys.argv[4] - else: - plotfile = '' +# =================== # +# Plot pull histogram # +# =================== # +def plot_pull_histogram(filename, parname, nbins, plotfile): + """ + Plot pull histogram + Parameters + ---------- + filename : str + Pull filename + parname : str + Parameter name + nbins : int + Number of hisogram bins + plotfile : str + Plot filename + """ # Read values from CSV file values = read_pull(filename, parname) @@ -132,3 +128,29 @@ if __name__ == '__main__': plt.savefig(plotfile) else: plt.show() + + # Return + return + + +# ======================== # +# Main routine entry point # +# ======================== # +if __name__ == '__main__': + + # Set usage string + usage = 'show_pull_histogram.py [-n bins] [-p plotfile] file parameter' + + # Set default options + options = [{'option': '-n', 'value': '50'}, + {'option': '-p', 'value': ''}] + + # Get arguments and options from command line arguments + args, options = cscripts.ioutils.get_args_options(options, usage) + + # Extract script parameters from options + nbins = int(options[0]['value']) + plotfile = options[1]['value'] + + # Plot pull histogram + plot_pull_histogram(args[0], args[1], nbins, plotfile) diff --git a/examples/show_response.py b/examples/show_response.py index 39eeecb..4102bd9 100755 --- a/examples/show_response.py +++ b/examples/show_response.py @@ -21,6 +21,7 @@ import sys import math import gammalib +import cscripts try: import matplotlib.pyplot as plt plt.figure() @@ -313,22 +314,18 @@ def show_one_response(rspname, dbname, name, rootdir=None, color='r'): return -# ======================== # -# Main routine entry point # -# ======================== # -if __name__ == '__main__': - - # Print usage information - usage = 'Usage: show_response.py [file]' - if len(sys.argv) < 1: - print(usage) - sys.exit() - - # Check if plotting in file is requested - plotfile = '' - if len(sys.argv) == 2: - plotfile = sys.argv[1] +# ==================== # +# Show one sensitivity # +# ==================== # +def show_response(plotfile): + """ + Show response + Parameters + ---------- + plotfile : str + Plot filename + """ # Create figures plt.figure(1) plt.title('Effective area') @@ -338,16 +335,14 @@ if __name__ == '__main__': plt.title('Sensitivity') # Set response dictionary - rsps = [ - {'dbname': 'prod2', + rsps = [{'dbname': 'prod2', 'rspname': 'South_0.5h', 'name': 'Prod2 (30 min)', 'color': 'b'}, {'dbname': 'prod2', 'rspname': 'South_50h', 'name': 'Prod2 (50 h)', - 'color': 'r'} - ] + 'color': 'r'}] # Loop over all responses for rsp in rsps: @@ -357,10 +352,7 @@ if __name__ == '__main__': rspname = rsp['rspname'] color = rsp['color'] name = rsp['name'] - if rsp.has_key('rootdir'): - rootdir = rsp['rootdir'] - else: - rootdir = None + rootdir = rsp.setdefault('rootdir', None) # Show response show_one_response(rspname, dbname, name, rootdir=rootdir, color=color) @@ -370,3 +362,27 @@ if __name__ == '__main__': plt.savefig(plotfile) else: plt.show() + + # Return + return + + +# ======================== # +# Main routine entry point # +# ======================== # +if __name__ == '__main__': + + # Set usage string + usage = 'show_response.py [-p plotfile]' + + # Set default options + options = [{'option': '-p', 'value': ''}] + + # Get arguments and options from command line arguments + args, options = cscripts.ioutils.get_args_options(options, usage) + + # Extract script parameters from options + plotfile = options[0]['value'] + + # Show response + show_response(plotfile) diff --git a/examples/show_sensitivity.py b/examples/show_sensitivity.py index f72dcd3..c19a54e 100755 --- a/examples/show_sensitivity.py +++ b/examples/show_sensitivity.py @@ -21,6 +21,7 @@ import sys import math import gammalib +import cscripts try: import matplotlib.pyplot as plt plt.figure() @@ -96,7 +97,7 @@ def read_sensitivity(filename): # ===================== # # Show sensitivity data # # ===================== # -def show_sensitivity(sensitivity, filename, plotfile=''): +def show_sensitivity(sensitivity, filename, plotfile): """ Plot sensitivity data @@ -106,7 +107,7 @@ def show_sensitivity(sensitivity, filename, plotfile=''): Dictionary with sensitivity information filename : str Name of CSV file - plotfile : str, optional + plotfile : str Plot file name """ # Build title @@ -145,23 +146,24 @@ def show_sensitivity(sensitivity, filename, plotfile=''): # ======================== # if __name__ == '__main__': - # Print usage information - usage = 'Usage: show_sensitivity.py [filename] [file]' - if len(sys.argv) < 1: - print(usage) - sys.exit() + # Set usage string + usage = 'show_sensitivity.py [-p plotfile] [file]' - # Extract parameters - if len(sys.argv) == 1: - filename = 'sensitivity.dat' + # Set default options + options = [{'option': '-p', 'value': ''}] + + # Get arguments and options from command line arguments + args, options = cscripts.ioutils.get_args_options(options, usage) + + # Extract script parameters from options + plotfile = options[0]['value'] + if len(args) > 0: + filename = args[0] else: - filename = sys.argv[1] - plotfile = '' - if len(sys.argv) == 3: - plotfile = sys.argv[2] + filename = 'sensitivity.dat' # Read sensitivity data sensitivity = read_sensitivity(filename) # Show sensitivity data - show_sensitivity(sensitivity, filename, plotfile=plotfile) + show_sensitivity(sensitivity, filename, plotfile) diff --git a/examples/show_spectrum.py b/examples/show_spectrum.py index 970bdc1..e5f79ab 100755 --- a/examples/show_spectrum.py +++ b/examples/show_spectrum.py @@ -20,6 +20,7 @@ # ========================================================================== import sys import gammalib +import cscripts try: import matplotlib.pyplot as plt plt.figure() @@ -32,7 +33,7 @@ except: # ============= # # Show spectrum # # ============= # -def show_spectrum(filename, plotfile=''): +def show_spectrum(filename, plotfile): """ Plot sensitivity data @@ -40,7 +41,7 @@ def show_spectrum(filename, plotfile=''): ---------- filename : str Name of spectrum FITS file - plotfile : str, optional + plotfile : str Plot file name """ # Read spectrum file @@ -116,17 +117,17 @@ def show_spectrum(filename, plotfile=''): # ======================== # if __name__ == '__main__': - # Print usage information - usage = 'Usage: show_spectrum.py filename [file]' - if len(sys.argv) < 2: - print(usage) - sys.exit() + # Set usage string + usage = 'show_spectrum.py [-p plotfile] [file]' - # Extract parameters - filename = sys.argv[1] - plotfile = '' - if len(sys.argv) == 3: - plotfile = sys.argv[2] + # Set default options + options = [{'option': '-p', 'value': ''}] + + # Get arguments and options from command line arguments + args, options = cscripts.ioutils.get_args_options(options, usage) + + # Extract script parameters from options + plotfile = options[0]['value'] # Show spectrum - show_spectrum(filename, plotfile=plotfile) + show_spectrum(args[0], plotfile) diff --git a/examples/show_ts_distribution.py b/examples/show_ts_distribution.py index 1288237..da7a716 100755 --- a/examples/show_ts_distribution.py +++ b/examples/show_ts_distribution.py @@ -21,6 +21,7 @@ import sys import csv import math +import cscripts try: import matplotlib.pyplot as plt plt.figure() @@ -130,7 +131,7 @@ def erf(x): # =============================== # # Display cumulative distribution # # =============================== # -def dist_cdf(values, nbins, title, expect='', plotfile=''): +def dist_cdf(values, nbins, title, plotfile, expect=''): """ Display cumulative TS distribution and overplots expectation @@ -142,8 +143,10 @@ def dist_cdf(values, nbins, title, expect='', plotfile=''): Number of bins title : str Plot title - plotfile : str, optional + plotfile : str Plot file name + expect : str, optional + If 'show', show expectation """ # Set range and adapt number of bins. We make sure that we have a # histogram bin centred on 0 that will capture the TS=0 part. @@ -205,7 +208,7 @@ def dist_cdf(values, nbins, title, expect='', plotfile=''): # ======================================== # # Display probability density distribution # # ======================================== # -def dist_pdf(values, nbins, title, expect='', plotfile=''): +def dist_pdf(values, nbins, title, plotfile, expect=''): """ Display probability density TS distribution and overplots expectation @@ -217,8 +220,10 @@ def dist_pdf(values, nbins, title, expect='', plotfile=''): Number of bins title : str Plot title - plotfile : str, optional + plotfile : str Plot file name + expect : str, optional + If 'show', show expectation """ # Set range and adapt number of bins. We make sure that we have a # histogram bin centred on 0 that will capture the TS=0 part. @@ -279,59 +284,35 @@ def dist_pdf(values, nbins, title, expect='', plotfile=''): # ======================== # if __name__ == '__main__': - # Print usage information - usage = 'Usage: show_ts_distribution.py filename' \ - ' [-n bins] [-c column] [-t title] [-p plot] [-f file]' - if len(sys.argv) < 2: - print(usage) - sys.exit() - - # Extract parameters - filename = sys.argv[1] - - # Parameter dictionnary - pars = [{'option': '-n', 'value': 30}, - {'option': '-c', 'value': 'TS'}, - {'option': '-t', 'value': 'TS distribution'}, - {'option': '-p', 'value': 'pdf'}, - {'option': '-e', 'value': ''}, - {'option': '-f', 'value': ''}] - - # Gather parameters from command line - i = 2 - while i < len(sys.argv): - - # Search for options - for par in pars: - if sys.argv[i] == par['option']: - if len(sys.argv) > i+1: - i += 1 - try: - par['value'] = sys.argv[i] - except: - print(usage) - sys.exit() - else: - print(usage) - sys.exit() - - # Next item - i += 1 - - # Recover parameters - nbins = int(pars[0]['value']) - tsname = pars[1]['value'] - title = pars[2]['value'] - plot = pars[3]['value'] - expect = pars[4]['value'] - plotfile = pars[5]['value'] + # Set usage string + usage = ('show_ts_distribution.py [-n bins] [-c column] [-title title] ' + '[-e expect] [-t type] [-p plotfile] file') + + # Set default options + options = [{'option': '-n', 'value': 30}, + {'option': '-c', 'value': 'TS'}, + {'option': '-title', 'value': 'TS distribution'}, + {'option': '-e', 'value': ''}, + {'option': '-t', 'value': 'pdf'}, + {'option': '-p', 'value': ''}] + + # Get arguments and options from command line arguments + args, options = cscripts.ioutils.get_args_options(options, usage) + + # Extract script parameters from options + nbins = int(options[0]['value']) + tsname = options[1]['value'] + title = options[2]['value'] + expect = options[3]['value'] + disttype = options[4]['value'] + plotfile = options[5]['value'] # Read values from CSV file - values = read_ts(filename, tsname=tsname) + values = read_ts(args[0], tsname=tsname) print('%d values read.' % len(values)) # Show histogram - if plot == 'pdf': - dist_pdf(values, nbins, title, expect=expect, plotfile=plotfile) + if disttype == 'pdf': + dist_pdf(values, nbins, title, plotfile, expect=expect) else: - dist_cdf(values, nbins, title, expect=expect, plotfile=plotfile) + dist_cdf(values, nbins, title, plotfile, expect=expect) diff --git a/test/test_examples.py.in b/test/test_examples.py.in index cc148d7..1c2142d 100755 --- a/test/test_examples.py.in +++ b/test/test_examples.py.in @@ -161,8 +161,7 @@ class Test(gammalib.GPythonTestSuite): Test generate_prod3_irfs """ # Execute script - self._execute_python('generate_prod3_irfs', - args=self._datadir) + self._execute_python('generate_prod3_irfs', args=self._datadir) # Return return @@ -172,9 +171,11 @@ class Test(gammalib.GPythonTestSuite): """ Test make_spectrum """ + # Set arguments + args = '-d %s -p example_spectrum.eps' % self._datadir + # Execute script - self._execute_python('make_spectrum', - args=self._datadir+' eps') + self._execute_python('make_spectrum', args=args) # Return return @@ -184,9 +185,11 @@ class Test(gammalib.GPythonTestSuite): """ Test make_ts_distributions """ + # Set arguments + args = '-n 2 -e 0 -t 1800 -d %s' % self._datadir + # Execute script - self._execute_python('make_ts_distributions', - args='-n 2 -e 0 -t 1800 -d '+self._datadir) + self._execute_python('make_ts_distributions', args=args) # Return return @@ -262,10 +265,11 @@ class Test(gammalib.GPythonTestSuite): """ Test show_butterfly """ + # Set arguments + args = '-p example_show_butterfly.eps %s/butterfly.txt' % self._datadir + # Execute script - self._execute_python('show_butterfly', - args=self._datadir+'/butterfly.txt ' - 'example_show_butterfly.eps') + self._execute_python('show_butterfly', args=args) # Return return @@ -275,10 +279,17 @@ class Test(gammalib.GPythonTestSuite): """ Test show_model """ + # Set arguments + args = '-n Crab -p example_show_model1.eps %s/crab.xml' % self._datadir + + # Execute script + self._execute_python('show_model', args=args) + + # Set new arguments + args = '-p example_show_model2.eps %s/crab.xml' % self._datadir + # Execute script - self._execute_python('show_model', - args=self._datadir+'/crab.xml ' - 'Crab example_show_model.eps') + self._execute_python('show_model', args=args) # Return return @@ -288,10 +299,11 @@ class Test(gammalib.GPythonTestSuite): """ Test show_pha """ + # Set arguments + args = '-p example_show_pha.eps %s/pha.fits' % self._datadir + # Execute script - self._execute_python('show_pha', - args=self._datadir+'/pha.fits ' - 'example_show_pha.eps') + self._execute_python('show_pha', args=args) # Return return @@ -301,11 +313,12 @@ class Test(gammalib.GPythonTestSuite): """ Test show_pull_evolution """ + # Set arguments + args = ('-p example_show_pull_evolution.eps %s/pull.dat ' + 'Pull_Crab_Prefactor' % self._datadir) + # Execute script - self._execute_python('show_pull_evolution', - args=self._datadir+'/pull.dat ' - 'Pull_Crab_Prefactor ' - 'example_show_pull_evolution.eps') + self._execute_python('show_pull_evolution', args=args) # Return return @@ -315,11 +328,19 @@ class Test(gammalib.GPythonTestSuite): """ Test show_pull_histogram """ + # Set arguments + args = ('-p example_show_pull_histogram1.eps %s/pull.dat ' + 'Pull_Crab_Prefactor' % self._datadir) + + # Execute script + self._execute_python('show_pull_histogram', args=args) + + # Set arguments + args = ('-n 20 -p example_show_pull_histogram2.eps %s/pull.dat ' + 'Pull_Crab_Prefactor' % self._datadir) + # Execute script - self._execute_python('show_pull_histogram', - args=self._datadir+'/pull.dat ' - 'Pull_Crab_Prefactor 50 ' - 'example_show_pull_histogram.eps') + self._execute_python('show_pull_histogram', args=args) # Return return @@ -329,8 +350,11 @@ class Test(gammalib.GPythonTestSuite): """ Test show_response """ + # Set arguments + args = '-p example_show_response.eps' + # Execute script - self._execute_python('show_response', args='example_show_response.eps') + self._execute_python('show_response', args=args) # Return return @@ -340,10 +364,12 @@ class Test(gammalib.GPythonTestSuite): """ Test show_sensitivity """ + # Set arguments + args = ('-p example_show_sensitivity.eps %s/sensitivity.dat' % + self._datadir) + # Execute script - self._execute_python('show_sensitivity', - args=self._datadir+'/sensitivity.dat ' - 'example_show_sensitivity.eps') + self._execute_python('show_sensitivity', args=args) # Return return @@ -353,10 +379,11 @@ class Test(gammalib.GPythonTestSuite): """ Test show_spectrum """ + # Set arguments + args = '-p example_show_spectrum.eps %s/spectrum.fits' % self._datadir + # Execute script - self._execute_python('show_spectrum', - args=self._datadir+'/spectrum.fits ' - 'example_show_spectrum.eps') + self._execute_python('show_spectrum', args=args) # Return return @@ -366,10 +393,19 @@ class Test(gammalib.GPythonTestSuite): """ Test show_ts_distribution """ + # Set arguments + args = ('-n 100 -p example_show_ts_distribution1.eps %s/ts.dat' % + self._datadir) + + # Execute script + self._execute_python('show_ts_distribution', args=args) + + # Set arguments + args = ('-n 100 -t cdf -p example_show_ts_distribution2.eps %s/ts.dat' % + self._datadir) + # Execute script - self._execute_python('show_ts_distribution', - args=self._datadir+'/ts.dat -n 100 ' - '-f example_show_ts_distribution.eps') + self._execute_python('show_ts_distribution', args=args) # Return return @@ -379,10 +415,18 @@ class Test(gammalib.GPythonTestSuite): """ Test show_obs """ + # Set arguments + args = '-p example_show_obs1.eps %s/obs_unbinned.xml' % self._datadir + + # Execute script + self._execute_python('show_obs', args=args) + + # Set arguments + args = ('-ra 83.63 -dec 22.01 -p example_show_obs2.eps ' + '%s/obs_unbinned.xml' % self._datadir) + # Execute script - self._execute_python('show_obs', - args=self._datadir+'/obs_unbinned.xml ' - 'example_show_obs.eps') + self._execute_python('show_obs', args=args) # Return return