Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

#!/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 

    (http://www.gnu.org/copyleft/lesser.html) 

""" 

 

import doctest 

import numpy as np 

from obspy.core import AttribDict 

 

 

def readPaz(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 the instrument 

    simulation the A0_normalization_factor might be set wrongly, use 

    :func:`~obspy.gse2.libgse2.attach_paz` instead. 

 

    >>> import StringIO 

    >>> f = StringIO.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 = readPaz(f) 

    >>> ['%.4f' % i for i in (p[0].real, z[0].real, k)] 

    ['-4.3982', '0.0000', '0.4000'] 

    ''' 

    poles = [] 

    zeros = [] 

 

    if isinstance(paz_file, str): 

        paz_file = open(paz_file, 'r') 

 

    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 xrange(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 xrange(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 import Trace 

    >>> tr = Trace(header={'calib': .094856, 'gse2': {'calper': 1}}) 

    >>> import StringIO 

    >>> f = StringIO.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, -4)) 

    671140000.0 

    ''' 

    poles, zeros, seismometer_gain = readPaz(paz_file) 

    found_zero = False 

 

    # 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) 

            found_zero = True 

            break 

    if not found_zero: 

        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)