Commit db573ea0cde31f55534dfb93e7f861faccc9ca22
1 parent
3377b3d7
Added csmodelmerge including unit test
Showing
5 changed files
with
331 additions
and
7 deletions
cscripts/Makefile.am
| 1 | ## Process this file with automake to produce Makefile.in | 1 | ## Process this file with automake to produce Makefile.in |
| 2 | 2 | ||
| 3 | # Scripts to be distributed in bindir | 3 | # Scripts to be distributed in bindir |
| 4 | -binscripts = tsmerge.py modelmerge.py | 4 | +binscripts = tsmerge.py |
| 5 | 5 | ||
| 6 | # Scripts to be distributed in $(pyexecdir)/cscripts | 6 | # Scripts to be distributed in $(pyexecdir)/cscripts |
| 7 | pythonscripts = __init__.py \ | 7 | pythonscripts = __init__.py \ |
| 8 | cspull.py cssens.py cstsdist.py cscaldb.py csresmap.py \ | 8 | cspull.py cssens.py cstsdist.py cscaldb.py csresmap.py \ |
| 9 | cshessobs.py csobsdef.py csspec.py cslightcrv.py csobsinfo.py \ | 9 | cshessobs.py csobsdef.py csspec.py cslightcrv.py csobsinfo.py \ |
| 10 | - csinfo.py \ | 10 | + csinfo.py csmodelinfo.py csmodelmerge.py \ |
| 11 | obsutils.py | 11 | obsutils.py |
| 12 | 12 | ||
| 13 | # Scripts for which symbolic links will be created in bindir | 13 | # Scripts for which symbolic links will be created in bindir |
| 14 | linkscripts = cspull cssens cstsdist cscaldb csresmap \ | 14 | linkscripts = cspull cssens cstsdist cscaldb csresmap \ |
| 15 | - cshessobs csobsdef csspec cslightcrv csobsinfo \ | ||
| 16 | - csinfo | 15 | + cshessobs csobsdef csspec cslightcrv csobsinfo csmodelmerge \ |
| 16 | + csinfo csmodelinfo | ||
| 17 | 17 | ||
| 18 | # Distribute scripts | 18 | # Distribute scripts |
| 19 | EXTRA_DIST = $(binscripts) $(pythonscripts) | 19 | EXTRA_DIST = $(binscripts) $(pythonscripts) |
cscripts/csmodelinfo.py
100644 → 100755
cscripts/csmodelmerge.py
0 → 100755
| 1 | +#!/usr/bin/env python | ||
| 2 | +# ========================================================================== | ||
| 3 | +# Merges model XML files into one file | ||
| 4 | +# | ||
| 5 | +# Copyright (C) 2015 Michael Mayer | ||
| 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 gammalib | ||
| 22 | +import ctools | ||
| 23 | +import sys | ||
| 24 | +import glob | ||
| 25 | + | ||
| 26 | + | ||
| 27 | +# =============== # | ||
| 28 | +# cshessobs class # | ||
| 29 | +# =============== # | ||
| 30 | +class csmodelmerge(ctools.cscript): | ||
| 31 | + """ | ||
| 32 | + This class merges an arbitray number of model container into one output model XML file | ||
| 33 | + """ | ||
| 34 | + def __init__(self, *argv): | ||
| 35 | + """ | ||
| 36 | + Constructor. | ||
| 37 | + """ | ||
| 38 | + # Set name | ||
| 39 | + self.name = "csmodelmerge" | ||
| 40 | + self.version = "0.1.0" | ||
| 41 | + | ||
| 42 | + # Make sure that parfile exists | ||
| 43 | + file = self.parfile() | ||
| 44 | + | ||
| 45 | + # Initialise application | ||
| 46 | + if len(argv) == 0: | ||
| 47 | + ctools.cscript.__init__(self, self.name, self.version) | ||
| 48 | + elif len(argv) == 1: | ||
| 49 | + ctools.cscript.__init__(self, self.name, self.version, *argv) | ||
| 50 | + else: | ||
| 51 | + raise TypeError("Invalid number of arguments given.") | ||
| 52 | + | ||
| 53 | + # Set logger properties | ||
| 54 | + self.log_header() | ||
| 55 | + self.log.date(True) | ||
| 56 | + | ||
| 57 | + # Return | ||
| 58 | + return | ||
| 59 | + | ||
| 60 | + def __del__(self): | ||
| 61 | + """ | ||
| 62 | + Destructor. | ||
| 63 | + """ | ||
| 64 | + # Return | ||
| 65 | + return | ||
| 66 | + | ||
| 67 | + def parfile(self): | ||
| 68 | + """ | ||
| 69 | + Check if parfile exists. If parfile does not exist then create a | ||
| 70 | + default parfile. This kluge avoids shipping the cscript with a parfile. | ||
| 71 | + """ | ||
| 72 | + | ||
| 73 | + # Set parfile name | ||
| 74 | + parfile = self.name+".par" | ||
| 75 | + | ||
| 76 | + try: | ||
| 77 | + pars = gammalib.GApplicationPars(parfile) | ||
| 78 | + except: | ||
| 79 | + # Signal that parfile was not found | ||
| 80 | + print("Parfile \""+parfile+"\" not found. Create default parfile.") | ||
| 81 | + | ||
| 82 | + # Create default parfile | ||
| 83 | + pars = gammalib.GApplicationPars() | ||
| 84 | + pars.append(gammalib.GApplicationPar("inmodels","s","a","model.xml","","","Input model XML files")) | ||
| 85 | + pars.append(gammalib.GApplicationPar("outmodel","f","a","NONE","","","Output model file")) | ||
| 86 | + pars.append_standard() | ||
| 87 | + pars.append(gammalib.GApplicationPar("logfile","f","h","csmodelmerge.log","","","Log filename")) | ||
| 88 | + pars.save(parfile) | ||
| 89 | + | ||
| 90 | + # Return | ||
| 91 | + return | ||
| 92 | + | ||
| 93 | + def get_parameters(self): | ||
| 94 | + """ | ||
| 95 | + Get parameters from parfile and setup the observation. | ||
| 96 | + """ | ||
| 97 | + # Get input models | ||
| 98 | + self.inmodels = self["inmodels"].string() | ||
| 99 | + | ||
| 100 | + # Interpet inmodels string | ||
| 101 | + # Handle ascii files | ||
| 102 | + if "@" == self.inmodels[0]: | ||
| 103 | + self.files = open(self.inmodels.replace("@","")).read().splitlines() | ||
| 104 | + | ||
| 105 | + # Handle wild card strings | ||
| 106 | + elif "*" in self.inmodels: | ||
| 107 | + self.files = glob.glob(self.inmodels) | ||
| 108 | + | ||
| 109 | + # Handle space-separated list | ||
| 110 | + elif " " in self.inmodels: | ||
| 111 | + self.files = self.inmodels.split(" ") | ||
| 112 | + | ||
| 113 | + # Handle comma-separated list | ||
| 114 | + elif "," in self.inmodels: | ||
| 115 | + self.files = self.inmodels.split(",") | ||
| 116 | + | ||
| 117 | + # Throw exception if input models cannot be decoded | ||
| 118 | + else: | ||
| 119 | + msg = "Parameter \"inmodels\" must contain either an @asci file, a comma-separated or space-separated list of files or a wildcard string" | ||
| 120 | + raise gammalib.GException.invalid_argument("csmodelmerge::get_parameters", msg) | ||
| 121 | + | ||
| 122 | + # Get output file | ||
| 123 | + self.outmodel = self["outmodel"].filename() | ||
| 124 | + | ||
| 125 | + # Return | ||
| 126 | + return | ||
| 127 | + | ||
| 128 | + | ||
| 129 | + def run(self): | ||
| 130 | + """ | ||
| 131 | + Run the script. | ||
| 132 | + """ | ||
| 133 | + # Switch screen logging on in debug mode | ||
| 134 | + if self.logDebug(): | ||
| 135 | + self.log.cout(True) | ||
| 136 | + | ||
| 137 | + # Get parameters | ||
| 138 | + self.get_parameters() | ||
| 139 | + | ||
| 140 | + # Write input parameters into logger | ||
| 141 | + if self.logTerse(): | ||
| 142 | + self.log_parameters() | ||
| 143 | + self.log("\n") | ||
| 144 | + | ||
| 145 | + if self.logTerse(): | ||
| 146 | + self.log("\n") | ||
| 147 | + self.log.header1("Merging Models") | ||
| 148 | + self.log("\n") | ||
| 149 | + | ||
| 150 | + | ||
| 151 | + # Initialise output model container | ||
| 152 | + self.models = gammalib.GModels() | ||
| 153 | + | ||
| 154 | + # Loop over model files | ||
| 155 | + for modelfile in self.files: | ||
| 156 | + | ||
| 157 | + # Logging | ||
| 158 | + self.log("Adding "+modelfile) | ||
| 159 | + self.log("\n") | ||
| 160 | + | ||
| 161 | + # Open models | ||
| 162 | + model_add = gammalib.GModels(modelfile) | ||
| 163 | + | ||
| 164 | + # Append models to container | ||
| 165 | + for model in model_add: | ||
| 166 | + self.models.append(model) | ||
| 167 | + | ||
| 168 | + # Return | ||
| 169 | + return | ||
| 170 | + | ||
| 171 | + def save(self): | ||
| 172 | + """ | ||
| 173 | + Save pointings to ds9 region file if required | ||
| 174 | + """ | ||
| 175 | + | ||
| 176 | + # Logging | ||
| 177 | + if self.logExplicit(): | ||
| 178 | + self.log("\n") | ||
| 179 | + self.log.header1("Saving model file") | ||
| 180 | + self.log("\n") | ||
| 181 | + | ||
| 182 | + # Save models | ||
| 183 | + self.models.save(self.outmodel) | ||
| 184 | + | ||
| 185 | + # Return | ||
| 186 | + return | ||
| 187 | + | ||
| 188 | + | ||
| 189 | + def execute(self): | ||
| 190 | + """ | ||
| 191 | + Execute the script. | ||
| 192 | + """ | ||
| 193 | + # Run the script | ||
| 194 | + self.run() | ||
| 195 | + | ||
| 196 | + # Save ds9 file if required | ||
| 197 | + self.save() | ||
| 198 | + | ||
| 199 | + # Return | ||
| 200 | + return | ||
| 201 | + | ||
| 202 | + | ||
| 203 | +# ======================== # | ||
| 204 | +# Main routine entry point # | ||
| 205 | +# ======================== # | ||
| 206 | +if __name__ == '__main__': | ||
| 207 | + """ | ||
| 208 | + Generates caldb entry. | ||
| 209 | + """ | ||
| 210 | + # Create instance of application | ||
| 211 | + app = csmodelmerge(sys.argv) | ||
| 212 | + | ||
| 213 | + # Open logfile | ||
| 214 | + app.logFileOpen() | ||
| 215 | + | ||
| 216 | + # Execute application | ||
| 217 | + app.execute() |
test/data/model_cube_background2.xml
0 → 100644
| 1 | +<?xml version="1.0" standalone="no"?> | ||
| 2 | +<source_library title="source library"> | ||
| 3 | + <source name="CTABackgroundModel2" type="CTACubeBackground" instrument="CTA"> | ||
| 4 | + <spectrum type="PowerLaw"> | ||
| 5 | + <parameter name="Prefactor" scale="1.0" value="1.0" min="1e-3" max="1e+3" free="1"/> | ||
| 6 | + <parameter name="Index" scale="1.0" value="0.0" min="-5.0" max="+5.0" free="1"/> | ||
| 7 | + <parameter name="Scale" scale="1e6" value="1.0" min="0.01" max="1000.0" free="0"/> | ||
| 8 | + </spectrum> | ||
| 9 | + </source> | ||
| 10 | +</source_library> |
test/test_cscripts.sh.in
| @@ -80,22 +80,119 @@ $ECHO -n "Test csobsinfo: " | @@ -80,22 +80,119 @@ $ECHO -n "Test csobsinfo: " | ||
| 80 | chmod +x @abs_top_srcdir@/cscripts/csobsinfo.py | 80 | chmod +x @abs_top_srcdir@/cscripts/csobsinfo.py |
| 81 | @abs_top_srcdir@/cscripts/csobsinfo.py \ | 81 | @abs_top_srcdir@/cscripts/csobsinfo.py \ |
| 82 | inobs="obs.xml" \ | 82 | inobs="obs.xml" \ |
| 83 | - ds9file="ds9.reg" \ | 83 | + ds9file="obs.reg" \ |
| 84 | offset=yes \ | 84 | offset=yes \ |
| 85 | ra="83.6331" \ | 85 | ra="83.6331" \ |
| 86 | dec="22.01" > csobsinfo.log | 86 | dec="22.01" > csobsinfo.log |
| 87 | $ECHO -n "." | 87 | $ECHO -n "." |
| 88 | -if [ -s "ds9.reg" ] | 88 | +if [ -s "obs.reg" ] |
| 89 | then | 89 | then |
| 90 | $ECHO -n "." | 90 | $ECHO -n "." |
| 91 | else | 91 | else |
| 92 | - $ECHO " ds9.reg file is not valid" | 92 | + $ECHO " obs.reg file is not valid" |
| 93 | exit 1 | 93 | exit 1 |
| 94 | fi | 94 | fi |
| 95 | $ECHO " ok" | 95 | $ECHO " ok" |
| 96 | 96 | ||
| 97 | 97 | ||
| 98 | # | 98 | # |
| 99 | +# Test csmodelinfo | ||
| 100 | +# ============== | ||
| 101 | +$ECHO -n "Test csmodelinfo: " | ||
| 102 | +chmod +x @abs_top_srcdir@/cscripts/csmodelinfo.py | ||
| 103 | +@abs_top_srcdir@/cscripts/csmodelinfo.py \ | ||
| 104 | + inmodel="data/crab.xml" \ | ||
| 105 | + ds9file="model.reg" > csmodelinfo.log | ||
| 106 | +$ECHO -n "." | ||
| 107 | +if [ -s "model.reg" ] | ||
| 108 | +then | ||
| 109 | + $ECHO -n "." | ||
| 110 | +else | ||
| 111 | + $ECHO " model.reg file is not valid" | ||
| 112 | + exit 1 | ||
| 113 | +fi | ||
| 114 | +$ECHO " ok" | ||
| 115 | + | ||
| 116 | + | ||
| 117 | +# | ||
| 118 | +# Test csmodelmerge (space-separated) | ||
| 119 | +# ============== | ||
| 120 | +$ECHO -n "Test csmodelmerge: " | ||
| 121 | +chmod +x @abs_top_srcdir@/cscripts/csmodelmerge.py | ||
| 122 | +@abs_top_srcdir@/cscripts/csmodelmerge.py \ | ||
| 123 | + inmodels="data/crab.xml data/source1.xml" \ | ||
| 124 | + outmodel="merged_models.xml" > csmodelmerge.log | ||
| 125 | +$ECHO -n "." | ||
| 126 | +if [ -s "merged_models.xml" ] | ||
| 127 | +then | ||
| 128 | + $ECHO -n "." | ||
| 129 | +else | ||
| 130 | + $ECHO " merged_models.xml file is not valid" | ||
| 131 | + exit 1 | ||
| 132 | +fi | ||
| 133 | +$ECHO " ok" | ||
| 134 | + | ||
| 135 | + | ||
| 136 | +# | ||
| 137 | +# Test csmodelmerge (comma-separated) | ||
| 138 | +# ============== | ||
| 139 | +$ECHO -n "Test csmodelmerge: " | ||
| 140 | +chmod +x @abs_top_srcdir@/cscripts/csmodelmerge.py | ||
| 141 | +@abs_top_srcdir@/cscripts/csmodelmerge.py \ | ||
| 142 | + inmodels="data/crab.xml,data/source1.xml" \ | ||
| 143 | + outmodel="merged_models2.xml" > csmodelmerge.log | ||
| 144 | +$ECHO -n "." | ||
| 145 | +if [ -s "merged_models2.xml" ] | ||
| 146 | +then | ||
| 147 | + $ECHO -n "." | ||
| 148 | +else | ||
| 149 | + $ECHO " merged_models2.xml file is not valid" | ||
| 150 | + exit 1 | ||
| 151 | +fi | ||
| 152 | +$ECHO " ok" | ||
| 153 | + | ||
| 154 | + | ||
| 155 | +# | ||
| 156 | +# Test csmodelmerge (wildcard string) | ||
| 157 | +# ============== | ||
| 158 | +$ECHO -n "Test csmodelmerge: " | ||
| 159 | +chmod +x @abs_top_srcdir@/cscripts/csmodelmerge.py | ||
| 160 | +@abs_top_srcdir@/cscripts/csmodelmerge.py \ | ||
| 161 | + inmodels="data/model_cube_background*.xml" \ | ||
| 162 | + outmodel="merged_models3.xml" > csmodelmerge.log | ||
| 163 | +$ECHO -n "." | ||
| 164 | +if [ -s "merged_models3.xml" ] | ||
| 165 | +then | ||
| 166 | + $ECHO -n "." | ||
| 167 | +else | ||
| 168 | + $ECHO " merged_models3.xml file is not valid" | ||
| 169 | + exit 1 | ||
| 170 | +fi | ||
| 171 | +$ECHO " ok" | ||
| 172 | + | ||
| 173 | + | ||
| 174 | +# | ||
| 175 | +# Test csmodelmerge (ascii-file) | ||
| 176 | +# ============== | ||
| 177 | +ls data/model_cube_background* > models.txt | ||
| 178 | +$ECHO -n "Test csmodelmerge: " | ||
| 179 | +chmod +x @abs_top_srcdir@/cscripts/csmodelmerge.py | ||
| 180 | +@abs_top_srcdir@/cscripts/csmodelmerge.py \ | ||
| 181 | + inmodels="@models.txt" \ | ||
| 182 | + outmodel="merged_models4.xml" > csmodelmerge.log | ||
| 183 | +$ECHO -n "." | ||
| 184 | +if [ -s "merged_models4.xml" ] | ||
| 185 | +then | ||
| 186 | + $ECHO -n "." | ||
| 187 | +else | ||
| 188 | + $ECHO " merged_models4.xml file is not valid" | ||
| 189 | + exit 1 | ||
| 190 | +fi | ||
| 191 | +$ECHO " ok" | ||
| 192 | +rm models.txt | ||
| 193 | + | ||
| 194 | + | ||
| 195 | +# | ||
| 99 | # Test cspull | 196 | # Test cspull |
| 100 | # =========== | 197 | # =========== |
| 101 | $ECHO -n "Test cspull: " | 198 | $ECHO -n "Test cspull: " |