Commit d8577cdfd62e5852d20fd7d19314cc54649ae14f
1 parent
ea3e9e13
Integrate code
Showing
3 changed files
with
55 additions
and
47 deletions
cscripts/csiactdata.py
| @@ -25,14 +25,15 @@ import os | @@ -25,14 +25,15 @@ import os | ||
| 25 | import json | 25 | import json |
| 26 | 26 | ||
| 27 | 27 | ||
| 28 | -# =============== # | ||
| 29 | -# cshessobs class # | ||
| 30 | -# =============== # | 28 | +# ================ # |
| 29 | +# csiactdata class # | ||
| 30 | +# ================ # | ||
| 31 | class csiactdata(ctools.cscript): | 31 | class csiactdata(ctools.cscript): |
| 32 | """ | 32 | """ |
| 33 | - This script inspects the available FITS data storage on the user machine and writes | ||
| 34 | - information about the available FITS configurations into a log file or prints it | ||
| 35 | - on the screen. These information can be used as input for 'csiactobs'. | 33 | + This script inspects the available FITS data storage on the user machine |
| 34 | + and writes information about the available FITS configurations into a log | ||
| 35 | + file or prints it on the screen. These information can be used as input | ||
| 36 | + for 'csiactobs'. | ||
| 36 | """ | 37 | """ |
| 37 | def __init__(self, *argv): | 38 | def __init__(self, *argv): |
| 38 | """ | 39 | """ |
| @@ -71,7 +72,8 @@ class csiactdata(ctools.cscript): | @@ -71,7 +72,8 @@ class csiactdata(ctools.cscript): | ||
| 71 | def parfile(self): | 72 | def parfile(self): |
| 72 | """ | 73 | """ |
| 73 | Check if parfile exists. If parfile does not exist then create a | 74 | Check if parfile exists. If parfile does not exist then create a |
| 74 | - default parfile. This kluge avoids shipping the cscript with a parfile. | 75 | + default parfile. This kluge avoids shipping the cscript with a |
| 76 | + parfile. | ||
| 75 | """ | 77 | """ |
| 76 | # Set parfile name | 78 | # Set parfile name |
| 77 | parfile = self.name+".par" | 79 | parfile = self.name+".par" |
| @@ -87,6 +89,7 @@ class csiactdata(ctools.cscript): | @@ -87,6 +89,7 @@ class csiactdata(ctools.cscript): | ||
| 87 | pars.append(gammalib.GApplicationPar("datapath","s","a",self.datapath,"","","Path were data is located")) | 89 | pars.append(gammalib.GApplicationPar("datapath","s","a",self.datapath,"","","Path were data is located")) |
| 88 | pars.append(gammalib.GApplicationPar("master_indx","s","h","master.json","","","Name of master index file")) | 90 | pars.append(gammalib.GApplicationPar("master_indx","s","h","master.json","","","Name of master index file")) |
| 89 | pars.append_standard() | 91 | pars.append_standard() |
| 92 | + pars.append(gammalib.GApplicationPar("logfile","f","h","csiactdata.log","","","Log filename")) | ||
| 90 | pars.save(parfile) | 93 | pars.save(parfile) |
| 91 | 94 | ||
| 92 | # Return | 95 | # Return |
| @@ -111,7 +114,6 @@ class csiactdata(ctools.cscript): | @@ -111,7 +114,6 @@ class csiactdata(ctools.cscript): | ||
| 111 | # Return | 114 | # Return |
| 112 | return | 115 | return |
| 113 | 116 | ||
| 114 | - | ||
| 115 | def execute(self): | 117 | def execute(self): |
| 116 | """ | 118 | """ |
| 117 | Execute the script. | 119 | Execute the script. |
| @@ -145,10 +147,10 @@ class csiactdata(ctools.cscript): | @@ -145,10 +147,10 @@ class csiactdata(ctools.cscript): | ||
| 145 | self.log("\n") | 147 | self.log("\n") |
| 146 | self.log("\n") | 148 | self.log("\n") |
| 147 | 149 | ||
| 148 | - | 150 | + # Open and load JSON file |
| 149 | json_data = open(self.m_master_file).read() | 151 | json_data = open(self.m_master_file).read() |
| 150 | - data = json.loads(json_data) | ||
| 151 | - configs = data["datasets"] | 152 | + data = json.loads(json_data) |
| 153 | + configs = data["datasets"] | ||
| 152 | 154 | ||
| 153 | # Loop over configs and display unavailable storage first | 155 | # Loop over configs and display unavailable storage first |
| 154 | for config in configs: | 156 | for config in configs: |
| @@ -184,7 +186,6 @@ class csiactdata(ctools.cscript): | @@ -184,7 +186,6 @@ class csiactdata(ctools.cscript): | ||
| 184 | # Return | 186 | # Return |
| 185 | return | 187 | return |
| 186 | 188 | ||
| 187 | - | ||
| 188 | # ======================== # | 189 | # ======================== # |
| 189 | # Main routine entry point # | 190 | # Main routine entry point # |
| 190 | # ======================== # | 191 | # ======================== # |
cscripts/csiactobs.py
| @@ -26,14 +26,15 @@ import json | @@ -26,14 +26,15 @@ import json | ||
| 26 | 26 | ||
| 27 | 27 | ||
| 28 | # =============== # | 28 | # =============== # |
| 29 | -# cshessobs class # | 29 | +# csiactobs class # |
| 30 | # =============== # | 30 | # =============== # |
| 31 | class csiactobs(ctools.cscript): | 31 | class csiactobs(ctools.cscript): |
| 32 | """ | 32 | """ |
| 33 | This class implements the creation of a observation xml file for IACT | 33 | This class implements the creation of a observation xml file for IACT |
| 34 | - data analysis. This class is dedicated for use inside a IACT Collaboration, | ||
| 35 | - i.e. it can only be used if you have access to IACT data in FITS format. | ||
| 36 | - The FITS data has to be structured and in the format described here: | 34 | + data analysis. This class is dedicated for use inside a IACT |
| 35 | + Collaboration, i.e. it can only be used if you have access to IACT data | ||
| 36 | + in FITS format. The FITS data has to be structured and in the format | ||
| 37 | + described here: | ||
| 37 | http://gamma-astro-data-formats.readthedocs.org/en/latest/ | 38 | http://gamma-astro-data-formats.readthedocs.org/en/latest/ |
| 38 | """ | 39 | """ |
| 39 | def __init__(self, *argv): | 40 | def __init__(self, *argv): |
| @@ -112,8 +113,8 @@ class csiactobs(ctools.cscript): | @@ -112,8 +113,8 @@ class csiactobs(ctools.cscript): | ||
| 112 | pars.append(gammalib.GApplicationPar("bkg_aeff_norm","r","h","1e-14","","","Input normalisation for effective area background")) | 113 | pars.append(gammalib.GApplicationPar("bkg_aeff_norm","r","h","1e-14","","","Input normalisation for effective area background")) |
| 113 | pars.append(gammalib.GApplicationPar("bkg_aeff_index","r","h","-2.0","","","Input spectral index for effective area background")) | 114 | pars.append(gammalib.GApplicationPar("bkg_aeff_index","r","h","-2.0","","","Input spectral index for effective area background")) |
| 114 | pars.append(gammalib.GApplicationPar("bkg_range_factor","r","h","100.0","","","Factor to determine range of background normalisation")) | 115 | pars.append(gammalib.GApplicationPar("bkg_range_factor","r","h","100.0","","","Factor to determine range of background normalisation")) |
| 115 | - | ||
| 116 | pars.append_standard() | 116 | pars.append_standard() |
| 117 | + pars.append(gammalib.GApplicationPar("logfile","f","h","csiactobs.log","","","Log filename")) | ||
| 117 | pars.save(parfile) | 118 | pars.save(parfile) |
| 118 | 119 | ||
| 119 | # Return | 120 | # Return |
| @@ -131,9 +132,9 @@ class csiactobs(ctools.cscript): | @@ -131,9 +132,9 @@ class csiactobs(ctools.cscript): | ||
| 131 | self.datapath = gammalib.expand_env(self.datapath) | 132 | self.datapath = gammalib.expand_env(self.datapath) |
| 132 | 133 | ||
| 133 | # Read user parameters | 134 | # Read user parameters |
| 134 | - self.m_prodname = self["prodname"].string() | 135 | + self.m_prodname = self["prodname"].string() |
| 135 | self.m_runlistfile = gammalib.expand_env(self["runlistfile"].filename()) | 136 | self.m_runlistfile = gammalib.expand_env(self["runlistfile"].filename()) |
| 136 | - self.m_bkgpars = self["bkgpars"].integer() | 137 | + self.m_bkgpars = self["bkgpars"].integer() |
| 137 | 138 | ||
| 138 | # Output model file | 139 | # Output model file |
| 139 | self.outmodel = self["outmodel"].filename() | 140 | self.outmodel = self["outmodel"].filename() |
| @@ -145,19 +146,19 @@ class csiactobs(ctools.cscript): | @@ -145,19 +146,19 @@ class csiactobs(ctools.cscript): | ||
| 145 | self.m_master_indx = self["master_indx"].string() | 146 | self.m_master_indx = self["master_indx"].string() |
| 146 | 147 | ||
| 147 | # Read hierarchy of file loading | 148 | # Read hierarchy of file loading |
| 148 | - self.m_ev_hiera = self["ev_hiera"].string().split("|") | ||
| 149 | - self.m_aeff_hiera = self["aeff_hiera"].string().split("|") | ||
| 150 | - self.m_psf_hiera = self["psf_hiera"].string().split("|") | ||
| 151 | - self.m_bkg_hiera = self["bkg_hiera"].string().split("|") | ||
| 152 | - self.m_edisp_hiera = self["edisp_hiera"].string().split("|") | 149 | + self.m_ev_hiera = self["ev_hiera"].string().split("|") |
| 150 | + self.m_aeff_hiera = self["aeff_hiera"].string().split("|") | ||
| 151 | + self.m_psf_hiera = self["psf_hiera"].string().split("|") | ||
| 152 | + self.m_bkg_hiera = self["bkg_hiera"].string().split("|") | ||
| 153 | + self.m_edisp_hiera = self["edisp_hiera"].string().split("|") | ||
| 153 | self.m_bkg_mod_hiera = self["bkg_mod_hiera"].string().split("|") | 154 | self.m_bkg_mod_hiera = self["bkg_mod_hiera"].string().split("|") |
| 154 | 155 | ||
| 155 | # Read hidden background parameters | 156 | # Read hidden background parameters |
| 156 | - self.m_bkg_gauss_norm = self["bkg_gauss_norm"].real() | ||
| 157 | - self.m_bkg_gauss_index = self["bkg_gauss_index"].real() | ||
| 158 | - self.m_bkg_gauss_sigma = self["bkg_gauss_sigma"].real() | ||
| 159 | - self.m_bkg_aeff_index = self["bkg_aeff_index"].real() | ||
| 160 | - self.m_bkg_aeff_norm = self["bkg_aeff_norm"].real() | 157 | + self.m_bkg_gauss_norm = self["bkg_gauss_norm"].real() |
| 158 | + self.m_bkg_gauss_index = self["bkg_gauss_index"].real() | ||
| 159 | + self.m_bkg_gauss_sigma = self["bkg_gauss_sigma"].real() | ||
| 160 | + self.m_bkg_aeff_index = self["bkg_aeff_index"].real() | ||
| 161 | + self.m_bkg_aeff_norm = self["bkg_aeff_norm"].real() | ||
| 161 | self.m_bkg_range_factor = self["bkg_range_factor"].real() | 162 | self.m_bkg_range_factor = self["bkg_range_factor"].real() |
| 162 | 163 | ||
| 163 | # Open master index file and look for prodname | 164 | # Open master index file and look for prodname |
| @@ -165,28 +166,32 @@ class csiactobs(ctools.cscript): | @@ -165,28 +166,32 @@ class csiactobs(ctools.cscript): | ||
| 165 | if not os.path.isfile(master_file): | 166 | if not os.path.isfile(master_file): |
| 166 | raise RuntimeError("FITS data store not available. No master index file found. Make sure the file is copied from the server and your datapath is set correctly.") | 167 | raise RuntimeError("FITS data store not available. No master index file found. Make sure the file is copied from the server and your datapath is set correctly.") |
| 167 | 168 | ||
| 169 | + # Open and load JSON file | ||
| 168 | json_data = open(master_file).read() | 170 | json_data = open(master_file).read() |
| 169 | - data = json.loads(json_data) | ||
| 170 | - | 171 | + data = json.loads(json_data) |
| 171 | if not "datasets" in data: | 172 | if not "datasets" in data: |
| 172 | raise RuntimeError("Key \"datasets\" not available in master index file.") | 173 | raise RuntimeError("Key \"datasets\" not available in master index file.") |
| 173 | 174 | ||
| 175 | + # Get configurations | ||
| 174 | configs = data["datasets"] | 176 | configs = data["datasets"] |
| 175 | - | 177 | + |
| 178 | + # Initialise HDUs | ||
| 176 | self.m_hdu_index = self.m_obs_index = "" | 179 | self.m_hdu_index = self.m_obs_index = "" |
| 177 | - | 180 | + |
| 181 | + # Get HDUs | ||
| 178 | for config in configs: | 182 | for config in configs: |
| 179 | if self.m_prodname == config["name"]: | 183 | if self.m_prodname == config["name"]: |
| 180 | self.m_hdu_index = str(os.path.join(self.datapath, config["hduindx"])) | 184 | self.m_hdu_index = str(os.path.join(self.datapath, config["hduindx"])) |
| 181 | self.m_obs_index = str(os.path.join(self.datapath, config["obsindx"])) | 185 | self.m_obs_index = str(os.path.join(self.datapath, config["obsindx"])) |
| 182 | break | 186 | break |
| 183 | - | 187 | + |
| 188 | + # Check HDUs | ||
| 184 | if self.m_hdu_index == "" or self.m_obs_index == "": | 189 | if self.m_hdu_index == "" or self.m_obs_index == "": |
| 185 | raise RuntimeError("FITS data store \""+self.m_prodname+"\" not available. Run csdsinfo to get a list of available storage names") | 190 | raise RuntimeError("FITS data store \""+self.m_prodname+"\" not available. Run csdsinfo to get a list of available storage names") |
| 186 | - | ||
| 187 | if not gammalib.is_fits(self.m_hdu_index+"[HDU_INDEX]"): | 191 | if not gammalib.is_fits(self.m_hdu_index+"[HDU_INDEX]"): |
| 188 | raise RuntimeError("HDU index file \""+self.m_hdu_index+"[HDU_INDEX]\" for FITS data store \""+self.m_prodname+"\" not available. Check your master index file or run csdsinfo to get a list of available storage names.") | 192 | raise RuntimeError("HDU index file \""+self.m_hdu_index+"[HDU_INDEX]\" for FITS data store \""+self.m_prodname+"\" not available. Check your master index file or run csdsinfo to get a list of available storage names.") |
| 189 | - | 193 | + |
| 194 | + # Load index | ||
| 190 | if gammalib.is_fits(self.m_obs_index+"[OBS_INDEX]"): | 195 | if gammalib.is_fits(self.m_obs_index+"[OBS_INDEX]"): |
| 191 | fits = gammalib.GFits(self.m_obs_index) | 196 | fits = gammalib.GFits(self.m_obs_index) |
| 192 | self.has_bkg_scale = fits["OBS_INDEX"].contains("BKG_SCALE") | 197 | self.has_bkg_scale = fits["OBS_INDEX"].contains("BKG_SCALE") |
| @@ -195,8 +200,7 @@ class csiactobs(ctools.cscript): | @@ -195,8 +200,7 @@ class csiactobs(ctools.cscript): | ||
| 195 | self.has_bkg_scale = False | 200 | self.has_bkg_scale = False |
| 196 | 201 | ||
| 197 | # Create base data directory from hdu index file location | 202 | # Create base data directory from hdu index file location |
| 198 | - self.subdir = os.path.dirname(self.m_hdu_index) | ||
| 199 | - | 203 | + self.subdir = os.path.dirname(self.m_hdu_index) |
| 200 | self.m_log = False # Logging in client tools | 204 | self.m_log = False # Logging in client tools |
| 201 | self.m_debug = False # Debugging in client tools | 205 | self.m_debug = False # Debugging in client tools |
| 202 | self.m_clobber = self["clobber"].boolean() | 206 | self.m_clobber = self["clobber"].boolean() |
| @@ -269,9 +273,8 @@ class csiactobs(ctools.cscript): | @@ -269,9 +273,8 @@ class csiactobs(ctools.cscript): | ||
| 269 | 273 | ||
| 270 | # handle IrfBackground | 274 | # handle IrfBackground |
| 271 | if bkgtype == "irf": | 275 | if bkgtype == "irf": |
| 272 | - | ||
| 273 | - prefactor = 1.0 | ||
| 274 | - index = 0.0 | 276 | + prefactor = 1.0 |
| 277 | + index = 0.0 | ||
| 275 | prefactor *= bkg_scale | 278 | prefactor *= bkg_scale |
| 276 | spec = self.background_spectrum(obs_id, prefactor, index, emin, emax) | 279 | spec = self.background_spectrum(obs_id, prefactor, index, emin, emax) |
| 277 | 280 | ||
| @@ -289,11 +292,10 @@ class csiactobs(ctools.cscript): | @@ -289,11 +292,10 @@ class csiactobs(ctools.cscript): | ||
| 289 | 292 | ||
| 290 | # Set Gaussian Background | 293 | # Set Gaussian Background |
| 291 | elif self.bkgtype == "gauss": | 294 | elif self.bkgtype == "gauss": |
| 292 | - | ||
| 293 | prefactor = bkg_scale * self.m_bkg_gauss_norm | 295 | prefactor = bkg_scale * self.m_bkg_gauss_norm |
| 294 | - spec = self.background_spectrum(obs_id, prefactor, self.m_bkg_gauss_index, emin, emax) | ||
| 295 | - radial = gammalib.GCTAModelRadialGauss(self.m_bkg_gauss_sigma) | ||
| 296 | - bck = gammalib.GCTAModelRadialAcceptance(radial, spec) | 296 | + spec = self.background_spectrum(obs_id, prefactor, self.m_bkg_gauss_index, emin, emax) |
| 297 | + radial = gammalib.GCTAModelRadialGauss(self.m_bkg_gauss_sigma) | ||
| 298 | + bck = gammalib.GCTAModelRadialAcceptance(radial, spec) | ||
| 297 | 299 | ||
| 298 | else: | 300 | else: |
| 299 | sys.exit("Background type \""+self.bkgtype+"\" unsupported") | 301 | sys.exit("Background type \""+self.bkgtype+"\" unsupported") |
| @@ -315,8 +317,7 @@ class csiactobs(ctools.cscript): | @@ -315,8 +317,7 @@ class csiactobs(ctools.cscript): | ||
| 315 | 317 | ||
| 316 | # Return model | 318 | # Return model |
| 317 | return model | 319 | return model |
| 318 | - | ||
| 319 | - | 320 | + |
| 320 | def erange(self): | 321 | def erange(self): |
| 321 | """ | 322 | """ |
| 322 | Returns runlist energy range | 323 | Returns runlist energy range |
| @@ -606,6 +607,8 @@ class csiactobs(ctools.cscript): | @@ -606,6 +607,8 @@ class csiactobs(ctools.cscript): | ||
| 606 | # Save model XML file | 607 | # Save model XML file |
| 607 | self.models.save(self.outmodel) | 608 | self.models.save(self.outmodel) |
| 608 | 609 | ||
| 610 | + # Return | ||
| 611 | + return | ||
| 609 | 612 | ||
| 610 | # ======================== # | 613 | # ======================== # |
| 611 | # Main routine entry point # | 614 | # Main routine entry point # |
test/test_cscripts.sh.in
| @@ -50,6 +50,7 @@ cp -r ../src/*/*.par pfiles/ | @@ -50,6 +50,7 @@ cp -r ../src/*/*.par pfiles/ | ||
| 50 | rm -rf $CALDB | 50 | rm -rf $CALDB |
| 51 | mkdir -p $CALDB | 51 | mkdir -p $CALDB |
| 52 | 52 | ||
| 53 | + | ||
| 53 | # | 54 | # |
| 54 | # Test csobsdef | 55 | # Test csobsdef |
| 55 | # ============= | 56 | # ============= |
| @@ -74,6 +75,7 @@ else | @@ -74,6 +75,7 @@ else | ||
| 74 | fi | 75 | fi |
| 75 | $ECHO " ok" | 76 | $ECHO " ok" |
| 76 | 77 | ||
| 78 | + | ||
| 77 | # | 79 | # |
| 78 | # Test csobs2caldb | 80 | # Test csobs2caldb |
| 79 | # ================ | 81 | # ================ |
| @@ -93,6 +95,7 @@ else | @@ -93,6 +95,7 @@ else | ||
| 93 | fi | 95 | fi |
| 94 | $ECHO " ok" | 96 | $ECHO " ok" |
| 95 | 97 | ||
| 98 | + | ||
| 96 | # | 99 | # |
| 97 | # Test csobsinfo | 100 | # Test csobsinfo |
| 98 | # ============== | 101 | # ============== |
| @@ -114,6 +117,7 @@ else | @@ -114,6 +117,7 @@ else | ||
| 114 | fi | 117 | fi |
| 115 | $ECHO " ok" | 118 | $ECHO " ok" |
| 116 | 119 | ||
| 120 | + | ||
| 117 | # | 121 | # |
| 118 | # Test csiactobs | 122 | # Test csiactobs |
| 119 | # ============== | 123 | # ============== |