def makeDistDistrib(x, bLo, bHi):
    nb = len(x)
    retVal = numpy.zeros(len(bLo), dtype=float)
    for i in range(0, nb):
        t0 = pyCommon.arrayAnd([x[i] >= bLo, x[i] < bHi])
        t1 = numpy.where(t0)[0]
        if len(t1) != 1:
            print '*** Could not bin: %f' % x[i]
        retVal[t1] += 1
    return retVal
def makeDistDistrib(x, bLo, bHi):
    nb = len(x)
    retVal = numpy.zeros(len(bLo), dtype=float)
    for i in range(0, nb):
        t0 = pyCommon.arrayAnd([x[i] >= bLo, x[i] < bHi])
        t1 = numpy.where(t0)[0]
        if len(t1) != 1:
            print '*** Could not bin: %f' % x[i]
        retVal[t1] += 1
    return retVal
for i in range(0, nTrim):
    if trimMags[i] > trimMagThresh: continue
    if trimTypes[i] == galaxyType: c0 = 'green'
    elif trimTypes[i] == wDType: c0 = 'cyan'
    elif trimTypes[i] == kuruczType: c0 = 'blue'
    elif trimTypes[i] == mLTType: c0 = 'red'
    elif trimTypes[i] == sSMType: c0 = 'yellow'
    else: raise RuntimeError, '*** Unknown SED Type: %s' % trimTypes[i]
    regOut.write('box %5.5f %5.5f 20 20 0 # color=%s\n' % (
        trimxs[i], trimys[i], c0))

# execfile('compareTrimEImage.py')

print 'Matching ref catalog to trim cat.'
t0 = numpy.where(refMags < refMagThresh)[0]
t1 = pyCommon.arrayAnd([trimMags < trimMagThresh, trimTypes != galaxyType,
                        trimTypes != sSMType])
t2 = numpy.where(t1)[0]
bestRefTrimXOffset, bestRefTrimYOffset, t1, t2 = \
                 matchTrimToDet.searchTrimDetMatch(
    refxs[t0], refys[t0], trimxs[t2], trimys[t2])
print 'Found match dx: %5.2f, dy: %5.2f pix' % (
    bestRefTrimXOffset, bestRefTrimYOffset)
offrefxsTrim = refxs + bestRefTrimXOffset
offrefysTrim = refys + bestRefTrimYOffset

# Now recalculate for the final pixDist and trimToRef
refPixDistsTrim, trimToRef, t0, t1 = matchTrimToDet.matchTrimToDet(
    offrefxsTrim, offrefysTrim, trimxs, trimys)

simplePlot.clear()
simplePlot.plot(trimxs, trimys, f='ko')
for i in range(0, nTrim):
    if trimMags[i] > trimMagThresh: continue
    if trimTypes[i] == galaxyType: c0 = 'green'
    elif trimTypes[i] == wDType: c0 = 'cyan'
    elif trimTypes[i] == kuruczType: c0 = 'blue'
    elif trimTypes[i] == mLTType: c0 = 'red'
    elif trimTypes[i] == sSMType: c0 = 'yellow'
    else: raise RuntimeError, '*** Unknown SED Type: %s' % trimTypes[i]
    regOut.write('box %5.5f %5.5f 20 20 0 # color=%s\n' %
                 (trimxs[i], trimys[i], c0))

# execfile('compareTrimEImage.py')

print 'Matching ref catalog to trim cat.'
t0 = numpy.where(refMags < refMagThresh)[0]
t1 = pyCommon.arrayAnd(
    [trimMags < trimMagThresh, trimTypes != galaxyType, trimTypes != sSMType])
t2 = numpy.where(t1)[0]
bestRefTrimXOffset, bestRefTrimYOffset, t1, t2 = \
                 matchTrimToDet.searchTrimDetMatch(
    refxs[t0], refys[t0], trimxs[t2], trimys[t2])
print 'Found match dx: %5.2f, dy: %5.2f pix' % (bestRefTrimXOffset,
                                                bestRefTrimYOffset)
offrefxsTrim = refxs + bestRefTrimXOffset
offrefysTrim = refys + bestRefTrimYOffset

# Now recalculate for the final pixDist and trimToRef
refPixDistsTrim, trimToRef, t0, t1 = matchTrimToDet.matchTrimToDet(
    offrefxsTrim, offrefysTrim, trimxs, trimys)

simplePlot.clear()
simplePlot.plot(trimxs, trimys, f='ko')