Ejemplo n.º 1
0
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
Ejemplo n.º 2
0

sa = SpectralAnalysis(data_specification,
                      binsperdec = 8,
                      emin       = 10, # MeV  
                      emax       = 1e6, # MeV  
                      irf        = 'P7SOURCE_V6',
                      roi_dir    = roi_dir,  
                      maxROI     = 10,
                      event_class= 0,
                      minROI     = 10,
                      use_weighted_livetime=True)

roi = sa.roi_from_xml(
    xmlfile=expandvars(join(simdir,"gtlike_model.xml")),
    roi_dir=roi_dir,
    fit_emin=10**1.75, # 56 MeV
    fit_emax=10**3.25, # 1778 MeV
)

state=PointlikeState(roi)

results=dict()

print 'bins',roi.bin_edges

roi.print_summary(galactic=True)

results['pointlike'] = dict()
results['pointlike']['mc'] = sourcedict(roi,which,errors=False)

roi.fit(use_gradient=False, fit_bg_first = True)
Ejemplo n.º 3
0
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_V8_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 :
        print model
        roi=spectral_analysis.roi_from_xml(
            roi_dir=center,
            xmlfile = model,
            fit_emin = [emin,emin],
            fit_emax = [emax,emax])

    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=name, model=PowerLaw2(p=[1.0e-8,2,1e2,1e5]), keep_old_flux=True)

    return roi