Commit 617da5ee0127fe61d21092b15025b3eb7f6bf305
1 parent
907508c6
Add csinfo script
Showing
2 changed files
with
184 additions
and
1 deletions
scripts/Makefile.am
| @@ -12,7 +12,7 @@ pythonscripts = cspull.py cssens.py cstsdist.py cscaldb.py csresmap.py cshessobs | @@ -12,7 +12,7 @@ pythonscripts = cspull.py cssens.py cstsdist.py cscaldb.py csresmap.py cshessobs | ||
| 12 | # Scripts for which symbolic links will be created in bindir | 12 | # Scripts for which symbolic links will be created in bindir |
| 13 | linkscripts = cspull cssens cstsdist cscaldb csresmap \ | 13 | linkscripts = cspull cssens cstsdist cscaldb csresmap \ |
| 14 | csobsdef csspec cshessobs cslightcrv \ | 14 | csobsdef csspec cshessobs cslightcrv \ |
| 15 | - lsmodel | 15 | + lsmodel csinfo |
| 16 | 16 | ||
| 17 | # Distribute scripts | 17 | # Distribute scripts |
| 18 | EXTRA_DIST = $(binscripts) $(pythonscripts) | 18 | EXTRA_DIST = $(binscripts) $(pythonscripts) |
scripts/csinfo.py
0 → 100755
| 1 | +#!/usr/bin/env python | ||
| 2 | +# ========================================================================== | ||
| 3 | +# Print info about Gammalib / ctools to the console. | ||
| 4 | +# | ||
| 5 | +# Copyright (C) 2015 Christoph Deil | ||
| 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 os | ||
| 23 | +import subprocess | ||
| 24 | + | ||
| 25 | +# Copied and edited a bit from the `doc/source/reference_manual/index.rst` file | ||
| 26 | +# The list there and here needs to be manually updated and kept in sync. | ||
| 27 | +CTOOL_LIST = """ | ||
| 28 | + ctbin Generates counts cube | ||
| 29 | + ctbkgcube Generates background cube | ||
| 30 | + ctbutterfly Compute butterfly | ||
| 31 | + ctcubemask Filter counts cube | ||
| 32 | + ctexpcube Generates exposure cube | ||
| 33 | + ctlike Performs maximum likelihood fitting | ||
| 34 | + ctmodel Computes model counts cube | ||
| 35 | + ctobssim Simulate CTA observations | ||
| 36 | + ctpsfcube Generates point spread function cube | ||
| 37 | + ctselect Selects event data | ||
| 38 | + ctskymap Generates sky map | ||
| 39 | + cttsmap Generates Test Statistics map | ||
| 40 | + ctulimit Calculates upper limit | ||
| 41 | + cterror Calculates likelihood profile errors | ||
| 42 | + | ||
| 43 | + cscaldb Lists available instrument response functions | ||
| 44 | + csobsdef Generates observation definition file | ||
| 45 | + cslightcrv Computes lightcurve | ||
| 46 | + cspull Generates pull distribution | ||
| 47 | + cssens Computes CTA sensitivity | ||
| 48 | + csspec Computes spectral points | ||
| 49 | + csresmap Generates residual map | ||
| 50 | + cstsdist Generates TS distribution | ||
| 51 | +""" | ||
| 52 | + | ||
| 53 | +def log(text, end='\n'): | ||
| 54 | + """Print text to console. | ||
| 55 | + | ||
| 56 | + Workaround to have an `end=''` option on old Pythons: | ||
| 57 | + http://stackoverflow.com/a/493399/498873 | ||
| 58 | + """ | ||
| 59 | + sys.stdout.write(text + end) | ||
| 60 | + | ||
| 61 | + | ||
| 62 | +def print_ctools_list(): | ||
| 63 | + """Print available ctools with one-line description to the console. | ||
| 64 | + """ | ||
| 65 | + print('\nAvailable ctools:') | ||
| 66 | + print(CTOOL_LIST) | ||
| 67 | + | ||
| 68 | + | ||
| 69 | +def csinfo_setup_check(): | ||
| 70 | + print('\nGammalib / ctools setup info:\n') | ||
| 71 | + | ||
| 72 | + all_ok = True | ||
| 73 | + | ||
| 74 | + log(' GAMMALIB environment variable ... ', end='') | ||
| 75 | + try: | ||
| 76 | + gammalib_env = os.environ['GAMMALIB'] | ||
| 77 | + print('ok') | ||
| 78 | + except KeyError: | ||
| 79 | + print('NOT OK') | ||
| 80 | + all_ok = False | ||
| 81 | + | ||
| 82 | + log(' CTOOLS environment variable ... ', end='') | ||
| 83 | + try: | ||
| 84 | + ctools_env = os.environ['CTOOLS'] | ||
| 85 | + print('ok') | ||
| 86 | + except KeyError: | ||
| 87 | + print('NOT_OK') | ||
| 88 | + all_ok = False | ||
| 89 | + | ||
| 90 | + log(' gammalib Python import .......... ', end='') | ||
| 91 | + try: | ||
| 92 | + import gammalib | ||
| 93 | + print('ok') | ||
| 94 | + except: | ||
| 95 | + print('NOT OK') | ||
| 96 | + all_ok = False | ||
| 97 | + | ||
| 98 | + log(' ctools Python import .......... ', end='') | ||
| 99 | + try: | ||
| 100 | + import ctools | ||
| 101 | + print('ok') | ||
| 102 | + except: | ||
| 103 | + print('NOT OK') | ||
| 104 | + all_ok = False | ||
| 105 | + | ||
| 106 | + if all_ok: | ||
| 107 | + print('\n Your Gammalib / ctools setup is OK.\n') | ||
| 108 | + else: | ||
| 109 | + print('\n WARNING: Your Gammalib / ctools setup is NOT OK!\n') | ||
| 110 | + print(' - Did you source gammalib-init.sh ?') | ||
| 111 | + print(' - Did you source gammalib-ctools.sh ?') | ||
| 112 | + print(' - Are you using the correct Python ?') | ||
| 113 | + | ||
| 114 | + print() | ||
| 115 | + | ||
| 116 | +def csinfo_setup_info(): | ||
| 117 | + | ||
| 118 | + log(' CTOOLS environment variable ... ', end='') | ||
| 119 | + try: | ||
| 120 | + ctools_env = os.environ['CTOOLS'] | ||
| 121 | + print(' $CTOOLS = {0}'.format(ctools_env)) | ||
| 122 | + except KeyError: | ||
| 123 | + print('WARNING: $CTOOLS environment variable not set. Did you source ctools-init.sh?') | ||
| 124 | + all_ok = False | ||
| 125 | + | ||
| 126 | + try: | ||
| 127 | + import gammalib | ||
| 128 | + print(' Python `gammalib` path: {0}'.format(gammalib.__path__[0])) | ||
| 129 | + except: | ||
| 130 | + print('WARNING: Python `gammalib` import failed. Are you using the right Python?') | ||
| 131 | + | ||
| 132 | + try: | ||
| 133 | + import ctools | ||
| 134 | + print(' Python `ctools` path: {0}'.format(ctools.__path__[0])) | ||
| 135 | + except: | ||
| 136 | + print('WARNING: Python `ctools` import failed. Are you using the right Python?') | ||
| 137 | + | ||
| 138 | + print(' GAMMALIB CFLAGS: {0}'.format(get_pkg_config_info('cflags', 'gammalib'))) | ||
| 139 | + print(' CTOOLS CFLAGS: {0}'.format(get_pkg_config_info('cflags', 'ctools'))) | ||
| 140 | + | ||
| 141 | + print() | ||
| 142 | + | ||
| 143 | + | ||
| 144 | +def get_pkg_config_info(info, library): | ||
| 145 | + cmd = 'pkg-config --{0} {1}'.format(info, library) | ||
| 146 | + out = subprocess.getoutput(cmd) | ||
| 147 | + if 'was not found' in out: | ||
| 148 | + return 'Not available' | ||
| 149 | + else: | ||
| 150 | + return out | ||
| 151 | + | ||
| 152 | + | ||
| 153 | +def csinfo_print_help(): | ||
| 154 | + print('\nPrint info about Gammalib and ctools to the console.\n') | ||
| 155 | + print('Available commands:') | ||
| 156 | + print(' csinfo tools List available ctools') | ||
| 157 | + print(' csinfo setup-check Check Gammalib / ctools setup') | ||
| 158 | + print(' csinfo setup-info Print Gammalib / ctools setup info') | ||
| 159 | + print() | ||
| 160 | + | ||
| 161 | + | ||
| 162 | +def csinfo(argv): | ||
| 163 | + """Print info about Gammalib / ctools to the console. | ||
| 164 | + """ | ||
| 165 | + if len(argv) <= 1: | ||
| 166 | + csinfo_print_help() | ||
| 167 | + sys.exit(0) | ||
| 168 | + | ||
| 169 | + cmd = argv[1] | ||
| 170 | + if cmd == 'list-tools': | ||
| 171 | + csinfo_list_tools() | ||
| 172 | + elif cmd == 'setup-check': | ||
| 173 | + csinfo_setup_check() | ||
| 174 | + elif cmd == 'setup-info': | ||
| 175 | + csinfo_setup_info() | ||
| 176 | + else: | ||
| 177 | + print('\nERROR: invalid command: `{0}`'.format(cmd)) | ||
| 178 | + csinfo_print_help() | ||
| 179 | + sys.exit(-1) | ||
| 180 | + | ||
| 181 | + | ||
| 182 | +if __name__ == '__main__': | ||
| 183 | + csinfo(sys.argv) |