def diffusefun(gal ='ring_24month_P74_v1.fits', energy=1000, nside=512): """ an HParray object wrapping the galactic diffuse """ f = os.path.expandvars(os.path.join('$FERMI', 'diffuse', gal)) t = skymaps.DiffuseFunction(f) def skyplotfun(skydir): return t.value(skydir, energy) return HPskyfun('galactic', skyplotfun, nside=nside)
def mapcube_to_healpix(inputfile, suffix='_nside256_bpd4', inpath= '$FERMI/diffuse', outpath=None, energy_bins=np.logspace(1.75,6.5, 20), emin=58.5 ): """ convert a MapCube to column format, consistent with diffuse group """ fullinputfile = os.path.expandvars(os.path.join(inpath,inputfile)) assert os.path.exists(fullinputfile), 'File not found: %s' % fullinputfile d = skymaps.DiffuseFunction(fullinputfile) galbands = [] energies = np.sqrt( energy_bins[:-1] * energy_bins[1:] ) for elow, ehigh in zip(energy_bins[:-1], energy_bins[1:]): print elow,ehigh d.setEnergyRange(max(elow, emin), ehigh) t = HPskyfun('e_%d'%np.sqrt(elow*ehigh), d, 256) t.setcol() t.vec /= (ehigh-elow) galbands.append(t) tt = HEALPixFITS(galbands) flux_table = tt.make_table() energy_column = pyfits.Column('MeV', format='E', unit='MeV', array=energies) energy_table = pyfits.new_table([energy_column]) energy_table.name='ENERGIES' hdus = [ pyfits.PrimaryHDU(header=None), flux_table, energy_table] fulloutfile = os.path.expandvars(os.path.join(outpath if outpath is not None else inpath, inputfile.replace('.fits', suffix+'.fits'))) if os.path.exists(fulloutfile): os.remove(fulloutfile) print 'writing output file %s' %fulloutfile pyfits.HDUList(hdus).writeto(fulloutfile)
def make_exposure(): if dataset.exposure_cube is not None: ## use pregenerated gtexpcube2 cube; turn off interpolation return [ skymaps.DiffuseFunction(f, 1000., False) for f in dataset.exposure_cube ] skymaps.EffectiveArea.set_CALDB(dataset.CALDBManager.CALDB) skymaps.Exposure.set_cutoff(np.cos(np.radians(dataset.thetacut))) inst = ['front', 'back'] aeff_files = dataset.CALDBManager.get_aeff() ok = [os.path.exists(file) for file in aeff_files] if not all(ok): raise DataSetError('one of CALDB aeff files not found: %s' % aeff_files) self.ea = [skymaps.EffectiveArea('', file) for file in aeff_files] if dataset.verbose: print ' -->effective areas at 1 GeV: ', \ ['%s: %6.1f'% (inst[i],self.ea[i](1000)) for i in range(len(inst))] if dataset.use_weighted_livetime and hasattr( dataset, 'weighted_lt'): return [ skymaps.Exposure(dataset.lt, dataset.weighted_lt, ea) for ea in self.ea ] else: return [skymaps.Exposure(dataset.lt, ea) for ea in self.ea]
def __init__(self,livetime,aeff, correction=None, weighted_livetime=None, exposure_cube=None, cthetamin=.4): self.aeff = aeff self.lt = livetime self.weighted_lt = weighted_livetime if correction is not None: self.correction = correction else: self.correction = lambda x: 1.0 if exposure_cube is not None: self._cpp_exposure = skymaps.DiffuseFunction(exposure_cube,1000.,False) else: skymaps.Exposure.set_cutoff(cthetamin) if self.weighted_lt is None: self._cpp_exposure = skymaps.Exposure(self.lt, self.aeff) else: self._cpp_exposure = skymaps.Exposure(self.lt,self.weighted_lt,self.aeff)
def make_exposure(): if dataset.exposure_cube is not None: ## use pregenerated gtexpcube2 cube; turn off interpolation return [ skymaps.DiffuseFunction(f, 1000., False) for f in dataset.exposure_cube ] type_names = datadict.get('type_names', ('FRONT', 'BACK')) if type_names == 'psf': type_names = ['PSF{}'.format(i) for i in range(4)] skymaps.EffectiveArea.set_CALDB(dataset.CALDBManager.CALDB) skymaps.Exposure.set_cutoff(np.cos(np.radians(dataset.thetacut))) aeff_files = dataset.CALDBManager.get_aeff() ok = [os.path.exists(file) for file in aeff_files] if not all(ok): raise DataSetError('one of CALDB aeff files not found: %s' % aeff_files) if pyfits.open(aeff_files[0])[1].name != 'EFFECTIVE AREA': # new format with combined self.ea = [ skymaps.EffectiveArea('', file, 'EFFECTIVE AREA_' + name) for file, name in zip(aeff_files, type_names) ] else: self.ea = [ skymaps.EffectiveArea('', file) for file in aeff_files ] if dataset.verbose: print ' -->effective areas at 1 GeV: ', \ ['%s: %6.1f'% (type_names[i],self.ea[i](1000)) for i in range(len(type_names))] if dataset.use_weighted_livetime and hasattr( dataset, 'weighted_lt'): return [ skymaps.Exposure(dataset.lt, dataset.weighted_lt, ea) for ea in self.ea ] else: return [skymaps.Exposure(dataset.lt, ea) for ea in self.ea]