示例#1
0
    def __init__(self, **kwargs):
        keyword_options.process(self, kwargs)

        if self.point_sources == [] and self.diffuse_sources == []:
            self.point_sources, self.diffuse_sources = self.get_default_sources(
            )

        ltcube = join(self.tempdir, 'ltcube.fits')
        ds = DataSpecification(ft1files=join(self.tempdir, 'ft1.fits'),
                               ft2files=join(self.tempdir, 'ft2.fits'),
                               ltcube=ltcube,
                               binfile=join(self.tempdir, 'binfile.fits'))

        sa = SpectralAnalysisMC(
            ds,
            seed=self.seed,
            emin=self.emin,
            emax=self.emax,
            binsperdec=self.binsperdec,
            event_class=self.event_class,
            conv_type=self.conv_type,
            roi_dir=self.roi_dir,
            minROI=self.maxROI,
            maxROI=self.maxROI,
            irf=self.irf,
            use_weighted_livetime=True,
            savedir=self.tempdir,
            tstart=0,
            tstop=self.simtime,
            ltfrac=0.9,
        )

        roi = sa.roi(roi_dir=self.roi_dir,
                     point_sources=self.point_sources,
                     diffuse_sources=self.diffuse_sources)

        self.roi = roi

        fix_pointlike_ltcube(ltcube)
示例#2
0
    def __init__(self,**kwargs):
        keyword_options.process(self, kwargs)

        if self.point_sources == [] and self.diffuse_sources == []:
            self.point_sources, self.diffuse_sources = self.get_default_sources()

        ltcube = join(self.tempdir,'ltcube.fits')
        ds = DataSpecification(
            ft1files = join(self.tempdir,'ft1.fits'),
            ft2files = join(self.tempdir,'ft2.fits'),
            ltcube = ltcube, 
            binfile = join(self.tempdir,'binfile.fits')
        )

        sa = SpectralAnalysisMC(ds,
                                seed=self.seed,
                                emin=self.emin,
                                emax=self.emax,
                                binsperdec=self.binsperdec,
                                event_class=self.event_class,
                                conv_type=self.conv_type,
                                roi_dir=self.roi_dir,
                                minROI=self.maxROI,
                                maxROI=self.maxROI,
                                irf=self.irf,
                                use_weighted_livetime=True,
                                savedir=self.tempdir,
                                tstart=0,
                                tstop=self.simtime,
                                ltfrac=0.9,
                               )

        roi = sa.roi(roi_dir=self.roi_dir,
                     point_sources = self.point_sources,
                     diffuse_sources = self.diffuse_sources)

        self.roi = roi

        fix_pointlike_ltcube(ltcube)
示例#3
0
    def __init__(self, roi, **kwargs):
        """ Build a gtlike pyLikelihood object
            which is consistent with a pointlike roi. """
        keyword_options.process(self, kwargs)
        self.roi = roi

        if not roi.quiet: print 'Running a gtlike followup'

        self.old_dir = os.getcwd()
        if self.savedir is not None:
            self.savedata = True
            if not os.path.exists(self.savedir):
                os.makedirs(self.savedir)
        else:
            self.savedata = False
            self.savedir = mkdtemp(prefix=self.savedir_prefix)

        # put pfiles into savedir
        os.environ['PFILES'] = self.savedir + ';' + os.environ['PFILES'].split(
            ';')[-1]

        if not roi.quiet: print 'Saving files to ', self.savedir

        if self.emin == None and self.emax == None and self.enumbins == None:
            self.emin, self.emax = roi.bin_edges[0], roi.bin_edges[-1]
            self.enumbins = len(roi.bin_edges) - 1
        elif self.emin is not None and \
                self.emax is not None and \
                self.enumbins is not None:
            # all set
            pass
        else:
            raise Exception("emin, emax, and enumbins must all be set.")

        # Note that this formulation makes the gtlike slightly smaller than
        # the pointlike ROI (so the gtlike ROI is inside the pointlike ROI)
        roi_radius = np.degrees(max(_.radius_in_rad for _ in roi.bands))
        if self.bigger_roi:
            npix = int(math.ceil(2.0 * roi_radius / self.binsz))
        else:
            npix = int(math.ceil(np.sqrt(2.0) * roi_radius / self.binsz))

        ct = roi.sa.pixeldata.conv_type

        cmap_file = join(self.savedir, 'ccube.fits')
        srcmap_file = join(self.savedir, 'srcmap.fits')
        bexpmap_file = join(self.savedir, 'bexpmap.fits')
        input_srcmdl_file = join(self.savedir, 'srcmdl.xml')
        cut_ft1 = join(self.savedir, "ft1_cut.fits")

        ft2 = Gtlike.get_ft2(roi)
        ltcube = roi.sa.pixeldata.ltcube

        if self.fix_pointlike_ltcube:
            print 'Fixing pointlike ltcube %s' % ltcube
            livetime.fix_pointlike_ltcube(ltcube)

        irfs = Gtlike.get_gtlike_irfs(roi)

        if self.galactic:
            x, y, coordsys_str = roi.roi_dir.l(), roi.roi_dir.b(), 'GAL'
        else:
            x, y, coordsys_str = roi.roi_dir.ra(), roi.roi_dir.dec(), 'CEL'

        Gtlike.save_xml(roi,
                        input_srcmdl_file,
                        extended_dir_name=self.extended_dir_name)

        evfile = Gtlike.make_evfile(roi, self.savedir)

        if not os.path.exists(cut_ft1):
            if not roi.quiet: print 'Running gtselect'
            gtselect = GtApp('gtselect', 'dataSubselector')
            gtselect.run(infile=evfile,
                         outfile=cut_ft1,
                         ra=0,
                         dec=0,
                         rad=180,
                         tmin=0,
                         tmax=0,
                         emin=self.emin,
                         emax=self.emax,
                         zmax=180,
                         convtype=ct,
                         chatter=self.chatter)
        else:
            if not roi.quiet: print '... Skiping gtselect'

        if not os.path.exists(cmap_file):
            if not roi.quiet: print 'Running gtbin (ccube)'
            gtbin = GtApp('gtbin', 'evtbin')
            gtbin.run(algorithm='ccube',
                      nxpix=npix,
                      nypix=npix,
                      binsz=self.binsz,
                      evfile=cut_ft1,
                      outfile=cmap_file,
                      scfile=ft2,
                      xref=x,
                      yref=y,
                      axisrot=0,
                      proj=self.proj,
                      ebinalg='LOG',
                      emin=self.emin,
                      emax=self.emax,
                      enumbins=self.enumbins,
                      coordsys=coordsys_str,
                      chatter=self.chatter)
        else:
            if not roi.quiet: print '... Skiping gtbin (ccube)'

        if not os.path.exists(bexpmap_file):
            # Use the default binning all sky, 1deg/pixel
            if not roi.quiet: print 'Running gtexpcube'
            gtexpcube = GtApp('gtexpcube2', 'Likelihood')
            gtexpcube.run(infile=ltcube,
                          cmap='none',
                          ebinalg='LOG',
                          emin=self.emin,
                          emax=self.emax,
                          enumbins=self.enumbins,
                          outfile=bexpmap_file,
                          proj='CAR',
                          nxpix=360,
                          nypix=180,
                          binsz=1,
                          irfs=irfs,
                          coordsys=coordsys_str,
                          chatter=self.chatter)
        else:
            if not roi.quiet: print '... Skiping gtexpcube'

        if not os.path.exists(srcmap_file):
            if not roi.quiet: print 'Running gtsrcmaps'
            gtsrcmaps = GtApp('gtsrcmaps', 'Likelihood')
            gtsrcmaps.run(scfile=ft2,
                          expcube=ltcube,
                          cmap=cmap_file,
                          srcmdl=input_srcmdl_file,
                          bexpmap=bexpmap_file,
                          outfile=srcmap_file,
                          irfs=irfs,
                          rfactor=self.rfactor,
                          resample=self.resample,
                          minbinsz=self.minbinsz,
                          chatter=self.chatter)
        else:
            if not roi.quiet: print '... Skiping gtsrcmaps'

        if not roi.quiet: print 'Creating Binned LIKE'
        obs = BinnedObs(srcMaps=srcmap_file,
                        expCube=ltcube,
                        binnedExpMap=bexpmap_file,
                        irfs=irfs)

        self.like = BinnedAnalysis(binnedData=obs,
                                   srcModel=input_srcmdl_file,
                                   optimizer=self.optimizer)

        if self.enable_edisp:
            if not roi.quiet: print 'Enabeling energy dispersion'
            self.like.logLike.set_edisp_flag(True)

        if not roi.quiet: print 'Binned LIKE Created!'
示例#4
0
    def __init__(self, roi, **kwargs):
        """ Build a gtlike pyLikelihood object
            which is consistent with a pointlike roi. """
        keyword_options.process(self, kwargs)
        self.roi = roi

        if not roi.quiet: print 'Running a gtlike followup'

        self.old_dir=os.getcwd()
        if self.savedir is not None:
            self.savedata = True
            if not os.path.exists(self.savedir):
                os.makedirs(self.savedir)
        else:
            self.savedata = False
            self.savedir=mkdtemp(prefix=self.savedir_prefix)

        # put pfiles into savedir
        os.environ['PFILES']=self.savedir+';'+os.environ['PFILES'].split(';')[-1]

        if not roi.quiet: print 'Saving files to ',self.savedir

        if self.emin==None and self.emax==None and self.enumbins==None:
            self.emin,self.emax=roi.bin_edges[0],roi.bin_edges[-1]
            self.enumbins=len(roi.bin_edges)-1
        elif self.emin is not None and \
                self.emax is not None and \
                self.enumbins is not None:
            # all set
            pass
        else:
            raise Exception("emin, emax, and enumbins must all be set.")

        # Note that this formulation makes the gtlike slightly smaller than
        # the pointlike ROI (so the gtlike ROI is inside the pointlike ROI)
        roi_radius=np.degrees(max(_.radius_in_rad for _ in roi.bands))
        if self.bigger_roi:
            npix=int(math.ceil(2.0*roi_radius/self.binsz))
        else:
            npix=int(math.ceil(np.sqrt(2.0)*roi_radius/self.binsz))

        ct = roi.sa.pixeldata.conv_type

        cmap_file=join(self.savedir,'ccube.fits')
        srcmap_file=join(self.savedir,'srcmap.fits')
        bexpmap_file=join(self.savedir,'bexpmap.fits')
        input_srcmdl_file=join(self.savedir,'srcmdl.xml')
        cut_ft1=join(self.savedir,"ft1_cut.fits")


        ft2=Gtlike.get_ft2(roi)
        ltcube=roi.sa.pixeldata.ltcube 

        if self.fix_pointlike_ltcube:
            print 'Fixing pointlike ltcube %s' % ltcube
            livetime.fix_pointlike_ltcube(ltcube)
        
        irfs=Gtlike.get_gtlike_irfs(roi)

        if self.galactic:
            x,y,coordsys_str=roi.roi_dir.l(),roi.roi_dir.b(),'GAL'
        else:
            x,y,coordsys_str=roi.roi_dir.ra(),roi.roi_dir.dec(),'CEL'

        Gtlike.save_xml(roi, input_srcmdl_file, extended_dir_name=self.extended_dir_name)

        evfile=Gtlike.make_evfile(roi,self.savedir)

        if not os.path.exists(cut_ft1):
            if not roi.quiet: print 'Running gtselect'
            gtselect=GtApp('gtselect','dataSubselector')
            gtselect.run(infile=evfile,
                         outfile=cut_ft1,
                         ra=0, dec=0, rad=180,
                         tmin=0, tmax=0,
                         emin=self.emin, emax=self.emax,
                         zmax=180, convtype=ct,
                         chatter=self.chatter)
        else:
            if not roi.quiet: print '... Skiping gtselect'

        if not os.path.exists(cmap_file):
            if not roi.quiet: print 'Running gtbin (ccube)'
            gtbin=GtApp('gtbin','evtbin')
            gtbin.run(algorithm='ccube',
                      nxpix=npix, nypix=npix, binsz=self.binsz,
                      evfile=cut_ft1,
                      outfile=cmap_file,
                      scfile=ft2,
                      xref=x, yref=y, axisrot=0, proj=self.proj,
                      ebinalg='LOG', emin=self.emin, emax=self.emax, enumbins=self.enumbins,
                      coordsys=coordsys_str,
                      chatter=self.chatter)
        else:
            if not roi.quiet: print '... Skiping gtbin (ccube)'

        if not os.path.exists(bexpmap_file):
            # Use the default binning all sky, 1deg/pixel
            if not roi.quiet: print 'Running gtexpcube'
            gtexpcube=GtApp('gtexpcube2','Likelihood')
            gtexpcube.run(infile=ltcube,
                          cmap='none',
                          ebinalg='LOG', emin=self.emin, emax=self.emax, enumbins=self.enumbins,
                          outfile=bexpmap_file, proj='CAR',
                          nxpix=360, nypix=180, binsz=1,
                          irfs=irfs,
                          coordsys=coordsys_str,
                          chatter=self.chatter)
        else:
            if not roi.quiet: print '... Skiping gtexpcube'

        if not os.path.exists(srcmap_file):
            if not roi.quiet: print 'Running gtsrcmaps'
            gtsrcmaps=GtApp('gtsrcmaps','Likelihood')
            gtsrcmaps.run(scfile=ft2,
                          expcube=ltcube,
                          cmap=cmap_file,
                          srcmdl=input_srcmdl_file,
                          bexpmap=bexpmap_file,
                          outfile=srcmap_file,
                          irfs=irfs,
                          rfactor=self.rfactor,
                          resample=self.resample,
                          minbinsz=self.minbinsz,
                          chatter=self.chatter)
        else:
            if not roi.quiet: print '... Skiping gtsrcmaps'

        if not roi.quiet: print 'Creating Binned LIKE'
        obs=BinnedObs(srcMaps=srcmap_file,expCube=ltcube,binnedExpMap=bexpmap_file,irfs=irfs)

        self.like = BinnedAnalysis(binnedData=obs,srcModel=input_srcmdl_file,optimizer=self.optimizer)

        if self.enable_edisp:
            if not roi.quiet: print 'Enabeling energy dispersion'
            self.like.logLike.set_edisp_flag(True)


        if not roi.quiet: print 'Binned LIKE Created!'