def get_sources(self,skydir,radius=15): """ Returns a list of ExtendedSource objects from the extended source catalog that have a center withing a distance radius of the position skydir. Note that if there are none, it returns an empty list. """ from uw.utilities.xml_parsers import parse_sources diffs = np.degrees(np.asarray([skydir.difference(d) for d in self.dirs])) mask = diffs < radius if sum(mask)==0: return [] diffs = diffs[mask] sorting = np.argsort(diffs) names = self.names[mask][sorting] xmls = self.xmls[mask][sorting] spatials = self.spatial_models[mask][sorting] sources = [] failed=False for name,xml,spatial in zip(names,xmls,spatials): full_xml=os.path.join(self.archive_directory,'XML',os.path.basename(xml)) # Use the built in xml parser to load the extended source. xmlfile=full_xml.replace(' ', '') ps,ds=parse_sources(xmlfile=xmlfile) # try: # ps,ds=parse_sources(xmlfile=xmlfile) # except Exception, msg: # print 'Source {}: Fail: {}'.format(name, msg) # failed=True # continue if len(ps) > 0: raise Exception("A point source was found in the extended source file %s" % xmlfile) if len(ds) > 1: raise Exception("No diffuse sources were found in the extended soruce file %s" % xmlfile) if len(ds) < 1: raise Exception("More than one diffuse source was found in the extended source file %s" % xmlfile) source=ds[0] if False: # don't support analytic maps spatial is not None: # replace the SpatialMap extended source with an analytic one. analytic_source = ExtendedSource(name=source.name,model=source.model, spatial_model=spatial) analytic_source.original_template = source.spatial_model.file # for reference sources.append(analytic_source) else: sources.append(source) if failed: raise Exception('Could not parse file') return sources
def get_sources(self, roi_dir, point_sources=[], diffuse_sources=[], xmlfile=None, diffdir=None, catalogs=[], include_radius=None): """ Create a list of PointSource and ROIDiffuseModel objects that are needed for the ROI. """ for ps in point_sources: if not isinstance(ps, PointSource): raise PointlikeException("Source %s is not a point source" % ps.name) if diffuse_sources is not None: for ds in diffuse_sources: if not isinstance(ds, DiffuseSource): raise PointlikeException( "Source %s is not a diffuse source" % ds.name) if xmlfile is not None: ps, ds = parse_sources( xmlfile, diffdir=diffdir, roi_dir=roi_dir, max_roi=self.maxROI + 5 if include_radius is None else include_radius) point_sources += ps diffuse_sources += ds if not isinstance(catalogs,collections.Iterable) or \ isinstance(catalogs,types.StringType): catalogs = [catalogs] for cat in catalogs: if not isinstance(cat, PointSourceCatalog): raise PointlikeException( "Catalog %s must be of type PointSourceCatalog" % cat) point_sources, diffuse_sources = cat.merge_lists( roi_dir, self.maxROI + 5 if include_radius is None else include_radius, point_sources, diffuse_sources) if point_sources == [] and not self.quiet: print 'WARNING! No point sources are included in the model.' if diffuse_sources == [] and not self.quiet: print 'WARNING! No diffuse sources are included in the model.' return point_sources, diffuse_sources
def prepareIC(roi,j,mass,final_state,tmpdir,specDir): if not final_state in ["ee","mumu"]: raise Exception("IC only implemented for ee, mumu") ### assembles the necessary file spectrum for IC emission! ### # first bit: build sources from uw.utilities.xml_parsers import parse_sources, write_sources from uw.like.scalemodels import ScaleFactorFileFunction ps,ds=parse_sources(roi.modelxml) # requires to pass the sources into the pointlike format! # get rid of virgo in the source model # is in the ds section new_ds = [s for s in ds if not s.name == "Virgo"] virgo = [s for s in ds if s.name == "Virgo"][0] # now modify the virgo source # that way the scale factor can be identified as <sigmav> filename = None if final_state in ["ee","mumu"]: if final_state == "ee": # e+/e- final state if not mass in eeIC.keys(): raise Exception("Could not find mass point") filename = os.path.join(specDir,eeIC[mass]) elif final_state == "mumu": # mu+/mu- final state if not mass in mumuIC.keys(): raise Exception("Could not find mass point") filename = os.path.join(specDir,mumuIC[mass]) j*=effectiveJ(mass,finalState=final_state) # include the IC correction if not os.path.isfile(filename): raise IOError("could not find file spectrum %s"%filename) if filename is None: raise Exception("could not find associated file") model = ScaleFactorFileFunction(ScaleFactor=1,normalization=norm(mass,j),file=filename) model.set_limits("Normalization",0,1e30) model.set_limits("ScaleFactor",0,1e8) model.setp(0,1) model.setp(1,norm(mass,j)) model.free = np.array([True,False]) virgo.model = model # replace DMFit model with file spectrum ds = new_ds ds.append(virgo) ofile = os.path.join(tmpdir,"%s_%s_%sGeV.xml"%(roi.name,final_state,"%1.0f"%mass)) write_sources(ps,ds, ofile, strict=True, convert_extended=False, extended_dir_name=None, expand_env_vars=True) print '*INF* wrote temporary xml with IC: %s'%ofile return ofile
def convert(input, output, emin, emax): if (output == 'None'): output = input[:-4] + '_simlibrary.xml' print 'Using default output name:', output ps, ds = parse_sources(input) sources = ps sources.extend(ds) directory = os.path.dirname(output) if (directory == ''): directory = os.path.abspath('.') else: directory = os.path.abspath(directory) pass mc = uw.like.roi_monte_carlo.MCModelBuilder(sources, savedir=directory, emin=float(emin), emax=float(emax)) mc.build(output) print 'File saved as', output return output
def main(inputfile,tag,idstart=None,idstop=None,debug=False,diffdir=None,tstart=0, tstop=8000, ft2file='None', emin=100, emax=1e5, seed=1000,irf='P7SOURCE_V6', dry_run=True, gtifile=None, zmax=None): if not diffdir is None: diffdir = diffdir.replace("~",os.getenv('HOME')) fid = verifyFile(inputfile) catalogxml = None if fid == 1: catalogxml = inputfile elif fid == 2: catalogxml = cFITS2XML(inputfile) if not os.getenv("LSB_JOBID")==None: Outdir = NamedTemporaryFile(dir='/scratch/').name # data is stored in Outdir/raw else: Outdir = NamedTemporaryFile(dir='/tmp/').name # data is stored in Outdir/raw print '*OUTDIR* %s'%Outdir Emin = 10 Emax = 1e6 print '*INFO*: current pfile settings',os.getenv('PFILES'); # AllSky by default, need a ROI object to proceed os.mkdir(Outdir) modXML = AddCatalog(inputfile,idstart,idstop,Outdir,diffdir=diffdir) # now we check whether we have files that refer to nowhere checkXML(modXML) ps,ds = None,None ps,ds=parse_sources(modXML) sources = ps+ds # this is only necessary to make use of the roi_montecarlo.py code montecarlo_object = MonteCarlo( # ST-09-29-00 sources = sources, gtifile=gtifile, zmax = zmax, tempbase = Outdir, irf = irf, ft1 = Outdir+'/raw/ft1.fits', ft2 = ft2file, tstart = tstart, tstop = tstop, emin = emin, emax = emax, seed = seed, savedir = Outdir+'/raw/' ) montecarlo_object.simulate(dry_run=dry_run) # now assemble list of files # check if we have parts tags = tag.split(";") storedir = os.getenv("SKYMODEL_DIR")+"/" for t in tags: storedir+="/"+t print '*INFO* store data here: %s'%storedir files = os.listdir(os.path.join(Outdir,'raw')) try: os.mkdir(storedir) except OSError: print '*WARNING* dir %s exists already, overwriting'%storedir removePath(storedir) # now move for f in files: infile = os.path.join(Outdir+'/raw/',f) outfile= os.path.join(storedir,f) move(infile,outfile) # finally, clean up removePath(Outdir)
class ExtendedCatalog(ExtendedSourceCatalog): """ subclass to add this lookup function TODO: merge, keeping only needed features if a folder 'XML2' is found, add sources to it. Must be analytic I guess. """ def __init__(self, extended_catalog_name, force_map=False, **kwargs): """ initialize by also filling an array with all source spectral models""" self.alias = kwargs.pop('alias', dict()) self.quiet = kwargs.pop('quiet', True) self.catname = extended_catalog_name if os.path.isabs(self.catname): extended_catalog_name = self.catname else: extended_catalog_name = \ os.path.expandvars(os.path.join('$FERMI','catalog',extended_catalog_name)) if not os.path.exists(extended_catalog_name): raise Exception('extended source folder "%s" not found' % extended_catalog_name) if not self.quiet: print 'Loaded extended catalog %s' % extended_catalog_name super(ExtendedCatalog, self).__init__(extended_catalog_name, force_map=force_map) #**kwargs) # create list of sources using superclass, for lookup by name self.sources = [ self.get_sources(self.dirs[i], 0.1)[0] for i in range(len(self.names)) ] fail = False for source, spatial_model in zip(self.sources, self.spatial_models): model = source.model try: if model.mappers[0].__class__.__name__ == 'LimitMapper': #print 'converting mappers for model for source %s, model %s' % (source.name, model.name) source.model = eval( 'Models.%s(p=%s)' % (model.name, list(model.get_all_parameters()))) except Exception, msg: fail = True print 'Failed to parse {} spectrum : {}'.format( source.name, msg) # Replace the spatial model with the one specified by the FITS catalog list dmodel = source.dmodel if spatial_model.name != 'SpatialMap': assert spatial_model.center.difference(dmodel[0].center)<0.001,\ 'Center discrepance for {}: {},{}'.format(source.name, spatial_model.center, dmodel[0].center) source.dmodel[0] = source.spatial_model = spatial_model if fail: raise Exception('Parse error(s)') # Now check for XML2 ff = glob.glob('{}/XML2/*.xml'.format(extended_catalog_name)) #print '{} sources found in folder {}:'.format(len(ff), 'XML2') if len(ff) == 0: return for f in ff: ps, ds = parse_sources(f) assert len(ps) == 0 and len( ds ) == 1, 'expected a single extended source in file {}'.format(f) s = ds[0] name = s.name if name[4] == 'J': # insert space name = name[:4] + ' ' + name[4:] s.name = name i = self.lookup(name) if i is not None: if not self.quiet: print 'replacing spectral model for {}'.format(name) self.sources[i].dmodel = s.dmodel else: if not self.quiet: print 'adding extended source {} at {}'.format( s.name, s.skydir) self.names = np.append(self.names, name) self.sources.append(s)
def run(**kwargs): if (not available): raise RuntimeError( "The command gtconvertxmlmodel.py is not currently usable with public Science Tools" ) if (len(kwargs.keys()) == 0): #Nothing specified, the user needs just help! thisCommand.getHelp() return pass #Get parameters values thisCommand.setParValuesFromDictionary(kwargs) try: xmlmodel = thisCommand.getParValue('likexmlresults') Emin = float(thisCommand.getParValue('emin')) Emax = float(thisCommand.getParValue('emax')) xmlsimmodel = thisCommand.getParValue('xmlsimmodel') clobber = _yesOrNoToBool(thisCommand.getParValue('clobber')) verbose = _yesOrNoToBool(thisCommand.getParValue('verbose')) except KeyError as err: print("\n\nERROR: Parameter %s not found or incorrect! \n\n" % (err.args[0])) #Print help print thisCommand.getHelp() return from GtBurst import dataHandling irf = dataHandling._getParamFromXML(xmlmodel, 'IRF') ra = dataHandling._getParamFromXML(xmlmodel, 'RA') dec = dataHandling._getParamFromXML(xmlmodel, 'DEC') name = dataHandling._getParamFromXML(xmlmodel, 'OBJECT') if (irf == None): print( "\n\nWARNING: could not read IRF from XML file. Be sure you know what you are doing..." ) sourceList = xmlmodel.replace('.xml', '.txt') #Quick fix: MCModelBuilder cannot integrate a isotropic model if it has not a normalization of 1 #We will edit the XML model, put temporarily the normalization of the IsotropicTemplate to 1, #convert the XML, then multiply the output normalization by the factor contained at the beginning tmpxml = "__temp__xmlmodel.xml" shutil.copy(xmlmodel, tmpxml) originalNorm = dataHandling.getIsotropicTemplateNormalization(xmlmodel) if (originalNorm != None or originalNorm != 1): dataHandling.setIsotropicTemplateNormalization(tmpxml, 1) else: #Either the template is not in the XML file (possibly because the user used Transient class), #or it is already 1, nothing to do originalNorm = 1 pass ps, ds = parse_sources(tmpxml) sources = ps sources.extend(ds) mc = uw.like.roi_monte_carlo.MCModelBuilder(sources, savedir='.', emin=Emin, emax=Emax) mc.build(xmlsimmodel) dataHandling.multiplyIsotropicTemplateFluxSim(xmlsimmodel, originalNorm) os.remove(tmpxml) txt = '' for x in sources: txt += x.name.replace('2FGL ', '_2FGL_').replace('-', 'm').replace( '.', '') + '\n' file(sourceList, 'w').writelines(txt) lines = file(xmlsimmodel, 'r').readlines() newlines = '' pwd = os.environ['PWD'] for l in lines: newlines += l.replace('$($SIMDIR)', pwd) pass file(xmlsimmodel, 'w').writelines(newlines) if (irf != None): dataHandling._writeParamIntoXML(xmlsimmodel, IRF=irf, OBJECT=name, RA=ra, DEC=dec) pass return 'xmlsimmodel', xmlsimmodel, 'srclist', sourceList
def run(**kwargs): if(not available): raise RuntimeError("The command gtconvertxmlmodel.py is not currently usable with public Science Tools") if(len(kwargs.keys())==0): #Nothing specified, the user needs just help! thisCommand.getHelp() return pass #Get parameters values thisCommand.setParValuesFromDictionary(kwargs) try: xmlmodel = thisCommand.getParValue('likexmlresults') Emin = float(thisCommand.getParValue('emin')) Emax = float(thisCommand.getParValue('emax')) xmlsimmodel = thisCommand.getParValue('xmlsimmodel') clobber = _yesOrNoToBool(thisCommand.getParValue('clobber')) verbose = _yesOrNoToBool(thisCommand.getParValue('verbose')) except KeyError as err: print("\n\nERROR: Parameter %s not found or incorrect! \n\n" %(err.args[0])) #Print help print thisCommand.getHelp() return from GtBurst import dataHandling irf = dataHandling._getParamFromXML(xmlmodel,'IRF') ra = dataHandling._getParamFromXML(xmlmodel,'RA') dec = dataHandling._getParamFromXML(xmlmodel,'DEC') name = dataHandling._getParamFromXML(xmlmodel,'OBJECT') if(irf is None): print("\n\nWARNING: could not read IRF from XML file. Be sure you know what you are doing...") sourceList = xmlmodel.replace('.xml','.txt') #Quick fix: MCModelBuilder cannot integrate a isotropic model if it has not a normalization of 1 #We will edit the XML model, put temporarily the normalization of the IsotropicTemplate to 1, #convert the XML, then multiply the output normalization by the factor contained at the beginning tmpxml = "__temp__xmlmodel.xml" shutil.copy(xmlmodel,tmpxml) originalNorm = dataHandling.getIsotropicTemplateNormalization(xmlmodel) if(originalNorm is not None or originalNorm!=1): dataHandling.setIsotropicTemplateNormalization(tmpxml,1) else: #Either the template is not in the XML file (possibly because the user used Transient class), #or it is already 1, nothing to do originalNorm = 1 pass ps,ds = parse_sources(tmpxml) sources = ps sources.extend(ds) mc = uw.like.roi_monte_carlo.MCModelBuilder(sources,savedir='.',emin=Emin,emax=Emax) mc.build(xmlsimmodel) dataHandling.multiplyIsotropicTemplateFluxSim(xmlsimmodel,originalNorm) os.remove(tmpxml) txt='' for x in sources: txt += x.name.replace('2FGL ','_2FGL_').replace('-','m').replace('.','')+'\n' file(sourceList,'w').writelines(txt) lines = file(xmlsimmodel,'r').readlines() newlines ='' pwd = os.environ['PWD'] for l in lines: newlines += l.replace('$($SIMDIR)',pwd) pass file(xmlsimmodel,'w').writelines(newlines) if(irf is not None): dataHandling._writeParamIntoXML(xmlsimmodel,IRF=irf,OBJECT=name,RA=ra,DEC=dec) pass return 'xmlsimmodel', xmlsimmodel, 'srclist', sourceList