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