示例#1
0
    def __init__(self, dataset_spec, **kwargs):
        """
        Create a new DataSet object.

        dataset_spec: str | dict
            an instance of DataSpecification with links to the FT1/FT2,
                            and/or binned data / livetime cube needed for analysis
                            (see docstring for that class) 
        """

        dataspec = self._process_dataset(
            dataset_spec,
            quiet=kwargs.get('quiet', False),
            interval=kwargs.pop('interval', None),
            #binfile=kwargs.pop('binfile',None),
        ).__dict__
        dataspec.update(
            ft1=dataspec.pop('ft1files', None),
            ft2=dataspec.pop('ft2files', None),
        )

        # replace binfile if requested (for simuation, at least)
        new_binfile = kwargs.pop('binfile', None)
        dataspec.update(kwargs)

        if new_binfile is not None:
            print 'binfile: {}'.format(new_binfile)
            if not os.path.exists(new_binfile):
                new_binfile = os.path.join(os.path.expandvars('$FERMI/data'),
                                           new_binfile)
                if not os.path.exists(new_binfile):
                    raise DataSetError(
                        'Replacement binfile not found, {}'.format(
                            new_binfile))
            dataspec['binfile'] = new_binfile

        # Now invoke the superclass to actually load the data, which may involve creating the binfile and livetime cube
        super(DataSet, self).__init__(**dataspec)
        assert self.irf is not None, 'irf was not specifed!'

        # new IRF management
        self.CALDBManager = caldb.CALDB(CALDB_dir=self.CALDB, irfname=self.irf)
        if self.exposure_cube is None:
            ltcubes = glob.glob(self.ltcube)  #allow for more than one
            self.lt = [
                skymaps.LivetimeCube(lt, weighted=False) for lt in ltcubes
            ]
            if self.use_weighted_livetime:
                self.weighted_lt = [
                    skymaps.LivetimeCube(lt, weighted=True) for lt in ltcubes
                ]
        else:
            self.lt = None
        if not self.postpone:
            self._load_binfile()
示例#2
0
 def __init__(self, ds):
     """Initialize a DataManager instance.
     
         ds: a DataSpec instance
     """
     self.dataspec = ds
     self.bpd = skymaps.BinnedPhotonData(ds.binfile)
     self.lt = skymaps.LivetimeCube(ds.ltcube, weighted=False)
     if ds.use_weighted_livetime:
         self.weighted_lt = skymaps.LivetimeCube(ds.ltcube, weighted=True)
     else:
         self.weighted_lt = None
     self.gti = self.lt.gti()  #Just to provide a reference.
示例#3
0
    def _make_ltcube(self):
        """ Generate the livetime cube."""
        #TODO -- unify weighted livetime
        import sys
        self.quiet = False
        self.ltcube = self.ltcube or self._make_default_name(prefix='lt')
        roi_info = self.dss.roi_info() if hasattr(self.dss,
                                                  'roi_info') else None
        if (roi_info is None) or (roi_info[2] > 90):
            # full sky ltcube
            roi_dir = skymaps.SkyDir(0, 0)
            exp_radius = 180
        else:
            roi_dir = skymaps.SkyDir(roi_info[0], roi_info[1])
            exp_radius = roi_info[2] + self.livetime_buffer
        if self.zenith_cut is None:
            self.zenith_cut = get_default('ZENITH_ANGLE', None)
            print 'Warning: using default zenith_cut, {}'.format(
                self.zenith_cut)
            # raise DataManException('zenith cut not specified')
        zenithcut = self.zenith_cut.get_bounds()[1]
        if not self.quiet:
            if exp_radius == 180:
                print 'Constructing all-sky livetime cube'
            else:
                print(
                    'Constructing livetime cube about RA,Dec = ({0:0.3f},{1:0.3f}) with a radius of {2:0.3f} deg.'
                    .format(roi_dir.ra(), roi_dir.dec(), exp_radius))
        for i in xrange(1 + self.use_weighted_livetime):
            #print('on iteration {0}'.format(i))
            sys.stdout.flush()
            lt = skymaps.LivetimeCube(cone_angle=exp_radius,
                                      dir=roi_dir,
                                      zcut=np.cos(np.radians(zenithcut)),
                                      pixelsize=self.livetime_pixelsize,
                                      quiet=self.quiet,
                                      weighted=i)

            for hf in self.ft2files:
                if not self.quiet:
                    print('checking FT2 file {0}...'.format(hf)),
                lt_gti = skymaps.Gti(hf, 'SC_DATA')
                if not ((lt_gti.maxValue() < self.gti.minValue()) or
                        (lt_gti.minValue() > self.gti.maxValue())):
                    lt.load(hf, self.gti)
                    if not self.quiet: print 'loaded'
                else:
                    if not self.quiet: print 'not in Gti range'

            # write out ltcube
            extension = 'WEIGHTED_EXPOSURE' if i else 'EXPOSURE'
            lt.write(self.ltcube, extension, not bool(i))
        if self.dss is not None:
            self.dss.write(self.ltcube, header_key=0)
        # write some info to livetime file
        f = pyfits.open(self.ltcube)
        f[0]._header['RADIUS'] = exp_radius
        f[0]._header['PIXSIZE'] = self.livetime_pixelsize
        f.writeto(self.ltcube, overwrite=True)
        f.close()
示例#4
0
    def get_livetime(self, pixelsize=1.0, weighted=False, clobber=True):

        gti = self.gti
        if self.ltcube is not None and os.path.exists(self.ltcube):
            try:
                lt = skymaps.LivetimeCube(self.ltcube, weighted=weighted)
                if not self.quiet:
                    print 'loaded LivetimeCube %s ' % self.ltcube
                return lt
            except RuntimeError:
                if not self.quiet:
                    ext = 'WEIGHTED_EXPOSURE' if weighted else 'EXPOSURE'
                    print(
                        'no extension %s in file %s: will generate it from the ft2files'
                        % (ext, self.ltcube))
        elif not self.quiet:
            print 'LivetimeCube file %s does not exist: will generate it from the ft2 files' % self.ltcube
        if self.roi_dir is None:
            # no roi specified: use full sky
            self.roi_dir = skymaps.SkyDir(0, 0)
            self.exp_radius = 180
        lt = skymaps.LivetimeCube(cone_angle=self.exp_radius,
                                  dir=self.roi_dir,
                                  zcut=math.cos(math.radians(self.zenithcut)),
                                  pixelsize=pixelsize,
                                  quiet=self.quiet,
                                  weighted=weighted)

        for hf in self.ft2files:
            if not self.quiet: print 'loading FT2 file %s' % hf,
            lt_gti = skymaps.Gti(hf, 'SC_DATA')
            if not ((lt_gti.maxValue() < self.gti.minValue()) or
                    (lt_gti.minValue() > self.gti.maxValue())):
                lt.load(hf, gti)

        # write out ltcube if requested
        if self.ltcube is not None:
            extension = 'WEIGHTED_EXPOSURE' if weighted else 'EXPOSURE'
            lt.write(self.ltcube, extension, clobber)
        return lt
示例#5
0
def pointlike_ltcube(evfile,scfile,outfile,dcostheta,binsz, zmax, cone_angle,dir,quiet=False):
    """ Run the pointlike ltcube code. """

    gti = skymaps.Gti(evfile)

    for weighted,clobber in [[False,True],[True,False]]:

        lt = skymaps.LivetimeCube(
            cone_angle = cone_angle,
            dir        = dir,
            zcut       = math.cos(math.radians(zmax)),
            pixelsize  = binsz,
            quiet      = quiet,
            weighted   = weighted)

        lt.load(scfile,gti)

        extension = 'WEIGHTED_EXPOSURE' if weighted else 'EXPOSURE'
        lt.write(outfile,extension,clobber)

    fix_pointlike_ltcube(outfile)