Commit bc55f554e3db5e321cd4a40d5006ac7c3169fb91

Authored by Michael Mayer
1 parent 51c7615b

Add correct version of cstsmapmerge

Showing 1 changed file with 22 additions and 126 deletions
cscripts/cstsmapmerge.py
@@ -30,17 +30,17 @@ import ctools @@ -30,17 +30,17 @@ import ctools
30 # ================== # 30 # ================== #
31 class cstsmapmerge(ctools.cscript): 31 class cstsmapmerge(ctools.cscript):
32 """ 32 """
33 - Merge Test Statistic maps 33 + Merge Test Statistic maps.
34 """ 34 """
35 35
36 # Constructor 36 # Constructor
37 def __init__(self, *argv): 37 def __init__(self, *argv):
38 """ 38 """
39 - Constructor 39 + Constructor.
40 """ 40 """
41 # Set name 41 # Set name
42 self._name = 'cstsmapmerge' 42 self._name = 'cstsmapmerge'
43 - self._version = '1.2.0' 43 + self._version = '1.1.0'
44 44
45 # Initialise class members 45 # Initialise class members
46 self._files = None 46 self._files = None
@@ -64,7 +64,7 @@ class cstsmapmerge(ctools.cscript): @@ -64,7 +64,7 @@ class cstsmapmerge(ctools.cscript):
64 # Private methods 64 # Private methods
65 def _get_parameters(self): 65 def _get_parameters(self):
66 """ 66 """
67 - Get parameters from parfile 67 + Get parameters from parfile.
68 """ 68 """
69 # Get input maps string 69 # Get input maps string
70 inmaps = self['inmaps'].string() 70 inmaps = self['inmaps'].string()
@@ -118,11 +118,7 @@ class cstsmapmerge(ctools.cscript): @@ -118,11 +118,7 @@ class cstsmapmerge(ctools.cscript):
118 if self._read_ahead(): 118 if self._read_ahead():
119 self['outmap'].filename() 119 self['outmap'].filename()
120 120
121 -<<<<<<< HEAD  
122 - # Write input parameters into logger  
123 -=======  
124 # Write input parameters into logger 121 # Write input parameters into logger
125 ->>>>>>> b7dd76b35e6d2fd74ff010258ffa7606e072dbf5  
126 self._log_parameters(gammalib.TERSE) 122 self._log_parameters(gammalib.TERSE)
127 123
128 # Return 124 # Return
@@ -130,12 +126,7 @@ class cstsmapmerge(ctools.cscript): @@ -130,12 +126,7 @@ class cstsmapmerge(ctools.cscript):
130 126
131 def _init_ts_map(self, fitsfile): 127 def _init_ts_map(self, fitsfile):
132 """ 128 """
133 - Initialise Test Statistic map  
134 -  
135 - Parameters  
136 - ----------  
137 - fitsfile : str  
138 - FITS file to be merged 129 + Initialise Test Statistic map.
139 """ 130 """
140 # Set filename 131 # Set filename
141 self._in_filename = fitsfile 132 self._in_filename = fitsfile
@@ -157,11 +148,7 @@ class cstsmapmerge(ctools.cscript): @@ -157,11 +148,7 @@ class cstsmapmerge(ctools.cscript):
157 for hdu in fits: 148 for hdu in fits:
158 149
159 # Leave out primary and status extension 150 # Leave out primary and status extension
160 -<<<<<<< HEAD  
161 - if hdu.extname() != 'IMAGE' and hdu.extname() != 'STATUS MAP':  
162 -=======  
163 if hdu.extname() != "IMAGE" and hdu.extname() != 'STATUS MAP': 151 if hdu.extname() != "IMAGE" and hdu.extname() != 'STATUS MAP':
164 ->>>>>>> b7dd76b35e6d2fd74ff010258ffa7606e072dbf5  
165 152
166 # Add present maps 153 # Add present maps
167 skymap = gammalib.GSkyMap() 154 skymap = gammalib.GSkyMap()
@@ -177,12 +164,10 @@ class cstsmapmerge(ctools.cscript): @@ -177,12 +164,10 @@ class cstsmapmerge(ctools.cscript):
177 164
178 def _merge_ts_map(self, fitsfile): 165 def _merge_ts_map(self, fitsfile):
179 """ 166 """
180 - Merge TS map from FITS file into output TS map 167 + Merge TS map from FITS file into output TS map.
181 168
182 - Parameters  
183 - ----------  
184 - fitsfile : str  
185 - FITS file to be merged 169 + Args:
  170 + fitsfile: FITS file to be merged.
186 """ 171 """
187 # Open FITS file 172 # Open FITS file
188 fits = gammalib.GFits(fitsfile) 173 fits = gammalib.GFits(fitsfile)
@@ -200,11 +185,7 @@ class cstsmapmerge(ctools.cscript): @@ -200,11 +185,7 @@ class cstsmapmerge(ctools.cscript):
200 for hdu in fits: 185 for hdu in fits:
201 186
202 # Leave out primary and status extension 187 # Leave out primary and status extension
203 -<<<<<<< HEAD  
204 - if hdu.extname() != 'IMAGE' and hdu.extname() != 'STATUS MAP':  
205 -=======  
206 if hdu.extname() != "IMAGE" and hdu.extname() != 'STATUS MAP': 188 if hdu.extname() != "IMAGE" and hdu.extname() != 'STATUS MAP':
207 ->>>>>>> b7dd76b35e6d2fd74ff010258ffa7606e072dbf5  
208 189
209 # Add present maps 190 # Add present maps
210 skymap = gammalib.GSkyMap() 191 skymap = gammalib.GSkyMap()
@@ -216,9 +197,9 @@ class cstsmapmerge(ctools.cscript): @@ -216,9 +197,9 @@ class cstsmapmerge(ctools.cscript):
216 197
217 # Compare size of maps 198 # Compare size of maps
218 if not len(add_maps) == len(self._maps): 199 if not len(add_maps) == len(self._maps):
219 - msg = ('Cannot merge map "%s" into map "%s" since the number of '  
220 - 'parameters between both maps is different.' %  
221 - (fitsfile, self._in_filename)) 200 + msg = "Cannot merge map \""+fitsfile+"\" into map \""+\
  201 + self._in_filename+"\" since the number of parameters "\
  202 + "between both maps is different."
222 raise RuntimeError(msg) 203 raise RuntimeError(msg)
223 204
224 # Loop over bins 205 # Loop over bins
@@ -228,19 +209,11 @@ class cstsmapmerge(ctools.cscript): @@ -228,19 +209,11 @@ class cstsmapmerge(ctools.cscript):
228 if add_statusmap[i] > -0.5: 209 if add_statusmap[i] > -0.5:
229 210
230 # Raise exception if this bin has already been computed 211 # Raise exception if this bin has already been computed
231 -<<<<<<< HEAD  
232 - if self._statusmap[i] > 0.5 and not self._overwrite:  
233 - msg = ('Attempt to merge bin which apparently has already '  
234 - 'been merged. File "%s" contains already merged '  
235 - 'bins. Set hidden parameter "overwrite=yes" to '  
236 - 'avoid this error.' % fitsfile)  
237 -=======  
238 if self._statusmap[i] > -0.5 and not self._overwrite: 212 if self._statusmap[i] > -0.5 and not self._overwrite:
239 msg = "Attempt to merge bin which apparently has "+\ 213 msg = "Attempt to merge bin which apparently has "+\
240 "already been merged. File \""+fitsfile+"\" "+\ 214 "already been merged. File \""+fitsfile+"\" "+\
241 "contains already merged bins. Set hidden "+\ 215 "contains already merged bins. Set hidden "+\
242 "parameter \"overwrite=yes\" to avoid this error." 216 "parameter \"overwrite=yes\" to avoid this error."
243 ->>>>>>> b7dd76b35e6d2fd74ff010258ffa7606e072dbf5  
244 raise RuntimeError(msg) 217 raise RuntimeError(msg)
245 218
246 # Copy TS values 219 # Copy TS values
@@ -258,20 +231,16 @@ class cstsmapmerge(ctools.cscript): @@ -258,20 +231,16 @@ class cstsmapmerge(ctools.cscript):
258 231
259 def _get_number_of_ts_pixels(self, fitsfile): 232 def _get_number_of_ts_pixels(self, fitsfile):
260 """ 233 """
261 - Return number of pixels with TS values 234 + Return number of pixels with TS values.
262 235
263 - Parameters  
264 - ----------  
265 - fitsfile : str  
266 - FITS file to be merged 236 + Args:
  237 + fitsfile: FITS file to be merged.
267 238
268 - Returns  
269 - -------  
270 - counts : int  
271 - Number of pixels for which TS has been computed 239 + Returns:
  240 + Number of pixels for which TS has been computed.
272 """ 241 """
273 # Get status map for this file 242 # Get status map for this file
274 - status = gammalib.GSkyMap(fitsfile+'[STATUS MAP]') 243 + status = gammalib.GSkyMap(fitsfile+"[STATUS MAP]")
275 244
276 # Count number of pixels with TS status set 245 # Count number of pixels with TS status set
277 count = 0 246 count = 0
@@ -286,7 +255,7 @@ class cstsmapmerge(ctools.cscript): @@ -286,7 +255,7 @@ class cstsmapmerge(ctools.cscript):
286 # Public methods 255 # Public methods
287 def run(self): 256 def run(self):
288 """ 257 """
289 - Run the script 258 + Run the script.
290 """ 259 """
291 # Switch screen logging on in debug mode 260 # Switch screen logging on in debug mode
292 if self._logDebug(): 261 if self._logDebug():
@@ -296,12 +265,10 @@ class cstsmapmerge(ctools.cscript): @@ -296,12 +265,10 @@ class cstsmapmerge(ctools.cscript):
296 self._get_parameters() 265 self._get_parameters()
297 266
298 # Write header into logger 267 # Write header into logger
299 - if self._logTerse():  
300 - self._log('\n')  
301 - self._log.header1('Merge TS maps') 268 + self._log_header1(gammalib.TERSE, "Merge TS maps")
302 269
303 # Initialise file to start with 270 # Initialise file to start with
304 - file0 = '' 271 + file0 = ""
305 self._merged_files = [] 272 self._merged_files = []
306 273
307 # Test files for the entry status map. Use the first one to appear 274 # Test files for the entry status map. Use the first one to appear
@@ -310,16 +277,11 @@ class cstsmapmerge(ctools.cscript): @@ -310,16 +277,11 @@ class cstsmapmerge(ctools.cscript):
310 277
311 # Skip file if it's not a FITS file 278 # Skip file if it's not a FITS file
312 if not gammalib.GFilename(fitsfile).is_fits(): 279 if not gammalib.GFilename(fitsfile).is_fits():
313 -<<<<<<< HEAD  
314 - self._log_value(gammalib.EXPLICIT, 'Skip file',  
315 - '%s (not a FITS file)' % fitsfile)  
316 -=======  
317 280
318 # Log message 281 # Log message
319 self._log_value(gammalib.EXPLICIT, "Skip file", fitsfile +" (not a FITS file)") 282 self._log_value(gammalib.EXPLICIT, "Skip file", fitsfile +" (not a FITS file)")
320 283
321 # Continue 284 # Continue
322 ->>>>>>> b7dd76b35e6d2fd74ff010258ffa7606e072dbf5  
323 continue 285 continue
324 286
325 # Open FITS file 287 # Open FITS file
@@ -327,47 +289,22 @@ class cstsmapmerge(ctools.cscript): @@ -327,47 +289,22 @@ class cstsmapmerge(ctools.cscript):
327 289
328 # If file contains a status map then use it 290 # If file contains a status map then use it
329 if fits.contains('STATUS MAP'): 291 if fits.contains('STATUS MAP'):
330 -<<<<<<< HEAD  
331 -=======  
332 292
333 # Close FITS file 293 # Close FITS file
334 ->>>>>>> b7dd76b35e6d2fd74ff010258ffa7606e072dbf5  
335 fits.close() 294 fits.close()
336 295
337 # Set filename to initial filename 296 # Set filename to initial filename
338 file0 = fitsfile 297 file0 = fitsfile
339 -<<<<<<< HEAD  
340 - if self._logTerse():  
341 - self._log(gammalib.parformat('Initial TS map file'))  
342 - self._log(fitsfile)  
343 - count = self._get_number_of_ts_pixels(fitsfile)  
344 - self._log(' (%d TS pixels computed)' % count)  
345 - self._log('\n')  
346 -=======  
347 298
348 # Log message 299 # Log message
349 pix_info = " (%d TS pixels computed)" % self._get_number_of_ts_pixels(fitsfile) 300 pix_info = " (%d TS pixels computed)" % self._get_number_of_ts_pixels(fitsfile)
350 self._log_value(gammalib.TERSE, "Initial TS map file", fitsfile + pix_info) 301 self._log_value(gammalib.TERSE, "Initial TS map file", fitsfile + pix_info)
351 302
352 # Leave loop 303 # Leave loop
353 ->>>>>>> b7dd76b35e6d2fd74ff010258ffa7606e072dbf5  
354 break 304 break
355 305
356 # ... otherwise signal that file is useless 306 # ... otherwise signal that file is useless
357 else: 307 else:
358 -<<<<<<< HEAD  
359 - fits.close()  
360 - self._log_value(gammalib.EXPLICIT, 'Skip file',  
361 - '%s (no "STATUS MAP" extension)' % fitsfile)  
362 - continue  
363 -  
364 - # Signal if no suitable file was found  
365 - if file0 == '':  
366 - if self._logTerse():  
367 - self._log('None of the provided files seems to be a sliced '  
368 - 'TS map file (none has a "STATUS MAP" extension).\n')  
369 -  
370 -=======  
371 308
372 # Close fits file 309 # Close fits file
373 fits.close() 310 fits.close()
@@ -384,7 +321,6 @@ class cstsmapmerge(ctools.cscript): @@ -384,7 +321,6 @@ class cstsmapmerge(ctools.cscript):
384 "TS map file (none has a \"STATUS MAP\" " 321 "TS map file (none has a \"STATUS MAP\" "
385 "extension).\n") 322 "extension).\n")
386 323
387 ->>>>>>> b7dd76b35e6d2fd74ff010258ffa7606e072dbf5  
388 # ... otherwise merge files 324 # ... otherwise merge files
389 else: 325 else:
390 326
@@ -405,14 +341,8 @@ class cstsmapmerge(ctools.cscript): @@ -405,14 +341,8 @@ class cstsmapmerge(ctools.cscript):
405 341
406 # Skip if file is not FITS 342 # Skip if file is not FITS
407 if not gammalib.GFilename(fitsfile).is_fits(): 343 if not gammalib.GFilename(fitsfile).is_fits():
408 -<<<<<<< HEAD  
409 - self._log_value(gammalib.EXPLICIT, 'Skip file',  
410 - '%s (not a FITS file)' % fitsfile)  
411 - continue  
412 -=======  
413 self._log_value(gammalib.EXPLICIT, "Skip file", fitsfile +" (not a FITS file)") 344 self._log_value(gammalib.EXPLICIT, "Skip file", fitsfile +" (not a FITS file)")
414 continue 345 continue
415 ->>>>>>> b7dd76b35e6d2fd74ff010258ffa7606e072dbf5  
416 346
417 # Open FITS file 347 # Open FITS file
418 fits = gammalib.GFits(fitsfile) 348 fits = gammalib.GFits(fitsfile)
@@ -420,29 +350,15 @@ class cstsmapmerge(ctools.cscript): @@ -420,29 +350,15 @@ class cstsmapmerge(ctools.cscript):
420 # Skip if file does not contain status map 350 # Skip if file does not contain status map
421 if not fits.contains('STATUS MAP'): 351 if not fits.contains('STATUS MAP'):
422 fits.close() 352 fits.close()
423 -<<<<<<< HEAD  
424 - self._log_value(gammalib.EXPLICIT, 'Skip file',  
425 - '%s (no "STATUS MAP" extension)' % fitsfile)  
426 -=======  
427 self._log_value(gammalib.EXPLICIT, "Skip file", fitsfile +" (no \"STATUS MAP\" extension)") 353 self._log_value(gammalib.EXPLICIT, "Skip file", fitsfile +" (no \"STATUS MAP\" extension)")
428 ->>>>>>> b7dd76b35e6d2fd74ff010258ffa7606e072dbf5  
429 continue 354 continue
430 355
431 # Close FITS file 356 # Close FITS file
432 fits.close() 357 fits.close()
433 358
434 # Logging 359 # Logging
435 -<<<<<<< HEAD  
436 - if self._logTerse():  
437 - self._log(gammalib.parformat('Merge TS map file'))  
438 - self._log(fitsfile)  
439 - count = self._get_number_of_ts_pixels(fitsfile)  
440 - self._log(' (%d TS pixels computed)' % count)  
441 - self._log('\n')  
442 -=======  
443 pix_info = " (%d TS pixels computed)" % self._get_number_of_ts_pixels(fitsfile) 360 pix_info = " (%d TS pixels computed)" % self._get_number_of_ts_pixels(fitsfile)
444 self._log_value(gammalib.TERSE, "Merge TS map file", fitsfile + pix_info) 361 self._log_value(gammalib.TERSE, "Merge TS map file", fitsfile + pix_info)
445 ->>>>>>> b7dd76b35e6d2fd74ff010258ffa7606e072dbf5  
446 362
447 # Merge TS map 363 # Merge TS map
448 self._merge_ts_map(fitsfile) 364 self._merge_ts_map(fitsfile)
@@ -455,22 +371,11 @@ class cstsmapmerge(ctools.cscript): @@ -455,22 +371,11 @@ class cstsmapmerge(ctools.cscript):
455 371
456 def save(self): 372 def save(self):
457 """ 373 """
458 - Save TS map and remove slices if requested 374 + Save TS map and remove slices if requested.
459 """ 375 """
460 -<<<<<<< HEAD  
461 - # Write header  
462 - self._log_header1(gammalib.TERSE, 'Save TS map')  
463 -  
464 - # Get output filename in case it was not read ahead  
465 - outmap = self['outmap'].filename()  
466 -  
467 - # Log filename  
468 - self._log_value(gammalib.TERSE, 'TS map file', outmap.url())  
469 -=======  
470 376
471 # Get output filename in case it was not read ahead 377 # Get output filename in case it was not read ahead
472 outmap = self["outmap"].filename() 378 outmap = self["outmap"].filename()
473 ->>>>>>> b7dd76b35e6d2fd74ff010258ffa7606e072dbf5  
474 379
475 # Create FITS file 380 # Create FITS file
476 fits = gammalib.GFits() 381 fits = gammalib.GFits()
@@ -504,10 +409,6 @@ class cstsmapmerge(ctools.cscript): @@ -504,10 +409,6 @@ class cstsmapmerge(ctools.cscript):
504 409
505 # Write status map if we are not done yet 410 # Write status map if we are not done yet
506 if not done: 411 if not done:
507 -<<<<<<< HEAD  
508 - self._statusmap.write(fits)  
509 - fits[fits.size()-1].extname('STATUS MAP')  
510 -=======  
511 412
512 # Log bins that were computed and those that were missing 413 # Log bins that were computed and those that were missing
513 self._log_value(gammalib.TERSE, "TS map bins", self._tsmap.npix()) 414 self._log_value(gammalib.TERSE, "TS map bins", self._tsmap.npix())
@@ -524,7 +425,6 @@ class cstsmapmerge(ctools.cscript): @@ -524,7 +425,6 @@ class cstsmapmerge(ctools.cscript):
524 425
525 # Log filename 426 # Log filename
526 self._log_value(gammalib.TERSE, "TS map file", outmap.url()) 427 self._log_value(gammalib.TERSE, "TS map file", outmap.url())
527 ->>>>>>> b7dd76b35e6d2fd74ff010258ffa7606e072dbf5  
528 428
529 # Save FITS file 429 # Save FITS file
530 fits.saveto(outmap, self._clobber()) 430 fits.saveto(outmap, self._clobber())
@@ -540,20 +440,16 @@ class cstsmapmerge(ctools.cscript): @@ -540,20 +440,16 @@ class cstsmapmerge(ctools.cscript):
540 440
541 # Remove file 441 # Remove file
542 os.remove(filename) 442 os.remove(filename)
543 -<<<<<<< HEAD  
544 - self._log_value(gammalib.TERSE, 'Deleted input file', filename)  
545 -=======  
546 443
547 # Log message about file 444 # Log message about file
548 self._log_value(gammalib.TERSE,"Deleted input file", filename) 445 self._log_value(gammalib.TERSE,"Deleted input file", filename)
549 ->>>>>>> b7dd76b35e6d2fd74ff010258ffa7606e072dbf5  
550 446
551 # Return 447 # Return
552 return 448 return
553 449
554 def execute(self): 450 def execute(self):
555 """ 451 """
556 - Execute the script 452 + Execute the script.
557 """ 453 """
558 # Open logfile 454 # Open logfile
559 self.logFileOpen() 455 self.logFileOpen()