def resolve_fits_files(infile): '''This function returns a properly formatted infile loaation since most of the analysis is done in subdirectories and the actual support files are in ../.''' infile = os.path.abspath(infile) foo = infile.strip('@') try: pyfits.open(foo) except IOError: return [item.strip() for item in open(foo)] return [infile]
def getInteractiveFigureFromCSPEC(cspecfile,**kwargs): trigTime = dataHandling.getTriggerTime(cspecfile) f = pyfits.open(cspecfile) if('DETNAM' in f['SPECTRUM'].header.keys()): detector = transformGBMdetname(f['SPECTRUM'].header['DETNAM']) elif('LLECUT' in f['SPECTRUM'].header.keys()): detector = "LAT" elif('TELESCOP' in f['SPECTRUM'].keys()): detector = f['SPECTRUM'].header['TELESCOP'] else: detector = 'Unknown' pass s = f['SPECTRUM'] d = s.data[(s.data.field('QUALITY')==0)] #Avoid overflow and underflow channels counts = d.field('COUNTS')[:,3:126] met = d.field('TIME')-trigTime exposure = d.field('EXPOSURE') N = len(met) LC = np.zeros(N) for i in range(N): LC[i]=counts[i].sum() mask = (exposure > 0) LC[mask] /= exposure[mask] kwargs['xlabel'] = "Time since trigger" kwargs['ylabel'] = "Counts/s" kwargs['xoffset'] = trigTime kwargs['title'] = detector interactiveFigure = InteractiveFigure(met,LC,**kwargs) f.close() return interactiveFigure
def gtdiffrsp_mp(bins, SCFile, EVFile, OutFile, SaveTemp, SrcModel,IRF): '''This function looks at the start and stop times in an event file and splits the time into chunks. It then submits jobs based upon those start and stop times.''' print "Opening event file to determine break points..." hdulist = pyfits.open(EVFile) tstart = hdulist[0].header['TSTART'] tstop = hdulist[0].header['TSTOP'] hdulist.close() starts, step = np.linspace(tstart,tstop,bins,endpoint=False, retstep=True) stops = starts + step scfiles = [SCFile for st in starts] evfiles = [EVFile for st in starts] srcmdls = [SrcModel for st in starts] irfs = [IRF for st in starts] pool = Pool(processes=bins) times = np.array([starts,stops,scfiles,evfiles,srcmdls,irfs]) print "Spawning {} jobs...".format(bins) tempfilenames = pool.map(diffrsp,times.transpose()) print "Combining temporary files..." eventsum(tempfilenames, OutFile, SaveTemp)
def __init__(self, filteredFt1, ft2, triggerName, triggertime, outdir): global active if (active): self.ft1 = filteredFt1 self.ft2 = ft2 #Read the cuts from the filtered ft1 file f = pyfits.open(self.ft1) h = f[0].header self.ra = float(h['_ROI_RA']) self.dec = float(h['_ROI_DEC']) self.roi = float(h['_ROI_RAD']) self.emin = float(h['_EMIN']) self.emax = float(h['_EMAX']) self.zmax = float(h['_ZMAX']) self.irf = str(h['_IRF']) f.close() self.triggertime = triggertime self.grbname = triggerName self.outdir = outdir else: raise RuntimeError( "BKGE is not available in your system, you cannot use it!")
def getInteractiveFigureFromCSPEC(cspecfile, **kwargs): trigTime = dataHandling.getTriggerTime(cspecfile) f = pyfits.open(cspecfile) if ('DETNAM' in f['SPECTRUM'].header.keys()): detector = transformGBMdetname(f['SPECTRUM'].header['DETNAM']) elif ('LLECUT' in f['SPECTRUM'].header.keys()): detector = "LAT" elif ('TELESCOP' in f['SPECTRUM'].keys()): detector = f['SPECTRUM'].header['TELESCOP'] else: detector = 'Unknown' pass s = f['SPECTRUM'] d = s.data[(s.data.field('QUALITY') == 0)] #Avoid overflow and underflow channels counts = d.field('COUNTS')[:, 3:126] met = d.field('TIME') - trigTime exposure = d.field('EXPOSURE') N = len(met) LC = np.zeros(N) for i in range(N): LC[i] = counts[i].sum() mask = (exposure > 0) LC[mask] /= exposure[mask] kwargs['xlabel'] = "Time since trigger" kwargs['ylabel'] = "Counts/s" kwargs['xoffset'] = trigTime kwargs['title'] = detector interactiveFigure = InteractiveFigure(met, LC, **kwargs) f.close() return interactiveFigure
def expsum(filenames, Outfile, SaveTemp): '''This function takes a list of exposure maps and adds them together. If there is only one file to be added, it just copies it to the outfile. If there is more than one, it uses pyfits to open them all up, sum the first hdus (the data) and then replaces the first file's primary hdu with this summed data and writes it to the output file.''' if len(filenames) <= 1: subprocess.call(["cp", filenames[0], Outfile]) else: expmap_files = [pyfits.open(filename) for filename in filenames] summed_expmap_hdu = (np.array([expmap_file[0].data for expmap_file in expmap_files])).sum(axis=0) expmap_files[0][0].data = summed_expmap_hdu expmap_files[0][0].update_header() expmap_files[0].writeto(Outfile, clobber='True') for expmap_file in expmap_files: expmap_file.close() if SaveTemp: print "Did not delete the following temporary files:" print filenames else: print "Deleting temporary files..." for filename in filenames: os.remove(filename)
def fitsToPNG(fitsfile, pngfile, vmin=None, vmax=None, **kwargs): #Display the TS map #figure = plt.figure() tsfig = aplpy.FITSFigure(fitsfile, convention='calabretta') tsfig.set_tick_labels_font(size='small') tsfig.set_axis_labels_font(size='small') if (vmin is not None and vmax is not None): tsfig.show_colorscale(cmap='gist_heat', aspect='auto', vmin=float(vmin), vmax=float(vmax)) else: #Get maximum of image f = pyfits.open(fitsfile) maximum = f[0].data.max() f.close() tsfig.show_colorscale(cmap='gist_heat', aspect='auto', vmin=0, vmax=float(maximum)) # Modify the tick labels for precision and format tsfig.tick_labels.set_xformat('ddd.dd') tsfig.tick_labels.set_yformat('ddd.dd') # Display a grid and tweak the properties tsfig.show_grid() tsfig.add_colorbar() if ('sources' in kwargs.keys()): sources = kwargs['sources'] for src in sources: tsfig.add_label(float(src[1]), float(src[2]), "%s" % src[0], relative=False, weight='bold', color='green', size='x-small', verticalalignment='top', horizontalalignment='left') tsfig.show_markers([float(src[1])], [float(src[2])], edgecolor='green', marker='x') pass if ('ra' in kwargs.keys()): ra = float(kwargs['ra']) dec = float(kwargs['dec']) tsfig.show_markers([ra], [dec], edgecolor='cyan', facecolor='cyan', marker='x', s=120, alpha=0.5, linewidth=3) pass #figure.canvas.draw() tsfig.save(pngfile)
def merge_results(self): tsmap = pyfits.open(self.tpl_file) tsmap[0].data = num.zeros(tsmap[0].data.shape, dtype=num.float) for subdir in self.subdirs: result_file = os.path.join(subdir, 'ts_results.dat') try: ii, jj, ra, dec, ts_vals = read_data(result_file) except: continue for i, j, ts in zip(ii, jj, ts_vals): tsmap[0].data[int(j)][int(i)] = ts tsmap.writeto(self.pars['outfile'], clobber=True)
def displayImage(self): self.image = aplpy.FITSFigure(self.skyimage, convention='calabretta', figure=self.figure, subplot=[0.1, 0.10, 0.40, 0.7], label='sky image') imageFits = pyfits.open(self.skyimage) img = imageFits[0].data # Apply grayscale mapping of image if (not self.empty): skm = self.image.show_colorscale(cmap='gist_heat', vmin=0.1, vmax=max(img.flatten()), stretch='log') else: skm = self.image.show_colorscale(cmap='gist_heat', vmin=0, vmax=0.1) imageFits.close() # Modify the tick labels for precision and format self.image.tick_labels.set_xformat('ddd.dd') self.image.tick_labels.set_yformat('ddd.dd') # Display a grid and tweak the properties try: self.image.show_grid() except: #show_grid throw an exception if the grid was already there pass pass #Try to plot a cross at the source position if (self.obj_ra is not None): self.image.show_markers([float(self.obj_ra)], [float(self.obj_dec)], edgecolor='cyan', facecolor='cyan', marker='x', s=120, alpha=0.5, linewidth=2) self.figure.canvas.draw()
def __init__(self,**kwargs): self.tstarts = [] self.tstops = [] self.counts = [] eventsCounter = EventsCounter(eventfile=_fixPath(kwargs['eventfile'])) timeBinsFile = pyfits.open(_fixPath(kwargs['timeBinsFile'])) for rowID in range(timeBinsFile["TIMEBINS"].data.shape[0]): curTstart = timeBinsFile["TIMEBINS"].data[rowID][0] curTstop = timeBinsFile["TIMEBINS"].data[rowID][1] self.tstarts.append(curTstart) self.tstops.append(curTstop) #Count how many events are contained in the TTE file between tstart and tstop self.counts.append(eventsCounter.getNevents(curTstart,curTstop)) pass timeBinsFile.close()
def __init__(self, **kwargs): self.tstarts = [] self.tstops = [] self.counts = [] eventsCounter = EventsCounter(eventfile=_fixPath(kwargs['eventfile'])) timeBinsFile = pyfits.open(_fixPath(kwargs['timeBinsFile'])) for rowID in range(timeBinsFile["TIMEBINS"].data.shape[0]): curTstart = timeBinsFile["TIMEBINS"].data[rowID][0] curTstop = timeBinsFile["TIMEBINS"].data[rowID][1] self.tstarts.append(curTstart) self.tstops.append(curTstop) #Count how many events are contained in the TTE file between tstart and tstop self.counts.append(eventsCounter.getNevents(curTstart, curTstop)) pass timeBinsFile.close()
def initWithCSPEC(self, cspec): f = pyfits.open(cspec) self.inputFile = 'cspec' self.data = f["SPECTRUM", 1].data.copy() cspectstarts = self.data.field("TIME") cspectstops = self.data.field("ENDTIME") cspectelapse = cspectstops - cspectstarts #Counts or rates? try: cspeccounts = self.data.field("COUNTS") except: rates = self.data.field("RATE") cspeccounts = map(lambda x: x[0] * x[1], zip(rates, cspectelapse)) pass self.tstarts = numpy.array(cspectstarts) self.tstops = numpy.array(cspectstops) self.counts = numpy.array(map(lambda x: numpy.sum(x), cspeccounts)) #Place zeros where QUALITY is > 0 (bad data) self.goodDataMask = (f["SPECTRUM"].data.field('QUALITY') == 0) self.counts[~self.goodDataMask] = 0 f.close()
def initWithCSPEC(self,cspec): f = pyfits.open(cspec) self.inputFile = 'cspec' self.data = f["SPECTRUM",1].data.copy() cspectstarts = self.data.field("TIME") cspectstops = self.data.field("ENDTIME") cspectelapse = cspectstops-cspectstarts #Counts or rates? try: cspeccounts = self.data.field("COUNTS") except: rates = self.data.field("RATE") cspeccounts = map(lambda x:x[0]*x[1],zip(rates,cspectelapse)) pass self.tstarts = numpy.array(cspectstarts) self.tstops = numpy.array(cspectstops) self.counts = numpy.array(map(lambda x:numpy.sum(x),cspeccounts)) #Place zeros where QUALITY is > 0 (bad data) self.goodDataMask = (f["SPECTRUM"].data.field('QUALITY')==0) self.counts[~self.goodDataMask] = 0 f.close()
def fitsToPNG(fitsfile,pngfile,vmin=None,vmax=None,**kwargs): #Display the TS map #figure = plt.figure() tsfig = aplpy.FITSFigure(fitsfile,convention='calabretta') tsfig.set_tick_labels_font(size='small') tsfig.set_axis_labels_font(size='small') if(vmin is not None and vmax is not None): tsfig.show_colorscale(cmap='gist_heat',aspect='auto',vmin=float(vmin),vmax=float(vmax)) else: #Get maximum of image f = pyfits.open(fitsfile) maximum = f[0].data.max() f.close() tsfig.show_colorscale(cmap='gist_heat',aspect='auto',vmin=0,vmax=float(maximum)) # Modify the tick labels for precision and format tsfig.tick_labels.set_xformat('ddd.dd') tsfig.tick_labels.set_yformat('ddd.dd') # Display a grid and tweak the properties tsfig.show_grid() tsfig.add_colorbar() if('sources' in kwargs.keys()): sources = kwargs['sources'] for src in sources: tsfig.add_label(float(src[1]),float(src[2]), "%s" % src[0], relative=False,weight='bold',color='green', size='x-small', verticalalignment='top', horizontalalignment='left') tsfig.show_markers([float(src[1])],[float(src[2])],edgecolor='green',marker='x') pass if('ra' in kwargs.keys()): ra = float(kwargs['ra']) dec = float(kwargs['dec']) tsfig.show_markers([ra],[dec],edgecolor='cyan',facecolor='cyan',marker='x',s=120,alpha=0.5,linewidth=3) pass #figure.canvas.draw() tsfig.save(pngfile)
def displayImage(self): self.image = aplpy.FITSFigure(self.skyimage,convention='calabretta', figure=self.figure, subplot=[0.1,0.10,0.40,0.7], label='sky image') imageFits = pyfits.open(self.skyimage) img = imageFits[0].data # Apply grayscale mapping of image if(not self.empty): skm = self.image.show_colorscale(cmap='gist_heat',vmin=0.1, vmax=max(img.flatten()), stretch='log') else: skm = self.image.show_colorscale(cmap='gist_heat',vmin=0, vmax=0.1) imageFits.close() # Modify the tick labels for precision and format self.image.tick_labels.set_xformat('ddd.dd') self.image.tick_labels.set_yformat('ddd.dd') # Display a grid and tweak the properties try: self.image.show_grid() except: #show_grid throw an exception if the grid was already there pass pass #Try to plot a cross at the source position if(self.obj_ra is not None): self.image.show_markers([float(self.obj_ra)], [float(self.obj_dec)], edgecolor='cyan', facecolor='cyan',marker='x', s=120, alpha=0.5,linewidth=2) self.figure.canvas.draw()
def __init__(self,filteredFt1,ft2,triggerName,triggertime,outdir): global active if(active): self.ft1 = filteredFt1 self.ft2 = ft2 #Read the cuts from the filtered ft1 file f = pyfits.open(self.ft1) h = f[0].header self.ra = float(h['_ROI_RA']) self.dec = float(h['_ROI_DEC']) self.roi = float(h['_ROI_RAD']) self.emin = float(h['_EMIN']) self.emax = float(h['_EMAX']) self.zmax = float(h['_ZMAX']) self.irf = str(h['_IRF']) f.close() self.triggertime = triggertime self.grbname = triggerName self.outdir = outdir else: raise RuntimeError("BKGE is not available in your system, you cannot use it!")
def run(**kwargs): if (len(kwargs.keys()) == 0): #Nothing specified, the user needs just help! thisCommand.getHelp() return pass #Get parameters values thisCommand.setParValuesFromDictionary(kwargs) try: eventfile = thisCommand.getParValue('filteredeventfile') rspfile = thisCommand.getParValue('rspfile') ft2file = thisCommand.getParValue('ft2file') expomap = thisCommand.getParValue('expomap') ltcube = thisCommand.getParValue('ltcube') xmlmodel = thisCommand.getParValue('xmlmodel') showmodelimage = thisCommand.getParValue('showmodelimage') optimize = thisCommand.getParValue('optimizeposition') spectralfiles = thisCommand.getParValue('spectralfiles') tsmin = float(thisCommand.getParValue('tsmin')) skymap = thisCommand.getParValue('skymap') liketype = thisCommand.getParValue('liketype') clobber = _yesOrNoToBool(thisCommand.getParValue('clobber')) verbose = _yesOrNoToBool(thisCommand.getParValue('verbose')) flemin = thisCommand.getParValue('flemin') flemax = thisCommand.getParValue('flemax') clul = float(thisCommand.getParValue('clul')) figure = thisCommand.getParValue('figure') except KeyError as err: print("\n\nERROR: Parameter %s not found or incorrect! \n\n" % (err.args[0])) #Print help print thisCommand.getHelp() return pass assert clul < 1.0, "The confidence level for the upper limit (clul) must be < 1" from GtBurst import dataHandling from GtBurst.angularDistance import getAngularDistance LATdata = dataHandling.LATData(eventfile, rspfile, ft2file) try: if (liketype == 'unbinned'): outfilelike, sources = LATdata.doUnbinnedLikelihoodAnalysis( xmlmodel, tsmin, expomap=expomap, ltcube=ltcube, emin=flemin, emax=flemax, clul=clul) else: #Generation of spectral files and optimization of the position is #not supported yet for binned analysis if (spectralfiles == 'yes'): print( "\nWARNING: you specified spectralfiles=yes, but the generation of spectral files is not supported for binned analysis\n" ) spectralfiles = 'no' if (optimize == 'yes'): print( "\nWARNING: you specified optimize=yes, but position optimization is not supported for binned analysis\n" ) optimize = 'no' outfilelike, sources = LATdata.doBinnedLikelihoodAnalysis( xmlmodel, tsmin, expomap=expomap, ltcube=ltcube, emin=flemin, emax=flemax, clul=clul) except GtBurstException as gt: raise gt except: raise #Transfer information on the source from the input to the output XML irf = dataHandling._getParamFromXML(xmlmodel, 'IRF') ra = dataHandling._getParamFromXML(xmlmodel, 'RA') dec = dataHandling._getParamFromXML(xmlmodel, 'DEC') name = dataHandling._getParamFromXML(xmlmodel, 'OBJECT') try: grb = filter(lambda x: x.name.lower().find(name.lower()) >= 0, sources)[0] grb_TS = grb.TS except: #A model without GRB print("\nWarning: no GRB in the model!") grb_TS = -1 pass if (irf is None): print( "\n\nWARNING: could not read IRF from XML file. Be sure you know what you are doing..." ) else: dataHandling._writeParamIntoXML(outfilelike, IRF=irf, OBJECT=name, RA=ra, DEC=dec) pass if (spectralfiles == 'yes'): phafile, rspfile, bakfile = LATdata.doSpectralFiles(outfilelike) pass localizationMessage = '' bestra = '' bestdec = '' poserr = '' distance = '' if (optimize == 'yes'): sourceName = name #If the TS for the source is above tsmin, optimize its position #grb = filter(lambda x:x.name.lower().find(sourceName.lower())>=0,sources)[0] if (math.ceil(grb_TS) >= tsmin): try: bestra, bestdec, poserr = LATdata.optimizeSourcePosition( outfilelike, sourceName) except: raise GtBurstException( 207, "gtfindsrc execution failed. Were the source detected in the likelihood step?" ) else: localizationMessage += "\nNew localization from gtfindsrc:\n\n" localizationMessage += "(R.A., Dec) = (%6.3f, %6.3f)\n" % ( bestra, bestdec) localizationMessage += "68 %s containment radius = %6.3f\n" % ( '%', poserr) localizationMessage += "90 %s containment radius = %6.3f\n" % ( '%', 1.41 * poserr) distance = getAngularDistance(float(ra), float(dec), float(bestra), float(bestdec)) localizationMessage += "Distance from initial position = %6.3f\n\n" % ( distance) localizationMessage += "NOTE: this new localization WILL NOT be used by default. If you judge" localizationMessage += " it is a better localization than the one you started with, update the" localizationMessage += " coordinates yourself and re-run the likelihood\n" pass pass if (figure is not None and skymap is not None and showmodelimage == 'yes'): #Now produce the binned exposure map (needed in order to display the fitted model as an image) modelmapfile = LATdata.makeModelSkyMap(outfilelike) #Display point sources in the image, and report in the table #all 3FGL sources with TS > 9 + always the GRB, independently of its TS detectedSources = [] grbFlux = 1e-13 for src in sources: weight = 'bold' if (src.type == 'PointSource'): if (src.TS > 4): detectedSources.append(src) if (src.name.find('3FGL') < 0): #GRB grbFlux = src.flux pass pass pass #Display the counts map from GtBurst import aplpy import matplotlib.pyplot as plt figure.clear() orig = aplpy.FITSFigure(skymap, convention='calabretta', figure=figure, subplot=[0.1, 0.1, 0.45, 0.7]) vmax = max(pyfits.open(skymap)[0].data.flatten()) nEvents = numpy.sum(pyfits.open(skymap)[0].data) telapsed = pyfits.open(eventfile)[0].header['TSTOP'] - pyfits.open( eventfile)[0].header['TSTART'] orig.set_tick_labels_font(size='small') orig.set_axis_labels_font(size='small') orig.show_colorscale(cmap='gist_heat', vmin=0.1, vmax=max(vmax, 0.11), stretch='log', aspect='auto') # Modify the tick labels for precision and format orig.tick_labels.set_xformat('ddd.dd') orig.tick_labels.set_yformat('ddd.dd') # Display a grid and tweak the properties orig.show_grid() #Renormalize the modelmapfile to the flux of the grb f = pyfits.open(modelmapfile, 'update') f[0].data = f[0].data / numpy.max(f[0].data) * nEvents / telapsed print(numpy.max(f[0].data)) f.close() img = aplpy.FITSFigure(modelmapfile, convention='calabretta', figure=figure, subplot=[0.55, 0.1, 0.4, 0.7]) img.set_tick_labels_font(size='small') img.set_axis_labels_font(size='small') #vmax = max(pyfits.open(modelmapfile)[0].data.flatten()) img.show_colorscale(cmap='gist_heat', aspect='auto', stretch='log') for src in detectedSources: img.add_label(float(src.ra), float(src.dec), "%s\n(ts = %i)" % (src.name, int(math.ceil(src.TS))), relative=False, weight=weight, color='green', size='small') pass # Modify the tick labels for precision and format img.tick_labels.set_xformat('ddd.dd') img.tick_labels.set_yformat('ddd.dd') # Display a grid and tweak the properties img.show_grid() #ax = figure.add_axes([0.1,0.72,0.85,0.25],frame_on=False) #ax.xaxis.set_visible(False) #ax.yaxis.set_visible(False) #col_labels =['Source Name','TS','Energy flux','Photon index'] #table_vals = map(lambda x:[x.name,"%i" %(int(math.ceil(x.TS))), # "%s +/- %s" % (x.flux,x.fluxError), # "%s +/- %s" % (x.photonIndex,x.photonIndexError)],detectedSources) # #if(len(table_vals)>0): # the_table = ax.table(cellText=table_vals, # colLabels=col_labels, # loc='upper center') figure.canvas.draw() figure.savefig("likelihood_results.png") pass if (figure is not None): #Assume we have an X server running #Now display the results likemsg = "Log(likelihood) = %s" % (LATdata.logL) displayResults( figure.canvas._tkcanvas, LATdata.resultsStrings + "\n" + likemsg + "\n" + localizationMessage) print(localizationMessage) return 'likexmlresults', outfilelike, 'TS', grb_TS, 'bestra', bestra, 'bestdec', bestdec, 'poserr', poserr, 'distance', distance, 'sources', sources
def run(**kwargs): if(len(kwargs.keys())==0): #Nothing specified, the user needs just help! thisCommand.getHelp() return pass #Get parameters values thisCommand.setParValuesFromDictionary(kwargs) try: eventfile = thisCommand.getParValue('filteredeventfile') rspfile = thisCommand.getParValue('rspfile') ft2file = thisCommand.getParValue('ft2file') tsmin = float(thisCommand.getParValue('tsmin')) fixphindex = thisCommand.getParValue('fixphindex') expomap = thisCommand.getParValue('expomap') ltcube = thisCommand.getParValue('ltcube') xmlmodel = thisCommand.getParValue('xmlmodel') sedtxt = thisCommand.getParValue('sedtxt') energybins = thisCommand.getParValue('energybins') clobber = _yesOrNoToBool(thisCommand.getParValue('clobber')) verbose = _yesOrNoToBool(thisCommand.getParValue('verbose')) figure = thisCommand.getParValue('figure') except KeyError as err: print("\n\nERROR: Parameter %s not found or incorrect! \n\n" %(err.args[0])) #Print help print thisCommand.getHelp() return pass from GtBurst import dataHandling from GtBurst.angularDistance import getAngularDistance LATdata = dataHandling.LATData(eventfile,rspfile,ft2file) try: outfilelike, sources = LATdata.doUnbinnedLikelihoodAnalysis(xmlmodel,tsmin=20,expomap=expomap,ltcube=ltcube) except GtBurstException as gt: raise gt except: raise #Now produce a pha1 file (just to compute the total exposure) pha1file = LATdata.binByEnergy(2) totalExposure = pyfits.getheader(pha1file,'SPECTRUM').get("EXPOSURE") print("\nTotal exposure: %s s" %(totalExposure)) #Transfer information on the source from the input to the output XML irf = dataHandling._getParamFromXML(xmlmodel,'IRF') ra = dataHandling._getParamFromXML(xmlmodel,'RA') dec = dataHandling._getParamFromXML(xmlmodel,'DEC') sourceName = 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...") else: dataHandling._writeParamIntoXML(outfilelike,IRF=irf,OBJECT=sourceName,RA=ra,DEC=dec) pass #Make a copy of the output XML file and freeze all parameters except the source tree = ET.parse(outfilelike) root = tree.getroot() phIndex = -2.0 for source in root.findall('source'): if(source.get('name')!=sourceName): #Freeze all parameters for spectrum in source.findall('spectrum'): for param in spectrum.findall('parameter'): param.set('free',"%s" % 0) pass pass else: if(fixphindex.lower()!='no' and fixphindex!=False): #Fix photon index for spectrum in source.findall('spectrum'): for param in spectrum.findall('parameter'): if(param.get('name')=='Index'): if(fixphindex.lower()!='prefit'): try: value = float(fixphindex) except: raise ValueError("The value for the photon index (%s) is not a float. Value not recognized." % fixphindex) print("\n\nFixing photon index to the provided value (%s) \n\n" %(value)) param.set('value',"%s" % value) else: print("\n\nFixing photon index to the best fit value on the whole energy range\n\n") param.set('free',"%s" % 0) phIndex = float(param.get('value')) pass pass pass pass f = open("__temporary_XML.xml",'w+') tree.write(f) f.write('''<!-- OBJECT=%s --> <!-- DEC=%s --> <!-- RA=%s --> <!-- IRF=%s -->\n''' %(sourceName,ra,dec,irf)) f.close() #Now for each energy band, make a likelihood and compute the flux f = pyfits.open(eventfile) #Take the list in inverse order so I will rebin at low energies, instead that at high energies energies = numpy.array(sorted(f['EVENTS'].data.ENERGY)[::-1]) totalInputCounts = len(energies) f.close() if(energybins is not None): energyBoundaries = map(lambda x:float(x),energybins.split(',')) else: energyBoundaries = LikelihoodComponent.optimizeBins(LATdata.like1,energies,sourceName,minTs=tsmin,minEvt=3) print("\nEnergy boundaries:") for i,ee1,ee2 in zip(range(len(energyBoundaries)-1),energyBoundaries[:-1],energyBoundaries[1:]): print("%02i: %10s - %10s" %(i+1,ee1,ee2)) pass print("\n") print("\nNumber of energy bins: %s\n" %(len(energyBoundaries)-1)) fluxes = numpy.zeros(len(energyBoundaries)-1) fluxes_errors = numpy.zeros(len(energyBoundaries)-1) phfluxes = numpy.zeros(len(energyBoundaries)-1) phfluxes_errors = numpy.zeros(len(energyBoundaries)-1) TSs = numpy.zeros(len(energyBoundaries)-1) phIndexes = numpy.zeros(len(energyBoundaries)-1) totalCounts = 0 for i,e1,e2 in zip(range(len(fluxes)),energyBoundaries[:-1],energyBoundaries[1:]): thisLATdata = dataHandling.LATData(eventfile,rspfile,ft2file,root="SED_%s-%s" %(e1,e2)) #Further cut in the energy range for this Band #Remove the version _vv from the name of the irf cleanedIrf = "_".join(LATdata.irf.split("_")[:-1]) outf,thisCounts = thisLATdata.performStandardCut(LATdata.ra,LATdata.dec,LATdata.rad,cleanedIrf, LATdata.tmin,LATdata.tmax,e1,e2,180,gtmktime=False,roicut=False) totalCounts += thisCounts #Perform the likelihood analysis outfilelike, sources = thisLATdata.doUnbinnedLikelihoodAnalysis("__temporary_XML.xml", tsmin=tsmin, dogtdiffrsp=False, expomap=expomap, ltcube=ltcube) source = filter(lambda x:x.name==sourceName,sources)[0] if(source.flux.find("<")>=0): #This is an upper limit source.flux = float(source.flux.split("<")[1].strip()) source.fluxError = -1 source.photonFlux = float(source.photonFlux.split("<")[1].strip()) source.photonFluxError = -1 pass fluxes[i] = source.flux fluxes_errors[i] = source.fluxError phfluxes[i] = source.photonFlux phfluxes_errors[i] = source.photonFluxError TSs[i] = float(source.TS) phIndexes[i] = float(source.photonIndex) pass if(totalCounts!=totalInputCounts): raise RuntimeError("We have losted somewhere %s counts!" %(totalInputCounts-totalCounts)) pass #Now compute the SED points MeV2Erg = 1.60217657e-6 ee1 = numpy.array(energyBoundaries[:-1]) ee2 = numpy.array(energyBoundaries[1:]) ee = (ee1+ee2)/2.0 de = (ee2-ee1) #Use the photon index of the total fit to compute the mean energy pow = numpy.power meanEnergies = numpy.array(map(lambda x:computeMeanEnergy(x[0],x[1],x[2]),zip(phIndexes,ee1,ee2))) nuFnu = phfluxes / de * pow(meanEnergies,2.0) * MeV2Erg nuFnuError = phfluxes_errors / de * pow(meanEnergies,2.0) * MeV2Erg #Print the results of the SED fw = open(sedtxt,'w+') fw.write("#Energy_min Energy_Max Flux Flux_error PhotonFlux PhotonFluxError TS nuFnu_energy nuFnu_energy_negerr nuFnu_energy_poserr nuFnu_value nuFnu_value_error\n") fw.write("#MeV MeV erg/cm2/s erg/cm2/s ph/cm2/s ph/cm2/s - MeV MeV MeV erg/cm2/s erg/cm2/s\n") fw.write("#Total exposure: %s s\n" %(totalExposure)) for e1,e2,f,fe,ph,phe,ts,ne,nee1,nee2,nuF,nuFe in zip(energyBoundaries[:-1],energyBoundaries[1:],fluxes,fluxes_errors,phfluxes,phfluxes_errors,TSs,meanEnergies,meanEnergies-ee1,ee2-meanEnergies,nuFnu,nuFnuError): fw.write("%s %s %s %s %s %s %s %s %s %s %s %s\n" %(e1,e2,f,fe,ph,phe,ts,ne,nee1,nee2,nuF,nuFe)) print("%10s - %10s MeV -> %g +/- %g erg/cm2/s, %g +/- %g ph/cm2/s, TS = %s" %(e1,e2,f,fe,ph,phe,ts)) pass fw.close() if(figure is not None): #Display the SED if(os.environ.get('DISPLAY') is None): os.environ.set('DISPLAY','/dev/null') import matplotlib matplotlib.use('Agg') pass import matplotlib.pyplot as plt try: figure.clear() except: print("Creating new figure...") try: figure = plt.figure() except: plt.switch_backend('Agg') figure = plt.figure() pass sub = figure.add_subplot(111) sub.errorbar(meanEnergies,nuFnu,xerr=[meanEnergies-ee1,ee2-meanEnergies],yerr=nuFnuError,fmt='.') sub.set_xlabel("Energy (MeV)") sub.set_ylabel("Flux (erg/cm2/s)") sub.loglog() sub.set_xlim(0.9*min(ee1),1.1*max(ee2)) figure.canvas.draw() figure.savefig("%s.png" % ".".join(sedtxt.split(".")[0:-1])) pass return 'sedtxt', sedtxt
def initWithTTE(self, tte): f = pyfits.open(tte) self.inputFile = 'TTE' self.data = f["EVENTS", 1].data.copy() self.eventTimes = numpy.array(list(self.data.field('TIME'))) f.close()
def gtltcube_mp(bins, SCFile, EVFile, OutFile, SaveTemp, zmax): '''This functions looks at a spacecraft file and splits the time into chunks that match the bin edges in the spacecraft file. It then submits jobs based upon those start and stop times. This is to make the resulting files as close to the original as possible. Note that this assumes you are using the full time period in your spacecraft file.''' verbose = False print "Opening event file to determine start and stop times..." evfile = pyfits.open(EVFile, mode='readonly') tstart = evfile[0].header['TSTART'] tstop = evfile[0].header['TSTOP'] gti_data = evfile[2].data print "Opening SC file to determine break points..." hdulist = pyfits.open(SCFile, mode='readonly') scdata = hdulist[1].data hdulist.close() scstart = scdata.field('START') scstop = scdata.field('STOP') time_filter = (tstart <= scstart) & (scstop <= tstop) redo = True print "Checking for good times in the event file..." while redo: redo = False scstartssplit = np.array_split(scstart[time_filter], int(bins)) scstopssplit = np.array_split(scstop[time_filter], bins) #Explicitly set the first and last point to the values in the evfile header scstartssplit[0][0] = tstart scstopssplit[-1][-1] = tstop starts = [st[0] for st in scstartssplit] stops = [st[-1] for st in scstopssplit] for interval in zip(starts, stops): if verbose: print "Looking at interval", interval[0], "to", interval[1] good_times = False #grrrr. some bug in pyfits doesn't let me do this the python way... for gti_i in range(len(gti_data)): if (not good_times): if verbose: print " Checking gti", gti_data[gti_i][ 'START'], "to", gti_data[gti_i]['STOP'] gti_starts = interval[0] <= gti_data[gti_i][ 'START'] <= interval[1] gti_stops = interval[0] < gti_data[gti_i][ 'STOP'] <= interval[1] if verbose: print " Does this gti start inside this interval? ", gti_starts if verbose: print " Does this gti stop inside this interval? ", gti_stops good_times = gti_starts or gti_stops if verbose: print if verbose: print " Are there good times inside this interval? ", good_times if not good_times: redo = True if verbose: print if redo: if bins <= 1: print "No good time intervals found. Bailing..." sys.exit(1) print "One (or more) of the slices doesn't have a GTI." print "Reducing the number of threads from ", bins, "to", bins - 1 bins -= 1 scfiles = [SCFile for st in scstartssplit] evfiles = [EVFile for st in scstartssplit] zmaxes = [zmax for st in scstartssplit] pool = Pool(processes=bins) times = np.array([starts, stops, scfiles, evfiles, zmaxes]) print "Spawning {} jobs...".format(bins) tempfilenames = pool.map(ltcube, times.transpose()) print "Combining temporary files..." ltsum(tempfilenames, OutFile, SaveTemp)
def run(**kwargs): if (len(kwargs.keys()) == 0): #Nothing specified, the user needs just help! thisCommand.getHelp() return pass #Get parameters values thisCommand.setParValuesFromDictionary(kwargs) try: ra = thisCommand.getParValue('ra') dec = thisCommand.getParValue('dec') particlemodel = thisCommand.getParValue('particle_model') galacticmodel = thisCommand.getParValue('galactic_model') sourcemodel = thisCommand.getParValue('source_model') filteredeventfile = thisCommand.getParValue('filteredeventfile') xmlmodel = thisCommand.getParValue('xmlmodel') triggername = thisCommand.getParValue('triggername') ft2file = thisCommand.getParValue('ft2file') fglmode = thisCommand.getParValue('fgl_mode') 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 pass #Get the IRF from the event file try: f = pyfits.open(filteredeventfile) except: raise GtBurstException( 31, "Could not open filtered event file %s" % (filteredeventfile)) tstart = float(f[0].header['_TMIN']) tstop = float(f[0].header['_TMAX']) irf = str(f[0].header['_IRF']) ra = float(f[0].header['_ROI_RA']) dec = float(f[0].header['_ROI_DEC']) roi = float(f[0].header['_ROI_RAD']) #Lookup table for the models models = {} if (particlemodel == 'isotr with pow spectrum'): models[ 'isotr with pow spectrum'] = LikelihoodComponent.IsotropicPowerlaw( ) elif (particlemodel == 'isotr template'): models['isotr template'] = LikelihoodComponent.IsotropicTemplate(irf) pass if (galacticmodel == 'template'): models['template'] = LikelihoodComponent.GalaxyAndExtragalacticDiffuse( irf, ra, dec, 2.5 * roi) elif (galacticmodel == 'template (fixed norm.)'): models[ 'template (fixed norm.)'] = LikelihoodComponent.GalaxyAndExtragalacticDiffuse( irf, ra, dec, 2.5 * roi) models['template (fixed norm.)'].fixNormalization() pass deltat = numpy.sum(f['GTI'].data.field('STOP') - f['GTI'].data.field('START')) f.close() triggertime = dataHandling.getTriggerTime(filteredeventfile) if (irf.lower().find('source') >= 0 and particlemodel != 'isotr template'): raise GtBurstException( 6, "Do not use '%s' as model for the particle background in SOURCE class. Use '%s' instead." % (particlemodel, 'isotropic template')) modelsToUse = [ LikelihoodComponent.PointSource(ra, dec, triggername, sourcemodel) ] if (particlemodel != 'none'): if (particlemodel == 'bkge'): if (ft2file is None or ft2file == ''): raise ValueError( "If you want to use the BKGE, you have to provide an FT2 file!" ) modelsToUse.append( LikelihoodComponent.BKGETemplate(filteredeventfile, ft2file, tstart, tstop, triggername, triggertime)) else: modelsToUse.append(models[particlemodel]) if (galacticmodel != 'none'): modelsToUse.append(models[galacticmodel]) xml = LikelihoodComponent.LikelihoodModel() xml.addSources(*modelsToUse) xml.writeXML(xmlmodel) if (fglmode == 'complete'): #Use a very large delta T so that #all FGL sources are included in the #xml model deltat = 1e12 xml.add2FGLsources(ra, dec, float(roi) + 8.0, xmlmodel, deltat) dataHandling._writeParamIntoXML(xmlmodel, IRF=irf, OBJECT=triggername, RA=ra, DEC=dec) return 'xmlmodel', xmlmodel
def run(**kwargs): if(len(kwargs.keys())==0): #Nothing specified, the user needs just help! thisCommand.getHelp() return pass #Get parameters values thisCommand.setParValuesFromDictionary(kwargs) try: eventfile = thisCommand.getParValue('filteredeventfile') rspfile = thisCommand.getParValue('rspfile') ft2file = thisCommand.getParValue('ft2file') expomap = thisCommand.getParValue('expomap') ltcube = thisCommand.getParValue('ltcube') xmlmodel = thisCommand.getParValue('xmlmodel') showmodelimage = thisCommand.getParValue('showmodelimage') optimize = thisCommand.getParValue('optimizeposition') spectralfiles = thisCommand.getParValue('spectralfiles') tsmin = float(thisCommand.getParValue('tsmin')) skymap = thisCommand.getParValue('skymap') liketype = thisCommand.getParValue('liketype') clobber = _yesOrNoToBool(thisCommand.getParValue('clobber')) verbose = _yesOrNoToBool(thisCommand.getParValue('verbose')) flemin = thisCommand.getParValue('flemin') flemax = thisCommand.getParValue('flemax') clul = float(thisCommand.getParValue('clul')) figure = thisCommand.getParValue('figure') except KeyError as err: print("\n\nERROR: Parameter %s not found or incorrect! \n\n" %(err.args[0])) #Print help print thisCommand.getHelp() return pass assert clul < 1.0, "The confidence level for the upper limit (clul) must be < 1" from GtBurst import dataHandling from GtBurst.angularDistance import getAngularDistance LATdata = dataHandling.LATData(eventfile,rspfile,ft2file) try: if(liketype=='unbinned'): outfilelike, sources = LATdata.doUnbinnedLikelihoodAnalysis(xmlmodel,tsmin,expomap=expomap,ltcube=ltcube,emin=flemin,emax=flemax, clul=clul) else: #Generation of spectral files and optimization of the position is #not supported yet for binned analysis if(spectralfiles=='yes'): print("\nWARNING: you specified spectralfiles=yes, but the generation of spectral files is not supported for binned analysis\n") spectralfiles = 'no' if(optimize=='yes'): print("\nWARNING: you specified optimize=yes, but position optimization is not supported for binned analysis\n") optimize = 'no' outfilelike, sources = LATdata.doBinnedLikelihoodAnalysis(xmlmodel,tsmin,expomap=expomap,ltcube=ltcube,emin=flemin,emax=flemax, clul=clul) except GtBurstException as gt: raise gt except: raise #Transfer information on the source from the input to the output XML irf = dataHandling._getParamFromXML(xmlmodel,'IRF') ra = dataHandling._getParamFromXML(xmlmodel,'RA') dec = dataHandling._getParamFromXML(xmlmodel,'DEC') name = dataHandling._getParamFromXML(xmlmodel,'OBJECT') try: grb = filter(lambda x:x.name.lower().find(name.lower())>=0,sources)[0] grb_TS = grb.TS except: #A model without GRB print("\nWarning: no GRB in the model!") grb_TS = -1 pass if(irf is None): print("\n\nWARNING: could not read IRF from XML file. Be sure you know what you are doing...") else: dataHandling._writeParamIntoXML(outfilelike,IRF=irf,OBJECT=name,RA=ra,DEC=dec) pass if(spectralfiles=='yes'): phafile,rspfile,bakfile = LATdata.doSpectralFiles(outfilelike) pass localizationMessage = '' bestra = '' bestdec = '' poserr = '' distance = '' if(optimize=='yes'): sourceName = name #If the TS for the source is above tsmin, optimize its position #grb = filter(lambda x:x.name.lower().find(sourceName.lower())>=0,sources)[0] if(math.ceil(grb_TS) >= tsmin): try: bestra,bestdec,poserr = LATdata.optimizeSourcePosition(outfilelike,sourceName) except: raise GtBurstException(207,"gtfindsrc execution failed. Were the source detected in the likelihood step?") else: localizationMessage += "\nNew localization from gtfindsrc:\n\n" localizationMessage += "(R.A., Dec) = (%6.3f, %6.3f)\n" %(bestra,bestdec) localizationMessage += "68 %s containment radius = %6.3f\n" %('%',poserr) localizationMessage += "90 %s containment radius = %6.3f\n" %('%',1.41*poserr) distance = getAngularDistance(float(ra),float(dec),float(bestra),float(bestdec)) localizationMessage += "Distance from initial position = %6.3f\n\n" %(distance) localizationMessage += "NOTE: this new localization WILL NOT be used by default. If you judge" localizationMessage += " it is a better localization than the one you started with, update the" localizationMessage += " coordinates yourself and re-run the likelihood\n" pass pass if(figure is not None and skymap is not None and showmodelimage=='yes'): #Now produce the binned exposure map (needed in order to display the fitted model as an image) modelmapfile = LATdata.makeModelSkyMap(outfilelike) #Display point sources in the image, and report in the table #all 3FGL sources with TS > 9 + always the GRB, independently of its TS detectedSources = [] grbFlux = 1e-13 for src in sources: weight = 'bold' if(src.type=='PointSource'): if(src.TS > 4): detectedSources.append(src) if(src.name.find('3FGL')<0): #GRB grbFlux = src.flux pass pass pass #Display the counts map from GtBurst import aplpy import matplotlib.pyplot as plt figure.clear() orig = aplpy.FITSFigure(skymap,convention='calabretta', figure=figure,subplot=[0.1,0.1,0.45,0.7]) vmax = max(pyfits.open(skymap)[0].data.flatten()) nEvents = numpy.sum(pyfits.open(skymap)[0].data) telapsed = pyfits.open(eventfile)[0].header['TSTOP']-pyfits.open(eventfile)[0].header['TSTART'] orig.set_tick_labels_font(size='small') orig.set_axis_labels_font(size='small') orig.show_colorscale(cmap='gist_heat',vmin=0.1,vmax=max(vmax,0.11),stretch='log',aspect='auto') # Modify the tick labels for precision and format orig.tick_labels.set_xformat('ddd.dd') orig.tick_labels.set_yformat('ddd.dd') # Display a grid and tweak the properties orig.show_grid() #Renormalize the modelmapfile to the flux of the grb f = pyfits.open(modelmapfile,'update') f[0].data = f[0].data/numpy.max(f[0].data)*nEvents/telapsed print(numpy.max(f[0].data)) f.close() img = aplpy.FITSFigure(modelmapfile,convention='calabretta', figure=figure,subplot=[0.55,0.1,0.4,0.7]) img.set_tick_labels_font(size='small') img.set_axis_labels_font(size='small') #vmax = max(pyfits.open(modelmapfile)[0].data.flatten()) img.show_colorscale(cmap='gist_heat',aspect='auto',stretch='log') for src in detectedSources: img.add_label(float(src.ra),float(src.dec), "%s\n(ts = %i)" %(src.name,int(math.ceil(src.TS))), relative=False,weight=weight, color='green', size='small') pass # Modify the tick labels for precision and format img.tick_labels.set_xformat('ddd.dd') img.tick_labels.set_yformat('ddd.dd') # Display a grid and tweak the properties img.show_grid() #ax = figure.add_axes([0.1,0.72,0.85,0.25],frame_on=False) #ax.xaxis.set_visible(False) #ax.yaxis.set_visible(False) #col_labels =['Source Name','TS','Energy flux','Photon index'] #table_vals = map(lambda x:[x.name,"%i" %(int(math.ceil(x.TS))), # "%s +/- %s" % (x.flux,x.fluxError), # "%s +/- %s" % (x.photonIndex,x.photonIndexError)],detectedSources) # #if(len(table_vals)>0): # the_table = ax.table(cellText=table_vals, # colLabels=col_labels, # loc='upper center') figure.canvas.draw() figure.savefig("likelihood_results.png") pass if(figure is not None): #Assume we have an X server running #Now display the results likemsg = "Log(likelihood) = %s" %(LATdata.logL) displayResults(figure.canvas._tkcanvas, LATdata.resultsStrings + "\n" + likemsg + "\n" + localizationMessage) print(localizationMessage) return 'likexmlresults', outfilelike, 'TS', grb_TS, 'bestra', bestra, 'bestdec', bestdec, 'poserr', poserr, 'distance', distance,'sources', sources
def run(**kwargs): if(len(kwargs.keys())==0): #Nothing specified, the user needs just help! thisCommand.getHelp() return # Get parameters values thisCommand.setParValuesFromDictionary(kwargs) cspec_in = None try: eventfile = thisCommand.getParValue('eventfile') rspfile = thisCommand.getParValue('rspfile') ft2file = thisCommand.getParValue('ft2file') cspec_in = thisCommand.getParValue('cspec_in') dt = thisCommand.getParValue('dt') tstart = thisCommand.getParValue('tstart') tstop = thisCommand.getParValue('tstop') outfile = thisCommand.getParValue('cspecfile') 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 pass #I import this here to save time if the user makes error in the command line from GtBurst import dataHandling message = Message(verbose) #Load LLE data message(" * Get energy binning from the response matrix...") lleData = dataHandling.LLEData(eventfile,rspfile,ft2file) message("\n done.") #Make PHA2 with gtbin tempPHA2filename = "__gtllebin__pha2.pha" # This will use the same energy binning as in the EBOUNDS extension of the rspfile message("\n * Run gtbindef and gtbin and bin in energy and time...\n") # if you input a cspec file it will clone the time binning: if cspec_in is not None: tmpFile=pyfits.open(cspec_in) message('=> Copying the binning from: %s' % cspec_in) _SPECTRUM=tmpFile['SPECTRUM'].data _TIME =_SPECTRUM.field('TIME') _ENDTIME =_SPECTRUM.field('ENDTIME') tempTimeBinFile="__tmpBinFileFromCSPEC.txt" txt='' for i in range(len(_TIME)): txt+='%s\t%s\n' %( _TIME[i],_ENDTIME[i]) file(tempTimeBinFile,'w').writelines(txt) tempTimeBinFile_fits="__tmpBinFileFromCSPEC.fits" lleData.gtbindef['bintype']='T' lleData.gtbindef['binfile']=tempTimeBinFile lleData.gtbindef['outfile']=tempTimeBinFile_fits lleData.gtbindef.run() lleData.binByEnergyAndTime(tempTimeBinFile_fits,tempPHA2filename) else: lleData.binByEnergyAndTime(tstart,tstop,dt,tempPHA2filename) message("\n done.") #Transform the PHA2 in CSPEC message("\n * Transform gtbin output in CSPEC format...") pha2 = dataHandling.Spectra(tempPHA2filename) #Set Poisson errors to true (this will make POISSERR=True in the output CSPEC file) pha2.setPoisson(True) pha2.write(outfile,format="CSPEC",trigtime=lleData.trigTime,clobber=clobber) #Remove the temporary PHA2 file os.remove(tempPHA2filename) message("\n done.") #Copy some keywords from the LLE file to the CSPEC file message("\n * Updating keywords in the headers of the CSPEC file...") dataHandling.fixHeaders(eventfile,outfile) message("\n done.") message("\n%s done!" %(thisCommand.name)) return 'cspecfile', outfile
def RSPweight(**kwargs): ''' Weight the response matrices contained in the rsp file. Usage: RSPweight(eventfile=eventfile,timeBinsFile=timeBinsFile,rsp2file=rsp2file, outfile=outfile,triggerTime=triggerTime) where eventfile can be either a TTE/FT1 file or a CSPEC file. ''' timeIntervals = TimeIntervals(**kwargs) rsp2 = _fixPath(kwargs['rsp2file']) out = _fixPath(kwargs['outfile']) #The trigger time is only for printing purposes: if specified, #all the messages from the program will contain time intervals referred #to the trigger time, otherwise they will be in MET if ('triggerTime' in kwargs.keys()): trigger = kwargs['triggerTime'] else: trigger = 0 #name for the output outrsp = out #Open files rsp2File = pyfits.open(rsp2) #Get instrument name try: instrument = pyfits.getval(_fixPath(kwargs['eventfile']), "INSTRUME", extname="EVENTS", extver=1) except: try: instrument = pyfits.getval(_fixPath(kwargs['eventfile']), "INSTRUME", extname="SPECTRUM", extver=1) except: instrument = "UNKN-INSTRUME" #For every interval contained in the time bins file #find the applying matrices and compute the weight (if necessary) nIntervals = len(timeIntervals.tstarts) nMatrix = len(rsp2File) createdRspNames = [] eventsCounter = EventsCounter(**kwargs) for tstart, tstop, nEvents, intervalNumber in zip(timeIntervals.tstarts, timeIntervals.tstops, timeIntervals.counts, range(nIntervals)): print("\nInterval: %s - %s" % (tstart - trigger, tstop - trigger)) #find the RSP matrices falling in the current tstart-tstop interval rspList = [] rspStarts = [] rspStops = [] firstResponse = True for extNumber in range(nMatrix): if (rsp2File[extNumber].name == "SPECRESP MATRIX" or rsp2File[extNumber].name == "MATRIX"): curHeader = rsp2File[extNumber].header #Check that the instrument is the same of the TTE file instrument2 = curHeader["INSTRUME"] if (instrument2 != instrument): print( "WARNING: the Events file %s and the response matrix file %s refers to different instruments" % (instrument, instrument2)) pass #Find the start and stop time of the interval covered #by this response matrix headerStart = curHeader["TSTART"] headerStop = curHeader["TSTOP"] #The matrix cover the period going from the middle point between its start #time and the start time of the previous matrix (or its start time if it is the #first matrix of the file), and the middle point between its start time and its #stop time (that is equal to the start time of the next one): # # tstart tstop # |==================================| # |--------------x---------------|-----------x-----------|----------x--.. #rspStart1 rspStop1= headerStart2 rspStop2= headerStart3 rspStop3 # rspStart2 rspStart3 # #covered by: |m1 | m2 | m3| # if (firstResponse): #This is the first matrix if (extNumber == len(rsp2File) - 1): #This is both the first both the last matrix rspStart = headerStart rspStop = headerStop else: rspStart = headerStart rspStop = (headerStart + headerStop) / 2.0 firstResponse = False pass elif (extNumber == len(rsp2File) - 1): #This is the last matrix rspStart = prevRspStop rspStop = headerStop else: rspStart = prevRspStop rspStop = (headerStart + headerStop) / 2.0 pass #Save the stop time for the next iteration of the loop prevRspStop = rspStop #Check if the current matrix covers at least a portion of the #start-stop time interval if (rspStop >= tstart and rspStart <= tstop): #Found a matrix covering a part of the interval: #adding it to the list: rspList.append(fixMatrixHDU(rsp2File[extNumber])) #Get the "true" start time of the time sub-interval covered by this matrix trueRspStart = max(rspStart, tstart) #Get the "true" stop time of the time sub-interval covered by this matrix if (extNumber == len(rsp2File) - 1): #Since there are no matrices after this one, this has to cover until the end of the interval if (tstop > rspStop): print( "\nWARNING: RSPweight: The RSP file does not cover the required time interval." ) print( " The last response should cover from %s to %s, but we'll use it" % (trueRspStart, rspStop)) print( " to cover until the end of the time interval (%s)." % (tstop)) trueRspStop = tstop else: trueRspStop = tstop else: trueRspStop = min(rspStop, tstop) pass #Save the true start and stop rspStarts.append(trueRspStart) rspStops.append(trueRspStop) print(" Matr. in ext #%s covers %s - %s" % (extNumber, trueRspStart - trigger, trueRspStop - trigger)) if (rspStop >= tstop): #This matrix cover after the tstop, #no reason to analize the other matrices #pass break pass pass pass pass weight = [] if (len(rspList) > 1): #We want to weight matrices by number of counts #so we need the number of events falling in the current #time interval #Number of total events contained in the interval nThisTotalEvt = nEvents print("\n Total counts for this interval: %s" % (nThisTotalEvt)) if (nThisTotalEvt <= 0): print( " Counts is zero: no signal here. Weight will be exposure-based." ) #Weight according to the exposure if (sum(weight) == 0): for index, rsp in enumerate(rspList): thisWeight = (rspStops[index] - rspStarts[index]) / (tstop - tstart) weight.append(thisWeight) print(" Weight for response %s - %s: %s" % (rspStarts[index] - trigger, rspStops[index] - trigger, thisWeight)) pass pass else: #Weight according to the counts #now look upon the selected events, searching for events #falling in the interval covered by each #response matrix for index, rsp in enumerate(rspList): rspStart = rspStarts[index] rspStop = rspStops[index] #how many events of the time interval contained here? nThisRspEvt = eventsCounter.getNevents(rspStart, rspStop) #Compute the weight if (nThisRspEvt > 0): thisWeight = float(nThisRspEvt) / float(nThisTotalEvt) else: thisWeight = 0 pass #Append the weight to the list of weights weight.append(thisWeight) #Print information print(" Weight for response %s - %s: %s" % (rspStart - trigger, rspStop - trigger, thisWeight)) print(" Counts in time covered by this rsp: %s" % nThisRspEvt) pass pass #Now, if the sum of the weight is not 1, redistribuite the lacking weight #according to the exposure. This can happen due to precision problems if (sum(weight) != 1): lackingWeight = 1.0 - sum(weight) for index, rsp in enumerate(rspList): thisExposureWeight = (rspStops[index] - rspStarts[index]) / (tstop - tstart) weight[index] += (thisExposureWeight * lackingWeight) pass pass print("\nTotal weight ---> %s" % (sum(weight))) else: #There is only one matrix... weight = [1] pass print("\n") #Use addrmf to add and weight the response matrix #We don't use directly pyFits because dealing with response matrices #can be dangerous due to the variable lenght column, #and other features, so we leave the work to a specialized tool #we need to create a file for every matrix, and an ascii file #containing the list of those files and the weights to be applied asciiFilename = "__rspweight_rspList.ascii" asciiFile = open(asciiFilename, 'w') asciiFile.write('') #Loop on the matrices and sum them names = [] for rsp_i in range(len(rspList)): #Decide a temporary filename name = "__rspweight_matrix" + str(rsp_i) + ".rsp" names.append(name) primaryHDU = rsp2File[0].copy() eboundsHDU = rsp2File['EBOUNDS'].copy() responseHDU = rspList[rsp_i].copy() HDUlist = pyfits.HDUList([primaryHDU, eboundsHDU, responseHDU]) HDUlist.writeto(name, output_verify='fix', clobber=True) if (instrument.find("GBM") >= 0): # FIX the wrong GBM matrices data = pyfits.open(name, 'update') #EBOUNDS: #Find what is the tlminColumn column columns = data['EBOUNDS'].data.names tlminID = columns.index('CHANNEL') + 1 #Find the number of channels (usually 128) nChannels = data['EBOUNDS'].data.size #Fix the CHANNEL column, and TLMIN/TLMAX keywords data['EBOUNDS'].data.field('CHANNEL')[:] = numpy.array( range(1, nChannels + 1)) data['EBOUNDS'].header.set("TLMIN%s" % (tlminID), 1) data['EBOUNDS'].header.set("TLMAX%s" % (tlminID), nChannels) #SPECRESP MATRIX #Now we have to correct the rows whith wrong F_CHAN and N_CHAN values: #There are rows where F_CHAN = 128, and N_CHAN = 1, but the only element not null #in the row is the first, thus it should be: F_CHAN = 1, N_CHAN = 1 for row in range(nChannels): if (data['SPECRESP MATRIX'].data.field('F_CHAN')[row] == 128 and data['SPECRESP MATRIX'].data.field('N_CHAN')[row] == 1): #We have a possibly wrong row #Try to access the channel 128 of the matrix try: data['SPECRESP MATRIX'].data.field( 'MATRIX')[row][127] except: #Impossible to access it, thus the values are wrong. Fix them data['SPECRESP MATRIX'].data.field( 'F_CHAN')[row] = numpy.array([1]) data['SPECRESP MATRIX'].data.field( 'N_CHAN')[row] = numpy.array([1]) pass pass pass #Find what is the tlminColumn column columns = data['SPECRESP MATRIX'].data.names tlminID = columns.index('F_CHAN') + 1 data['SPECRESP MATRIX'].header.set("TLMIN%s" % (tlminID), 1) data['SPECRESP MATRIX'].header.set("TLMAX%s" % (tlminID), nChannels) data.close() pass asciiFile.write(name + " " + str(weight[rsp_i]) + "\n") pass asciiFile.close() #Decide a name for the output matrix for this interval thisRspName = "__rspweight_interval" + str(intervalNumber) + ".rsp" createdRspNames.append(thisRspName) if (len(names) == 1): #Only one matrix. No need to weight shutil.copy(names[0], thisRspName) else: #Run addrmf and weight the relevant matrices cmdline = "addrmf @%s rmffile=%s clobber=yes" % (asciiFilename, thisRspName) print("\n %s \n" % (cmdline)) out, err = _callSubprocess(cmdline) print out print err pass #clean up os.remove(asciiFilename) pass #now append all the produced matrix in one RSP2 file #Take the PRIMARY extension from the input RSP2 file primary = rsp2File[0].copy() primary.header.set("DRM_NUM", nIntervals) primary.header.set("TSTART", timeIntervals.tstarts[0]) primary.header.set("TSTOP", timeIntervals.tstops[-1]) primary.writeto(outrsp, clobber='True') #Get the EBOUNDS extension from the first matrix firstRsp = pyfits.open(createdRspNames[0]) ebounds = firstRsp['EBOUNDS'].copy() firstRsp.close() #Reopen the file just created outRsp2 = pyfits.open(outrsp, 'update') outRsp2.append(ebounds) #now write an extension for each interval for i in range(nIntervals): thisRsp = pyfits.open(createdRspNames[i]) try: curM = fixMatrixHDU(thisRsp["SPECRESP MATRIX"]).copy() name = "SPECRESP MATRIX" except: try: curM = fixMatrixHDU(thisRsp["SPECRESP MATRIX"]).copy() name = "MATRIX" except: raise RuntimeError( "Something gone wrong. Invalid file produced by the weighting algorithm." ) pass thisRsp.close() curHeader = curM.header curData = curM.data #Update TSTART and TSTOP curHeader.set("TSTART", timeIntervals.tstarts[intervalNumber]) curHeader.set("TSTOP", timeIntervals.tstops[intervalNumber]) #Update RSP_NUM keyword curHeader.set("RSP_NUM", i + 1) #update EXTVER keyword curHeader.set("EXTVER", i + 1) #Add history history = "This is a matrix computed by weighting applying matrices contained in " + rsp2 curHeader.add_history(history) print("Appending matrix number %s..." % (i + 1)) #Write this as an extension in the output file outRsp2.append(curM) pass #Closing everything outRsp2.close() #Close files rsp2File.close() #Cleanup print("Cleaning up...") fileToDelete = glob.glob("__rspweight*") for f in fileToDelete: os.remove(f)
def RSPweight(**kwargs): ''' Weight the response matrices contained in the rsp file. Usage: RSPweight(eventfile=eventfile,timeBinsFile=timeBinsFile,rsp2file=rsp2file, outfile=outfile,triggerTime=triggerTime) where eventfile can be either a TTE/FT1 file or a CSPEC file. ''' timeIntervals = TimeIntervals(**kwargs) rsp2 = _fixPath(kwargs['rsp2file']) out = _fixPath(kwargs['outfile']) #The trigger time is only for printing purposes: if specified, #all the messages from the program will contain time intervals referred #to the trigger time, otherwise they will be in MET if('triggerTime' in kwargs.keys()): trigger = kwargs['triggerTime'] else: trigger = 0 #name for the output outrsp = out #Open files rsp2File = pyfits.open(rsp2) #Get instrument name try: instrument = pyfits.getval(_fixPath(kwargs['eventfile']),"INSTRUME",extname="EVENTS",extver=1) except: try: instrument = pyfits.getval(_fixPath(kwargs['eventfile']),"INSTRUME",extname="SPECTRUM",extver=1) except: instrument = "UNKN-INSTRUME" #For every interval contained in the time bins file #find the applying matrices and compute the weight (if necessary) nIntervals = len(timeIntervals.tstarts) nMatrix = len(rsp2File) createdRspNames = [] eventsCounter = EventsCounter(**kwargs) for tstart,tstop,nEvents,intervalNumber in zip(timeIntervals.tstarts, timeIntervals.tstops, timeIntervals.counts, range(nIntervals)): print("\nInterval: %s - %s" % (tstart-trigger,tstop-trigger)) #find the RSP matrices falling in the current tstart-tstop interval rspList = [] rspStarts = [] rspStops = [] firstResponse = True for extNumber in range(nMatrix): if(rsp2File[extNumber].name=="SPECRESP MATRIX" or rsp2File[extNumber].name=="MATRIX"): curHeader = rsp2File[extNumber].header #Check that the instrument is the same of the TTE file instrument2 = curHeader["INSTRUME"] if(instrument2!=instrument): print("WARNING: the Events file %s and the response matrix file %s refers to different instruments" % (instrument,instrument2)) pass #Find the start and stop time of the interval covered #by this response matrix headerStart = curHeader["TSTART"] headerStop = curHeader["TSTOP"] #The matrix cover the period going from the middle point between its start #time and the start time of the previous matrix (or its start time if it is the #first matrix of the file), and the middle point between its start time and its #stop time (that is equal to the start time of the next one): # # tstart tstop # |==================================| # |--------------x---------------|-----------x-----------|----------x--.. #rspStart1 rspStop1= headerStart2 rspStop2= headerStart3 rspStop3 # rspStart2 rspStart3 # #covered by: |m1 | m2 | m3| # if(firstResponse): #This is the first matrix if(extNumber==len(rsp2File)-1): #This is both the first both the last matrix rspStart = headerStart rspStop = headerStop else: rspStart = headerStart rspStop = (headerStart+headerStop)/2.0 firstResponse = False pass elif(extNumber==len(rsp2File)-1): #This is the last matrix rspStart = prevRspStop rspStop = headerStop else: rspStart = prevRspStop rspStop = (headerStart+headerStop)/2.0 pass #Save the stop time for the next iteration of the loop prevRspStop = rspStop #Check if the current matrix covers at least a portion of the #start-stop time interval if( rspStop >= tstart and rspStart <= tstop): #Found a matrix covering a part of the interval: #adding it to the list: rspList.append(fixMatrixHDU(rsp2File[extNumber])) #Get the "true" start time of the time sub-interval covered by this matrix trueRspStart = max(rspStart,tstart) #Get the "true" stop time of the time sub-interval covered by this matrix if(extNumber==len(rsp2File)-1): #Since there are no matrices after this one, this has to cover until the end of the interval if(tstop > rspStop): print("\nWARNING: RSPweight: The RSP file does not cover the required time interval.") print(" The last response should cover from %s to %s, but we'll use it" % (trueRspStart, rspStop)) print(" to cover until the end of the time interval (%s)." % (tstop)) trueRspStop = tstop else: trueRspStop = tstop else: trueRspStop = min(rspStop,tstop) pass #Save the true start and stop rspStarts.append(trueRspStart) rspStops.append(trueRspStop) print(" Matr. in ext #%s covers %s - %s" % (extNumber,trueRspStart-trigger,trueRspStop-trigger)) if(rspStop >= tstop): #This matrix cover after the tstop, #no reason to analize the other matrices #pass break pass pass pass pass weight = [] if(len(rspList) > 1): #We want to weight matrices by number of counts #so we need the number of events falling in the current #time interval #Number of total events contained in the interval nThisTotalEvt = nEvents print("\n Total counts for this interval: %s" %(nThisTotalEvt)) if(nThisTotalEvt <= 0): print(" Counts is zero: no signal here. Weight will be exposure-based.") #Weight according to the exposure if(sum(weight)==0): for index,rsp in enumerate(rspList): thisWeight = (rspStops[index]-rspStarts[index])/(tstop-tstart) weight.append(thisWeight) print(" Weight for response %s - %s: %s" % (rspStarts[index]-trigger,rspStops[index]-trigger,thisWeight)) pass pass else: #Weight according to the counts #now look upon the selected events, searching for events #falling in the interval covered by each #response matrix for index,rsp in enumerate(rspList): rspStart = rspStarts[index] rspStop = rspStops[index] #how many events of the time interval contained here? nThisRspEvt = eventsCounter.getNevents(rspStart,rspStop) #Compute the weight if(nThisRspEvt > 0): thisWeight = float(nThisRspEvt)/float(nThisTotalEvt) else: thisWeight = 0 pass #Append the weight to the list of weights weight.append(thisWeight) #Print information print(" Weight for response %s - %s: %s" % (rspStart-trigger,rspStop-trigger,thisWeight)) print(" Counts in time covered by this rsp: %s" % nThisRspEvt) pass pass #Now, if the sum of the weight is not 1, redistribuite the lacking weight #according to the exposure. This can happen due to precision problems if(sum(weight)!=1): lackingWeight = 1.0-sum(weight) for index,rsp in enumerate(rspList): thisExposureWeight = (rspStops[index]-rspStarts[index])/(tstop-tstart) weight[index] += (thisExposureWeight*lackingWeight) pass pass print("\nTotal weight ---> %s" %(sum(weight))) else: #There is only one matrix... weight = [1] pass print("\n") #Use addrmf to add and weight the response matrix #We don't use directly pyFits because dealing with response matrices #can be dangerous due to the variable lenght column, #and other features, so we leave the work to a specialized tool #we need to create a file for every matrix, and an ascii file #containing the list of those files and the weights to be applied asciiFilename = "__rspweight_rspList.ascii" asciiFile = open(asciiFilename,'w') asciiFile.write('') #Loop on the matrices and sum them names = [] for rsp_i in range(len(rspList)): #Decide a temporary filename name = "__rspweight_matrix"+str(rsp_i)+".rsp" names.append(name) primaryHDU = rsp2File[0].copy() eboundsHDU = rsp2File['EBOUNDS'].copy() responseHDU = rspList[rsp_i].copy() HDUlist = pyfits.HDUList([primaryHDU,eboundsHDU,responseHDU]) HDUlist.writeto(name,output_verify='fix',clobber=True) if(instrument.find("GBM")>=0): # FIX the wrong GBM matrices data = pyfits.open(name,'update') #EBOUNDS: #Find what is the tlminColumn column columns = data['EBOUNDS'].data.names tlminID = columns.index('CHANNEL')+1 #Find the number of channels (usually 128) nChannels = data['EBOUNDS'].data.size #Fix the CHANNEL column, and TLMIN/TLMAX keywords data['EBOUNDS'].data.field('CHANNEL')[:]=numpy.array(range(1,nChannels+1)) data['EBOUNDS'].header.set("TLMIN%s" %(tlminID),1) data['EBOUNDS'].header.set("TLMAX%s" %(tlminID),nChannels) #SPECRESP MATRIX #Now we have to correct the rows whith wrong F_CHAN and N_CHAN values: #There are rows where F_CHAN = 128, and N_CHAN = 1, but the only element not null #in the row is the first, thus it should be: F_CHAN = 1, N_CHAN = 1 for row in range(nChannels): if(data['SPECRESP MATRIX'].data.field('F_CHAN')[row]==128 and data['SPECRESP MATRIX'].data.field('N_CHAN')[row]==1): #We have a possibly wrong row #Try to access the channel 128 of the matrix try: data['SPECRESP MATRIX'].data.field('MATRIX')[row][127] except: #Impossible to access it, thus the values are wrong. Fix them data['SPECRESP MATRIX'].data.field('F_CHAN')[row] = numpy.array([1]) data['SPECRESP MATRIX'].data.field('N_CHAN')[row] = numpy.array([1]) pass pass pass #Find what is the tlminColumn column columns = data['SPECRESP MATRIX'].data.names tlminID = columns.index('F_CHAN')+1 data['SPECRESP MATRIX'].header.set("TLMIN%s" %(tlminID),1) data['SPECRESP MATRIX'].header.set("TLMAX%s" %(tlminID),nChannels) data.close() pass asciiFile.write(name+" "+str(weight[rsp_i])+"\n") pass asciiFile.close() #Decide a name for the output matrix for this interval thisRspName = "__rspweight_interval"+str(intervalNumber)+".rsp" createdRspNames.append(thisRspName) if(len(names)==1): #Only one matrix. No need to weight shutil.copy(names[0],thisRspName) else: #Run addrmf and weight the relevant matrices cmdline = "addrmf @%s rmffile=%s clobber=yes" % (asciiFilename,thisRspName) print("\n %s \n" %(cmdline)) out, err = _callSubprocess(cmdline) print out print err pass #clean up os.remove(asciiFilename) pass #now append all the produced matrix in one RSP2 file #Take the PRIMARY extension from the input RSP2 file primary = rsp2File[0].copy() primary.header.set("DRM_NUM",nIntervals) primary.header.set("TSTART",timeIntervals.tstarts[0]) primary.header.set("TSTOP",timeIntervals.tstops[-1]) primary.writeto(outrsp,clobber='True') #Get the EBOUNDS extension from the first matrix firstRsp = pyfits.open(createdRspNames[0]) ebounds = firstRsp['EBOUNDS'].copy() firstRsp.close() #Reopen the file just created outRsp2 = pyfits.open(outrsp,'update') outRsp2.append(ebounds) #now write an extension for each interval for i in range(nIntervals): thisRsp = pyfits.open(createdRspNames[i]) try: curM = fixMatrixHDU(thisRsp["SPECRESP MATRIX"]).copy() name = "SPECRESP MATRIX" except: try: curM = fixMatrixHDU(thisRsp["SPECRESP MATRIX"]).copy() name = "MATRIX" except: raise RuntimeError("Something gone wrong. Invalid file produced by the weighting algorithm.") pass thisRsp.close() curHeader = curM.header curData = curM.data #Update TSTART and TSTOP curHeader.set("TSTART",timeIntervals.tstarts[intervalNumber]) curHeader.set("TSTOP",timeIntervals.tstops[intervalNumber]) #Update RSP_NUM keyword curHeader.set("RSP_NUM",i+1) #update EXTVER keyword curHeader.set("EXTVER",i+1) #Add history history="This is a matrix computed by weighting applying matrices contained in "+rsp2 curHeader.add_history(history) print("Appending matrix number %s..." %(i+1)) #Write this as an extension in the output file outRsp2.append(curM) pass #Closing everything outRsp2.close() #Close files rsp2File.close() #Cleanup print("Cleaning up...") fileToDelete = glob.glob("__rspweight*") for f in fileToDelete: os.remove(f)
def initWithTTE(self,tte): f = pyfits.open(tte) self.inputFile = 'TTE' self.data = f["EVENTS",1].data.copy() self.eventTimes = numpy.array(list(self.data.field('TIME'))) f.close()
def __init__(self, template_file): self.proj = pyLike.SkyProj(template_file) template = pyfits.open(template_file) self.nx = template[0].header['NAXIS1'] self.ny = template[0].header['NAXIS2']
def gtltcube_mp(bins, SCFile, EVFile, OutFile, SaveTemp, zmax): '''This functions looks at a spacecraft file and splits the time into chunks that match the bin edges in the spacecraft file. It then submits jobs based upon those start and stop times. This is to make the resulting files as close to the original as possible. Note that this assumes you are using the full time period in your spacecraft file.''' verbose = False print "Opening event file to determine start and stop times..." evfile = pyfits.open(EVFile, mode='readonly') tstart = evfile[0].header['TSTART'] tstop = evfile[0].header['TSTOP'] gti_data = evfile[2].data print "Opening SC file to determine break points..." hdulist = pyfits.open(SCFile, mode='readonly') scdata = hdulist[1].data hdulist.close() scstart = scdata.field('START') scstop = scdata.field('STOP') time_filter = (tstart <= scstart) & (scstop <= tstop) redo = True print "Checking for good times in the event file..." while redo: redo = False scstartssplit = np.array_split(scstart[time_filter],int(bins)) scstopssplit = np.array_split(scstop[time_filter],bins) #Explicitly set the first and last point to the values in the evfile header scstartssplit[0][0] = tstart scstopssplit[-1][-1] = tstop starts = [st[0] for st in scstartssplit] stops = [st[-1] for st in scstopssplit] for interval in zip(starts,stops): if verbose: print "Looking at interval",interval[0],"to",interval[1] good_times = False #grrrr. some bug in pyfits doesn't let me do this the python way... for gti_i in range(len(gti_data)): if(not good_times): if verbose: print " Checking gti",gti_data[gti_i]['START'],"to",gti_data[gti_i]['STOP'] gti_starts = interval[0] <= gti_data[gti_i]['START'] <= interval[1] gti_stops = interval[0] < gti_data[gti_i]['STOP'] <= interval[1] if verbose: print " Does this gti start inside this interval? ", gti_starts if verbose: print " Does this gti stop inside this interval? ", gti_stops good_times = gti_starts or gti_stops if verbose: print if verbose: print " Are there good times inside this interval? ", good_times if not good_times: redo = True if verbose: print if redo: if bins <= 1: print "No good time intervals found. Bailing..." sys.exit(1) print "One (or more) of the slices doesn't have a GTI." print "Reducing the number of threads from ",bins,"to",bins-1 bins -= 1 scfiles = [SCFile for st in scstartssplit] evfiles = [EVFile for st in scstartssplit] zmaxes = [zmax for st in scstartssplit] pool = Pool(processes=bins) times = np.array([starts,stops,scfiles,evfiles,zmaxes]) print "Spawning {} jobs...".format(bins) tempfilenames = pool.map(ltcube,times.transpose()) print "Combining temporary files..." ltsum(tempfilenames, OutFile, SaveTemp)
def run(**kwargs): if(len(kwargs.keys())==0): #Nothing specified, the user needs just help! thisCommand.getHelp() return pass #Get parameters values thisCommand.setParValuesFromDictionary(kwargs) try: ra = thisCommand.getParValue('ra') dec = thisCommand.getParValue('dec') particlemodel = thisCommand.getParValue('particle_model') galacticmodel = thisCommand.getParValue('galactic_model') sourcemodel = thisCommand.getParValue('source_model') filteredeventfile = thisCommand.getParValue('filteredeventfile') xmlmodel = thisCommand.getParValue('xmlmodel') triggername = thisCommand.getParValue('triggername') ft2file = thisCommand.getParValue('ft2file') fglmode = thisCommand.getParValue('fgl_mode') 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 pass #Get the IRF from the event file try: f = pyfits.open(filteredeventfile) except: raise GtBurstException(31,"Could not open filtered event file %s" %(filteredeventfile)) tstart = float(f[0].header['_TMIN']) tstop = float(f[0].header['_TMAX']) irf = str(f[0].header['_IRF']) ra = float(f[0].header['_ROI_RA']) dec = float(f[0].header['_ROI_DEC']) roi = float(f[0].header['_ROI_RAD']) #Lookup table for the models models = {} if(particlemodel=='isotr with pow spectrum'): models['isotr with pow spectrum'] = LikelihoodComponent.IsotropicPowerlaw() elif(particlemodel=='isotr template'): models['isotr template'] = LikelihoodComponent.IsotropicTemplate(irf) pass if(galacticmodel=='template'): models['template'] = LikelihoodComponent.GalaxyAndExtragalacticDiffuse(irf,ra,dec,2.5*roi) elif(galacticmodel=='template (fixed norm.)'): models['template (fixed norm.)'] = LikelihoodComponent.GalaxyAndExtragalacticDiffuse(irf,ra,dec,2.5*roi) models['template (fixed norm.)'].fixNormalization() pass deltat = numpy.sum(f['GTI'].data.field('STOP')-f['GTI'].data.field('START')) f.close() triggertime = dataHandling.getTriggerTime(filteredeventfile) if(irf.lower().find('source')>=0 and particlemodel!='isotr template'): raise GtBurstException(6,"Do not use '%s' as model for the particle background in SOURCE class. Use '%s' instead." %(particlemodel,'isotropic template')) modelsToUse = [LikelihoodComponent.PointSource(ra,dec,triggername,sourcemodel)] if(particlemodel!='none'): if(particlemodel=='bkge'): if(ft2file is None or ft2file==''): raise ValueError("If you want to use the BKGE, you have to provide an FT2 file!") modelsToUse.append(LikelihoodComponent.BKGETemplate(filteredeventfile, ft2file,tstart,tstop,triggername,triggertime)) else: modelsToUse.append(models[particlemodel]) if(galacticmodel!='none'): modelsToUse.append(models[galacticmodel]) xml = LikelihoodComponent.LikelihoodModel() xml.addSources(*modelsToUse) xml.writeXML(xmlmodel) if(fglmode=='complete'): #Use a very large delta T so that #all FGL sources are included in the #xml model deltat = 1e12 xml.add2FGLsources(ra,dec,float(roi)+8.0,xmlmodel,deltat) dataHandling._writeParamIntoXML(xmlmodel,IRF=irf,OBJECT=triggername,RA=ra,DEC=dec) return 'xmlmodel', xmlmodel
def __init__(self,ft1file,skyimage,figure,obj_ra=None,obj_dec=None): self.skyimage = skyimage ft1 = pyfits.open(ft1file) self.events = ft1['EVENTS'].data self.empty = False if(len(self.events)==0): print("No events in FT1 file %s" %(ft1file)) self.empty = True #raise RuntimeError("No events in FT1 file %s" %(ft1file)) pass self.obj_ra = obj_ra self.obj_dec = obj_dec #Read in the different classes of events self.trigtime = dataHandling.getTriggerTime(ft1file) time = self.events.field("TIME") energy = self.events.field("ENERGY") if(not self.empty): self.tmin = min(time)-self.trigtime self.tmax = max(time)-self.trigtime self.energyMin = min(energy) self.energyMax = max(energy) else: self.tmin = float(ft1['EVENTS'].header['TSTART']) self.tmax = float(ft1['EVENTS'].header['TSTOP']) self.energyMin = 100 self.energyMax = 1e7 pass #Get the reprocessing self.reprocVer = str(ft1[0].header['PROC_VER']) self.generateColorMap() #Print a summary irfs = numpy.array(map(lambda x:IRFS.fromEvclassToIRF(self.reprocVer,x),self.events.field("EVENT_CLASS"))) print("") for irf in IRFS.PROCS[self.reprocVer]: try: n = irfs[irfs==irf].shape[0] except: n = 0 pass print("%-50s %s" %("Class %s only:" % irf,n)) self.pickerID = None self.oldxmin = -1e9 self.oldxmax = 1e9 self.oldymin = -1e9 self.oldymax = 1e9 self.user_ra = None self.user_dec = None self.evtext = None self.figure = figure self.figure.clear() self.displayImage() self.initEventDisplay() self.displayEvents() self.figure.canvas.draw() self.connectEvents() ft1.close()
def __init__(self, ft1file, skyimage, figure, obj_ra=None, obj_dec=None): self.skyimage = skyimage ft1 = pyfits.open(ft1file) self.events = ft1['EVENTS'].data self.empty = False if (len(self.events) == 0): print("No events in FT1 file %s" % (ft1file)) self.empty = True #raise RuntimeError("No events in FT1 file %s" %(ft1file)) pass self.obj_ra = obj_ra self.obj_dec = obj_dec #Read in the different classes of events self.trigtime = dataHandling.getTriggerTime(ft1file) time = self.events.field("TIME") energy = self.events.field("ENERGY") if (not self.empty): self.tmin = min(time) - self.trigtime self.tmax = max(time) - self.trigtime self.energyMin = min(energy) self.energyMax = max(energy) else: self.tmin = float(ft1['EVENTS'].header['TSTART']) self.tmax = float(ft1['EVENTS'].header['TSTOP']) self.energyMin = 100 self.energyMax = 1e7 pass #Get the reprocessing self.reprocVer = str(ft1[0].header['PROC_VER']) self.generateColorMap() #Print a summary irfs = numpy.array( map(lambda x: IRFS.fromEvclassToIRF(self.reprocVer, x), self.events.field("EVENT_CLASS"))) print("") for irf in IRFS.PROCS[self.reprocVer]: try: n = irfs[irfs == irf].shape[0] except: n = 0 pass print("%-50s %s" % ("Class %s only:" % irf, n)) self.pickerID = None self.oldxmin = -1e9 self.oldxmax = 1e9 self.oldymin = -1e9 self.oldymax = 1e9 self.user_ra = None self.user_dec = None self.evtext = None self.figure = figure self.figure.clear() self.displayImage() self.initEventDisplay() self.displayEvents() self.figure.canvas.draw() self.connectEvents() ft1.close()
def run(**kwargs): if(len(kwargs.keys())==0): #Nothing specified, the user needs just help! thisCommand.getHelp() return pass #Get parameters values thisCommand.setParValuesFromDictionary(kwargs) try: eventfile = thisCommand.getParValue('eventfile') rspfile = thisCommand.getParValue('rspfile') ft2file = thisCommand.getParValue('ft2file') ra = thisCommand.getParValue('ra') dec = thisCommand.getParValue('dec') rad = thisCommand.getParValue('rad') irf = thisCommand.getParValue('irf') zmax = thisCommand.getParValue('zmax') tstart = thisCommand.getParValue('tstart') tstop = thisCommand.getParValue('tstop') emin = thisCommand.getParValue('emin') emax = thisCommand.getParValue('emax') skybinsize = thisCommand.getParValue('skybinsize') outfile = thisCommand.getParValue('skymap') strategy = thisCommand.getParValue('strategy') thetamax = float(thisCommand.getParValue('thetamax')) allowEmpty = _yesOrNoToBool(thisCommand.getParValue('allowEmpty')) clobber = _yesOrNoToBool(thisCommand.getParValue('clobber')) verbose = _yesOrNoToBool(thisCommand.getParValue('verbose')) figure = thisCommand.getParValue('figure') except KeyError as err: print("\n\nERROR: Parameter %s not found or incorrect! \n\n" %(err.args[0])) #Print help print thisCommand.getHelp() return pass from GtBurst import dataHandling global lastDisplay LATdata = dataHandling.LATData(eventfile,rspfile,ft2file) if(strategy.lower()=="time"): #gtmktime cut filteredFile,nEvents = LATdata.performStandardCut(ra,dec,rad,irf,tstart,tstop,emin,emax,zmax,thetamax,True,strategy='time') elif(strategy.lower()=="events"): #no gtmktime cut, Zenith cut applied directly to the events filteredFile,nEvents = LATdata.performStandardCut(ra,dec,rad,irf,tstart,tstop,emin,emax,zmax,thetamax,True,strategy='events') pass LATdata.doSkyMap(outfile,skybinsize) #Now open the output file and get exposure and total number of events skymap = pyfits.open(outfile) totalNumberOfEvents = numpy.sum(skymap[0].data) totalTime = numpy.sum(skymap['GTI'].data.field('STOP')-skymap['GTI'].data.field('START')) skymap.close() print("\nTotal number of events in the counts map: %s" %(totalNumberOfEvents)) print("Total time in Good Time Intervals: %s" %(totalTime)) if((totalTime==0) and allowEmpty==False): raise GtBurstException(2,"Your filter resulted in zero exposure. \n\n" + " Loose your cuts, or enlarge the time interval. You might want to "+ " check also the navigation plots (in the Tools menu) to make sure "+ " that the ROI is within the LAT FOV in the desired time interval.") displ = None if(figure is not None): if(lastDisplay is not None): lastDisplay.unbind() pass from GtBurst.InteractiveFt1Display import InteractiveFt1Display lastDisplay = InteractiveFt1Display(filteredFile,outfile,figure,ra,dec) pass return 'skymap', outfile, 'filteredeventfile', filteredFile, 'irf', irf, 'eventDisplay', lastDisplay
def run(**kwargs): if (len(kwargs.keys()) == 0): #Nothing specified, the user needs just help! thisCommand.getHelp() return pass #Get parameters values thisCommand.setParValuesFromDictionary(kwargs) try: eventfile = thisCommand.getParValue('eventfile') rspfile = thisCommand.getParValue('rspfile') ft2file = thisCommand.getParValue('ft2file') ra = thisCommand.getParValue('ra') dec = thisCommand.getParValue('dec') rad = thisCommand.getParValue('rad') irf = thisCommand.getParValue('irf') zmax = thisCommand.getParValue('zmax') tstart = thisCommand.getParValue('tstart') tstop = thisCommand.getParValue('tstop') emin = thisCommand.getParValue('emin') emax = thisCommand.getParValue('emax') skybinsize = thisCommand.getParValue('skybinsize') outfile = thisCommand.getParValue('skymap') strategy = thisCommand.getParValue('strategy') thetamax = float(thisCommand.getParValue('thetamax')) allowEmpty = _yesOrNoToBool(thisCommand.getParValue('allowEmpty')) clobber = _yesOrNoToBool(thisCommand.getParValue('clobber')) verbose = _yesOrNoToBool(thisCommand.getParValue('verbose')) figure = thisCommand.getParValue('figure') except KeyError as err: print("\n\nERROR: Parameter %s not found or incorrect! \n\n" % (err.args[0])) #Print help print thisCommand.getHelp() return pass from GtBurst import dataHandling global lastDisplay LATdata = dataHandling.LATData(eventfile, rspfile, ft2file) if (strategy.lower() == "time"): #gtmktime cut filteredFile, nEvents = LATdata.performStandardCut(ra, dec, rad, irf, tstart, tstop, emin, emax, zmax, thetamax, True, strategy='time') elif (strategy.lower() == "events"): #no gtmktime cut, Zenith cut applied directly to the events filteredFile, nEvents = LATdata.performStandardCut(ra, dec, rad, irf, tstart, tstop, emin, emax, zmax, thetamax, True, strategy='events') pass LATdata.doSkyMap(outfile, skybinsize) #Now open the output file and get exposure and total number of events skymap = pyfits.open(outfile) totalNumberOfEvents = numpy.sum(skymap[0].data) totalTime = numpy.sum(skymap['GTI'].data.field('STOP') - skymap['GTI'].data.field('START')) skymap.close() print("\nTotal number of events in the counts map: %s" % (totalNumberOfEvents)) print("Total time in Good Time Intervals: %s" % (totalTime)) if ((totalTime == 0) and allowEmpty == False): raise GtBurstException( 2, "Your filter resulted in zero exposure. \n\n" + " Loose your cuts, or enlarge the time interval. You might want to " + " check also the navigation plots (in the Tools menu) to make sure " + " that the ROI is within the LAT FOV in the desired time interval." ) displ = None if (figure is not None): if (lastDisplay is not None): lastDisplay.unbind() pass from GtBurst.InteractiveFt1Display import InteractiveFt1Display lastDisplay = InteractiveFt1Display(filteredFile, outfile, figure, ra, dec) pass return 'skymap', outfile, 'filteredeventfile', filteredFile, 'irf', irf, 'eventDisplay', lastDisplay
required=True) parser.add_argument("--outfile", help="File for the results (will be overwritten)", type=str, required=True) #Main code if __name__ == "__main__": args = parser.parse_args() if (not os.path.exists(args.infile)): raise RuntimeError("File %s does not exist" % (args.infile)) #Read input file with pyfits.open(args.infile) as f: data = f['EVENTS'].data tstart = f['EVENTS'].header.get("TSTART") tstop = f['EVENTS'].header.get("TSTOP") time = data.field("TIME") #This is probably useless, but sometimes input files #are not time ordered and the BB algorithm would #crash time.sort() BayesianBlocks.logger.setLevel(logging.DEBUG)