# Path: pysmo/aimbat/qualsort.py
# def initQual(saclist, hdrsel, qheaders):
# def sortQual(saclist, qheaders, qweights, increase=True):
# def seleSeis(saclist):
# def sortSeisQual(saclist, qheaders, qweights, qfactors, increase=True):
# def sortSeisHeaderDiff(saclist, hdr0, hdr1, increase=True):
# def hdrtype(sacdh, hdr):
# def sortSeisHeader(saclist, hdr, increase=True):
# def getOptions():
# 
# Path: pysmo/aimbat/ttconfig.py
# def getDefaults():
#     def __init__(self):
#     def __init__(self):
# def _load_xcorr_func(func):
#     def __init__(self):
#     def __init__(self):
# def getParser():
# class PPConfig:
# class QCConfig:
# class CCConfig:
# class MCConfig:
# 
# Path: pysmo/aimbat/sacpickle.py
# def zipFile(zipmode='gz'):
# def fileZipMode(ifilename):
# def writePickle(d, picklefile, zipmode=None):
# def readPickle(picklefile, zipmode=None):
#     def __init__(self, ifile, delta=-1):
#     def resampleData(self, delta):
#     def gethdr(self, hdr):
#     def sethdr(self, hdr, val):
#     def writeHdrs(self):
#     def sethdrs(self, sacobj):
#     def savesac(self):
#     def __init__(self, ifiles, delta=-1):
#     def resampleData(self, delta):
# def resampleSeis(data, deltaold, delta):
# def sac2obj(ifiles, delta=-1):
# def sac2pkl(ifiles, pkfile='sac.pkl', delta=-1, zipmode='gz'):
# def obj2sac(gsac):
# def pkl2sac(pkfile, zipmode):
# def _days(year):
# def date2jul(year, mon, day):
# def jul2date(year, jday):
# def taper(data, taperwidth=0.1, tapertype='hanning'):
# def taperWindow(timewindow, taperwidth=0.1):
# def windowIndex(saclist, reftimes, timewindow=(-5.0,5.0), taperwindow=1.0):
# def windowData(saclist, nstart, ntotal, taperwidth, tapertype='hanning', datatype='data'):
# def windowTime(saclist, nstart, ntotal, taperwidth, tapertype='hanning'):
# def windowTimeData(saclist, nstart, ntotal, taperwidth, tapertype='hanning'):
# def loadData(ifiles, opts, para):
# def saveData(gsac, opts):
# def getOptions():
# def main():
# class SacDataHdrs:
# class SacGroup:
#     class BreakIt(Exception): pass
# 
# Path: pysmo/aimbat/filtering.py
# def filtering_time_freq(originalTime, originalSignalTime, delta, filterType, highFreq, lowFreq, order, runReversePass = False):
# def get_filter_params(delta, lowFreq, highFreq, filterType, order, MULTIPLE=3000):
# def filtering_time_signal(originalSignalTime, delta, lowFreq, highFreq, filterType, order, MULTIPLE, runReversePass = False):
# def time_to_freq(originalTime, originalSignalTime, delta):
# 	MULTIPLE = 0.7*max(np.abs(originalSignalFreq))
# 	NYQ = 1.0/(2*delta)
# 	B, A = signal.butter(order, Wn, analog=False, btype='bandpass')
# 		B, A = signal.butter(order, Wn, analog=False, btype='lowpass')
# 		B, A = signal.butter(order, Wn, analog=False, btype='highpass')


import numpy as np
import sys
from pysmo.aimbat import qualsort, ttconfig
from pysmo.aimbat import sacpickle as sacpkl
from pysmo.aimbat import filtering as ftr
    if sacdh.gethdr(pppara.fhdrLowFreq) != -12345.:
        filterParameters['lowFreq'] = sacdh.gethdr(pppara.fhdrLowFreq)
    if sacdh.gethdr(pppara.fhdrHighFreq) != -12345.:
        filterParameters['highFreq'] = sacdh.gethdr(pppara.fhdrHighFreq)
    if sacdh.gethdr(pppara.fhdrBand) != '-1234567':
        filterParameters['band'] =  sacdh.gethdr(pppara.fhdrBand)
    if int(sacdh.gethdr(pppara.fhdrOrder)) != -12345:
        filterParameters['order'] = int(sacdh.gethdr(pppara.fhdrOrder))
    filterParameters['apply'] = sacdh.gethdr(pppara.fhdrApply)==1
    filterParameters['reversepass'] = sacdh.gethdr(pppara.fhdrRevPass)==1
    return filterParameters

def setFilterPara(sacdh, pppara, filterParameters):
    """
    Set filter parameters dict to sacdh.
    """
    sacdh.sethdr(pppara.fhdrApply,    int(filterParameters['apply']))
    sacdh.sethdr(pppara.fhdrBand,     filterParameters['band'])
    sacdh.sethdr(pppara.fhdrLowFreq,  filterParameters['lowFreq'])
    sacdh.sethdr(pppara.fhdrHighFreq, filterParameters['highFreq'])
    sacdh.sethdr(pppara.fhdrOrder,    filterParameters['order'])
    sacdh.sethdr(pppara.fhdrRevPass,  int(filterParameters['reversepass']))
    return

def seisApplyFilter(saclist, filtParas):
    'Filter seismograms by butterworth filter'
    for sacdh in saclist:
        origTimeT = sacdh.time
        origDataT = sacdh.data
        origTimeF, origDataF = ftr.time_to_freq(origTimeT, origDataT, sacdh.delta)

# Path: pysmo/aimbat/qualsort.py
# def initQual(saclist, hdrsel, qheaders):
# def sortQual(saclist, qheaders, qweights, increase=True):
# def seleSeis(saclist):
# def sortSeisQual(saclist, qheaders, qweights, qfactors, increase=True):
# def sortSeisHeaderDiff(saclist, hdr0, hdr1, increase=True):
# def hdrtype(sacdh, hdr):
# def sortSeisHeader(saclist, hdr, increase=True):
# def getOptions():
# 
# Path: pysmo/aimbat/ttconfig.py
# def getDefaults():
#     def __init__(self):
#     def __init__(self):
# def _load_xcorr_func(func):
#     def __init__(self):
#     def __init__(self):
# def getParser():
# class PPConfig:
# class QCConfig:
# class CCConfig:
# class MCConfig:
# 
# Path: pysmo/aimbat/sacpickle.py
# def zipFile(zipmode='gz'):
# def fileZipMode(ifilename):
# def writePickle(d, picklefile, zipmode=None):
# def readPickle(picklefile, zipmode=None):
#     def __init__(self, ifile, delta=-1):
#     def resampleData(self, delta):
#     def gethdr(self, hdr):
#     def sethdr(self, hdr, val):
#     def writeHdrs(self):
#     def sethdrs(self, sacobj):
#     def savesac(self):
#     def __init__(self, ifiles, delta=-1):
#     def resampleData(self, delta):
# def resampleSeis(data, deltaold, delta):
# def sac2obj(ifiles, delta=-1):
# def sac2pkl(ifiles, pkfile='sac.pkl', delta=-1, zipmode='gz'):
# def obj2sac(gsac):
# def pkl2sac(pkfile, zipmode):
# def _days(year):
# def date2jul(year, mon, day):
# def jul2date(year, jday):
# def taper(data, taperwidth=0.1, tapertype='hanning'):
# def taperWindow(timewindow, taperwidth=0.1):
# def windowIndex(saclist, reftimes, timewindow=(-5.0,5.0), taperwindow=1.0):
# def windowData(saclist, nstart, ntotal, taperwidth, tapertype='hanning', datatype='data'):
# def windowTime(saclist, nstart, ntotal, taperwidth, tapertype='hanning'):
# def windowTimeData(saclist, nstart, ntotal, taperwidth, tapertype='hanning'):
# def loadData(ifiles, opts, para):
# def saveData(gsac, opts):
# def getOptions():
# def main():
# class SacDataHdrs:
# class SacGroup:
#     class BreakIt(Exception): pass
# 
# Path: pysmo/aimbat/filtering.py
# def filtering_time_freq(originalTime, originalSignalTime, delta, filterType, highFreq, lowFreq, order, runReversePass = False):
# def get_filter_params(delta, lowFreq, highFreq, filterType, order, MULTIPLE=3000):
# def filtering_time_signal(originalSignalTime, delta, lowFreq, highFreq, filterType, order, MULTIPLE, runReversePass = False):
# def time_to_freq(originalTime, originalSignalTime, delta):
# 	MULTIPLE = 0.7*max(np.abs(originalSignalFreq))
# 	NYQ = 1.0/(2*delta)
# 	B, A = signal.butter(order, Wn, analog=False, btype='bandpass')
# 		B, A = signal.butter(order, Wn, analog=False, btype='lowpass')
# 		B, A = signal.butter(order, Wn, analog=False, btype='highpass')


# Path: pysmo/aimbat/prepdata.py
import numpy as np
import sys
from pysmo.aimbat import qualsort, ttconfig
from pysmo.aimbat import sacpickle as sacpkl
from pysmo.aimbat import filtering as ftr

    if sacdh.gethdr(pppara.fhdrLowFreq) != -12345.:
        filterParameters['lowFreq'] = sacdh.gethdr(pppara.fhdrLowFreq)
    if sacdh.gethdr(pppara.fhdrHighFreq) != -12345.:
        filterParameters['highFreq'] = sacdh.gethdr(pppara.fhdrHighFreq)
    if sacdh.gethdr(pppara.fhdrBand) != '-1234567':
        filterParameters['band'] =  sacdh.gethdr(pppara.fhdrBand)
    if int(sacdh.gethdr(pppara.fhdrOrder)) != -12345:
        filterParameters['order'] = int(sacdh.gethdr(pppara.fhdrOrder))
    filterParameters['apply'] = sacdh.gethdr(pppara.fhdrApply)==1
    filterParameters['reversepass'] = sacdh.gethdr(pppara.fhdrRevPass)==1
    return filterParameters

def setFilterPara(sacdh, pppara, filterParameters):
    """
    Set filter parameters dict to sacdh.
    """
    sacdh.sethdr(pppara.fhdrApply,    int(filterParameters['apply']))
    sacdh.sethdr(pppara.fhdrBand,     filterParameters['band'])
    sacdh.sethdr(pppara.fhdrLowFreq,  filterParameters['lowFreq'])
    sacdh.sethdr(pppara.fhdrHighFreq, filterParameters['highFreq'])
    sacdh.sethdr(pppara.fhdrOrder,    filterParameters['order'])
    sacdh.sethdr(pppara.fhdrRevPass,  int(filterParameters['reversepass']))
    return

def seisApplyFilter(saclist, filtParas):
    'Filter seismograms by butterworth filter'
    for sacdh in saclist:
        origTimeT = sacdh.time
        origDataT = sacdh.data
        origTimeF, origDataF = ftr.time_to_freq(origTimeT, origDataT, sacdh.delta)

        filtDataT, filtDataF, adjw, adjh = ftr.filtering_time_freq(origTimeT, origDataT, sacdh.delta, filtParas['band'], filtParas['highFreq'], filtParas['lowFreq'], filtParas['order'], filtParas['reversepass'])