Beispiel #1
0
def test_get_detail_data_frame():
    datadir = get_datadir()
    tape_file = os.path.join(datadir, 'vcr_detail_frame.yaml')
    with vcr.use_cassette(tape_file):
        events = search(starttime=datetime(1994, 6, 1),
                        endtime=datetime(1994, 10, 6),
                        minmagnitude=8.0, maxmagnitude=9.0)
        all_mags = get_detail_data_frame(
            events, get_all_magnitudes=True, verbose=True)
        assert all_mags.iloc[0]['magnitude'] == 8.2
Beispiel #2
0
def test_get_detail_data_frame():
    cassettes, datadir = get_datadir()
    tape_file = os.path.join(cassettes, 'dataframes_detailed.yaml')
    with vcr.use_cassette(tape_file):
        events = search(starttime=datetime(1994, 6, 1),
                        endtime=datetime(1994, 10, 6),
                        minmagnitude=8.0,
                        maxmagnitude=9.0)
        all_mags = get_detail_data_frame(events, get_all_magnitudes=True)
        assert all_mags.iloc[0]['magnitude'] == 8.2
def test_get_detail_data_frame():
    cassettes, datadir = get_datadir()
    tape_file = os.path.join(cassettes, "dataframes_detailed.yaml")
    with vcr.use_cassette(tape_file, record_mode="new_episodes"):
        events = search.search(
            starttime=datetime(1994, 6, 1),
            endtime=datetime(1994, 10, 6),
            minmagnitude=8.0,
            maxmagnitude=9.0,
        )
        all_mags = get_detail_data_frame(events, get_all_magnitudes=True)
        assert all_mags.iloc[0]["magnitude"] == 8.2
Beispiel #4
0
from datetime import datetime
from libcomcat.dataframes import get_detail_data_frame
from libcomcat.search import search, get_event_by_id

summary_events = search(starttime=datetime(2010, 1, 1, 00, 00),
                        endtime=datetime(2019, 12, 31, 23, 59),
                        minlatitude=45,
                        maxlatitude=72,
                        minlongitude=-180,
                        maxlongitude=-125,
                        minmagnitude=4)
detail_df = get_detail_data_frame(summary_events,
                                  get_tensors='all',
                                  get_focals='all',
                                  get_moment_supplement=True)
detail_df.to_pickle('usgs_alaska_2010.p')
Beispiel #5
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)
Beispiel #6
0
remember to load correct environment in order to use the libcomcat library
@author: talongi
"""

from libcomcat.dataframes import get_summary_data_frame, get_detail_data_frame
from libcomcat.search import search, DetailEvent
from datetime import datetime

events = search(starttime=datetime(2006, 10, 30),
                endtime=datetime(2006, 11, 15),
                minlatitude=40.4,
                maxlatitude=40.6,
                minlongitude=-125,
                maxlongitude=-123)

detail_events = get_detail_data_frame(events)

#detail_events.to_csv('/auto/home/talongi/Cascadia/Data_tables/Events/ANSS_1980-2019_detailed.csv')

#%%
#find nans
import numpy as np
import pandas as pd

index_match = []
for i, x in enumerate(detail_events.nc_np1_strike):
    if type(x) == str:
        index_match.append(i)

foc_mech_cat = detail_events.iloc[index_match]
Beispiel #7
0
def main(args):

    intervalno = args.intervalno
    directory = args.directory
    if intervalno > 11:
        if args.previous is not None:
            previous = args.previous
        else:
            print('previous file required at input if interval no > 11.')
            print('enter previous file with flag -p previousFile at input')
            print('Exiting ...')
            exit()

    os.system('mkdir %s' % directory)

    if intervalno == 1:
        start = datetime(1900, 1, 1)
        finish = datetime(1970, 1, 1)
    if intervalno == 2:
        start = datetime(1970, 1, 1)
        finish = datetime(1980, 1, 1)
    if intervalno == 3:
        start = datetime(1980, 1, 1)
        finish = datetime(1990, 1, 1)
    if intervalno == 4:
        start = datetime(1990, 1, 1)
        finish = datetime(1995, 1, 1)
    if intervalno == 5:
        start = datetime(1995, 1, 1)
        finish = datetime(2000, 1, 1)
    if intervalno == 6:
        start = datetime(2000, 1, 1)
        finish = datetime(2005, 1, 1)
    if intervalno == 7:
        start = datetime(2005, 1, 1)
        finish = datetime(2010, 1, 1)
    if intervalno == 8:
        start = datetime(2010, 1, 1)
        finish = datetime(2012, 1, 1)
    if intervalno == 9:
        start = datetime(2012, 1, 1)
        finish = datetime(2014, 1, 1)
    if intervalno == 10:
        start = datetime(2014, 1, 1)
        finish = datetime(2016, 1, 1)
    if intervalno == 11:
        start = datetime(2016, 1, 1)
        finish = datetime.utcnow()
    if intervalno > 11:
        predata = pd.read_csv(previous)
        predata['time'] = pd.to_datetime(predata['time'])
        start = predata['time'].max()
        finish = datetime.utcnow()

    print(start)
    print(finish)

    #define magnitude range of earthquakes to search over for shallow earthquakes
    #(these are whatever magnitude range is defined in the catalogues)
    min_mag = 3.0
    max_mag = 9.9
    magrange = (min_mag, max_mag)

    #define depth to search over
    min_sh = 0
    max_sh = 900
    depthrange_sh = (min_sh, max_sh)

    #define grid size and extent of search (representative of lower left corner)
    if intervalno > 11:
        grid = 50.0
    else:
        grid = 10.0
    lonmin, lonmax = -180, 180
    latmin, latmax = -75, 75

    #define grid of searches (representative of lower left corner)
    xall = np.arange(lonmin, lonmax, grid)
    yall = np.arange(latmin, latmax, grid)
    lons1, lats1 = np.meshgrid(xall, yall)

    #flatten into list of lower left corners
    lllons = lons1.flatten()
    lllats = lats1.flatten()

    #define lists representing upper right corners
    urlons = lllons + grid
    urlats = lllats + grid

    #combine into one array (lonmin,lonmax,latmin,latmax)
    bounds = np.zeros((len(lllons), 4))
    bounds[:, 0] = lllons
    bounds[:, 1] = urlons
    bounds[:, 2] = lllats
    bounds[:, 3] = urlats
    iterations = len(bounds)

    lllatfail = []
    lllonfail = []
    urlatfail = []
    urlonfail = []

    totdf = pd.DataFrame()
    num = 0
    for i, line in enumerate(bounds):

        bounds = line

        #to follow along with the progress of data querying
        #since there are a lot of iterations, only print every so often
        k = 100
        if i % k == 0:

            print('Now querying grid %s of %s' % (i, iterations))

        searchlist = search(starttime=start,
                            endtime=finish,
                            minlatitude=bounds[2],
                            maxlatitude=bounds[3],
                            minlongitude=bounds[0],
                            maxlongitude=bounds[1],
                            minmagnitude=3.0)

        if len(searchlist) > 0:
            detaildf = get_detail_data_frame(searchlist,
                                             get_tensors='preferred',
                                             get_moment_supplement=True)

            totdf = pd.concat([totdf, detaildf], sort=True)
            print(bounds, len(detaildf), len(totdf))

            if len(totdf) > 5000:
                totdf.to_csv('%s/%s_%i.%i.csv' %
                             (directory, directory, intervalno, num),
                             header=True,
                             index=False,
                             na_rep=np.nan)
                num += 1
                totdf = pd.DataFrame()

    totdf.to_csv('%s/%s_%i.csv' % (directory, directory, intervalno),
                 header=True,
                 index=False,
                 na_rep=np.nan)