-
Notifications
You must be signed in to change notification settings - Fork 186
/
hawc.py
337 lines (258 loc) · 10.4 KB
/
hawc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# Licensed under a 3-clause BSD style license - see LICENSE.rst
"""HAWC catalogs (https://www.hawc-observatory.org)."""
import abc
import numpy as np
from astropy.table import Table
from gammapy.modeling.models import Model, SkyModel
from gammapy.utils.scripts import make_path
from .core import SourceCatalog, SourceCatalogObject
__all__ = [
"SourceCatalog2HWC",
"SourceCatalog3HWC",
"SourceCatalogObject2HWC",
"SourceCatalogObject3HWC",
]
class SourceCatalogObjectHWCBase(SourceCatalogObject, abc.ABC):
"""Base class for the HAWC catalogs objects."""
_source_name_key = "source_name"
def __str__(self):
return self.info()
def info(self, info="all"):
"""Summary information string.
Parameters
----------
info : {'all', 'basic', 'position', 'spectrum'}
Comma separated list of options
"""
if info == "all":
info = "basic,position,spectrum"
ss = ""
ops = info.split(",")
if "basic" in ops:
ss += self._info_basic()
if "position" in ops:
ss += self._info_position()
if "spectrum" in ops:
ss += self._info_spectrum()
return ss
def _info_basic(self):
"""Print basic information."""
return (
f"\n*** Basic info ***\n\n"
f"Catalog row index (zero-based) : {self.row_index}\n"
f"Source name : {self.name}\n"
)
def _info_position(self):
"""Print position information."""
return (
f"\n*** Position info ***\n\n"
f"RA: {self.data.ra:.3f}\n"
f"DEC: {self.data.dec:.3f}\n"
f"GLON: {self.data.glon:.3f}\n"
f"GLAT: {self.data.glat:.3f}\n"
f"Position error: {self.data.pos_err:.3f}\n"
)
def _info_spectrum(self):
"""Print spectral information."""
ss = "\n*** Spectral info ***\n\n"
ss += self._info_spectrum_one(0)
if self.n_models == 2:
ss += self._info_spectrum_one(1)
else:
ss += "No second spectrum available"
return ss
class SourceCatalogObject2HWC(SourceCatalogObjectHWCBase):
"""One source from the HAWC 2HWC catalog.
Catalog is represented by `~gammapy.catalog.SourceCatalog2HWC`.
"""
@property
def n_models(self):
"""Number of models (1 or 2)."""
if hasattr(self.data, "spec1_dnde"):
return 1 if np.isnan(self.data.spec1_dnde) else 2
else:
return 1
def _get_idx(self, which):
if which == "point":
return 0
elif which == "extended":
if self.n_models == 2:
return 1
else:
raise ValueError(f"No extended source analysis available: {self.name}")
else:
raise ValueError(f"Invalid which: {which!r}")
def _info_spectrum_one(self, idx):
d = self.data
ss = f"Spectrum {idx}:\n"
val, err = d[f"spec{idx}_dnde"].value, d[f"spec{idx}_dnde_err"].value
ss += f"Flux at 7 TeV: {val:.3} +- {err:.3} cm-2 s-1 TeV-1\n"
val, err = d[f"spec{idx}_index"], d[f"spec{idx}_index_err"]
ss += f"Spectral index: {val:.3f} +- {err:.3f}\n"
radius = d[f"spec{idx}_radius"]
ss += f"Test Radius: {radius:1}\n\n"
return ss
def spectral_model(self, which="point"):
"""Spectral model (`~gammapy.modeling.models.PowerLawSpectralModel`).
* ``which="point"`` -- Spectral model under the point source assumption.
* ``which="extended"`` -- Spectral model under the extended source assumption.
Only available for some sources. Raise ValueError if not available.
"""
idx = self._get_idx(which)
pars = {
"reference": "7 TeV",
"amplitude": self.data[f"spec{idx}_dnde"],
"index": -self.data[f"spec{idx}_index"],
}
errs = {
"amplitude": self.data[f"spec{idx}_dnde_err"],
"index": self.data[f"spec{idx}_index_err"],
}
model = Model.create("PowerLawSpectralModel", "spectral", **pars)
for name, value in errs.items():
model.parameters[name].error = value
return model
def spatial_model(self, which="point"):
"""Spatial model (`~gammapy.modeling.models.SpatialModel`).
* ``which="point"`` - `~gammapy.modeling.models.PointSpatialModel`.
* ``which="extended"`` - `~gammapy.modeling.models.DiskSpatialModel`.
Only available for some sources. Raise ValueError if not available.
"""
idx = self._get_idx(which)
pars = {"lon_0": self.data.glon, "lat_0": self.data.glat, "frame": "galactic"}
if idx == 0:
tag = "PointSpatialModel"
else:
tag = "DiskSpatialModel"
pars["r_0"] = self.data[f"spec{idx}_radius"]
errs = {
"lat_0": self.data.pos_err,
"lon_0": self.data.pos_err / np.cos(self.data.glat),
}
model = Model.create(tag, "spatial", **pars)
for name, value in errs.items():
model.parameters[name].error = value
return model
def sky_model(self, which="point"):
"""Sky model (`~gammapy.modeling.models.SkyModel`).
* ``which="point"`` - Sky model for point source analysis.
* ``which="extended"`` - Sky model for extended source analysis.
Only available for some sources. Raise ValueError if not available.
According to the paper, the radius of the extended source model is only a rough estimate
of the source size, based on the residual excess.
"""
return SkyModel(
spatial_model=self.spatial_model(which),
spectral_model=self.spectral_model(which),
name=self.name,
)
class SourceCatalog2HWC(SourceCatalog):
"""HAWC 2HWC catalog.
One source is represented by `~gammapy.catalog.SourceCatalogObject2HWC`.
The data is from tables 2 and 3 in the paper [1]_.
The catalog table contains 40 rows / sources.
The paper mentions 39 sources e.g. in the abstract.
The difference is due to Geminga, which was detected as two "sources" by the algorithm
used to make the catalog, but then in the discussion considered as one source.
References
----------
.. [1] Abeysekara et al, "The 2HWC HAWC Observatory Gamma Ray Catalog",
On ADS: `2017ApJ...843...40A <https://ui.adsabs.harvard.edu/abs/2017ApJ...843...40A>`__
"""
tag = "2hwc"
"""Catalog name"""
description = "2HWC catalog from the HAWC observatory"
"""Catalog description"""
source_object_class = SourceCatalogObject2HWC
def __init__(self, filename="$GAMMAPY_DATA/catalogs/2HWC.ecsv"):
table = Table.read(make_path(filename), format="ascii.ecsv")
source_name_key = "source_name"
super().__init__(table=table, source_name_key=source_name_key)
class SourceCatalogObject3HWC(SourceCatalogObjectHWCBase):
"""One source from the HAWC 3HWC catalog.
Catalog is represented by `~gammapy.catalog.SourceCatalog3HWC`.
"""
@property
def n_models(self):
"""Number of models."""
return 1
def _info_spectrum_one(self, idx):
d = self.data
ss = f"Spectrum {idx}:\n"
val, errn, errp = (
d[f"spec{idx}_dnde"].value,
d[f"spec{idx}_dnde_errn"].value,
d[f"spec{idx}_dnde_errp"].value,
)
ss += f"Flux at 7 TeV: {val:.3} {errn:.3} + {errp:.3} cm-2 s-1 TeV-1\n"
val, errn, errp = (
d[f"spec{idx}_index"],
d[f"spec{idx}_index_errn"],
d[f"spec{idx}_index_errp"],
)
ss += f"Spectral index: {val:.3f} {errn:.3f} + {errp:.3f}\n"
radius = d[f"spec{idx}_radius"]
ss += f"Test Radius: {radius:1}\n\n"
return ss
@property
def is_pointlike(self):
return self.data["spec0_radius"] == 0.0
def spectral_model(self):
"""Spectral model as a `~gammapy.modeling.models.PowerLawSpectralModel` object."""
pars = {
"reference": "7 TeV",
"amplitude": self.data["spec0_dnde"],
"index": -self.data["spec0_index"],
}
errs = {
"index": 0.5
* (self.data["spec0_index_errp"] + np.abs(self.data["spec0_index_errn"])),
"amplitude": 0.5
* (self.data["spec0_dnde_errp"] + np.abs(self.data["spec0_dnde_errn"])),
}
model = Model.create("PowerLawSpectralModel", "spectral", **pars)
for name, value in errs.items():
model.parameters[name].error = value
return model
def spatial_model(self):
"""Spatial model as a `~gammapy.modeling.models.SpatialModel` object."""
pars = {"lon_0": self.data.glon, "lat_0": self.data.glat, "frame": "galactic"}
if self.is_pointlike:
tag = "PointSpatialModel"
else:
tag = "DiskSpatialModel"
pars["r_0"] = self.data["spec0_radius"]
errs = {
"lat_0": self.data.pos_err,
"lon_0": self.data.pos_err / np.cos(self.data.glat),
}
model = Model.create(tag, "spatial", **pars)
for name, value in errs.items():
model.parameters[name].error = value
return model
def sky_model(self):
"""Sky model as a `~gammapy.modeling.models.SkyModel` object."""
return SkyModel(
spatial_model=self.spatial_model(),
spectral_model=self.spectral_model(),
name=self.name,
)
class SourceCatalog3HWC(SourceCatalog):
"""HAWC 3HWC catalog.
One source is represented by `~gammapy.catalog.SourceCatalogObject3HWC`.
The data is from tables 2 and 3 in the paper [1]_.
The catalog table contains 65 rows / sources.
References
----------
.. [1] 3HWC: The Third HAWC Catalog of Very-High-Energy Gamma-ray Sources",
https://data.hawc-observatory.org/datasets/3hwc-survey/index.php
"""
tag = "3hwc"
"""Catalog name"""
description = "3HWC catalog from the HAWC observatory"
"""Catalog description"""
source_object_class = SourceCatalogObject3HWC
def __init__(self, filename="$GAMMAPY_DATA/catalogs/3HWC.ecsv"):
table = Table.read(make_path(filename), format="ascii.ecsv")
source_name_key = "source_name"
super().__init__(table=table, source_name_key=source_name_key)