def get_iso_integral_flux_map(iso_ascii_file, e_min, e_max, nside=512): """Returns a isotropic map of the integral IGRB between emin and emax from 'iso_P8R2_SOURCE_V6_v06.txt' file. iso_ascii_file: str Ascii file found here https://fermi.gsfc.nasa.gov/ssc/data/access/lat/BackgroundModels.html emin: float minimum energy of the bin [MeV] emax: float maximum energy of the bin [MeV] nside: int healpix nside parameter """ isofile = os.path.join(GRATOOLS_CONFIG, 'models', 'iso_P8R2_ULTRACLEANVETO_V6_v06.txt') from GRATools.utils.gFTools import iso_parse e, difflux, diffluxerr = iso_parse(iso_ascii_file) index = np.where((e>e_min)*(e<e_max)) erange = e[index] frange = difflux[index] f_e = xInterpolatedUnivariateSplineLinear(erange, frange) intf = f_e.integral(e_min, e_max) logger.info('Isotropic Bkg %e [cm-2s-1]'%intf) npix = hp.nside2npix(nside) intiso_map = np.full(npix, intf) return intiso_map
def get_crbkg(txt_file): """Get the CR residual bkg (spline) as a function of the energy from the txt files. txt_file : str It must contain 2 columns: the first one with the energy, the second one with the cosmic ray residual background flux. """ logger.info('Getting CR residual bkg from file %s'%txt_file) f = open(txt_file, 'r') _bkg, _en = [], [] for line in f: try: e, bkg = [float(item) for item in line.split()] _en.append(e) _bkg.append(bkg) except: pass fmt = dict(xname='Energy', xunits='MeV', yname='E$^{2}$ x CR Residual flux', yunits='MeV cm$^{-2}$ s$^{-1}$ sr$^{-1}$') crbkg = xInterpolatedUnivariateSplineLinear(np.array(_en), np.array(_bkg),\ **fmt) f.close() return crbkg
def get_cl_model_SFG_spline(model_file, log=True): """ Returns a spline of the model with which to fit the APS. model_file : str ascii file with 2 columns: the first with multiples and the second for Cl. log : bool if True (default) the interpolation of the spline is done in logarithmic space. """ f = open(model_file, 'r') _l, _cl = [], [] for line in f: try: l, cl = [float(item) for item in line.split()] _l.append(l) _cl.append(cl) except: pass _l = np.array(_l) _cl = np.array(_cl) if log==True: model_spline = xInterpolatedUnivariateLogSpline(_l, _cl) else: model_spline = xInterpolatedUnivariateSplineLinear(_l, _cl) return model_spline
def get_psf(psf_file, show=False): """Get the PSF from the fits file created by gtpsf """ hdu_list = pf.open(psf_file) _th = np.radians(hdu_list['THETA'].data.field('Theta')) _en = hdu_list['PSF'].data.field('ENERGY') _psf = hdu_list['PSF'].data.field('PSF') fmt = dict(yname='Theta', yunits='rad', xname='Energy', xunits='MeV', zname='PSF') psf_th_e = xInterpolatedBivariateSplineLinear(_en, _th, _psf, **fmt) hdu_list.close() if show == True: for e in _en: psf_th = psf_th_e.vslice(e) fmt = dict(xname='cos(th)', xunits='', yname='psf[cos(th)]',\ yunits='') _psfxsin = xInterpolatedUnivariateSplineLinear(psf_th.x, \ psf_th.y*np.sin(psf_th.x), **fmt) plt.plot(np.degrees(psf_th.x), psf_th.y, label='%.2f'%e) print 'INT(E=%.2f) ='%e, 2*np.pi*_psfxsin.integral(0, \ np.amax(psf_th.x[:-2])) plt.yscale('log') plt.legend() plt.show() return psf_th_e
def build_wbeam(psf, _l, out_file): """Calculate the Wbeam(l, E) and return a bivariate slpine. """ out_txt = open(out_file, 'w') _en = psf.x _wb = [] energy = str(list(_en)).replace('[','').replace(']','').replace(', ', ' ') out_txt.write('l\t%s\n'%energy) for e in _en: wb_e = np.array([]) psf_th = psf.vslice(e) for l in _l: pl_th = get_pl_vs_th(l, np.cos(psf_th.x)) fmt = dict(xname='th', xunits='rad', yname='convolution', \ yunits='MeV') _conv = xInterpolatedUnivariateSplineLinear(psf_th.x, \ np.sin(psf_th.x)*psf_th.y*pl_th, **fmt) wb_e_l = min(1., 2*np.pi*(_conv.integral(np.amin(psf_th.x), \ np.amax(psf_th.x)))) print 'Wbeam(%i, %.2f)'%(l, e), wb_e_l wb_e = np.append(wb_e, [wb_e_l]) _wb.append(wb_e) _wb = np.array(_wb) fmt = dict(xname='$l$', xunits='', yname='Energy', yunits='MeV', zname='W$_{beam}$(E,$l$)') wbeam = xInterpolatedBivariateSplineLinear(_l, _en, _wb.T, **fmt) for i, l in enumerate(_l): wb = str(list(_wb.T[i])).replace('[','').replace(']','').\ replace(', ', ' ') out_txt.write('%i\t%s\n'%(l, wb)) out_txt.close() return wbeam
def get_ref_igrb_spline(): """Returns a spline of the IGRB measurement as a funcion of the energy. this curve is used as initial guess of the constant parameter of the poissonian fit. """ f = open(os.path.join(GRATOOLS_CONFIG,'ascii/IGRB_guess.txt'), 'r') x = np.array([float(l.split()[0]) for l in f]) f = open(os.path.join(GRATOOLS_CONFIG,'ascii/IGRB_guess.txt'), 'r') y = np.array([float(l.split()[1]) for l in f]) fmt = dict(xname='Energy' , xunits='MeV', yname='IGRB', yunits='MeV$^{-1}$/cm$^{-2}$/s') igrb = xInterpolatedUnivariateSplineLinear(x, y, **fmt) return igrb
def main(): """ crbkg_PSF1_file = os.path.join(GRATOOLS_CONFIG, 'ascii/p8r2_bkg_flux_t8.txt') crbkg_PSF1 = get_crbkg(crbkg_PSF1_file) crbkg_PSF1.plot(show=False) x = crbkg_PSF1.x xx = x**2 print x y1 = crbkg_PSF1.y crbkg_PSF2_file = os.path.join(GRATOOLS_CONFIG, 'ascii/p8r2_bkg_flux_t16.txt') crbkg_PSF2 = get_crbkg(crbkg_PSF2_file) crbkg_PSF2.plot(show=False) y2 = crbkg_PSF2(x) crbkg_PSF3_file = os.path.join(GRATOOLS_CONFIG, 'ascii/p8r2_bkg_flux_t32.txt') crbkg_PSF3 = get_crbkg(crbkg_PSF3_file) crbkg_PSF3.plot(show=False) y3 = crbkg_PSF2(x) y = (y1/x+y2/x+y3/x)*x print y plt.yscale('log') plt.xscale('log') plt.show() fore_files_list = [os.path.join(GRATOOLS_CONFIG, \ 'fits/gll_iem_v06_hp512_146647.fits'), os.path.join(GRATOOLS_CONFIG, \ 'fits/gll_iem_v06_hp512_200561.fits'), os.path.join(GRATOOLS_CONFIG, \ 'fits/gll_iem_v06_hp512_274296.fits'), os.path.join(GRATOOLS_CONFIG, \ 'fits/gll_iem_v06_hp512_375138.fits'), os.path.join(GRATOOLS_CONFIG, \ 'fits/gll_iem_v06_hp512_513056.fits'), ] e_min, e_max = 524807.00, 575439.00 get_foreground_integral_flux_map(fore_files_list, e_min, e_max)""" from scipy.optimize import curve_fit ClFORE_FILE = os.path.join(GRATOOLS_OUT, 'fore_polspicecls.txt') min1, emax1, clsfore = clfore_parse(ClFORE_FILE) l = np.arange(len(clsfore[0])) clsfore_spline = xInterpolatedUnivariateSplineLinear(l, clsfore[0]) clsfore_spline.plot(show=False) plt.xscale('log') plt.yscale('log') plt.show()
def get_psf_ref(psf_file): """get the published curve of the psf as a func of the energy """ f = open(psf_file, 'r') _e, _ang = [], [] for line in f: try: e, ang = [float(item) for item in line.split()] _e.append(e) _ang.append(ang) except: pass fmt = dict(xname='Energy', xunits='MeV', yname='Containment Angle', yunits='deg') psf = xInterpolatedUnivariateSplineLinear(np.array(_e), np.array(_ang),\ **fmt) f.close() return psf
def get_crbkg(txt_file): """Get the CR residual bkg (spline) as a function of the energy from the txt files """ logger.info('Getting CR residual bkg from file %s' % txt_file) f = open(txt_file, 'r') _bkg, _en = [], [] for line in f: try: e, bkg = [float(item) for item in line.split()] _en.append(e) _bkg.append(bkg) except: pass fmt = dict(xname='Energy', xunits='MeV', yname='E$^{2}$ x CR Residual flux', yunits='MeV cm$^{-2}$ s$^{-1}$ sr$^{-1}$') crbkg = xInterpolatedUnivariateSplineLinear(np.array(_en), np.array(_bkg),\ **fmt) f.close() return crbkg