#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''SignalConstructor plugin system'''
import math

from signallib import Bsig, Asig
from signallib.info import MyAnn


def plugin_main(view):
    '''
    Small example how to create new signal and use direct
    access to signal data.

    This method is called via Python button on the toolbar.
    '''

    # create sig3 if not exists
    if 'sig3' not in view:
        sig3 = Asig(nb_steps=view.nbsteps, data=None,
                    name='sig3', note='plugin note',
                    period=view.period)
        view.add_signal(sig3)

        ann = MyAnn(x=1000, y=35, text='This signal is from plugin')
        sig3.info.add_annotation(ann)

    # set values in sig3 according sig1 + superposed sinus
    sig1 = view['sig1']
    sig3 = view['sig3']
    for step in range(view.nbsteps):
        sig3[step] = sig1[step] + 20 * math.sin(step / 3.)

    # you can directly set value on step
    sig3[10] = 150
    sig3.taint()  # graph on the screen will be updated

    # you can set your own message
    msg = 'Added sig3\n(this message comes from plugin)'
    return msg

# another example how to use scipy package to filter signal
# (but you have to copy scipy package next to the executable)
#     from scipy import signal
#     from scipy.signal.filter_design import bilinear
#     from scipy.signal.signaltools import filtfilt
#
#     x = view['sig1'].data
#     T = 10.0  # [s]   http://en.wikipedia.org/wiki/RC_time_constant
#     a = 0.05 / (T * 3.14)
#     b, a = signal.butter(N=1, Wn=a, btype='low', analog=False, output='ba')
#     # y = filtfilt(b, a, x)
#     y = signal.lfilter(b, a, x)  # filtered output
#     sig3.data = y
