def test_count(): datadir = get_datadir() tape_file = os.path.join(datadir, 'search_count.yaml') with vcr.use_cassette(tape_file): nevents = count(starttime=datetime(1994, 1, 17, 12, 30), endtime=datetime(1994, 1, 18, 12, 35), minmagnitude=6.6, updatedafter=datetime(2010, 1, 1)) assert nevents == 1
def test_count(): datadir = get_datadir() tape_file = os.path.join(datadir, 'vcr_count.yaml') with vcr.use_cassette(tape_file): nevents = count(starttime=datetime(1994, 1, 17, 12, 30), endtime=datetime(1994, 1, 18, 12, 35), minmagnitude=6.6, verbose=True, updatedafter=datetime(2010, 1, 1)) assert nevents == 1
def main(): parser = get_parser() args = parser.parse_args() # make sure we don't have -e option AND --numdays option if args.endTime is not None and args.numdays is not None: msg = ('You must specify end time or number of days since ' 'start time, not both. Exiting.') print(msg) sys.exit(1) if not args.endTime and args.numdays: args.endTime = args.startTime + timedelta(args.numdays) setup_logger(args.logfile, args.loglevel) tsum = (args.bounds is not None) + \ (args.radius is not None) + (args.country is not None) if tsum != 1: logging.error( 'Please specify a bounding box, radius, or country code.') sys.exit(1) latitude = None longitude = None radiuskm = None lonmin = latmin = lonmax = latmax = None bounds = None if args.radius: latitude = args.radius[0] longitude = args.radius[1] radiuskm = args.radius[2] if args.bounds: lonmin, lonmax, latmin, latmax = args.bounds # fix longitude bounds when crossing dateline if lonmin > lonmax and lonmax >= -180: lonmin -= 360 else: lonmin, lonmax, latmin, latmax = None, None, None, None bounds = (lonmin, lonmax, latmin, latmax) if args.country: ccode = args.country if not check_ccode(ccode): curl = 'https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2' fmt = ('%s is not a valid ISO 3166 country code. ' 'See %s for the list.') tpl = (ccode, curl) logging.error(fmt % tpl) sys.exit(1) bounds = get_country_bounds(ccode, args.buffer) # this returns a list minmag = 0.0 maxmag = 9.9 if args.magRange: minmag = args.magRange[0] maxmag = args.magRange[1] minsig = 0 maxsig = 5000 if args.sigRange: minsig = args.sigRange[0] maxsig = args.sigRange[1] if args.getCount: if isinstance(bounds, tuple) or bounds is None: nevents = count(starttime=args.startTime, endtime=args.endTime, updatedafter=args.after, minlatitude=latmin, maxlatitude=latmax, minlongitude=lonmin, maxlongitude=lonmax, latitude=latitude, longitude=longitude, maxradiuskm=radiuskm, catalog=args.catalog, contributor=args.contributor, maxmagnitude=maxmag, minmagnitude=minmag, minsig=minsig, maxsig=maxsig, producttype=args.limitByProductType) else: for lonmin, lonmax, latmin, latmax in bounds: nevents = 0 nevents += count(starttime=args.startTime, endtime=args.endTime, updatedafter=args.after, minlatitude=latmin, maxlatitude=latmax, minlongitude=lonmin, maxlongitude=lonmax, latitude=latitude, longitude=longitude, maxradiuskm=radiuskm, catalog=args.catalog, contributor=args.contributor, minsig=minsig, maxsig=maxsig, maxmagnitude=maxmag, minmagnitude=minmag, producttype=args.limitByProductType) print('There are %i events matching input criteria.' % nevents) sys.exit(0) if isinstance(bounds, tuple) or bounds is None: events = search(starttime=args.startTime, endtime=args.endTime, updatedafter=args.after, minlatitude=latmin, maxlatitude=latmax, minlongitude=lonmin, maxlongitude=lonmax, latitude=latitude, longitude=longitude, maxradiuskm=radiuskm, catalog=args.catalog, contributor=args.contributor, maxmagnitude=maxmag, minmagnitude=minmag, minsig=minsig, maxsig=maxsig, producttype=args.limitByProductType, host=args.host, eventtype=args.event_type, alertlevel=args.alert_level) else: events = [] for i, tbounds in enumerate(bounds): lonmin, lonmax, latmin, latmax = tbounds fmt = 'Checking bounds %i of %i for %s...\n' tpl = (i + 1, len(bounds), ccode) logging.debug(fmt % tpl) tevents = search(starttime=args.startTime, endtime=args.endTime, updatedafter=args.after, minlatitude=latmin, maxlatitude=latmax, minlongitude=lonmin, maxlongitude=lonmax, latitude=latitude, longitude=longitude, maxradiuskm=radiuskm, catalog=args.catalog, contributor=args.contributor, maxmagnitude=maxmag, minmagnitude=minmag, minsig=minsig, maxsig=maxsig, producttype=args.limitByProductType, host=args.host, eventtype=args.event_type, alertlevel=args.alert_level) events += tevents if not len(events): logging.info('No events found matching your search criteria. Exiting.') sys.exit(0) if (args.getAngles != 'none' or args.getAllMags or args.getComponents != 'none'): logging.info( 'Fetched %i events...creating table.\n' % (len(events))) supp = args.getMomentSupplement df = get_detail_data_frame(events, get_all_magnitudes=args.getAllMags, get_tensors=args.getComponents, get_focals=args.getAngles, get_moment_supplement=supp) else: logging.info( 'Fetched %i events...creating summary table.\n' % (len(events))) df = get_summary_data_frame(events) # order the columns so that at least the initial parameters come the way # we want them... first_columns = list(events[0].toDict().keys()) col_list = list(df.columns) for column in first_columns: try: col_list.remove(column) except Exception as e: x = 1 df = df[first_columns + col_list] if args.country: df = filter_by_country(df, ccode, buffer_km=args.buffer) logging.info('Created table...saving %i records to %s.\n' % (len(df), args.filename)) if args.format == 'excel': df.to_excel(args.filename, index=False) elif args.format == 'tab': df.to_csv(args.filename, sep='\t', index=False) else: df.to_csv(args.filename, index=False, chunksize=1000) logging.info('%i records saved to %s.' % (len(df), args.filename)) sys.exit(0)
def main(): parser = get_parser() args = parser.parse_args() setup_logger(args.logfile, args.loglevel) latitude = None longitude = None radiuskm = None lonmin = latmin = lonmax = latmax = None if args.radius: latitude = args.radius[0] longitude = args.radius[1] radiuskm = args.radius[2] if args.bounds: lonmin, lonmax, latmin, latmax = args.bounds # fix longitude bounds when crossing dateline if lonmin > lonmax and lonmax >= -180: lonmin -= 360 else: lonmin, lonmax, latmin, latmax = None, None, None, None minmag = 0.0 maxmag = 9.9 if args.magRange: minmag = args.magRange[0] maxmag = args.magRange[1] if args.getCount: nevents = count(starttime=args.startTime, endtime=args.endTime, updatedafter=args.after, minlatitude=latmin, maxlatitude=latmax, minlongitude=lonmin, maxlongitude=lonmax, latitude=latitude, longitude=longitude, maxradiuskm=radiuskm, maxmagnitude=maxmag, minmagnitude=minmag, verbose=args.verbose) print('There are %i events matching input criteria.' % nevents) sys.exit(0) if args.bounds and args.radius: print('Please specify either a bounding box OR radius search.') sys.exit(1) events = search(starttime=args.startTime, endtime=args.endTime, updatedafter=args.after, minlatitude=latmin, maxlatitude=latmax, minlongitude=lonmin, maxlongitude=lonmax, latitude=latitude, longitude=longitude, maxradiuskm=radiuskm, maxmagnitude=maxmag, minmagnitude=minmag, verbose=args.verbose) if not len(events): print('No events found matching your search criteria. Exiting.') sys.exit(0) # create a dataframe with these columns - we'll add more later df = pd.DataFrame(columns=['id', 'time', 'lat', 'lon', 'depth', 'location', 'url', 'hypo_src']) ievent = 1 for event in events: id_list = event['ids'].split(',')[1:-1] source = event.id.replace(event['code'], '') row = pd.Series(data={'id': event.id, 'time': event.time, 'lat': event.latitude, 'lon': event.longitude, 'depth': event.depth, 'location': event.location, 'url': event.url, 'hypo_src': source}) imag = 1 if args.verbose: tpl = (event.id, ievent, len(events), len(id_list)) print('Parsing event %s (%i of %i) - %i origins' % tpl) ievent += 1 errors = [] mags = {} for eid in id_list: magtypes, msg = get_all_mags(eid) if args.verbose and len(msg): print(msg) mags.update(magtypes) imag += 1 row = pd.concat([row, pd.Series(mags)]) df = df.append(row, ignore_index=True) if len(errors): print('Some events could not be retrieved:') for error in errors: print('\t%s' % error) if args.format == 'excel': df.to_excel(args.filename, index=False) else: df.to_csv(args.filename, index=False) print('%i records saved to %s.' % (len(df), args.filename)) sys.exit(0)
def test_count(): nevents = count(starttime=datetime(1994,1,17,12,30), endtime=datetime(1994,1,18,12,35), minmagnitude=6.6,verbose=True, updatedafter=datetime(2010,1,1)) assert nevents == 1