# 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
#        ipick = opts.qcpara.ichdrs[0]
#        wpick = 't'+str(opts.reltime)
        ipick = 't'+sortby[2]
        wpick = 't'+sortby[1]
        if ipick == wpick:
            print('Same time pick: {0:s} and {1:s}. Exit'.format(ipick, wpick))
            sys.exit()
        gsac.selist, gsac.delist = qualsort.sortSeisHeaderDiff(gsac.saclist, ipick, wpick, sortincrease)
    elif sortby.isdigit() or sortby in opts.qheaders + ['all',]: # by quality factors
        if sortby == '0' or sortby == 'all':
            opts.qweights = [1./3, 1./3, 1./3]
            print('Sort by all quality factors: ccc+snr+coh')
        elif sortby == '1' or sortby == 'ccc':
            opts.qweights = [1, 0, 0]
            print('Sort by quality factor ccc (Cross Correlation Coefficient)')
        elif sortby == '2' or sortby == 'snr':
            opts.qweights = [0, 1, 0]
            print('Sort by quality factor snr (Signal/Noise Ratio)')
        elif sortby == '3' or sortby == 'coh':
            opts.qweights = [0, 0, 1]
            print('Sort by quality factor coh (Coherence)')
        gsac.selist, gsac.delist = qualsort.sortSeisQual(gsac.saclist, opts.qheaders, opts.qweights, opts.qfactors, sortincrease)
    else: # by a given header
        gsac.selist, gsac.delist = qualsort.sortSeisHeader(gsac.saclist, sortby, sortincrease)
    return


def paraDataOpts(opts, ifiles):
    'Common parameters, data and options'
    pppara = ttconfig.PPConfig()

# 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

#        ipick = opts.qcpara.ichdrs[0]
#        wpick = 't'+str(opts.reltime)
        ipick = 't'+sortby[2]
        wpick = 't'+sortby[1]
        if ipick == wpick:
            print('Same time pick: {0:s} and {1:s}. Exit'.format(ipick, wpick))
            sys.exit()
        gsac.selist, gsac.delist = qualsort.sortSeisHeaderDiff(gsac.saclist, ipick, wpick, sortincrease)
    elif sortby.isdigit() or sortby in opts.qheaders + ['all',]: # by quality factors
        if sortby == '0' or sortby == 'all':
            opts.qweights = [1./3, 1./3, 1./3]
            print('Sort by all quality factors: ccc+snr+coh')
        elif sortby == '1' or sortby == 'ccc':
            opts.qweights = [1, 0, 0]
            print('Sort by quality factor ccc (Cross Correlation Coefficient)')
        elif sortby == '2' or sortby == 'snr':
            opts.qweights = [0, 1, 0]
            print('Sort by quality factor snr (Signal/Noise Ratio)')
        elif sortby == '3' or sortby == 'coh':
            opts.qweights = [0, 0, 1]
            print('Sort by quality factor coh (Coherence)')
        gsac.selist, gsac.delist = qualsort.sortSeisQual(gsac.saclist, opts.qheaders, opts.qweights, opts.qfactors, sortincrease)
    else: # by a given header
        gsac.selist, gsac.delist = qualsort.sortSeisHeader(gsac.saclist, sortby, sortincrease)
    return


def paraDataOpts(opts, ifiles):
    'Common parameters, data and options'
    pppara = ttconfig.PPConfig()

    qcpara = ttconfig.QCConfig()