Пример #1
0
 def _createObservation(self, srcMaps, expCube, irfs):
     self._respFuncs = pyLike.ResponseFunctions()
     self._respFuncs.load_with_event_types(irfs, "", self.srcMaps, "")
     self._expMap = pyLike.ExposureMap()
     self._scData = pyLike.ScData()
     self._roiCuts = pyLike.RoiCuts()
     self._roiCuts.readCuts(srcMaps, "", False)
     self._expCube = pyLike.ExposureCube()
     self._expCube.readExposureCube(expCube)
     self._expCube.setEfficiencyFactor(self._respFuncs.efficiencyFactor())
     self._eventCont = pyLike.EventContainer(self._respFuncs, self._roiCuts,
                                             self._scData)
     # EAC: Use AppHelpers to get the right type of BinnedExposure map
     self._bexpmap = pyLike.AppHelpers_readBinnedExposure(self.binnedExpMap)
     if self.phased_expmap is not None:
         self._phased_expmap = pyLike.WcsMap2(self.phased_expmap)
         self.observation = pyLike.Observation(self._respFuncs,
                                               self._scData, self._roiCuts,
                                               self._expCube, self._expMap,
                                               self._eventCont,
                                               self._bexpmap,
                                               self._phased_expmap)
     else:
         self.observation = pyLike.Observation(self._respFuncs,
                                               self._scData, self._roiCuts,
                                               self._expCube, self._expMap,
                                               self._eventCont,
                                               self._bexpmap)
     self._meanPsf = pyLike.MeanPsf(self.countsMap.refDir(),
                                    self.countsMap.energies(),
                                    self.observation)
     self.observation.setMeanPsf(self._meanPsf)
Пример #2
0
 def __init__(self,
              eventFile=None,
              scFile=None,
              expMap=None,
              expCube=None,
              irfs='TEST',
              checkCuts=True):
     self.checkCuts = checkCuts
     if eventFile is None and scFile is None:
         eventFile, scFile, expMap, expCube, irfs = self._obsDialog()
     if checkCuts:
         self._checkCuts(eventFile, expMap, expCube)
     self._inputs = eventFile, scFile, expMap, irfs
     self._respFuncs = pyLike.ResponseFunctions()
     self._respFuncs.load(irfs)
     self._expMap = pyLike.ExposureMap()
     if expMap is not None and expMap is not "":
         self._expMap.readExposureFile(expMap)
     self._scData = pyLike.ScData()
     self._roiCuts = pyLike.RoiCuts()
     self._expCube = pyLike.ExposureCube()
     if expCube is not None and expCube is not "":
         self._expCube.readExposureCube(expCube)
     self._eventCont = pyLike.EventContainer(self._respFuncs, self._roiCuts,
                                             self._scData)
     self.observation = pyLike.Observation(self._respFuncs, self._scData,
                                           self._roiCuts, self._expCube,
                                           self._expMap, self._eventCont)
     self._readData(scFile, eventFile)
Пример #3
0
 def __init__(self,
              eventFile=None,
              scFile=None,
              expMap=None,
              expCube=None,
              irfs=None,
              checkCuts=True,
              sctable='SC_DATA'):
     self.sctable = sctable
     self.checkCuts = checkCuts
     if eventFile is None and scFile is None:
         eventFile, scFile, expMap, expCube, irfs = self._obsDialog()
     if checkCuts:
         self._checkCuts(eventFile, expMap, expCube)
     self.expMap = expMap
     self.expCube = expCube
     if irfs is None or irfs == 'CALDB':
         evfiles = self._fileList(eventFile)
         my_cuts = pyLike.Cuts(evfiles[0], "EVENTS", False, True, True)
         self.irfs = my_cuts.CALDB_implied_irfs()
     else:
         self.irfs = irfs
     self._inputs = '\n'.join(
         ('Event file(s): ' + str(eventFile),
          'Spacecraft file(s): ' + str(scFile),
          'Exposure map: ' + str(expMap), 'Exposure cube: ' + str(expCube),
          'IRFs: ' + str(irfs)))
     self._respFuncs = pyLike.ResponseFunctions()
     evfiles = self._fileList(eventFile)
     self._respFuncs.load_with_event_types(self.irfs, "", evfiles[0],
                                           "EVENTS")
     self._expMap = pyLike.ExposureMap()
     if expMap is not None and expMap != "":
         self._expMap.readExposureFile(expMap)
     self._scData = pyLike.ScData()
     self._roiCuts = pyLike.RoiCuts()
     self._expCube = pyLike.ExposureCube()
     if expCube is not None and expCube != "":
         self._expCube.readExposureCube(expCube)
     self._expCube.setEfficiencyFactor(self._respFuncs.efficiencyFactor())
     self._eventCont = pyLike.EventContainer(self._respFuncs, self._roiCuts,
                                             self._scData)
     self.observation = pyLike.Observation(self._respFuncs, self._scData,
                                           self._roiCuts, self._expCube,
                                           self._expMap, self._eventCont)
     self._readData(scFile, eventFile)
Пример #4
0
    def __init__(
        self,
        name,
        eventFile,
        ft2File,
        livetimeCube,
        kind,
        exposureMap=None,
        sourceMaps=None,
        binnedExpoMap=None,
        source_name=None,
    ):

        # Initially the nuisance parameters dict is empty, as we don't know yet
        # the likelihood model. They will be updated in set_model

        super(FermiLATLike, self).__init__(name, {})

        # Read the ROI cut
        cc = pyLike.RoiCuts()
        cc.readCuts(eventFile, "EVENTS")
        self.ra, self.dec, self.rad = cc.roiCone()

        # Read the IRF selection
        c = pyLike.Cuts(eventFile, "EVENTS")
        self.irf = c.CALDB_implied_irfs()

        self.ft2File = ft2File
        self.livetimeCube = livetimeCube

        # These are the boundaries and the number of energies for the computation
        # of the model (in keV)
        self.emin = 1e4
        self.emax = 5e8
        self.Nenergies = 200

        # This is the limit on the effective area correction factor,
        # which is a multiplicative factor in front of the whole model
        # to account for inter-calibration issues. By default it can vary
        # by 10%. This can be changed by issuing:
        # FermiLATUnbinnedLikeInstance.effCorrLimit = [new limit]
        # where for example a [new limit] of 0.2 allow for an effective
        # area correction up to +/- 20 %

        self.effCorrLimit = 0.1

        if kind.upper() != "UNBINNED" and kind.upper() != "BINNED":

            raise RuntimeError("Accepted values for the kind parameter are: " +
                               "binned, unbinned. You specified: %s" % (kind))

        else:

            self.kind = kind.upper()

        if kind.upper() == "UNBINNED":

            assert exposureMap is not None, "You have to provide an exposure map"

            self.eventFile = eventFile
            self.exposureMap = exposureMap
            # Read the files and generate the pyLikelihood object
            self.obs = UnbinnedAnalysis.UnbinnedObs(
                self.eventFile,
                self.ft2File,
                expMap=self.exposureMap,
                expCube=self.livetimeCube,
                irfs=self.irf,
            )

        elif kind.upper() == "BINNED":

            assert sourceMaps is not None, "You have to provide a source map"
            assert (
                binnedExpoMap
                is not None), "You have to provided a (binned) exposure map"

            self.sourceMaps = sourceMaps
            self.binnedExpoMap = binnedExpoMap

            self.obs = BinnedAnalysis.BinnedObs(
                srcMaps=self.sourceMaps,
                expCube=self.livetimeCube,
                binnedExpMap=self.binnedExpoMap,
                irfs=self.irf,
            )
        pass

        # Activate inner minimization by default
        self.setInnerMinimization(True)

        self._source_name = source_name
Пример #5
0
    def __init__(self,
                 original_ft1,
                 original_ft2,
                 path_of_tar_file_with_simulated_ft1_files,
                 workdir="simulated_ft1s"):

        # Make absolute path and resolve env. variables (if any)

        original_ft1 = sanitize_filename(original_ft1)
        original_ft2 = sanitize_filename(
            original_ft2
        )  # This is needed only if we want to switch back to gtmktime
        path_of_tar_file_with_simulated_ft1_files = sanitize_filename(
            path_of_tar_file_with_simulated_ft1_files)

        # Read from the original FT1 the cuts
        roi_cuts = pyLike.RoiCuts()
        roi_cuts.readCuts(original_ft1)

        # ROI definition

        ra_center, dec_center, radius = roi_cuts.roiCone()

        # Store them as well
        self._ra_center = ra_center
        self._dec_center = dec_center
        self._radius = radius

        # Energy minimum and maximum
        emin, emax = roi_cuts.getEnergyCuts()

        with pyfits.open(original_ft1) as f:

            tstart = f['EVENTS'].header['TSTART']
            tstop = f['EVENTS'].header['TSTOP']

        # Unpack tar file here
        with within_directory(workdir, create=True):

            # Copy tar here, unpack, then remove copy
            log.info("Copying %s to %s..." %
                     (path_of_tar_file_with_simulated_ft1_files, workdir))
            shutil.copy2(path_of_tar_file_with_simulated_ft1_files, ".")

            execute_command(
                log, "tar xvf %s" % path_of_tar_file_with_simulated_ft1_files)

            os.remove(
                os.path.basename(path_of_tar_file_with_simulated_ft1_files))

            # Now get the names of all ft1s
            all_ft1s_raw = glob.glob("gll_ft1_tr_bn*_v00.fit")

            log.info(
                "Found %s simulated FT1 files in archive %s" %
                (len(all_ft1s_raw), path_of_tar_file_with_simulated_ft1_files))

            log.info("Filtering them with the same cuts as in %s" %
                     (original_ft1))

            self._all_ft1s = []

            # Apply the cuts to them
            for i, this_simulated_ft1 in enumerate(all_ft1s_raw):

                if (i + 1) % 100 == 0:

                    log.info("Processed %i of %i" % (i + 1, len(all_ft1s_raw)))

                # temp_file1 = "__temp_ft1.fit"
                #
                # self.gtmktime_from_file(original_ft1, original_ft2, this_simulated_ft1, temp_file1)
                #
                # temp_file2 = "__temp_ft1_2.fit"
                #
                # self.gtselect(ra_center, dec_center, radius, tstart, tstop, emin, emax, temp_file1, temp_file2)
                #
                # os.remove(temp_file1)
                #
                basename = os.path.splitext(
                    os.path.basename(this_simulated_ft1))[0]

                new_name = "%s_filt.fit" % basename

                self._filter_simulated_ft1(original_ft1, this_simulated_ft1,
                                           ra_center, dec_center, radius,
                                           tstart, tstop, emin, emax, new_name)

                # os.rename(temp_file2, new_name)

                self._all_ft1s.append(sanitize_filename(new_name))

                # Remove the simulated FT1 to save space

                os.remove(this_simulated_ft1)