27 def test_source(models, srcname, ra=None, dec=None, fitspat=False,
30 Return model for TS fitting of a test source
34 models : `~gammalib.GModels`
39 Right Ascension of test source (deg)
41 Declination of test source (deg)
42 fitspat : bool, optional
43 Fit spatial parameters of all models?
44 fitspec : bool, optional
45 Fit spectral parameters of all models?
49 model : `~gammalib.GModels`
50 Model container for TS fitting
53 outmodels = models.copy()
57 for model
in outmodels:
61 model = outmodels[srcname]
65 spectral = model.spectral()
68 if not (spectral.has_par(
'Normalization')
or
69 spectral.has_par(
'Value')
or
70 spectral.has_par(
'Prefactor')
or
71 spectral.has_par(
'Integral')
or
72 spectral.has_par(
'PhotonFlux')
or
73 spectral.has_par(
'EnergyFlux')):
74 msg = (
'Model "%s" has no flux normalisation parameter. Only spectral '
75 'models with a flux normalisation parameter (one of '
76 '"Normalization", "Value", "Prefactor", "Integral", '
77 '"PhotonFlux", and "EnergyFlux") are supported.' %
79 raise RuntimeError(msg)
82 if ra !=
None and dec !=
None:
83 if model.has_par(
'RA')
and model.has_par(
'DEC'):
85 model[
'DEC'].value(dec)
88 spatial = [
'RA',
'DEC',
'Sigma',
'Radius',
'Width',
'PA',
89 'MinorRadius',
'MajorRadius']
90 spectral = [
'Index',
'Index1',
'Index2',
'BreakEnergy',
'CutoffEnergy',
91 'InverseCutoffEnergy']
95 if model.has_par(par):
103 if model.has_par(par):
118 Return spectral model normalisation parameter
122 model : `~gammalib.GModel`
127 par : `~gammalib.GModelPar`
128 Spectral model normalisation parameter
134 norm_pars = [
'Normalization',
'Value',
'Prefactor',
'Integral',
135 'PhotonFlux',
'EnergyFlux']
138 spectral = model.spectral()
141 for norm_par
in norm_pars:
142 if spectral.has_par(norm_par):
143 par = spectral[norm_par]
147 msg = (
'Model "%s" has no flux normalisation parameter. Only spectral '
148 'models with a flux normalisation parameter (one of '
149 '"Normalization", "Value", "Prefactor", "Integral", '
150 '"PhotonFlux", and "EnergyFlux") are supported.' %
152 raise RuntimeError(msg)
161 def ds9attributes(model, free_color='green', fixed_color='magenta',
162 width=2, fontfamily=
'helvetica', fontsize=12,
163 fontweight=
'normal', fontslant=
'roman'):
165 Returns attributes for DS9 string
169 model : `~gammalib.GModel`
171 free_color : str, optional
172 Color for sources with free parameters (any DS9 color or hex code)
173 fixed_color : str, optional
174 Color for source without free parameters (any DS9 color or hex code)
175 width : int, optional
176 Line width for regions
177 fontfamily : str, optional
178 Font family for source labels (helvetica,times,courier)
179 fontsize : int, optional
180 Font size for source labels
181 fontweight : str, optional
182 Font weight for source labels (normal,bold)
183 fontslant : str, optional
184 Font slant for source labels (roman,italic)
201 attributes = (
' color=%s width=%d font="%s %d %s %s"' %
202 (color, width, fontfamily, fontsize, fontweight, fontslant))
212 show_labels=
True, show_ext_type=
True,
213 free_color=
'green', fixed_color=
'magenta',
214 width=2, fontfamily=
'helvetica', fontsize=12,
215 fontweight=
'normal', fontslant=
'roman'):
217 Converts model into a DS9 region string
221 model : `~gammalib.GModel`
223 pnt_type : str, optional
224 Marker type for point sources (circle,box,diamond,cross,x,arrow,boxcircle)
225 pnt_mark_size : integer, optional
226 Marker size for point sources
227 show_labels : boolean, optional
229 show_ext_type : boolean, optional
230 Show type of extended model in source name?
231 free_color : str, optional
232 Color for sources with free parameters (any DS9 color or hex code)
233 fixed_color : str, optional
234 Color for source without free parameters (any DS9 color or hex code)
235 width : int, optional
236 Line width for regions
237 fontfamily : str, optional
238 Font family for source labels (helvetica,times,courier)
239 fontsize : int, optional
240 Font size for source labels
241 fontweight : str, optional
242 Font weight for source labels (normal,bold)
243 fontslant : str, optional
244 Font slant for source labels (roman,italic)
261 modelpos = model.spatial().dir()
262 except AttributeError:
263 msg = (
'Skip model "%s" since it has no sky direction.\n' %
271 modeltype = model.type()
272 modelname = model.name()
275 if modeltype ==
'PointSource':
281 ds9string += (
'point(%.6f,%.6f) # point=%s %d' %
282 (modelpos.ra_deg(), modelpos.dec_deg(),
283 pnt_type, pnt_mark_size))
286 elif modeltype ==
"ExtendedSource":
289 spatial = model.spatial()
290 classname = spatial.classname()
293 if 'Radial' in classname:
297 shorttype = classname.split(
'Radial')[-1]
300 if (classname ==
'GModelSpatialRadialDisk' or
301 classname ==
'GModelSpatialRadialShell'):
302 size = spatial.radius()
305 elif classname ==
'GModelSpatialRadialGauss':
306 size = spatial.sigma()
310 msg = (
'Skip model "%s" since the radial model "%s" '
311 'is unknown.\n' % (model.name(), classname))
316 ds9string += (
'circle(%.6f,%.6f,%.6f") #' %
317 (modelpos.ra_deg(), modelpos.dec_deg(),
321 elif 'Elliptical' in classname:
324 shorttype = classname.split(
'Elliptical')[-1]
325 size1 = spatial.semimajor()
326 size2 = spatial.semiminor()
327 angle = spatial.posangle()
330 ds9string += (
'ellipse(%.6f,%.6f,%.6f",%.6f",%.6f) #' %
331 (modelpos.ra_deg(), modelpos.dec_deg(),
332 size1*3600.0, size2*3600.0, angle+90.0))
336 msg = (
'Skip model "%s" since the model "%s" is neither '
337 'a point source, a radial source, nor an elliptical '
338 'source.\n' % (model.name(), classname))
343 modelname +=
' ('+shorttype+
')'
348 fixed_color=fixed_color,
350 fontfamily=fontfamily,
352 fontweight=fontweight,
355 ds9string +=
' text={'+modelname+
'}'
360 return ds9string, msg
367 pnt_type=
'cross', pnt_mark_size=12,
368 show_labels=
True, show_ext_type=
True,
369 free_color=
'green', fixed_color=
'magenta',
370 width=2, fontfamily=
'helvetica', fontsize=12,
371 fontweight=
'normal', fontslant=
'roman'):
373 Save models in a DS9 region file
377 model : `~gammalib.GModel`
381 pnt_type : str, optional
382 Marker type for point sources (circle,box,diamond,cross,x,arrow,boxcircle)
383 pnt_mark_size : integer, optional
384 Marker size for point sources
385 show_labels : boolean, optional
387 show_ext_type : boolean, optional
388 Show type of extended model in source name?
389 free_color : str, optional
390 Color for sources with free parameters (any DS9 color or hex code)
391 fixed_color : str, optional
392 Color for source without free parameters (any DS9 color or hex code)
393 width : int, optional
394 Line width for regions
395 fontfamily : str, optional
396 Font family for source labels (helvetica,times,courier)
397 fontsize : int, optional
398 Font size for source labels
399 fontweight : str, optional
400 Font weight for source labels (normal,bold)
401 fontslant : str, optional
402 Font slant for source labels (roman,italic)
413 f = open(filename,
'w')
422 if (model.type() ==
'PointSource' or
423 model.type() ==
'ExtendedSource'):
426 pnt_mark_size=pnt_mark_size,
427 show_labels=show_labels,
428 show_ext_type=show_ext_type,
429 free_color=free_color,
430 fixed_color=fixed_color,
432 fontfamily=fontfamily,
434 fontweight=fontweight,
442 elif model.type() ==
'DiffuseSource':
443 errors +=
'Skipping diffuse model "'+model.name()+
'".\n'
447 errors +=
'Skipping background model "'+model.name()+
'".\n'
def normalisation_parameter