csinfo.py
11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#!/usr/bin/env python
# ==========================================================================
# Print info about Gammalib / ctools to the console
#
# Copyright (C) 2015-2021 Christoph Deil
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# ==========================================================================
import sys
import os
# List of available ctools. The list was copied from
# `doc/source/users/reference_manual/index.rst`
# and needs to be manually updated and kept in sync
CTOOL_LIST = """
ctbin Generates counts cube
ctbkgcube Generates background cube
ctbutterfly Compute butterfly
ctcubemask Filter counts cube
ctedispcube Generates energy dispersion cube
cterror Calculates likelihood profile errors
ctexpcube Generates exposure cube
ctfindvar Search for source variability
ctlike Performs maximum likelihood fitting
ctmapcube Generates a map cube
ctmodel Computes model counts cube
ctobssim Simulate CTA observations
ctphase Computes the phase of each event
ctprob Computes event probability for a given model
ctpsfcube Generates point spread function cube
ctselect Selects event data
ctskymap Generates sky map
cttsmap Generates Test Statistics map
ctulimit Calculates upper limit
"""
# List of available cscripts. The list was copied from
# `doc/source/users/reference_manual/index.rst`
# and needs to be manually updated and kept in sync
CSCRIPT_LIST = """
csbkgmodel Generates background model for 3D analysis
cscaldb Lists available instrument response functions
csebins Generates energy boundaries for stacked analysis
cslightcrv Computes lightcurve
csinfo Checks ctools and GammaLib installations
csmodelinfo Shows model container content
csmodelmerge Merges several model containers into one file
csmodelselect Select models from model definition file
csmodelsois Generate map cube from subset of models
csobsdef Generates observation definition file
csobsinfo Shows observation container content
csobsselect Select observations from observation definition file
csphagen Generates PHA, ARF, RMF files based on source/background regions
csphasecrv Computes phase curve
cspull Generates pull distribution
csresmap Generates residual map
csresspec Generates residual spectrum
csscs Performs spectral component separation
cssens Computes CTA sensitivity
csspec Computes spectral points
cssrcdetect Detects sources in sky map
cstsdist Generates TS distribution
cstsmapmerge Merges slices from Test Statistic map computations
cstsmapsplit Creates commands to split the Test Statistic map computations
csviscube Computes visibility cube\n
csobs2caldb Creates a caldb entry from an input observation
csroot2caldb Creates a caldb entry from a ROOT performance file
csiactdata Shows information about IACT data available on the user machine
csiactobs Generates observation definition file for IACT data from observation IDs
csfindobs Generates a list of IACT observation IDs
csiactcopy Copies IACT data from one location to another
"""
# ============================================================ #
# Workaround for command output on old and new Python versions #
# ============================================================ #
def get_command_output(cmd):
"""
Utility function to get command output on old and new Python versions
Parameters
----------
cmd : str
Command string
Returns
-------
output : str
Command output
"""
# Try to import getoutput from the "subprocess" module, alternatively get
# it from the "commands" module
try:
from subprocess import getoutput
except ImportError:
from commands import getoutput
# Return command output
return getoutput(cmd)
# ====================== #
# Print list information #
# ====================== #
def csinfo_list_tools():
"""
Print available ctools with one-line description to the console
"""
# Print list of available ctools
print('\nAvailable ctools:')
print(CTOOL_LIST)
# Print list of available cscripts
print('Available cscripts:')
print(CSCRIPT_LIST)
# Return
return
# ======================= #
# Print check information #
# ======================= #
def csinfo_setup_check():
"""
Print check information
"""
# Print header
print('\nGammalib / ctools setup check:\n')
# Check if the "GAMMALIB" environment variable is set
sys.stdout.write(' GAMMALIB environment variable ... ')
gammalib_environ = 'GAMMALIB' in os.environ
if gammalib_environ:
print('ok')
else:
print('NOT OK')
# Check if the "CTOOLS" environment variable is set
sys.stdout.write(' CTOOLS environment variable ... ')
ctools_environ = 'CTOOLS' in os.environ
if ctools_environ:
print('ok')
else:
print('NOT OK')
# Check if gammalib Python module import works
sys.stdout.write(' gammalib Python import .......... ')
try:
import gammalib
print('ok')
gammalib_python = True
except:
print('NOT OK')
gammalib_python = False
# Check if ctools Python module import works
sys.stdout.write(' ctools Python import .......... ')
try:
import ctools
print('ok')
ctools_python = True
except:
print('NOT OK')
ctools_python = False
# Check if cscripts Python module import works
sys.stdout.write(' cscripts Python import .......... ')
try:
import cscripts
print('ok')
cscripts_python = True
except:
print('NOT OK')
cscripts_python = False
# Set flag that everything is okay
all_ok = gammalib_environ and ctools_environ and gammalib_python and \
ctools_python and cscripts_python
# Signal if everything is okay, or notify about the problems that have
# been encountered
if all_ok:
print('\n ===> Your Gammalib / ctools setup is OK.')
else:
print('\n ===> WARNING: Your Gammalib / ctools setup is NOT OK!\n')
if not gammalib_environ:
print(' - Have you set the GAMMALIB environment variable?')
if not ctools_environ:
print(' - Have you set the CTOOLS environment variable?')
if not gammalib_python or not ctools_python or not cscripts_python:
print(' - Did you source gammalib-init.sh?')
print(' - Did you source ctools-init.sh ?')
print(' - Are you using the correct Python ?')
print('')
print(' Gammalib: http://cta.irap.omp.eu/gammalib/')
print(' ctools: http://cta.irap.omp.eu/ctools/')
print('')
# Return
return
# ======================= #
# Print setup information #
# ======================= #
def csinfo_setup_info():
"""
Print setup information
"""
# Try importing GammaLib, and flag if this was unsuccessful
try:
import gammalib
gammalib_path = gammalib.__path__[0]
gammalib_version = gammalib.__version__
except ImportError:
gammalib_path = 'Not found'
gammalib_version = 'Unknown'
# Try importing ctools, and flag if this was unsuccessful
try:
import ctools
ctools_path = ctools.__path__[0]
ctools_version = ctools.__version__
except ImportError:
ctools_path = 'Not found'
ctools_version = 'Unknown'
# Try importing cscripts, and flag if this was unsuccessful
try:
import cscripts
cscripts_path = cscripts.__path__[0]
cscripts_version = cscripts.__version__
except ImportError:
cscripts_path = 'Not found'
cscripts_version = 'Unknown'
# Get GAMMALIB and CTOOLS environment variables
gammalib_env = os.environ.get('GAMMALIB', 'Not found')
ctools_env = os.environ.get('CTOOLS', 'Not found')
# Print setup information
print('\nGammalib / ctools setup info:\n')
print(' Gammalib version ................ %s' % gammalib_version)
print(' ctools version ................ %s' % ctools_version)
print(' cscripts version ................ %s' % cscripts_version)
print(' $GAMMALIB environment variable ... %s' % gammalib_env)
print(' $CTOOLS environment variable ... %s' % ctools_env)
print(' Python executable ................ %s' % sys.executable)
print(' gammalib Python module .......... %s' % gammalib_path)
print(' ctools Python module .......... %s' % ctools_path)
print(' cscripts Python module .......... %s' % cscripts_path)
print(' GAMMALIB CFLAGS ................. %s' % get_pkg_config_info('cflags', 'gammalib'))
print(' CTOOLS CFLAGS ................. %s' % get_pkg_config_info('cflags', 'ctools'))
print(' GAMMALIB LIBS ................. %s' % get_pkg_config_info('libs', 'gammalib'))
print(' CTOOLS LIBS ................. %s' % get_pkg_config_info('libs', 'ctools'))
print('')
# Return
return
# =============================== #
# Get information from pkg-config #
# =============================== #
def get_pkg_config_info(info, library):
"""
Get information from pkg-config
Parameters
----------
info : str
Information to extract
library : str
Library name for which information is to be extracted
Returns
-------
out : str
Information string, 'Not available' if pkg-config is not installed
"""
# Set pkg-config command
cmd = 'pkg-config --%s %s' % (info, library)
# Execute command
out = get_command_output(cmd)
# If the command returns "not found" then pkg-config is not available
if 'not found' in out:
out = 'Not available'
# Return output
return out
# ============================ #
# Print help text into console #
# ============================ #
def csinfo_print_help():
"""
Print help text into console
"""
# Print help text
print('\nPrint info about Gammalib and ctools to the console.\n')
print('Available commands:')
print(' csinfo list List available ctools and cscripts')
print(' csinfo check Check Gammalib / ctools setup')
print(' csinfo info Print Gammalib / ctools setup info')
print('')
# Return
return
# ============================ #
# Print information to console #
# ============================ #
def csinfo(argv):
"""
Print information about Gammalib and ctools to the console
"""
# If there are no command line arguments then print help text and exit
# with success
if len(argv) <= 1:
csinfo_print_help()
sys.exit(0)
# Dispatch according to the command line argument
cmd = argv[1]
if cmd == 'list':
csinfo_list_tools()
elif cmd == 'check':
csinfo_setup_check()
elif cmd == 'info':
csinfo_setup_info()
else:
print('\nERROR: invalid command: "%s"' % cmd)
csinfo_print_help()
sys.exit(-1)
# Return
return
# ======================== #
# Main routine entry point #
# ======================== #
if __name__ == '__main__':
# Run the script
csinfo(sys.argv)