def build_roi(name, snrdata, latdata): snrdata=loaddict(snrdata) latdata=loaddict(latdata) roi_dir = SkyDir(*snrdata[name]['cel']) snrsize = snrdata[name]['size'] if isinstance(snrsize,list) and len(snrsize) == 2: snrradius = math.sqrt(snrsize[0]*snrsize[1])/2.0 else: snrradius = snrsize/2.0 ds = DataSpecification(**latdata['data']) sa = SpectralAnalysis(ds, binsperdec = 4, emin = 1e4, emax = 10**5.5, irf = "P7SOURCE_V6", roi_dir = roi_dir, maxROI = 10, minROI = 10, event_class= 0) diffuse_sources = get_default_diffuse(**latdata['diffuse']) catalog = Catalog2FGL(**latdata['catalog']) roi=sa.roi(point_sources=[], diffuse_sources=diffuse_sources, catalogs=catalog) print 'bins',roi.bin_edges for source in roi.get_sources(): if np.degrees(source.skydir.difference(roi_dir)) < snrradius + 0.5: roi.del_source(source) snr = ExtendedSource( name = name, model = PowerLaw(), spatial_model = Disk(sigma=snrradius, center=roi_dir) ) roi.add_source(snr) return roi
def setup_roi(name, snrdata, catalog_dict=dict(), roi_dict=dict()): snr=yaml.load(open(snrdata))[name] skydir=SkyDir(*snr['cel']) size=snr['size'] ds=DataSpecification( ft1files='$FERMILANDE/data/PWNCAT2/nov_30_2011/ft1_PWNCAT2_allsky.fits', ft2files='$FERMILANDE/data/PWNCAT2/nov_30_2011/ft2_PWNCAT2_allsky.fits', ltcube='$FERMILANDE/data/PWNCAT2/nov_30_2011/ltcube_PWNCAT2_allsky.fits', binfile='$FERMILANDE/data/PWNCAT2/nov_30_2011/binned_%s.fits' % 4) sa=SpectralAnalysis(ds, binsperdec = 4, roi_dir = skydir, irf = 'P7SOURCE_V6', maxROI = 10, minROI = 10, event_class = 0) diffuse_sources = diffuse_sources = get_default_diffuse( diffdir=e('$FERMILANDE/diffuse'), gfile='ring_2year_P76_v0.fits', ifile='isotrop_2year_P76_source_v0.txt') catalogs = Catalog2FGL('$FERMILANDE/catalogs/gll_psc_v05.fit', latextdir='$FERMILANDE/extended_archives/gll_psc_v05_templates/', **catalog_dict) roi = sa.roi( catalogs = catalogs, diffuse_sources = diffuse_sources, **roi_dict) return roi
def test_ps1(self): if PointlikeTest.VERBOSE: print '\nAnalyze a simulated point source against the galactic + isotropic diffuse\n' center = SkyDir(0, 0) diffuse_sources = get_default_diffuse( diffdir='$GLAST_EXT/diffuseModels/v2r0p1/', gfile='ring_2year_P76_v0.fits', ifile='isotrop_2year_P76_source_v1.txt') model = PowerLaw(index=2) model.set_flux(1e-6) ps_mc = PointSource(name='source', skydir=center, model=model) ps_fit = ps_mc.copy() point_sources = [ps_fit] roi = PointlikeTest.get_roi('ps1', center, point_sources, diffuse_sources) global roi_pt roi_pt = roi # helps with debugging if PointlikeTest.VERBOSE: print roi roi.fit(use_gradient=PointlikeTest.USE_GRADIENT) if PointlikeTest.VERBOSE: print roi roi.localize(update=True) roi.fit(use_gradient=PointlikeTest.USE_GRADIENT) if PointlikeTest.VERBOSE: roi.print_summary() print roi self.compare_model(ps_fit, ps_mc) self.compare_spatial_model(ps_fit, ps_mc, roi.lsigma)
def setup_pwn(name,pwndata,phase, free_radius=5, tempdir=None, emin=1.0e2, emax=1.0e5,maxroi=10,model=None,**kwargs): """Name of the source pwndata Yaml file returns pointlike ROI. """ sources=yaml.load(open(pwndata)) catalog_name=sources[name]['catalog'] ltcube=sources[name]['ltcube'] pulsar_position=SkyDir(*sources[name]['dir']) ft2=sources[name]['ft2'] ft1=sources[name]['ft1'] # in case no list was passed if len(phase)==2 and isinstance(phase[0],numbers.Real) and \ isinstance(phase[1],numbers.Real): # write in case phase wraps around. if phase[0]>phase[1]: phase=[[phase[0],1.0],[0.0,phase[1]]] else: phase = [phase] phase_factor=get_phase_factor(phase) print "phase" print phase print "phase_factor=%.2f"%phase_factor catalog=FermiCatalog(e("$FERMI/catalogs/gll_psc_v02.fit"),free_radius=free_radius) catalog_source=[i for i in catalog.get_sources(SkyDir(),180) if i.name==catalog_name][0] center=catalog_source.skydir if tempdir is None: tempdir=mkdtemp(prefix='/scratch/') binfile=j(tempdir,'binned_phased.fits') # apply phase cut to ft1 file phased_ft1 = j(tempdir,'ft1_phased.fits') phasetools.phase_cut(ft1,phased_ft1,phaseranges=phase) # create a temporary ltcube scaled by the phase factor # phased_ltcube=j(tempdir,'phased_ltcube.fits') # phase_ltcube(ltcube,phased_ltcube, phase=phase) phased_ltcube=ltcube from uw.like.pointspec import DataSpecification data_specification = DataSpecification( ft1files = phased_ft1, ft2files = ft2, ltcube = phased_ltcube, binfile = binfile) spectral_analysis = SpectralAnalysis(data_specification, binsperdec = 4, emin = 100, emax = 100000, irf = "P6_V3_DIFFUSE", roi_dir = center, maxROI = maxroi, minROI = maxroi) if model == None : roi=spectral_analysis.roi( roi_dir=center, diffuse_sources=get_default_diffuse(diffdir=e("$FERMI/diffuse"), gfile="gll_iem_v02.fit", ifile="isotropic_iem_v02.txt"), catalogs = catalog, phase_factor = 1.0, fit_emin = [emin,emin], fit_emax = [emax,emax], **kwargs) else : roi=spectral_analysis.roi( roi_dir=center, xmlfile = model, phase_factor =1.0, fit_emin = [emin,emin], fit_emax = [emax,emax], **kwargs) print "---------------------Energy range--------------------" print "emin="+str(roi.bands[0].emin)+"\n" print "emax="+str(roi.bands[len(roi.bands)-1].emax)+"\n" # keep overall flux of catalog source, # but change the starting index to 2. roi.modify(which=catalog_name, name=name, index=2, keep_old_flux=True) return roi
from lande.utilities.random import random_on_sphere from lande.utilities.tools import savedict for roi_dir in [SkyDir(0,0, SkyDir.GALACTIC), SkyDir(70,85, SkyDir.GALACTIC)]: savedir='/nfs/slac/g/ki/ki03/lande/fermi/data/monte_carlo/test_mapcube_cutting/plot_example/datadir_l_%d_b_%d' % (roi_dir.l(), roi_dir.b()) emin=1e3 emax=1e5 diffdir='/afs/slac/g/glast/groups/diffuse/rings/2year/' diffuse_sources = get_default_diffuse(diffdir=diffdir, gfile='ring_2year_P76_v0.fits', ifile='isotrop_2year_P76_source_v0.txt') ft2 = dict2fgl['ft2'] ltcube = dict2fgl['ltcube'] ft1 = join(savedir,'ft1.fits') m=MonteCarlo( sources=diffuse_sources, emin = emin, emax = emax, irf='P7SOURCE_V6', roi_dir=roi_dir, maxROI=10,
def setup_pointlike(name,pwnlist,phasing=True): """Name of the source pwnlist Yaml file phasing=true : apply phase cut phasing=false : don't do it""" sources=yaml.load(open(pwnlist)) catalog_name=sources[name]['catalog'] phase=sources[name]['phase'] ltcube=sources[name]['ltcube'] pulsar_position=SkyDir(*sources[name]['dir']) if phasing==True: phase_factor=phase[1]-phase[0] if phase[1]>phase[0] else (1-phase[0]) + (phase[1]-0) ft1=sources[name]['ft1'] else : phase_factor=1.0 raise Exception("Unable to phase data") catalog=FermiCatalog(e("$FERMI/catalogs/gll_psc_v02.fit"),free_radius=5) catalog_source=[i for i in catalog.get_sources(SkyDir(),180) if i.name==catalog_name][0] center=catalog_source.skydir from uw.like.pointspec import DataSpecification data_specification = DataSpecification( ft1files = ft1, ltcube = ltcube, binfile = "binned_%s.fits" % name) spectral_analysis = SpectralAnalysis(data_specification, binsperdec = 8, emin = 100, emax = 100000, irf = "P6_V3_DIFFUSE", roi_dir = center, maxROI = 10, minROI = 10) roi=LandeROI(spectral_analysis.roi( roi_dir=center, diffuse_sources=get_default_diffuse(diffdir=e("$FERMI/diffuse"), gfile="gll_iem_v02.fit", ifile="isotropic_iem_v02.txt"), catalogs = catalog, fit_emin = 100, fit_emax = 100000, catalog_include_radius = 20, phase_factor = phase_factor) ) roi.del_source(catalog_name) # make residual TS map # add in PWN Candidate source=PointSource( name=name, model=PowerLaw(p=[1,2]), skydir=pulsar_position ) source.model.set_flux(1e-7,emin=100,emax=100000) roi.add_source(source) return roi
def build_roi(self, name, fast): if fast: roi_size=5 binsperdec = 2 max_free=2 free_radius=2 else: roi_size = 10 binsperdec = 4 max_free=5 free_radius=5 catalog = Catalog2FGL('$FERMI/catalogs/gll_psc_v05.fit', latextdir='$FERMI/extended_archives/gll_psc_v05_templates', prune_radius=0, max_free=max_free, free_radius=free_radius, limit_parameters=True) ft1 = self.radiopsr_loader.get_ft1(name) ft2 = self.radiopsr_loader.get_ft2(name) ltcube = self.radiopsr_loader.get_ltcube(name) binfile = self.radiopsr_loader.get_binfile(name, binsperdec) roi_dir = self.radiopsr_loader.get_skydir(name) ds = DataSpecification( ft1files = ft1, ft2files = ft2, ltcube = ltcube, binfile = binfile) sa = SpectralAnalysis(ds, binsperdec = binsperdec, emin = 100, emax = 1000000, irf = "P7SOURCE_V6", roi_dir = roi_dir, maxROI = roi_size, minROI = roi_size, event_class= 0) fit_emin = 1e2 fit_emax = 10**5.5 model=PowerLaw(index=2, e0=np.sqrt(fit_emin*fit_emax)) model.set_limits('index',-5,5) ps = PointSource(name=name, model=model, skydir=roi_dir) point_sources = [ps] diffuse_sources = get_default_diffuse(diffdir="/afs/slac/g/glast/groups/diffuse/rings/2year", gfile="ring_2year_P76_v0.fits", ifile="isotrop_2year_P76_source_v0.txt", limit_parameters=True) roi=sa.roi(point_sources=point_sources, diffuse_sources=diffuse_sources, catalogs=catalog, fit_emin=fit_emin, fit_emax=fit_emax) return roi
def setup_region(name,pwndata,phase, free_radius=5, tempdir=None, maxroi=10, xml=None, **kwargs): """Name of the source pwndata Yaml file returns pointlike ROI. """ phase = PhaseRange(phase) sources=yaml.load(open(pwndata)) catalog_name=sources[name]['catalog']['2fgl'] ltcube=sources[name]['ltcube'] pulsar_position=SkyDir(*sources[name]['dir']) ft2=sources[name]['ft2'] ft1=sources[name]['ft1'] catalog=FermiCatalog(e("$FERMI/catalogs/gll_psc_v02.fit")) catalog=Catalog2FGL('$FERMI/catalogs/gll_psc_v05.fit', latextdir='$FERMI/extended_archives/gll_psc_v05_templates', free_radius=free_radius) catalog_source=catalog.get_source(catalog_name) center=catalog_source.skydir if tempdir is None: tempdir=mkdtemp(prefix='/scratch/') binfile=j(tempdir,'binned_phased.fits') if np.allclose(phase.phase_fraction,1): phased_ltcube = ltcube phased_ft1 = ft1 else: # create a temporary ltcube scaled by the phase factor phased_ltcube=j(tempdir,'phased_ltcube.fits') phase_ltcube(ltcube,phased_ltcube, phase=phase) # apply phase cut to ft1 file phased_ft1 = j(tempdir,'ft1_phased.fits') phasetools.phase_cut(ft1,phased_ft1,phaseranges=phase.tolist(dense=False)) from uw.like.pointspec import DataSpecification ds = DataSpecification( ft1files = phased_ft1, ft2files = ft2, ltcube = phased_ltcube, binfile = binfile) sa = SpectralAnalysis(ds, binsperdec = 8, emin = 100, emax = 100000, irf = "P6_V11_DIFFUSE", roi_dir = center, maxROI = maxroi, minROI = maxroi) if xml is None: roi=sa.roi( diffuse_sources=get_default_diffuse(diffdir="/afs/slac/g/glast/groups/diffuse/mapcubes", gfile="gll_iem_v02.fit", ifile="isotropic_iem_v02.txt"), catalogs = catalog, phase_factor =1, **kwargs) else: roi=sa.roi_from_xml( roi_dir=center, xmlfile = xml, phase_factor =1, **kwargs) print 'bins ',roi.bin_edges roi.del_source(catalog_name) return roi
#indices_and_galcenter_fluxes = [[1.5, 2.2e-8], [2.0, 1.2e-7], [2.5, 5.7e-7], [3.0, 2.7e-6]] # v11 #indices_and_galcenter_fluxes = [[1.5, 2.3e-8], [2.0, 1.3e-7], [2.5, 4.5e-7], [3.0, 1.2e-6]] # v12 indices_and_galcenter_fluxes = [[1.5, 2.3e-8], [2.0, 1.2e-7], [2.5, 4.5e-7], [3.0, 2e-6]] np.random.shuffle(indices_and_galcenter_fluxes) for index,galcenter_flux in indices_and_galcenter_fluxes: diffuse=gal,iso=get_default_diffuse(diffdir='/afs/slac/g/glast/groups/diffuse/rings/2year', gfile='ring_2year_P76_v0.fits', ifile='isotrop_2year_P76_source_v0.txt') def integral(skydir): i = lambda m: m.integral(skydir, emin, emax) return i(gal.dmodel[0]) + i(iso.dmodel[0]) bg_ratio = integral(SkyDir(0,0,SkyDir.GALACTIC))/integral(roi_dir) flux = galcenter_flux*bg_ratio**-0.5 print 'index=%.1f, galcenter_flux=%.1e, bg_ratio=%.2f, l,b=%.2f,%.2f, flux=%.1e' % \ (index,galcenter_flux,bg_ratio,roi_dir.l(),roi_dir.b(),flux) name = 'source_index_%g' % index tempdir = mkdtemp(prefix='/scratch/')
roi_dir=SkyDir(94.30999756,22.57999992) elif name == 'W51C': ft1='/u/gl/funk/data3/ExtendedSources/NewAnalysis/gtlike/W51C/SmoothBrokenPowerlaw/W51C-ft1.fits' ltcube='/u/gl/funk/data3/ExtendedSources/NewAnalysis/gtlike/W51C/ltcube_239557414_334152002.fits' roi_dir=SkyDir(290.81799316,14.14500046) else: raise Exception("...") ds = DataSpecification( ft1files=ft1, ft2files=ft2, ltcube=ltcube, binfile='binned_ct=%s.fits' % conv_type) diffuse_sources = get_default_diffuse( diffdir=expandvars('$diffuse'), gfile="gal_2yearp7v6_v0.fits", ifile="iso_p7v6source_extrapolated.txt") catalog = Catalog2FGL(expandvars('$catalogs/gll_psc_v07.fit'), latextdir=expandvars('$extended_archives/gll_psc_v07_templates'), free_radius=5, ) if args.test: roi_size=5 else: roi_size=10*np.sqrt(2) print 'conv',conv_type sa = SpectralAnalysis(ds,
def setup_pwn(name,pwndata,phase, free_radius=5, tempdir=None, **kwargs): """Name of the source pwndata Yaml file returns pointlike ROI. """ sources=yaml.load(open(pwndata)) catalog_name=sources[name]['catalog'] ltcube=sources[name]['ltcube'] pulsar_position=SkyDir(*sources[name]['dir']) ft2=sources[name]['ft2'] ft1=sources[name]['ft1'] # in case no list was passed if len(phase)==2 and isinstance(phase[0],numbers.Real) and \ isinstance(phase[1],numbers.Real): # write in case phase wraps around. if phase[0]>phase[1]: phase=[[phase[0],1.0],[0.0,phase[1]]] else: phase = [phase] phase_factor=get_phase_factor(phase) catalog=FermiCatalog(e("$FERMI/catalogs/gll_psc_v02.fit"),free_radius=free_radius) catalog_source=[i for i in catalog.get_sources(SkyDir(),180) if i.name==catalog_name][0] center=catalog_source.skydir if tempdir is None: tempdir=mkdtemp(prefix='/scratch/') binfile=j(tempdir,'binned_phased.fits') # apply phase cut to ft1 file phased_ft1 = j(tempdir,'ft1_phased.fits') phasetools.phase_cut(ft1,phased_ft1,phaseranges=phase) # create a temporary ltcube scaled by the phase factor # phased_ltcube=j(tempdir,'phased_ltcube.fits') # phase_ltcube(ltcube,phased_ltcube, phase=[0.0,1.0]) phased_ltcube=ltcube from uw.like.pointspec import DataSpecification data_specification = DataSpecification( ft1files = phased_ft1, ft2files = ft2, ltcube = phased_ltcube, binfile = binfile) spectral_analysis = SpectralAnalysis(data_specification, binsperdec = 4, emin = 100, emax = 100000, irf = "P6_V3_DIFFUSE", roi_dir = center, maxROI = 10, minROI = 10) roi=spectral_analysis.roi( roi_dir=center, diffuse_sources=get_default_diffuse(diffdir=e("$FERMI/diffuse"), gfile="gll_iem_v02.fit", ifile="isotropic_iem_v02.txt"), catalogs = catalog, phase_factor = phase_factor, **kwargs) # phaseing already done to the ltcube print "phase_factor=%.2f"%phase_factor # keep overall flux of catalog source, # but change the starting index to 2. roi.modify(which=catalog_name, name=name, index=2, keep_old_flux=True) roi.toXML(filename="essai") print roi roi.print_summary() for names in roi.get_names(): try : roi.modify(names,Norm=roi.get_model(names)[0]*roi.phase_factor) except : try : roi.modify(names,Int_flux=roi.get_model(names)[0]*roi.phase_factor) except : print names table=roi.get_names() print roi.modify(which=table[len(table)-2],model=PowerLaw(p=[1.0*phase_factor,0.1]),free=[True,False]) print roi.modify(which=table[len(table)-1],model=PowerLaw(p=[1.0*phase_factor,0.1]),free=[True,False]) # print roi.modify(which='eg_v02',free=[False]) print roi return roi
def get_background(): return get_default_diffuse(diffdir="/afs/slac/g/glast/groups/diffuse/rings/2year", gfile="ring_2year_P76_v0.fits", ifile="isotrop_2year_P76_source_v0.txt", limit_parameters=True)
ft1 = 'simulated_ft1.fits' catalog = Catalog2FGL('/afs/slac/g/glast/groups/catalog/2FGL/gll_psc_v05.fit', latextdir='/afs/slac/g/glast/groups/catalog/2FGL/gll_psc_v05_templates') if source == 'W44': skydir = SkyDir(284.005,1.345) gtifile = '/u/gl/funk/data/GLAST/ExtendedSources/NewAnalysis/gtlike/W44/sel_W44_60_100000.fits' elif source == 'IC443': skydir = SkyDir(94.310,22.580) gtifile = '/u/gl/funk/data/GLAST/ExtendedSources/NewAnalysis/gtlike/IC443/sel_IC443_60_100000.fits' if args.diffuse == 'galactic': ds = get_default_diffuse( diffdir="/nfs/slac/g/ki/ki03/lande/fermi/diffuse/", gfile="gal_2yearp7v6_v0.fits", ifile="iso_p7v6source.txt") elif args.diffuse == 'sreekumar': ds = [ get_sreekumar() ] elif args.diffuse == 'nobackground': ds = [] elif args.diffuse == 'extrapolated': ds = get_background( "/afs/slac/g/glast/groups/scienceTools/bugs/Likelihood/energy_dispersion/Simulations/galdif_extended.fits", '/nfs/slac/g/ki/ki03/lande/fermi/diffuse/iso_p7v6source_extrapolated.txt') else: raise Exception("...") ps,ds = catalog.merge_lists(skydir, radius=15, user_diffuse_list=ds)
def get_background(): return get_default_diffuse( diffdir="/afs/slac/g/glast/groups/diffuse/rings/2year", gfile="ring_2year_P76_v0.fits", ifile="isotrop_2year_P76_source_v0.txt", limit_parameters=True)