def get_psf_en_univariatespline(PSF_FILE, show=False): """ Gives the 95% containment angle as a function of energy. PSF_FILE: str PSF file produced with 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') _psf_e = [] for j, en in enumerate(_en): psf_ph_spline = xInterpolatedUnivariateSplineLinear(_th, _psf[j]) tot_integral = psf_ph_spline.integral(_th[0], _th[-1]) k = 1 while psf_ph_spline.integral(_th[0], _th[k]) < 0.95 * tot_integral: k = k + 1 _psf_e.append(np.degrees(_th[k])) _psf_e = np.array(_psf_e) psf_e_spline = xInterpolatedUnivariateSplineLinear(_en, _psf_e) if show == True: psf_e_spline.plot(show=False) plt.xscale('log') plt.show() return psf_e_spline
def get_2params_profile_likelihood(lh_matrix, param1_list, param2_list): """ Returns splines with profile likelihood for the two parameters of the fit. NOTE: param1 is supposed to be the normalization, param2 the constant. """ n_lh = np.amin(lh_matrix, axis=1) c_lh = np.amin(lh_matrix, axis=0) fmt1 = dict(xname='N', xunits='', yname='Likelihood', yunits='') spline1 = xInterpolatedUnivariateSplineLinear(param1_list, n_lh, **fmt1) fmt2 = dict(xname='C', xunits='', yname='Likelihood', yunits='') spline2 = xInterpolatedUnivariateSplineLinear(param2_list, c_lh, **fmt2) return spline1, spline2
def build_wbeam(psf, l_, out_file): """ Calculates the Wbeam(l, E) and return a bivariate slpine. Parameters ---------- psf : spline psf bivariate spline generated by get_psf l_ : array array of multipoles at wicht to compute the Wbeam out_file : str name of the output txt file that will be created in output/ Returns ------- spline spline of the beam window function """ out_txt = open(os.path.join(X_OUT, 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)))) logger.info('Wbeam(%i, %.2f) = %e'%(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 _wb_
def build_wpix(nside, l_max=1500): """ Calculate the pixel window function. Parameters ---------- nside : int nside of the map you are analyzing l_max : int the maximum multipol at wich to calculate the pixel window function Returns ------- spline spline of the pixel window function """ wpix = hp.sphtfunc.pixwin(nside)[:l_max] fmt = dict(xname='$l$', xunits='', yname='W_{pixel}', yunits='') wpix_spline = xInterpolatedUnivariateSplineLinear(np.arange(l_max), wpix, **fmt) return wpix_spline
def get_1D_wbeam(wb_file, spectrum_spline, e_min, e_max): """ Returns the integral Wbeam as a function of the multiple in a certain energy interval. Parameters ---------- wb_file : str .txt file generated by build_wbeam spectrum_spline : numpy spline spline of the spectrum returned by get_powerlaw_spline e_min : float lower limit of the energy interval e_max : float upper limit of the energy interval Returns ------- spline spline Wbeam(l) resulting from Wbeam(E, l) integrated between Emin and Emax """ en_, l_, _z_ = wbeam_parse(wb_file) int_spec = spectrum_spline.integral(e_min, e_max) fmt = dict(xname='$l$', xunits='', yname='Energy', yunits='MeV', zname='W$_{beam}$(E,$l$)') int_wbeam = xInterpolatedBivariateSplineLinear( l_, en_, _z_*spectrum_spline(en_), **fmt) int_wbeam_l = [] for l in l_: l_slice = int_wbeam.vslice(l) int_wbeam_l.append(l_slice.integral(e_min, e_max)) int_wbeam_l = np.array(int_wbeam_l) fmt2 = dict(xname='$l$', xunits='', yname='integral W$_{beam}$ ($%.1f - %.1f$)'%(e_min, e_max), yunits='') wbeam = xInterpolatedUnivariateSplineLinear(l_, int_wbeam_l/int_spec, **fmt2) norm = 1/wbeam.y[0] return wbeam
def get_powerlaw_spline(index=2.3): """ Returns a simple power law spline with a given spectral index. Parameters ---------- index : float spectral index of the powerlaw you want to generate Returns ------- spline spline of the energy spectrum (power law with index = -gamma) """ EMIN = 2 EMAX = 6 en_ = np.logspace(EMIN, EMAX, 2000) pl_ = en_**(-index) fmt = dict(xname='$E$', xunits='MeV', yname='E$^{-%.1f}$'%index, yunits='') spectrum = xInterpolatedUnivariateSplineLinear(en_, pl_, **fmt) return spectrum