@@ -9,23 +9,31 @@ import textwrap
99import pandas as pd
1010from openpyxl import load_workbook
1111from obspy .geodetics .base import gps2dist_azimuth
12+ import numpy as np
1213
1314# local imports
1415from libcomcat .search import search
1516from libcomcat .dataframes import (get_history_data_frame , split_history_frame ,
1617 PRODUCTS , TIMEFMT , PRODUCT_COLUMNS )
1718
1819
20+ DISPLAY_TIME_FMT = '%Y-%m-%d %H:%M:%S'
21+
22+
23+ class MyFormatter (argparse .RawTextHelpFormatter ,
24+ argparse .ArgumentDefaultsHelpFormatter ):
25+ pass
26+
27+
1928def get_parser ():
2029 desc = '''Print out ComCat event history.
2130
2231 Usage:
2332 geteventhist ci38457511
2433
2534 '''
26- formatter = argparse .RawDescriptionHelpFormatter
2735 parser = argparse .ArgumentParser (description = desc ,
28- formatter_class = formatter )
36+ formatter_class = MyFormatter )
2937 # positional arguments
3038 parser .add_argument ('eventid' ,
3139 metavar = 'EVENTID' , help = 'ComCat event ID.' )
@@ -53,7 +61,7 @@ def get_parser():
5361 parser .add_argument ('-o' , '--outdir' , help = ohelp ,
5462 default = None )
5563
56- parser .add_argument ('-w' , '--web' , help = 'Produce HTML output .' ,
64+ parser .add_argument ('-w' , '--web' , help = 'Print HTML tables to stdout .' ,
5765 default = False , action = 'store_true' )
5866
5967 parser .add_argument ('-f' , '--format' , help = 'Control output format' ,
@@ -113,10 +121,10 @@ def save_dataframe(outdir, format, event, dataframe, product=None):
113121 wb .save (outfile )
114122 else :
115123 if product is not None :
116- outfile = os .path .join (args . outdir ,
117- event .id + '_' + products [ 0 ] + '.csv' )
124+ outfile = os .path .join (outdir ,
125+ event .id + '_' + product + '.csv' )
118126 else :
119- outfile = os .path .join (args . outdir , event .id + '.csv' )
127+ outfile = os .path .join (outdir , event .id + '.csv' )
120128 dataframe .to_csv (outfile , index = False )
121129 cdata = open (outfile , 'rt' ).read ()
122130 with open (outfile , 'wt' ) as f :
@@ -131,10 +139,43 @@ def save_dataframe(outdir, format, event, dataframe, product=None):
131139 return outfile
132140
133141
142+ def web_print (event , dataframe ):
143+ etable_fmt = '''
144+ <pre>
145+ Event ID: %s
146+ Origin Time: %s
147+ Magnitude: %.1f
148+ Latitude: %.4f
149+ Longitude: %.4f
150+ Depth: %.1f
151+ </pre>
152+ '''
153+ etable_tpl = (event .id ,
154+ event .time .strftime (TIMEFMT ),
155+ event .magnitude ,
156+ event .latitude ,
157+ event .longitude ,
158+ event .depth )
159+ etable = etable_fmt % etable_tpl
160+ print (textwrap .dedent (etable ))
161+ print (dataframe .to_html (index = False , border = 0 , max_rows = None , max_cols = None ))
162+
163+
164+ def simplify_times (dataframe ):
165+ # re-format all time columns to be like: 2019-01-01 17:34:16.1
166+ # first figure out all the time-like columns
167+ # df['date'] = pd.to_datetime(df["date"].dt.strftime('%Y-%m'))
168+ dtypes = dataframe .dtypes
169+ for idx , dtype in dtypes .iteritems ():
170+ if np .issubdtype (dtype , np .datetime64 ):
171+ dataframe [idx ] = pd .to_datetime (
172+ dataframe [idx ].dt .strftime (DISPLAY_TIME_FMT ))
173+
174+
134175def main ():
135176 pd .set_option ('display.width' , 1000 )
136177 pd .set_option ('display.max_rows' , 1000 )
137- pd .set_option ('display.max_colwidth' , 120 )
178+ pd .set_option ('display.max_colwidth' , - 1 )
138179 pd .set_option ('display.max_columns' , 1000 )
139180 pd .set_option ("display.colheader_justify" , "left" )
140181 parser = get_parser ()
@@ -155,6 +196,16 @@ def main():
155196 print (fmt % (',' .join (unsupported )))
156197 sys .exit (1 )
157198
199+ # web output and directory output are mutually exclusive
200+ if args .outdir and args .web :
201+ msg = '''The -o and -w options are mutually exclusive, meaning
202+ that you cannot choose to write files to a directory and print
203+ HTML output to the screen simultaneously. Please choose one of
204+ those two options and try again.
205+ '''
206+ print (msg )
207+ sys .exit (1 )
208+
158209 if args .products :
159210 products = args .products
160211 else :
@@ -196,8 +247,14 @@ def main():
196247 # now re-sort by update time
197248 dataframe = dataframe .sort_values ('Update Time' )
198249 dataframe = dataframe [PRODUCT_COLUMNS ]
250+ else :
251+ # since "Authoritative Event ID" and "Associated" columns are only applicable when
252+ # we're including other events in our results, drop those columns
253+ # if we're not doing that.
254+ drop_columns = ['Authoritative Event ID' , 'Associated' ]
255+ dataframe = dataframe .drop (drop_columns , axis = 'columns' )
199256
200- if not os .path .isdir (args .outdir ):
257+ if args . outdir is not None and not os .path .isdir (args .outdir ):
201258 os .makedirs (args .outdir )
202259
203260 if args .split :
@@ -207,36 +264,23 @@ def main():
207264 # somehow in this process
208265 for product in available_products :
209266 pframe = split_history_frame (dataframe , product = product )
210- outfile = save_dataframe (args .outdir , args .format , event ,
211- pframe , product = product )
212- print ('%i rows saved to %s' % (len (pframe ), outfile ))
267+ simplify_times (pframe )
268+ if args .web :
269+ web_print (event , pframe )
270+ else :
271+ outfile = save_dataframe (args .outdir , args .format , event ,
272+ pframe , product = product )
273+ print ('%i rows saved to %s' % (len (pframe ), outfile ))
274+ sys .exit (0 )
275+
213276 if args .outdir :
214277 outfile = save_dataframe (args .outdir , args .format , event ,
215278 dataframe , product = None )
216279
217280 print ('%i rows saved to %s' % (len (dataframe ), outfile ))
218281 elif args .web :
219- etable_fmt = '''
220- <pre>
221- Event ID: %s
222- Origin Time: %s
223- Magnitude: %.1f
224- Latitude: %.4f
225- Longitude: %.4f
226- Depth: %.1f
227- </pre>
228- '''
229- etable_tpl = (event .id ,
230- event .time .strftime (TIMEFMT ),
231- event .magnitude ,
232- event .latitude ,
233- event .longitude ,
234- event .depth )
235- etable = etable_fmt % etable_tpl
236- print (textwrap .dedent (etable ))
237- print (dataframe .to_html (index = False , border = 0 ))
238- else :
239- print (dataframe .to_string (index = False ))
282+ simplify_times (dataframe )
283+ web_print (event , dataframe )
240284
241285 sys .exit (0 )
242286
0 commit comments