Commit 75c1aae664bf09255e04bca75bfd2b4155d359e5
1 parent
addee0ea
Add cscript code generation (#2709)
Showing
15 changed files
with
876 additions
and
36 deletions
ChangeLog
| 1 | -2018-10-18 | |
| 1 | +2018-11-03 | |
| 2 | 2 | |
| 3 | 3 | * Version 1.6.0 released |
| 4 | 4 | ======================== |
| 5 | 5 | |
| 6 | + Add code generator (#2709) | |
| 6 | 7 | Add csbkgmodel script (#2694) |
| 7 | 8 | Compute DETX and DETY in ctmodel if they are not available (#2694) |
| 8 | 9 | Do no longer set missing boundaries in ctool::get_observations and | ... | ... |
NEWS
| 1 | 1 | New Features and Important Changes in ctools 1.6.0 |
| 2 | 2 | |
| 3 | -18 October 2018 | |
| 3 | +3 November 2018 | |
| 4 | 4 | |
| 5 | 5 | |
| 6 | 6 | Introduction |
| ... | ... | @@ -11,6 +11,12 @@ ctools release with respect to the precedent release. |
| 11 | 11 | |
| 12 | 12 | Configuration |
| 13 | 13 | ------------- |
| 14 | +A Code Generator was added to the development directory that can be started | |
| 15 | +using dev/codegen.py and that requires GammaLib to be installed. The code | |
| 16 | +generator allows adding ctools and cscripts, including unit tests and code | |
| 17 | +documentation, that are fully functional. The developer then only needs to | |
| 18 | +fill the skeleton with the relevant code (#2709). | |
| 19 | + | |
| 14 | 20 | Keep existing library path in installation script on Mac OS X (#2420). |
| 15 | 21 | |
| 16 | 22 | ... | ... |
cscripts/csspec.py
dev/codegen.py
| ... | ... | @@ -143,13 +143,13 @@ def add_file(infile, outfile, tokens): |
| 143 | 143 | |
| 144 | 144 | # If we have a header line than format the line |
| 145 | 145 | if is_header_ast: |
| 146 | - text = line.strip(' *\n') | |
| 146 | + text = line.strip(' *\n')+' ' | |
| 147 | 147 | if n_header == 2: |
| 148 | 148 | line = ' *'+gammalib.centre(text,73)+'*\n' |
| 149 | 149 | elif n_header == 4: |
| 150 | 150 | line = ' *'+gammalib.left(' '+text,73)+'*\n' |
| 151 | 151 | if is_header_hash: |
| 152 | - text = line.strip('#\n') | |
| 152 | + text = line.strip('#\n')+' ' | |
| 153 | 153 | line = '#'+text[0:75]+'#\n' |
| 154 | 154 | |
| 155 | 155 | # Write out line |
| ... | ... | @@ -162,12 +162,12 @@ def add_file(infile, outfile, tokens): |
| 162 | 162 | return |
| 163 | 163 | |
| 164 | 164 | |
| 165 | -# ========== # | |
| 166 | -# Set tokens # | |
| 167 | -# ========== # | |
| 168 | -def set_tokens(classname, author, what, basename): | |
| 165 | +# ================ # | |
| 166 | +# Set ctool tokens # | |
| 167 | +# ================ # | |
| 168 | +def set_ctool_tokens(classname, basename, what, author, email, affiliation): | |
| 169 | 169 | """ |
| 170 | - Set replacement tokens for a ctool or cscript | |
| 170 | + Set replacement tokens for a ctool | |
| 171 | 171 | """ |
| 172 | 172 | # Get current year |
| 173 | 173 | year = str(date.today().year) |
| ... | ... | @@ -180,6 +180,34 @@ def set_tokens(classname, author, what, basename): |
| 180 | 180 | {'pattern': '[WHAT]', 'string': what}, |
| 181 | 181 | {'pattern': '[what]', 'string': what.lower()}, |
| 182 | 182 | {'pattern': '[AUTHOR]', 'string': author}, |
| 183 | + {'pattern': '[EMAIL]', 'string': email}, | |
| 184 | + {'pattern': '[AFFILIATION]', 'string': affiliation}, | |
| 185 | + {'pattern': '[YEAR]', 'string': year}] | |
| 186 | + | |
| 187 | + # Return tokens | |
| 188 | + return tokens | |
| 189 | + | |
| 190 | + | |
| 191 | +# ================== # | |
| 192 | +# Set cscript tokens # | |
| 193 | +# ================== # | |
| 194 | +def set_cscript_tokens(classname, basename, what, author, email, affiliation): | |
| 195 | + """ | |
| 196 | + Set replacement tokens for a cscript | |
| 197 | + """ | |
| 198 | + # Get current year | |
| 199 | + year = str(date.today().year) | |
| 200 | + | |
| 201 | + # Set tokens | |
| 202 | + tokens = [{'pattern': 'cscript_%s' % basename, 'string': classname}, | |
| 203 | + {'pattern': 'CSCRIPT_%s' % basename.upper(), 'string': classname.upper()}, | |
| 204 | + {'pattern': 'xxx', 'string': classname}, | |
| 205 | + {'pattern': 'XXX', 'string': classname.upper()}, | |
| 206 | + {'pattern': '[WHAT]', 'string': what}, | |
| 207 | + {'pattern': '[what]', 'string': what.lower()}, | |
| 208 | + {'pattern': '[AUTHOR]', 'string': author}, | |
| 209 | + {'pattern': '[EMAIL]', 'string': email}, | |
| 210 | + {'pattern': '[AFFILIATION]', 'string': affiliation}, | |
| 183 | 211 | {'pattern': '[YEAR]', 'string': year}] |
| 184 | 212 | |
| 185 | 213 | # Return tokens |
| ... | ... | @@ -191,12 +219,12 @@ def set_tokens(classname, author, what, basename): |
| 191 | 219 | # ========= # |
| 192 | 220 | def add_ctool(name, tokens, baseclass): |
| 193 | 221 | """ |
| 194 | - Add an instrument class | |
| 222 | + Add a ctool | |
| 195 | 223 | |
| 196 | 224 | Parameters |
| 197 | 225 | ---------- |
| 198 | 226 | name : str |
| 199 | - ctool or script name | |
| 227 | + ctool name | |
| 200 | 228 | tokens : str |
| 201 | 229 | Tokens for replacement |
| 202 | 230 | baseclass : str |
| ... | ... | @@ -285,12 +313,87 @@ def add_ctool(name, tokens, baseclass): |
| 285 | 313 | return |
| 286 | 314 | |
| 287 | 315 | |
| 288 | -# ============ # | |
| 289 | -# Manage ctool # | |
| 290 | -# ============ # | |
| 316 | +# =========== # | |
| 317 | +# Add cscript # | |
| 318 | +# =========== # | |
| 319 | +def add_cscript(name, tokens, baseclass): | |
| 320 | + """ | |
| 321 | + Add a cscript | |
| 322 | + | |
| 323 | + Parameters | |
| 324 | + ---------- | |
| 325 | + name : str | |
| 326 | + cscript name | |
| 327 | + tokens : str | |
| 328 | + Tokens for replacement | |
| 329 | + baseclass : str | |
| 330 | + Baseclass | |
| 331 | + """ | |
| 332 | + # Set template file names | |
| 333 | + pytemp = 'src/template/cscript_%s.py' % (baseclass) | |
| 334 | + partemp = 'src/template/cscript.par' | |
| 335 | + testtemp = 'src/template/cscript_test.py' | |
| 336 | + doctemp = 'src/template/cscript.rst' | |
| 337 | + | |
| 338 | + # Set destination file names | |
| 339 | + pyfile = 'cscripts/%s.py' % (name) | |
| 340 | + parfile = 'cscripts/%s.par' % (name) | |
| 341 | + testfile = 'test/test_%s.py' % (name) | |
| 342 | + docfile = 'doc/source/users/reference_manual/%s.rst' % (name) | |
| 343 | + | |
| 344 | + # Add files | |
| 345 | + add_file(pytemp, pyfile, tokens) | |
| 346 | + add_file(partemp, parfile, tokens) | |
| 347 | + add_file(testtemp, testfile, tokens) | |
| 348 | + add_file(doctemp, docfile, tokens) | |
| 349 | + | |
| 350 | + # Set file permissions | |
| 351 | + os.chmod(pyfile, 0o755) | |
| 352 | + | |
| 353 | + # Update cscripts/__init__.py.in file | |
| 354 | + for line in fileinput.FileInput('cscripts/__init__.py.in',inplace=1): | |
| 355 | + if '"csworkflow",' in line: | |
| 356 | + print(' "%s",' % (name)) | |
| 357 | + elif 'from cscripts.csworkflow import csworkflow' in line: | |
| 358 | + print('from cscripts.%s import %s' % (name, name)) | |
| 359 | + print line, | |
| 360 | + | |
| 361 | + # Update cscripts/Makefile.am file | |
| 362 | + for line in fileinput.FileInput('cscripts/Makefile.am',inplace=1): | |
| 363 | + if '$(srcdir)/csworkflow.py' in line: | |
| 364 | + print(' $(srcdir)/%s.py \\' % (name)) | |
| 365 | + elif '$(srcdir)/csworkflow.par' in line: | |
| 366 | + print(' $(srcdir)/%s.par \\' % (name)) | |
| 367 | + elif 'csworkflow \\' in line: | |
| 368 | + print(' %s \\' % (name)) | |
| 369 | + elif '$(top_srcdir)/test/test_csworkflow.py' in line: | |
| 370 | + print(' $(top_srcdir)/test/test_%s.py \\' % (name)) | |
| 371 | + print line, | |
| 372 | + | |
| 373 | + # Update test/test_python_cscripts.py file | |
| 374 | + for line in fileinput.FileInput('test/test_python_cscripts.py',inplace=1): | |
| 375 | + if 'import test_csworkflow' in line: | |
| 376 | + print('import test_%s' % (name)) | |
| 377 | + elif 'test_csworkflow.Test()' in line: | |
| 378 | + print(' test_%s.Test(),' % (name)) | |
| 379 | + print line, | |
| 380 | + | |
| 381 | + # Update reference manual index file | |
| 382 | + for line in fileinput.FileInput('doc/source/users/reference_manual/index.rst',inplace=1): | |
| 383 | + if 'csbkgmodel --- Generates background model for 3D analysis <csbkgmodel>' in line: | |
| 384 | + print(' %s --- ToDo: Describe what script is doing <%s>' % (name, name)) | |
| 385 | + print line, | |
| 386 | + | |
| 387 | + # Return | |
| 388 | + return | |
| 389 | + | |
| 390 | + | |
| 391 | +# ========== # | |
| 392 | +# ctool menu # | |
| 393 | +# ========== # | |
| 291 | 394 | def ctool_menu(): |
| 292 | 395 | """ |
| 293 | - Manage ctool | |
| 396 | + ctool menu | |
| 294 | 397 | """ |
| 295 | 398 | # Annonce actions |
| 296 | 399 | print("") |
| ... | ... | @@ -300,10 +403,8 @@ def ctool_menu(): |
| 300 | 403 | # Stay in loop until there is a final confirmation |
| 301 | 404 | while True: |
| 302 | 405 | |
| 303 | - # Enter information | |
| 406 | + # Enter tool name | |
| 304 | 407 | ctoolname = response('Please enter a ctool name (e.g. "ctpntsim")') |
| 305 | - what = response('Please say what the tool is for (e.g. "Pointing simulation")') | |
| 306 | - author = response('Please enter your name (e.g. "Joe Public")') | |
| 307 | 408 | |
| 308 | 409 | # Enter baseclass |
| 309 | 410 | print('From which baseclass should the ctool derive?') |
| ... | ... | @@ -326,17 +427,25 @@ def ctool_menu(): |
| 326 | 427 | basename = 'like' |
| 327 | 428 | baseclass = 'ctlikelihood' |
| 328 | 429 | |
| 430 | + # Enter further information | |
| 431 | + what = response('Please say what the tool is for (e.g. "Pointing simulation")') | |
| 432 | + author = response('Please enter your name (e.g. "Joe Public")') | |
| 433 | + email = response('Please enter your e-mail (e.g. "joe.public@dot.com")') | |
| 434 | + affiliation = response('Please enter your affiliation (e.g. "ESA")') | |
| 435 | + | |
| 329 | 436 | # Ask to confirm module summary |
| 330 | 437 | print('\nAll right. Have now:') |
| 331 | - print('ctool name ......: "%s"' % ctoolname) | |
| 332 | - print('Tools descriptor : "%s"' % what) | |
| 333 | - print('Your name .......: "%s"' % author) | |
| 334 | - print('Base class ......: "%s"' % baseclass) | |
| 438 | + print('ctool name .......: "%s"' % ctoolname) | |
| 439 | + print('Base class .......: "%s"' % baseclass) | |
| 440 | + print('Tools descriptor .: "%s"' % what) | |
| 441 | + print('Your name ........: "%s"' % author) | |
| 442 | + print('Your e-mail ......: "%s"' % email) | |
| 443 | + print('Your affiliation .: "%s"' % affiliation) | |
| 335 | 444 | if confirm('Is this correct?'): |
| 336 | 445 | break |
| 337 | 446 | |
| 338 | 447 | # Set tokens |
| 339 | - tokens = set_tokens(ctoolname, author, what, basename) | |
| 448 | + tokens = set_ctool_tokens(ctoolname, basename, what, author, email, affiliation) | |
| 340 | 449 | |
| 341 | 450 | # Add ctool |
| 342 | 451 | add_ctool(ctoolname, tokens, basename) |
| ... | ... | @@ -345,6 +454,72 @@ def ctool_menu(): |
| 345 | 454 | return |
| 346 | 455 | |
| 347 | 456 | |
| 457 | +# ============ # | |
| 458 | +# cscript menu # | |
| 459 | +# ============ # | |
| 460 | +def cscript_menu(): | |
| 461 | + """ | |
| 462 | + cscript menu | |
| 463 | + """ | |
| 464 | + # Annonce actions | |
| 465 | + print("") | |
| 466 | + print("Add cscript") | |
| 467 | + print("-----------") | |
| 468 | + | |
| 469 | + # Stay in loop until there is a final confirmation | |
| 470 | + while True: | |
| 471 | + | |
| 472 | + # Enter tool name | |
| 473 | + cscriptname = response('Please enter a cscript name (e.g. "cspntsim")') | |
| 474 | + | |
| 475 | + # Enter baseclass | |
| 476 | + print('From which baseclass should the cscript derive?') | |
| 477 | + print('[1] cscript') | |
| 478 | + print('[2] csobservation') | |
| 479 | + print('[3] cslikelihood') | |
| 480 | + waiting = True | |
| 481 | + choice = 1 | |
| 482 | + while waiting: | |
| 483 | + choice = str(raw_input('Enter your choice: ')) | |
| 484 | + if choice == '1' or choice == '2' or choice == '3': | |
| 485 | + waiting = False | |
| 486 | + if choice == '1': | |
| 487 | + basename = 'base' | |
| 488 | + baseclass = 'cscript' | |
| 489 | + elif choice == '2': | |
| 490 | + basename = 'obs' | |
| 491 | + baseclass = 'csobservation' | |
| 492 | + elif choice == '3': | |
| 493 | + basename = 'like' | |
| 494 | + baseclass = 'cslikelihood' | |
| 495 | + | |
| 496 | + # Enter further information | |
| 497 | + what = response('Please say what the script is for (e.g. "Pointing simulation")') | |
| 498 | + author = response('Please enter your name (e.g. "Joe Public")') | |
| 499 | + email = response('Please enter your e-mail (e.g. "joe.public@dot.com")') | |
| 500 | + affiliation = response('Please enter your affiliation (e.g. "ESA")') | |
| 501 | + | |
| 502 | + # Ask to confirm module summary | |
| 503 | + print('\nAll right. Have now:') | |
| 504 | + print('cscript name .....: "%s"' % cscriptname) | |
| 505 | + print('Base class .......: "%s"' % baseclass) | |
| 506 | + print('Script descriptor : "%s"' % what) | |
| 507 | + print('Your name ........: "%s"' % author) | |
| 508 | + print('Your e-mail ......: "%s"' % email) | |
| 509 | + print('Your affiliation .: "%s"' % affiliation) | |
| 510 | + if confirm('Is this correct?'): | |
| 511 | + break | |
| 512 | + | |
| 513 | + # Set tokens | |
| 514 | + tokens = set_cscript_tokens(cscriptname, basename, what, author, email, affiliation) | |
| 515 | + | |
| 516 | + # Add cscript | |
| 517 | + add_cscript(cscriptname, tokens, basename) | |
| 518 | + | |
| 519 | + # Return | |
| 520 | + return | |
| 521 | + | |
| 522 | + | |
| 348 | 523 | # ================ # |
| 349 | 524 | # Manage main menu # |
| 350 | 525 | # ================ # | ... | ... |
doc/source/admin/release_history/1.6.rst
| ... | ... | @@ -15,6 +15,7 @@ In particular, this release provides: |
| 15 | 15 | * Improved computation speed for :ref:`ctskymap` and :ref:`csphagen` |
| 16 | 16 | * Parallelisation of various cscripts |
| 17 | 17 | * Support for Python pickeling |
| 18 | +* A Code Generator to facilitate adding ctools and cscripts | |
| 18 | 19 | |
| 19 | 20 | |
| 20 | 21 | Bug fixes |
| ... | ... | @@ -90,6 +91,8 @@ Improvements |
| 90 | 91 | New features |
| 91 | 92 | ------------ |
| 92 | 93 | |
| 94 | +* [`2709 <https://cta-redmine.irap.omp.eu/issues/2709>`_] - | |
| 95 | + Add code generator | |
| 93 | 96 | * [`2694 <https://cta-redmine.irap.omp.eu/issues/2694>`_] - |
| 94 | 97 | Add :ref:`csbkgmodel` script |
| 95 | 98 | * [`2421 <https://cta-redmine.irap.omp.eu/issues/2421>`_] - | ... | ... |
src/template/Makefile.am
| ... | ... | @@ -46,18 +46,25 @@ ctool_like_CPPFLAGS = @CPPFLAGS@ |
| 46 | 46 | ctool_like_LDADD = $(top_builddir)/src/support/libsupport.la |
| 47 | 47 | |
| 48 | 48 | # Extra files to be distributed in the tar file |
| 49 | -EXTRA_DIST = ctool.par \ | |
| 49 | +EXTRA_DIST = cscript.par \ | |
| 50 | + cscript.rst \ | |
| 51 | + cscript_base.py \ | |
| 52 | + cscript_like.py \ | |
| 53 | + cscript_obs.py \ | |
| 54 | + cscript_test.py \ | |
| 55 | + ctool.par \ | |
| 56 | + ctool.rst \ | |
| 50 | 57 | ctool_base.cpp \ |
| 51 | 58 | ctool_base.hpp \ |
| 52 | 59 | ctool_base.i \ |
| 53 | - main_base.cpp \ | |
| 54 | 60 | ctool_like.cpp \ |
| 55 | 61 | ctool_like.hpp \ |
| 56 | 62 | ctool_like.i \ |
| 57 | - main_like.cpp \ | |
| 63 | + ctool_Makefile.am \ | |
| 58 | 64 | ctool_obs.cpp \ |
| 59 | 65 | ctool_obs.hpp \ |
| 60 | 66 | ctool_obs.i \ |
| 61 | - main_obs.cpp \ | |
| 62 | - ctool_Makefile.am \ | |
| 63 | - ctool_test.py | |
| 67 | + ctool_test.py \ | |
| 68 | + main_base.cpp \ | |
| 69 | + main_like.cpp \ | |
| 70 | + main_obs.cpp | ... | ... |
src/template/cscript.par
0 → 100755
| 1 | +############################################################################# | |
| 2 | +# # | |
| 3 | +# CTA Science Analysis Tools # | |
| 4 | +# # | |
| 5 | +# ------------------------------------------------------------------------- # | |
| 6 | +# # | |
| 7 | +# File: xxx.par # | |
| 8 | +# # | |
| 9 | +# Author: [AUTHOR] # | |
| 10 | +# [EMAIL] # | |
| 11 | +# [AFFILIATION] # | |
| 12 | +# # | |
| 13 | +# Purpose: Parameter file for the xxx script # | |
| 14 | +# # | |
| 15 | +############################################################################# | |
| 16 | +# | |
| 17 | +# General parameters | |
| 18 | +#=================== | |
| 19 | +# Your parameters go here | |
| 20 | + | |
| 21 | +# | |
| 22 | +# Standard parameters | |
| 23 | +#==================== | |
| 24 | +chatter, i, h, 2,0,4, "Chattiness of output" | |
| 25 | +clobber, b, h, yes,,, "Overwrite existing output files with new output files?" | |
| 26 | +debug, b, h, no,,, "Activate debugging mode?" | |
| 27 | +mode, s, h, ql,,, "Mode of automatic parameters" | |
| 28 | +logfile, f, h, xxx.log,,, "Log filename" | ... | ... |
src/template/cscript.rst
0 → 100755
| 1 | +.. _xxx: | |
| 2 | + | |
| 3 | +xxx | |
| 4 | +============================== | |
| 5 | + | |
| 6 | +ToDo: Describe in a one liner what the script is doing. | |
| 7 | + | |
| 8 | + | |
| 9 | +Synopsis | |
| 10 | +-------- | |
| 11 | + | |
| 12 | +ToDo: Desribe in detail what the script is doing, what it takes on input and | |
| 13 | +what it produces on output. Please do not write more than 80 characters per | |
| 14 | +line since this file is also used to produce a help text for the terminal. | |
| 15 | + | |
| 16 | + | |
| 17 | +General parameters | |
| 18 | +------------------ | |
| 19 | + | |
| 20 | +ToDo: Add here your parameters. | |
| 21 | + | |
| 22 | + | |
| 23 | +Standard parameters | |
| 24 | +------------------- | |
| 25 | + | |
| 26 | +``(chatter = 2) [integer]`` | |
| 27 | + Verbosity of the executable: | |
| 28 | + ``chatter = 0``: no information will be logged | |
| 29 | + | |
| 30 | + ``chatter = 1``: only errors will be logged | |
| 31 | + | |
| 32 | + ``chatter = 2``: errors and actions will be logged | |
| 33 | + | |
| 34 | + ``chatter = 3``: report about the task execution | |
| 35 | + | |
| 36 | + ``chatter = 4``: detailed report about the task execution | |
| 37 | + | |
| 38 | +``(clobber = yes) [boolean]`` | |
| 39 | + Specifies whether an existing output file should be overwritten. | |
| 40 | + | |
| 41 | +``(debug = no) [boolean]`` | |
| 42 | + Enables debug mode. In debug mode the executable will dump any log file output to the console. | |
| 43 | + | |
| 44 | +``(mode = ql) [string]`` | |
| 45 | + Mode of automatic parameters (default is ``ql``, i.e. "query and learn"). | |
| 46 | + | |
| 47 | +``(logfile = xxx.log) [string]`` | |
| 48 | + Name of log file. | |
| 49 | + | |
| 50 | + | |
| 51 | +Related tools or scripts | |
| 52 | +------------------------ | |
| 53 | + | |
| 54 | +None | ... | ... |
src/template/cscript_base.py
0 → 100755
| 1 | +#! /usr/bin/env python | |
| 2 | +# ========================================================================== | |
| 3 | +# [WHAT] script | |
| 4 | +# | |
| 5 | +# Copyright (C) [YEAR] [AUTHOR] | |
| 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 ctools | |
| 24 | + | |
| 25 | + | |
| 26 | +# ================== # | |
| 27 | +# cscript_base class # | |
| 28 | +# ================== # | |
| 29 | +class cscript_base(ctools.cscript): | |
| 30 | + """ | |
| 31 | + [WHAT] script | |
| 32 | + """ | |
| 33 | + | |
| 34 | + # Constructor | |
| 35 | + def __init__(self, *argv): | |
| 36 | + """ | |
| 37 | + Constructor | |
| 38 | + | |
| 39 | + Parameters | |
| 40 | + ---------- | |
| 41 | + argv : list of str | |
| 42 | + List of IRAF command line parameter strings of the form | |
| 43 | + ``parameter=3``. | |
| 44 | + """ | |
| 45 | + # Initialise application by calling the base class constructor | |
| 46 | + self._init_cscript(self.__class__.__name__, ctools.__version__, argv) | |
| 47 | + | |
| 48 | + # Return | |
| 49 | + return | |
| 50 | + | |
| 51 | + # State methods for pickling | |
| 52 | + def __getstate__(self): | |
| 53 | + """ | |
| 54 | + Extend ctools.cscript __getstate__ method | |
| 55 | + | |
| 56 | + Returns | |
| 57 | + ------- | |
| 58 | + state : dict | |
| 59 | + Pickled instance | |
| 60 | + """ | |
| 61 | + # Set pickled dictionary | |
| 62 | + state = {'base' : ctools.cscript.__getstate__(self)} | |
| 63 | + | |
| 64 | + # Return pickled dictionary | |
| 65 | + return state | |
| 66 | + | |
| 67 | + def __setstate__(self, state): | |
| 68 | + """ | |
| 69 | + Extend ctools.cscript __setstate__ method | |
| 70 | + | |
| 71 | + Parameters | |
| 72 | + ---------- | |
| 73 | + state : dict | |
| 74 | + Pickled instance | |
| 75 | + """ | |
| 76 | + # Set state | |
| 77 | + ctools.cscript.__setstate__(self, state['base']) | |
| 78 | + | |
| 79 | + # Return | |
| 80 | + return | |
| 81 | + | |
| 82 | + | |
| 83 | + # Private methods | |
| 84 | + def _get_parameters(self): | |
| 85 | + """ | |
| 86 | + Get parameters from parfile | |
| 87 | + """ | |
| 88 | + # TODO: Add code to query all relevant parameters | |
| 89 | + | |
| 90 | + # Write input parameters into logger | |
| 91 | + self._log_parameters(gammalib.TERSE) | |
| 92 | + | |
| 93 | + # Return | |
| 94 | + return | |
| 95 | + | |
| 96 | + | |
| 97 | + # Public methods | |
| 98 | + def run(self): | |
| 99 | + """ | |
| 100 | + Run the script | |
| 101 | + """ | |
| 102 | + # Switch screen logging on in debug mode | |
| 103 | + if self._logDebug(): | |
| 104 | + self._log.cout(True) | |
| 105 | + | |
| 106 | + # Get parameters | |
| 107 | + self._get_parameters() | |
| 108 | + | |
| 109 | + # TODO: Your code goes here | |
| 110 | + | |
| 111 | + # Return | |
| 112 | + return | |
| 113 | + | |
| 114 | + def save(self): | |
| 115 | + """ | |
| 116 | + Save something | |
| 117 | + """ | |
| 118 | + # Write header | |
| 119 | + self._log_header1(gammalib.TERSE, 'Save something') | |
| 120 | + | |
| 121 | + # TODO: Your code goes here | |
| 122 | + | |
| 123 | + # Return | |
| 124 | + return | |
| 125 | + | |
| 126 | + | |
| 127 | +# ======================== # | |
| 128 | +# Main routine entry point # | |
| 129 | +# ======================== # | |
| 130 | +if __name__ == '__main__': | |
| 131 | + | |
| 132 | + # Create instance of application | |
| 133 | + app = cscript_base(sys.argv) | |
| 134 | + | |
| 135 | + # Execute application | |
| 136 | + app.execute() | ... | ... |
src/template/cscript_like.py
0 → 100755
| 1 | +#! /usr/bin/env python | |
| 2 | +# ========================================================================== | |
| 3 | +# [WHAT] script | |
| 4 | +# | |
| 5 | +# Copyright (C) [YEAR] [AUTHOR] | |
| 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 ctools | |
| 24 | + | |
| 25 | + | |
| 26 | +# ================== # | |
| 27 | +# cscript_like class # | |
| 28 | +# ================== # | |
| 29 | +class cscript_like(ctools.cslikelihood): | |
| 30 | + """ | |
| 31 | + [WHAT] script | |
| 32 | + """ | |
| 33 | + | |
| 34 | + # Constructor | |
| 35 | + def __init__(self, *argv): | |
| 36 | + """ | |
| 37 | + Constructor | |
| 38 | + | |
| 39 | + Parameters | |
| 40 | + ---------- | |
| 41 | + argv : list of str | |
| 42 | + List of IRAF command line parameter strings of the form | |
| 43 | + ``parameter=3``. | |
| 44 | + """ | |
| 45 | + # Initialise application by calling the base class constructor | |
| 46 | + self._init_cslikelihood(self.__class__.__name__, ctools.__version__, argv) | |
| 47 | + | |
| 48 | + # Return | |
| 49 | + return | |
| 50 | + | |
| 51 | + # State methods for pickling | |
| 52 | + def __getstate__(self): | |
| 53 | + """ | |
| 54 | + Extend ctools.cslikelihood __getstate__ method | |
| 55 | + | |
| 56 | + Returns | |
| 57 | + ------- | |
| 58 | + state : dict | |
| 59 | + Pickled instance | |
| 60 | + """ | |
| 61 | + # Set pickled dictionary | |
| 62 | + state = {'base' : ctools.cslikelihood.__getstate__(self)} | |
| 63 | + | |
| 64 | + # Return pickled dictionary | |
| 65 | + return state | |
| 66 | + | |
| 67 | + def __setstate__(self, state): | |
| 68 | + """ | |
| 69 | + Extend ctools.cslikelihood __setstate__ method | |
| 70 | + | |
| 71 | + Parameters | |
| 72 | + ---------- | |
| 73 | + state : dict | |
| 74 | + Pickled instance | |
| 75 | + """ | |
| 76 | + # Set state | |
| 77 | + ctools.cslikelihood.__setstate__(self, state['base']) | |
| 78 | + | |
| 79 | + # Return | |
| 80 | + return | |
| 81 | + | |
| 82 | + | |
| 83 | + # Private methods | |
| 84 | + def _get_parameters(self): | |
| 85 | + """ | |
| 86 | + Get parameters from parfile | |
| 87 | + """ | |
| 88 | + # TODO: Add code to query all relevant parameters | |
| 89 | + | |
| 90 | + # Write input parameters into logger | |
| 91 | + self._log_parameters(gammalib.TERSE) | |
| 92 | + | |
| 93 | + # Return | |
| 94 | + return | |
| 95 | + | |
| 96 | + | |
| 97 | + # Public methods | |
| 98 | + def run(self): | |
| 99 | + """ | |
| 100 | + Run the script | |
| 101 | + """ | |
| 102 | + # Switch screen logging on in debug mode | |
| 103 | + if self._logDebug(): | |
| 104 | + self._log.cout(True) | |
| 105 | + | |
| 106 | + # Get parameters | |
| 107 | + self._get_parameters() | |
| 108 | + | |
| 109 | + # TODO: Your code goes here | |
| 110 | + | |
| 111 | + # Return | |
| 112 | + return | |
| 113 | + | |
| 114 | + def save(self): | |
| 115 | + """ | |
| 116 | + Save something | |
| 117 | + """ | |
| 118 | + # Write header | |
| 119 | + self._log_header1(gammalib.TERSE, 'Save something') | |
| 120 | + | |
| 121 | + # TODO: Your code goes here | |
| 122 | + | |
| 123 | + # Return | |
| 124 | + return | |
| 125 | + | |
| 126 | + | |
| 127 | +# ======================== # | |
| 128 | +# Main routine entry point # | |
| 129 | +# ======================== # | |
| 130 | +if __name__ == '__main__': | |
| 131 | + | |
| 132 | + # Create instance of application | |
| 133 | + app = cscript_like(sys.argv) | |
| 134 | + | |
| 135 | + # Execute application | |
| 136 | + app.execute() | ... | ... |
src/template/cscript_obs.py
0 → 100755
| 1 | +#! /usr/bin/env python | |
| 2 | +# ========================================================================== | |
| 3 | +# [WHAT] script | |
| 4 | +# | |
| 5 | +# Copyright (C) [YEAR] [AUTHOR] | |
| 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 ctools | |
| 24 | + | |
| 25 | + | |
| 26 | +# ================= # | |
| 27 | +# cscript_obs class # | |
| 28 | +# ================= # | |
| 29 | +class cscript_obs(ctools.csobservation): | |
| 30 | + """ | |
| 31 | + [WHAT] script | |
| 32 | + """ | |
| 33 | + | |
| 34 | + # Constructor | |
| 35 | + def __init__(self, *argv): | |
| 36 | + """ | |
| 37 | + Constructor | |
| 38 | + | |
| 39 | + Parameters | |
| 40 | + ---------- | |
| 41 | + argv : list of str | |
| 42 | + List of IRAF command line parameter strings of the form | |
| 43 | + ``parameter=3``. | |
| 44 | + """ | |
| 45 | + # Initialise application by calling the base class constructor | |
| 46 | + self._init_csobservation(self.__class__.__name__, ctools.__version__, argv) | |
| 47 | + | |
| 48 | + # Return | |
| 49 | + return | |
| 50 | + | |
| 51 | + # State methods for pickling | |
| 52 | + def __getstate__(self): | |
| 53 | + """ | |
| 54 | + Extend ctools.csobservation __getstate__ method | |
| 55 | + | |
| 56 | + Returns | |
| 57 | + ------- | |
| 58 | + state : dict | |
| 59 | + Pickled instance | |
| 60 | + """ | |
| 61 | + # Set pickled dictionary | |
| 62 | + state = {'base' : ctools.csobservation.__getstate__(self)} | |
| 63 | + | |
| 64 | + # Return pickled dictionary | |
| 65 | + return state | |
| 66 | + | |
| 67 | + def __setstate__(self, state): | |
| 68 | + """ | |
| 69 | + Extend ctools.csobservation __setstate__ method | |
| 70 | + | |
| 71 | + Parameters | |
| 72 | + ---------- | |
| 73 | + state : dict | |
| 74 | + Pickled instance | |
| 75 | + """ | |
| 76 | + # Set state | |
| 77 | + ctools.csobservation.__setstate__(self, state['base']) | |
| 78 | + | |
| 79 | + # Return | |
| 80 | + return | |
| 81 | + | |
| 82 | + | |
| 83 | + # Private methods | |
| 84 | + def _get_parameters(self): | |
| 85 | + """ | |
| 86 | + Get parameters from parfile | |
| 87 | + """ | |
| 88 | + # TODO: Add code to query all relevant parameters | |
| 89 | + | |
| 90 | + # Write input parameters into logger | |
| 91 | + self._log_parameters(gammalib.TERSE) | |
| 92 | + | |
| 93 | + # Return | |
| 94 | + return | |
| 95 | + | |
| 96 | + | |
| 97 | + # Public methods | |
| 98 | + def run(self): | |
| 99 | + """ | |
| 100 | + Run the script | |
| 101 | + """ | |
| 102 | + # Switch screen logging on in debug mode | |
| 103 | + if self._logDebug(): | |
| 104 | + self._log.cout(True) | |
| 105 | + | |
| 106 | + # Get parameters | |
| 107 | + self._get_parameters() | |
| 108 | + | |
| 109 | + # TODO: Your code goes here | |
| 110 | + | |
| 111 | + # Return | |
| 112 | + return | |
| 113 | + | |
| 114 | + def save(self): | |
| 115 | + """ | |
| 116 | + Save something | |
| 117 | + """ | |
| 118 | + # Write header | |
| 119 | + self._log_header1(gammalib.TERSE, 'Save something') | |
| 120 | + | |
| 121 | + # TODO: Your code goes here | |
| 122 | + | |
| 123 | + # Return | |
| 124 | + return | |
| 125 | + | |
| 126 | + | |
| 127 | +# ======================== # | |
| 128 | +# Main routine entry point # | |
| 129 | +# ======================== # | |
| 130 | +if __name__ == '__main__': | |
| 131 | + | |
| 132 | + # Create instance of application | |
| 133 | + app = cscript_obs(sys.argv) | |
| 134 | + | |
| 135 | + # Execute application | |
| 136 | + app.execute() | ... | ... |
src/template/cscript_test.py
0 → 100755
| 1 | +#! /usr/bin/env python | |
| 2 | +# ========================================================================== | |
| 3 | +# This scripts performs unit tests for the xxx script. | |
| 4 | +# | |
| 5 | +# Copyright (C) [YEAR] [AUTHOR] | |
| 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 os | |
| 22 | +import gammalib | |
| 23 | +import cscripts | |
| 24 | +from testing import test | |
| 25 | + | |
| 26 | + | |
| 27 | +# ========================= # | |
| 28 | +# Test class for xxx script # | |
| 29 | +# ========================= # | |
| 30 | +class Test(test): | |
| 31 | + """ | |
| 32 | + Test class for xxx script | |
| 33 | + | |
| 34 | + This test class makes unit tests for the xxx script by using it from | |
| 35 | + the command line and from Python. | |
| 36 | + """ | |
| 37 | + | |
| 38 | + # Constructor | |
| 39 | + def __init__(self): | |
| 40 | + """ | |
| 41 | + Constructor | |
| 42 | + """ | |
| 43 | + # Call base class constructor | |
| 44 | + test.__init__(self) | |
| 45 | + | |
| 46 | + # Return | |
| 47 | + return | |
| 48 | + | |
| 49 | + # Set test functions | |
| 50 | + def set(self): | |
| 51 | + """ | |
| 52 | + Set all test functions | |
| 53 | + """ | |
| 54 | + # Set test name | |
| 55 | + self.name('xxx') | |
| 56 | + | |
| 57 | + # Append tests | |
| 58 | + self.append(self._test_cmd, 'Test xxx on command line') | |
| 59 | + self.append(self._test_python, 'Test xxx from Python') | |
| 60 | + self.append(self._test_pickeling, 'Test xxx pickeling') | |
| 61 | + | |
| 62 | + # Return | |
| 63 | + return | |
| 64 | + | |
| 65 | + # Test xxx on command line | |
| 66 | + def _test_cmd(self): | |
| 67 | + """ | |
| 68 | + Test xxx on the command line | |
| 69 | + """ | |
| 70 | + # Set script name | |
| 71 | + xxx = self._script('xxx') | |
| 72 | + | |
| 73 | + # Setup xxx command | |
| 74 | + cmd = xxx+' logfile="xxx_cmd1.log" chatter=1' | |
| 75 | + | |
| 76 | + # Check if execution was successful | |
| 77 | + self.test_value(self._execute(cmd), 0, | |
| 78 | + 'Check successful execution from command line') | |
| 79 | + | |
| 80 | + # Check xxx --help | |
| 81 | + self._check_help(xxx) | |
| 82 | + | |
| 83 | + # Return | |
| 84 | + return | |
| 85 | + | |
| 86 | + # Test xxx from Python | |
| 87 | + def _test_python(self): | |
| 88 | + """ | |
| 89 | + Test xxx from Python | |
| 90 | + """ | |
| 91 | + # Allocate xxx | |
| 92 | + script = cscripts.xxx() | |
| 93 | + | |
| 94 | + # Check that saving does not nothing | |
| 95 | + script['logfile'] = 'xxx_py0.log' | |
| 96 | + script.logFileOpen() | |
| 97 | + script.save() | |
| 98 | + self.test_assert(not os.path.isfile('xxx_py0.fits'), | |
| 99 | + 'Check that no FITS file has been created') | |
| 100 | + | |
| 101 | + # Check that clearing does not lead to an exception or segfault | |
| 102 | + script.clear() | |
| 103 | + | |
| 104 | + # Now set xxx parameters | |
| 105 | + script['logfile'] = 'xxx_py1.log' | |
| 106 | + script['chatter'] = 2 | |
| 107 | + | |
| 108 | + # Run xxx script | |
| 109 | + script.logFileOpen() | |
| 110 | + script.run() | |
| 111 | + | |
| 112 | + # Check result | |
| 113 | + self._check_result(script) | |
| 114 | + | |
| 115 | + # Return | |
| 116 | + return | |
| 117 | + | |
| 118 | + # Test xxx pickeling | |
| 119 | + def _test_pickeling(self): | |
| 120 | + """ | |
| 121 | + Test xxx pickeling | |
| 122 | + """ | |
| 123 | + # Perform pickeling test of empty class | |
| 124 | + self._pickeling(cscripts.xxx()) | |
| 125 | + | |
| 126 | + # Setup script for pickling text | |
| 127 | + script = cscripts.xxx() | |
| 128 | + script['logfile'] = 'xxx_py1_pickle.log' | |
| 129 | + script['chatter'] = 2 | |
| 130 | + | |
| 131 | + # Perform pickeling tests of filled class | |
| 132 | + obj = self._pickeling(script) | |
| 133 | + | |
| 134 | + # Run xxx script and save result | |
| 135 | + obj.logFileOpen() # Make sure we get a log file | |
| 136 | + obj.run() | |
| 137 | + obj.save() | |
| 138 | + | |
| 139 | + # TODO: Check result file | |
| 140 | + | |
| 141 | + # Return | |
| 142 | + return | |
| 143 | + | |
| 144 | + # Check xxx result | |
| 145 | + def _check_result(self, script): | |
| 146 | + """ | |
| 147 | + Check content of script | |
| 148 | + | |
| 149 | + Parameters | |
| 150 | + ---------- | |
| 151 | + script : `~cscripts.xxx` | |
| 152 | + xxx instance | |
| 153 | + """ | |
| 154 | + # TODO: Implement test on script result | |
| 155 | + | |
| 156 | + # Return | |
| 157 | + return | ... | ... |
src/template/ctool.par
src/template/ctool.rst
src/template/ctool_test.py
| ... | ... | @@ -123,7 +123,8 @@ class Test(test): |
| 123 | 123 | cpy_tool.logFileOpen() |
| 124 | 124 | cpy_tool.run() |
| 125 | 125 | |
| 126 | - # TODO: Check result | |
| 126 | + # Check result | |
| 127 | + self._check_result(cpy_tool) | |
| 127 | 128 | |
| 128 | 129 | # Return |
| 129 | 130 | return |
| ... | ... | @@ -131,7 +132,7 @@ class Test(test): |
| 131 | 132 | # Check xxx result |
| 132 | 133 | def _check_result(self, tool): |
| 133 | 134 | """ |
| 134 | - Check content of an observation | |
| 135 | + Check content of tool | |
| 135 | 136 | |
| 136 | 137 | Parameters |
| 137 | 138 | ---------- | ... | ... |