#!/usr/bin/env python
# Live feedback of a PSCAN
# Tracker feedback for clicked positons
#raise Exception("Needs Testing...!")
# built-in:
import os
import sys
import time
import thread


if "--log" in sys.argv:
    mynorm = "log"
elif "--gamma" in sys.argv:
    iarg = sys.argv.index("--gamma")
    mynorm = float(sys.argv[iarg+1])
    #mynorm = "linear"
else:
    mynorm = "linear"

# community:
import numpy as np
import matplotlib.pyplot as pl

if "--gray" in sys.argv:
    cmap = pl.cm.gray
else:
    cmap = pl.cm.jet

try:
    #raise #tests
    from id01lib import PScanTracker, SpecClientWrapper
except:
    print("trying local import.")
    sys.path.insert(0, os.path.join(os.path.abspath(os.pardir)))
    from id01lib import PScanTracker, SpecClientWrapper


def thread1(threadName,tracker):
    """
        Outsource the live update
    """
    count=0
    while count<1000:
        time.sleep(3.)
        tracker.reload()


specname = "nano3:psic_nano"

specsession = SpecClientWrapper.SpecClientSession(specname=specname, verbose=False)




fig = pl.figure()
ax = fig.add_subplot(111)
tracker = PScanTracker.PScanTracker(ax, specsession, norm=mynorm)
tracker.im.set_cmap(cmap)
try:
    thread.start_new_thread(thread1,('thread-1',tracker))
except Exception as emsg:
    print("Error: %s"%emsg)

pl.show()
