Exemplo n.º 1
0
    def phase_ltcube(ltcube,phase,savedir):

        if np.allclose(phase.phase_fraction,1):
            phased_ltcube = ltcube
        else:
            # create a temporary ltcube scaled by the phase factor
            phased_ltcube=join(savedir,'phased_ltcube.fits')
            if not os.path.exists(phased_ltcube):
                phasetools.phase_ltcube(ltcube,phased_ltcube, phase=phase)
        return phased_ltcube
Exemplo n.º 2
0
    def phase_ltcube(ltcube, phase, savedir):

        if np.allclose(phase.phase_fraction, 1):
            phased_ltcube = ltcube
        else:
            # create a temporary ltcube scaled by the phase factor
            phased_ltcube = join(savedir, 'phased_ltcube.fits')
            if not os.path.exists(phased_ltcube):
                phasetools.phase_ltcube(ltcube, phased_ltcube, phase=phase)
        return phased_ltcube
Exemplo n.º 3
0
    def time_cut(roi, tstart, tstop, subdir, use_pointlike_ltcube, verbosity):
        """ Create a new ROI given a time cut. """

        sa = roi.sa
        ds = sa.dataspec

        get_defaults=lambda obj: [k[0] for k in obj.defaults if not isinstance(k,str)]
        get_kwargs=lambda obj: {k:obj.__dict__[k] for k in get_defaults(obj)}

        ds_kwargs, sa_kwargs, roi_kwargs = map(get_kwargs,[ds,sa,roi])

        point_sources = [i.copy() for i in roi.psm.point_sources]
        diffuse_sources = [i.copy() for i in roi.dsm.diffuse_sources]

        if sa_kwargs['tstart'] !=0 or sa_kwargs['tstop'] !=0:
            raise Exception("sanity check")

        # * cut ft1file on time using gtselect

        ft2files=roi.sa.pixeldata.ft2files
        if len(ft2files) > 1: raise Exception("...")
        ft2file=ft2files[0]

        evfile=Gtlike.make_evfile(roi,subdir)

        cut_evfile=join(subdir,"cut_ft1_%s_%s.fits" % (tstart, tstop))

        if not exists(cut_evfile):
            if verbosity: print 'Running gtselect'
            gtselect=GtApp('gtselect', 'dataSubselector')
            gtselect.run(infile=evfile,
                         outfile=cut_evfile,
                         ra=0, dec=0, rad=180,
                         tmin=tstart, tmax=tstop,
                         emin=1, emax=1e7,
                         zmax=180)

        else:
            if verbosity: print '... Skiping gtselect for %s to %s' % (tstart,tstop)

        ds_kwargs['ft1files'] = cut_evfile

        # * create new binfile and ltcube
        ds_kwargs['binfile'] = join(subdir,'binned_%s_%s.fits' % (tstart, tstop))

        # save this to see if it has been phased by 
        # the function uw.utilities.phasetools.phase_ltcube
        all_time_ltcube = ds_kwargs['ltcube']

        new_ltcube = join(subdir,'ltcube_%s_%s.fits' % (tstart, tstop))

        if not exists(new_ltcube):
            if verbosity: print 'Running gtltcube for %s to %s' % (tstart,tstop)

            if use_pointlike_ltcube:
                pointlike_ltcube(evfile=cut_evfile,
                                 scfile=ft2file,
                                 outfile=new_ltcube,
                                 dcostheta=0.025, 
                                 binsz=1,
                                 zmax=roi.sa.zenithcut,
                                 cone_angle=roi.sa.exp_radius,
                                 dir=roi.roi_dir)
            else:
                gtltcube=GtApp('gtltcube', 'Likelihood')
                gtltcube.run(evfile=cut_evfile,
                             scfile=ft2file,
                             outfile=new_ltcube,
                             dcostheta=0.025, 
                             binsz=1)
        else:
            if verbosity: print '... Skiping gtltcube for %s to %s' % (tstart,tstop)

        # next, check if ltcube is phased, kind of a kluge
        f = pyfits.open(all_time_ltcube)
        if f['exposure'].header.has_key('PHASE'):
            assert f['exposure'].header['PHASE'] == f['weighted_exposure'].header['PHASE']
            # If so, phase new ltcube
            phase = f['exposure'].header['PHASE']

            phased_ltcube = join(subdir,'phased_ltcube_%s_%s.fits' % (tstart, tstop))
            if not exists(phased_ltcube):
                phase_ltcube(new_ltcube, phased_ltcube, phase)
            else:
                if verbosity: print '... Skiping gtltcube phasing for %s to %s' % (tstart,tstop)

            ds_kwargs['ltcube'] = phased_ltcube
        else:
            ds_kwargs['ltcube'] = new_ltcube

        # * create new ds, sa, and roi
        new_ds = DataSpecification(**ds_kwargs)
        sa = SpectralAnalysis(new_ds, **sa_kwargs)
        return sa.roi(
            point_sources = point_sources,
            diffuse_sources = diffuse_sources,
            **roi_kwargs)
Exemplo n.º 4
0
    def time_cut(roi, tstart, tstop, subdir, use_pointlike_ltcube, verbosity):
        """ Create a new ROI given a time cut. """

        sa = roi.sa
        ds = sa.dataspec

        get_defaults = lambda obj: [
            k[0] for k in obj.defaults if not isinstance(k, str)
        ]
        get_kwargs = lambda obj: {
            k: obj.__dict__[k]
            for k in get_defaults(obj)
        }

        ds_kwargs, sa_kwargs, roi_kwargs = map(get_kwargs, [ds, sa, roi])

        point_sources = [i.copy() for i in roi.psm.point_sources]
        diffuse_sources = [i.copy() for i in roi.dsm.diffuse_sources]

        if sa_kwargs['tstart'] != 0 or sa_kwargs['tstop'] != 0:
            raise Exception("sanity check")

        # * cut ft1file on time using gtselect

        ft2files = roi.sa.pixeldata.ft2files
        if len(ft2files) > 1: raise Exception("...")
        ft2file = ft2files[0]

        evfile = Gtlike.make_evfile(roi, subdir)

        cut_evfile = join(subdir, "cut_ft1_%s_%s.fits" % (tstart, tstop))

        if not exists(cut_evfile):
            if verbosity: print 'Running gtselect'
            gtselect = GtApp('gtselect', 'dataSubselector')
            gtselect.run(infile=evfile,
                         outfile=cut_evfile,
                         ra=0,
                         dec=0,
                         rad=180,
                         tmin=tstart,
                         tmax=tstop,
                         emin=1,
                         emax=1e7,
                         zmax=180)

        else:
            if verbosity:
                print '... Skiping gtselect for %s to %s' % (tstart, tstop)

        ds_kwargs['ft1files'] = cut_evfile

        # * create new binfile and ltcube
        ds_kwargs['binfile'] = join(subdir,
                                    'binned_%s_%s.fits' % (tstart, tstop))

        # save this to see if it has been phased by
        # the function uw.utilities.phasetools.phase_ltcube
        all_time_ltcube = ds_kwargs['ltcube']

        new_ltcube = join(subdir, 'ltcube_%s_%s.fits' % (tstart, tstop))

        if not exists(new_ltcube):
            if verbosity:
                print 'Running gtltcube for %s to %s' % (tstart, tstop)

            if use_pointlike_ltcube:
                pointlike_ltcube(evfile=cut_evfile,
                                 scfile=ft2file,
                                 outfile=new_ltcube,
                                 dcostheta=0.025,
                                 binsz=1,
                                 zmax=roi.sa.zenithcut,
                                 cone_angle=roi.sa.exp_radius,
                                 dir=roi.roi_dir)
            else:
                gtltcube = GtApp('gtltcube', 'Likelihood')
                gtltcube.run(evfile=cut_evfile,
                             scfile=ft2file,
                             outfile=new_ltcube,
                             dcostheta=0.025,
                             binsz=1)
        else:
            if verbosity:
                print '... Skiping gtltcube for %s to %s' % (tstart, tstop)

        # next, check if ltcube is phased, kind of a kluge
        f = pyfits.open(all_time_ltcube)
        if f['exposure'].header.has_key('PHASE'):
            assert f['exposure'].header['PHASE'] == f[
                'weighted_exposure'].header['PHASE']
            # If so, phase new ltcube
            phase = f['exposure'].header['PHASE']

            phased_ltcube = join(subdir,
                                 'phased_ltcube_%s_%s.fits' % (tstart, tstop))
            if not exists(phased_ltcube):
                phase_ltcube(new_ltcube, phased_ltcube, phase)
            else:
                if verbosity:
                    print '... Skiping gtltcube phasing for %s to %s' % (
                        tstart, tstop)

            ds_kwargs['ltcube'] = phased_ltcube
        else:
            ds_kwargs['ltcube'] = new_ltcube

        # * create new ds, sa, and roi
        new_ds = DataSpecification(**ds_kwargs)
        sa = SpectralAnalysis(new_ds, **sa_kwargs)
        return sa.roi(point_sources=point_sources,
                      diffuse_sources=diffuse_sources,
                      **roi_kwargs)