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

223

224

225

226

227

228

# http://pyrocko.org - GPLv3 

# 

# The Pyrocko Developers, 21st Century 

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

from __future__ import absolute_import, division 

 

import time 

import calendar 

import re 

import logging 

 

from pyrocko import model, util 

from pyrocko.moment_tensor import MomentTensor 

from pyrocko.util import Request, urlopen 

from .base_catalog import EarthquakeCatalog 

 

 

import numpy as num 

 

 

logger = logging.getLogger('pyrocko.client.globalcmt') 

 

km = 1000. 

 

 

class Anon(object): 

pass 

 

 

class GlobalCMT(EarthquakeCatalog): 

 

def __init__(self): 

self.events = {} 

 

def flush(self): 

self.events = {} 

 

def iter_event_names( 

self, 

time_range=None, 

magmin=0., 

magmax=10., 

latmin=-90., 

latmax=90., 

lonmin=-180., 

lonmax=180., 

depthmin=0., 

depthmax=1000*km): 

 

yearbeg, monbeg, daybeg = time.gmtime(time_range[0])[:3] 

yearend, monend, dayend = time.gmtime(time_range[1])[:3] 

 

url = 'http://www.globalcmt.org/cgi-bin/globalcmt-cgi-bin/CMT5/form?' \ 

+ '&'.join([ 

'itype=ymd', 

'yr=%i' % yearbeg, 'mo=%i' % monbeg, 'day=%i' % daybeg, 

'otype=ymd', 

'oyr=%i' % yearend, 'omo=%i' % monend, 'oday=%i' % dayend, 

'jyr=1976', 'jday=1', 'ojyr=1976', 'ojday=1', 'nday=1', 

'lmw=%g' % magmin, 'umw=%g' % magmax, 

'lms=0', 'ums=10', 

'lmb=0', 'umb=10', 

'llat=%g' % latmin, 'ulat=%g' % latmax, 

'llon=%g' % lonmin, 'ulon=%g' % lonmax, 

'lhd=%g' % (depthmin/km), 'uhd=%g' % (depthmax/km), 

'lts=-9999', 'uts=9999', 

'lpe1=0', 'upe1=90', 

'lpe2=0', 'upe2=90', 

'list=5']) 

 

while True: 

logger.debug('Opening URL: %s' % url) 

req = Request(url) 

page = urlopen(req).read() 

logger.debug('Received page (%i bytes)' % len(page)) 

 

events, more = self._parse_events_page(page) 

 

for ev in events: 

self.events[ev.name] = ev 

 

for ev in events: 

if time_range[0] <= ev.time and ev.time <= time_range[1]: 

yield ev.name 

 

if more: 

url = more.decode('ascii') 

else: 

break 

 

def get_event(self, name): 

if name not in self.events: 

t = self._name_to_date(name) 

for name2 in self.iter_event_names( 

time_range=(t-24*60*60, t+2*24*60*60)): 

 

if name2 == name: 

break 

 

return self.events[name] 

 

def _parse_events_page(self, page): 

 

lines = page.splitlines() 

state = 0 

 

events = [] 

 

def complete(data): 

try: 

t = util.to_time_float( 

calendar.timegm(( 

data.year, data.month, data.day, 

data.hour, data.minute, data.seconds))) 

 

m = num.array( 

[data.mrr, data.mrt, data.mrp, 

data.mrt, data.mtt, data.mtp, 

data.mrp, data.mtp, data.mpp], 

dtype=num.float).reshape(3, 3) 

 

m *= 10.0**(data.exponent-7) 

mt = MomentTensor(m_up_south_east=m) 

ev = model.Event( 

lat=data.lat, 

lon=data.lon, 

time=t, 

name=data.eventname, 

depth=data.depth_km*1000., 

magnitude=float(mt.moment_magnitude()), 

duration=data.half_duration * 2., 

region=data.region.rstrip(), 

catalog=data.catalog) 

 

ev.moment_tensor = mt 

events.append(ev) 

 

except AttributeError: 

pass 

 

catalog = 'gCMT' 

 

data = None 

more = None 

for line in lines: 

if state == 0: 

 

m = re.search(br'<a href="([^"]+)">More solutions', line) 

if m: 

more = m.group(1) 

 

m = re.search(br'From Quick CMT catalog', line) 

if m: 

catalog = 'gCMT-Q' 

 

m = re.search(br'Event name:\s+(\S+)', line) 

if m: 

if data: 

complete(data) 

 

data = Anon() 

data.eventname = str(m.group(1).decode('ascii')) 

data.catalog = catalog 

 

if data: 

m = re.search(br'Region name:\s+([^<]+)', line) 

if m: 

data.region = str(m.group(1).decode('ascii')) 

 

m = re.search( 

br'Date \(y/m/d\): (\d\d\d\d)/(\d+)/(\d+)', line) 

 

if m: 

data.year, data.month, data.day = ( 

int(m.group(1)), int(m.group(2)), int(m.group(3))) 

 

m = re.search(br'Timing and location information', line) 

if m: 

state = 1 

 

if state == 1: 

toks = line.split() 

if toks and toks[0] == b'CMT': 

data.hour, data.minute = [int(x) for x in toks[1:3]] 

data.seconds, data.lat, data.lon, data.depth_km = [ 

float(x) for x in toks[3:]] 

 

m = re.search(br'Assumed half duration:\s+(\S+)', line) 

if m: 

data.half_duration = float(m.group(1)) 

 

m = re.search(br'Mechanism information', line) 

if m: 

state = 2 

 

if state == 2: 

m = re.search(br'Exponent for moment tensor:\s+(\d+)', line) 

if m: 

data.exponent = int(m.group(1)) 

 

toks = line.split() 

if toks and toks[0] == b'CMT': 

data.mrr, data.mtt, data.mpp, \ 

data.mrt, data.mrp, data.mtp = [ 

float(x) for x in toks[1:]] 

 

m = re.search(br'^Eigenvector:', line) 

if m: 

state = 0 

 

if data is not None: 

complete(data) 

 

return events, more 

 

def _name_to_date(self, name): 

 

if len(name) == 7: 

y, m, d = time.strptime(name[:6], '%m%d%y')[:3] 

if y > 2005: 

y -= 100 

 

else: 

y, m, d = time.strptime(name[:8], '%Y%m%d')[:3] 

 

t = util.to_time_float( 

calendar.timegm((y, m, d, 0, 0, 0))) 

return t