-
Notifications
You must be signed in to change notification settings - Fork 7
/
createshake.py
executable file
·149 lines (136 loc) · 5.88 KB
/
createshake.py
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
#!/usr/bin/env python
#stdlib imports
import os.path
import urllib.request, urllib.error, urllib.parse
import urllib.parse
from xml.dom.minidom import parseString
import sys
import io
import argparse
import json
from datetime import datetime,timedelta
from time import strptime
EVENT_TEMPLATE = '''<?xml version="1.0" encoding="US-ASCII" standalone="yes"?>
<!DOCTYPE earthquake [
<!ELEMENT earthquake EMPTY>
<!ATTLIST earthquake
id ID #REQUIRED
lat CDATA #REQUIRED
lon CDATA #REQUIRED
mag CDATA #REQUIRED
year CDATA #REQUIRED
month CDATA #REQUIRED
day CDATA #REQUIRED
hour CDATA #REQUIRED
minute CDATA #REQUIRED
second CDATA #REQUIRED
timezone CDATA #REQUIRED
depth CDATA #REQUIRED
type CDATA #REQUIRED
locstring CDATA #REQUIRED
pga CDATA #REQUIRED
pgv CDATA #REQUIRED
sp03 CDATA #REQUIRED
sp10 CDATA #REQUIRED
sp30 CDATA #REQUIRED
created CDATA #REQUIRED
>
]>
<earthquake id="[ID]" lat="[LAT]" lon="[LON]" mag="[MAG]" year="[YEAR]" month="[MONTH]" day="[DAY]" hour="[HOUR]" minute="[MINUTE]" second="[SECOND]" timezone="GMT" depth="[DEPTH]" locstring="[LOCSTRING]" created="[CREATED]" network="us" />'''
TIMEFMT = '%Y-%m-%dT%H:%M:%S'
BASEURL = 'http://earthquake.usgs.gov/fdsnws/event/1/query?eventid=[EVENT]&format=geojson'
def getEventInfo(weburl):
eventdict = {}
parts = urllib.parse.urlsplit(weburl.rstrip('/'))
eventid = parts.path.split('/')[-1]
url = BASEURL.replace('[EVENT]',eventid)
fh = urllib.request.urlopen(url)
data = fh.read()
fh.close()
jdict = json.loads(data)
eventdict['lon'],eventdict['lat'],eventdict['depth'] = jdict['geometry']['coordinates']
ms_since_epoch = jdict['properties']['time']
sec_since_epoch = ms_since_epoch/1000
microseconds = int((ms_since_epoch/1000.0 - sec_since_epoch)*1e6)
eventdict['time'] = datetime.utcfromtimestamp(sec_since_epoch)
eventdict['time'] += timedelta(microseconds = microseconds)
eventdict['mag'] = jdict['properties']['mag']
eventdict['locstring'] = jdict['properties']['place']
eventdict['id'] = eventid
return eventdict
def writeEvent(eventdict,shakehome):
efolder = os.path.join(shakehome,eventdict['id'])
if not os.path.isdir(efolder):
os.makedirs(efolder)
inputfolder = os.path.join(efolder,'input')
if not os.path.isdir(inputfolder):
os.makedirs(inputfolder)
configfolder = os.path.join(efolder,'config')
if not os.path.isdir(configfolder):
os.makedirs(configfolder)
eventdata = EVENT_TEMPLATE.replace('[ID]',eventdict['id'])
eventdata = eventdata.replace('[LAT]','%.4f' % eventdict['lat'])
eventdata = eventdata.replace('[LON]','%.4f' % eventdict['lon'])
eventdata = eventdata.replace('[DEPTH]','%.1f' % eventdict['depth'])
eventdata = eventdata.replace('[MAG]','%.1f' % eventdict['mag'])
eventdata = eventdata.replace('[LOCSTRING]',eventdict['locstring'])
epochtime = (eventdict['time'] - datetime(1970,1,1)).total_seconds()
eventdata = eventdata.replace('[CREATED]','%i' % epochtime)
eventdata = eventdata.replace('[YEAR]','%i' % eventdict['time'].year)
eventdata = eventdata.replace('[MONTH]','%i' % eventdict['time'].month)
eventdata = eventdata.replace('[DAY]','%i' % eventdict['time'].day)
eventdata = eventdata.replace('[HOUR]','%i' % eventdict['time'].hour)
eventdata = eventdata.replace('[MINUTE]','%i' % eventdict['time'].minute)
eventdata = eventdata.replace('[SECOND]','%i' % eventdict['time'].second)
eventfile = os.path.join(inputfolder,'event.xml')
f = open(eventfile,'wt')
f.write(eventdata)
f.close()
return efolder
def parseParams(params):
eventdict = {}
try:
eventdict['time'] = datetime.strptime(params[0],TIMEFMT)
eventdict['lat'] = float(params[1])
eventdict['lon'] = float(params[2])
eventdict['depth'] = float(params[3])
eventdict['mag'] = float(params[4])
eventdict['id'] = eventdict['time'].strftime('%Y%m%d%H%M%S')
eventdict['locstring'] = '(%.4f,%.4f)' % (eventdict['lat'],eventdict['lon'])
except Exception as excobj:
raise excobj
return eventdict
def main(args):
if sys.platform == 'darwin':
shakehome = '/opt/local/ShakeMap/data/'
else:
shakehome = '/home/shake/ShakeMap/data/'
if args.url:
#remove any stuff after a # sign in the url
url = args.url[0:args.url.find('#')]
if not url.endswith('/'):
url += '/'
eventdict = getEventInfo(url)
else:
eventdict = parseParams(args.params)
efolder = writeEvent(eventdict,shakehome)
print('Completed creating ShakeMap %s.\nTo run this event, do:\n%s/bin/shake -event %s' % (efolder,shakehome,eventdict['id']))
if __name__ == '__main__':
desc = '''Create a ShakeMap from NEIC web site or from scratch.
Examples:
Creating a ShakeMap from an event in ComCat:
%(prog)s -u http://comcat.cr.usgs.gov/earthquakes/eventpage/usb000slwn#summary
Creating a ShakeMap ab novo:
%(prog)s -p 2015-01-01T13:45:59 34.3 -118.1 20.5 6.3 => will be assigned ID of us20150101134559
'''
parser = argparse.ArgumentParser(description=desc,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('-u','--url', help='the URL of the desired event in ComCat.')
parser.add_argument('-p','--params', nargs=5,
help='the 5 params defining a ShakeMap input (time (YYYY-MM-DDTHH:MM:SS),lat,lon,depth,mag.')
pargs = parser.parse_args()
#must have at least one set
if (pargs.url is None and pargs.params is None) or (pargs.url is not None and pargs.params is not None):
print(parser.print_help())
sys.exit(1)
main(pargs)