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 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 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 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 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 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 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()