cr_flux_from_exposure.py

minimal python script to generate count cubes and further calculations - Eckner Christopher, 02/19/2020 03:10 PM

Download (2.12 KB)

 
1
import numpy as np
2
from astropy.io import fits
3
from scipy.interpolate import interp1d
4
import matplotlib.pyplot as plt
5
import gammalib
6
import ctools
7
import cscripts
8

    
9
sim_cr = ctools.ctmodel()
10
sim_cr['inobs'] = 'obs_CTA_GC_survey_ROI_5deg.xml'
11
sim_cr['emin'] = 0.03
12
sim_cr['emax'] = 100.0
13
sim_cr['inmodel'] = 'cr.xml'
14
sim_cr['incube'] = 'bin_generic_GC_survey_ROI_12x12deg_100EBINS_0p01deg.fits.gz'
15
sim_cr['outcube'] = 'ctmodel_CR_GC_survey_ROI_12x12deg_100Ebins_0p01deg.fits.gz'
16
sim_cr.execute()
17

    
18
sim_expcube = ctools.ctexpcube()
19
sim_expcube['inobs'] = 'obs_CTA_GC_survey_ROI_5deg.xml'
20
sim_expcube['emin'] = 0.03
21
sim_expcube['emax'] = 100.0
22
sim_expcube['incube'] = 'bin_generic_GC_survey_ROI_12x12deg_100EBINS_0p01deg.fits.gz'
23
sim_expcube['outcube'] = 'ctexpcube_GC_survey_ROI_12x12deg_100Ebins_0p01deg.fits.gz'
24
sim_expcube.execute()
25

    
26
expcube = fits.open('ctexpcube_GC_survey_ROI_12x12deg_100Ebins_0p01deg.fits.gz')
27

    
28
map_expcube = np.array(expcube[0].data).astype('float')
29
E_expcube = np.array(expcube[1].data).astype('float')
30
f_expsoure = interp1d(E_expcube*1e-03, map_expcube, axis = 0)
31

    
32
cr = fits.open('ctmodel_CR_GC_survey_ROI_12x12deg_100Ebins_0p01deg.fits.gz')
33
map_cr = np.array(cr[0].data).astype('float') 
34
E_cr = np.array([np.sqrt(cr[2].data[i][0] * cr[2].data[i][1]) * 1e-06 for i in range(len(cr[2].data))]) ###calculate residual cosmic-ray flux at an energy bin's central energy (w.r.t. logarithmic-spacing)
35

    
36
all_cr_fluxes = []
37
for i, E in enumerate(E_cr):
38
    exposure_shape = np.array([1.0 if x != 0. else 0. for x in f_expsoure(E).flatten()]) ### Both maps might have zeros at different positions, I try to lift them on the same footing by imposing the structure of the exposure map on the CR background map
39
    cr_flux = map_cr[i].flatten() * exposure_shape / np.array([x if x != 0. else 1.0 for x in f_expsoure(E).flatten()])  ### To avoid divisony by zero, since both maps have their zeros at the same position.s
40
    cr_flux = cr_flux.sum()
41
    all_cr_fluxes.append(cr_flux)
42
    
43
plt.plot(E_cr, all_cr_fluxes)
44
plt.yscale('log')
45
plt.xscale('log')
46
plt.xlabel('Energy [GeV]')
47
plt.ylabel('Residual cosmic-ray flux [arbitrary units]')
48
plt.show()
49
    
50
    
51