Пример #1
0
def plot_sec_spec():
    particlePoints=1000
    Eg=np.logspace(7.,13.,100)/1.e9
    Ee=np.logspace(6.,15.,particlePoints)/1.e9
    parameter=[1.,9.,-2.5,10.]
    #Ne=spsh.bplcut(Ee,[49.4,-2.43,-2.7,1.69,5.])
    Ne=spsh.sbplcuty(Ee,[49.4,1.,-1.5,1.176,-1.4,5.])
    
    #Ne2=spsh.plcut()
    ylist=[]
    xlist=[]
    taglist=[]
    elist=[]
    ppcrosspath='/Users/jogler/Physik/Fermi/scripts/pp_data/'
    ppcross=crs.crossSection(ppcrosspath)

    source=sed.source(6000,30.,3.e4)
    source.set_radiation_densities(0.25,0.84,3.0e-3,0.9)#W51C densities Abdo et al 2009
    source.set_radiation_photons(-8.,4.,0.1)
    source.set_am_prop(10.,10.,1.)
    
    fluxScale=Eg/(4.*np.pi*(source.dist*const.PARSEC*const.m2cm)**2.)/const.erg2GeV
    print 'Scale: ',fluxScale
    pp=mod.model(Ne,'pp',[],source,ppcross)
    ppSpec=pp.nucFactor*pp.EdQdE_pp(Ne,Ee,Eg)
    #secEPoints=np.logspace(np.log10(Eg[0]),np.log10(Eg[len(Eg)-1]),particlePoints)
    eSecSpec=pp.nucFactor*(pp.EdQdE_pp(Ne,Ee,Ee,1)+pp.EdQdE_pp(Ne,Ee,Ee,2))*source.age*const.yr2s
    print 'points in secondary spec: ',np.size(eSecSpec)
    
    secBrems=pp.Brems_spec(eSecSpec,Ee,Eg)
    secIC=pp.IC_spec_gamma(source.phTot,source.Eph,eSecSpec,Ee,Eg)

    ylist.append(ppSpec*fluxScale)
    ylist.append(secBrems*fluxScale)
    ylist.append(secIC*fluxScale)
    
    xlist.append(Eg)
    xlist.append(Eg)
    xlist.append(Eg)
    elist.append(0)
    elist.append(0)
    elist.append(0)
    taglist.append('pp')
    taglist.append('brems sec')
    taglist.append('IC sec')
    
    fig=plot(xlist,ylist,[],elist,taglist,r'$\mathrm{E}_{\gamma}\, \mathrm{ [GeV]}$',r'$EF_{\gamma}\, [\mathrm{erg}\, \mathrm{ cm}^{-2} \mathrm{s}^{-1}]$')
    plt.show(fig)
def run_sedfitter(inputfile,collumns,ispec,ipara,iprocess,erange,distance,size,shape,shellth,age,crosspath,testrun,testerror,config,confpath):
    #print crosspath
    print ipara,type(ipara)
    if ipara!=[]:
        ipara=ut.argparse_list_of_list_conv(ipara)
    else:
        fn=open(confpath)
        confDict=eval(fn.read())
        specname=[]
        counter=0
        for p in iprocess:
            specname.append('%s_%s_%i'%(iprocess[counter],ispec[counter],counter))
            counter+=1
        print 'spectra: ',specname
        ipara=ut.conv_dict2list(specname,['a','b','c','d','e','f','g','h'],confDict)
        print 'parameters retrieved from config files:'
        print ipara
        print '\n'
    if erange!=[]:
        erange=ut.argparse_list_of_list_conv(erange)
    else:
        for p in iprocess:
            erange.append([1e6,1e15])


    #print erange,erange[0][0],erange[0][1]

    
    if testrun:
        '''generate test data only works with initial parameter
        provided at the commandline
        '''
        print ispec[0]
        test=testsed.testdata(15,ipara,erange[0][0],erange[0][1],ispec[0],testerror)
        apara=ipara
        print 'par' ,apara
        data=test.make_data()
        #print data
        '''randomize the initial parameters'''
        ipara=np.array(ipara)
        ipara=np.random.normal(ipara,testerror*abs(ipara))
        newerange=[[data[0][0],data[0][np.size(data[0])-1]]]
        print "energyrange changed to data energy range: ",newerange
        Fitroutine=sed.SEDfitter(data,ispec,ipara,newerange,iprocess,sed.source(),crosspath)
    else:
        metadata=[]
        data=[[],[],[],[]]
        ''' read in the data files'''
        if len(inputfile)==1:
            datapart=ut.fill_array_from_txtfile(inputfile[0],collumns," ",8,4.,3,1)
            print type(datapart)
            metadata+=datapart
            data=ut.build_fitdata(data,datapart)
        else:
            for infile in inputfile:
                if inputfile.index(infile)==len(inputfile)-1:
                    datapart=ut.fill_array_from_txtfile(infile,[1,2,3,-1,4,-1]," ")
                    #print 'type: ',type(datapart)
                    #print datapart
                    datapart=ut.conv_magic_sed(datapart)
                    #print len(datapart)
                    #print 'type: ',type(datapart),datapart
                else:
                    datapart=ut.fill_array_from_txtfile(infile,collumns," ",8,2.,3,1)
                    print type(datapart)
                metadata+=datapart
                data=ut.build_fitdata(data,datapart)
   
        #print metadata
        #testspec=sed.spectrum(spsh.test,ipara,data[0][0],data[0][np.size(data[0])-1],np.size(data[0]))
            

    Fitroutine=sed.SEDfitter(data,ispec,ipara,erange,iprocess,sed.source(),crosspath)
    '''SNR specific setting of source parameters'''
    '''W51C'''
    Fitroutine.set_source(distance,size,age,shape,shellth)
    Fitroutine.source.set_radiation_photons(-7.,2.,0.05)
    Fitroutine.set_im(10,0,150)
    Fitroutine.source.set_radiation_densities(0.25,0.84,3.0e-3,0.9)
    
    if config:
        Fitroutine.set_config_dict(confpath)

    modelSpec=Fitroutine.get_model_func()[0]
    modelval=modelSpec(ipara[0])
    
    def pp2T_func(ppFunc,MParticle):
       # newfunc=np.sqrt((ppFunc*const.C)**2+MParticle**2*const.C**4)-MParticle*const.C**2
        newfunc=np.sqrt(ppFunc**2+MParticle**2)#-MParticle
        return newfunc
    
    def dp2dT_func(ppFunc,MParticle,p):
        newfunc=ppFunc/((np.sqrt(p**2+MParticle**2)*p-MParticle**2))
        return newfunc

    
    protonPVal=Fitroutine.parentspec[0].get_xvals()
    protonEnergy=Fitroutine.parentspec[0].get_xvals()
    protonSpecVal=dp2dT_func(Fitroutine.parentspec[0].value(),const.ME_GeV*1.e3,protonPVal)

    finalPar,finalParError,finalChi2,finalVal,func,fineGamma,xFineGamma=Fitroutine.fit(config)
    
    #print 'type func: ',type(func),func
    
    if testrun:
        print 'simulation parameter: ',apara

    '''
    Prepare the plotting of the data and the fit functions
    in a pretty way
    '''

    xvals=[]
    yvals=[]
    yerrors=[]
    yerrflaq=[]
    tags=['Fermi/LAT','MAGIC']
    counter=0
    Fscale=const.MeV2erg
    for p in iprocess:
       ''' if p =="pp_wos":
            tags.append('pp $\chi^2:$ %.1f'%(finalChi2[counter]))
        else: '''   
       tags.append('%s $\chi^2:$ %.1f'%(p,finalChi2[counter]))
       counter+=1
        
    #tags=['Fermi/LAT','MAGIC','%s $\chi^2:$ %.1f'%(finalChi2[0]),'Brems $\chi^2:$ %.1f'%(finalChi2[1]),'IC $\chi^2:$ %.1f'%(finalChi2[2])]
    counter=0
    for i in range(len(metadata)/4):
        xvals.append(metadata[counter])
        yvals.append(metadata[counter+1]*Fscale)
        yerrors.append(metadata[counter+3]*Fscale)
        yerrflaq.append(1)
        counter+=4
    for f in fineGamma:
        if type(f)==list:# required for secondaries in pp process
            for ff in f:
                yvals.append(ff*Fscale)
                tags.append('sub')
                yerrflaq.append(0)
        else:    
            yvals.append(f*Fscale)
            yerrflaq.append(0)
    for xf in xFineGamma:
        if type(xf)==list:# required for secondaries in pp process
            for xxf in xf:
                tags.append('sub')
                xvals.append(xxf)
        else:    
            xvals.append(xf)

    if len(xvals)!= len(yvals):
        print 'ERROR: number of x arrays not equal to y arrays'
        exit()

    tags.append('pp only')
    tags.append('sec brems')
    tags.append('sec IC')
    
    #print xvals,yvals
    #print len(xvals),len(yvals),len(yerrflaq),len(tags)

    fig=ut.plot(xvals,yvals,yerrors,yerrflaq,tags,res=False,onlybands=False,forceLog=True,ytitle=r'E$^{2}$ dN/dE [erg $\mathrm{cm}^{-2} \mathrm{ s}^{-1}$]')
    fig.show()


    ''' 
    xvals=[data[0]]
    finalVal.insert(0,data[1])
            
    for f in fineGamma:
        finalVal.append(f)
        xvals.append(data[0]) # adds the xvals for the rough model fucntions
    for xf in xFineGamma:
        xvals.append(xf)# adds the xvals for the fine model fucntions
    
    if len(finalVal)!=len(xvals):
        print 'ERROR: number of plotted x and y lists is not equal'
        exit()
    
    plot(xvals,finalVal,data[3])
    '''    
    
    #plot([data[0],data[0],func.get_xvals()/1.e6],[data[1],finalVal,func.value()],data[4])
    #plot([data[0],10**np.arange(-2.+3.,4.+3.,0.1),protonEnergy],[data[1],modelval,protonSpecVal],data[4])
    #plot([data[0],data[0],protonEnergy],[data[1],modelval,protonSpecVal],data[4])

    plt.figure()
    #print 'protonPVal: ',protonPVal
    #print 'Ep: ',protonEnergy
    #print 'Fp: ',protonSpecVal
    '''
    print 'test: ',Fitroutine.parentspec[0].get_parameters()
    Fitroutine.parentspec[0].set_precision(np.size(protonEnergy))
    plt.loglog(protonEnergy,Fitroutine.parentspec[0].value())
    plt.loglog(protonEnergy,protonSpecVal)
    plt.show()
    plt.figure()
    plt.loglog(protonEnergy,protonPVal)
    plt.show()
    '''
  
    var=raw_input("Press enter to exit")
Пример #3
0
def plot_spectrum(typ=['ic','brems','pp']):
    '''plots spectra'''
    Eg=np.logspace(6.,12.,100)
    Ee=np.logspace(6.,15.,1000)
    parameter=[1.,9.,-2.5,10.]
    #Ne2=spsh.bplcut(Ee,[49.4,-1.5,-2.9,10.176,13.])
    Ne2=spsh.sbplcut(Ee,[49.4,9.,-1.5,10.301,2.3,13.])
    #print Ne
    Ne=spsh.sbplcuty(Ee,[49.4,9.,-1.5,10.301,2.3,13.])#W51C values abdo et al 2009
    ENe=Ee*Ne2
    spec=sed.spectrum(spsh.sbplcut,[49.4,9.,-1.5,10.301,2.3,13.],1.e6,1.e15,1000.)
    
    #Ne2=spsh.plcut()
    ylist=[]
    xlist=[]
    taglist=[]
    elist=[]
    source=sed.source()
    source.set_radiation_densities(0.25,0.84,3.0e-3,0.9)#W51C densities Abdo et al 2009
    source.set_radiation_photons(-8.,4.,0.1)
    source.set_am_prop(10.,10.,1.)
    ppcrosspath='/Users/jogler/Physik/Fermi/scripts/pp_data/'
    ppcross=crs.crossSection(ppcrosspath)

    if 'brems' in typ:
        brems=mod.model(spec,'brems',[],source,ppcross)
        En=brems.spec.get_xvals()*brems.spec.value()
        print En-ENe
        ylist.append((Eg/1.e9)*brems.Brems_spec(ENe,Ee/1.e9,Eg/1.e9))
        #ylist.append(brems.Brems_spec(ENe,Ee/10**9,Eg/10**9))
        # ylist.append(brems.Brems_spec_ee(Ne,Ee,Eg))
        xlist.append(Eg)
        #xlist.append(Eg)
        taglist.append('Brems N')
        #taglist.append('Brems ee')
        elist.append(0)
        #elist.append(0)

    if 'ic' in typ:
        for ph in [source.phTot,source.phCMB,source.phDust,source.phStar]:
            ic=mod.model(Ne,'ic',[],source,ppcrosspath)
            ylist.append(Eg/1.e9*ic.IC_spec_gamma(ph/source.Eph,source.Eph,ENe,Ee/10**9,Eg/10**9))
            xlist.append(Eg)
            elist.append(0)
            
        for t in['IC total','IC CMB','IC Dust','IC Star']: 
            taglist.append(t)
    
    if 'pp' in typ:
        pp=mod.model(Ne,'pp',[],source,ppcross)
        ylist.append(pp.EdQdE_pp(Ne,Ee/10**9,Eg/10**9))
        xlist.append(Eg)
        elist.append(0)
        taglist.append(r'$\mathrm{pp}_{\gamma} $')
        ''' plot e+ e- sec spec'''
        ylist.append(pp.EdQdE_pp(Ne,Ee/10**9,Eg/10**9,1))
        xlist.append(Eg)
        elist.append(0)
        taglist.append('pp_e-')
        ylist.append(pp.EdQdE_pp(Ne,Ee/10**9,Eg/10**9,2))
        xlist.append(Eg)
        elist.append(0)
        taglist.append('pp_e+')
    if 'comp' in typ:
        if 'brems' in typ:
            print 'nH: %.2f nHeff: %.2f Zeff: %.2f'%(source.nH,source.nHeff,source.Zeff)
            bremsSp=gs.brems_spectrum(ENe,Ee/10**9,source.nHeff)
            ylist.append(Eg/10**9*bremsSp(Eg/10**9))
            xlist.append(Eg)
            elist.append(0)
            taglist.append('brems dima')
        if 'ic' in typ:
            icSp=gs.IC_spectrum(source.phTot/source.Eph,source.Eph,ENe,Ee/10**9)
            ylist.append(Eg/10**9*icSp(Eg/10**9))
            xlist.append(Eg)
            elist.append(0)
            taglist.append('ic dima')
        if 'pp' in typ:
            ppSp=gs.EdQdE_pp(Ne,Ee/10**9,source.nH,0)
            ylist.append(ppSp(Eg/10**9))
            xlist.append(Eg)
            elist.append(0)
            taglist.append('pp dima')
        
    fig0=plot(xlist,ylist,[],elist,taglist,r'$\mathrm{E}_{\gamma}\, \mathrm{ [eV]}$',r'$E^2F_{\gamma}\, [\mathrm{erg}\,\mathrm{ cm}^{-3} \mathrm{s}^{-1}]$')
    #plt.show(fig0)

    xlist=[]
    ylist=[]
    elist=[]
    taglist=[]
    
    xlist.append(Ee)
    xlist.append(Ee)
    ylist.append(Ne)
    ylist.append(Ne2)
    elist.append(0)
    elist.append(0)
    taglist.append('Ne')
    taglist.append('Ne2')
    fig1=plot(xlist,ylist,[],elist,taglist,r'$\mathrm{E}_{\gamma}\, \mathrm{ [eV]}$',r'$N_e\, [\mathrm{N}_{e}\mathrm{ cm}^{-3} \mathrm{s}^{-1}]$')
    plt.show()
Пример #4
0
def plot_cross(typ=['ic','brems','pp']):
    Eg=np.logspace(6.,12.,300)
    Ee=np.logspace(7.,15.,100)
    Ne=spsh.plcut(Ee,[1.,9.,-3.,14.])
    Ne2=spsh.plcut(Ee,[1.,9.,-2.0,11.])
    ylist=[]
    xlist=[]
    taglist=[]
    elist=[]

    if 'brems' in typ:
        Ee/=10**9
        Eg/=10**9
        ylist.append(const.C*const.m2cm*crs.sigma_brems(Eg,Ee,Ne))
        print const.C*const.m2cm*crs.sigma_brems(Eg,Ee,Ne)
        #ylist.append(crs.sigma_brems(Eg,Ee,Ne2))
        ylist.append(const.C*const.m2cm*crs.sigma_brems_ee(Eg,Ee,Ne))
        #ylist.append(crs.sigma_brems_ee(Eg,Ee,Ne2))
        ylist.append(const.C*const.m2cm*gs.sigmaB_test(Ne,Eg,Ee))
        xlist.append(Eg)
        xlist.append(Eg)
        xlist.append(Eg)
        #xlist.append(Eg)
        #xlist.append(Eg)
        for t in['brems_N -1.5','brems_ee -1.5','brems dima']: 
            taglist.append(t)
            elist.append(0)
    if 'ic' in typ:
        Ee/=10**9
        Eg/=10**9
        #print Ee,Eg
        source=sed.source()
        source.set_radiation_densities(0.25,0.84,3.0e-3,0.9)#W51C densities Abdo et al 2009
        source.set_radiation_photons(-8.,4.,0.1)
        '''
        print 'CMB: ',source.phCMB
        print 'Dust : ',source.phDust
        print 'Star: ',source.phStar
        print 'tot: ',source.phTot
        '''
        for ph in [source.phTot,source.phCMB,source.phDust,source.phStar]:
            def sigma_fcn(x):
                sigma=crs.sigma_ic(x,source.Eph,Ee)
                #print 'sigma: ',np.size(sigma)
                return const.C*const.m2cm*np.dot(ph,np.dot(sigma,np.ones_like(Ee)))

            sigma=np.frompyfunc(sigma_fcn,1,1)
            ylist.append(sigma(Eg))
            xlist.append(Eg)
            elist.append(0)
        for t in['IC total','IC CMB','IC Dust','IC Star']: 
            taglist.append(t)
        plot([source.Eph,source.Eph,source.Eph,source.Eph],[source.phTot,source.phCMB,source.phDust,source.phStar],[],elist,['Tot','CMB','Dust','Star'],r'E [eV]',r'$N_{ph}$')
    
    #print ylist
    
    plot(xlist,ylist,[],elist,taglist,r'E [eV]',r'$N_{e}\times \sigma_{\mathrm{brems}}$')

    #plot([source.Eph,source.Eph,source.Eph,source.Eph],[source.phTot,source.phCMB,source.phDust,source.phStar],[],elist,['Tot','CMB','Dust','Star'],r'E [eV]',r'$N_{ph}$')
    
    plt.show()