from obspy.clients.seedlink.easyseedlink import create_client
from obspy import UTCDateTime
import matplotlib.pyplot as plt
import numpy as np

CHN = 'EHN'
SEC = 60
SPS = 100

data = []
time = []

now = UTCDateTime()

def handle_data(trace):
    print(trace)
    global time, data, now
    data += [i for i in trace.data]
    time += [i for i in trace.times(reftime=now)]

#client = create_client('192.168.1.108:18000', handle_data)
#client.select_stream('LK','GE008','HNN')

client = create_client('antmac.crs.inogs.it:18000', handle_data)
client.select_stream('ST', 'ZIAN', 'EHN')

import threading
th = threading.Thread(target=client.run)
th.daemon = True
th.start()

while 1:
    if data:
        tlen = SEC*SPS
        if len(data) > tlen:
            tlen = len(data)

        tx = time[-tlen:]
        dx = data[-tlen:]

        plt.cla()
        plt.plot(tx, dx - np.mean(dx), 'k', linewidth=1)
        plt.xlabel('Time (s)', fontsize=12, fontweight='bold')
        plt.ylabel('Amplitude', fontsize=12, fontweight='bold')
        plt.xlim(min(tx), max(tx))
        plt.show(block=False)
        plt.pause(0.5)


