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

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

# http://pyrocko.org - GPLv3 

# 

# The Pyrocko Developers, 21st Century 

# ---|P------/S----------~Lg---------- 

from __future__ import division, absolute_import 

 

import numpy as num 

import struct 

import calendar 

 

from pyrocko import trace 

from .io_common import FileLoadError 

 

 

def ibm2ieee(ibm): 

""" 

Converts an IBM floating point number into IEEE format. 

:param: ibm - 32 bit unsigned integer: unpack('>L', f.read(4)) 

""" 

if ibm == 0: 

return 0.0 

sign = ibm >> 31 & 0x01 

exponent = ibm >> 24 & 0x7f 

mantissa = (ibm & 0x00ffffff) / float(pow(2, 24)) 

print('x', sign, exponent - 64, mantissa) 

return (1 - 2 * sign) * mantissa * float(pow(16, exponent - 64)) 

 

 

def unpack_ibm_f4(data): 

ibm = num.fromstring(data, dtype='>u4').astype(num.int32) 

sign = (ibm >> 31) & 0x01 

exponent = (ibm >> 24) & 0x7f 

mantissa = (ibm & 0x00ffffff) / float(pow(2, 24)) 

xxx = (1 - 2 * sign) * mantissa * (16.0 ** (exponent - 64))\ 

.astype(num.float) 

# for i in range(len(data)/4): 

# yyy = ibm2ieee(struct.unpack('>L', data[i*4:(i+1)*4])[0]) 

# print('y', sign[i], exponent[i] - 64, mantissa[i]) 

# print(xxx[i], yyy) 

# if xxx[i] != yyy: 

# sys.exit() 

# print 

return xxx 

 

 

class SEGYError(Exception): 

pass 

 

 

def iload(filename, load_data, endianness='>'): 

'''Read SEGY file. 

 

filename -- Name of SEGY file. 

load_data -- If True, the data is read, otherwise only read headers. 

''' 

 

endianness = endianness 

 

nbth = 3200 

nbbh = 400 

nbthx = 3200 

nbtrh = 240 

 

try: 

f = open(filename, 'rb') 

 

textual_file_header = f.read(nbth) 

 

if len(textual_file_header) != nbth: 

raise SEGYError('incomplete textual file header') 

 

binary_file_header = f.read(nbbh) 

if len(binary_file_header) != nbbh: 

raise SEGYError('incomplete binary file header') 

 

line_number = struct.unpack(endianness+'1I', binary_file_header[4:8]) 

hvals = struct.unpack(endianness+'24H', binary_file_header[12:12+24*2]) 

(ntraces, nauxtraces, deltat_us, deltat_us_orig, nsamples, 

nsamples_orig, format, ensemble_fold) = hvals[0:8] 

 

(segy_revision, fixed_length_traces, nextended_headers) = \ 

struct.unpack(endianness+'3H', binary_file_header[100:100+3*2]) 

 

if ntraces == 0 and nauxtraces == 0: 

ntraces = 1 

 

formats = { 

1: (unpack_ibm_f4, 4, "4-byte IBM floating-point"), 

2: (endianness+'i4', 4, "4-byte, two's complement integer"), 

3: (endianness+'i4', 2, "2-byte, two's complement integer"), 

4: (None, 4, "4-byte fixed-point with gain (obolete)"), 

5: (endianness+'f4', 4, "4-byte IEEE floating-point"), 

6: (None, 0, "not currently used"), 

7: (None, 0, "not currently used"), 

8: ('i1', 1, "1-byte, two's complement integer")} 

 

dtype = formats[format][0] 

sample_size = formats[format][1] 

if dtype is None: 

raise SEGYError('unsupported sample data format %i: %s' % ( 

format, formats[format][2])) 

 

for ihead in range(nextended_headers): 

f.read(nbthx) 

 

atend = False 

while not atend: 

for itrace in range((ntraces+nauxtraces)): 

trace_header = f.read(nbtrh) 

if len(trace_header) == 0: 

atend = True 

break 

 

if len(trace_header) != nbtrh: 

raise SEGYError('incomplete trace header') 

 

(scoordx, scoordy, gcoordx, gcoordy) = \ 

struct.unpack(endianness+'4f', trace_header[72:72+4*4]) 

 

(ensemblex, ensembley) = \ 

struct.unpack(endianness+'2f', trace_header[180:180+2*4]) 

(ensemble_num,) = \ 

struct.unpack(endianness+'1I', trace_header[20:24]) 

(trensemble_num,) = \ 

struct.unpack(endianness+'1I', trace_header[24:28]) 

(trace_number,) = \ 

struct.unpack(endianness+'1I', trace_header[0:4]) 

(trace_numbersegy,) = \ 

struct.unpack(endianness+'1I', trace_header[4:8]) 

(orfield_num,) = \ 

struct.unpack(endianness+'1I', trace_header[8:12]) 

(ortrace_num,) = \ 

struct.unpack(endianness+'1I', trace_header[12:16]) 

 

# don't know if this is standard: distance to shot [m] as int 

(idist,) = struct.unpack(endianness+'1I', trace_header[36:40]) 

 

tvals = struct.unpack( 

endianness+'12H', trace_header[94:94+12*2]) 

 

(nsamples_this, deltat_us_this) = tvals[-2:] 

 

tscalar = struct.unpack( 

endianness+'1H', trace_header[214:216])[0] 

 

if tscalar == 0: 

tscalar = 1. 

elif tscalar < 0: 

tscalar = 1.0 / tscalar 

else: 

tscalar = float(tscalar) 

 

tvals = [x * tscalar for x in tvals[:-2]] 

 

(year, doy, hour, minute, second) = \ 

struct.unpack(endianness+'5H', trace_header[156:156+2*5]) 

 

# maybe not standard? 

(msecs, usecs) = struct.unpack( 

endianness+'2H', trace_header[168:168+4]) 

 

try: 

if year < 100: 

year += 2000 

 

tmin = calendar.timegm( 

(year, 1, doy, hour, minute, second)) \ 

+ msecs * 1.0e-3 + usecs * 1.0e-6 

 

except Exception: 

raise SEGYError('invalid start date/time') 

 

if fixed_length_traces: 

if (nsamples_this, deltat_us_this) \ 

!= (nsamples, deltat_us): 

 

raise SEGYError( 

'trace of incorrect length or sampling ' 

'rate (trace=%i)' % itrace+1) 

 

if load_data: 

datablock = f.read(nsamples_this*sample_size) 

if len(datablock) != nsamples_this*sample_size: 

raise SEGYError('incomplete trace data') 

 

if isinstance(dtype, str): 

data = num.fromstring(datablock, dtype=dtype) 

else: 

data = dtype(datablock) 

 

tmax = None 

else: 

f.seek(nsamples_this*sample_size, 1) 

tmax = tmin + deltat_us_this/1000000.*(nsamples_this-1) 

data = None 

 

if data is not None and isinstance(dtype, str) and ( 

str(dtype).startswith('<') 

or str(dtype).startswith('>')): 

 

data = data.astype(str(dtype)[1:]) 

 

tr = trace.Trace( 

'', 

'%05i' % (line_number), 

'%02i' % (ensemble_num), 

'%03i' % (ortrace_num), 

tmin=tmin, 

tmax=tmax, 

deltat=deltat_us_this/1000000., 

ydata=data, 

meta=dict( 

orfield_num=orfield_num, 

distance=float(idist))) 

 

yield tr 

 

except (OSError, SEGYError) as e: 

raise FileLoadError(e) 

 

finally: 

f.close()