def centroidObs(obsPath,centroidPath,centroidRa,centroidDec,haOffset,xGuess,yGuess,savePath,tstamp): obs = ObsFile(obsPath) # if not os.path.exists(hotPath): # hp.findHotPixels(obsFile=obs,outputFileName=hotPath) obs.loadAllCals() # obs.loadHotPixCalFile(hotPath,switchOnMask=True) # obs.loadBestWvlCalFile() # obs.loadFlatCalFile(flatPath) obs.setWvlCutoffs(3000,11000) obs.loadCentroidListFile(centroidPath) ctrdFile = obs.centroidListFile sliceTimes = ctrdFile.root.centroidlist.times.read() xPositions = ctrdFile.root.centroidlist.xPositions.read() yPositions = ctrdFile.root.centroidlist.yPositions.read() intTime = sliceTimes[1]-sliceTimes[0] for iTime,time in enumerate(sliceTimes): x = xPositions[iTime] y = yPositions[iTime] title='centroid_{}_{}s'.format(tstamp,time) imgDict = obs.getPixelCountImage(firstSec=time,integrationTime=intTime,weighted=True) imgPath=os.path.join(savePath,title+'.png') pop = PopUp(showMe=False) pop.plotArray(imgDict['image'],title=title) pop.axes.plot(x,y,color='g',marker='d') pop.fig.savefig(imgPath) print 'saved to',imgPath del obs
def showArrayStdVsIntTime(self): intTimes = [1,2,3,5,10,15,30] sdevVsIntTime = [] madVsIntTime = [] medVsIntTime = [] for intTime in intTimes: image = np.zeros((self.nRow,self.nCol)) for iOb,ob in enumerate(self.skyObList): x = ob.getPixelCountImage(firstSec=0,integrationTime=intTime) image+=x['image'] hotPixMask = hotPixels.checkInterval(image=image)['mask'] image[hotPixMask!=0]=0 countList = image[image!=0] sdevVsIntTime.append(np.std(countList)) madVsIntTime.append(np.median(np.abs(countList-np.median(countList)))) medVsIntTime.append(np.median(countList)) PopUp(parent=self).plotArray(image,title=r'%d std=%f mad=%f med=%f'%(intTime,sdevVsIntTime[-1],madVsIntTime[-1],medVsIntTime[-1])) medVsIntTime = np.array(medVsIntTime) sqrtNVsIntTime = np.sqrt(medVsIntTime) pop = PopUp(parent=self,title='showArrayStdVsIntTime') pop.axes.set_xlabel('integration time (s)') pop.axes.set_ylabel('$\sigma$') pop.axes.plot(intTimes,sqrtNVsIntTime,'k--',label=r'$\sqrt(med(N))$') pop.axes.plot(intTimes,sdevVsIntTime,'k') pop.axes.plot(intTimes,madVsIntTime,'r') pop.draw()
def phaseMap(tile,nPhases,nSlopes,maxSlope=10,displayMap=False,minFreqSep=.5): phases = np.linspace(0.,2.*np.pi,nPhases) slopes = np.linspace(0.5,maxSlope,nSlopes) pmap = np.zeros((nSlopes,nPhases)) for iSlope,slope in enumerate(slopes): if iSlope % 100 == 0: print iSlope for iPhase,phase in enumerate(phases): tileWithGrad = addGradient(tile,slope,phase) #plotArray(tileWithGrad,title='slope {} angle {}'.format(slope,phase)) pmap[iSlope,iPhase] = countCollisions(tileWithGrad,minFreqSep=minFreqSep) percentMap = 100.*pmap/len(np.ravel(tile)) print 'median unusable pixels (%) above slope {}: {}'.format(slopes[0],np.median(percentMap.ravel())) print 'sdev unusable pixels (%) above slope {}: {}'.format(slopes[0],np.std(percentMap.ravel())) print 'min,max unusable pixels (%) above slope {}: {} {}'.format(slopes[0],np.min(percentMap.ravel()),np.max(percentMap.ravel())) hist,binEdges = np.histogram(percentMap,bins=150) plt.plot(binEdges[0:-1],hist) plt.show() if displayMap: pop = PopUp(showMe=False) pop.plotArray(percentMap,cbarLabel='Unusable Pixels (%)') xticks = np.array(pop.axes.get_xticks(),dtype=np.int) yticks = np.array(pop.axes.get_yticks(),dtype=np.int) print xticks print yticks pop.axes.set_xticklabels(['{:.0f}'.format(phase*180./np.pi) for phase in phases[xticks[0:-1]]]) pop.axes.set_yticklabels(['{:.1f}'.format(slope) for slope in slopes[yticks[0:-1]]]) pop.axes.set_xlabel('Gradient Angle (${}^{\circ}$)') pop.axes.set_ylabel('Gradient Slope (MHz/pixel)') pop.axes.xaxis.tick_bottom() pop.show() return {'numberUnusablePixels':pmap,'gradientAnglesDeg':(phases*180./np.pi),'gradientSlopesMHz':slopes,'percentUnusable':percentMap}
def showTwilightPixelSpectrum(self,row,col): spectrum = self.twilightSpectra[row,col] pop = PopUp(parent=self,title='showTwilightPixelSpectrum') pop.axes.step(self.wvlBinEdges[:-1],spectrum,where='post') pop.axes.set_xlabel(r'$\lambda$ ($\AA$)') pop.axes.set_ylabel(r'total counts') pop.axes.set_title('twilight spectrum (%d,%d) '%(row,col)) pop.draw()
def showPixelRawBaselineHist(self,row,col): baselines = np.array([],dtype=np.double) for iOb,ob in enumerate(self.obList): x = ob.getTimedPacketList(row,col) baselines=np.append(baselines,np.array(x['baselines'],dtype=np.double)) pop = PopUp(parent=self,title='showPixelRawBaselineHist') nBins=np.max(baselines)-np.min(baselines) histBaselines,binEdges = np.histogram(baselines,bins=nBins) pop.axes.step(binEdges[:-1],histBaselines,where='post') pop.axes.set_xlabel('baseline') pop.axes.set_title('Baselines') pop.draw()
def showPixelRawPeakHist(self,row,col): peaks = np.array([],dtype=np.double) for iOb,ob in enumerate(self.obList): x = ob.getTimedPacketList(row,col) peaks = np.append(peaks,np.array(x['peakHeights'],dtype=np.double)) pop = PopUp(parent=self,title='showPixelRawPeakHist') nBins=np.max(peaks)-np.min(peaks) histPeaks,binEdges = np.histogram(peaks,bins=nBins) pop.axes.step(binEdges[:-1],histPeaks,where='post') pop.axes.set_xlabel('peak') pop.axes.set_title('Packet Peaks (No Baseline Subtracted)') pop.draw()
def showTwilightPixelStdVsFlux(self,row,col): spectrumVsFluxVsTime = [] for iOb,ob in enumerate(self.twilightObList): spectrumInTime = [] for sec in range(0,ob.getFromHeader('exptime'),self.twilightIntTime): x = ob.getPixelSpectrum(pixelRow=row,pixelCol=col,firstSec=sec,integrationTime=self.twilightIntTime,weighted=True) spectrum = x['spectrum'] binEdges = x['wvlBinEdges'] spectrum = np.convolve(spectrum,np.ones(self.rebinSpecBins),'same')[self.firstAfterConvolve::self.rebinSpecBins] spectrumInTime.append(spectrum) spectrumInTime = np.array(spectrumInTime) spectrumVsFluxVsTime.append(spectrumInTime) spectrumVsFluxVsTime = np.array(spectrumVsFluxVsTime) #resulting array indexed as #spectrumVsFluxVsTime[iOb][iTimeChunk][iWvlBin] #sum over wavelength for total counts countsVsFluxVsTime = [np.sum(spectrumInTime,axis=1) for spectrumInTime in spectrumVsFluxVsTime] #countsVsFluxVsTime[iFlux][iTimeChunk] countStds = [np.std(countsVsTime) for countsVsTime in countsVsFluxVsTime] fluxes = [np.median(countsVsTime) for countsVsTime in countsVsFluxVsTime] fluxes = np.array(fluxes) countStds = np.array(countStds) countSqrts = [np.sqrt(np.median(countsVsTime)) for countsVsTime in countsVsFluxVsTime] countSqrts = np.array(countSqrts) spectrumStds = [np.std(spectrumVsTime,axis=0) for spectrumVsTime in spectrumVsFluxVsTime] spectrumSqrts = [np.sqrt(np.median(spectrumVsTime,axis=0)) for spectrumVsTime in spectrumVsFluxVsTime] spectrumStds = np.array(spectrumStds) spectrumSqrts = np.array(spectrumSqrts) pop = PopUp(parent=self,title='showTwilightPixelStdVsFlux') pop.axes.set_xlabel('median counts') pop.axes.set_ylabel('normalized $\sigma$') pop.axes.plot(fluxes,countSqrts/np.max(countSqrts),'k--', label=r'$\sqrt{N}$') pop.axes.plot(fluxes,countStds/np.max(countSqrts),'k', label=r'%d-%d $\AA$'%(self.rebinnedWvlEdges[0],self.rebinnedWvlEdges[-1])) nBins = np.shape(spectrumStds)[1] for iBin in xrange(nBins): pop.axes.plot(fluxes, spectrumStds[:,iBin]/np.max(spectrumSqrts[:,iBin]), c=cm.jet((iBin+1.0)/nBins), label=r'%d-%d $\AA$'%(self.rebinnedWvlEdges[iBin], self.rebinnedWvlEdges[iBin+1])) pop.axes.legend(loc='upper left') pop.axes.set_title('Normalized Standard Deviation vs Twilight Flux, (%d,%d)'%(row,col)) pop.draw()
def showPixelLightCurve(self,row,col): lightCurve = [] for iOb,ob in enumerate(self.obList): for sec in range(0,ob.getFromHeader('exptime'),self.intTime): x = ob.getPixelCount(iRow=row,iCol=col,firstSec=sec,integrationTime=self.intTime,weighted=True) counts = x['counts']/self.intTime lightCurve.append(counts) pop = PopUp(parent=self,title='showPixelLightCurve') times=np.arange(0,len(lightCurve)*self.intTime,self.intTime) pop.axes.set_xlabel('time (s)') pop.axes.set_ylabel('cps') pop.axes.plot(times,lightCurve,c='k') pop.axes.set_title('Light Curve (%d,%d)'%(row,col)) pop.draw()
def showPixelLaserSpectrum(self,row,col): #First plot the laser cal spectrum for this pixel to see if it's good x = self.cal.getTimedPacketList(row,col) phases=np.array(x['peakHeights'],dtype=np.double)-np.array(x['baselines'],dtype=np.double) pop = PopUp(parent=self,title='showPixelLaserSpectrum') nBins=np.max(phases)-np.min(phases) histPhases,binEdges = np.histogram(phases,bins=nBins) lambdaBinEdges = self.cal.convertToWvl(binEdges,row,col,excludeBad=True) pop.axes.set_xlabel(r'$\lambda$ ($\AA$)') if len(lambdaBinEdges)==0: #no wavecal for this pixel, so lambdaBinEdges came back empty lambdaBinEdges = binEdges pop.axes.set_xlabel('phase (ADU)') pop.axes.step(lambdaBinEdges[:-1],histPhases,where='post',color='k') pop.axes.set_ylabel('counts') pop.axes.set_title('Raw Laser Cal Spectrum (%d,%d)'%(row,col)) wvlCalSigma = self.cal.wvlErrorTable[row,col] xOffset = self.cal.wvlCalTable[row,col,0] yOffset = self.cal.wvlCalTable[row,col,1] amplitude = self.cal.wvlCalTable[row,col,2] #energy(eV) = amplitude*(pulseHeight-xOffset)**2+yOffset stackLabel = 'obs' run = self.params['run'] sunsetDate = self.params[stackLabel+'SunsetDate'] calTimestamp = self.params[stackLabel+'WvlTimestamp'] wvlDriftFileName = FileName(run=run,date=sunsetDate,tstamp=calTimestamp).calDriftInfo() wvlDriftFile = tables.openFile(wvlDriftFileName,mode='r') wvlDriftInfo = wvlDriftFile.root.params_drift.driftparams.read() wvlDriftFile.close() driftEntry = wvlDriftInfo[np.logical_and(wvlDriftInfo['pixelrow']==row ,wvlDriftInfo['pixelcol']==col)][0] #extract gaussianparams in first column of selected row bluePhaseSigma=driftEntry[0][2] bluePhaseAmp = driftEntry[0][1] bluePhaseOffset = driftEntry[0][0] redPhaseSigma=driftEntry[0][5] redPhaseAmp = driftEntry[0][4] redPhaseOffset = driftEntry[0][3] phases = np.linspace(np.min(phases),np.max(phases),(nBins+1)*100.) blueGaussFit = bluePhaseAmp*np.exp(-1/2*((phases-bluePhaseOffset)/bluePhaseSigma)**2) redGaussFit = redPhaseAmp*np.exp(-1/2*((phases-redPhaseOffset)/redPhaseSigma)**2) wavelengths = self.cal.convertToWvl(phases,row,col) if len(wavelengths)==0: #no wavecal for this pixel, so lambdaBinEdges came back empty wavelengths=phases pop.axes.plot(wavelengths,blueGaussFit,'b') pop.axes.plot(wavelengths,redGaussFit,'r') pop.draw()
def showPixelFlatWeights(self,row,col): pop = PopUp(parent=self,title='showPixelFlatWeights') for iFlat,flatInfo in enumerate(self.flatInfos): weights = flatInfo['weights'][row,col] flatSpectra = flatInfo['spectra'][row,col] flatMedians = flatInfo['median'] deltaFlatSpectra = np.sqrt(flatSpectra) deltaWeights = weights*deltaFlatSpectra/flatSpectra color=cm.jet((iFlat+1.)/len(self.flatInfos)) wvlBinCenters = self.wvlBinEdges[:-1]+np.diff(self.wvlBinEdges)/2. pop.axes.plot(self.wvlBinEdges[:-1],weights,linestyle='-',label=self.params['flatInfoFiles'][iFlat],color=color,) pop.axes.errorbar(wvlBinCenters,weights,linestyle=',',yerr=deltaWeights,color=color) pop.axes.set_xlabel(r'$\lambda$ ($\AA$)') pop.axes.set_ylabel(r'Weights') pop.axes.set_title('Flat Weights') pop.axes.legend(loc='lower right') pop.draw()
def showPixelSpectrum(self,row,col): spectrum = self.spectra[row,col] binWidths = np.diff(self.wvlBinEdges) if self.params['showPixelRawSpectrum']: weights = self.flatInfo['weights'][row,col] rawSpectrum = self.spectra[row,col]/weights rawSpectrum/=binWidths spectrum/=binWidths pop = PopUp(parent=self,title='showPixelSpectrum') pop.axes.step(self.wvlBinEdges[:-1],spectrum,label='calibrated',color='b',where='post') if self.params['showPixelRawSpectrum']: pop.axes.step(self.wvlBinEdges[:-1],rawSpectrum,label='raw',color='r',where='post') pop.axes.set_xlabel(r'$\lambda$ ($\AA$)') pop.axes.set_ylabel(r'counts/$\AA$') pop.axes.legend(loc='lower right') pop.axes.set_title('spectrum (%d,%d)'%(row,col)) pop.draw()
def showTwilightPixelDeviationFromMedian(self,row,col): x = self.getChisq(row,col) reducedChisq = x['reducedChisq'] chisq = x['chisq'] percentDiffSpectrum = x['percentDiffSpectrum'] deltaPercentDiffSpectrum = x['deltaPercentDiffSpectrum'] nDeltaFromZero = x['nDeltaFromZero'] degreesOfFreedom = x['degreesOfFreedom'] print 'reduced chisq =',reducedChisq print 'P-value =',1-chi2.cdf(chisq,degreesOfFreedom) pop = PopUp(parent=self,title='showTwilightPixelDeviationFromMedian') pop.axes.errorbar(self.wvlBinEdges[:-1],percentDiffSpectrum,linestyle='-',color='k',yerr=deltaPercentDiffSpectrum) pop.axes.set_xlabel(r'$\lambda$ ($\AA$)') pop.axes.set_ylabel(r'percent difference') pop.axes.plot(self.wvlBinEdges[:-1],len(self.wvlBinEdges[:-1])*[0],'gray') axes2 = pop.axes.twinx() axes2.plot(self.wvlBinEdges[:-1],nDeltaFromZero,'m',alpha=.7) align_yaxis(pop.axes,0,axes2,0) axes2.set_ylabel(r'(pixelSpectrum-avgSpectrum)/$\sigma$',color='m') pop.axes.set_title('Deviation from Avg Spectrum (%d,%d)'%(row,col)) pop.draw() weights = self.flatInfo['weights'][row,col] pop = PopUp(parent=self,title='showTwilightPixelDeviationFromMedian') pop.axes.step(self.wvlBinEdges[:-1],self.averageTwilightSpectrum/self.wvlBinWidths,'k',label='avg') pop.axes.step(self.wvlBinEdges[:-1],self.twilightSpectra[row,col]/self.wvlBinWidths,'b',label='weighted') pop.axes.step(self.wvlBinEdges[:-1],(self.twilightSpectra[row,col]/weights)/self.wvlBinWidths,'r',label='raw') pop.axes.set_xlabel(r'$\lambda$ ($\AA$)') pop.axes.set_ylabel(r'counts per $\AA$') pop.axes.set_title('Twilight Spectrum (%d,%d)'%(row,col)) pop.axes.legend(loc='lower right') pop.draw()
def showPixelWvlLightCurves(self,row,col): spectrumInTime = [] for iOb,ob in enumerate(self.obList): for sec in range(0,ob.getFromHeader('exptime'),self.intTime): x = ob.getPixelSpectrum(pixelRow=row,pixelCol=col,firstSec=sec,integrationTime=self.intTime,weighted=True) spectrum = x['spectrum'] spectrum = np.convolve(spectrum,np.ones(self.rebinSpecBins),'same')[self.firstAfterConvolve::self.rebinSpecBins] spectrumInTime.append(spectrum) spectrumInTime = np.array(spectrumInTime) nBins = np.shape(spectrumInTime)[1] pop = PopUp(parent=self,title='showPixelWvlLightCurves') #plot counts vs time for each wavelength bin times=np.arange(len(spectrumInTime[:,0]))*self.intTime for iBin in xrange(nBins): pop.axes.plot(times,1.0*spectrumInTime[:,iBin]/self.intTime, c=cm.jet((iBin+1.)/nBins), label=r'%d-%d $\AA$'%(self.rebinnedWvlEdges[iBin], self.rebinnedWvlEdges[iBin+1])) pop.axes.set_xlabel('time (s)') pop.axes.set_ylabel('cps') #plot counts vs time summed over all wavelengths pop.axes.legend(loc='upper right') pop.axes.set_title('Light Curve by Band (%d,%d)'%(row,col)) pop.draw()
data = np.load(dataPath) pixelImages = data['pixelImages'] labels = data['obsTimestamps'] apertures = [] psfDicts = [] for iObs,(img,label) in enumerate(zip(pixelImages,labels)): centroidGuess = np.unravel_index(np.argmax(img),np.shape(img)) centroidGuess = centroidGuess[::-1] #switch from (row,col) to (x,y) psfPhot = PSFphotometry(img,centroid=[centroidGuess],verbose=False,showPlot=False) psfDict = psfPhot.PSFfit(aper_radius=5) params = psfDict['parameters'] sigma = psfDict['parameters'][4] flux = psfDict['flux'] optimalApertureRadius = 1.6*sigma print int(psfDict['flag']),label,'sigma',sigma,'flux',flux,'aperture',optimalApertureRadius psfDict2 = data['psfFits'][iObs] print int(psfDict2['flag']),label,'sigma',psfDict2['parameters'][4],'flux',psfDict2['flux'],'aperture',1.6*psfDict2['parameters'][4] apertures.append(optimalApertureRadius) psfDicts.append(psfDict) imgPath = os.path.join(os.path.join(savePath,'pics/'),'apert'+label+'.png') pop = PopUp(showMe=False) pop.plotArray(img,title=label+' \sigma={},f={}'.format(sigma,flux),vmax=np.max(img)) pop.fig.savefig(imgPath) apertures = np.array(apertures)
psfFits = psfFits[tstampMask] dateMasks = [] phaseProfilesByDate = [] for iDate,obsSeq in enumerate(obsSequences): dateMask = np.array([tstamp in obsSeq for tstamp in tstamps]) phaseProfilesOnDate = phaseProfiles[dateMask] combineProfiles(phaseBinEdges,phaseProfilesOnDate,label=sunsetDates[iDate]) dateMasks.append(dateMask) combineProfiles(phaseBinEdges,phaseProfiles,label='total') #print '20140924',tstamps[0:17] #print '20140925',tstamps[17:47] #print '20141021',tstamps[47:] #profileErrors = dataDict['profileErrors'] pop = PopUp(showMe=False) pop.plotArray(phaseProfiles,aspect=2.) pop.axes.set_yticks(np.arange(np.shape(phaseProfiles)[0])) pop.axes.tick_params(axis='both', which='major', labelsize=7) pop.axes.set_yticklabels(tstamps) pop.show() #plotArray(phaseProfiles) # combineProfiles(phaseBinEdges,phaseProfiles,label='total') # combineProfiles(phaseBinEdges,phaseProfiles[0:15],label='20140924') # combineProfiles(phaseBinEdges,phaseProfiles[17:47],label='20140925') # combineProfiles(phaseBinEdges,phaseProfiles[47:],label='20141021') timeProfile = np.mean(phaseProfiles,axis=1) fig,(ax,ax2,ax3,ax4) = plt.subplots(4,1,sharex='col') ax.plot(apertureRadiusList)
except: return '0' yWvlTickLocations = np.append(3600,np.arange(4000,12000,1000)) xWvlTickLocations = np.array([3600,4000,5000,7000,11000]) yIdxTickLocations = np.array([wvlToIdx(wvl) for wvl in yWvlTickLocations]) xIdxTickLocations = np.array([wvlToIdx(wvl) for wvl in xWvlTickLocations]) tickFormatter = FuncFormatter(wvlFormatter) xTickLocator = FixedLocator(xIdxTickLocations) yTickLocator = FixedLocator(yIdxTickLocations) np.savez('metricImg.npz',metricImg=metricImg,wvlLimits=wvlLimits,wvlStartList=wvlStartList,wvlStopList=wvlStopList) pop = PopUp(showMe=False) pop.plotArray(metricImg,cbarLabel='h metric',cbarShrink=.82,cbarAspect=15,cbarPad=.05,vmax=13,cmap='hot') #pop.axes.set_yticks(np.arange(np.shape(wvlLimits)[0])) pop.axes.tick_params(axis='both', which='major', labelsize=11,labelbottom=True,labeltop=False) pop.axes.xaxis.set_major_formatter(tickFormatter) pop.axes.yaxis.set_major_formatter(tickFormatter) pop.axes.xaxis.set_major_locator(xTickLocator) pop.axes.yaxis.set_major_locator(yTickLocator) pop.axes.set_xlabel('upper wavelength limit ($\AA$)') pop.axes.set_ylabel('lower wavelength limit ($\AA$)') pop.fig.subplots_adjust(left=.18,right=.93,top=1.0,bottom=None) pop.fig.set_size_inches(5,4) #pop.fig.set_tight_layout({}) #pop.fig.tight_layout() #pop.axes.set_title('H Metric')
def showArrayLaserImage(self): getImageOutput = self.cal.getPixelCountImage(getRawCount=True,weighted=False) frame = getImageOutput['image'] #self.popUpArray(image=self.rawFrame,title='raw image') pop = PopUp(parent=self,title='showArrayLaserImage') pop.plotArray(image=frame,title='laser cal raw image')