#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""
Produce a dayplot from seismogram recordings

Subversion information:
$Id$

license: gpl3
Copyright 2012-2020 Seismological Observatory, Ruhr-University Bochum
http://www.gmg.ruhr-uni-bochum.de/geophysik/seisobs
Contributors:
    Martina Rische <martina.rische@rub.de>
    Kasper D. Fischer <kasper.fischer@rub.de>
    Sebastian Wehling-Benatelli <sebastian.wehling@rub.de>

This program is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option)
any later version.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License along
with this program. If not, see http://www.gnu.org/licenses/.
"""


def trace_dayplot(st, ftype='bandpass', fmin=1.0, fmax=7.0,
                  col=('b', 'r', 'g'), interval=20.0, outpattern=''):
    """

    :type st: object
    :type ftype: str
    :type fmin: float
    :type fmax: float
    :type col: tuple
    :type interval: float
    :type outpattern: str
    """

    # filter
    if (ftype == 'bandpass') or (ftype == 'bandstop'):
        st.filter(ftype, freqmin=fmin, freqmax=fmax)
    elif (ftype == 'lowpass') or (ftype == 'highpass'):
        st.filter(ftype, freq=fmin)
    st.merge()

    # plot
    for i in range(0, len(st)):
        if outpattern != '':
            imagefile = outpattern.format(st[i].stats.network,
                                          st[i].stats.station,
                                          st[i].stats.channel,
                                          st[i].stats.starttime.year,
                                          st[i].stats.starttime.julday)
            st[i].plot(type='dayplot', color=col, interval=interval,
                       outfile=imagefile)
        else:
            st[i].plot(type='dayplot', color=col, interval=interval)


# __main__
if __name__ == "__main__":

    # parse arguments
    import argparse
    from obspy.core import read

    parser = argparse.ArgumentParser(
        description='Produce filtered 24h-plot (dayplot).',
        epilog='$Rev: 403 $ ($Date: 2012-04-13 12:16:22 +0200 (Fri, 13 Apr 2012) $, $Author: kasper $)')
    parser.add_argument('-v', '-V', '--version', action='version',
                        version='$Rev: 403 $ ($Date: 2012-04-13 12:16:22 +0200 (Fri, 13 Apr 2012) $, $Author: kasper $)')
    parser.add_argument('file', action='store', metavar='FILE', nargs='+',
                        help='File(s) to use for the dayplot. One dayplot will be used for every file. \
              Use wildcards to use multiple file for one plot')
    parser.add_argument('-f', '--filter', action='store', dest='ftype',
                        default='bandpass',
                        choices=['none', 'bandpass', 'bandstop', 'lowpass', 'highpass'],
                        help='Select filtertype to filter the data (default: bandpass)\
              Note: For low- and highpass only fmin is used.')
    parser.add_argument('--fmin', action='store', type=float, dest='fmin',
                        default=1.0,
                        help='Lower frequency of the filter in Hz (default: 1.0)')
    parser.add_argument('--fmax', action='store', type=float, dest='fmax',
                        default=7.0,
                        help='Upper frequency of the filter in Hz (default: 7.0)')
    parser.add_argument('-c', '--color', '--colour', action='store', dest='color',
                        default=('b', 'r', 'g'),
                        help='Color selection to use in the dayplot (default: brg)')
    parser.add_argument('-i', '--interval', action='store', type=int, dest='interval',
                        default=20,
                        help='Interval length to show in each line of the dayplot in minutes (default: 20)')
    parser.add_argument('-o', '--output', action='store', dest='outpattern',
                        default='',
                        help="Output filename pattern for the plot. (default: unset, show plot on screen only), \
              Use {0} to substitute network code, \
                  {1} to substitute station code, \
                  {2} to substitute station channel, \
                  {3} to substitute year, \
                  {4} to substitute doy number \
                  .format (supported formats: emf, eps, pdf, png, ps, raw, rgba, svg, svgz)")

    cla = parser.parse_args()
    if cla.fmin < 0:
        cla.fmin = -cla.fmin
    if cla.fmax < 0:
        cla.fmax = -cla.fmax

    # call trace_dayplot(...) for each file
    for datafile in cla.file:
        trace_dayplot(read(datafile), cla.ftype, cla.fmin, cla.fmax, cla.color,
                      cla.interval, cla.outpattern)