@@ -3,59 +3,16 @@ import argparse
33import sys
44
55from libcomcat .search import search , count
6- from libcomcat .utils import get_detail_data_frame , get_summary_data_frame , maketime
7- from impactutils .time .ancient_time import HistoricTime
6+ from libcomcat .utils import (maketime , get_all_mags )
87from obspy .clients .fdsn import Client
98import pandas as pd
109
1110
12- class CustomFormatter (argparse .ArgumentDefaultsHelpFormatter , argparse .RawDescriptionHelpFormatter ):
11+ class CustomFormatter (argparse .ArgumentDefaultsHelpFormatter ,
12+ argparse .RawDescriptionHelpFormatter ):
1313 pass
1414
1515
16- def get_mag_src (mag ):
17- """Try to find the best magnitude source from a Magnitude object.
18-
19- Note: This can be difficult, as there is a great deal of variance
20- in how magnitude information is submitted in QuakeML within ComCat.
21-
22- Args:
23- mag (obspy Magnitude): Magnitude object from obspy.
24- Returns:
25- str: String indicating the most likely source of the magnitude solution.
26-
27- """
28- if mag .creation_info is not None and mag .creation_info .agency_id is not None :
29- magsrc = mag .creation_info .agency_id .lower ()
30- else :
31- has_gcmt = mag .resource_id .id .lower ().find ('gcmt' ) > - 1
32- has_at = mag .resource_id .id .lower ().find ('at' ) > - 1
33- has_pt = mag .resource_id .id .lower ().find ('pt' ) > - 1
34- has_ak = (mag .resource_id .id .lower ().find ('ak' ) > - 1 or
35- mag .resource_id .id .lower ().find ('alaska' ) > - 1 )
36- has_pr = mag .resource_id .id .lower ().find ('pr' ) > - 1
37- has_dup = mag .resource_id .id .lower ().find ('duputel' ) > - 1
38- has_us = mag .resource_id .id .lower ().find ('us' ) > - 1
39- if has_gcmt :
40- magsrc = 'gcmt'
41- elif has_dup :
42- magsrc = 'duputel'
43- elif has_at :
44- magsrc = 'at'
45- elif has_pt :
46- magsrc = 'pt'
47- elif has_ak :
48- magsrc = 'ak'
49- elif has_pr :
50- magsrc = 'pr'
51- elif has_us :
52- magsrc = 'us'
53- else :
54- magsrc = 'unknown'
55-
56- return magsrc
57-
58-
5916def get_parser ():
6017 desc = '''Download epicenter and all contributed magnitudes in line format (csv, tab, etc.).
6118
@@ -186,7 +143,6 @@ def main():
186143 # create a dataframe with these columns - we'll add more later
187144 df = pd .DataFrame (columns = ['id' , 'time' , 'lat' , 'lon' , 'depth' ,
188145 'location' , 'url' , 'hypo_src' ])
189- client = Client ('USGS' )
190146 ievent = 1
191147
192148 for event in events :
@@ -208,24 +164,14 @@ def main():
208164 print ('Parsing event %s (%i of %i) - %i origins' % tpl )
209165 ievent += 1
210166 errors = []
167+ mags = {}
211168 for eid in id_list :
212- try :
213- obsevent = client .get_events (eventid = eid ).events [0 ]
214- except Exception as e :
215- msg = 'Failed to download data for event %s. Skipping.' % eid
216- errors .append (msg )
217- for mag in obsevent .magnitudes :
218- magvalue = mag .mag
219- magtype = mag .magnitude_type
220- magsrc = get_mag_src (mag )
221- if magsrc == 'unknown' and args .verbose :
222- print ('Unable to determine magnitude source from %s' % eid )
223- colname = '%s-%s' % (magsrc , magtype )
224- if colname in row :
225- # print('Duplicate column %s for event %s - skipping' % (colname,event.id))
226- continue
227- row [colname ] = magvalue
228- imag += 1
169+ magtypes , msg = get_all_mags (eid )
170+ if args .verbose and len (msg ):
171+ print (msg )
172+ mags .update (magtypes )
173+ imag += 1
174+ row = pd .concat ([row , pd .Series (mags )])
229175 df = df .append (row , ignore_index = True )
230176
231177 if len (errors ):
@@ -234,9 +180,9 @@ def main():
234180 print ('\t %s' % error )
235181
236182 if args .format == 'excel' :
237- df .to_excel (args .filename )
183+ df .to_excel (args .filename , index = False )
238184 else :
239- df .to_csv (args .filename )
185+ df .to_csv (args .filename , index = False )
240186 print ('%i records saved to %s.' % (len (df ), args .filename ))
241187 sys .exit (0 )
242188
0 commit comments