# 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/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/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/prepdata.py
# def dataNorm(d, w=0.05):
# def dataNormWindow(d, t, twindow):
# def axLimit(minmax, w=0.05):
# def findPhase(filename):
# def getFilterPara(sacdh, pppara):
# def setFilterPara(sacdh, pppara, filterParameters):
# def seisApplyFilter(saclist, filtParas):
# def seisUnApplyFilter(saclist):
# def seisTimeData(saclist):
# def seisTimeRefr(saclist, opts):
# def seisTimeWindow(saclist, twhdrs):
# def sacDataNorm(sacdh, opts):
# def seisDataNorm(saclist, opts):
# def seisWave(sacdh):
# def seisDataBaseline(gsac):
# def prepData(gsac, opts):
# def prepStack(opts):
# def seisSort(gsac, opts):
# def paraDataOpts(opts, ifiles):


import os
import sys
import copy
    import matplotlib.pyplot as plt
from optparse import OptionParser
from numpy import array, ones, zeros, sqrt, dot, corrcoef, mean, transpose, linspace
from numpy import linalg as LA
from pysmo.aimbat import ttconfig
from pysmo.aimbat import qualsort
from pysmo.aimbat import sacpickle as sacpkl
from pysmo.aimbat import prepdata  as pdata
                      help='Run ICCS with -a option but initially use all seismograms.')
    parser.add_option('-q', '--minqual', dest='minqual', type='float', nargs=3,
                      help='Minimum quality factor (ccc,snr,coh) for auto selection. '
                      'Defaults are {:.2f} {:.2f} {:.2f}.'.format(minccc, minsnr, mincoh))
    parser.add_option('-n', '--minnsel', dest='minnsel', type='int',
                      help='Minimum number of selected seismograms for auto selection. '
                      'Default is {:d}.'.format(minnsel))
    opts, files = parser.parse_args(sys.argv[1:])
    if not files:
        print(parser.usage)
        sys.exit()
    return opts, files

def corrmax(datai, dataj, delta, xcorr, shift):
    """ Calculate time lag at maximum cross correlation between two time series.
    """
    delay, ccmax, ccpol = xcorr(datai, dataj, shift)
    return delay*delta, ccmax, ccpol

def meanStack(data, taperwidth, tapertype):
    """ Calculate array stack by averaging without weighting.
    """
    sdata = mean(data, 0)
    sdata = sacpkl.taper(sdata, taperwidth, tapertype)
    return sdata

def weightStack(data, wgts, taperwidth, tapertype):
    """ Calculate array stack by averaging with weighting.
    """
    sdata = mean(transpose(data) * wgts, 1)

# 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/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/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/prepdata.py
# def dataNorm(d, w=0.05):
# def dataNormWindow(d, t, twindow):
# def axLimit(minmax, w=0.05):
# def findPhase(filename):
# def getFilterPara(sacdh, pppara):
# def setFilterPara(sacdh, pppara, filterParameters):
# def seisApplyFilter(saclist, filtParas):
# def seisUnApplyFilter(saclist):
# def seisTimeData(saclist):
# def seisTimeRefr(saclist, opts):
# def seisTimeWindow(saclist, twhdrs):
# def sacDataNorm(sacdh, opts):
# def seisDataNorm(saclist, opts):
# def seisWave(sacdh):
# def seisDataBaseline(gsac):
# def prepData(gsac, opts):
# def prepStack(opts):
# def seisSort(gsac, opts):
# def paraDataOpts(opts, ifiles):


# Path: pysmo/aimbat/algiccs.py
import os
import sys
import copy
    import matplotlib.pyplot as plt
from optparse import OptionParser
from numpy import array, ones, zeros, sqrt, dot, corrcoef, mean, transpose, linspace
from numpy import linalg as LA
from pysmo.aimbat import ttconfig
from pysmo.aimbat import qualsort
from pysmo.aimbat import sacpickle as sacpkl
from pysmo.aimbat import prepdata  as pdata

                      help='Run ICCS with -a option but initially use all seismograms.')
    parser.add_option('-q', '--minqual', dest='minqual', type='float', nargs=3,
                      help='Minimum quality factor (ccc,snr,coh) for auto selection. '
                      'Defaults are {:.2f} {:.2f} {:.2f}.'.format(minccc, minsnr, mincoh))
    parser.add_option('-n', '--minnsel', dest='minnsel', type='int',
                      help='Minimum number of selected seismograms for auto selection. '
                      'Default is {:d}.'.format(minnsel))
    opts, files = parser.parse_args(sys.argv[1:])
    if not files:
        print(parser.usage)
        sys.exit()
    return opts, files

def corrmax(datai, dataj, delta, xcorr, shift):
    """ Calculate time lag at maximum cross correlation between two time series.
    """
    delay, ccmax, ccpol = xcorr(datai, dataj, shift)
    return delay*delta, ccmax, ccpol

def meanStack(data, taperwidth, tapertype):
    """ Calculate array stack by averaging without weighting.
    """
    sdata = mean(data, 0)
    sdata = sacpkl.taper(sdata, taperwidth, tapertype)
    return sdata

def weightStack(data, wgts, taperwidth, tapertype):
    """ Calculate array stack by averaging with weighting.
    """
    sdata = mean(transpose(data) * wgts, 1)

    sdata = sacpkl.taper(sdata, taperwidth, tapertype)