diff --git a/cscripts/csfindobs.py b/cscripts/csfindobs.py index 9f9b808..7b6b426 100755 --- a/cscripts/csfindobs.py +++ b/cscripts/csfindobs.py @@ -174,9 +174,7 @@ class csfindobs(ctools.cscript): self._get_parameters() # Write header into logger - if self._logTerse(): - self._log('\n') - self._log.header1('Find observations') + self._log_header1(gammalib.TERSE, 'Find observations') # Initialise run list self._runs = [] @@ -204,9 +202,7 @@ class csfindobs(ctools.cscript): expr += '&&' + expression # Write the expression into the logger - if self._logNormal(): - self._log(gammalib.parformat('Expression')) - self._log(expr+'\n') + self._log_value(gammalib.NORMAL, 'Expression', expr) # Set filename including the selection expression filename = self._obs_index+'[OBS_INDEX]['+expr+']' @@ -222,13 +218,12 @@ class csfindobs(ctools.cscript): self._runs.append(obs_index['OBS_ID'][i]) # Write the number of observations into the logger - if self._logTerse(): - self._log_value('Observations', len(self._runs)) + self._log_value(gammalib.TERSE, 'Observations', len(self._runs)) # Write the observation identifiers into the logger - if self._logNormal() and len(self._runs) > 0: + if len(self._runs) > 0: for i, run in enumerate(self._runs): - self._log_value('Observation %d' % (i+1), run) + self._log_value(gammalib.NORMAL, 'Observation %d' % (i+1), run) # Close FITS file fits.close() @@ -260,9 +255,7 @@ class csfindobs(ctools.cscript): Save runlist """ # Write header - if self._logTerse(): - self._log('\n') - self._log.header1('Save runlist') + self._log_header1(gammalib.TERSE, 'Save runlist') # Get filename outfile = self['outfile'].filename() @@ -270,18 +263,15 @@ class csfindobs(ctools.cscript): # If the runlist file exists but the clobber flag is not set then signal # that runlist file exists already if outfile.exists() and not self._clobber(): - if self._logTerse(): - self._log('File "'+outfile+'" exists already, runlist not ') - self._log('saved. Set clobber=yes to overwrite the file.\n') + msg = ('File "'+outfile+'" exists already, runlist not saved. ' + 'Set clobber=yes to overwrite the file.\n') + self._log_string(gammalib.TERSE, msg) # ... otherwise save the runlist into a file else: # Log file name - if self._logTerse(): - self._log(gammalib.parformat('Runlist file')) - self._log(outfile.url()) - self._log('\n') + self._log_value(gammalib.NORMAL, 'Runlist file', outfile.url()) # Write all runs to file f = open(outfile.url(),'w') diff --git a/cscripts/csiactcopy.py b/cscripts/csiactcopy.py index bb4f8ae..78f9de8 100755 --- a/cscripts/csiactcopy.py +++ b/cscripts/csiactcopy.py @@ -107,32 +107,25 @@ class csiactcopy(ctools.cscript): # Initialise return value filesize = 0.0 + + # Flag if file destination is already available + is_file = os.path.isfile(destination) # Logging - if self._logVerbose(): - self._log('\n') - self._log.header3('Copy file') - self._log_value('Source', source) - self._log_value('Destination', destination) - self._log_value('Already exists', str(os.path.isfile(destination))) - self._log_value('Overwrite', clobber) - self._log.parformat('Copying') + self._log_header3(gammalib.VERBOSE, 'Copy file') + self._log_value(gammalib.VERBOSE, 'Source', source) + self._log_value(gammalib.VERBOSE, 'Destination', destination) + self._log_value(gammalib.VERBOSE, 'Already exists', is_file) + self._log_value(gammalib.VERBOSE, 'Overwrite', clobber) - # Flag if file destination is already available - is_file = os.path.isfile(destination) - # check if file could be skipped because clobber=no if is_file and clobber == False: - if self._logVerbose(): - self._log('Skip (clobber=no)') - self._log('\n') + self._log_value(gammalib.VERBOSE, 'Copying', 'Skip (clobber=no)') # check if file could be skipped because it is the same file elif is_file and os.path.samefile(destination, source): - if self._logVerbose(): - self._log('Skip (same file)') - self._log('\n') - + self._log_value(gammalib.VERBOSE, 'Copying', 'Skip (same file)') + else: # Create directory if not existent dest_dir = os.path.dirname(destination) @@ -146,10 +139,8 @@ class csiactcopy(ctools.cscript): filesize = os.stat(destination).st_size # Logging - if self._logVerbose(): - self._log('Done!') - self._log('\n') - + self._log_value(gammalib.VERBOSE, 'Copying', 'Done!') + # Return return filesize @@ -171,15 +162,14 @@ class csiactcopy(ctools.cscript): """ # Logging - if self._logTerse(): - self._log_value('Local file', localfits) - self._log_value('Remote file', remotefits) - self._log_value('HDU name', hduname) - if clobber: - preference = 'Remote' - else: - preference = 'Local' - self._log_value('Conflict preference', preference) + self._log_value(gammalib.TERSE, 'Local file', localfits) + self._log_value(gammalib.TERSE, 'Remote file', remotefits) + self._log_value(gammalib.TERSE, 'HDU name', hduname) + if clobber: + preference = 'Remote' + else: + preference = 'Local' + self._log_value(gammalib.TERSE, 'Conflict preference', preference) # Check if local fits file is available if os.path.isfile(localfits): @@ -251,9 +241,8 @@ class csiactcopy(ctools.cscript): remote_obs.append(obs_id) # Log entries of index files - if self._logTerse(): - self._log_value('Remote entries', len(remote_obs)) - self._log_value('Local entries', len(local_obs)) + self._log_value(gammalib.TERSE, 'Remote entries', len(remote_obs)) + self._log_value(gammalib.TERSE, 'Local entries', len(local_obs)) # initialise counter for logging removed_rows = 0 @@ -318,12 +307,11 @@ class csiactcopy(ctools.cscript): local_hdu.insert_rows(old_local_rows, remote_hdu.nrows()) # Log actions - if self._logTerse(): - if clobber: - what = 'Removed local rows' - else: - what = 'Skipped remote rows' - self._log_value(what, removed_rows) + if clobber: + what = 'Removed local rows' + else: + what = 'Skipped remote rows' + self._log_value(gammalib.TERSE, what, removed_rows) # Loop over columns for i in range(local_hdu.ncols()): @@ -384,8 +372,8 @@ class csiactcopy(ctools.cscript): runfile.close() # Logging - if self._logTerse(): - self._log_value('Number of observations', len(self._runs)) + self._log_value(gammalib.TERSE, 'Number of observations', + len(self._runs)) # Copy all data if runlist file is 'NONE' elif self._runlist == 'NONE': @@ -446,10 +434,10 @@ class csiactcopy(ctools.cscript): remote_obs = os.path.join(self._remote_base, config['obsindx']) # Log information - if self._logTerse(): - self._log.header3('Remote config "'+self._prodname+'"') - self._log_value('HDU index', remote_hdu) - self._log_value('Observation index', remote_obs) + self._log_header3(gammalib.TERSE, 'Remote config "'+ + self._prodname+'"') + self._log_value(gammalib.TERSE, 'HDU index', remote_hdu) + self._log_value(gammalib.TERSE, 'Observation index', remote_obs) # Open remote HDU index file fits = gammalib.GFits(str(remote_hdu)) @@ -524,29 +512,22 @@ class csiactcopy(ctools.cscript): cp_size += table['SIZE'][row] # Log file information - if self._logTerse(): - self._log('\n') - self._log.header2('File information') - self._log_value('Number of files', len(files)) - if has_size: - size = float(cp_size) * 1.0e-6 - self._log_value('Size', '%.2f MB' % size) - if self._logVerbose(): - self._log('\n') - self._log.header3('File names') - for filename in files: - self._log(str(filename)+'\n') + self._log_header2(gammalib.TERSE, 'File information') + self._log_value(gammalib.TERSE, 'Number of files', len(files)) + if has_size: + size = float(cp_size) * 1.0e-6 + self._log_value(gammalib.TERSE, 'Size', '%.2f MB' % size) + self._log_header3(gammalib.VERBOSE, 'File names') + for filename in files: + self._log_string(gammalib.VERBOSE, str(filename)+'\n') # Close HDU index file fits.close() # If prodname is not found just log that we skip the config else: - if self._logExplicit(): - self._log('\n') - self._log.header3('Skipping config "'+ - str(config['name'])+'"') - self._log('\n') + self._log_header3(gammalib.EXPLICIT, 'Skipping config "'+ + str(config['name'])+'"') # Raise Exception if prodname was not found if not has_prod: @@ -556,10 +537,8 @@ class csiactcopy(ctools.cscript): msg += ' - '+config['name']+'\n' raise RuntimeError(msg) - # Logging - if self._logNormal(): - self._log('\n') - self._log.header1('Copying files') + # Write header + self._log_header1(gammalib.NORMAL, 'Copying files') # Intialise counter k = 0 @@ -592,8 +571,8 @@ class csiactcopy(ctools.cscript): while fraction > last_fraction: # Print status of copying procedure - if self._logNormal() and not self._logVerbose(): - self._log_value('Status', '%d %%' % int(last_fraction)) + self._log_value(gammalib.NORMAL, 'Status', '%d %%' % + int(last_fraction)) last_fraction += fraction_increment # Copy file @@ -632,17 +611,13 @@ class csiactcopy(ctools.cscript): if len(self._runs): # Logging - if self._logTerse(): - self._log('\n') - self._log.header3('HDU index') + self._log_header3(gammalib.TERSE, 'HDU index') # Merge remote index files with local files self._merge(local_hdu, remote_hdu, 'HDU_INDEX', self._clobber()) # Logging - if self._logTerse(): - self._log('\n') - self._log.header3('OBS index') + self._log_header3(gammalib.TERSE, 'OBS index') # Merge remote index files with local files self._merge(local_obs, remote_obs, 'OBS_INDEX', self._clobber()) @@ -653,9 +628,7 @@ class csiactcopy(ctools.cscript): self._copy(remote_obs, self._clobber()) # Logging - if self._logTerse(): - self._log('\n') - self._log.header3('Master index file') + self._log_header3(gammalib.TERSE, 'Master index file') # Adding prodname to local master localmaster = os.path.join(self._outpath, 'master.json') @@ -722,20 +695,18 @@ class csiactcopy(ctools.cscript): json.dump(data, f, indent=2) f.close() + # Compute total size in MB + size_MB = float(total_size) * 1.0e-6 + # Log summary - if self._logNormal(): - self._log('\n') - self._log.header1('Summary') - self._log_value('Data files found', len(files)) - self._log_value('Data files copied', n_copied) - self._log_value('Size', '%.2f MB' % (float(total_size)*1.0e-6)) - self._log_value('Local configs', len(newconfigs)) - if self._logTerse(): - self._log('\n') - self._log.header3('Content of master index') - for config in newconfigs: - self._log(str(config['name'])) - self._log('\n') + self._log_header1(gammalib.NORMAL, 'Summary') + self._log_value(gammalib.NORMAL, 'Data files found', len(files)) + self._log_value(gammalib.NORMAL, 'Data files copied', n_copied) + self._log_value(gammalib.NORMAL, 'Size', '%.2f MB' % size_MB) + self._log_value(gammalib.NORMAL, 'Local configs', len(newconfigs)) + self._log_header3(gammalib.TERSE, 'Content of master index') + for config in newconfigs: + self._log_string(gammalib.TERSE, str(config['name'])+'\n') # Return return diff --git a/cscripts/csiactdata.py b/cscripts/csiactdata.py index b5a3c77..e021255 100755 --- a/cscripts/csiactdata.py +++ b/cscripts/csiactdata.py @@ -98,24 +98,20 @@ class csiactdata(ctools.cscript): ---------- config : dict Dictionary containing data configuration - """ - # Log only if the level is at least "Terse" - if self._logTerse(): - - # Log configuration information - self._log.header3(str(config['name'])) - self._log_value('Name', config['name']) - self._log_value('Observation index', config['obsindx']) - self._log_value('HDU index', config['hduindx']) + # Log configuration information + self._log_header3(gammalib.TERSE, str(config['name'])) + self._log_value(gammalib.TERSE, 'Name', config['name']) + self._log_value(gammalib.TERSE, 'Observation index', config['obsindx']) + self._log_value(gammalib.TERSE, 'HDU index', config['hduindx']) - # Print additional information - for key in config: + # Print additional information + for key in config: - # Skip keys that were treated above - if key in ['name','hduindx','obsindx']: - continue - self._log_value(key, config[key]) + # Skip keys that were treated above + if key in ['name','hduindx','obsindx']: + continue + self._log_value(gammalib.TERSE, key, config[key]) # Return return diff --git a/cscripts/csiactobs.py b/cscripts/csiactobs.py index 26e76d6..901ed72 100755 --- a/cscripts/csiactobs.py +++ b/cscripts/csiactobs.py @@ -401,11 +401,9 @@ class csiactobs(ctools.cscript): filename = self['inmodel'].filename().url() if filename != '' and filename != 'NONE': - # Write header - if self._logTerse(): - self._log('\n') - self._log.header1('Append input models') - self._log_value('Input model file', filename) + # Log header and input model file + self._log_header1(gammalib.TERSE, 'Append input models') + self._log_value(gammalib.NORMAL, 'Input model file', filename) # Load input models models = gammalib.GModels(filename) @@ -413,8 +411,8 @@ class csiactobs(ctools.cscript): # Loop over input models and append them to the model container for model in models: self._models.append(model) - if self._logTerse(): - self._log_value('Append model', '"'+model.name()+'"') + self._log_value(gammalib.NORMAL, 'Append model', + '"'+model.name()+'"') # Return return @@ -423,23 +421,19 @@ class csiactobs(ctools.cscript): """ Write observation summary """ - # Write observation summary - if self._logTerse(): + # Log header + self._log_header1(gammalib.TERSE, 'Observation summary') + + # Set energy range dependent on whether boundaries exist or + # not + if self._ebounds.size() > 0: + erange = '%s - %s' % (str(self._ebounds.emin()), + str(self._ebounds.emax())) + else: + erange = 'not available' - # Log header - self._log('\n') - self._log.header1('Observation summary') - - # Set energy range dependent on whether boundaries exist or - # not - if self._ebounds.size() > 0: - erange = '%s - %s' % (str(self._ebounds.emin()), - str(self._ebounds.emax())) - else: - erange = 'not available' - - # Log energy range - self._log_value('Energy range', erange) + # Log energy range + self._log_value(gammalib.NORMAL, 'Energy range', erange) # Return return @@ -712,19 +706,17 @@ class csiactobs(ctools.cscript): obs.append(bck) # Log the observation ID and object name that has been appended - if self._logTerse(): - self._log_value('Append observation', '%s ("%s")' % - (obs_id, object_name)) + self._log_value(gammalib.TERSE, 'Append observation', '%s ("%s")' % + (obs_id, object_name)) # Log the file names of the event and response files - if self._logExplicit(): - self._log_value(' Event file', eventfile) - self._log_value(' Effective area file', aefffile) - self._log_value(' Point spread function file', psffile) - self._log_value(' Energy dispersion file', edispfile) - self._log_value(' Background file', bkgfile) - if self._use_bkg_scale: - self._log_value(' Background scale', bkg_scale) + self._log_value(gammalib.EXPLICIT, ' Event file', eventfile) + self._log_value(gammalib.EXPLICIT, ' Effective area file', aefffile) + self._log_value(gammalib.EXPLICIT, ' Point spread function file', psffile) + self._log_value(gammalib.EXPLICIT, ' Energy dispersion file', edispfile) + self._log_value(gammalib.EXPLICIT, ' Background file', bkgfile) + if self._use_bkg_scale: + self._log_value(gammalib.EXPLICIT, ' Background scale', bkg_scale) # Append models provided by "inmodel" to the model container self._append_inmodels() diff --git a/cscripts/cslightcrv.py b/cscripts/cslightcrv.py index a345f69..ac3bbb3 100755 --- a/cscripts/cslightcrv.py +++ b/cscripts/cslightcrv.py @@ -558,9 +558,8 @@ class cslightcrv(ctools.cscript): if like.obs().logL() == 0.0: # Signal skipping of bin - if self._logTerse(): - self._log_value('Warning', 'No event in this time bin, ' - 'skip bin.') + self._log_value(gammalib.TERSE, 'Warning', + 'No event in this time bin, skip bin.') # Set all results to 0 for par in pars: @@ -594,19 +593,19 @@ class cslightcrv(ctools.cscript): results.append(result) # Log results for this time bin - if self._logNormal(): - self._log.header3('Results') - pars = self._get_free_par_names() - for par in pars: - value = source.spectral()[par].value() - error = source.spectral()[par].error() - unit = source.spectral()[par].unit() - self._log_value(par, str(value)+' +/- '+str(error)+' '+unit) - if result['ulimit'] > 0.0: - self._log_value('Upper flux limit', str(result['ulimit'])+ - ' ph/cm2/s') - if self['calc_ts'].boolean(): - self._log_value('Test Statistic', result['ts']) + self._log.header3('Results') + pars = self._get_free_par_names() + for par in pars: + value = source.spectral()[par].value() + error = source.spectral()[par].error() + unit = source.spectral()[par].unit() + self._log_value(gammalib.NORMAL, par, + str(value)+' +/- '+str(error)+' '+unit) + if result['ulimit'] > 0.0: + self._log_value(gammalib.NORMAL, 'Upper flux limit', + str(result['ulimit'])+' ph/cm2/s') + if self['calc_ts'].boolean(): + self._log_value(gammalib.NORMAL, 'Test Statistic', result['ts']) # Create FITS table from results table = self._create_fits_table(results) @@ -642,9 +641,7 @@ class cslightcrv(ctools.cscript): Save light curve """ # Write header - if self._logTerse(): - self._log('\n') - self._log.header1('Save light curve') + self._log_header1(gammalib.TERSE, 'Save light curve') # Get light curve filename outfile = self['outfile'].filename() @@ -653,8 +650,7 @@ class cslightcrv(ctools.cscript): if self._fits != None: # Log file name - if self._logTerse(): - self._log_value('Light curve file', outfile.url()) + self._log_value(gammalib.NORMAL, 'Light curve file', outfile.url()) # Save spectrum self._fits.saveto(outfile, self._clobber()) diff --git a/cscripts/csmodelinfo.py b/cscripts/csmodelinfo.py index ceaf4e0..190b177 100755 --- a/cscripts/csmodelinfo.py +++ b/cscripts/csmodelinfo.py @@ -337,62 +337,57 @@ class csmodelinfo(ctools.cscript): if self._logTerse(): # Log summary - self._log('\n') - self._log.header1('Summary') + self._log_header1(gammalib.TERSE, 'Summary') # Log instruments self._log.header3('Instrument specific models') for inst, n_inst in instruments.items(): if inst == '': inst = 'All' - self._log_value(inst, n_inst) + self._log_value(gammalib.TERSE, inst, n_inst) # Log model types - self._log.header3('Model types') + self._log_header3(gammalib.TERSE, 'Model types') for modeltype, n_types in types.items(): - self._log_value(modeltype, n_types) + self._log_value(gammalib.TERSE, modeltype, n_types) # Log model parameter information - self._log('\n') - self._log.header1('Parameter information') - self._log_value('All parameters', n_par_total) - self._log_value('Fixed parameters', n_par_fixed) - self._log_value('Free parameters (total)', n_par_free) - self._log_value('Free background parameters', n_par_free_bkg) - self._log_value('Free source parameters', n_par_free_src) + self._log_header1(gammalib.TERSE,'Parameter information') + self._log_value(gammalib.TERSE,'All parameters', n_par_total) + self._log_value(gammalib.TERSE,'Fixed parameters', n_par_fixed) + self._log_value(gammalib.TERSE,'Free parameters (total)', n_par_free) + self._log_value(gammalib.TERSE,'Free background parameters', n_par_free_bkg) + self._log_value(gammalib.TERSE,'Free source parameters', n_par_free_src) if n_par_free_spec > 0: - self._log_value('Free spectral parameters', n_par_free_spec) + self._log_value(gammalib.TERSE,'Free spectral parameters', n_par_free_spec) if n_par_free_spat > 0: - self._log_value('Free spatial parameters', n_par_free_spat) + self._log_value(gammalib.TERSE,'Free spatial parameters', n_par_free_spat) if n_par_free_temp > 0: - self._log_value('Free temporal parameters', n_par_free_temp) - self._log_value('Parameters at limit', n_par_at_limit) + self._log_value(gammalib.TERSE,'Free temporal parameters', n_par_free_temp) + self._log_value(gammalib.TERSE,'Parameters at limit', n_par_at_limit) # Log parameters at limit (if any) if n_par_at_limit > 0: - self._log.header3('Parameters at limit') + self._log_header3(gammalib.TERSE, 'Parameters at limit') for source, parameter in pars_at_limit.items(): if len(parameter): for par in parameter: self._log_value(source, par) # Optionally log free parameters - if self._logExplicit() and len(free_src_pars): - self._log('\n') - self._log.header2('Free source parameters') + if len(free_src_pars): + self._log_header2(gammalib.EXPLICIT, 'Free source parameters') for source, parameter in free_src_pars.items(): if len(parameter): self._log.header3(source) for par in parameter: - self._log(str(par)) - self._log('\n') - self._log('\n') + self._log_string(gammalib.EXPLICIT, str(par)+'\n') # Log TS values if available if len(ts): - self._log.header3('Test statistics') + self._log_header3(gammalib.TERSE, 'Test statistics') for source, tsvalue in ts.items(): - self._log_value(source, tsvalue) + self._log_value(gammalib.TERSE, source, tsvalue) # Return return @@ -408,13 +403,10 @@ class csmodelinfo(ctools.cscript): if ds9file != '' and ds9file != 'NONE': # Write header - if self._logTerse(): - self._log('\n') - self._log.header1('Save models in DS9 file') + self._log_header1(gammalib.TERSE, 'Save models in DS9 file') # Log filename - if self._logTerse(): - self._log_value('DS9 filename', ds9file.url()) + self._log_value(gammalib.NORMAL, 'DS9 filename', ds9file.url()) # Open file f = open(ds9file.url(), 'w') diff --git a/cscripts/csmodelmerge.py b/cscripts/csmodelmerge.py index 69e8732..ad285d9 100755 --- a/cscripts/csmodelmerge.py +++ b/cscripts/csmodelmerge.py @@ -141,22 +141,21 @@ class csmodelmerge(ctools.cscript): models = gammalib.GModels(f) # Log number of models to add - if self._logTerse(): - nmodels = models.size() - if nmodels == 0: - name = 'Add no model from file' - elif nmodels == 1: - name = 'Add 1 model from file' - else: - name = 'Add %d models from file' % nmodels - self._log_value(name, f) + nmodels = models.size() + if nmodels == 0: + name = 'Add no model from file' + elif nmodels == 1: + name = 'Add 1 model from file' + else: + name = 'Add %d models from file' % nmodels + self._log_value(gammalib.TERSE, name, f) # Extend model container by adding all models in the model file self._models.extend(models) # Log total number of models - if self._logTerse(): - self._log_value('Models after merging', self._models.size()) + self._log_value(gammalib.TERSE, 'Models after merging', + self._models.size()) # Return return @@ -166,24 +165,22 @@ class csmodelmerge(ctools.cscript): Save model definition XML file """ # Write header - if self._logTerse(): - self._log('\n') - self._log.header1('Save models') + self._log_header1(gammalib.TERSE, 'Save models') # Get output filename in case it was not read ahead outmodel = self['outmodel'].filename() # If file exists and clobber flag is false then raise an exception if outmodel.exists() and not self._clobber: - msg = 'Cannot save ""+outmodel.url()+"": File already exists. '\ - 'Use parameter clobber=yes to allow overwriting of files.' + msg = ('Cannot save "'+outmodel.url()+'": File already exists. ' + 'Use parameter clobber=yes to allow overwriting of files.') raise RuntimeError(msg) else: # Log filename - if self._logTerse(): - self._log_value('Model definition XML file', outmodel.url()) + self._log_value(gammalib.NORMAL, 'Model definition XML file', + outmodel.url()) # Save models self._models.save(outmodel) diff --git a/cscripts/csobs2caldb.py b/cscripts/csobs2caldb.py index dcdeefe..9c9d55a 100755 --- a/cscripts/csobs2caldb.py +++ b/cscripts/csobs2caldb.py @@ -153,12 +153,11 @@ class csobs2caldb(ctools.cscript): fname_bkg = rsp.background().filename() # Log filenames - if self._logNormal(): - self._log.header3('IRF input files') - self._log_value('Effective area', fname_aeff.url()) - self._log_value('Point spread function', fname_psf.url()) - self._log_value('Energy dispersion', fname_edisp.url()) - self._log_value('Background rate', fname_bkg.url()) + self._log_header3(gammalib.NORMAL, 'IRF input files') + self._log_value(gammalib.NORMAL, 'Effective area', fname_aeff.url()) + self._log_value(gammalib.NORMAL, 'Point spread function', fname_psf.url()) + self._log_value(gammalib.NORMAL, 'Energy dispersion', fname_edisp.url()) + self._log_value(gammalib.NORMAL, 'Background rate', fname_bkg.url()) # Open FITS files of response components fits_aeff = gammalib.GFits(fname_aeff) @@ -325,14 +324,13 @@ class csobs2caldb(ctools.cscript): self._rsp_dir = caldb.rootdir() + "/" + self._cal_dir # Log resulting FITS table - if self._logNormal(): - self._log_value('Calibration directory', self._cal_dir) - self._log_value('Base directory', self._base_dir) - if not os.path.isdir(self._rsp_dir): - name = 'IRF directory' - else: - name = 'IRF directory (existing)' - self._log_value(name, self._rsp_dir) + self._log_value(gammalib.NORMAL, 'Calibration directory', self._cal_dir) + self._log_value(gammalib.NORMAL, 'Base directory', self._base_dir) + if not os.path.isdir(self._rsp_dir): + name = 'IRF directory' + else: + name = 'IRF directory (existing)' + self._log_value(gammalib.NORMAL, name, self._rsp_dir) # Create IRF directory is it does not yet exist if not os.path.isdir(self._rsp_dir): @@ -376,17 +374,15 @@ class csobs2caldb(ctools.cscript): Save calibration database FITS file """ # Write header into logger - if self._logTerse(): - self._log('\n') - self._log.header1('Save calibration database') + self._log_header1(gammalib.TERSE, 'Save calibration database') # Set response filename filename = self._rsp_dir + '/' + self._outfile # Write filenames into logger - if self._logNormal(): - self._log_value('CALDB index file', self._caldb_inx.filename().url()) - self._log_value('Response file', filename) + self._log_value(gammalib.NORMAL, 'CALDB index file', + self._caldb_inx.filename().url()) + self._log_value(gammalib.NORMAL, 'Response file', filename) # Save caldb index file self._caldb_inx.save(self._clobber()) diff --git a/cscripts/csobsdef.py b/cscripts/csobsdef.py index f375073..9e02d0f 100755 --- a/cscripts/csobsdef.py +++ b/cscripts/csobsdef.py @@ -177,9 +177,8 @@ class csobsdef(ctools.cscript): self._get_parameters() # Write header into logger - if self._logTerse(): - self._log('\n') - self._log.header1('Creating observation definition XML file') + self._log_header1(gammalib.TERSE, + 'Creating observation definition XML file') # Load pointing definition file if it is not already set. Extract # the number of columns and pointings @@ -335,13 +334,10 @@ class csobsdef(ctools.cscript): obs.events(event_list) # Write observation into logger - if self._logExplicit(): - self._log(str(obs)) - self._log('\n') - elif self._logTerse(): - name = obs.instrument()+' observation' - value = 'Name="%s" ID="%s"' % (obs.name(), obs.id()) - self._log_value(name, value) + name = obs.instrument()+' observation' + value = 'Name="%s" ID="%s"' % (obs.name(), obs.id()) + self._log_value(gammalib.TERSE, name, value) + self._log_string(gammalib.EXPLICIT, str(obs)+'\n') # Append observation self._obs.append(obs) @@ -354,9 +350,7 @@ class csobsdef(ctools.cscript): Save observation definition XML file. """ # Write header and filename into logger - if self._logTerse(): - self._log('\n') - self._log.header1('Save observation definition XML file') + self._log_header1(gammalib.TERSE, 'Save observation definition XML file') # Get output filename in case it was not read ahead outobs = self['outobs'].filename() @@ -365,8 +359,7 @@ class csobsdef(ctools.cscript): if outobs.url() != 'NONE': # Log filename - if self._logTerse(): - self._log_value('Observation XML file', outobs.url()) + self._log_value(gammalib.NORMAL, 'Observation XML file', outobs.url()) # Save observation definition XML file self._obs.save(outobs) diff --git a/cscripts/csobsinfo.py b/cscripts/csobsinfo.py index 4533a8d..8931de1 100755 --- a/cscripts/csobsinfo.py +++ b/cscripts/csobsinfo.py @@ -178,15 +178,15 @@ class csobsinfo(ctools.cscript): if obs.eventtype() == 'CountsCube': n_eventbins += obs.events().size() n_obs_binned += 1 - if self._logTerse(): - self._log_value('Binned', 'yes') - self._log_value('Number of bins', obs.events().size()) + is_binned = 'yes' + is_what = 'Number of bins' else: n_events += obs.events().size() n_obs_unbinned += 1 - if self._logTerse(): - self._log_value('Binned', 'no') - self._log_value('Number of events', obs.events().size()) + is_binned = 'no' + is_what = 'Number of events' + self._log_value(gammalib.TERSE, 'Binned', is_binned) + self._log_value(gammalib.TERSE, is_what, obs.events().size()) # Retrieve zenith and azimuth and store for later use zenith = obs.pointing().zenith() @@ -224,86 +224,100 @@ class csobsinfo(ctools.cscript): self._log(str(obs_gti.tstop().mjd())) self._log('\n') - # Log observation information - self._log_value('Ontime', '%.3f s' % obs.ontime()) - self._log_value('Livetime', '%.3f s' % obs.livetime()) - self._log_value('Deadtime fraction', '%.3f %%' % deadfrac) - self._log_value('Pointing', pnt_dir) - - # Optionally log offset with respect to target direction - if self._compute_offset: - self._log_value('Offset from target', '%.2f deg' % offset) - - # Log Zenith and Azimuth angles - self._log_value('Zenith angle', '%.2f deg' % zenith) - self._log_value('Azimuth angle', '%.2f deg' % azimuth) - - # Log summary - if self._logTerse(): - - # Write header - self._log('\n') - self._log.header1('Summary') - - # Log general summary - self._log.header3('Observations') - self._log_value('Unbinned observations', n_obs_unbinned) - self._log_value('Binned observations', n_obs_binned) - self._log_value('Binned observations', n_obs_binned) - self._log.header3('Events') - self._log_value('Number of events', n_events) - self._log_value('Number of bins', n_eventbins) - - # Log pointing summary - self._log.header3('Pointings') - - # Log mean offset if possible + # Log observation information + self._log_value(gammalib.TERSE, 'Ontime', '%.3f s' % + obs.ontime()) + self._log_value(gammalib.TERSE, 'Livetime', '%.3f s' % + obs.livetime()) + self._log_value(gammalib.TERSE, 'Deadtime fraction', '%.3f %%' % + deadfrac) + self._log_value(gammalib.TERSE, 'Pointing', pnt_dir) + + # Optionally log offset with respect to target direction if self._compute_offset: - self._log_value('Mean offset angle', '%.2f deg' % - (sum(self._offsets)/len(self._offsets))) - - # Log mean azimuth and zenith angle - self._log_value('Mean zenith angle', '%.2f deg' % - (sum(self._zeniths)/len(self._zeniths))) - self._log_value('Mean azimuth angle', '%.2f deg' % - (sum(self._azimuths)/len(self._azimuths))) + self._log_value(gammalib.TERSE, + 'Offset from target', '%.2f deg' % offset) + + # Log Zenith and Azimuth angles + self._log_value(gammalib.TERSE, 'Zenith angle', '%.2f deg' % + zenith) + self._log_value(gammalib.TERSE, 'Azimuth angle', '%.2f deg' % + azimuth) + + # Write summary header + self._log_string(gammalib.TERSE, '\n') + self._log_header1(gammalib.TERSE, 'Summary') + + # Log general summary + self._log_header3(gammalib.TERSE, 'Observations') + self._log_value(gammalib.TERSE, 'Unbinned observations', n_obs_unbinned) + self._log_value(gammalib.TERSE, 'Binned observations', n_obs_binned) + self._log_value(gammalib.TERSE, 'Binned observations', n_obs_binned) + self._log_header3(gammalib.TERSE, 'Events') + self._log_value(gammalib.TERSE, 'Number of events', n_events) + self._log_value(gammalib.TERSE, 'Number of bins', n_eventbins) + + + # Compute mean offset, azimuth and zenith angle + if len(self._offsets) > 0: + mean_offset = sum(self._offsets) / len(self._offsets) + else: + mean_offset = 'Unknown' + if len(self._zeniths) > 0: + mean_zenith = sum(self._zeniths) / len(self._zeniths) + else: + mean_zenith = 'Unknown' + if len(self._azimuths) > 0: + mean_azimuth = sum(self._azimuths) / len(self._azimuths) + else: + mean_azimuth = 'Unknown' - # Optionally log names of observations. Note that the set class - # is used to extract all different observation names from the - # list of observation names, and the set class is only available - # from Python 2.4 on. - if self._logExplicit() and sys.version_info >= (2,4): - obs_set = set(obs_names) - for name in obs_set: - self._log_value('"'+name+'"', obs_names.count(name)) - self._log('\n') + # Log mean offset, azimuth and zenith angle + self._log_header3(gammalib.TERSE, 'Pointings') + self._log_value(gammalib.TERSE, 'Mean offset angle', '%.2f deg' % + mean_offset) + self._log_value(gammalib.TERSE, 'Mean zenith angle', '%.2f deg' % + mean_zenith) + self._log_value(gammalib.TERSE, 'Mean azimuth angle', '%.2f deg' % + mean_azimuth) + + # Optionally log names of observations. Note that the set class is + # used to extract all different observation names from the list of + # observation names, and the set class is only available from + # Python 2.4 on. + if sys.version_info >= (2,4): + obs_set = set(obs_names) + for name in obs_set: + self._log_value(gammalib.EXPLICIT,'"'+name+'"', + obs_names.count(name)) + self._log('\n') - # Get energy boundary information - if self._ebounds.size() == 0: - min_value = 'undefined' - max_value = 'undefined' - else: - min_value = str(self._ebounds.emin()) - max_value = str(self._ebounds.emax()) - - # Log energy range - self._log.header3('Energy range') - self._log_value('Minimum energy', min_value) - self._log_value('Maximum energy', max_value) - - # Log time range - self._log.header3('Time range') - self._log_value('Start (MJD)', self._gti.tstart().mjd()) - self._log_value('Stop (MJD)', self._gti.tstop().mjd()) - - # Log ontime and livetime in different units - on_time = '%.2f s = %.2f min = %.2f h' % \ - (ontime, ontime/60., ontime/3600.) - live_time = '%.2f s = %.2f min = %.2f h' % \ - (livetime, livetime/60., livetime/3600.) - self._log_value('Total ontime', on_time) - self._log_value('Total livetime', live_time) - + # Get energy boundary information + if self._ebounds.size() == 0: + min_value = 'undefined' + max_value = 'undefined' + else: + min_value = str(self._ebounds.emin()) + max_value = str(self._ebounds.emax()) + + # Log energy range + self._log_header3(gammalib.TERSE, 'Energy range') + self._log_value(gammalib.TERSE, 'Minimum energy', min_value) + self._log_value(gammalib.TERSE, 'Maximum energy', max_value) + + # Log time range + self._log_header3(gammalib.TERSE, 'Time range') + self._log_value(gammalib.TERSE, 'Start (MJD)', self._gti.tstart().mjd()) + self._log_value(gammalib.TERSE, 'Stop (MJD)', self._gti.tstop().mjd()) + + # Log ontime and livetime in different units + on_time = '%.2f s = %.2f min = %.2f h' % \ + (ontime, ontime/60., ontime/3600.) + live_time = '%.2f s = %.2f min = %.2f h' % \ + (livetime, livetime/60., livetime/3600.) + self._log_value(gammalib.TERSE, 'Total ontime', on_time) + self._log_value(gammalib.TERSE, 'Total livetime', live_time) + # Return return @@ -323,13 +337,10 @@ class csobsinfo(ctools.cscript): if ds9file.url() != 'NONE': # Write header - if self._logTerse(): - self._log('\n') - self._log.header1('Save pointings in DS9 file') + self._log_header1(gammalib.TERSE, 'Save pointings in DS9 file') # Log filename - if self._logTerse(): - self._log_value('DS9 filename', ds9file.url()) + self._log_value(gammalib.TERSE, 'DS9 filename', ds9file.url()) # Open file f = open(ds9file.url(),'w') diff --git a/cscripts/cspull.py b/cscripts/cspull.py index 823cea4..4f8fa79 100755 --- a/cscripts/cspull.py +++ b/cscripts/cspull.py @@ -209,15 +209,14 @@ class cspull(ctools.cscript): nevents += run.events().number() # Write simulation results - if self._logNormal(): - self._log.header3('Simulation') - for run in self._obs: - self._log_value('Input observation %s' % run.id(), - self._obs_string(run)) - for run in obs: - self._log_value('Output observation %s' % run.id(), - self._obs_string(run)) - self._log_value('Number of simulated events', nevents) + self._log_header3(gammalib.NORMAL, 'Simulation') + for run in self._obs: + self._log_value(gammalib.NORMAL, 'Input observation %s' % run.id(), + self._obs_string(run)) + for run in obs: + self._log_value(gammalib.NORMAL, 'Output observation %s' % run.id(), + self._obs_string(run)) + self._log_value(gammalib.NORMAL, 'Number of simulated events', nevents) # Fit model if self['profile'].boolean(): @@ -284,9 +283,8 @@ class cspull(ctools.cscript): values['e_'+name] = error # Write result - if self._logNormal(): - value = '%.4f (%e +/- %e)' % (pull, fitted_value, error) - self._log_value(name, value) + value = '%.4f (%e +/- %e)' % (pull, fitted_value, error) + self._log_value(gammalib.NORMAL, name, value) # Bundle together results result = {'colnames': colnames, 'values': values} diff --git a/cscripts/csresmap.py b/cscripts/csresmap.py index 0be341d..5171af1 100755 --- a/cscripts/csresmap.py +++ b/cscripts/csresmap.py @@ -276,9 +276,7 @@ class csresmap(ctools.cscript): Save residual map """ # Write header - if self._logTerse(): - self._log('\n') - self._log.header1('Save residual map') + self._log_header1(gammalib.TERSE, 'Save residual map') # Get outmap parameter outmap = self['outmap'].filename() @@ -287,8 +285,7 @@ class csresmap(ctools.cscript): if self._resmap != None: # Log file name - if self._logTerse(): - self._log_value('Residual map file', outmap.url()) + self._log_value(gammalib.TERSE, 'Residual map file', outmap.url()) # Save residual map self._resmap.save(outmap, self['clobber'].boolean()) @@ -306,9 +303,7 @@ class csresmap(ctools.cscript): Name of residual map """ # Write header - if self._logTerse(): - self._log('\n') - self._log.header1('Publish residual map') + self._log_header1(gammalib.TERSE, 'Publish residual map') # Continue only if residual map is valid if self._resmap != None: @@ -320,8 +315,7 @@ class csresmap(ctools.cscript): user_name = name # Log map name - if self._logTerse(): - self._log_value('Residual map name', user_name) + self._log_value(gammalib.TERSE, 'Residual map name', user_name) # Publish map self._resmap.publish(user_name) diff --git a/cscripts/cssens.py b/cscripts/cssens.py index 1d3830f..b84bb6c 100755 --- a/cscripts/cssens.py +++ b/cscripts/cssens.py @@ -301,12 +301,11 @@ class cssens(ctools.cscript): crab_unit = crab_flux/src_flux # Write header - if self._logTerse(): - self._log('\n') - self._log.header2('Energies: '+str(emin)+' - '+str(emax)) - self._log_value('Crab flux', str(crab_flux)+' ph/cm2/s') - self._log_value('Source model flux', str(src_flux)+' ph/cm2/s') - self._log_value('Crab unit factor', crab_unit) + self._log_header2(gammalib.TERSE, 'Energies: '+str(emin)+' - '+str(emax)) + self._log_value(gammalib.TERSE, 'Crab flux', str(crab_flux)+' ph/cm2/s') + self._log_value(gammalib.TERSE, 'Source model flux', str(src_flux)+ + ' ph/cm2/s') + self._log_value(gammalib.TERSE, 'Crab unit factor', crab_unit) # Initialise loop crab_flux_value = [] @@ -348,9 +347,9 @@ class cssens(ctools.cscript): nevents += run.events().number() # Write simulation results - if self._logExplicit(): - self._log.header3('Simulation') - self._log_value('Number of simulated events', nevents) + self._log_header3(gammalib.EXPLICIT, 'Simulation') + self._log_value(gammalib.EXPLICIT, 'Number of simulated events', + nevents) # Fit test source fit = ctools.ctlike(sim) @@ -379,16 +378,16 @@ class cssens(ctools.cscript): quality = npred - nevents # Write test source fit results - if self._logExplicit(): - self._log.header3('Test source model fit') - self._log_value('Test statistics', ts) - self._log_value('log likelihood', logL) - self._log_value('Number of predicted events', quality) - self._log_value('Fit quality', npred) - for model in models: - self._log_value('Model', model.name()) - for par in model: - self._log(str(par)+'\n') + self._log_header3(gammalib.EXPLICIT, 'Test source model fit') + self._log_value(gammalib.EXPLICIT, 'Test statistics', ts) + self._log_value(gammalib.EXPLICIT, 'log likelihood', logL) + self._log_value(gammalib.EXPLICIT, 'Number of predicted events', + quality) + self._log_value(gammalib.EXPLICIT, 'Fit quality', npred) + for model in models: + self._log_value(gammalib.EXPLICIT, 'Model', model.name()) + for par in model: + self._log(str(par)+'\n') # If TS was non-positive then increase the test flux and start # over @@ -427,13 +426,16 @@ class cssens(ctools.cscript): # Write fit results if self._logExplicit(): - self._log_value('Photon flux', str(photon_flux)+' ph/cm2/s') - self._log_value('Energy flux', str(energy_flux)+' erg/cm2/s') - self._log_value('Crab flux', str(crab_flux*1000.0)+' mCrab') - self._log_value('Differential sensitivity', str(sensitivity)+ - ' erg/cm2/s') + self._log_value(gammalib.EXPLICIT, 'Photon flux', + str(photon_flux)+' ph/cm2/s') + self._log_value(gammalib.EXPLICIT, 'Energy flux', + str(energy_flux)+' erg/cm2/s') + self._log_value(gammalib.EXPLICIT, 'Crab flux', + str(crab_flux*1000.0)+' mCrab') + self._log_value(gammalib.EXPLICIT, 'Differential sensitivity', + str(sensitivity)+' erg/cm2/s') for model in models: - self._log_value('Model', model.name()) + self._log_value(gammalib.EXPLICIT, 'Model', model.name()) for par in model: self._log(str(par)+'\n') elif self._logTerse(): @@ -441,7 +443,7 @@ class cssens(ctools.cscript): value = ('TS=%.4f corr=%e %e ph/cm2/s = %e erg/cm2/s = %.2f ' 'mCrab = %e erg/cm2/s' % (ts, correct, photon_flux, energy_flux, crab_flux*1000.0, sensitivity)) - self._log_value(name, value) + self._log_value(gammalib.TERSE, name, value) # Compute sliding average of extrapolated fitted prefactor, # photon and energy flux. This damps out fluctuations and @@ -493,21 +495,24 @@ class cssens(ctools.cscript): break # Write fit results - if self._logTerse(): - self._log.header3('Fit results') - self._log_value('Test statistics', ts) - self._log_value('Photon flux', str(photon_flux)+' ph/cm2/s') - self._log_value('Energy flux', str(energy_flux)+' erg/cm2/s') - self._log_value('Crab flux', str(crab_flux*1000.0)+' mCrab') - self._log_value('Differential sensitivity', str(sensitivity)+' erg/cm2/s') - self._log_value('Number of simulated events', nevents) - self._log.header3('Test source model fitting') - self._log_value('log likelihood', logL) - self._log_value('Number of predicted events', npred) - for model in models: - self._log_value('Model', model.name()) - for par in model: - self._log(str(par)+'\n') + self._log_header3(gammalib.TERSE, 'Fit results') + self._log_value(gammalib.TERSE, 'Test statistics', ts) + self._log_value(gammalib.TERSE, 'Photon flux', + str(photon_flux)+' ph/cm2/s') + self._log_value(gammalib.TERSE, 'Energy flux', + str(energy_flux)+' erg/cm2/s') + self._log_value(gammalib.TERSE, 'Crab flux', + str(crab_flux*1000.0)+' mCrab') + self._log_value(gammalib.TERSE, 'Differential sensitivity', + str(sensitivity)+' erg/cm2/s') + self._log_value(gammalib.TERSE, 'Number of simulated events', nevents) + self._log_header3(gammalib.TERSE, 'Test source model fitting') + self._log_value(gammalib.TERSE, 'log likelihood', logL) + self._log_value(gammalib.TERSE, 'Number of predicted events', npred) + for model in models: + self._log_value(gammalib.TERSE, 'Model', model.name()) + for par in model: + self._log_string(gammalib.TERSE, str(par)+'\n') # Restore energy boundaries of observation container for i, obs in enumerate(self._obs): diff --git a/cscripts/csspec.py b/cscripts/csspec.py index 2f035cb..e064dbe 100755 --- a/cscripts/csspec.py +++ b/cscripts/csspec.py @@ -207,18 +207,17 @@ class csspec(ctools.cscript): self._get_parameters() # Write spectral binning into header - if self._logTerse(): - self._log("\n") - self._log.header1("Spectral binning") - if self._binned_mode: - cube_ebounds = self._obs[0].events().ebounds() - value = '%s - %s' % (str(cube_ebounds.emin()), - str(cube_ebounds.emax())) - self._log_value('Counts cube energy range', value) - for i in range(self._ebounds.size()): - value = '%s - %s' % (str(self._ebounds.emin(i)), - str(self._ebounds.emax(i))) - self._log_value('Bin %d' % (i+1), value) + self._log("\n") + self._log.header1("Spectral binning") + if self._binned_mode: + cube_ebounds = self._obs[0].events().ebounds() + value = '%s - %s' % (str(cube_ebounds.emin()), + str(cube_ebounds.emax())) + self._log_value(gammalib.TERSE, 'Counts cube energy range', value) + for i in range(self._ebounds.size()): + value = '%s - %s' % (str(self._ebounds.emin(i)), + str(self._ebounds.emax(i))) + self._log_value(gammalib.TERSE, 'Bin %d' % (i+1), value) # Write observation into logger if self._logTerse(): diff --git a/cscripts/cstsdist.py b/cscripts/cstsdist.py index 77a7124..e049a22 100755 --- a/cscripts/cstsdist.py +++ b/cscripts/cstsdist.py @@ -156,9 +156,8 @@ class cstsdist(ctools.cscript): nevents += run.events().number() # Write simulation results - if self._logExplicit(): - self._log.header3('Simulation') - self._log_value('Number of simulated events', nevents) + self._log_header3(gammalib.EXPLICIT, 'Simulation') + self._log_value(gammalib.EXPLICIT, 'Number of simulated events', nevents) # Fit model fit = ctools.ctlike(sim) @@ -175,19 +174,19 @@ class cstsdist(ctools.cscript): # Write fit results, either explicit or normal if self._logExplicit(): - self._log.header3('Test source model fit') - self._log_value('Test statistics', ts) - self._log_value('log likelihood', logL) - self._log_value('Number of predicted events', npred) + self._log_header3(gammalib.EXPLICIT, 'Test source model fit') + self._log_value(gammalib.EXPLICIT, 'Test statistics', ts) + self._log_value(gammalib.EXPLICIT, 'log likelihood', logL) + self._log_value(gammalib.EXPLICIT, 'Number of predicted events', npred) for model in models: - self._log_value('Model', model.name()) + self._log_value(gammalib.EXPLICIT, 'Model', model.name()) for par in model: - self._log(str(par)+'\n') + self._log_string(gammalib.EXPLICIT, str(par)+'\n') elif self._logNormal(): name = 'Trial %d' % seed value = 'TS=%.3f Prefactor=%e +/- %e' % \ (ts, model['Prefactor'].value(), model['Prefactor'].error()) - self._log_value(name, value) + self._log_value(gammalib.TERSE, name, value) # Initialise results colnames = [] diff --git a/cscripts/cstsmapmerge.py b/cscripts/cstsmapmerge.py index 987958e..e17c24f 100755 --- a/cscripts/cstsmapmerge.py +++ b/cscripts/cstsmapmerge.py @@ -290,9 +290,8 @@ class cstsmapmerge(ctools.cscript): # Skip file if it's not a FITS file if not gammalib.GFilename(fitsfile).is_fits(): - if self._logExplicit(): - self._log_value('Skip file', - '%s (not a FITS file)' % fitsfile) + self._log_value(gammalib.EXPLICIT, 'Skip file', + '%s (not a FITS file)' % fitsfile) continue # Open FITS file @@ -313,9 +312,8 @@ class cstsmapmerge(ctools.cscript): # ... otherwise signal that file is useless else: fits.close() - if self._logExplicit(): - self._log_value('Skip file', - '%s (no "STATUS MAP" extension)' % fitsfile) + self._log_value(gammalib.EXPLICIT, 'Skip file', + '%s (no "STATUS MAP" extension)' % fitsfile) continue # Signal if no suitable file was found @@ -344,9 +342,8 @@ class cstsmapmerge(ctools.cscript): # Skip if file is not FITS if not gammalib.GFilename(fitsfile).is_fits(): - if self._logExplicit(): - self._log_value('Skip file', - '%s (not a FITS file)' % fitsfile) + self._log_value(gammalib.EXPLICIT, 'Skip file', + '%s (not a FITS file)' % fitsfile) continue # Open FITS file @@ -355,10 +352,8 @@ class cstsmapmerge(ctools.cscript): # Skip if file does not contain status map if not fits.contains('STATUS MAP'): fits.close() - if self._logExplicit(): - self._log_value('Skip file', - '%s (no "STATUS MAP" extension)' % - fitsfile) + self._log_value(gammalib.EXPLICIT, 'Skip file', + '%s (no "STATUS MAP" extension)' % fitsfile) continue # Close FITS file @@ -386,16 +381,13 @@ class cstsmapmerge(ctools.cscript): Save TS map and remove slices if requested """ # Write header - if self._logTerse(): - self._log('\n') - self._log.header1('Save TS map') + self._log_header1(gammalib.TERSE, 'Save TS map') # Get output filename in case it was not read ahead outmap = self['outmap'].filename() # Log filename - if self._logTerse(): - self._log_value('TS map file', outmap.url()) + self._log_value(gammalib.TERSE, 'TS map file', outmap.url()) # Create FITS file fits = gammalib.GFits() @@ -430,8 +422,7 @@ class cstsmapmerge(ctools.cscript): if self._delete: for filename in self._merged_files: os.remove(filename) - if self._logTerse(): - self._log_value('Deleted input file', filename) + self._log_value(gammalib.TERSE, 'Deleted input file', filename) # Return return diff --git a/cscripts/cstsmapsplit.py b/cscripts/cstsmapsplit.py index 52127ca..1443d31 100755 --- a/cscripts/cstsmapsplit.py +++ b/cscripts/cstsmapsplit.py @@ -168,9 +168,8 @@ class cstsmapsplit(ctools.cscript): logL0 = self._compute_null_hypothesis() # Write likelihood into logger - if self._logTerse(): - self._log_value('Source removed', self._srcname) - self._log_value('Log-likelihood', repr(logL0)) + self._log_value(gammalib.TERSE, 'Source removed', self._srcname) + self._log_value(gammalib.TERSE, 'Log-likelihood', repr(logL0)) # Get parameters of TS map ctool pars = gammalib.GApplicationPars('cttsmap.par') @@ -186,10 +185,8 @@ class cstsmapsplit(ctools.cscript): base_command = 'cttsmap' # Write information into logger - if self._logTerse(): - self._log('\n') - self._log.header1('Create commands') - self._log_value('Number of cttsmap calls', njobs) + self._log_header1(gammalib.TERSE, 'Create commands') + self._log_value(gammalib.TERSE, 'Number of cttsmap calls', njobs) # Loop over TS map parameters for par in pars: diff --git a/cscripts/csworkflow.py b/cscripts/csworkflow.py index 1e83460..4ecdb91 100755 --- a/cscripts/csworkflow.py +++ b/cscripts/csworkflow.py @@ -165,20 +165,22 @@ class csworkflow(ctools.cscript): self._actors.append(entry) # Log information about actors - if self._logNormal(): - self._log_value('Actor "%s"' % name, tool) - if num_inputs == 0: - self._log_value(' Predecessor', 'none') - else: - if num_inputs == 1: - self._log.parformat(' Predecessor') - else: - self._log.parformat(' Predecessors') - for k in range(num_inputs): - if k > 0: - self._log(', ') - self._log('"'+input_actors[k]+'"') - self._log('\n') + self._log_value(gammalib.NORMAL, 'Actor "%s"' % name, tool) + + # Compute list of predecessors + if num_inputs == 0: + predecessors = 'none' + else: + predecessors = '' + for k in range(num_inputs): + if k > 0: + predecessors += ', ' + predecessors += '"'+input_actors[k]+'"' + + # Log predecessors + self._log_value(gammalib.NORMAL, + gammalib.number(' Predecessor', num_inputs), + predecessors) # Return return @@ -197,16 +199,15 @@ class csworkflow(ctools.cscript): if actor['status'] == 'ready': # Log execution start - if self._logNormal(): - self._log_value('Execute actor', '"%s"' % actor['name']) + self._log_value(gammalib.NORMAL, 'Execute actor', + '"%s"' % actor['name']) # Execute actor self._execute_actor(actor) # Log execution finish - if self._logNormal(): - self._log_value('Finished actor execution', - '"%s"' % actor['name']) + self._log_value(gammalib.NORMAL, 'Finished actor execution', + '"%s"' % actor['name']) # Set actor status to finished actor['status'] = 'finished' @@ -238,18 +239,16 @@ class csworkflow(ctools.cscript): def _execute_actor(self, actor): # Log input parameters - if self._logNormal(): - pars = actor['input_parameters'] - for par in pars: - self._log_value(' Input parameter', '%s=%s' % - (par['name'], self._get_parameter_value(par))) + pars = actor['input_parameters'] + for par in pars: + self._log_value(gammalib.NORMAL, ' Input parameter', '%s=%s' % + (par['name'], self._get_parameter_value(par))) # Log output parameters - if self._logNormal(): - pars = actor['output_parameters'] - for par in pars: - self._log_value(' Output parameter', '%s=%s' % - (par['name'], self._get_parameter_value(par))) + pars = actor['output_parameters'] + for par in pars: + self._log_value(gammalib.NORMAL, ' Output parameter', '%s=%s' % + (par['name'], self._get_parameter_value(par))) # Set actor tool if 'tool' in actor: diff --git a/pyext/ctool.i b/pyext/ctool.i index 123c69b..6ae4b84 100644 --- a/pyext/ctool.i +++ b/pyext/ctool.i @@ -195,11 +195,4 @@ def _init_cscript(self, argv): self._log_header() self._log.date(True) cscript._init_cscript = _init_cscript - -# Log the value of a parameter -def _log_value(self, name, value): - self._log.parformat(str(name)) - self._log(str(value)) - self._log('\n') -cscript._log_value = _log_value %}