Commit 70e9743c31b91678bd8ed538a7f5e1aea5d8c24c
1 parent
e7b90c18
Try to resolve some Sonar issues.
Showing
9 changed files
with
72 additions
and
74 deletions
cscripts/cspull.py
| ... | ... | @@ -18,7 +18,7 @@ |
| 18 | 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | 19 | # |
| 20 | 20 | # ========================================================================== |
| 21 | -import gammalib | |
| 21 | +#import gammalib | |
| 22 | 22 | import ctools |
| 23 | 23 | from cscripts import obsutils |
| 24 | 24 | import sys |
| ... | ... | @@ -410,17 +410,17 @@ class cspull(ctools.cscript): |
| 410 | 410 | |
| 411 | 411 | # Write out result immediately |
| 412 | 412 | if seed == 0: |
| 413 | - file = open(self._outfile.url(), 'w') | |
| 414 | - writer = csv.DictWriter(file, result['colnames']) | |
| 413 | + f = open(self._outfile.url(), 'w') | |
| 414 | + writer = csv.DictWriter(f, result['colnames']) | |
| 415 | 415 | headers = {} |
| 416 | 416 | for n in result['colnames']: |
| 417 | 417 | headers[n] = n |
| 418 | 418 | writer.writerow(headers) |
| 419 | 419 | else: |
| 420 | - file = open(self._outfile.url(), 'a') | |
| 421 | - writer = csv.DictWriter(file, result['colnames']) | |
| 420 | + f = open(self._outfile.url(), 'a') | |
| 421 | + writer = csv.DictWriter(f, result['colnames']) | |
| 422 | 422 | writer.writerow(result['values']) |
| 423 | - file.close() | |
| 423 | + f.close() | |
| 424 | 424 | |
| 425 | 425 | # Return |
| 426 | 426 | return | ... | ... |
cscripts/cssens.py
| ... | ... | @@ -347,18 +347,18 @@ class cssens(ctools.cscript): |
| 347 | 347 | photon_flux_value = [] |
| 348 | 348 | energy_flux_value = [] |
| 349 | 349 | sensitivity_value = [] |
| 350 | - iter = 0 | |
| 350 | + iterations = 0 | |
| 351 | 351 | test_crab_flux = 0.1 # Initial test flux in Crab units (100 mCrab) |
| 352 | 352 | |
| 353 | 353 | # Loop until we break |
| 354 | 354 | while True: |
| 355 | 355 | |
| 356 | 356 | # Update iteration counter |
| 357 | - iter += 1 | |
| 357 | + iterations += 1 | |
| 358 | 358 | |
| 359 | 359 | # Write header |
| 360 | 360 | if self._logExplicit(): |
| 361 | - self._log.header2("Iteration "+str(iter)) | |
| 361 | + self._log.header2("Iteration "+str(iterations)) | |
| 362 | 362 | |
| 363 | 363 | # Set source model. crab_prefactor is the Prefactor that |
| 364 | 364 | # corresponds to 1 Crab |
| ... | ... | @@ -370,7 +370,7 @@ class cssens(ctools.cscript): |
| 370 | 370 | self._obs.models(src_model) |
| 371 | 371 | |
| 372 | 372 | # Simulate events |
| 373 | - sim = obsutils.sim(self._obs, nbins=self._enumbins, seed=iter, | |
| 373 | + sim = obsutils.sim(self._obs, nbins=self._enumbins, seed=iterations, | |
| 374 | 374 | binsz=self._binsz, npix=self._npix, |
| 375 | 375 | log=self._log_clients, debug=self._debug, |
| 376 | 376 | edisp=self._edisp) |
| ... | ... | @@ -464,8 +464,8 @@ class cssens(ctools.cscript): |
| 464 | 464 | # Get fitted Crab, photon and energy fluxes |
| 465 | 465 | crab_flux = result_all[self._srcname]['Prefactor'].value() / \ |
| 466 | 466 | crab_prefactor |
| 467 | - crab_flux_err = result_all[self._srcname]['Prefactor'].error() / \ | |
| 468 | - crab_prefactor | |
| 467 | + #crab_flux_err = result_all[self._srcname]['Prefactor'].error() / \ | |
| 468 | + # crab_prefactor | |
| 469 | 469 | photon_flux = result_all[self._srcname].spectral().flux(emin, emax) |
| 470 | 470 | energy_flux = result_all[self._srcname].spectral().eflux(emin, emax) |
| 471 | 471 | |
| ... | ... | @@ -511,7 +511,7 @@ class cssens(ctools.cscript): |
| 511 | 511 | for par in model: |
| 512 | 512 | self._log(str(par)+"\n") |
| 513 | 513 | elif self._logTerse(): |
| 514 | - self._log.parformat("Iteration "+str(iter)) | |
| 514 | + self._log.parformat("Iteration "+str(iterations)) | |
| 515 | 515 | self._log("TS=") |
| 516 | 516 | self._log(ts) |
| 517 | 517 | self._log(" ") |
| ... | ... | @@ -549,7 +549,7 @@ class cssens(ctools.cscript): |
| 549 | 549 | sensitivity /= num |
| 550 | 550 | |
| 551 | 551 | # Compare average flux to last average |
| 552 | - if iter > self._num_avg: | |
| 552 | + if iterations > self._num_avg: | |
| 553 | 553 | if test_crab_flux > 0: |
| 554 | 554 | ratio = crab_flux/test_crab_flux |
| 555 | 555 | |
| ... | ... | @@ -570,7 +570,7 @@ class cssens(ctools.cscript): |
| 570 | 570 | test_crab_flux = crab_flux |
| 571 | 571 | |
| 572 | 572 | # Exit loop if number of trials exhausted |
| 573 | - if (iter >= self._max_iter): | |
| 573 | + if (iterations >= self._max_iter): | |
| 574 | 574 | if self._logTerse(): |
| 575 | 575 | self._log(" Test ended after "+str(self._max_iter)+ |
| 576 | 576 | " iterations.\n") | ... | ... |
cscripts/csspec.py
| ... | ... | @@ -158,9 +158,10 @@ class csspec(ctools.cscript): |
| 158 | 158 | |
| 159 | 159 | # If no layers overlap then throw an exception |
| 160 | 160 | if (len(use_layers) == 0): |
| 161 | - msg = "Energy range ["+str(cube_ebounds.emin(i))+ \ | |
| 162 | - ", "+str(cube_ebounds.emax(i))+"] of counts "+ \ | |
| 163 | - "cube does not overlap with specified energy range ["+ \ | |
| 161 | + msg = "Energy range ["+str(cube_ebounds.emin())+ \ | |
| 162 | + ", "+str(cube_ebounds.emax())+"] of counts "+ \ | |
| 163 | + "cube does not overlap with specified energy "+ \ | |
| 164 | + "range ["+ \ | |
| 164 | 165 | str(emin)+", "+str(emax)+"]. Specify "+ \ |
| 165 | 166 | "overlapping energy range." |
| 166 | 167 | raise RuntimeError(msg) | ... | ... |
cscripts/cstsdist.py
| ... | ... | @@ -419,17 +419,17 @@ class cstsdist(ctools.cscript): |
| 419 | 419 | |
| 420 | 420 | # Write out result immediately |
| 421 | 421 | if seed == 0: |
| 422 | - file = open(self._outfile.url(), 'w') | |
| 423 | - writer = csv.DictWriter(file, result['colnames']) | |
| 422 | + f = open(self._outfile.url(), 'w') | |
| 423 | + writer = csv.DictWriter(f, result['colnames']) | |
| 424 | 424 | headers = {} |
| 425 | 425 | for n in result['colnames']: |
| 426 | 426 | headers[n] = n |
| 427 | 427 | writer.writerow(headers) |
| 428 | 428 | else: |
| 429 | - file = open(self._outfile.url(), 'a') | |
| 430 | - writer = csv.DictWriter(file, result['colnames']) | |
| 429 | + f = open(self._outfile.url(), 'a') | |
| 430 | + writer = csv.DictWriter(f, result['colnames']) | |
| 431 | 431 | writer.writerow(result['values']) |
| 432 | - file.close() | |
| 432 | + f.close() | |
| 433 | 433 | |
| 434 | 434 | # Return |
| 435 | 435 | return | ... | ... |
cscripts/cstsmapmerge.py
| ... | ... | @@ -414,8 +414,8 @@ class cstsmapmerge(ctools.cscript): |
| 414 | 414 | # Delete TS input maps if requested |
| 415 | 415 | if self._delete: |
| 416 | 416 | for filename in self._merged_files: |
| 417 | - os.remove(filename) | |
| 418 | - if self._logTerse(): | |
| 417 | + os.remove(filename) | |
| 418 | + if self._logTerse(): | |
| 419 | 419 | self._log(gammalib.parformat("Deleted input file")) |
| 420 | 420 | self._log(filename) |
| 421 | 421 | self._log("\n") | ... | ... |
cscripts/cta_kb_bkgrates.py
| ... | ... | @@ -4,7 +4,7 @@ |
| 4 | 4 | # into a column separated file function table. The table is automatically |
| 5 | 5 | # named "bkg_XXX.txt" where "XXX" is for example "kb_E_50h_v3". |
| 6 | 6 | # |
| 7 | -# Copyright (C) 2011-2014 Juergen Knoedlseder | |
| 7 | +# Copyright (C) 2011-2016 Juergen Knoedlseder | |
| 8 | 8 | # |
| 9 | 9 | # This program is free software: you can redistribute it and/or modify |
| 10 | 10 | # it under the terms of the GNU General Public License as published by |
| ... | ... | @@ -38,8 +38,8 @@ def make_file_function(irfname, filename): |
| 38 | 38 | filename - File function filename |
| 39 | 39 | """ |
| 40 | 40 | # Open performance file and file function |
| 41 | - irf = open(irfname, "r") | |
| 42 | - file = open(filename, "w") | |
| 41 | + irf = open(irfname, "r") | |
| 42 | + f = open(filename, "w") | |
| 43 | 43 | |
| 44 | 44 | # Read data |
| 45 | 45 | for row in irf: |
| ... | ... | @@ -53,12 +53,12 @@ def make_file_function(irfname, filename): |
| 53 | 53 | # Extract data |
| 54 | 54 | fields = row.replace(" ",",").replace(",,",",").rstrip("\n").split(",") |
| 55 | 55 | logE = float(fields[0]) |
| 56 | - aeff = float(fields[1]) | |
| 57 | - r68 = float(fields[2]) | |
| 56 | + #aeff = float(fields[1]) | |
| 57 | + #r68 = float(fields[2]) | |
| 58 | 58 | r80 = float(fields[3]) |
| 59 | - eres = float(fields[4]) | |
| 59 | + #eres = float(fields[4]) | |
| 60 | 60 | bgd = float(fields[5]) |
| 61 | - sens = float(fields[6]) | |
| 61 | + #sens = float(fields[6]) | |
| 62 | 62 | |
| 63 | 63 | # Compute energy (in MeV) |
| 64 | 64 | energy = math.pow(10.0, logE)*1.0e6 |
| ... | ... | @@ -76,11 +76,11 @@ def make_file_function(irfname, filename): |
| 76 | 76 | bkg_rate = 0.0 |
| 77 | 77 | |
| 78 | 78 | # Write |
| 79 | - file.write(str(energy)+" "+str(bkg_rate)+"\n") | |
| 79 | + f.write(str(energy)+" "+str(bkg_rate)+"\n") | |
| 80 | 80 | |
| 81 | 81 | # Close files |
| 82 | 82 | irf.close() |
| 83 | - file.close() | |
| 83 | + f.close() | |
| 84 | 84 | |
| 85 | 85 | # Return |
| 86 | 86 | return |
| ... | ... | @@ -90,9 +90,7 @@ def make_file_function(irfname, filename): |
| 90 | 90 | # Main entry point # |
| 91 | 91 | # ================ # |
| 92 | 92 | if __name__ == '__main__': |
| 93 | - """ | |
| 94 | - Main entry point | |
| 95 | - """ | |
| 93 | + | |
| 96 | 94 | # Set parameters |
| 97 | 95 | caldb = "/usr/local/gamma/share/caldb/data/cta/kb/" |
| 98 | 96 | ... | ... |
cscripts/cta_kb_root2caldb.py
| ... | ... | @@ -23,7 +23,8 @@ |
| 23 | 23 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 24 | 24 | # |
| 25 | 25 | # ========================================================================== |
| 26 | -from ROOT import TFile, TH1F, TH2F | |
| 26 | +#from ROOT import TFile, TH1F, TH2F | |
| 27 | +from ROOT import TFile, TH1F | |
| 27 | 28 | from datetime import datetime |
| 28 | 29 | import gammalib |
| 29 | 30 | import math |
| ... | ... | @@ -506,11 +507,11 @@ if __name__ == '__main__': |
| 506 | 507 | """ |
| 507 | 508 | """ |
| 508 | 509 | # Get ROOT filename from command line argument |
| 509 | - if (len(sys.argv) > 1): | |
| 510 | - filename = sys.argv[1] | |
| 511 | - else: | |
| 512 | - db = "/Users/jurgen/Documents/Travail/Projects/CTA/WP-MC/root/Kb_all_20deg_root" | |
| 513 | - #db = "/Users/jurgen/Documents/Travail/Projects/CTA/WP-MC/root/Kb_all_50deg_root" | |
| 510 | + #if (len(sys.argv) > 1): | |
| 511 | + # filename = sys.argv[1] | |
| 512 | + #else: | |
| 513 | + db = "/Users/jurgen/Documents/Travail/Projects/CTA/WP-MC/root/Kb_all_20deg_root" | |
| 514 | + #db = "/Users/jurgen/Documents/Travail/Projects/CTA/WP-MC/root/Kb_all_50deg_root" | |
| 514 | 515 | |
| 515 | 516 | # Get list of all performance files |
| 516 | 517 | files = glob.glob(db+"/*.root") |
| ... | ... | @@ -520,26 +521,26 @@ if __name__ == '__main__': |
| 520 | 521 | irf = caldb("kb") |
| 521 | 522 | |
| 522 | 523 | # Loop over all files |
| 523 | - for file in files: | |
| 524 | + for f in files: | |
| 524 | 525 | |
| 525 | 526 | # Get filename |
| 526 | - head, tail = os.path.split(file) | |
| 527 | + head, tail = os.path.split(f) | |
| 527 | 528 | |
| 528 | 529 | # Build name |
| 529 | 530 | name = tail.strip(".root") |
| 530 | 531 | if name.find("kb_") != -1: |
| 531 | 532 | name = name.replace("_20deg", "") |
| 532 | 533 | elif name.find("IFAE_") != -1: |
| 533 | - name = name.replace("Subarray", "") | |
| 534 | - name = name.replace("IFAE_", "") | |
| 535 | - name = "IFAE_"+name | |
| 536 | - i = name.find("hours") | |
| 537 | - if i != -1: | |
| 538 | - name = name[0:i+1] | |
| 534 | + name = name.replace("Subarray", "") | |
| 535 | + name = name.replace("IFAE_", "") | |
| 536 | + name = "IFAE_"+name | |
| 537 | + index = name.find("hours") | |
| 538 | + if index != -1: | |
| 539 | + name = name[0:index+1] | |
| 539 | 540 | print(tail) |
| 540 | 541 | |
| 541 | 542 | # Open calibration file |
| 542 | 543 | irf.add(name) |
| 543 | 544 | |
| 544 | 545 | # Translate ROOT to CALDB information |
| 545 | - irf.root2caldb(file) | |
| 546 | + irf.root2caldb(f) | ... | ... |
cscripts/cta_make_nodes.py
| ... | ... | @@ -20,7 +20,6 @@ |
| 20 | 20 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | 21 | # |
| 22 | 22 | # ========================================================================== |
| 23 | -#from gammalib import * | |
| 24 | 23 | import sys |
| 25 | 24 | import math |
| 26 | 25 | |
| ... | ... | @@ -28,7 +27,7 @@ import math |
| 28 | 27 | # ============== # |
| 29 | 28 | # Write one node # |
| 30 | 29 | # ============== # |
| 31 | -def write_node(file, energy, scale): | |
| 30 | +def write_node(f, energy, scale): | |
| 32 | 31 | """ |
| 33 | 32 | Writes one node to XML file. |
| 34 | 33 | """ |
| ... | ... | @@ -37,22 +36,22 @@ def write_node(file, energy, scale): |
| 37 | 36 | str_scale = str(scale) |
| 38 | 37 | |
| 39 | 38 | # Write start tag |
| 40 | - file.write(' <node>\n') | |
| 39 | + f.write(' <node>\n') | |
| 41 | 40 | |
| 42 | 41 | # Write energy |
| 43 | - file.write(' <parameter scale="1e6" name="Energy"') | |
| 44 | - file.write(' min="'+str_energy+'" max="'+str_energy+'"') | |
| 45 | - file.write(' value="'+str_energy+'"') | |
| 46 | - file.write(' free="0"/>\n') | |
| 42 | + f.write(' <parameter scale="1e6" name="Energy"') | |
| 43 | + f.write(' min="'+str_energy+'" max="'+str_energy+'"') | |
| 44 | + f.write(' value="'+str_energy+'"') | |
| 45 | + f.write(' free="0"/>\n') | |
| 47 | 46 | |
| 48 | 47 | # Write intensity |
| 49 | - file.write(' <parameter scale="'+str_scale+'" name="Intensity"') | |
| 50 | - file.write(' min="1e-5" max="1e5"') | |
| 51 | - file.write(' value="1.0"') | |
| 52 | - file.write(' free="1"/>\n') | |
| 48 | + f.write(' <parameter scale="'+str_scale+'" name="Intensity"') | |
| 49 | + f.write(' min="1e-5" max="1e5"') | |
| 50 | + f.write(' value="1.0"') | |
| 51 | + f.write(' free="1"/>\n') | |
| 53 | 52 | |
| 54 | 53 | # Write end tag |
| 55 | - file.write(' </node>\n') | |
| 54 | + f.write(' </node>\n') | |
| 56 | 55 | |
| 57 | 56 | # Return |
| 58 | 57 | return |
| ... | ... | @@ -66,7 +65,7 @@ def create_nodes(emin, emax, enumbins): |
| 66 | 65 | Create nodes (energies in TeV). |
| 67 | 66 | """ |
| 68 | 67 | # Open file |
| 69 | - file = open("nodes.xml", "w") | |
| 68 | + f = open("nodes.xml", "w") | |
| 70 | 69 | |
| 71 | 70 | # Set node boundaries |
| 72 | 71 | elogmin = math.log10(float(emin)) |
| ... | ... | @@ -83,14 +82,14 @@ def create_nodes(emin, emax, enumbins): |
| 83 | 82 | scale = 3.45e-17 * math.pow(energy, -2.63) |
| 84 | 83 | |
| 85 | 84 | # Write node |
| 86 | - write_node(file, energy, scale) | |
| 85 | + write_node(f, energy, scale) | |
| 87 | 86 | |
| 88 | 87 | # Debug |
| 89 | 88 | #sys.stdout.write(energy) |
| 90 | 89 | #sys.stdout.write(math.pow(10.0, int(math.log10(scale))-1.0)±"\n") |
| 91 | 90 | |
| 92 | 91 | # Close file |
| 93 | - file.close() | |
| 92 | + f.close() | |
| 94 | 93 | |
| 95 | 94 | # Return |
| 96 | 95 | return |
| ... | ... | @@ -100,9 +99,7 @@ def create_nodes(emin, emax, enumbins): |
| 100 | 99 | # Main script entry point # |
| 101 | 100 | # ======================= # |
| 102 | 101 | if __name__ == '__main__': |
| 103 | - """ | |
| 104 | - Compute nodes. | |
| 105 | - """ | |
| 102 | + | |
| 106 | 103 | # Check command line |
| 107 | 104 | usage = "Usage: cta_make_nodes emin emax enumbins" |
| 108 | 105 | if len(sys.argv) < 3: | ... | ... |
cscripts/cta_root2caldb.py
| ... | ... | @@ -3,7 +3,7 @@ |
| 3 | 3 | # This script generates IRFs in the CALDB format of HEASARC using ROOT |
| 4 | 4 | # offaxis performance files. |
| 5 | 5 | # ------------------------------------------------------------------------- |
| 6 | -# Copyright (C) 2011-2015 Juergen Knoedlseder | |
| 6 | +# Copyright (C) 2011-2016 Juergen Knoedlseder | |
| 7 | 7 | # ------------------------------------------------------------------------- |
| 8 | 8 | # |
| 9 | 9 | # This program is free software: you can redistribute it and/or modify |
| ... | ... | @@ -24,12 +24,13 @@ |
| 24 | 24 | # - gammalib |
| 25 | 25 | # - pyroot |
| 26 | 26 | # ========================================================================== |
| 27 | -from ROOT import TFile, TH1F, TH2F, TH3F | |
| 27 | +#from ROOT import TFile, TH1F, TH2F, TH3F | |
| 28 | +from ROOT import TFile, TH2F, TH3F | |
| 28 | 29 | import gammalib |
| 29 | 30 | from datetime import datetime |
| 30 | 31 | import math |
| 31 | -import sys | |
| 32 | -import glob | |
| 32 | +#import sys | |
| 33 | +#import glob | |
| 33 | 34 | import os |
| 34 | 35 | |
| 35 | 36 | ... | ... |