cr_flux_from_exposure.py

Tibaldo Luigi, 02/26/2020 04:56 PM

Download (4.11 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
# print('done with cube computation')
27

    
28
#### Christopher's way
29
# expcube = fits.open('ctexpcube_GC_survey_ROI_12x12deg_100Ebins_0p01deg.fits.gz')
30
#
31
# map_expcube = np.array(expcube[0].data).astype('float')
32
# E_expcube = np.array(expcube[1].data).astype('float')
33
# f_expsoure = interp1d(E_expcube*1e-03, map_expcube, axis = 0)
34
#
35
# cr = fits.open('ctmodel_CR_GC_survey_ROI_12x12deg_100Ebins_0p01deg.fits.gz')
36
# map_cr = np.array(cr[0].data).astype('float')
37
# 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)
38
#
39
# all_cr_fluxes = []
40
# for i, E in enumerate(E_cr):
41
#     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
42
#     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
43
#     cr_flux = cr_flux.sum()
44
#     all_cr_fluxes.append(cr_flux)
45

    
46
#### my way
47

    
48
# extract data from FITS
49
map_exp = fits.getdata('ctexpcube_GC_survey_ROI_12x12deg_100Ebins_0p01deg.fits.gz',0)
50
E_exp = np.array(fits.getdata('ctexpcube_GC_survey_ROI_12x12deg_100Ebins_0p01deg.fits.gz',1))['Energy']
51
map_cr =fits.getdata('ctmodel_CR_GC_survey_ROI_12x12deg_100Ebins_0p01deg.fits.gz',0)
52

    
53
# convert energies from keV to GeV
54
E_exp /= 1.e6
55

    
56
# exposure is calculated on bin edges, determine bin centers using geometric mean
57
E_cr = np.sqrt(E_exp[1:] * E_exp[:-1])
58

    
59
# set mask, all bins where exposure at lower edge is 0
60
mask = np.ones([np.shape(map_exp)[0] - 1,np.shape(map_exp)[1],np.shape(map_exp)[2]])
61
mask[map_exp[:-1,:,:] == 0.] = 0.
62

    
63
#### all bins
64
# # derive exposure at bin center as PL interp of edges values
65
# # index
66
# idx = (np.log(map_exp[1:]) - np.log(map_exp[:-1])) / (np.log(E_exp[1:,np.newaxis,np.newaxis]) - np.log(E_exp[:-1,np.newaxis,np.newaxis]))
67
# # set NaNs to 0, this occurs for bins with exposure = 0 that are going to be taken care of later
68
# idx[np.isnan(idx)] = 0.
69
# # exposure at bin center
70
# exp = map_exp[:-1] * np.power(E_cr[:,np.newaxis,np.newaxis]/E_exp[:-1,np.newaxis,np.newaxis], idx)
71
#
72
# # divide cr counts by exposure to get "flux"
73
# cr_fluxes = map_cr / exp
74
#
75
# # # apply mask to flux map
76
# cr_fluxes = cr_fluxes * mask
77
#
78
# # sum over spatial bins
79
# all_cr_fluxes = np.nansum(cr_fluxes,axis=(1,2))
80

    
81
#### just central bin
82
ipix = 600
83
# derive exposure at bin center as PL interp of edges values
84
# index
85
idx = (np.log(map_exp[1:,ipix,ipix]) - np.log(map_exp[:-1,ipix,ipix])) / (np.log(E_exp[1:]) - np.log(E_exp[:-1]))
86
# set NaNs to 0, this occurs for bins with exposure = 0 that are going to be taken care of later
87
idx[np.isnan(idx)] = 0.
88
# exposure at bin center
89
exp = map_exp[:-1,ipix,ipix] * np.power(E_cr/E_exp[:-1], idx)
90

    
91
# divide cr counts by exposure to get "flux"
92
cr_fluxes = map_cr[:,ipix,ipix] / exp
93

    
94
all_cr_fluxes = cr_fluxes
95

    
96
# plot
97
plt.plot(E_cr, all_cr_fluxes)
98
plt.yscale('log')
99
plt.xscale('log')
100
plt.xlabel('Energy [GeV]')
101
plt.ylabel('Residual cosmic-ray flux [arbitrary units]')
102
plt.savefig('bin_600.png')
103
    
104
    
105