#!/usr/bin/env python #Import all of the needed modules import pyLikelihood as pyLike import UnbinnedAnalysis as ua import numpy as np from os import path from gt_apps import filter, maketime, expCube, expMap class LightCurve: """A really simple light curve generator. Not really robust. If you want to really make a light curve, you shouldn't use this. This is just an example of what can be done. The class has an init function and two other functions. If you run the generate_files function then the runLikelihood function you can get some data you can plot.""" def __init__(self,time_start = 239557417., time_end = 248370217, delta_t = 604800.): self.times = np.arange(time_start,time_end,delta_t) self.ra = 238.929 self.dec = 11.1901 self.evclass = 2 self.rad = 10. self.emin = 200. self.emax = 200000. self.irfs = "CALDB" self.SC = "PG1553_SC.fits" self.source = "_2FGLJ1555.7+1111" self.model = "PG1553_lightcurve.xml" self.infile = "PG1553_filtered.fits" #Set up some arrays to store the results self.IntFlux = np.zeros_like(self.times[:-1]) self.IntFluxErr = np.zeros_like(self.times[:-1]) self.Gamma = np.zeros_like(self.times[:-1]) self.GammaErr = np.zeros_like(self.times[:-1]) self.TS = np.zeros_like(self.times[:-1]) self.retCode = np.zeros_like(self.times[:-1]) self.npred = np.zeros_like(self.times[:-1]) def generate_files(self): """This function makes all the needed files like the livetime cube and the exposure map.""" #Loop through the start (times[:-1])/stop(times[1:]) times for i,time in enumerate(zip(self.times[:-1],self.times[1:])): print "**Working on range (%f,%f)" % (time[0],time[1]) print '***Running gtselect***' filter['evclass'] = self.evclass filter['ra'] = self.ra filter['dec'] = self.dec filter['rad'] = self.rad filter['emin'] = self.emin filter['emax'] = self.emax filter['zmax'] = 105 filter['tmin'] = time[0] filter['tmax'] = time[1] filter['infile'] = self.infile filter['outfile'] = 'LC_filtered_bin'+str(i)+'.fits' if(not path.exists('LC_filtered_bin'+str(i)+'.fits')): filter.run() else: print "File exists. Won't execute." print '***Running gtmktime***' maketime['scfile'] = self.SC maketime['filter'] = '(DATA_QUAL==1)&&(LAT_CONFIG==1)' maketime['roicut'] = 'yes' maketime['evfile'] = 'LC_filtered_bin'+str(i)+'.fits' maketime['outfile'] = 'LC_filtered_gti_bin'+str(i)+'.fits' if(not path.exists('LC_filtered_gti_bin'+str(i)+'.fits')): maketime.run() else: print "File exists. Won't execute." print '***Running gtltcube***' expCube['evfile'] = 'LC_filtered_gti_bin'+str(i)+'.fits' expCube['scfile'] = self.SC expCube['outfile'] = 'LC_expCube_bin'+str(i)+'.fits' expCube['dcostheta'] = 0.025 expCube['binsz'] = 1 if(not path.exists('LC_expCube_bin'+str(i)+'.fits')): expCube.run() else: print "File exists. Won't execute." print '***Running gtexpmap***' expMap['evfile'] = 'LC_filtered_gti_bin'+str(i)+'.fits' expMap['scfile'] = self.SC expMap['expcube'] ='LC_expCube_bin'+str(i)+'.fits' expMap['outfile'] ='LC_expMap_bin'+str(i)+'.fits' expMap['irfs'] = self.irfs expMap['srcrad'] =20 expMap['nlong'] =120 expMap['nlat'] =120 expMap['nenergies'] =20 if(not path.exists('LC_expMap_bin'+str(i)+'.fits')): expMap.run() else: print "File exists. Won't execute." def runLikelihood(self): """This function loops back through the files generated by the other function in this class and runs a likelihood anlaysis on them.""" #Loop through the start (times[:-1])/stop(times[1:]) times for i,time in enumerate(zip(self.times[:-1],self.times[1:])): print "**Working on range (%f,%f)" % (time[0],time[1]) print '***Running likelihood analysis***' obs = ua.UnbinnedObs('LC_filtered_gti_bin'+str(i)+'.fits', self.SC, expMap='LC_expMap_bin'+str(i)+'.fits', expCube='LC_expCube_bin'+str(i)+'.fits', irfs=self.irfs) like = ua.UnbinnedAnalysis(obs,self.model,optimizer='NewMinuit') likeObj = pyLike.NewMinuit(like.logLike) like.fit(verbosity=0,covar=True,optObject=likeObj) self.IntFlux[i] = like.flux(self.source,emin=self.emin,emax=self.emax) self.Gamma[i] = like.model[self.source].funcs['Spectrum'].getParam('Index').value() self.IntFluxErr[i] = like.fluxError(self.source,emin=self.emin,emax=self.emax) self.GammaErr[i] = like.model[self.source].funcs['Spectrum'].getParam('Index').error() self.TS[i] = like.Ts(self.source) self.retCode[i] = likeObj.getRetCode() self.npred[i] = like.NpredValue(self.source) #Clean up at the end of this iteration by #deleting some objects and incrementing the #bin start and stop times as well as the bin number del likeObj del like del obs