forked from usgs/libcomcat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getcomcat.py
executable file
·92 lines (79 loc) · 4.53 KB
/
getcomcat.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
#!/usr/bin/env python
#stdlib
import argparse
from datetime import datetime
import os
#third party
from libcomcat.comcat import getContents
TIMEFMT = '%Y-%m-%dT%H:%M:%S'
DATEFMT = '%Y-%m-%d'
def maketime(timestring):
outtime = None
try:
outtime = datetime.strptime(timestring,TIMEFMT)
except:
try:
outtime = datetime.strptime(timestring,DATEFMT)
except:
raise Exception,'Could not parse time or date from %s' % timestring
return outtime
def makedict(dictstring):
try:
parts = dictstring.split(':')
key = parts[0]
value = parts[1]
return {key:value}
except:
raise Exception,'Could not create a single key dictionary out of %s' % dictstring
def main(args):
files = getContents(args.product,args.contents,outfolder=args.outputFolder,bounds=args.bounds,
starttime=args.startTime,endtime=args.endTime,magrange=args.magRange,
catalog=args.catalog,contributor=args.contributor,eventid=args.eventid,
listURL=args.listURL,eventProperties=args.eventProperties,
productProperties=args.productProperties,since=args.after,
getAll=args.getAll)
print
print '%i files were downloaded to %s' % (len(files),args.outputFolder)
if __name__ == '__main__':
desc = '''Download product files from USGS ComCat.
To download ShakeMap grid.xml files for a box around New Zealand during 2013:
getcomcat.py shakemap grid.xml -o /home/user/newzealand -b 163.213 -178.945 -48.980 -32.324 -s 2013-01-01 -e 2014-01-01
Note that when specifying a search box that crosses the -180/180 meridian, you simply specify longitudes
as you would if you were not crossing that meridian.
'''
parser = argparse.ArgumentParser(description=desc,formatter_class=argparse.RawDescriptionHelpFormatter)
#positional arguments
parser.add_argument('product', metavar='PRODUCT',
help='The name of the desired product (shakemap, dyfi, etc.)')
parser.add_argument('contents', metavar='CONTENTLIST', nargs='*',
help='The names of the product contents (grid.xml, stationlist.txt, etc.) ')
#optional arguments
parser.add_argument('-o','--output-folder', dest='outputFolder', default=os.getcwd(),
help='Folder where output files should be written.')
parser.add_argument('-b','--bounds', metavar=('lonmin','lonmax','latmin','latmax'),
dest='bounds', type=float, nargs=4,
help='Bounds to constrain event search [lonmin lonmax latmin latmax]')
parser.add_argument('-s','--start-time', dest='startTime', type=maketime,
help='Start time for search (defaults to ~30 days ago). YYYY-mm-dd or YYYY-mm-ddTHH:MM:SS')
parser.add_argument('-e','--end-time', dest='endTime', type=maketime,
help='End time for search (defaults to current date/time). YYYY-mm-dd or YYYY-mm-ddTHH:MM:SS')
parser.add_argument('-a','--after', dest='after', type=maketime,
help='Limit to events after specified time. YYYY-mm-dd or YYYY-mm-ddTHH:MM:SS')
parser.add_argument('-m','--mag-range', metavar=('minmag','maxmag'),dest='magRange', type=float,nargs=2,
help='Min/max magnitude to restrict search.')
parser.add_argument('-c','--catalog', dest='catalog',
help='Source catalog from which products derive (atlas, centennial, etc.)')
parser.add_argument('-n','--contributor', dest='contributor',
help='Source contributor (who loaded product) (us, nc, etc.)')
parser.add_argument('-i','--event-id', dest='eventid',
help='Event ID from which to download product contents.')
parser.add_argument('-p','--product-property', dest='productProperties', type=makedict,
help='Product property (reviewstatus:approved).')
parser.add_argument('-t','--event-property', dest='eventProperties',
help='Event property (alert:yellow, status:REVIEWED, etc.).',type=makedict)
parser.add_argument('-l','--list-url', dest='listURL', action='store_true',
help='Only list urls for contents in events that match criteria.')
parser.add_argument('-g','--get-all-versions', dest='getAll', action='store_true',
help='Get products for every version of every event.')
pargs = parser.parse_args()
main(pargs)