# Source code for obspy.io.gse2.paz

    #!/usr/bin/env python
    # ------------------------------------------------------------------
    # Filename: paz.py
    #  Purpose: Python routines for reading GSE poles and zero files
    #   Author: Moritz Beyreuther
    #    Email: moritz.beyreuther@geophysik.uni-muenchen.de
    #
    # Copyright (C) 2008-2012 Moritz Beyreuther
    # --------------------------------------------------------------------
    """
    Python routines for reading GSE pole and zero (PAZ) files.

    The read in PAZ information can be used with
    :mod:`~obspy.signal` for instrument correction.

    :copyright:
        The ObsPy Development Team (devs@obspy.org)
    :license:
        GNU Lesser General Public License, Version 3
        (https://www.gnu.org/copyleft/lesser.html)
    """
    from __future__ import (absolute_import, division, print_function,
                            unicode_literals)
    from future.builtins import *  # NOQA
    from future.utils import native_str

    import doctest

    import numpy as np

    from obspy.core import AttribDict

    def read_paz(paz_file):
        '''
        Read GSE PAZ / Calibration file format and returns poles, zeros and the
        seismometer_gain.

        Do not use this function in connection with the ObsPy instrument
        simulation, the A0_normalization_factor might be set wrongly. Use
        :func:`~obspy.io.gse2.libgse2.attach_paz` instead.

        >>> import io
        >>> f = io.StringIO(
        ... """CAL1 RJOB   LE-3D    Z  M24    PAZ 010824 0001
        ... 2
        ... -4.39823 4.48709
        ... -4.39823 -4.48709
        ... 3
        ... 0.0 0.0
        ... 0.0 0.0
        ... 0.0 0.0
        ... 0.4""")
        >>> p, z, k = read_paz(f)
        >>> print('%.4f %.4f %.4f' % (p[0].real, z[0].real, k))
        -4.3982 0.0000 0.4000
        '''
        poles = []
        zeros = []

        if isinstance(paz_file, (str, native_str)):
            with open(paz_file, 'rt') as fh:
                paz = fh.readlines()
        else:
            paz = paz_file.readlines()
        if paz[0][0:4] != 'CAL1':
            raise NameError("Unknown GSE PAZ format %s" % paz[0][0:4])
        if paz[0][31:34] != 'PAZ':
            raise NameError("%s type is not known" % paz[0][31:34])

        ind = 1
        npoles = int(paz[ind])
        for i in range(npoles):
            try:
                poles.append(complex(*[float(n)
                                       for n in paz[i + 1 + ind].split()]))
            except ValueError:
                poles.append(complex(float(paz[i + 1 + ind][:8]),
                                     float(paz[i + 1 + ind][8:])))

        ind += i + 2
        nzeros = int(paz[ind])
        for i in range(nzeros):
            try:
                zeros.append(complex(*[float(n)
                                       for n in paz[i + 1 + ind].split()]))
            except ValueError:
                zeros.append(complex(float(paz[i + 1 + ind][:8]),
                                     float(paz[i + 1 + ind][8:])))

        ind += i + 2
        # in the observatory this is the seismometer gain [muVolt/nm/s]
        # the A0_normalization_factor is hardcoded to 1.0
        seismometer_gain = float(paz[ind])
        return poles, zeros, seismometer_gain

    def attach_paz(tr, paz_file):
        '''
        Attach tr.stats.paz AttribDict to trace from GSE2 paz_file

        This is experimental code, nevertheless it might be useful. It
        makes several assumption on the gse2 paz format which are valid for the
        geophysical observatory in Fuerstenfeldbruck but might be wrong in
        other cases.

        Attaches to a trace a paz AttribDict containing poles zeros and gain.
        The A0_normalization_factor is set to 1.0.

        :param tr: An ObsPy trace object containing the calib and gse2 calper
                attributes
        :param paz_file: path to pazfile or file pointer

        >>> from obspy.core import Trace
        >>> import io
        >>> tr = Trace(header={'calib': .094856, 'gse2': {'calper': 1}})
        >>> f = io.StringIO(
        ... """CAL1 RJOB   LE-3D    Z  M24    PAZ 010824 0001
        ... 2
        ... -4.39823 4.48709
        ... -4.39823 -4.48709
        ... 3
        ... 0.0 0.0
        ... 0.0 0.0
        ... 0.0 0.0
        ... 0.4""")
        >>> attach_paz(tr, f)
        >>> print(round(tr.stats.paz.sensitivity / 10E3) * 10E3)
        671140000.0
        '''
        poles, zeros, seismometer_gain = read_paz(paz_file)

        # remove zero at 0,0j to undo integration in GSE PAZ
        for i, zero in enumerate(list(zeros)):
            if zero == complex(0, 0j):
                zeros.pop(i)
                break
        else:
            raise Exception("Could not remove (0,0j) zero to undo GSE integration")

        # ftp://www.orfeus-eu.org/pub/software/conversion/GSE_UTI/gse2001.pdf
        # page 3
        calibration = tr.stats.calib * 2 * np.pi / tr.stats.gse2.calper

        # fill up ObsPy Poles and Zeros AttribDict
        tr.stats.paz = AttribDict()
        # convert seismometer gain from [muVolt/nm/s] to [Volt/m/s]
        tr.stats.paz.seismometer_gain = seismometer_gain * 1e3
        # convert digitizer gain [count/muVolt] to [count/Volt]
        tr.stats.paz.digitizer_gain = 1e6 / calibration
        tr.stats.paz.poles = poles
        tr.stats.paz.zeros = zeros
        tr.stats.paz.sensitivity = tr.stats.paz.digitizer_gain * \
            tr.stats.paz.seismometer_gain
        # A0_normalization_factor convention for gse2 paz in Observatory in FFB
        tr.stats.paz.gain = 1.0

    if __name__ == '__main__':
        doctest.testmod(exclude_empty=True)

### Thank you!

We would like to thank our contributors, whose efforts make this software what it is. These people have helped by writing code and documentation, and by testing. They have created and maintained this product, its associated libraries and applications, our build tools and our web sites.

#### Contributors

- Ackerley, Nick
- Allgeyer, Sebastien
- Ammon, Charles J.
- Antunes, Emanuel
- Arnarsson, Ólafur St.
- Bank, Markus
- Barsch, Robert
- Behr, Yannik
- Bernardi, Fabrizio
- Bernauer, Felix
- Bes de Berc, Maxime
- Beyreuther, Moritz
- Boltz, Shawn
- Bonaimé, Sébastien
- Carothers, Lloyd
- Chamberlain, Calum
- Chambers, Derrick
- Chen, Zhao
- Clark, Adam
- Danecek, Peter
- Egdorf, Sven
- Engels, Fabian
- Ermert, Laura
- Eulenfeld, Tom

- Fabbri, Tommaso
- Falco, Nicholas
- Fee, Jeremy
- Grellier, Clément
- Grunberg, Marc
- Hammer, Conny
- Heimann, Sebastian
- Heiniger, Lukas
- Hope, Gaute
- Igel, Heiner
- Inza, Adolfo
- Isken, Marius
- Ketchum, David
- Koymans, Mathijs
- Kremers, Simon
- Kress, Victor
- Krieger, Lars
- Krischer, Lion
- Kula, Damian
- Käufl, Paul
- Köhler, Andreas
- Lecocq, Thomas
- Leeman, John
- Legovini, Paride

- Lesage, Philippe
- Li, Yulin
- Lomax, Anthony
- Lopes, Rui L.
- MacCarthy, Jonathan
- Maggi, Alessia
- Martin, Henri
- Medlin, Andrew
- Megies, Tobias
- Meschede, Matthias
- Michelini, Alberto
- Miller, Nathaniel C.
- Morgenstern, Bernhard
- Nof, Ran Novitsky
- Panning, Mark P.
- Parker, Tom
- Pestourie, Romain
- Rapagnani, Giovanni
- Reyes, Celso
- Ringler, Adam
- Rothenhäusler, Nicolas
- Russo, Emiliano
- Sales de Andrade, Elliott

- Satriano, Claudio
- Saul, Joachim
- Scheingraber, Chris
- Schneider, Simon
- Sippl, Christian
- Snoke, Arthur
- Stange, Stefan
- Stähler, Simon C.
- Sullivan, Benjamin
- Tian, Dongdong
- Trabant, Chad
- Trani, Luca
- Uieda, Leonardo
- Walker, Andrew
- Walther, Marcus
- Wassermann, Joachim
- Williams, Mark C.
- Winkelman, Andrew
- Zaccarelli, Riccardo
- Zad, Seyed Kasra Hosseini
- Zhu, Lijun
- van Driel, Martin
- van Wijk, Kasper

#### Funds

ObsPy was partially funded by the

- German Science Foundation (DFG) via grant DFG IG 16/9-1
- German Ministry for Education and Research (BMBF), GEOTECHNOLOGIEN grant 03G0646H.
- NERA project (Network of European Research Infrastructures for Earthquake Risk Assessment and Mitigation) under the European Community's Seventh Framework Programme (FP7/2007-2013) grant agreement n° 262330
- Leibniz Institute for Applied Geophysics (LIAG)
- VERCE EU-FP7 project (no. 283543)