# Path: seispy/decon.py
# class RFTrace(obspy.Trace):
#     def __init__(self, data=..., header=None):
#         super().__init__(data=data, header=header)
# 
#     @classmethod
#     def deconvolute(cls, utr, wtr, method='iter', **kwargs):
#         header = utr.stats.__getstate__()
#         for key, value in kwargs.items():
#             header[key] = value
#         if method.lower() == 'iter':
#             rf, rms, it = deconit(utr.data, wtr.data, utr.stats.delta, **kwargs)
#             header['rms'] = rms
#             header['iter'] = it
#         elif method.lower() == 'water':
#             rf, rms = deconwater(utr.data, wtr.data, utr.stats.delta, **kwargs)
#             header['rms'] = rms
#             header['iter'] = np.nan
#         else:
#             raise ValueError('method must be \'iter\' or \'water\'')
#         return cls(rf, header)
# 
# Path: seispy/geo.py
# def snr(x, y):
#     spow = rssq(x)**2
#     npow = rssq(y)**2
#     if npow == 0:
#         npow = 0.001
#     return 10 * np.log10(spow / npow)
# 
# def srad2skm(srad):
#     sdeg = srad * ((2*pi)/360)
#     return sdeg / deg2km(1)
# 
# def rotateSeisENtoTR(E, N, BAZ):
#     angle = mod(BAZ+180, 360)
#     R = N*cosd(angle) + E*sind(angle)
#     T = E*cosd(angle) - N*sind(angle)
#     return T, R
# 
# def skm2srad(skm):
#     sdeg = skm * deg2km(1)
#     return rad2deg(sdeg)
# 
# def rssq(x):
#     return np.sqrt(np.sum(x**2)/len(x))
# 
# def extrema(x, opt='max'):
#     if opt == 'max':
#         idx = np.intersect1d(np.where(np.diff(x) > 0)[0]+1, np.where(np.diff(x) < 0)[0])
#     elif opt == 'min':
#         idx = np.intersect1d(np.where(np.diff(x) < 0)[0]+1, np.where(np.diff(x) > 0)[0])
#     else:
#         raise ValueError('opt must be \'max\' or \'min\'')
#     return idx


import numpy as np
import obspy
from numpy.lib.arraysetops import isin
from obspy.io.sac import SACTrace
from obspy.signal.rotate import rotate2zne, rotate_zne_lqt
from scipy.signal import resample
from os.path import dirname, join, expanduser
from seispy.decon import RFTrace
from seispy.geo import snr, srad2skm, rotateSeisENtoTR, skm2srad, \
                       rssq, extrema
from obspy.signal.trigger import recursive_sta_lta
    def decon_s(self, tshift, **kwargs):
        if self.comp == 'lqt':
            win = self.st.select(channel='*Q')[0]
            uin = self.st.select(channel='*L')[0]
        else:
            win = self.st.select(channel='*R')[0]
            uin = self.st.select(channel='*Z')[0]
            win.data *= -1
        # win.data[0:int((tshift-4)/win.stats.delta)] = 0
        uout = RFTrace.deconvolute(uin, win, phase='S', tshift=tshift, **kwargs)
        uout.data = np.flip(uout.data)
        self.rf.append(uout)

    def saverf(self, path, evtstr=None, phase='P', shift=0, evla=-12345., evlo=-12345., evdp=-12345., mag=-12345.,
               gauss=0, baz=-12345., gcarc=-12345., only_r=False, **kwargs):
        if phase == 'P':
            if self.comp == 'lqt':
                svcomp = 'Q'
            else:
                svcomp = 'R'
            if only_r:
                loop_lst = [svcomp]
            else:
                loop_lst = [svcomp, 'T']
            rayp = srad2skm(self.PArrival.ray_param)
        elif phase == 'S':
            if self.comp == 'lqt':
                loop_lst = ['L']
            else:
                loop_lst = ['Z']

# Path: seispy/decon.py
# class RFTrace(obspy.Trace):
#     def __init__(self, data=..., header=None):
#         super().__init__(data=data, header=header)
# 
#     @classmethod
#     def deconvolute(cls, utr, wtr, method='iter', **kwargs):
#         header = utr.stats.__getstate__()
#         for key, value in kwargs.items():
#             header[key] = value
#         if method.lower() == 'iter':
#             rf, rms, it = deconit(utr.data, wtr.data, utr.stats.delta, **kwargs)
#             header['rms'] = rms
#             header['iter'] = it
#         elif method.lower() == 'water':
#             rf, rms = deconwater(utr.data, wtr.data, utr.stats.delta, **kwargs)
#             header['rms'] = rms
#             header['iter'] = np.nan
#         else:
#             raise ValueError('method must be \'iter\' or \'water\'')
#         return cls(rf, header)
# 
# Path: seispy/geo.py
# def snr(x, y):
#     spow = rssq(x)**2
#     npow = rssq(y)**2
#     if npow == 0:
#         npow = 0.001
#     return 10 * np.log10(spow / npow)
# 
# def srad2skm(srad):
#     sdeg = srad * ((2*pi)/360)
#     return sdeg / deg2km(1)
# 
# def rotateSeisENtoTR(E, N, BAZ):
#     angle = mod(BAZ+180, 360)
#     R = N*cosd(angle) + E*sind(angle)
#     T = E*cosd(angle) - N*sind(angle)
#     return T, R
# 
# def skm2srad(skm):
#     sdeg = skm * deg2km(1)
#     return rad2deg(sdeg)
# 
# def rssq(x):
#     return np.sqrt(np.sum(x**2)/len(x))
# 
# def extrema(x, opt='max'):
#     if opt == 'max':
#         idx = np.intersect1d(np.where(np.diff(x) > 0)[0]+1, np.where(np.diff(x) < 0)[0])
#     elif opt == 'min':
#         idx = np.intersect1d(np.where(np.diff(x) < 0)[0]+1, np.where(np.diff(x) > 0)[0])
#     else:
#         raise ValueError('opt must be \'max\' or \'min\'')
#     return idx


# Path: seispy/eq.py
import numpy as np
import obspy
from numpy.lib.arraysetops import isin
from obspy.io.sac import SACTrace
from obspy.signal.rotate import rotate2zne, rotate_zne_lqt
from scipy.signal import resample
from os.path import dirname, join, expanduser
from seispy.decon import RFTrace
from seispy.geo import snr, srad2skm, rotateSeisENtoTR, skm2srad, \
                       rssq, extrema
from obspy.signal.trigger import recursive_sta_lta

    def decon_s(self, tshift, **kwargs):
        if self.comp == 'lqt':
            win = self.st.select(channel='*Q')[0]
            uin = self.st.select(channel='*L')[0]
        else:
            win = self.st.select(channel='*R')[0]
            uin = self.st.select(channel='*Z')[0]
            win.data *= -1
        # win.data[0:int((tshift-4)/win.stats.delta)] = 0
        uout = RFTrace.deconvolute(uin, win, phase='S', tshift=tshift, **kwargs)
        uout.data = np.flip(uout.data)
        self.rf.append(uout)

    def saverf(self, path, evtstr=None, phase='P', shift=0, evla=-12345., evlo=-12345., evdp=-12345., mag=-12345.,
               gauss=0, baz=-12345., gcarc=-12345., only_r=False, **kwargs):
        if phase == 'P':
            if self.comp == 'lqt':
                svcomp = 'Q'
            else:
                svcomp = 'R'
            if only_r:
                loop_lst = [svcomp]
            else:
                loop_lst = [svcomp, 'T']
            rayp = srad2skm(self.PArrival.ray_param)
        elif phase == 'S':
            if self.comp == 'lqt':
                loop_lst = ['L']
            else:
                loop_lst = ['Z']

            rayp = srad2skm(self.SArrival.ray_param)