Exemplo n.º 1
0
def main():
    # set the display width such that table output is not truncated
    pd.set_option('display.max_columns', 10000)
    pd.set_option("display.max_colwidth", 10000)
    pd.set_option("display.expand_frame_repr", False)

    parser = get_parser()

    args = parser.parse_args()

    # make sure either args.eventinfo or args.eventid is specified
    if args.eventinfo is None and args.eventid is None:
        print('Please select --eventinfo or -i option. Exiting.')
        sys.exit(1)

    if args.eventinfo is None:
        detail = get_event_by_id(args.eventid)
        idlist = detail['ids'].split(',')[1:-1]
        idlist.remove(detail.id)
        print('Authoritative ID: %s\n' % detail.id)
        print('Contributing IDs:')
        for eid in idlist:
            print('  ' + eid)
        sys.exit(0)
    else:
        try:
            timestr = args.eventinfo[0]
            latstr = args.eventinfo[1]
            lonstr = args.eventinfo[2]
            try:
                time = datetime.strptime(timestr, TIMEFMT)
            except ValueError:
                time = datetime.strptime(timestr, DATEFMT)

            lat = float(latstr)
            lon = float(lonstr)
        except ValueError as ve:
            print('Error parsing event info:\n%s' % str(ve))
            sys.exit(1)

    # trap for mutually exclusive options a, u and v
    argsum = (args.print_all + args.print_url + args.print_verbose)
    if argsum > 1:
        msg = ('The -a, -v, and -u options are mutually exclusive. '
               'Choose one of these options. Exiting.')
        print(msg)
        sys.exit(1)

    # if -o option you must have specified -a option also
    if args.outfile is not None and not args.print_all:
        print('You must select -a and -o together. Exiting')
        sys.exit(1)

    setup_logger(args.logfile, args.loglevel)

    twindow = TIME_WINDOW
    if args.window:
        twindow = args.window
    # set distance thresholds
    radius = SEARCH_RADIUS
    if args.radius:
        radius = args.radius

    event_df = find_nearby_events(time, lat,
                                  lon, twindow, radius)

    if event_df is None:
        logging.error(
            'No events found matching your search criteria. Exiting.')
        sys.exit(0)

    nearest = event_df.iloc[0]

    if args.print_all:
        if not args.outfile:
            print(event_df)
        else:
            if args.format == 'excel':
                event_df.to_excel(args.outfile, index=False)
            elif args.format == 'tab':
                event_df.to_csv(args.outfile, sep='\t', index=False)
            else:
                event_df.to_csv(args.outfile, index=False)

            print('Wrote %i records to %s' % (len(event_df), args.outfile))
        sys.exit(0)

    if args.print_verbose:
        print('Event %s' % nearest['id'])
        cols = nearest.index.to_list()
        cols.remove('id')
        for col in cols:
            print('  %s : %s' % (col, nearest[col]))
        sys.exit(0)

    if args.print_url:
        print(nearest['url'])
        sys.exit(0)

    print(nearest['id'])
Exemplo n.º 2
0
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)
Exemplo n.º 3
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.bounds and args.radius:
        print('Please specify either a bounding box OR radius search.')
        sys.exit(1)

    if args.eventid:
        event = get_event_by_id(args.eventid, includesuperseded=args.all)
        events = [event]
    else:
        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,
                        producttype='losspager',
                        host=args.host)

    if not len(events):
        print('No events found matching your search criteria. Exiting.')
        sys.exit(0)

    dataframe = None
    nevents = len(events)
    i = 1
    for event in events:
        logging.debug('Processing event %s (%i of %i).\n' %
                      (event.id, i, nevents))

        if isinstance(event, SummaryEvent):
            detail = event.getDetailEvent(includesuperseded=args.all)
        else:
            detail = event
        df = get_pager_data_frame(detail,
                                  get_losses=args.get_losses,
                                  get_country_exposures=args.get_countries,
                                  get_all_versions=args.all)
        if dataframe is None:
            dataframe = df
        else:
            dataframe = pd.concat([dataframe, df])

    if dataframe is not None:
        logging.debug('Created table...saving %i records to %s.\n' %
                      (len(dataframe), args.filename))
        if args.format == 'excel':
            dataframe.to_excel(args.filename, index=False)
        elif args.format == 'tab':
            dataframe.to_csv(args.filename, sep='\t', index=False)
        else:
            dataframe.to_csv(args.filename, index=False, chunksize=1000)

        add_headers(args.filename, args.format)
        print('%i records saved to %s.' % (len(dataframe), args.filename))
    else:
        sys.stderr.write('No Pager products found for requested event(s)\n')
    sys.exit(0)
Exemplo n.º 4
0
def main():
    parser = get_parser()
    args = parser.parse_args()

    # --host and --scenario are mutually exclusive
    if args.host is not None and args.scenario:
        print(
            '--host and --scenario options are mutually exclusive. Please choose one.'
        )
        sys.exit(1)

    setup_logger(args.logfile, args.loglevel)

    if args.eventid:
        detail = get_event_by_id(args.eventid,
                                 includesuperseded=True,
                                 scenario=args.scenario)
        _get_product_from_detail(detail,
                                 args.product,
                                 args.contents,
                                 args.outputFolder,
                                 args.version,
                                 args.source,
                                 list_only=args.list_only)
        sys.exit(0)

    tsum = (args.bounds is not None) + \
        (args.radius is not None) + (args.country is not None)
    if tsum != 1:
        print('Please specify a bounding box, radius, or country code.')
        sys.exit(1)

    latitude = None
    longitude = None
    radiuskm = None
    lonmin = latmin = lonmax = latmax = None

    if args.startTime is None:
        starttime = datetime.utcnow() - timedelta(days=30)
        print('You did not specify a search start time, defaulting to %s' %
              str(starttime))
    else:
        starttime = args.startTime

    if args.endTime is None:
        endtime = datetime.utcnow()
        print('You did not specify a search end time, defaulting to %s' %
              str(endtime))
    else:
        endtime = args.endTime

    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)
            print(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]

    if isinstance(bounds, tuple) or bounds is None:
        events = search(starttime=starttime,
                        endtime=endtime,
                        updatedafter=args.after,
                        minlatitude=latmin,
                        maxlatitude=latmax,
                        minlongitude=lonmin,
                        maxlongitude=lonmax,
                        latitude=latitude,
                        longitude=longitude,
                        maxradiuskm=radiuskm,
                        catalog=args.catalog,
                        contributor=args.contributor,
                        producttype=args.product,
                        eventtype=args.eventType,
                        maxmagnitude=maxmag,
                        minmagnitude=minmag,
                        scenario=args.scenario,
                        host=args.host)
    else:
        events = []
        for i, tbounds in enumerate(bounds):
            lonmin, lonmax, latmin, latmax = tbounds
            tevents = search(starttime=starttime,
                             endtime=endtime,
                             updatedafter=args.after,
                             minlatitude=latmin,
                             maxlatitude=latmax,
                             minlongitude=lonmin,
                             maxlongitude=lonmax,
                             latitude=latitude,
                             longitude=longitude,
                             maxradiuskm=radiuskm,
                             catalog=args.catalog,
                             contributor=args.contributor,
                             producttype=args.product,
                             eventtype=args.eventType,
                             maxmagnitude=maxmag,
                             minmagnitude=minmag,
                             scenario=args.scenario,
                             host=args.host)
            events += tevents

    if not len(events):
        print('No events found matching your search criteria. Exiting.')
        sys.exit(0)

    if args.country:
        ids = [event.id for event in events]
        lats = [event.latitude for event in events]
        lons = [event.longitude for event in events]
        df = pd.DataFrame({'id': ids, 'latitude': lats, 'longitude': lons})
        df2 = filter_by_country(df, ccode, buffer_km=args.buffer)
        events = [event for event in events if event.id in df2['id'].unique()]

    for event in events:
        logging.debug('Retrieving products for event %s...' % event.id)
        if not event.hasProduct(args.product):
            continue
        try:
            detail = event.getDetailEvent(includesuperseded=True,
                                          scenario=args.scenario)
        except Exception as e:
            print(
                'Failed to retrieve detail event for event %s... continuing.' %
                event.id)
            continue
        _get_product_from_detail(detail,
                                 args.product,
                                 args.contents,
                                 args.outputFolder,
                                 args.version,
                                 args.source,
                                 list_only=args.list_only)

    sys.exit(0)
Exemplo n.º 5
0
def main():
    pd.set_option('display.width', 1000)
    pd.set_option('display.max_rows', 1000)
    pd.set_option('display.max_colwidth', -1)
    pd.set_option('display.max_columns', 1000)
    pd.set_option("display.colheader_justify", "left")
    parser = get_parser()
    args = parser.parse_args()

    setup_logger(args.logfile, args.loglevel)

    # make sure that input products are in the list of supported products
    if not set(args.products) <= set(PRODUCTS):
        unsupported = list(set(args.products) - set(PRODUCTS))
        fmt = 'The following event products are not supported: '
        print(fmt % (','.join(unsupported)))
        sys.exit(1)

    # make sure that excluded products are in the list of supported products
    if not set(args.exclude_products) <= set(PRODUCTS):
        unsupported = list(set(args.exclude_products) - set(PRODUCTS))
        fmt = ('The following event products you want to exclude '
               'are not supported: ')
        print(fmt % (','.join(unsupported)))
        sys.exit(1)

    # web output and directory output are mutually exclusive
    if args.outdir and args.web:
        msg = '''The -o and -w options are mutually exclusive, meaning
        that you cannot choose to write files to a directory and print
        HTML output to the screen simultaneously. Please choose one of
        those two options and try again.
        '''
        print(msg)
        sys.exit(1)

    if args.products:
        products = args.products
    else:
        products = PRODUCTS

    if args.exclude_products:
        products = set(products) - set(args.exclude_products)

    try:
        dataframe, event = get_history_data_frame(args.eventid, products)
    except Exception as e:
        fmt = '''Failed to retrieve event history data for
        event %s. Error message is as follows. Exiting.
        "%s"
        '''
        tpl = (args.eventid, str(e))
        print(fmt % tpl)
        sys.exit(1)

    if args.radius:
        radius_km = args.radius[0]
        radius_secs = args.radius[1]
        stime = event.time - timedelta(seconds=radius_secs)
        etime = event.time + timedelta(seconds=radius_secs)

        eventlist = search(starttime=stime,
                           endtime=etime,
                           latitude=event.latitude,
                           longitude=event.longitude,
                           maxradiuskm=radius_km)
        for tevent in eventlist:
            if tevent.id == event.id:
                continue
            detail = tevent.getDetailEvent(includesuperseded=True)
            tframe = get_history_data_frame(detail, products)
            newframe = _mod_tframe(event, tevent, tframe)
            dataframe = dataframe.append(newframe, ignore_index=True)

        # now re-sort by update time
        dataframe = dataframe.sort_values('Update Time')
        dataframe = dataframe[PRODUCT_COLUMNS]
    else:
        # since "Authoritative Event ID" and "Associated" columns are only applicable when
        # we're including other events in our results, drop those columns
        # if we're not doing that.
        drop_columns = ['Authoritative Event ID', 'Associated']
        dataframe = dataframe.drop(drop_columns, axis='columns')

    if args.outdir is not None and not os.path.isdir(args.outdir):
        os.makedirs(args.outdir)

    if args.split:
        df_products = dataframe['Product'].unique().tolist()
        available_products = set(df_products) & set(products)
        # TODO: Consider merging phase-data and origin products
        # somehow in this process
        for product in available_products:
            pframe = split_history_frame(dataframe, product=product)
            simplify_times(pframe)
            if args.web:
                web_print(event, pframe)
            else:
                outfile = save_dataframe(args.outdir,
                                         args.format,
                                         event,
                                         pframe,
                                         product=product)
                print('%i rows saved to %s' % (len(pframe), outfile))
        sys.exit(0)

    if args.outdir:
        outfile = save_dataframe(args.outdir,
                                 args.format,
                                 event,
                                 dataframe,
                                 product=None)

        print('%i rows saved to %s' % (len(dataframe), outfile))
    elif args.web:
        simplify_times(dataframe)
        web_print(event, dataframe)

    sys.exit(0)
Exemplo n.º 6
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)
Exemplo n.º 7
0
def main():
    parser = get_parser()
    args = parser.parse_args()

    setup_logger(args.logfile, args.loglevel)

    if args.eventid:
        detail = get_event_by_id(args.eventid, catalog=args.catalog)
        try:
            df = get_phase_dataframe(detail, args.catalog)
            filename = save_dataframe(
                df, args.directory, detail, args.format, catalog=args.catalog)
            print('Saved phase data for %s to %s' % (detail.id, filename))
            sys.exit(0)
        except Exception as e:
            fmt = ('Could not extract the phase data due to the '
                   'following error: \n"%s"\n\nExiting.')
            print(fmt % (str(e)))
            sys.exit(1)

    if args.bounds and args.radius:
        print('Please specify either a bounding box OR radius search.')
        sys.exit(1)

    if not os.path.isdir(args.directory):
        os.makedirs(args.directory)

    latitude = None
    longitude = None
    radiuskm = None
    lonmin = latmin = lonmax = latmax = None
    starttime = endtime = 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

    minmag = 0.0
    maxmag = 9.9
    if args.magRange:
        minmag = args.magRange[0]
        maxmag = args.magRange[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,
                    catalog=args.catalog,
                    contributor=args.contributor,
                    maxmagnitude=maxmag,
                    minmagnitude=minmag)

    if not len(events):
        print('No events found matching your search criteria. Exiting.')
        sys.exit(0)

    for event in events:
        if not event.hasProduct('phase-data'):
            continue
        try:
            detail = event.getDetailEvent()
            try:
                df = get_phase_dataframe(detail, args.catalog)
            except Exception as e:
                fmt = ('Could not get phase dataframe for '
                       'event %. Error "%s". Continuing.')
                tpl = (detail.id, str(e))
                print(fmt % tpl)
            filename = save_dataframe(
                df, args.directory, detail, args.format, catalog=args.catalog)

            print('Saved phase data for %s to %s' % (event.id, filename))
        except Exception as e:
            print('Failed to retrieve phase data for event %s.  Error "%s"... continuing.' % (
                event.id, str(e)))
            continue
Exemplo n.º 8
0
def main():
    # set the display width such that table output is not truncated
    pd.set_option('display.max_columns', 10000)
    pd.set_option("display.max_colwidth", 10000)
    pd.set_option("display.expand_frame_repr", False)

    parser = get_parser()

    args = parser.parse_args()

    # trap for mutually exclusive options a, u and v
    argsum = (args.print_all + args.print_url + args.print_verbose)
    if argsum > 1:
        msg = ('The -a, -v, and -u options are mutually exclusive. '
               'Choose one of these options. Exiting.')
        print(msg)
        sys.exit(1)

    # if -o option you must have specified -a option also
    if args.outfile is not None and not args.print_all:
        print('You must select -a and -o together. Exiting')
        sys.exit(1)

    # if -o format is not recognized, error out
    if args.outfile is not None:
        fpath, fext = os.path.splitext(args.outfile)
        supported = ['.xlsx', '.csv']
        if fext not in supported:
            fmt = ('File extension %s not in list of supported '
                   'formats: %s. Exiting.')
            print(fmt % (fext, str(supported)))
            sys.exit(1)

    setup_logger(args.logfile, args.loglevel)

    twindow = TIME_WINDOW
    if args.window:
        twindow = args.window
    # set distance thresholds
    radius = SEARCH_RADIUS
    if args.radius:
        radius = args.radius

    event_df = find_nearby_events(args.time, args.lat,
                                  args.lon, twindow, radius)

    if event_df is None:
        logging.error(
            'No events found matching your search criteria. Exiting.')
        sys.exit(0)

    nearest = event_df.iloc[0]

    if args.print_all:
        if not args.outfile:
            print(event_df)
        else:
            fpath, fext = os.path.splitext(args.outfile)
            if fext == '.xlsx':
                event_df.to_excel(args.outfile, index=False)
            else:
                event_df.to_csv(args.outfile, index=False)
            print('Wrote %i records to %s' % (len(event_df), args.outfile))
        sys.exit(0)

    if args.print_verbose:
        print('Event %s' % nearest['id'])
        cols = nearest.index.to_list()
        cols.remove('id')
        for col in cols:
            print('  %s : %s' % (col, nearest[col]))
        sys.exit(0)

    if args.print_url:
        print(nearest['url'])
        sys.exit(0)

    print(nearest['id'].values[0])