This is a fixed-text formatted version of a Jupyter notebook

Data structures

Introduction

This is a getting started tutorial for Gammapy.

In this tutorial we will use the Second Fermi-LAT Catalog of High-Energy Sources (3FHL) catalog, corresponding event list and images to learn how to work with some of the central Gammapy data structures.

We will cover the following topics:

Setup

Important: to run this tutorial the environment variable GAMMAPY_DATA must be defined and point to the directory on your machine where the datasets needed are placed. To check whether your setup is correct you can execute the following cell:

[1]:
import os

path = os.path.expandvars("$GAMMAPY_DATA")

if not os.path.exists(path):
    raise Exception("gammapy-data repository not found!")
else:
    print("Great your setup is correct!")
Great your setup is correct!

In case you encounter an error, you can un-comment and execute the following cell to continue. But we recommend to set up your environment correctly as described here after you are done with this notebook.

[2]:
# os.environ['GAMMAPY_DATA'] = os.path.join(os.getcwd(), '..')

Now we can continue with the usual IPython notebooks and Python imports:

[3]:
%matplotlib inline
import matplotlib.pyplot as plt
[4]:
import astropy.units as u
from astropy.coordinates import SkyCoord

Maps

The gammapy.maps package contains classes to work with sky images and cubes.

In this section, we will use a simple 2D sky image and will learn how to:

  • Read sky images from FITS files

  • Smooth images

  • Plot images

  • Cutout parts from images

[5]:
from gammapy.maps import Map

gc_3fhl = Map.read("$GAMMAPY_DATA/fermi-3fhl-gc/fermi-3fhl-gc-counts.fits.gz")

The image is a gammapy.maps.WcsNDMap object:

[6]:
gc_3fhl
[6]:
WcsNDMap

        geom  : WcsGeom
        axes  : ['lon', 'lat']
        shape : (400, 200)
        ndim  : 2
        unit  :
        dtype : >i8

The shape of the image is 400 x 200 pixel and it is defined using a cartesian projection in galactic coordinates.

The geom attribute is a gammapy.maps.WcsGeom object:

[7]:
gc_3fhl.geom
[7]:
WcsGeom

        axes       : ['lon', 'lat']
        shape      : (400, 200)
        ndim       : 2
        frame      : galactic
        projection : CAR
        center     : 0.0 deg, 0.0 deg
        width      : 20.0 deg x 10.0 deg
        wcs ref    : 0.0 deg, 0.0 deg

Let’s take a closer look a the .data attribute:

[8]:
gc_3fhl.data
[8]:
array([[0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       ...,
       [0, 0, 0, ..., 0, 0, 1],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 1]])

That looks familiar! It just an ordinary 2 dimensional numpy array, which means you can apply any known numpy method to it:

[9]:
print(f"Total number of counts in the image: {gc_3fhl.data.sum():.0f}")
Total number of counts in the image: 32684

To show the image on the screen we can use the plot method. It basically calls plt.imshow, passing the gc_3fhl.data attribute but in addition handles axis with world coordinates using astropy.visualization.wcsaxes and defines some defaults for nicer plots (e.g. the colormap ‘afmhot’):

[10]:
gc_3fhl.plot(stretch="sqrt");
../../_images/tutorials_starting_overview_20_0.png

To make the structures in the image more visible we will smooth the data using a Gaussian kernel.

[11]:
gc_3fhl_smoothed = gc_3fhl.smooth(kernel="gauss", width=0.2 * u.deg)
[12]:
gc_3fhl_smoothed.plot(stretch="sqrt");
../../_images/tutorials_starting_overview_23_0.png

The smoothed plot already looks much nicer, but still the image is rather large. As we are mostly interested in the inner part of the image, we will cut out a quadratic region of the size 9 deg x 9 deg around Vela. Therefore we use gammapy.maps.Map.cutout to make a cutout map:

[13]:
# define center and size of the cutout region
center = SkyCoord(0, 0, unit="deg", frame="galactic")
gc_3fhl_cutout = gc_3fhl_smoothed.cutout(center, 9 * u.deg)
gc_3fhl_cutout.plot(stretch="sqrt");
../../_images/tutorials_starting_overview_25_0.png

For a more detailed introduction to gammapy.maps, take a look a the maps.ipynb notebook.

Exercises

[ ]:

Event lists

Almost any high level gamma-ray data analysis starts with the raw measured counts data, which is stored in event lists. In Gammapy event lists are represented by the gammapy.data.EventList class.

In this section we will learn how to:

  • Read event lists from FITS files

  • Access and work with the EventList attributes such as .table and .energy

  • Filter events lists using convenience methods

Let’s start with the import from the gammapy.data submodule:

[14]:
from gammapy.data import EventList

Very similar to the sky map class an event list can be created, by passing a filename to the gammapy.data.EventList.read() method:

[15]:
events_3fhl = EventList.read(
    "$GAMMAPY_DATA/fermi-3fhl-gc/fermi-3fhl-gc-events.fits.gz"
)

This time the actual data is stored as an astropy.table.Table object. It can be accessed with .table attribute:

[16]:
events_3fhl.table
[16]:
Table length=32843
ENERGYRADECLBTHETAPHIZENITH_ANGLEEARTH_AZIMUTH_ANGLETIMEEVENT_IDRUN_IDRECON_VERSIONCALIB_VERSION [3]EVENT_CLASS [32]EVENT_TYPE [32]CONVERSION_TYPELIVETIMEDIFRSP0DIFRSP1DIFRSP2DIFRSP3DIFRSP4
MeVdegdegdegdegdegdegdegdegss
float32float32float32float32float32float32float32float32float32float64int32int32int16int16boolboolint16float64float32float32float32float32float32
12186.642260.45935-33.553337353.362731.753867671.977325125.5069459.22307231.79672239572401.29222104182304023957167000 .. 0False .. TrueFalse .. True0238.578372389078140.00.00.00.00.0
25496.598261.37506-34.395004353.096070.652065242.49406278.4934741.092773227.89838239577842.1621734255083323957766300 .. 0False .. TrueFalse .. False1176.168507546186450.00.00.00.00.0
15621.498259.56973-33.409416353.056732.445068464.32412234.2219466.526794232.75734239578244.7997108135317523957766300 .. 0False .. TrueFalse .. False19.3920756578445430.00.00.00.00.0
12816.32273.95883-25.3403916.45856-4.054887343.292503142.8739213.232716108.02273239605914.66160735963624123960127600 .. 0False .. TrueFalse .. False14.0347865521907810.00.00.00.00.0
18988.387260.8568-36.355804351.23734-0.10191239426.916113290.3933723.8726212.91147239611913.144604151123318823960687100 .. 0False .. TrueFalse .. True0131.601328969001770.00.00.00.00.0
11610.23266.15518-26.2244362.19860271.603481935.77363274.5338723.537594232.64166239623554.554147331415681123961832900 .. 0False .. TrueFalse .. False174.981109380722050.00.00.00.00.0
13960.802271.44742-29.6153161.6267247-4.143115525.917883238.036815.037035123.32094239634549.17487261414056923962978800 .. 0False .. TrueFalse .. False1106.373368173837660.00.00.00.00.0
10477.372266.3981-28.96814359.97003-0.01174817739.091587275.545733.02354229.59308239635161.879829821568839323962978800 .. 0False .. TrueFalse .. True0214.628174066543580.00.00.00.00.0
13030.88271.70428-20.6326279.593480.02624146852.622505161.320539.35084591.9986239639873.2076075173648223963943600 .. 0False .. TrueFalse .. True094.687530636787410.00.00.00.00.0
11517.904265.00894-30.065119358.401120.4390443641.812317276.0244837.975704229.18602239652422.2848442211681723965166000 .. 0False .. TrueFalse .. True0123.150073021650310.00.00.00.00.0
19958.182263.31854-37.094856351.71606-2.15371352.544586121.41576419.790777195.32071239657693.099792411458623965767300 .. 0False .. TrueFalse .. True017.1135305464267730.00.00.00.00.0
23760.14265.4694-31.55994357.34256-0.6880552.70319248.8781353.087788229.58832239669871.2372901457719323966964300 .. 0False .. TrueFalse .. False1225.25441130995750.00.00.00.00.0
32168.988266.35397-30.096745358.98682-0.5672306464.830696212.9364567.121216114.544685239673726.5025267758904623966964300 .. 0False .. TrueFalse .. False145.647767096757890.00.00.00.00.0
10165.9266.15427-19.8292797.6689374.932032651.85829207.017750.96344495.7839239691232.08568192829555723968712400 .. 0False .. TrueFalse .. True0204.404025763273240.00.00.00.00.0
25277.545269.68344-24.6686445.1628966-0.3417930342.77833129.1385710.365034205.91513239697927.854824751156666123969280900 .. 0False .. TrueFalse .. True085.297610104084010.00.00.00.00.0
223713.7261.1885-31.794796355.16192.23980225.89418284.872817.017782206.84254239703590.657642251280834823969853900 .. 0False .. TrueFalse .. True0166.998246610164640.00.00.00.00.0
15236.758264.52255-31.730831356.7694-0.0960457251.646976141.4457624.359364113.722145239708834.389992061262648423970426800 .. 0False .. TrueFalse .. True05.3342069685459140.00.00.00.00.0
36314.297264.79855-31.116806357.414340.03273258748.567314122.82487517.206326204.58994239709375.130452361418618423970426800 .. 0False .. TrueFalse .. False134.091806501150130.00.00.00.00.0
.....................................................................
74033.305268.76508-24.436124.9447240.497404951.1174225.204991.1782029344.13354459968408.4067848335304845996740200 .. 0False .. TrueFalse .. True065.831601262092590.00.00.00.00.0
47114.324266.99127-28.4210510.70769924-0.172607160.81272212.2702545.926052112.289925460036242.88448673787410846003265600 .. 0False .. TrueFalse .. False177.020128667354580.00.00.00.00.0
14473.36259.55792-32.497227353.798372.977414669.47437136.7977646.463108120.018074460030422.7761504683443846002694600 .. 0False .. TrueFalse .. True013.9225651621818540.00.00.00.00.0
20056.527267.50372-21.4946946.8887262.992457246.228405129.999196.3767605305.91837460031341.48834544975417846002694600 .. 0False .. TrueFalse .. False1115.297873973846440.00.00.00.00.0
10021.135266.58603-29.0060460.023192262-0.1718332658.983257139.4274129.243576109.29137460042219.35562307955255046003836600 .. 0False .. TrueFalse .. True0115.271165132522580.00.00.00.00.0
11217.242260.67258-32.080307354.678962.440756676.96116214.3027377.80194121.61477460104239.29869664521829846010137400 .. 0False .. TrueFalse .. False164.041108727455140.00.00.00.00.0
18494.64267.2745-36.705185353.7175-4.637091638.942596226.9218913.237086149.41605460116880.22025174957819746011259000 .. 0False .. TrueFalse .. False1111.120076417922970.00.00.00.00.0
13333.997265.57535-31.519987357.42413-0.743687843.89681230.499137.34928145.94727460128338.68317551145915946012401000 .. 0False .. TrueFalse .. False123.2387939691543580.00.00.00.00.0
13160.466269.90778-24.6718985.2616706-0.5201554360.9269226.4570840.456112248.86581460163408.592799811427146016340300 .. 0False .. TrueFalse .. True01.95164966583251950.00.00.00.00.0
387834.72270.3779-21.567118.1717490.6453147556.755512221.8471524.35845486.67913460185260.7970139759562646018126000 .. 0False .. TrueFalse .. True034.2146940231323240.00.00.00.00.0
20559.74268.5538-26.3456923.200638-0.3032898649.523575233.6728512.370642250.35716460185803.2027966878559046018126000 .. 0False .. TrueFalse .. True0103.176299691200260.00.00.00.00.0
27209.146266.59344-30.52607358.72775-0.9671817462.1856140.2743432.686306109.74662460190778.6372646727445346018697600 .. 0False .. TrueFalse .. True043.223347127437590.00.00.00.00.0
13911.061269.30997-27.2394392.7684028-1.336530165.15399224.5210153.017742242.62904460197889.26526911204988746019219800 .. 0False .. TrueFalse .. True095.463564038276670.00.00.00.00.0
13226.425265.16287-27.3442380.77969421.768017859.38332126.701932.12299246.97205460203215.108161451171688846019823500 .. 0False .. TrueFalse .. False13.7330975532531740.00.00.00.00.0
17445.463266.63342-28.8072010.21464892-0.103970555.48627135.5915514.227151106.7812460225372.9842249167908246022493300 .. 0False .. TrueFalse .. False180.522352814674380.00.00.00.00.0
13133.864270.42474-22.6510587.2511850.07135820448.704975134.731022.489122294.48605460225688.52486295287933546022493300 .. 0False .. TrueFalse .. False1117.881733417510990.00.00.00.00.0
32095.705266.0002-29.77206359.1034-0.1361523145.013103236.724986.92107212.86594460231367.1387127111370646023108400 .. 0False .. TrueFalse .. True0108.929764926433560.00.00.00.00.0
18465.783266.39728-29.105953359.85202-0.0829405855.97552135.8778718.909636112.137924459939497.057684768983145993557200 .. 0False .. TrueFalse .. True070.726386845111850.00.00.00.00.0
14457.25262.72217-34.388405353.7184-0.2690681245.683174237.7416225.728264240.87035459945845.47984051004968045994130200 .. 0False .. TrueFalse .. True0147.42747879028320.00.00.00.00.0

You can do len over event_3fhl.table to find the total number of events.

[17]:
len(events_3fhl.table)
[17]:
32843

And we can access any other attribute of the Table object as well:

[18]:
events_3fhl.table.colnames
[18]:
['ENERGY',
 'RA',
 'DEC',
 'L',
 'B',
 'THETA',
 'PHI',
 'ZENITH_ANGLE',
 'EARTH_AZIMUTH_ANGLE',
 'TIME',
 'EVENT_ID',
 'RUN_ID',
 'RECON_VERSION',
 'CALIB_VERSION',
 'EVENT_CLASS',
 'EVENT_TYPE',
 'CONVERSION_TYPE',
 'LIVETIME',
 'DIFRSP0',
 'DIFRSP1',
 'DIFRSP2',
 'DIFRSP3',
 'DIFRSP4']

For convenience we can access the most important event parameters as properties on the EventList objects. The attributes will return corresponding Astropy objects to represent the data, such as astropy.units.Quantity, astropy.coordinates.SkyCoord or astropy.time.Time objects:

[19]:
events_3fhl.energy.to("GeV")
[19]:
$$[12.186643,~25.496599,~15.621499,~\dots,~32.095707,~18.465784,~14.457251] \; \mathrm{GeV}$$
[20]:
events_3fhl.galactic
# events_3fhl.radec
[20]:
<SkyCoord (Galactic): (l, b) in deg
    [(353.36228879,  1.75408483), (353.09562941,  0.6522806 ),
     (353.05628243,  2.44528685), ..., (359.10295505, -0.1359316 ),
     (359.85157506, -0.08269984), (353.71795506, -0.26883694)]>
[21]:
events_3fhl.time
[21]:
<Time object: scale='tt' format='mjd' value=[54682.82946153 54682.89243456 54682.89709472 ... 57236.75267735
 57233.37455141 57233.44802852]>

In addition EventList provides convenience methods to filter the event lists. One possible use case is to find the highest energy event within a radius of 0.5 deg around the vela position:

[22]:
# select all events within a radius of 0.5 deg around center
from gammapy.utils.regions import SphericalCircleSkyRegion

region = SphericalCircleSkyRegion(center, radius=0.5 * u.deg)
events_gc_3fhl = events_3fhl.select_region(region)

# sort events by energy
events_gc_3fhl.table.sort("ENERGY")

# and show highest energy photon
events_gc_3fhl.energy[-1].to("GeV")
[22]:
$$1917.8592 \; \mathrm{GeV}$$

Exercises

  • Make a counts energy spectrum for the galactic center region, within a radius of 10 deg.

[ ]:

Source catalogs

Gammapy provides a convenient interface to access and work with catalog based data.

In this section we will learn how to:

  • Load builtins catalogs from gammapy.catalog

  • Sort and index the underlying Astropy tables

  • Access data from individual sources

Let’s start with importing the 3FHL catalog object from the gammapy.catalog submodule:

[23]:
from gammapy.catalog import SourceCatalog3FHL

First we initialize the Fermi-LAT 3FHL catalog and directly take a look at the .table attribute:

[24]:
fermi_3fhl = SourceCatalog3FHL()
fermi_3fhl.table
[24]:
Table length=1556
Source_NameRAJ2000DEJ2000GLONGLATConf_95_SemiMajorConf_95_SemiMinorConf_95_PosAngROI_numSignif_AvgPivot_EnergyFlux_DensityUnc_Flux_DensityFluxUnc_FluxEnergy_FluxUnc_Energy_FluxSignif_CurveSpectrumTypeSpectral_IndexUnc_Spectral_IndexbetaUnc_betaPowerLaw_IndexUnc_PowerLaw_IndexFlux_Band [5]Unc_Flux_Band [5,2]nuFnu [5]Sqrt_TS_Band [5]NpredHEP_EnergyHEP_ProbVariability_BayesBlocksExtended_Source_NameASSOC_GAMTEVCAT_FLAGASSOC_TEVCLASSASSOC1ASSOC2ASSOC_PROB_BAYASSOC_PROB_LRRedshiftNuPeak_obs
degdegdegdegdegdegdegGeV1 / (cm2 GeV s)1 / (cm2 GeV s)1 / (cm2 s)1 / (cm2 s)erg / (cm2 s)erg / (cm2 s)1 / (cm2 s)1 / (cm2 s)erg / (cm2 s)GeVHz
bytes18float32float32float32float32float32float32float32int16float32float32float32float32float32float32float32float32float32bytes11float32float32float32float32float32float32float32float32float32float32float32float32float32int16bytes18bytes18bytes1bytes21bytes7bytes26bytes26float32float32float32float32
3FHL J0001.2-07480.3107-7.807589.0094-67.31180.04240.0424--645.36223.735.3174e-132.0975e-132.9593e-111.1704e-111.6752e-121.0743e-121.02PowerLaw1.67240.82740.59160.71292.22260.48081.1127661e-11 .. 1.1422301e-22-6.0763976e-12 .. 6.529277e-123.533989e-13 .. 1.1789072e-223.1458344 .. 0.07.6386.9750.996413FGL J0001.2-0748NbllPMN J0001-07460.99740.9721--306196370000000.0
3FHL J0001.9-41550.4849-41.9303334.1216-72.06970.10180.1018--4295.63828.425.4253e-131.6839e-134.3230e-111.3428e-113.4900e-121.8276e-120.45PowerLaw1.78190.49410.11870.27981.94180.31002.1003905e-11 .. 1.9287885e-18-8.032091e-12 .. 5.8594097e-126.7452245e-13 .. 2.078675e-184.899907 .. 0.012.51266.6250.962213FGL J0002.2-4152Nbcu1RXS J000135.5-4155190.99600.0000--6309576500000000.0
3FHL J0002.1-67280.5283-67.4825310.0868-48.95490.03570.0357--3868.47020.821.2062e-123.2106e-135.0093e-111.3349e-112.3058e-129.5580e-131.53PowerLaw1.81090.62600.79330.59562.42850.37102.4550664e-11 .. 1.9009976e-21-8.634195e-12 .. 4.8021903e-127.7340695e-13 .. 1.9026535e-215.900217 .. 0.017.1152.1520.998813FGL J0002.0-6722NbcuSUMSS J000215-6726530.00000.9395--4466832000000000.0
3FHL J0003.3-52480.8300-52.8150318.9245-62.79360.04250.0425--1457.22923.667.5065e-132.3102e-134.1560e-111.2839e-112.2874e-121.1145e-121.70PowerLaw1.60100.56440.99720.17212.24810.37322.0886386e-11 .. 7.5867555e-23-8.143967e-12 .. 5.31299e-126.6265456e-13 .. 7.800202e-235.298393 .. 0.013.0267.3100.963613FGL J0003.2-5246NbcuRBS 00060.99960.9716--7.079464e+16
3FHL J0007.0+73031.764773.0560119.662510.46660.01010.0101--27775.26512.801.7436e-107.5950e-121.5308e-096.1341e-113.6785e-111.5973e-123.24LogParabola3.17510.21030.90210.26593.83150.11411.3514667e-09 .. 3.839895e-18-5.7581186e-11 .. 4.060418e-124.109739e-11 .. 2.9231144e-1871.33829 .. 0.0654.1560.2920.997213FGL J0007.0+7302ECTA 1PSRLAT PSR J0007+73031.00000.0000----
3FHL J0007.9+47111.993147.1920115.3093-15.03540.01960.0196--30217.77417.195.9778e-128.7683e-131.5131e-102.2181e-115.1444e-121.0540e-120.56PowerLaw2.67830.41960.16960.32822.85880.26851.0582407e-10 .. 1.9819723e-16-1.7538379e-11 .. 4.823511e-123.278615e-12 .. 1.8668298e-1615.209969 .. 0.050.9568.1520.975913FGL J0008.0+4713NbllMG4 J000800+47121.00000.98730.28002511884200000000.0
3FHL J0008.4-23392.1243-23.651450.2908-79.70210.03660.0366--5179.67916.963.0610e-127.3475e-137.4602e-111.7896e-112.4733e-128.1716e-130.34PowerLaw2.73880.71450.17370.56182.90700.45205.804992e-11 .. 1.1117311e-20-1.4419374e-11 .. 6.10661e-121.7951775e-12 .. 1.0403958e-209.133706 .. 0.019.8371.1220.996813FGL J0008.6-2340NbllRBS 00160.99960.96730.1470524807800000000.0
3FHL J0009.1+06282.28746.4814104.4637-54.86690.03850.0385--4026.28218.921.2691e-124.3696e-134.1597e-111.4317e-111.6903e-128.9372e-130.10PowerLaw2.55290.83630.01220.44772.58000.53912.4161059e-11 .. 6.6482124e-19-9.546595e-12 .. 6.287476e-127.566492e-13 .. 6.5095056e-194.678369 .. 0.010.9512.2560.972113FGL J0009.1+0630NbllCRATES J000903.95+062821.50.99930.9878--663742400000000.0
3FHL J0009.4+50302.350450.5049116.1257-11.81050.01760.0176--30222.40217.049.8252e-121.3192e-122.2191e-102.6212e-118.7336e-121.2488e-123.15LogParabola1.43050.35050.79650.30722.36100.16111.16274e-10 .. 9.252794e-17-1.8225135e-11 .. 4.417993e-123.8564165e-12 .. 7.0436765e-1715.780677 .. 0.078.5072.7620.995023FGL J0009.3+5030CbllNVSS J000922+5030281.00000.9698--1412536400000000.0
3FHL J0009.7-43192.4450-43.3195327.7563-71.74450.04960.0496--4294.95716.771.0257e-124.2463e-132.4189e-111.0008e-117.8730e-134.3643e-131.00PowerLaw2.20440.96400.99940.06092.94420.76551.4262526e-11 .. 1.124737e-17-6.5860017e-12 .. 5.5998656e-124.4044038e-13 .. 1.04738585e-174.1469994 .. 0.07.0026.0100.99121NbllSUMSS J000949-4316540.99850.9072----
3FHL J0013.0-39563.2523-39.9381332.3835-74.91500.05170.0517--4297.20114.273.5994e-121.0357e-124.9878e-111.4376e-111.2946e-124.5911e-130.99PowerLaw3.01300.94920.99780.11283.60660.78664.1576055e-11 .. 4.364096e-18-1.1673498e-11 .. 5.6003054e-121.2527341e-12 .. 3.750808e-187.4368286 .. 0.014.1920.6270.983713FGL J0013.2-3954NbllPKS 0010-4010.99980.9962--1862089000000.0
3FHL J0013.8-18553.4684-18.916974.5314-78.08760.04850.0485--1525.27218.949.4557e-133.6339e-133.1086e-111.1945e-111.2427e-127.0538e-131.06PowerLaw1.94380.91640.91350.82882.60200.58391.8761324e-11 .. 2.4010398e-18-8.03048e-12 .. 6.2478846e-125.8704425e-13 .. 2.343576e-184.4304204 .. 0.08.1937.2170.986113FGL J0013.9-1853PSHBL J001355.9-185406bllRBS 00300.99950.97720.09505.260181e+16
3FHL J0014.0-50243.5038-50.4003317.4913-65.65680.05970.0597--2394.86922.745.9817e-132.2478e-133.0263e-111.1370e-111.7404e-121.0928e-121.45PowerLaw1.49780.74880.99640.17762.20860.44991.0878415e-11 .. 1.7599409e-17-5.7917993e-12 .. 5.8792246e-123.4567488e-13 .. 1.8203253e-173.097385 .. 0.09.2326.0540.992713FGL J0014.0-5025NbllRBS 00320.99980.8923--8.413936e+16
3FHL J0014.7+58013.695158.0244118.0788-4.50150.02660.0266--5188.21525.558.7309e-132.1415e-135.6736e-111.3982e-113.5097e-121.3787e-120.51PowerLaw2.00130.44430.12590.26312.14590.27683.2196253e-11 .. 6.0640007e-18-9.896842e-12 .. 4.293739e-121.0256123e-12 .. 6.3322123e-186.1053777 .. 0.022.02160.8170.994313FGL J0014.7+5802Nbll1RXS J001442.2+5802011.00000.9778--4.3651522e+16
3FHL J0014.9+61183.736661.3025118.5645-1.25940.03390.0339--5189.09115.864.2551e-128.4587e-138.4783e-111.6849e-112.5742e-126.9789e-130.00PowerLaw3.09100.54820.00890.41143.10100.44166.525263e-11 .. 2.2172363e-18-1.3828337e-11 .. 4.048499e-122.0031396e-12 .. 2.0232124e-188.736899 .. 0.033.6834.4070.982213FGL J0014.6+6119Nbcu4C +60.010.99940.9996--12971790000000.0
3FHL J0015.7+55513.933755.8649117.9031-6.65790.03350.0335--3028.11231.185.5030e-131.3879e-135.2164e-111.3207e-114.5355e-121.9094e-120.74PowerLaw1.69690.37260.15310.22181.88980.24802.4335116e-11 .. 1.9521596e-17-8.442904e-12 .. 4.4789077e-127.8311885e-13 .. 2.1210226e-175.2631125 .. 0.019.71167.5780.982013FGL J0015.7+5552CbllGB6 J0015+55511.00000.9832--6180169000000000.0
3FHL J0018.6+29464.652529.7821114.4798-32.55030.03120.0312--5227.91729.206.0010e-131.7106e-135.0577e-111.4495e-113.8639e-121.7982e-120.92PowerLaw1.68020.49010.26390.32701.98160.29122.3603335e-11 .. 0.0-9.441283e-12 .. 5.7002103e-127.568024e-13 .. 0.04.5989604 .. 0.015.13127.3170.998813FGL J0018.4+2947NbllRBS 00420.99850.97110.10005.9156075e+16
3FHL J0019.1-81514.7762-81.8621304.3273-35.17830.04390.0439--3827.50416.952.3874e-126.0485e-135.8043e-111.4696e-111.9468e-126.8861e-130.68PowerLaw2.57370.76940.39010.73782.88440.47173.5834426e-11 .. 3.3873322e-25-1.0997976e-11 .. 4.837986e-121.1091266e-12 .. 3.1795783e-256.12071 .. 0.019.3457.5250.899013FGL J0018.9-8152NbllPMN J0019-81520.99980.9878--1393156900000000.0
....................................................................................................................................
3FHL J2338.9+2123354.745021.3966101.2522-38.40570.02840.0284--1857.24723.339.0048e-132.7961e-134.8313e-111.5041e-112.6473e-121.3074e-120.82PowerLaw1.90930.66660.33580.49782.25220.37672.564995e-11 .. 9.101627e-21-9.6132095e-12 .. 5.884382e-128.136546e-13 .. 9.351896e-215.4281335 .. 0.013.7275.6460.998313FGL J2339.0+2122NbllRX J2338.8+21240.99980.97690.2910524807800000000.0
3FHL J2339.2-7404354.8159-74.0796309.5087-42.11440.03320.0332--558.66428.076.2505e-131.6352e-134.8749e-111.2787e-113.6711e-121.6064e-121.52PowerLaw1.58060.46500.46610.37981.99210.27312.6423676e-11 .. 6.5882064e-17-8.613914e-12 .. 4.8000375e-128.468765e-13 .. 7.044718e-176.531144 .. 0.017.02126.7700.960013FGL J2338.7-7401Nbcu1RXS J233919.8-7404390.99990.9847--1.56675e+16
3FHL J2339.5-0533354.8927-5.557781.3085-62.47020.04480.0448--349.45112.471.0697e-112.5392e-128.0465e-111.9050e-111.6710e-124.1909e-131.03PowerLaw5.98091.1763-0.75430.37215.35481.07017.264754e-11 .. 5.1358784e-16-1.6699227e-11 .. 7.735961e-122.067454e-12 .. 3.8239114e-169.646015 .. 0.020.4839.6660.971413FGL J2339.6-0533NPSRPSR J2339-05331.00000.0000----
3FHL J2340.8+8015355.224480.2593119.845717.81290.01670.0167--27723.25221.005.1759e-125.6611e-132.1901e-102.3964e-111.0479e-111.8366e-122.73PowerLaw1.92850.26000.57340.25952.38640.15031.2925577e-10 .. 2.6074902e-17-1.7942073e-11 .. 3.828835e-124.0786077e-12 .. 2.6260199e-1717.065054 .. 0.094.02158.1820.950913FGL J2340.7+8016Cbll1RXS J234051.4+8015131.00000.99700.27403427676800000000.0
3FHL J2343.6+3439355.901234.6513107.4255-26.16960.02860.0286--4848.93724.361.1204e-122.8008e-136.5701e-111.6429e-114.1626e-121.7981e-121.13PowerLaw1.79020.49060.36000.40142.12610.29592.8448787e-11 .. 5.3683953e-21-1.00504665e-11 .. 5.499488e-129.069466e-13 .. 5.622843e-215.1358323 .. 0.020.0972.6600.999413FGL J2343.7+3437Cbll1RXS J234332.5+3439570.99980.98190.36601029200060000000.0
3FHL J2345.1-1554356.2999-15.915165.6780-70.98130.01660.0166--45028.82217.041.7869e-111.7238e-124.4142e-104.2557e-111.4800e-111.9974e-122.07PowerLaw2.45930.30780.62220.36332.88520.18263.1803404e-10 .. 5.8906217e-21-3.5389455e-11 .. 6.2676912e-129.843321e-12 .. 5.528767e-2124.107079 .. 0.0114.6269.5710.958533FGL J2345.2-1554NFSRQPMN J2345-15550.99990.99780.621020535250000000.0
3FHL J2346.6+0705356.67267.084495.9910-52.36760.02320.0232--40213.66725.332.0843e-123.7923e-131.3252e-102.4135e-118.7692e-122.7196e-122.28PowerLaw1.55680.38050.61540.34482.09100.20715.3996665e-11 .. 1.5570329e-16-1.4772433e-11 .. 6.39344e-121.7238113e-12 .. 1.6396604e-167.4013906 .. 0.035.1374.6360.996713FGL J2346.7+0705CbllTXS 2344+0680.99990.99710.1720568853050000000.0
3FHL J2347.0+5142356.768851.7008112.8895-9.91160.01290.0129--24829.45031.393.4657e-123.2859e-133.3165e-103.1490e-113.0741e-114.9987e-120.60PowerLaw1.78690.13400.04540.07301.84630.09251.4582405e-10 .. 5.3311014e-12-2.0452615e-11 .. 5.501045e-124.700856e-12 .. 5.8319695e-1217.46963 .. 2.4212334120.69652.3120.962913FGL J2347.0+5142P1ES 2344+514bll1ES 2344+5141.00000.99850.04407079464000000000.0
3FHL J2347.9+5435356.980554.5974113.7400-7.13680.02760.0276--24810.94222.661.4071e-122.9176e-137.0715e-111.4671e-113.8764e-121.3330e-121.11PowerLaw1.93290.43280.32930.34092.25190.26154.5147826e-11 .. 2.1944359e-17-1.1016588e-11 .. 4.4637232e-121.4321772e-12 .. 2.2549025e-178.825034 .. 0.026.27116.4190.897713FGL J2347.9+5436NbcuNVSS J234753+5436270.00000.9766--2.5118841e+16
3FHL J2347.9-1630356.9978-16.510665.5355-71.87660.02880.0288--4509.29716.283.1279e-128.0896e-136.7585e-111.7478e-112.0267e-126.5608e-130.07PowerLaw3.12590.77810.01040.57563.13240.52595.2519888e-11 .. 1.0747592e-20-1.4230782e-11 .. 6.211813e-121.6103665e-12 .. 9.768252e-218.333468 .. 0.017.5550.2150.986933FGL J2348.0-1630NfsrqPKS 2345-160.99940.99990.57609332549000000.0
3FHL J2350.5-3006357.6354-30.107016.7759-76.31940.04910.0491--706.49721.201.0879e-123.2997e-134.7039e-111.4274e-112.2909e-121.1390e-120.63PowerLaw2.10120.61730.28800.48702.36780.42342.1939225e-11 .. 4.5892933e-16-8.926376e-12 .. 6.097474e-126.927891e-13 .. 4.63469e-164.0536985 .. 0.012.8449.2860.964413FGL J2350.4-3004NbllNVSS J235034-3006030.99980.92180.22373981075200000000.0
3FHL J2351.5-7559357.8926-75.9890307.6546-40.58550.06500.0650--556.06726.824.9826e-131.6350e-133.5689e-111.1769e-112.3897e-121.2622e-120.61PowerLaw1.84740.58020.20030.36612.08160.35322.3730832e-11 .. 6.9375605e-17-8.570627e-12 .. 4.7928705e-127.578736e-13 .. 7.316245e-175.2754674 .. 0.012.41134.7210.989213FGL J2351.9-7601NbllSUMSS J235115-7600120.00000.9625----
3FHL J2352.1+1753358.041517.8865103.5764-42.74660.08380.0838--1854.11716.979.9227e-134.3475e-132.4254e-111.0640e-117.6327e-134.2356e-130.02PowerLaw3.01751.21640.01000.85243.01660.82701.5997077e-11 .. 2.9107688e-20-7.581037e-12 .. 5.821708e-124.926488e-13 .. 2.6849966e-203.5496242 .. 0.06.7343.1070.966813FGL J2352.0+1752NbllCLASS J2352+17490.99260.0000--1737799900000000.0
3FHL J2356.2+4035359.074640.5985111.7521-21.07320.02980.0298--3127.62529.015.2427e-131.5104e-134.3400e-111.2511e-113.6677e-121.8547e-120.35PowerLaw2.02330.4242-0.07060.19261.90950.29752.5777725e-11 .. 3.110794e-16-8.514681e-12 .. 5.4134618e-128.2889175e-13 .. 3.3694582e-166.2127647 .. 0.013.81417.8610.911913FGL J2356.0+4037NbllNVSS J235612+4036480.99980.91990.13106309576500000000.0
3FHL J2357.4-1717359.3690-17.299668.4009-74.12850.03270.0327--4506.96129.525.4394e-131.7370e-134.6654e-111.4945e-113.7598e-121.9583e-121.11PowerLaw1.57620.51870.35130.37711.94300.31161.9003682e-11 .. 2.714288e-20-8.131149e-12 .. 6.4742196e-126.1025685e-13 .. 2.92465e-204.552822 .. 0.012.30146.7570.983813FGL J2357.4-1716NbllRBS 20660.99990.9631--8.912525e+16
3FHL J2358.4-1808359.6205-18.140866.5520-74.85010.05110.0511--4506.49318.231.6335e-124.9686e-134.8680e-111.4811e-111.7825e-127.6480e-131.83PowerLaw2.05320.66730.99990.01342.73120.50242.6735683e-11 .. 6.0349635e-21-9.960717e-12 .. 6.2551535e-128.323882e-13 .. 5.7844478e-214.3616037 .. 0.012.7428.3040.984513FGL J2358.6-1809N0.00000.0000----
3FHL J2358.5+3829359.626638.4963111.6905-23.21730.05840.0584--3125.79718.241.4104e-124.4534e-134.2106e-111.3321e-111.7404e-129.8271e-130.44PowerLaw2.74660.6917-0.13290.30132.55760.57812.824428e-11 .. 9.750687e-17-9.458818e-12 .. 5.2791343e-128.852925e-13 .. 9.5778846e-175.7128677 .. 0.013.1357.3010.978213FGL J2358.5+3827NbcuB3 2355+3820.00000.9254----
3FHL J2359.1-3038359.7760-30.639712.7909-78.02680.02310.0231--7011.55121.211.8903e-124.1965e-138.1774e-111.8149e-114.2849e-121.6806e-120.08PowerLaw2.28650.46320.01010.24342.29440.30925.5015617e-11 .. 6.037456e-17-1.3604539e-11 .. 8.488618e-121.7422797e-12 .. 6.164239e-179.39347 .. 0.022.41111.3660.960713FGL J2359.3-3038PH 2356-309bllH 2356-3090.99990.99750.16502.818388e+17
3FHL J2359.3-2049359.8293-20.825658.0522-76.54110.07220.0722--5804.63819.029.1911e-133.6043e-133.0559e-111.1979e-111.2593e-127.4704e-130.32PowerLaw2.34020.94450.18510.66002.56150.58382.3253791e-11 .. 8.3778735e-21-8.939083e-12 .. 6.2386546e-127.2875863e-13 .. 8.224765e-214.8207045 .. 0.08.0664.1770.985913FGL J2359.5-2052NbllTXS 2356-2100.98940.99060.09604073799600000000.0

This looks very familiar again. The data is just stored as an astropy.table.Table object. We have all the methods and attributes of the Table object available. E.g. we can sort the underlying table by Signif_Avg to find the top 5 most significant sources:

[25]:
# sort table by significance
fermi_3fhl.table.sort("Signif_Avg")

# invert the order to find the highest values and take the top 5
top_five_TS_3fhl = fermi_3fhl.table[::-1][:5]

# print the top five significant sources with association and source class
top_five_TS_3fhl[["Source_Name", "ASSOC1", "ASSOC2", "CLASS", "Signif_Avg"]]
[25]:
Table length=5
Source_NameASSOC1ASSOC2CLASSSignif_Avg
bytes18bytes26bytes26bytes7float32
3FHL J0534.5+2201Crab NebulaPWN168.641
3FHL J1104.4+3812Mkn 421BLL144.406
3FHL J0835.3-4510PSR J0835-4510Vela X fieldPSR138.801
3FHL J0633.9+1746PSR J0633+1746PSR99.734
3FHL J1555.7+1111PG 1553+113BLL94.411

If you are interested in the data of an individual source you can access the information from catalog using the name of the source or any alias source name that is defined in the catalog:

[26]:
mkn_421_3fhl = fermi_3fhl["3FHL J1104.4+3812"]

# or use any alias source name that is defined in the catalog
mkn_421_3fhl = fermi_3fhl["Mkn 421"]
print(mkn_421_3fhl.data["Signif_Avg"])
144.40611

Exercises

  • Try to load the Fermi-LAT 2FHL catalog and check the total number of sources it contains.

  • Select all the sources from the 2FHL catalog which are contained in the Galactic Center region. The methods gammapy.maps.WcsGeom.contains() and gammapy.catalog.SourceCatalog.positions might be helpful for this. Add markers for all these sources and try to add labels with the source names. The function ax.text() might be also helpful.

  • Try to find the source class of the object at position ra=68.6803, dec=9.3331

[ ]:

Spectral models and flux points

In the previous section we learned how access basic data from individual sources in the catalog. Now we will go one step further and explore the full spectral information of sources. We will learn how to:

  • Plot spectral models

  • Compute integral and energy fluxes

  • Read and plot flux points

As a first example we will start with the Crab Nebula:

[27]:
crab_3fhl = fermi_3fhl["Crab Nebula"]
crab_3fhl_spec = crab_3fhl.spectral_model()
print(crab_3fhl_spec)
PowerLawSpectralModel

  type      name     value         unit        error   min max frozen link
-------- --------- ---------- -------------- --------- --- --- ------ ----
spectral     index 2.2202e+00                2.498e-02 nan nan  False
spectral amplitude 1.7132e-10 cm-2 GeV-1 s-1 3.389e-12 nan nan  False
spectral reference 2.2726e+01            GeV 0.000e+00 nan nan   True

The crab_3fhl_spec is an instance of the gammapy.modeling.models.PowerLaw2SpectralModel model, with the parameter values and errors taken from the 3FHL catalog.

Let’s plot the spectral model in the energy range between 10 GeV and 2000 GeV:

[28]:
ax_crab_3fhl = crab_3fhl_spec.plot(
    energy_bounds=[10, 2000] * u.GeV, energy_power=0
)
../../_images/tutorials_starting_overview_59_0.png

We assign the return axes object to variable called ax_crab_3fhl, because we will re-use it later to plot the flux points on top.

To compute the differential flux at 100 GeV we can simply call the model like normal Python function and convert to the desired units:

[29]:
crab_3fhl_spec(100 * u.GeV).to("cm-2 s-1 GeV-1")
[29]:
$$6.3848913 \times 10^{-12} \; \mathrm{\frac{1}{GeV\,s\,cm^{2}}}$$

Next we can compute the integral flux of the Crab between 10 GeV and 2000 GeV:

[30]:
crab_3fhl_spec.integral(energy_min=10 * u.GeV, energy_max=2000 * u.GeV).to(
    "cm-2 s-1"
)
[30]:
$$8.6745734 \times 10^{-9} \; \mathrm{\frac{1}{s\,cm^{2}}}$$

We can easily convince ourself, that it corresponds to the value given in the Fermi-LAT 3FHL catalog:

[31]:
crab_3fhl.data["Flux"]
[31]:
$$8.6589091 \times 10^{-9} \; \mathrm{\frac{1}{s\,cm^{2}}}$$

In addition we can compute the energy flux between 10 GeV and 2000 GeV:

[32]:
crab_3fhl_spec.energy_flux(energy_min=10 * u.GeV, energy_max=2000 * u.GeV).to(
    "erg cm-2 s-1"
)
[32]:
$$5.3114892 \times 10^{-10} \; \mathrm{\frac{erg}{s\,cm^{2}}}$$

Next we will access the flux points data of the Crab:

[33]:
print(crab_3fhl.flux_points)
FluxPoints
----------

  geom                   : RegionGeom
  axes                   : ['lon', 'lat', 'energy']
  shape                  : (1, 1, 5)
  quantities             : ['norm', 'norm_errp', 'norm_errn', 'norm_ul', 'sqrt_ts', 'is_ul']
  ref. model             : pl
  n_sigma                : 1
  n_sigma_ul             : 2
  sqrt_ts_threshold_ul   : 1
  sed type init          : flux

If you want to learn more about the different flux point formats you can read the specification here.

No we can check again the underlying astropy data structure by accessing the .table attribute:

[34]:
crab_3fhl.flux_points.to_table(sed_type="dnde", formatted=True)
[34]:
Table length=5
e_refe_mine_maxdndednde_errpdnde_errndnde_ulsqrt_tsis_ul
GeVGeVGeV1 / (cm2 GeV s)1 / (cm2 GeV s)1 / (cm2 GeV s)1 / (cm2 GeV s)
float64float64float64float64float64float64float64float32bool
14.14210.00020.0005.120e-101.321e-111.321e-11nan125.157False
31.62320.00050.0007.359e-112.842e-122.842e-12nan88.715False
86.60350.000150.0009.024e-125.367e-135.367e-13nan59.087False
273.861150.000500.0007.660e-138.707e-148.097e-14nan33.076False
1000.000500.0002000.0004.291e-141.086e-149.393e-15nan15.573False

Finally let’s combine spectral model and flux points in a single plot and scale with energy_power=2 to obtain the spectral energy distribution:

[35]:
ax = crab_3fhl_spec.plot(energy_bounds=[10, 2000] * u.GeV, energy_power=2)
crab_3fhl.flux_points.plot(ax=ax, sed_type="dnde", energy_power=2);
../../_images/tutorials_starting_overview_73_0.png

Exercises

  • Plot the spectral model and flux points for PKS 2155-304 for the 3FGL and 2FHL catalogs. Try to plot the error of the model (aka “Butterfly”) as well. Note this requires the uncertainties package to be installed on your machine.

[ ]:

What next?

This was a quick introduction to some of the high level classes in Astropy and Gammapy.

  • To learn more about those classes, go to the API docs (links are in the introduction at the top).

  • To learn more about other parts of Gammapy (e.g. Fermi-LAT and TeV data analysis), check out the other tutorial notebooks.

  • To see what’s available in Gammapy, browse the Gammapy docs or use the full-text search.

  • If you have any questions, ask on the mailing list.