I want to implement the following trigger function in Python:

Contribution:

- time vector t [n dimensional numpy vector]
- vector of data y [n dimensional numpy vector] (the values correspond to the vector t)
- threshold tr [float]
- Threshold type vector tr_type [m dimensional list of int values]

Exit:

- Tr_time threshold time vector [m dimensional list of float values]

A function:

I would like to return tr_time which consists of the exact time values (preferable also interpolated which are not yet in the code below) to which tr is crossed tr (crossing means going from less to greater or vice versa). The different values in tr_time correspond to the vector tr_type: the elements of tr_type indicate the number of the crossing and if it is a crossing upstream or downstream. For example, 1 means that the first time it passes from less than tr to greater than tr, -3 means that the third time that it passes from more than tr to less than tr (third time along the time vector t)

At the moment I have the following code:

```
import numpy as np
import matplotlib.pyplot as plt
def trigger (t, y, tr, tr_type):
triggermarker = np.diff (1 * (y> tr))
positive indicators = [i for i, x in enumerate(triggermarker) if x == 1]
negativeindices = [i for i, x in enumerate(triggermarker) if x == -1]
triggertime = []
for i in tr_type:
if i> = 0:
triggertime.append (t[positiveindices[i - 1]])
elif i <0:
triggertime.append (t[negativeindices[i - 1]])
return triggertime
t = np.linspace (0, 20, 1000)
y = np.sin (t)
tr = 0.5
tr_type = [1, 2, -2]
print (trigger (t, y, tr, tr_type))
plt.plot (t, y)
plt.grid ()
```

Now that I'm pretty new to Python, I'm wondering if there's a more pythonic and efficient way to implement that. For example, without loop and no need to write separate code for upstream or downstream crossings.

Thank you!