allPhotons = allPhotons[sortIndices]
    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
wvlStart = 4000.
wvlEnd = 5500.
photFile = tables.openFile(path,'r')
photTable = photFile.root.photons.photTable
i = 0
print 'cut wavelengths to range ({},{})'.format(wvlStart,wvlEnd)
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