phases = photTable.readWhere('(wvlStart < wavelength) & (wavelength < wvlEnd)')['phase']
photFile.close()

simDs = np.load(trialsPath)['dMetrics']
bUseSimulationFpp=False

nPhotons = len(phases)
print nPhotons,'real photons read'

D,pval = kuiper(phases)
simFpp = 1.*np.sum(simDs>=D) / len(simDs)

print 'kuiper test'
print 'D,fpp:',D,pval
print 'simulated fpp:',simFpp
print nSigma(1-pval),'sigmas'
print nSigma(1-simFpp),'sigmas (sim)'

nSigmaThreshold = 5.
nPhaseBins = 20
phaseBinEdges = np.linspace(0.,1.,nPhaseBins+1)
profile,_ = np.histogram(phases,bins=phaseBinEdges)
profileErrors = np.sqrt(profile)

magG = 17.93
photonIncrement = 1000

#Inject Sine first
nSigmaSig = 0.
nFakePhotons = 0
while nSigmaSig < nSigmaThreshold:
    print 'sorted phases'


i = 0
print 'sorted:',issorted(allPhotons['phase'])
for iWvlStart,wvlStart in enumerate(wvlLimits[:-1]):
    for iWvlEnd,wvlEnd in enumerate(wvlLimits[iWvlStart+1:]):
        print 'cut wavelengths to range ({},{})'.format(wvlStart,wvlEnd)
        mask = numexpr.evaluate('(wvlStart < allWavelengths) & (allWavelengths < wvlEnd)')
        phases = allPhotons['phase'][mask]

        nPhotons = len(phases)
        print nPhotons,'photons in range'

        D,pval = kuiper(phases,assumeSorted=True)
        sig = nSigma(1-pval)
        print 'kuiper test'
        print 'D,fpp:',D,pval
        print sig,'sigmas'

        metrics[i]['wvlStart'] = wvlStart
        metrics[i]['wvlEnd'] = wvlEnd
        metrics[i]['D'] = D
        metrics[i]['fpp'] = pval
        metricImg[iWvlStart,iWvlEnd+iWvlStart+1] = D
        fppImg[iWvlStart,iWvlEnd+iWvlStart+1] = pval
        sigmaImg[iWvlStart,iWvlEnd+iWvlStart+1] = sig

        del phases
        i+=1
        nWvlStarts = len(wvlLimits) - 1
        nWvlLimits = len(wvlLimits)

        iWvlPair = 0
        for iWvlSet in xrange(len(data)//nWvlPairs):
            metricImg = np.zeros((nWvlLimits,nWvlLimits))
            fppImg = np.zeros((nWvlLimits,nWvlLimits))
            sigmaImg = np.zeros((nWvlLimits,nWvlLimits))
            for iWvlStart,wvlStart in enumerate(wvlLimits[:-1]):
                for iWvlEnd,wvlEnd in enumerate(wvlLimits[iWvlStart+1:]):
                    assert wvlStart == wvlStartList[iWvlPair]
                    assert wvlEnd == wvlStopList[iWvlPair]

                    h = hMetrics[iWvlPair]
                    pval = h_fpp(h)
                    sig = nSigma(1-pval)
                    
                    metricImg[iWvlStart,iWvlEnd+iWvlStart+1] = h
                    fppImg[iWvlStart,iWvlEnd+iWvlStart+1] = pval
                    sigmaImg[iWvlStart,iWvlEnd+iWvlStart+1] = sig
                    iWvlPair += 1
            metricImgs.append(metricImg)
            fppImgs.append(fppImg)
            sigmaImgs.append(sigmaImg)

metricImgs = np.array(metricImgs)
fppImgs = np.array(fppImgs)
sigmaImgs = np.array(sigmaImgs)
metricImgs = metricImgs[0:1000] #trim to just 1000 trials

nTrials = len(metricImgs)