Commit b4dcdf64f400d37225cd5cd0ec068a4783a7a8ee
1 parent
4370b10b
Added script to merge model XML files
Showing
1 changed file
with
52 additions
and
0 deletions
cscripts/modelmerge.py
0 → 100644
| 1 | +#!/usr/bin/env python | ||
| 2 | +import gammalib | ||
| 3 | +import sys | ||
| 4 | + | ||
| 5 | +# ======================== # | ||
| 6 | +# Main routine entry point # | ||
| 7 | +# ======================== # | ||
| 8 | +if __name__ == '__main__': | ||
| 9 | + | ||
| 10 | + # Initialise flags | ||
| 11 | + need_help = False | ||
| 12 | + | ||
| 13 | + # Test for command line arguments | ||
| 14 | + if (len(sys.argv) > 1): | ||
| 15 | + if sys.argv[1] == "-h": | ||
| 16 | + need_help = True | ||
| 17 | + else: | ||
| 18 | + need_help = True | ||
| 19 | + | ||
| 20 | + # Print help if needed and exit | ||
| 21 | + if need_help: | ||
| 22 | + print("This script merges all model XML files provided as command line arguments. The usage is:\n") | ||
| 23 | + print("$ modelmerge.py modelfile1.xml modelfile2.xml ...\n") | ||
| 24 | + print("The resulting model will be written into \"merged_models.xml\" by default") | ||
| 25 | + print("This can be modified by specifying the keyword \"outmodel\", e.g.\n") | ||
| 26 | + print("$ modelmerge.py modelfile1.xml modelfile2.xml outmodel=my_merged_models.xml") | ||
| 27 | + sys.exit() | ||
| 28 | + | ||
| 29 | + # Get arguments | ||
| 30 | + args = sys.argv[1:] | ||
| 31 | + | ||
| 32 | + # Initialise empty model container | ||
| 33 | + models = gammalib.GModels() | ||
| 34 | + | ||
| 35 | + # Initialise outmodel file | ||
| 36 | + outmodel = "merged_models.xml" | ||
| 37 | + | ||
| 38 | + # Loop over input parameters | ||
| 39 | + for arg in args: | ||
| 40 | + if "outmodel=" in arg: | ||
| 41 | + outmodel = arg.replace("outmodel=","") | ||
| 42 | + continue | ||
| 43 | + | ||
| 44 | + # Load model file | ||
| 45 | + add_model = gammalib.GModels(arg) | ||
| 46 | + | ||
| 47 | + # Append each model to output container | ||
| 48 | + for model in add_model: | ||
| 49 | + models.append(model) | ||
| 50 | + | ||
| 51 | + # Save models | ||
| 52 | + models.save(outmodel) | ||
| 0 | \ No newline at end of file | 53 | \ No newline at end of file |