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