def test_ss3_m4p5():
    magnitude = 4.5
    dip = np.array([90])
    rake = 180.0
    width = np.array([15])
    fltx = np.array([0, 0])
    flty = np.array([0, 80])
    zp = np.array([0])
    epix = np.array([0])
    epiy = np.array([0.2 * flty[1]])

    # Convert to lat/lon
    proj = geo.utils.get_orthographic_projection(-122, -120, 39, 37)
    tlon, tlat = proj(fltx, flty, reverse=True)
    epilon, epilat = proj(epix, epiy, reverse=True)

    flt = rupture.QuadRupture.fromTrace(
        np.array([tlon[0]]), np.array([tlat[0]]),
        np.array([tlon[1]]), np.array([tlat[1]]),
        zp, width, dip, reference='ss3')

    event = {'lat': epilat[0],
             'lon': epilon[0],
             'depth': 10,
             'mag': magnitude,
             'id': 'ss3',
             'locstring': 'test',
             'type': 'SS',
             'timezone': 'UTC'}
    event['time'] = ShakeDateTime.utcfromtimestamp(int(time.time()))
    event['created'] = ShakeDateTime.utcfromtimestamp(int(time.time()))

    x = np.linspace(0, 20, 6)
    y = np.linspace(0, 90, 11)
    site_x, site_y = np.meshgrid(x, y)
    slon, slat = proj(site_x, site_y, reverse=True)
    deps = np.zeros_like(slon)
    source = Source(event, flt)
    source.setEventParam('rake', rake)

    test1 = Bayless2013(source, slat, slon, deps, T=1.0)

    # Test fd
    fd = test1.getFd()
    fd_test = np.array(
      [[ 0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.]])
    np.testing.assert_allclose(
        fd, fd_test, rtol=1e-4)
Exemple #2
0
def test():
    fault_text = """30.979788       103.454422      1
31.691615       104.419160      1
31.723569       104.374760      1
32.532213       105.220821      1
32.641450       105.135050      20
31.846790       104.246202      20
31.942158       104.205286      20
31.290105       103.284388      20
30.979788       103.454422      1"""
    event_text = """<?xml version="1.0" encoding="US-ASCII" standalone="yes"?>
<earthquake id="2008ryan" lat="30.9858" lon="103.3639" mag="7.9" year="2008" month="05" day="12" hour="06" minute="28" second="01" timezone="GMT" depth="19.0" locstring="EASTERN SICHUAN, CHINA" created="1211173621" otime="1210573681" type="" />
    """
    print('Testing creation of source object...')
    source_text = """mech=RS"""
    ffile = io.StringIO(fault_text)
    efile = io.StringIO(event_text)
    sfile = io.StringIO(source_text)
    source = Source.readFromFile(efile, faultfile=ffile, sourcefile=sfile)
    print('Passed creation of source object.')

    print('Testing creation of RuptureContext object...')
    gmpe = abrahamson_2014.AbrahamsonEtAl2014()
    rupture = source.getRuptureContext([gmpe])
    testdict = {
        'mag': 7.9,
        'strike': -133.083550974,
        'dip': 49.8524115024,
        'rake': 45.0,
        'ztor': 0.999999999995,
        'hypo_lon': 103.3639,
        'hypo_lat': 30.9858,
        'hypo_depth': 19.0,
        'width': 27.8623813381
    }
    for key in testdict.keys():
        value = eval('rupture.%s' % key)
        np.testing.assert_almost_equal(testdict[key], value)
    print('Passed creation of RuptureContext object...')

    print('Test setting mechanism and rake/dip...')
    mech = 'RS'
    exp_dip = 40
    exp_rake = 90
    source.setMechanism(mech)
    assert source.getEventParam('dip') == exp_dip
    assert source.getEventParam('rake') == exp_rake
    source.setMechanism('ALL', dip=45, rake=315)
    assert source.getEventParam('rake') == -45
    #this should raise an exception
    try:
        source.setMechanism('ALL', dip=110)
    except ShakeMapException as sme:
        print('Exception raised appropriately for dip greater than 90.')
    #this should raise an exception
    try:
        source.setMechanism('ALL', rake=370)
    except ShakeMapException as sme:
        print('Exception raised appropriately for rake greater than 360.')
    print('Test setting mechanism and rake/dip...')
Exemple #3
0
def test():
    fault_text = """30.979788       103.454422      1
31.691615       104.419160      1
31.723569       104.374760      1
32.532213       105.220821      1
32.641450       105.135050      20
31.846790       104.246202      20
31.942158       104.205286      20
31.290105       103.284388      20
30.979788       103.454422      1"""
    event_text = """<?xml version="1.0" encoding="US-ASCII" standalone="yes"?>
<earthquake id="2008ryan" lat="30.9858" lon="103.3639" mag="7.9" year="2008" month="05" day="12" hour="06" minute="28" second="01" timezone="GMT" depth="19.0" locstring="EASTERN SICHUAN, CHINA" created="1211173621" otime="1210573681" type="" />
    """
    print('Testing creation of source object...')
    source_text = """mech=RS"""
    ffile = io.StringIO(fault_text)
    efile = io.StringIO(event_text)
    sfile = io.StringIO(source_text)
    source = Source.readFromFile(efile,faultfile=ffile,sourcefile=sfile)
    print('Passed creation of source object.')

    print('Testing creation of RuptureContext object...')
    gmpe = abrahamson_2014.AbrahamsonEtAl2014()
    rupture = source.getRuptureContext([gmpe])
    testdict = {'mag':7.9,
                'strike': -133.083550974,
                'dip': 49.8524115024,
                'rake': 45.0,
                'ztor':0.999999999995,
                'hypo_lon':103.3639,
                'hypo_lat':30.9858,
                'hypo_depth':19.0,
                'width':27.8623813381}
    for key in testdict.keys():
        value = eval('rupture.%s' % key)
        np.testing.assert_almost_equal(testdict[key],value)
    print('Passed creation of RuptureContext object...')
    
    print('Test setting mechanism and rake/dip...')
    mech = 'RS'
    exp_dip = 40
    exp_rake = 90
    source.setMechanism(mech)
    assert source.getEventParam('dip') == exp_dip
    assert source.getEventParam('rake') == exp_rake
    source.setMechanism('ALL',dip=45,rake=315)
    assert source.getEventParam('rake') == -45
    #this should raise an exception
    try:
        source.setMechanism('ALL',dip=110)
    except ShakeMapException as sme:
        print('Exception raised appropriately for dip greater than 90.')
    #this should raise an exception
    try:
        source.setMechanism('ALL',rake=370)
    except ShakeMapException as sme:
        print('Exception raised appropriately for rake greater than 360.')
    print('Test setting mechanism and rake/dip...')
def test(stationfile,xmlfile,eventdict):
    tmp,dbfile = tempfile.mkstemp()
    os.close(tmp)
    os.remove(dbfile)
    try:
        print('Testing load from XML format...')
        t1 = time.time()
        stations1 = StationList.loadFromXML([xmlfile],dbfile)
        t2 = time.time()
        print('Passed load from XML format %i stations in %.2f seconds.' % (len(stations1),t2-t1))

        print('Testing filling in distance and derived MMI/PGM values...')
        source = Source(eventdict)
        stations1.fillTables(source)
        print('Passed filling in distance and derived MMI/PGM values...')
        
        print('Testing retrieval of MMI data from StationList object...')
        t1 = time.time()
        mmidf1 = stations1.getMMIStations()
        t2 = time.time()
        print('Passed retrieval of %i MMI data in %.2f seconds from StationList object.' % (len(mmidf1),t2-t1))

        print('Testing retrieval of instrumented data from StationList object...')
        t1 = time.time()
        imtdf1 = stations1.getInstrumentedStations()
        t2 = time.time()
        print('Passed retrieval of %i instrumented data in %.2f seconds from StationList object.' % (len(imtdf1),t2-t1))


        print('Testing load from sqlite format...')
        t1 = time.time()
        stations2 = StationList(stationfile)
        t2 = time.time()
        print('Passed load from sqlite format %i stations in %.2f seconds.' % (len(stations1),t2-t1))

        print('Testing retrieval of MMI data from StationList object...')
        t1 = time.time()
        mmidf2 = stations2.getMMIStations()
        t2 = time.time()
        print('Passed retrieval of %i MMI data in %.2f seconds from StationList object.' % (len(mmidf2),t2-t1))

        print('Testing retrieval of instrumented data from StationList object...')
        t1 = time.time()
        imtdf2 = stations2.getInstrumentedStations()
        t2 = time.time()
        print('Passed retrieval of %i instrumented data in %.2f seconds from StationList object.' % (len(imtdf1),t2-t1))

        assert(len(stations1) == len(stations2))

        
               
    except Exception as msg:
        print('Error caught: %s' % str(msg))
    if os.path.isfile(dbfile):
        os.remove(dbfile)
Exemple #5
0
def test_source():
    fault_text = """30.979788       103.454422      1
31.691615       104.419160      1
31.723569       104.374760      1
32.532213       105.220821      1
32.641450       105.135050      20
31.846790       104.246202      20
31.942158       104.205286      20
31.290105       103.284388      20
30.979788       103.454422      1"""
    event_text = """<?xml version="1.0" encoding="US-ASCII" standalone="yes"?>
<earthquake id="2008ryan" lat="30.9858" lon="103.3639" mag="7.9" year="2008" month="05" day="12" hour="06" minute="28" second="01" timezone="GMT" depth="19.0" locstring="EASTERN SICHUAN, CHINA" created="1211173621" otime="1210573681" type="" />
    """
    source_text = "mech=RS"
    ffile = io.StringIO(fault_text)
    efile = io.StringIO(event_text)
    sfile = io.StringIO(source_text)
    source = Source.fromFile(efile, faultfile=ffile, sourcefile=sfile)

    gmpe = abrahamson_2014.AbrahamsonEtAl2014()
    rupture = source.getRuptureContext([gmpe])
    testdict = {
        "mag": 7.9,
        "strike": -133.083550974,
        "dip": 49.8524115024,
        "rake": 45.0,
        "ztor": 0.999999999995,
        "hypo_lon": 103.3639,
        "hypo_lat": 30.9858,
        "hypo_depth": 19.0,
        "width": 27.8623813381,
    }
    for key in testdict.keys():
        value = eval("rupture.%s" % key)
        np.testing.assert_almost_equal(testdict[key], value)

    mech = "RS"
    exp_dip = 40
    exp_rake = 90
    source.setMechanism(mech)
    assert source.getEventParam("dip") == exp_dip
    assert source.getEventParam("rake") == exp_rake
    source.setMechanism("ALL", dip=45, rake=315)
    assert source.getEventParam("rake") == -45

    # this should raise an exception
    with pytest.raises(Exception) as e_info:
        source.setMechanism("ALL", dip=110)
    with pytest.raises(Exception) as e_info:
        source.setMechanism("ALL", rake=620)
Exemple #6
0
def _test_northridge():
    fault_text = """
    # Source: Wald, D. J., T. H. Heaton, and K. W. Hudnut (1996). The Slip History of the 1994 Northridge, California, Earthquake Determined from Strong-Motion, Teleseismic, GPS, and Leveling Data, Bull. Seism. Soc. Am. 86, S49-S70.
    34.315 -118.421 5.000
    34.401 -118.587 5.000
    34.261 -118.693 20.427
    34.175 -118.527 20.427
    34.315 -118.421 5.000
    """
    event_text = """<?xml version="1.0" encoding="US-ASCII" standalone="yes"?>
<earthquake id="blah" lat="34.213" lon="-118.537" mag="7.9" year="1994" month="01" day="17" hour="12" minute="30" second="55" timezone="GMT" depth="18.4" locstring="NORTHRIDGE" created="1211173621" otime="1210573681" type="" />
    """
    source_text = """mech=RS"""
    ffile = io.StringIO(fault_text)
    efile = io.StringIO(event_text)
    sfile = io.StringIO(source_text)
    source = Source.readFromFile(efile,faultfile=ffile,sourcefile=sfile)
    gmpe = abrahamson_2014.AbrahamsonEtAl2014()
    rupture = source.getRuptureContext(gmpe)
    mapwidth = 2.0
    latmin = rupture.hypo_lat - mapwidth
    latmax = rupture.hypo_lat + mapwidth
    lonmin = rupture.hypo_lon - mapwidth
    lonmax = rupture.hypo_lon + mapwidth
    dim = 0.02
    lats = np.arange(latmin,latmax,dim)
    lons = np.arange(lonmin,lonmax,dim)
    lon,lat = np.meshgrid(lons,lats)
    dep = np.zeros_like(lon)
    mesh = Mesh(lon,lat,dep)
    distances = source.getDistanceContext(gmpe,mesh)
    rupture = source.getRuptureContext(gmpe)
    for key in rupture._slots_:
        try:
            value = eval('rupture.%s' % key)
        except:
            print('No value set for %s' % key)
            continue
        print('%s = %s' % (key,str(value)))    

    cbuf = io.StringIO(fault_text)
    fault = Fault.readFaultFile(cbuf)
Exemple #7
0
def _test_northridge():
    fault_text = """
    # Source: Wald, D. J., T. H. Heaton, and K. W. Hudnut (1996). The Slip History of the 1994 Northridge, California, Earthquake Determined from Strong-Motion, Teleseismic, GPS, and Leveling Data, Bull. Seism. Soc. Am. 86, S49-S70.
    34.315 -118.421 5.000
    34.401 -118.587 5.000
    34.261 -118.693 20.427
    34.175 -118.527 20.427
    34.315 -118.421 5.000
    """
    event_text = """<?xml version="1.0" encoding="US-ASCII" standalone="yes"?>
<earthquake id="blah" lat="34.213" lon="-118.537" mag="7.9" year="1994" month="01" day="17" hour="12" minute="30" second="55" timezone="GMT" depth="18.4" locstring="NORTHRIDGE" created="1211173621" otime="1210573681" type="" />
    """
    source_text = """mech=RS"""
    ffile = io.StringIO(fault_text)
    efile = io.StringIO(event_text)
    sfile = io.StringIO(source_text)
    source = Source.readFromFile(efile, faultfile=ffile, sourcefile=sfile)
    gmpe = abrahamson_2014.AbrahamsonEtAl2014()
    rupture = source.getRuptureContext(gmpe)
    mapwidth = 2.0
    latmin = rupture.hypo_lat - mapwidth
    latmax = rupture.hypo_lat + mapwidth
    lonmin = rupture.hypo_lon - mapwidth
    lonmax = rupture.hypo_lon + mapwidth
    dim = 0.02
    lats = np.arange(latmin, latmax, dim)
    lons = np.arange(lonmin, lonmax, dim)
    lon, lat = np.meshgrid(lons, lats)
    dep = np.zeros_like(lon)
    mesh = Mesh(lon, lat, dep)
    distances = source.getDistanceContext(gmpe, mesh)
    rupture = source.getRuptureContext(gmpe)
    for key in rupture._slots_:
        try:
            value = eval('rupture.%s' % key)
        except:
            print('No value set for %s' % key)
            continue
        print('%s = %s' % (key, str(value)))

    cbuf = io.StringIO(fault_text)
    fault = Fault.readFaultFile(cbuf)
Exemple #8
0
def test_virtualipe():

    #
    # Set up the GMPE, IPE, and GMICE
    #
    gmpe_cy14 = ChiouYoungs2014()
    gmpe = MultiGMPE.from_list([gmpe_cy14], [1.0])
    gmice = WGRW12()
    ipe = VirtualIPE.fromFuncs(gmpe, gmice)

    #
    # Use the Calexico event info
    #
    homedir = os.path.dirname(os.path.abspath(__file__))
    datadir = os.path.abspath(os.path.join(homedir, "..", "data", "eventdata", "Calexico", "input"))

    #
    # Read the event, source, and fault files and produce a Source object
    #
    inputfile = os.path.join(datadir, "stationlist_dat.xml")
    dyfifile = os.path.join(datadir, "ciim3_dat.xml")
    eventfile = os.path.join(datadir, "event.xml")
    faultfile = os.path.join(datadir, "wei_fault.txt")

    source_obj = Source.fromFile(eventfile, faultfile=faultfile)
    rx = source_obj.getRuptureContext([gmpe])

    smdx = 0.0083333333
    smdy = 0.0083333333
    lonspan = 6.0
    latspan = 4.0
    vs30filename = os.path.join(datadir, "..", "vs30", "vs30.grd")

    sites_obj_grid = Sites.fromCenter(
        rx.hypo_lon,
        rx.hypo_lat,
        lonspan,
        latspan,
        smdx,
        smdy,
        defaultVs30=760.0,
        vs30File=vs30filename,
        vs30measured_grid=None,
        padding=False,
        resample=False,
    )

    npts = 200
    lats = np.empty(npts)
    lons = np.empty(npts)
    depths = np.zeros(npts)
    for i in range(npts):
        lats[i] = rx.hypo_lat
        lons[i] = rx.hypo_lon + i * 0.01
    lldict = {"lats": lats, "lons": lons}

    sx = sites_obj_grid.getSitesContext(lldict=lldict, rock_vs30=760.0)

    dobj = Distance(gmpe, source_obj, lats, lons, depths)
    dx = dobj.getDistanceContext()

    sd_types = [oqconst.StdDev.TOTAL]
    mmi_const_vs30, mmi_sd_const_vs30 = ipe.get_mean_and_stddevs(sx, rx, dx, MMI(), sd_types)

    # These prints are just so a human can examine the outputs
    #    print(mmi_const_vs30)
    #    print(mmi_sd_const_vs30)

    sx = sites_obj_grid.getSitesContext(lldict=lldict)
    mmi_variable_vs30, mmi_sd_variable_vs30 = ipe.get_mean_and_stddevs(sx, rx, dx, MMI(), sd_types)

    #    print(mmi_variable_vs30)
    #    print(mmi_sd_variable_vs30)

    sd_types = [oqconst.StdDev.TOTAL, oqconst.StdDev.INTRA_EVENT, oqconst.StdDev.INTER_EVENT]
    mmi_variable_vs30_intra, mmi_sd_variable_vs30_intra = ipe.get_mean_and_stddevs(sx, rx, dx, MMI(), sd_types)

    #    print(mmi_variable_vs30_intra)
    #    print(mmi_sd_variable_vs30_intra)
    #    assert(0)      # Assert causes test to fail and prints to be displayed

    #
    # Try with PGA
    #
    gmpe.DEFINED_FOR_INTENSITY_MEASURE_TYPES.remove(PGV)
    gmpe.ALL_GMPES_HAVE_PGV = False
    ipe = VirtualIPE.fromFuncs(gmpe, gmice)
    mmi_pga, mmi_sd_pga = ipe.get_mean_and_stddevs(sx, rx, dx, MMI(), sd_types)
    #
    # Try with SA(1.0)
    #
    gmpe.DEFINED_FOR_INTENSITY_MEASURE_TYPES.remove(PGA)
    ipe = VirtualIPE.fromFuncs(gmpe, gmice)
    mmi_psa, mmi_sd_psa = ipe.get_mean_and_stddevs(sx, rx, dx, MMI(), sd_types)

    #
    # This should raise an exception because the IMT isn't MMI
    #
    with pytest.raises(ValueError) as e:
        mmi_psa, mmi_sd_psa = ipe.get_mean_and_stddevs(sx, rx, dx, PGA(), sd_types)
    #
    # This should raise an exception because no valid IMTs are available
    #
    gmpe.DEFINED_FOR_INTENSITY_MEASURE_TYPES.remove(SA)
    with pytest.raises(ShakeMapException) as e:
        ipe = VirtualIPE.fromFuncs(gmpe, gmice)

    #
    # Now do a GMPE that uses Rjb instead of Rrup
    #
    gmpe_ba14 = BooreEtAl2014()
    gmpe = MultiGMPE.from_list([gmpe_ba14], [1.0])
    ipe = VirtualIPE.fromFuncs(gmpe, gmice)
    rx = source_obj.getRuptureContext([gmpe])
    dobj = Distance(gmpe, source_obj, lats, lons, depths)
    dx = dobj.getDistanceContext()

    mmi_rjb, mmi_sd_rjb = ipe.get_mean_and_stddevs(sx, rx, dx, MMI(), sd_types)

    #
    # Test the results against a known standard
    #
    savefile = os.path.abspath(
        os.path.join(homedir, "..", "data", "eventdata", "Calexico", "virtualipe_test", "savefile.npz")
    )

    #
    # If things change, set remake_save to True, and it will rebuild the
    # saved data file against which the comparisons are done
    # Remember to set this back to False once you've remade the test datafile
    #
    remake_save = False
    if remake_save:
        np.savez_compressed(
            savefile,
            mmi_const_vs30=mmi_const_vs30,
            mmi_sd_const_vs30=mmi_sd_const_vs30[0],
            mmi_variable_vs30=mmi_variable_vs30,
            mmi_sd_variable_vs30=mmi_sd_variable_vs30[0],
            mmi_variable_vs30_intra=mmi_variable_vs30_intra,
            mmi_sd_variable_vs30_total=mmi_sd_variable_vs30_intra[0],
            mmi_sd_variable_vs30_intra=mmi_sd_variable_vs30_intra[1],
            mmi_sd_variable_vs30_inter=mmi_sd_variable_vs30_intra[2],
            mmi_pga=mmi_pga,
            mmi_sd_pga=mmi_sd_pga[0],
            mmi_psa=mmi_psa,
            mmi_sd_psa=mmi_sd_psa[0],
            mmi_rjb=mmi_rjb,
            mmi_sd_rjb=mmi_sd_rjb[0],
        )

    td = np.load(savefile)

    assert np.allclose(td["mmi_const_vs30"], mmi_const_vs30)
    assert np.allclose(td["mmi_sd_const_vs30"], mmi_sd_const_vs30[0])
    assert np.allclose(td["mmi_variable_vs30"], mmi_variable_vs30)
    assert np.allclose(td["mmi_sd_variable_vs30"], mmi_sd_variable_vs30[0])
    assert np.allclose(td["mmi_variable_vs30_intra"], mmi_variable_vs30_intra)
    assert np.allclose(td["mmi_sd_variable_vs30_total"], mmi_sd_variable_vs30_intra[0])
    assert np.allclose(td["mmi_sd_variable_vs30_intra"], mmi_sd_variable_vs30_intra[1])
    assert np.allclose(td["mmi_sd_variable_vs30_inter"], mmi_sd_variable_vs30_intra[2])
    assert np.allclose(td["mmi_pga"], mmi_pga)
    assert np.allclose(td["mmi_sd_pga"], mmi_sd_pga[0])
    assert np.allclose(td["mmi_psa"], mmi_psa)
    assert np.allclose(td["mmi_sd_psa"], mmi_sd_psa[0])
    assert np.allclose(td["mmi_rjb"], mmi_rjb)
    assert np.allclose(td["mmi_sd_rjb"], mmi_sd_rjb[0])

    # The total uncertainties should be greater than the intra-event
    assert np.all(mmi_sd_variable_vs30[0] > mmi_sd_variable_vs30_intra[1])

    # The combined intra and inter-event uncertainty should be equal
    # to the total
    tot = np.sqrt(mmi_sd_variable_vs30_intra[1] ** 2 + mmi_sd_variable_vs30_intra[2] ** 2)
    assert np.allclose(tot, mmi_sd_variable_vs30_intra[0], rtol=1e-2)
def test_so6():
    event_name = 'so6'
    magnitude = 7.2
    dip = np.array([70])
    rake = 135
    width = np.array([15])
    L = 80
    fltx = np.array([0, 0])
    flty = np.array([0, L])
    zp = np.array([0])
    # Convert to lat/lon
    proj = geo.utils.get_orthographic_projection(-122, -120, 39, 37)
    tlon,tlat = proj(fltx, flty, reverse = True)
    flt = fault.Fault.fromTrace(np.array([tlon[0]]), np.array([tlat[0]]), 
                                np.array([tlon[1]]), np.array([tlat[1]]),
                                zp, width, dip, reference = 'rv4')
    x = np.linspace(-80, 80, 21)
    y = np.linspace(-50, 130, 21)
    site_x,site_y = np.meshgrid(x, y)
    slon,slat = proj(site_x, site_y, reverse = True)
    sdepth = np.zeros_like(slon)
    tmp = flt.getQuadrilaterals()[0]
    pp0 = Vector.fromPoint(point.Point(tmp[0].longitude, tmp[0].latitude, tmp[0].depth))
    pp1 = Vector.fromPoint(point.Point(tmp[1].longitude, tmp[1].latitude, tmp[1].depth))
    pp2 = Vector.fromPoint(point.Point(tmp[2].longitude, tmp[2].latitude, tmp[2].depth))
    pp3 = Vector.fromPoint(point.Point(tmp[3].longitude, tmp[3].latitude, tmp[3].depth))
    dxp = 10/L
    dyp = (width-5)/width
    mp0 = pp0 + (pp1 - pp0)*dxp
    mp1 = pp3 + (pp2 - pp3)*dxp
    rp = mp0 + (mp1 - mp0)*dyp
    epilat,epilon,epidepth = ecef2latlon(rp.x, rp.y, rp.z)
    epix,epiy = proj(epilon, epilat, reverse = False)
    event = {'lat': epilat, 
             'lon': epilon, 
             'depth':epidepth, 
             'mag': magnitude, 
             'id':'so6',
             'locstring':'so6',
             'type':'RV',
             'timezone':'UTC'}
    event['time'] = ShakeDateTime.utcfromtimestamp(int(time.time()))
    event['created'] = ShakeDateTime.utcfromtimestamp(int(time.time()))
    fltlat = [a.latitude for a in flt.getQuadrilaterals()[0]]
    fltlon = [a.longitude for a in flt.getQuadrilaterals()[0]]
    fltlat = np.append(fltlat, fltlat[0])
    fltlon = np.append(fltlon, fltlon[0])
    fltx,flty = proj(fltlon, fltlat, reverse = False)
    source = Source(event, flt)
    source.setEventParam('rake', rake)
    test1 = Bayless2013(source, slat, slon, sdepth, T = 5)
    fd = test1.getFd()
    fd_test = np.array(
      [[  0.00000000e+00,   0.00000000e+00,   0.00000000e+00,
         -8.92879772e-03,  -1.74526918e-02,  -2.22981746e-02,
         -2.34350450e-02,  -2.13620062e-02,  -1.72712346e-02,
         -1.29509613e-02,  -1.02545064e-02,  -1.03010185e-02,
         -1.28847597e-02,  -1.66274727e-02,  -1.96984070e-02,
         -2.05377743e-02,  -1.81831337e-02,  -1.21881814e-02,
         -2.64862879e-03,   0.00000000e+00,   0.00000000e+00],
       [  0.00000000e+00,   0.00000000e+00,  -8.73221519e-03,
         -2.21421374e-02,  -3.18438939e-02,  -3.71488270e-02,
         -3.76239913e-02,  -3.35015951e-02,  -2.61748968e-02,
         -1.83864728e-02,  -1.34793002e-02,  -1.36687799e-02,
         -1.85727143e-02,  -2.55527671e-02,  -3.14227568e-02,
         -3.38933995e-02,  -3.19289607e-02,  -2.53396980e-02,
         -1.45943649e-02,  -3.71405488e-04,   0.00000000e+00],
       [  0.00000000e+00,  -2.54621422e-03,  -2.11428566e-02,
         -3.68609103e-02,  -4.87464747e-02,  -5.56539037e-02,
         -5.64419387e-02,  -5.05331157e-02,  -3.52919381e-02,
         -2.18782050e-02,  -1.40858125e-02,  -1.47354546e-02,
         -2.35727189e-02,  -3.74838465e-02,  -4.75915414e-02,
         -5.13000399e-02,  -4.87882409e-02,  -4.05716321e-02,
         -2.77368254e-02,  -1.13542729e-02,   0.00000000e+00],
       [  0.00000000e+00,  -1.21642958e-02,  -3.33747360e-02,
         -5.21661817e-02,  -6.74724509e-02,  -7.77628842e-02,
         -8.00243748e-02,  -6.42496853e-02,  -4.38124530e-02,
         -1.97027426e-02,  -1.45897731e-02,  -1.07427056e-02,
         -3.08235222e-02,  -4.82656988e-02,  -6.67692677e-02,
         -7.35152908e-02,  -6.85574283e-02,  -5.71811573e-02,
         -4.12138780e-02,  -2.20396726e-02,  -6.24121310e-04],
       [  0.00000000e+00,  -2.00643401e-02,  -4.39827328e-02,
         -6.62722434e-02,  -8.60268414e-02,  -1.01730306e-01,
         -9.86277741e-02,  -9.82914922e-02,  -5.22335876e-02,
         -1.54622435e-02,  -1.57487554e-02,  -3.06190808e-03,
         -4.81481586e-02,  -8.92480491e-02,  -8.63776477e-02,
         -9.98130440e-02,  -8.95491230e-02,  -7.33553695e-02,
         -5.34401725e-02,  -3.11601812e-02,  -7.33715103e-03],
       [  0.00000000e+00,  -2.50053614e-02,  -5.11695772e-02,
         -7.65997026e-02,  -1.00809054e-01,  -1.22877573e-01,
         -1.18738178e-01,  -1.55236782e-01,  -7.45388001e-02,
          1.92779182e-03,  -1.94380016e-02,   1.94922939e-02,
         -7.66669920e-02,  -1.53909722e-01,  -1.10846875e-01,
         -1.19746768e-01,  -1.07680300e-01,  -8.59905101e-02,
         -6.22042294e-02,  -3.71802472e-02,  -1.13867485e-02],
       [  0.00000000e+00,  -2.63645827e-02,  -5.37984901e-02,
         -8.11337022e-02,  -1.08298371e-01,  -1.35146441e-01,
         -1.34825430e-01,  -1.85836050e-01,  -1.10730875e-01,
         -3.18861095e-02,   4.14395701e-02,  -1.52711946e-02,
         -1.31840763e-01,  -1.96794707e-01,  -1.33453212e-01,
         -1.34989129e-01,  -1.17922385e-01,  -9.21637323e-02,
         -6.58369237e-02,  -3.91646838e-02,  -1.22685698e-02],
       [  0.00000000e+00,  -2.64622244e-02,  -5.40483999e-02,
         -8.16190336e-02,  -1.09162854e-01,  -1.36656677e-01,
         -1.37081504e-01,  -1.89522811e-01,  -1.17723634e-01,
         -4.88765748e-02,  -5.04529015e-03,  -5.76414497e-02,
         -1.45712183e-01,  -2.03062804e-01,  -1.36859828e-01,
         -1.37107390e-01,  -1.19124650e-01,  -9.28263279e-02,
         -6.61800709e-02,  -3.93088682e-02,  -1.22842049e-02],
       [  0.00000000e+00,  -2.58466495e-02,  -5.24858827e-02,
         -7.86086164e-02,  -1.03856343e-01,  -1.27529509e-01,
         -1.23794779e-01,  -1.68810613e-01,  -8.22602627e-02,
          1.74236964e-02,   9.38708725e-02,   4.23208284e-02,
         -8.46343723e-02,  -1.70476759e-01,  -1.17547884e-01,
         -1.24569752e-01,  -1.11518670e-01,  -8.84736806e-02,
         -6.38037151e-02,  -3.81874381e-02,  -1.19867610e-02],
       [  0.00000000e+00,  -2.42186547e-02,  -4.84175525e-02,
         -7.09428614e-02,  -9.07754575e-02,  -1.06117824e-01,
         -9.50228292e-02,  -1.29781980e-01,  -3.08573454e-02,
          7.39058739e-02,   1.30478117e-01,   8.28181149e-02,
         -2.70389535e-02,  -1.20837502e-01,  -8.02081725e-02,
         -9.70274506e-02,  -9.35853383e-02,  -7.77422806e-02,
         -5.77817530e-02,  -3.53067886e-02,  -1.12414659e-02],
       [  0.00000000e+00,  -2.16818717e-02,  -4.22363856e-02,
         -5.96909893e-02,  -7.24805224e-02,  -7.81867829e-02,
         -6.11838569e-02,  -9.05679744e-02,   9.95934969e-03,
          1.07503875e-01,   1.52073917e-01,   1.05894634e-01,
          8.68652263e-03,  -7.98571818e-02,  -4.16548658e-02,
         -6.40511838e-02,  -6.99337160e-02,  -6.26305633e-02,
         -4.89098800e-02,  -3.09284566e-02,  -1.00919381e-02],
       [  0.00000000e+00,  -1.84940182e-02,  -3.47054606e-02,
         -4.65278129e-02,  -5.22037664e-02,  -4.93977115e-02,
         -2.95395230e-02,  -5.82421092e-02,   3.91025654e-02,
          1.29337956e-01,   1.67436703e-01,   1.21969296e-01,
          3.20823547e-02,  -5.00287386e-02,  -9.22993907e-03,
         -3.27186625e-02,  -4.52706958e-02,  -4.57409325e-02,
         -3.84701291e-02,  -2.55751405e-02,  -8.64950254e-03],
       [  0.00000000e+00,  -1.49431380e-02,  -2.65887341e-02,
         -3.29162158e-02,  -3.22994323e-02,  -2.29081781e-02,
         -2.60259636e-03,  -3.29856530e-02,   6.02631314e-02,
          1.45003704e-01,   1.79361264e-01,   1.34292814e-01,
          4.88007115e-02,  -2.82328554e-02,   1.64212421e-02,
         -5.72391847e-03,  -2.23438861e-02,  -2.90246794e-02,
         -2.76054402e-02,  -1.97779758e-02,  -7.03945406e-03],
       [  0.00000000e+00,  -1.12771143e-02,  -1.84737590e-02,
         -1.98228664e-02,  -1.40092305e-02,   1.84580818e-04,
          1.95817303e-02,  -1.32608487e-02,   7.62783168e-02,
          1.57076433e-01,   1.89083905e-01,   1.44259188e-01,
          6.15722813e-02,  -1.17505212e-02,   3.65938109e-02,
          1.66937711e-02,  -2.18970818e-03,  -1.35507683e-02,
         -1.70890527e-02,  -1.39519424e-02,  -5.37036892e-03],
       [  0.00000000e+00,  -7.67615215e-03,  -1.07348257e-02,
         -7.75276739e-03,   2.22351695e-03,   1.98662250e-02,
          3.77611177e-02,   2.42018661e-03,   8.89036172e-02,
          1.66855206e-01,   1.97260700e-01,   1.52590263e-01,
          7.17981256e-02,   1.18005972e-03,   5.26852303e-02,
          3.51638855e-02,   1.51012176e-02,   2.69654076e-04,
         -7.33815554e-03,  -8.36639665e-03,  -3.72176313e-03],
       [  0.00000000e+00,  -4.50552324e-03,  -4.32262850e-03,
          1.73559158e-03,   1.42670366e-02,   3.35040699e-02,
          4.97279358e-02,   1.85410528e-02,   9.39950666e-02,
          1.46646579e-01,   9.13474746e-02,   1.37004651e-01,
          7.74648339e-02,   1.59777072e-02,   6.25334939e-02,
          4.74577418e-02,   2.72155518e-02,   1.06174952e-02,
          3.94103899e-04,  -3.68465400e-03,  -2.19830733e-03],
       [  0.00000000e+00,  -1.74629916e-03,   5.44471813e-04,
          8.22933499e-03,   2.15699287e-02,   4.04232250e-02,
          5.69678048e-02,   5.52408259e-02,   9.04381272e-02,
          1.08204635e-01,   9.14439984e-02,   1.06884511e-01,
          8.17241884e-02,   5.55282924e-02,   6.78528399e-02,
          5.47188925e-02,   3.35251483e-02,   1.69615982e-02,
          5.72048628e-03,  -8.81437278e-05,  -7.36518436e-04],
       [  0.00000000e+00,   4.07838765e-05,   3.63933766e-03,
          1.20080876e-02,   2.51274691e-02,   4.25687176e-02,
          6.25685606e-02,   7.33480475e-02,   8.37515545e-02,
          9.52500287e-02,   9.15135660e-02,   9.66442834e-02,
          8.66659913e-02,   8.10325633e-02,   7.18836713e-02,
          5.45548434e-02,   3.55884875e-02,   2.00142359e-02,
          8.71200201e-03,   2.04407846e-03,  -6.53680674e-06],
       [  0.00000000e+00,   2.40054729e-04,   4.44975227e-03,
          1.27572519e-02,   2.49362989e-02,   4.03831326e-02,
          5.80039988e-02,   7.61280192e-02,   8.37404162e-02,
          8.89634569e-02,   9.15651607e-02,   9.13586235e-02,
          8.83589144e-02,   8.27804032e-02,   6.75666471e-02,
          5.00483249e-02,   3.36733366e-02,   1.96758691e-02,
          9.00603204e-03,   2.18370401e-03,   0.00000000e+00],
       [  0.00000000e+00,   0.00000000e+00,   2.78776980e-03,
          1.05086036e-02,   2.13238822e-02,   3.45577738e-02,
          4.91570145e-02,   6.36787133e-02,   7.63710088e-02,
          8.54072310e-02,   8.92960200e-02,   8.75702197e-02,
          8.07095447e-02,   6.97999389e-02,   5.63787286e-02,
          4.20734776e-02,   2.83073312e-02,   1.61614525e-02,
          6.56194125e-03,   1.00721924e-04,   0.00000000e+00],
       [  0.00000000e+00,   0.00000000e+00,   0.00000000e+00,
          5.49667845e-03,   1.47563319e-02,   2.57955743e-02,
          3.76689418e-02,   4.91861917e-02,   5.90108907e-02,
          6.58478416e-02,   6.87018515e-02,   6.73174642e-02,
          6.20270643e-02,   5.35456385e-02,   4.29400416e-02,
          3.14129728e-02,   2.00795162e-02,   9.84001885e-03,
          1.53992995e-03,   0.00000000e+00,   0.00000000e+00]]
    )
    np.testing.assert_allclose(fd, fd_test, rtol=1e-4)
def test_rv4():
    magnitude = 7.0
    rake = 90.0
    width = np.array([28])
    fltx = np.array([0, 0])
    flty = np.array([0, 32])
    zp = np.array([0])
    dip = np.array([30])

    # Convert to lat/lon
    proj = geo.utils.get_orthographic_projection(-122, -120, 39, 37)
    tlon, tlat = proj(fltx, flty, reverse=True)

    flt = fault.Fault.fromTrace(np.array([tlon[0]]), np.array([tlat[0]]),
                                np.array([tlon[1]]), np.array([tlat[1]]),
                                zp, width, dip, reference='')
    L = flt.getFaultLength()

    # Try to figure out epicenter
    tmp = flt.getQuadrilaterals()[0]
    pp0 = Vector.fromPoint(point.Point(tmp[0].longitude, tmp[0].latitude,
                                       tmp[0].depth))
    pp1 = Vector.fromPoint(point.Point(tmp[1].longitude, tmp[1].latitude,
                                       tmp[1].depth))
    pp2 = Vector.fromPoint(point.Point(tmp[2].longitude, tmp[2].latitude,
                                       tmp[2].depth))
    pp3 = Vector.fromPoint(point.Point(tmp[3].longitude, tmp[3].latitude,
                                       tmp[3].depth))
    dxp = 6/L
    dyp = (width-8)/width
    mp0 = pp0 + (pp1 - pp0)*dxp
    mp1 = pp3 + (pp2 - pp3)*dxp
    rp = mp0 + (mp1 - mp0)*dyp
    epilat,epilon,epidepth = ecef2latlon(rp.x, rp.y, rp.z)

    event = {'lat': epilat,
             'lon': epilon,
             'depth': epidepth,
             'mag': magnitude,
             'id': 'test',
             'locstring': 'rv4',
             'type': 'DS',
             'timezone': 'UTC'}
    event['time'] = ShakeDateTime.utcfromtimestamp(int(time.time()))
    event['created'] = ShakeDateTime.utcfromtimestamp(int(time.time()))

    x = np.linspace(-50, 50, 11)
    y = np.linspace(-50, 50, 11)
    site_x, site_y = np.meshgrid(x, y)
    slon, slat = proj(site_x, site_y, reverse=True)
    deps = np.zeros_like(slon)
    source = Source(event, flt)
    source.setEventParam('rake', rake)

    test1 = Bayless2013(source, slat, slon, deps, T=2.0)

    # Test fd
    fd = test1.getFd()
    fd_test = np.array(
      [[  0.00000000e+00,   0.00000000e+00,   0.00000000e+00,
          1.72143257e-03,   1.34977260e-03,   4.33616224e-15,
          1.24446253e-03,   1.16142357e-03,   2.25464716e-03,
          7.05281751e-04,   0.00000000e+00],
       [  0.00000000e+00,   0.00000000e+00,   7.62610242e-03,
          1.25133844e-02,   5.61896104e-03,   7.63126014e-15,
          4.52266194e-03,   4.67970900e-03,   1.02820316e-02,
          5.13160096e-03,  -6.13926251e-03],
       [  0.00000000e+00,   4.00495234e-03,   2.37608386e-02,
          2.37139333e-02,   9.55224050e-03,   5.66364910e-15,
          7.70344813e-03,   7.36466362e-03,   1.48239704e-02,
          8.40388145e-03,  -1.58592485e-02],
       [  8.08385547e-19,   9.38150101e-03,   3.38610620e-02,
          3.85351492e-02,   1.91044918e-02,   3.98697802e-15,
          1.54321666e-02,   1.21913760e-02,   2.04435166e-02,
          1.04931859e-02,  -1.85935894e-02],
       [  2.12025421e-18,   1.37316085e-02,   4.40193799e-02,
          6.16562477e-02,   4.77612496e-02,   2.60257085e-15,
          3.86322888e-02,   1.97965887e-02,   2.64882038e-02,
          1.23335908e-02,  -2.07389932e-02],
       [  2.64338576e-18,   1.45898292e-02,   4.89104213e-02,
          7.70703166e-02,   9.55225258e-02,   1.01875104e-01,
          7.73459329e-02,   2.50275508e-02,   2.93537540e-02,
          1.30949577e-02,  -2.15685454e-02],
       [  2.64330042e-18,   1.45898262e-02,   4.89104186e-02,
          7.70703146e-02,   9.55225248e-02,   1.01910945e-01,
          7.74050835e-02,   2.52307946e-02,   2.92970736e-02,
          1.30880504e-02,  -2.15685424e-02],
       [  2.64318867e-18,   1.45898259e-02,   4.89104184e-02,
          7.70703144e-02,   9.55225247e-02,   1.01933432e-01,
          7.74421258e-02,   2.53572923e-02,   2.92615130e-02,
          1.30837284e-02,  -2.15685422e-02],
       [  2.64305117e-18,   1.45898284e-02,   4.89104206e-02,
          7.70703161e-02,   9.55225256e-02,   1.01942593e-01,
          7.74571359e-02,   2.54081640e-02,   2.92472117e-02,
          1.30819985e-02,  -2.15685446e-02],
       [  2.30141673e-18,   1.40210825e-02,   4.56205547e-02,
          6.63109661e-02,   5.79266964e-02,   2.33044622e-15,
          4.69672564e-02,   2.18401553e-02,   2.72864925e-02,
          1.25728575e-02,  -2.10227772e-02],
       [  1.10672535e-18,   1.04777076e-02,   3.59041065e-02,
          4.24614318e-02,   2.24217216e-02,   3.66914762e-15,
          1.81728517e-02,   1.39301504e-02,   2.14956836e-02,
          1.08711460e-02,  -1.90802849e-02]]
    )
    np.testing.assert_allclose(fd, fd_test, rtol=2e-4)
def test_ss3():
    magnitude = 7.2
    dip = np.array([90])
    rake = 180.0
    width = np.array([15])
    fltx = np.array([0, 0])
    flty = np.array([0, 80])
    zp = np.array([0])
    epix = np.array([0])
    epiy = np.array([0.2 * flty[1]])

    # Convert to lat/lon
    proj = geo.utils.get_orthographic_projection(-122, -120, 39, 37)
    tlon, tlat = proj(fltx, flty, reverse=True)
    epilon, epilat = proj(epix, epiy, reverse=True)

    flt = fault.Fault.fromTrace(np.array([tlon[0]]), np.array([tlat[0]]),
                                np.array([tlon[1]]), np.array([tlat[1]]),
                                zp, width, dip, reference='ss3')

    event = {'lat': epilat[0],
             'lon': epilon[0],
             'depth': 10,
             'mag': magnitude,
             'id': 'ss3',
             'locstring': 'test',
             'type': 'SS',
             'timezone': 'UTC'}
    event['time'] = ShakeDateTime.utcfromtimestamp(int(time.time()))
    event['created'] = ShakeDateTime.utcfromtimestamp(int(time.time()))

    x = np.linspace(-60, 60, 21)
    y = np.linspace(-60, 138, 34)
    site_x, site_y = np.meshgrid(x, y)
    slon, slat = proj(site_x, site_y, reverse=True)
    deps = np.zeros_like(slon)
    source = Source(event, flt)
    source.setEventParam('rake', rake)

    test1 = Bayless2013(source, slat, slon, deps, T=1.0)

    # Test fd
    fd = test1.getFd()
    fd_test = np.array(
        [[0.00000000e+00, 0.00000000e+00, 2.14620746e-03,
          6.47899336e-03, 1.23119791e-02, 1.91676140e-02,
          2.64009788e-02, 3.32427846e-02, 3.88863288e-02,
          4.26104002e-02, 4.39120296e-02, 4.26104002e-02,
          3.88863288e-02, 3.32427846e-02, 2.64009788e-02,
          1.91676140e-02, 1.23119791e-02, 6.47899336e-03,
          2.14620746e-03, 0.00000000e+00, 0.00000000e+00],
         [0.00000000e+00, 8.57780996e-04, 3.99405791e-03,
          9.31948105e-03, 1.65406113e-02, 2.51316805e-02,
          3.43205435e-02, 4.31274592e-02, 5.04747209e-02,
          5.53634169e-02, 5.70796092e-02, 5.53634169e-02,
          5.04747209e-02, 4.31274592e-02, 3.43205435e-02,
          2.51316805e-02, 1.65406113e-02, 9.31948105e-03,
          3.99405791e-03, 8.57780996e-04, 0.00000000e+00],
            [-7.32594549e-04, 1.80425497e-04, 3.76908220e-03,
             1.00175179e-02, 1.86854835e-02, 2.92291145e-02,
             4.07487277e-02, 5.20057177e-02, 6.15509770e-02,
             6.79776087e-02, 7.02477931e-02, 6.79776087e-02,
             6.15509770e-02, 5.20057177e-02, 4.07487277e-02,
             2.92291145e-02, 1.86854835e-02, 1.00175179e-02,
             3.76908220e-03, 1.80425497e-04, -7.32594549e-04],
            [-3.29238561e-03, -2.60643191e-03, 1.16635260e-03,
             8.15185259e-03, 1.82290773e-02, 3.08983182e-02,
             4.51608038e-02, 5.94769126e-02, 7.18919113e-02,
             8.03888307e-02, 8.34165399e-02, 8.03888307e-02,
             7.18919113e-02, 5.94769126e-02, 4.51608038e-02,
             3.08983182e-02, 1.82290773e-02, 8.15185259e-03,
             1.16635260e-03, -2.60643191e-03, -3.29238561e-03],
            [-7.68543266e-03, -7.63179286e-03, -4.08866637e-03,
             3.27605236e-03, 1.45558215e-02, 2.94068040e-02,
             4.68176355e-02, 6.49397159e-02, 7.72066272e-02,
             8.50445368e-02, 8.77974692e-02, 8.50445368e-02,
             7.72066272e-02, 6.49397159e-02, 4.68176355e-02,
             2.94068040e-02, 1.45558215e-02, 3.27605236e-03,
             -4.08866637e-03, -7.63179286e-03, -7.68543266e-03],
            [-1.38078234e-02, -1.49011067e-02, -1.21731364e-02,
             -5.02168047e-03, 6.98177526e-03, 2.38268531e-02,
             4.30419205e-02, 6.00041964e-02, 7.44541603e-02,
             8.42939552e-02, 8.77989590e-02, 8.42939552e-02,
             7.44541603e-02, 6.00041964e-02, 4.30419205e-02,
             2.38268531e-02, 6.98177526e-03, -5.02168047e-03,
             -1.21731364e-02, -1.49011067e-02, -1.38078234e-02],
            [-2.13780396e-02, -2.42165379e-02, -2.30613142e-02,
             -1.70011475e-02, -5.15036128e-03, 1.25885635e-02,
             3.24536739e-02, 5.25619351e-02, 7.05100243e-02,
             8.31900906e-02, 8.78003567e-02, 8.31900906e-02,
             7.05100243e-02, 5.25619351e-02, 3.24536739e-02,
             1.25885635e-02, -5.15036128e-03, -1.70011475e-02,
             -2.30613142e-02, -2.42165379e-02, -2.13780396e-02],
            [-2.98882710e-02, -3.50862342e-02, -3.63793490e-02,
             -3.25716319e-02, -2.22546618e-02, -3.59274163e-03,
             1.83064517e-02, 4.20112440e-02, 6.46115966e-02,
             8.14746164e-02, 8.78016623e-02, 8.14746164e-02,
             6.46115966e-02, 4.20112440e-02, 1.83064517e-02,
             -3.59274163e-03, -2.22546618e-02, -3.25716319e-02,
             -3.63793490e-02, -3.50862342e-02, -2.98882710e-02],
            [-3.85810679e-02, -4.66488633e-02, -5.12430987e-02,
             -5.10089462e-02, -4.20856023e-02, -2.36905234e-02,
             -6.33876287e-04, 2.66765430e-02, 5.53289928e-02,
             7.86066125e-02, 8.78028757e-02, 7.86066125e-02,
             5.53289928e-02, 2.66765430e-02, -6.33876287e-04,
             -2.36905234e-02, -4.20856023e-02, -5.10089462e-02,
             -5.12430987e-02, -4.66488633e-02, -3.85810679e-02],
            [-4.64803335e-02, -5.76615888e-02, -6.61458422e-02,
             -7.06512643e-02, -6.38427394e-02, -4.77258398e-02,
             -2.55483969e-02, 4.05840724e-03, 3.98470070e-02,
             7.33053399e-02, 8.78039969e-02, 7.33053399e-02,
             3.98470070e-02, 4.05840724e-03, -2.55483969e-02,
             -4.77258398e-02, -6.38427394e-02, -7.06512643e-02,
             -6.61458422e-02, -5.76615888e-02, -4.64803335e-02],
            [-5.25038299e-02, -6.66129442e-02, -7.90147081e-02,
             -8.87629178e-02, -8.59653118e-02, -7.42828398e-02,
             -5.64316505e-02, -2.87083225e-02, 1.25945312e-02,
             6.19971667e-02, 8.78050260e-02, 6.19971667e-02,
             1.25945312e-02, -2.87083225e-02, -5.64316505e-02,
             -7.42828398e-02, -8.59653118e-02, -8.87629178e-02,
             -7.90147081e-02, -6.66129442e-02, -5.25038299e-02],
            [-5.69779111e-02, -7.36791817e-02, -8.97495345e-02,
             -1.04799583e-01, -1.07737239e-01, -1.02875880e-01,
             -9.46568471e-02, -7.95630162e-02, -4.96285112e-02,
             6.59954795e-03, 5.25569882e-02, 6.59954795e-03,
             -4.96285112e-02, -7.95630162e-02, -9.46568471e-02,
             -1.02875880e-01, -1.07737239e-01, -1.04799583e-01,
             -8.97495345e-02, -7.36791817e-02, -5.69779111e-02],
            [-5.90357675e-02, -7.69727119e-02, -9.48442826e-02,
             -1.12607620e-01, -1.18744885e-01, -1.18201834e-01,
             -1.17217017e-01, -1.15152899e-01, -1.09694433e-01,
             -8.82341332e-02, -1.61624035e-02, -8.82341332e-02,
             -1.09694433e-01, -1.15152899e-01, -1.17217017e-01,
             -1.18201834e-01, -1.18744885e-01, -1.12607620e-01,
             -9.48442826e-02, -7.69727119e-02, -5.90357675e-02],
            [-5.92189452e-02, -7.72680305e-02, -9.53051857e-02,
             -1.13322519e-01, -1.19770917e-01, -1.19670660e-01,
             -1.19486798e-01, -1.19092639e-01, -1.17989113e-01,
             -1.12555820e-01, -4.50009776e-02, -1.12555820e-01,
             -1.17989113e-01, -1.19092639e-01, -1.19486798e-01,
             -1.19670660e-01, -1.19770917e-01, -1.13322519e-01,
             -9.53051857e-02, -7.72680305e-02, -5.92189452e-02],
            [-5.79249958e-02, -7.51927112e-02, -9.20842554e-02,
             -1.08361430e-01, -1.12722790e-01, -1.09732675e-01,
             -1.04531672e-01, -9.44729544e-02, -7.23277773e-02,
             -2.05699911e-02, 3.58249631e-02, -2.05699911e-02,
             -7.23277773e-02, -9.44729544e-02, -1.04531672e-01,
             -1.09732675e-01, -1.12722790e-01, -1.08361430e-01,
             -9.20842554e-02, -7.51927112e-02, -5.79249958e-02],
            [-5.42527703e-02, -6.93641123e-02, -8.31684773e-02,
             -9.49114165e-02, -9.41989454e-02, -8.48645354e-02,
             -7.00894708e-02, -4.58286259e-02, -6.37563061e-03,
             4.68887998e-02, 7.77968419e-02, 4.68887998e-02,
             -6.37563061e-03, -4.58286259e-02, -7.00894708e-02,
             -8.48645354e-02, -9.41989454e-02, -9.49114165e-02,
             -8.31684773e-02, -6.93641123e-02, -5.42527703e-02],
            [-4.82490057e-02, -5.99997941e-02, -6.91786120e-02,
             -7.44891242e-02, -6.73705808e-02, -5.13001284e-02,
             -2.84188057e-02, 3.60143816e-03, 4.47470123e-02,
             8.58663851e-02, 1.04548354e-01, 8.58663851e-02,
             4.47470123e-02, 3.60143816e-03, -2.84188057e-02,
             -5.13001284e-02, -6.73705808e-02, -7.44891242e-02,
             -6.91786120e-02, -5.99997941e-02, -4.82490057e-02],
            [-4.03203010e-02, -4.79063206e-02, -5.16352259e-02,
             -4.98707253e-02, -3.67295509e-02, -1.57342058e-02,
             1.13668830e-02, 4.46551184e-02, 8.10450840e-02,
             1.11780747e-01, 1.24226598e-01, 1.11780747e-01,
             8.10450840e-02, 4.46551184e-02, 1.13668830e-02,
             -1.57342058e-02, -3.67295509e-02, -4.98707253e-02,
             -5.16352259e-02, -4.79063206e-02, -4.03203010e-02],
            [-3.10250239e-02, -3.40796094e-02, -3.22089254e-02,
             -2.37094100e-02, -5.85463114e-03, 1.77402761e-02,
             4.57786845e-02, 7.69637052e-02, 1.07537652e-01,
             1.30906328e-01, 1.39800436e-01, 1.30906328e-01,
             1.07537652e-01, 7.69637052e-02, 4.57786845e-02,
             1.77402761e-02, -5.85463114e-03, -2.37094100e-02,
             -3.22089254e-02, -3.40796094e-02, -3.10250239e-02],
            [-2.09301700e-02, -1.94475962e-02, -1.22970199e-02,
             2.07296407e-03, 2.31516868e-02, 4.74574033e-02,
             7.44743481e-02, 1.02380049e-01, 1.27776301e-01,
             1.46003379e-01, 1.52690015e-01, 1.46003379e-01,
             1.27776301e-01, 1.02380049e-01, 7.44743481e-02,
             4.74574033e-02, 2.31516868e-02, 2.07296407e-03,
             -1.22970199e-02, -1.94475962e-02, -2.09301700e-02],
            [-1.05257992e-02, -4.74329696e-03, 7.12107274e-03,
             2.63431361e-02, 4.93709790e-02, 7.31527220e-02,
             9.82233938e-02, 1.22728059e-01, 1.43894925e-01,
             1.58465026e-01, 1.63685984e-01, 1.58465026e-01,
             1.43894925e-01, 1.22728059e-01, 9.82233938e-02,
             7.31527220e-02, 4.93709790e-02, 2.63431361e-02,
             7.12107274e-03, -4.74329696e-03, -1.05257992e-02],
            [-1.89098657e-04, 9.52392382e-03, 2.54577716e-02,
             4.85730869e-02, 7.26048516e-02, 9.51726659e-02,
             1.17988523e-01, 1.39380421e-01, 1.57176612e-01,
             1.69076915e-01, 1.73274075e-01, 1.69076915e-01,
             1.57176612e-01, 1.39380421e-01, 1.17988523e-01,
             9.51726659e-02, 7.26048516e-02, 4.85730869e-02,
             2.54577716e-02, 9.52392382e-03, -1.89098657e-04],
            [9.81732797e-03, 2.30419581e-02, 4.24234701e-02,
             6.86213308e-02, 9.30164618e-02, 1.14050063e-01,
             1.34620894e-01, 1.53304069e-01, 1.68420867e-01,
             1.78321253e-01, 1.81774183e-01, 1.78321253e-01,
             1.68420867e-01, 1.53304069e-01, 1.34620894e-01,
             1.14050063e-01, 9.30164618e-02, 6.86213308e-02,
             4.24234701e-02, 2.30419581e-02, 9.81732797e-03],
            [1.93290725e-02, 3.56493099e-02, 5.79271157e-02,
             8.65611122e-02, 1.10914315e-01, 1.30317702e-01,
             1.48798006e-01, 1.65173224e-01, 1.78147031e-01,
             1.86513895e-01, 1.89408199e-01, 1.86513895e-01,
             1.78147031e-01, 1.65173224e-01, 1.48798006e-01,
             1.30317702e-01, 1.10914315e-01, 8.65611122e-02,
             5.79271157e-02, 3.56493099e-02, 1.93290725e-02],
            [2.68168937e-02, 4.52356810e-02, 6.92261217e-02,
             9.89630241e-02, 1.23093435e-01, 1.40640067e-01,
             1.56998943e-01, 1.71215219e-01, 1.82297185e-01,
             1.89360704e-01, 1.91789146e-01, 1.89360704e-01,
             1.82297185e-01, 1.71215219e-01, 1.56998943e-01,
             1.40640067e-01, 1.23093435e-01, 9.89630241e-02,
             6.92261217e-02, 4.52356810e-02, 2.68168937e-02],
            [3.19403269e-02, 5.15051953e-02, 7.61032066e-02,
             1.05705197e-01, 1.31722206e-01, 1.47466588e-01,
             1.61892450e-01, 1.74235616e-01, 1.83735386e-01,
             1.89735533e-01, 1.91788616e-01, 1.89735533e-01,
             1.83735386e-01, 1.74235616e-01, 1.61892450e-01,
             1.47466588e-01, 1.31722206e-01, 1.05705197e-01,
             7.61032066e-02, 5.15051953e-02, 3.19403269e-02],
            [3.48604070e-02, 5.49292382e-02, 7.94274234e-02,
             1.08149011e-01, 1.38923419e-01, 1.53070440e-01,
             1.65849067e-01, 1.76646162e-01, 1.84871647e-01,
             1.90029617e-01, 1.91787948e-01, 1.90029617e-01,
             1.84871647e-01, 1.76646162e-01, 1.65849067e-01,
             1.53070440e-01, 1.38923419e-01, 1.08149011e-01,
             7.94274234e-02, 5.49292382e-02, 3.48604070e-02],
            [3.53402022e-02, 5.53653759e-02, 7.91965502e-02,
             1.06486934e-01, 1.36563003e-01, 1.57713955e-01,
             1.69087164e-01, 1.78598269e-01, 1.85784340e-01,
             1.90264452e-01, 1.91787141e-01, 1.90264452e-01,
             1.85784340e-01, 1.78598269e-01, 1.69087164e-01,
             1.57713955e-01, 1.36563003e-01, 1.06486934e-01,
             7.91965502e-02, 5.53653759e-02, 3.53402022e-02],
            [3.32889822e-02, 5.28319225e-02, 7.55769079e-02,
             1.01077605e-01, 1.28592068e-01, 1.57023616e-01,
             1.71766715e-01, 1.80199729e-01, 1.86528091e-01,
             1.90454829e-01, 1.91786196e-01, 1.90454829e-01,
             1.86528091e-01, 1.80199729e-01, 1.71766715e-01,
             1.57023616e-01, 1.28592068e-01, 1.01077605e-01,
             7.55769079e-02, 5.28319225e-02, 3.32889822e-02],
            [2.87295370e-02, 4.74613283e-02, 6.88388861e-02,
             9.23568989e-02, 1.17254645e-01, 1.42483223e-01,
             1.66695764e-01, 1.81528776e-01, 1.87141877e-01,
             1.90611190e-01, 1.91785112e-01, 1.90611190e-01,
             1.87141877e-01, 1.81528776e-01, 1.66695764e-01,
             1.42483223e-01, 1.17254645e-01, 9.23568989e-02,
             6.88388861e-02, 4.74613283e-02, 2.87295370e-02],
            [2.17650266e-02, 3.94568191e-02, 5.93023344e-02,
             8.07720575e-02, 1.03124482e-01, 1.25394282e-01,
             1.46405870e-01, 1.64828303e-01, 1.79288925e-01,
             1.88553222e-01, 1.91747252e-01, 1.88553222e-01,
             1.79288925e-01, 1.64828303e-01, 1.46405870e-01,
             1.25394282e-01, 1.03124482e-01, 8.07720575e-02,
             5.93023344e-02, 3.94568191e-02, 2.17650266e-02],
            [1.25495284e-02, 2.90572166e-02, 4.72972116e-02,
             6.67423656e-02, 8.66951873e-02, 1.06290296e-01,
             1.24520131e-01, 1.40293247e-01, 1.52531693e-01,
             1.60303860e-01, 1.62970689e-01, 1.60303860e-01,
             1.52531693e-01, 1.40293247e-01, 1.24520131e-01,
             1.06290296e-01, 8.66951873e-02, 6.67423656e-02,
             4.72972116e-02, 2.90572166e-02, 1.25495284e-02],
            [1.26441934e-03, 1.65114811e-02, 3.31390978e-02,
             5.06407706e-02, 6.83765492e-02, 8.55839448e-02,
             1.01408074e-01, 1.14955639e-01, 1.25373662e-01,
             1.31946425e-01, 1.34193829e-01, 1.31946425e-01,
             1.25373662e-01, 1.14955639e-01, 1.01408074e-01,
             8.55839448e-02, 6.83765492e-02, 5.06407706e-02,
             3.31390978e-02, 1.65114811e-02, 1.26441934e-03],
            [0.00000000e+00, 2.06213867e-03, 1.71162845e-02,
             3.27888240e-02, 4.85026462e-02, 6.35932476e-02,
             7.73387997e-02, 8.90069217e-02, 9.79166934e-02,
             1.03509489e-01, 1.05416736e-01, 1.03509489e-01,
             9.79166934e-02, 8.90069217e-02, 7.73387997e-02,
             6.35932476e-02, 4.85026462e-02, 3.27888240e-02,
             1.71162845e-02, 2.06213867e-03, 0.00000000e+00]]
    )
    np.testing.assert_allclose(
        fd, fd_test, rtol=1e-4)
Exemple #12
0
def test_rv4():
    magnitude = 7.0
    rake = 90.0
    width = np.array([28])
    fltx = np.array([0, 0])
    flty = np.array([0, 32])
    zp = np.array([0])
    dip = np.array([30])

    # Convert to lat/lon
    proj = geo.utils.get_orthographic_projection(-122, -120, 39, 37)
    tlon, tlat = proj(fltx, flty, reverse=True)

    flt = fault.Fault.fromTrace(np.array([tlon[0]]), np.array([tlat[0]]),
                                np.array([tlon[1]]), np.array([tlat[1]]),
                                zp, width, dip, reference='')
    L = flt.getFaultLength()

    # Try to figure out epicenter
    tmp = flt.getQuadrilaterals()[0]
    pp0 = Vector.fromPoint(point.Point(tmp[0].longitude, tmp[0].latitude,
                                       tmp[0].depth))
    pp1 = Vector.fromPoint(point.Point(tmp[1].longitude, tmp[1].latitude,
                                       tmp[1].depth))
    pp2 = Vector.fromPoint(point.Point(tmp[2].longitude, tmp[2].latitude,
                                       tmp[2].depth))
    pp3 = Vector.fromPoint(point.Point(tmp[3].longitude, tmp[3].latitude,
                                       tmp[3].depth))
    dxp = 6/L
    dyp = (width-8)/width
    mp0 = pp0 + (pp1 - pp0)*dxp
    mp1 = pp3 + (pp2 - pp3)*dxp
    rp = mp0 + (mp1 - mp0)*dyp
    epilat,epilon,epidepth = ecef2latlon(rp.x, rp.y, rp.z)

    event = {'lat': epilat,
             'lon': epilon,
             'depth': epidepth,
             'mag': magnitude,
             'id': 'test',
             'locstring': 'rv4',
             'type': 'DS',
             'timezone': 'UTC'}
    event['time'] = ShakeDateTime.utcfromtimestamp(int(time.time()))
    event['created'] = ShakeDateTime.utcfromtimestamp(int(time.time()))

    x = np.linspace(-50, 50, 11)
    y = np.linspace(-50, 50, 11)
    site_x, site_y = np.meshgrid(x, y)
    slon, slat = proj(site_x, site_y, reverse=True)
    deps = np.zeros_like(slon)
    source = Source(event, flt)
    source.setEventParam('rake', rake)

    test1 = Bayless2013(source, slat, slon, deps, T=2.0)

    # Test fd
    fd = test1.getFd()
    fd_test = np.array(
      [[  0.00000000e+00,   0.00000000e+00,   0.00000000e+00,
          1.72147747e-03,   1.34981119e-03,   8.95673480e-29,
          1.24449087e-03,   1.16145147e-03,   2.25470229e-03,
          7.05301515e-04,   0.00000000e+00],
       [  0.00000000e+00,   0.00000000e+00,   7.62625126e-03,
          1.25136528e-02,   5.61909403e-03,   3.18694606e-28,
          4.52275052e-03,   4.67980272e-03,   1.02822365e-02,
          5.13171639e-03,  -6.13935060e-03],
       [  0.00000000e+00,   4.00499692e-03,   2.37611880e-02,
          2.37143264e-02,   9.55241972e-03,   5.65693221e-28,
          7.70357238e-03,   7.36477919e-03,   1.48241947e-02,
          8.40402367e-03,  -1.58594139e-02],
       [  8.08392720e-19,   9.38156493e-03,   3.38613859e-02,
          3.85355818e-02,   1.91047521e-02,   1.27066310e-27,
          1.54323543e-02,   1.21915074e-02,   2.04437211e-02,
          1.04933053e-02,  -1.85937074e-02],
       [  2.12026318e-18,   1.37316424e-02,   4.40195705e-02,
          6.16565712e-02,   4.77616016e-02,   5.07336347e-27,
          3.86325509e-02,   1.97966900e-02,   2.64883302e-02,
          1.23336661e-02,  -2.07390404e-02],
       [  2.64338576e-18,   1.45898071e-02,   4.89104103e-02,
          7.70703129e-02,   9.55225254e-02,   1.01875104e-01,
          7.73459333e-02,   2.50275520e-02,   2.93537605e-02,
          1.30949772e-02,  -2.15685118e-02],
       [  2.64330042e-18,   1.45898071e-02,   4.89104103e-02,
          7.70703129e-02,   9.55225254e-02,   1.01910945e-01,
          7.74050830e-02,   2.52307951e-02,   2.92970785e-02,
          1.30880672e-02,  -2.15685118e-02],
       [  2.64318867e-18,   1.45898071e-02,   4.89104103e-02,
          7.70703129e-02,   9.55225254e-02,   1.01933432e-01,
          7.74421253e-02,   2.53572928e-02,   2.92615177e-02,
          1.30837449e-02,  -2.15685118e-02],
       [  2.64305117e-18,   1.45898071e-02,   4.89104103e-02,
          7.70703129e-02,   9.55225254e-02,   1.01942593e-01,
          7.74571361e-02,   2.54081650e-02,   2.92472178e-02,
          1.30820173e-02,  -2.15685118e-02],
       [  2.30140686e-18,   1.40209885e-02,   4.56202616e-02,
          6.63103459e-02,   5.79255225e-02,   7.72925496e-27,
          4.69663059e-02,   2.18399567e-02,   2.72863359e-02,
          1.25728195e-02,  -2.10226512e-02],
       [  1.10671369e-18,   1.04775558e-02,   3.59035524e-02,
          4.24605614e-02,   2.24210618e-02,   1.53459722e-27,
          1.81723013e-02,   1.39298662e-02,   2.14953705e-02,
          1.08710398e-02,  -1.90800441e-02]]
    )
    np.testing.assert_allclose(fd, fd_test, rtol=1e-5)
def test_so6():
    event_name = 'so6'
    magnitude = 7.2
    dip = np.array([70])
    rake = 135
    width = np.array([15])
    L = 80
    fltx = np.array([0, 0])
    flty = np.array([0, L])
    zp = np.array([0])
    # Convert to lat/lon
    proj = geo.utils.get_orthographic_projection(-122, -120, 39, 37)
    tlon, tlat = proj(fltx, flty, reverse=True)
    flt = fault.Fault.fromTrace(np.array([tlon[0]]),
                                np.array([tlat[0]]),
                                np.array([tlon[1]]),
                                np.array([tlat[1]]),
                                zp,
                                width,
                                dip,
                                reference='rv4')
    x = np.linspace(-80, 80, 21)
    y = np.linspace(-50, 130, 21)
    site_x, site_y = np.meshgrid(x, y)
    slon, slat = proj(site_x, site_y, reverse=True)
    sdepth = np.zeros_like(slon)
    tmp = flt.getQuadrilaterals()[0]
    pp0 = Vector.fromPoint(
        point.Point(tmp[0].longitude, tmp[0].latitude, tmp[0].depth))
    pp1 = Vector.fromPoint(
        point.Point(tmp[1].longitude, tmp[1].latitude, tmp[1].depth))
    pp2 = Vector.fromPoint(
        point.Point(tmp[2].longitude, tmp[2].latitude, tmp[2].depth))
    pp3 = Vector.fromPoint(
        point.Point(tmp[3].longitude, tmp[3].latitude, tmp[3].depth))
    dxp = 10 / L
    dyp = (width - 5) / width
    mp0 = pp0 + (pp1 - pp0) * dxp
    mp1 = pp3 + (pp2 - pp3) * dxp
    rp = mp0 + (mp1 - mp0) * dyp
    epilat, epilon, epidepth = ecef2latlon(rp.x, rp.y, rp.z)
    epix, epiy = proj(epilon, epilat, reverse=False)
    event = {
        'lat': epilat,
        'lon': epilon,
        'depth': epidepth,
        'mag': magnitude,
        'id': 'so6',
        'locstring': 'so6',
        'type': 'RV',
        'timezone': 'UTC'
    }
    event['time'] = ShakeDateTime.utcfromtimestamp(int(time.time()))
    event['created'] = ShakeDateTime.utcfromtimestamp(int(time.time()))
    fltlat = [a.latitude for a in flt.getQuadrilaterals()[0]]
    fltlon = [a.longitude for a in flt.getQuadrilaterals()[0]]
    fltlat = np.append(fltlat, fltlat[0])
    fltlon = np.append(fltlon, fltlon[0])
    fltx, flty = proj(fltlon, fltlat, reverse=False)
    source = Source(event, flt)
    source.setEventParam('rake', rake)
    test1 = Bayless2013(source, slat, slon, sdepth, T=5)
    fd = test1.getFd()
    fd_test = np.array([
        [
            0.00000000e+00, 0.00000000e+00, 0.00000000e+00, -8.92879772e-03,
            -1.74526918e-02, -2.22981746e-02, -2.34350450e-02, -2.13620062e-02,
            -1.72712346e-02, -1.29509613e-02, -1.02545064e-02, -1.03010185e-02,
            -1.28847597e-02, -1.66274727e-02, -1.96984070e-02, -2.05377743e-02,
            -1.81831337e-02, -1.21881814e-02, -2.64862879e-03, 0.00000000e+00,
            0.00000000e+00
        ],
        [
            0.00000000e+00, 0.00000000e+00, -8.73221519e-03, -2.21421374e-02,
            -3.18438939e-02, -3.71488270e-02, -3.76239913e-02, -3.35015951e-02,
            -2.61748968e-02, -1.83864728e-02, -1.34793002e-02, -1.36687799e-02,
            -1.85727143e-02, -2.55527671e-02, -3.14227568e-02, -3.38933995e-02,
            -3.19289607e-02, -2.53396980e-02, -1.45943649e-02, -3.71405488e-04,
            0.00000000e+00
        ],
        [
            0.00000000e+00, -2.54621422e-03, -2.11428566e-02, -3.68609103e-02,
            -4.87464747e-02, -5.56539037e-02, -5.64419387e-02, -5.05331157e-02,
            -3.52919381e-02, -2.18782050e-02, -1.40858125e-02, -1.47354546e-02,
            -2.35727189e-02, -3.74838465e-02, -4.75915414e-02, -5.13000399e-02,
            -4.87882409e-02, -4.05716321e-02, -2.77368254e-02, -1.13542729e-02,
            0.00000000e+00
        ],
        [
            0.00000000e+00, -1.21642958e-02, -3.33747360e-02, -5.21661817e-02,
            -6.74724509e-02, -7.77628842e-02, -8.00243748e-02, -6.42496853e-02,
            -4.38124530e-02, -1.97027426e-02, -1.45897731e-02, -1.07427056e-02,
            -3.08235222e-02, -4.82656988e-02, -6.67692677e-02, -7.35152908e-02,
            -6.85574283e-02, -5.71811573e-02, -4.12138780e-02, -2.20396726e-02,
            -6.24121310e-04
        ],
        [
            0.00000000e+00, -2.00643401e-02, -4.39827328e-02, -6.62722434e-02,
            -8.60268414e-02, -1.01730306e-01, -9.86277741e-02, -9.82914922e-02,
            -5.22335876e-02, -1.54622435e-02, -1.57487554e-02, -3.06190808e-03,
            -4.81481586e-02, -8.92480491e-02, -8.63776477e-02, -9.98130440e-02,
            -8.95491230e-02, -7.33553695e-02, -5.34401725e-02, -3.11601812e-02,
            -7.33715103e-03
        ],
        [
            0.00000000e+00, -2.50053614e-02, -5.11695772e-02, -7.65997026e-02,
            -1.00809054e-01, -1.22877573e-01, -1.18738178e-01, -1.55236782e-01,
            -7.45388001e-02, 1.92779182e-03, -1.94380016e-02, 1.94922939e-02,
            -7.66669920e-02, -1.53909722e-01, -1.10846875e-01, -1.19746768e-01,
            -1.07680300e-01, -8.59905101e-02, -6.22042294e-02, -3.71802472e-02,
            -1.13867485e-02
        ],
        [
            0.00000000e+00, -2.63645827e-02, -5.37984901e-02, -8.11337022e-02,
            -1.08298371e-01, -1.35146441e-01, -1.34825430e-01, -1.85836050e-01,
            -1.10730875e-01, -3.18861095e-02, 4.14395701e-02, -1.52711946e-02,
            -1.31840763e-01, -1.96794707e-01, -1.33453212e-01, -1.34989129e-01,
            -1.17922385e-01, -9.21637323e-02, -6.58369237e-02, -3.91646838e-02,
            -1.22685698e-02
        ],
        [
            0.00000000e+00, -2.64622244e-02, -5.40483999e-02, -8.16190336e-02,
            -1.09162854e-01, -1.36656677e-01, -1.37081504e-01, -1.89522811e-01,
            -1.17723634e-01, -4.88765748e-02, -5.04529015e-03, -5.76414497e-02,
            -1.45712183e-01, -2.03062804e-01, -1.36859828e-01, -1.37107390e-01,
            -1.19124650e-01, -9.28263279e-02, -6.61800709e-02, -3.93088682e-02,
            -1.22842049e-02
        ],
        [
            0.00000000e+00, -2.58466495e-02, -5.24858827e-02, -7.86086164e-02,
            -1.03856343e-01, -1.27529509e-01, -1.23794779e-01, -1.68810613e-01,
            -8.22602627e-02, 1.74236964e-02, 9.38708725e-02, 4.23208284e-02,
            -8.46343723e-02, -1.70476759e-01, -1.17547884e-01, -1.24569752e-01,
            -1.11518670e-01, -8.84736806e-02, -6.38037151e-02, -3.81874381e-02,
            -1.19867610e-02
        ],
        [
            0.00000000e+00, -2.42186547e-02, -4.84175525e-02, -7.09428614e-02,
            -9.07754575e-02, -1.06117824e-01, -9.50228292e-02, -1.29781980e-01,
            -3.08573454e-02, 7.39058739e-02, 1.30478117e-01, 8.28181149e-02,
            -2.70389535e-02, -1.20837502e-01, -8.02081725e-02, -9.70274506e-02,
            -9.35853383e-02, -7.77422806e-02, -5.77817530e-02, -3.53067886e-02,
            -1.12414659e-02
        ],
        [
            0.00000000e+00, -2.16818717e-02, -4.22363856e-02, -5.96909893e-02,
            -7.24805224e-02, -7.81867829e-02, -6.11838569e-02, -9.05679744e-02,
            9.95934969e-03, 1.07503875e-01, 1.52073917e-01, 1.05894634e-01,
            8.68652263e-03, -7.98571818e-02, -4.16548658e-02, -6.40511838e-02,
            -6.99337160e-02, -6.26305633e-02, -4.89098800e-02, -3.09284566e-02,
            -1.00919381e-02
        ],
        [
            0.00000000e+00, -1.84940182e-02, -3.47054606e-02, -4.65278129e-02,
            -5.22037664e-02, -4.93977115e-02, -2.95395230e-02, -5.82421092e-02,
            3.91025654e-02, 1.29337956e-01, 1.67436703e-01, 1.21969296e-01,
            3.20823547e-02, -5.00287386e-02, -9.22993907e-03, -3.27186625e-02,
            -4.52706958e-02, -4.57409325e-02, -3.84701291e-02, -2.55751405e-02,
            -8.64950254e-03
        ],
        [
            0.00000000e+00, -1.49431380e-02, -2.65887341e-02, -3.29162158e-02,
            -3.22994323e-02, -2.29081781e-02, -2.60259636e-03, -3.29856530e-02,
            6.02631314e-02, 1.45003704e-01, 1.79361264e-01, 1.34292814e-01,
            4.88007115e-02, -2.82328554e-02, 1.64212421e-02, -5.72391847e-03,
            -2.23438861e-02, -2.90246794e-02, -2.76054402e-02, -1.97779758e-02,
            -7.03945406e-03
        ],
        [
            0.00000000e+00, -1.12771143e-02, -1.84737590e-02, -1.98228664e-02,
            -1.40092305e-02, 1.84580818e-04, 1.95817303e-02, -1.32608487e-02,
            7.62783168e-02, 1.57076433e-01, 1.89083905e-01, 1.44259188e-01,
            6.15722813e-02, -1.17505212e-02, 3.65938109e-02, 1.66937711e-02,
            -2.18970818e-03, -1.35507683e-02, -1.70890527e-02, -1.39519424e-02,
            -5.37036892e-03
        ],
        [
            0.00000000e+00, -7.67615215e-03, -1.07348257e-02, -7.75276739e-03,
            2.22351695e-03, 1.98662250e-02, 3.77611177e-02, 2.42018661e-03,
            8.89036172e-02, 1.66855206e-01, 1.97260700e-01, 1.52590263e-01,
            7.17981256e-02, 1.18005972e-03, 5.26852303e-02, 3.51638855e-02,
            1.51012176e-02, 2.69654076e-04, -7.33815554e-03, -8.36639665e-03,
            -3.72176313e-03
        ],
        [
            0.00000000e+00, -4.50552324e-03, -4.32262850e-03, 1.73559158e-03,
            1.42670366e-02, 3.35040699e-02, 4.97279358e-02, 1.85410528e-02,
            9.39950666e-02, 1.46646579e-01, 9.13474746e-02, 1.37004651e-01,
            7.74648339e-02, 1.59777072e-02, 6.25334939e-02, 4.74577418e-02,
            2.72155518e-02, 1.06174952e-02, 3.94103899e-04, -3.68465400e-03,
            -2.19830733e-03
        ],
        [
            0.00000000e+00, -1.74629916e-03, 5.44471813e-04, 8.22933499e-03,
            2.15699287e-02, 4.04232250e-02, 5.69678048e-02, 5.52408259e-02,
            9.04381272e-02, 1.08204635e-01, 9.14439984e-02, 1.06884511e-01,
            8.17241884e-02, 5.55282924e-02, 6.78528399e-02, 5.47188925e-02,
            3.35251483e-02, 1.69615982e-02, 5.72048628e-03, -8.81437278e-05,
            -7.36518436e-04
        ],
        [
            0.00000000e+00, 4.07838765e-05, 3.63933766e-03, 1.20080876e-02,
            2.51274691e-02, 4.25687176e-02, 6.25685606e-02, 7.33480475e-02,
            8.37515545e-02, 9.52500287e-02, 9.15135660e-02, 9.66442834e-02,
            8.66659913e-02, 8.10325633e-02, 7.18836713e-02, 5.45548434e-02,
            3.55884875e-02, 2.00142359e-02, 8.71200201e-03, 2.04407846e-03,
            -6.53680674e-06
        ],
        [
            0.00000000e+00, 2.40054729e-04, 4.44975227e-03, 1.27572519e-02,
            2.49362989e-02, 4.03831326e-02, 5.80039988e-02, 7.61280192e-02,
            8.37404162e-02, 8.89634569e-02, 9.15651607e-02, 9.13586235e-02,
            8.83589144e-02, 8.27804032e-02, 6.75666471e-02, 5.00483249e-02,
            3.36733366e-02, 1.96758691e-02, 9.00603204e-03, 2.18370401e-03,
            0.00000000e+00
        ],
        [
            0.00000000e+00, 0.00000000e+00, 2.78776980e-03, 1.05086036e-02,
            2.13238822e-02, 3.45577738e-02, 4.91570145e-02, 6.36787133e-02,
            7.63710088e-02, 8.54072310e-02, 8.92960200e-02, 8.75702197e-02,
            8.07095447e-02, 6.97999389e-02, 5.63787286e-02, 4.20734776e-02,
            2.83073312e-02, 1.61614525e-02, 6.56194125e-03, 1.00721924e-04,
            0.00000000e+00
        ],
        [
            0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 5.49667845e-03,
            1.47563319e-02, 2.57955743e-02, 3.76689418e-02, 4.91861917e-02,
            5.90108907e-02, 6.58478416e-02, 6.87018515e-02, 6.73174642e-02,
            6.20270643e-02, 5.35456385e-02, 4.29400416e-02, 3.14129728e-02,
            2.00795162e-02, 9.84001885e-03, 1.53992995e-03, 0.00000000e+00,
            0.00000000e+00
        ]
    ])
    np.testing.assert_allclose(fd, fd_test, rtol=1e-4)
def test_chichi():
    print('Testing Chi-Chi...')
    # read in fault file
    f = '../data/0137A.POL'
    i0 = np.arange(0, 9 * 11 * 3, 11)
    i1 = i0 + 10
    cs = zip(i0, i1)
    df = pd.read_fwf(f, cs, skiprows=2, nrows=5, header=None)
    mat = df.as_matrix()
    ix = np.arange(0, 9 * 3, 3)
    iy = ix + 1
    iz = ix + 2
    x0 = mat[0, ix]
    x1 = mat[1, ix]
    x2 = mat[2, ix]
    x3 = mat[3, ix]
    y0 = mat[0, iy]
    y1 = mat[1, iy]
    y2 = mat[2, iy]
    y3 = mat[3, iy]
    # Depth, positive down
    z0 = np.abs(mat[0, iz])
    z1 = np.abs(mat[1, iz])
    z2 = np.abs(mat[2, iz])
    z3 = np.abs(mat[3, iz])
    epilat = 23.85
    epilon = 120.82
    proj = get_orthographic_projection(epilon - 1, epilon + 1, epilat + 1,
                                       epilat - 1)
    lon0, lat0 = proj(x0, y0, reverse=True)
    lon1, lat1 = proj(x1, y1, reverse=True)
    lon2, lat2 = proj(x2, y2, reverse=True)
    lon3, lat3 = proj(x3, y3, reverse=True)
    flt = Fault.fromVertices(lon0, lat0, z0, lon1, lat1, z1, lon2, lat2, z2,
                             lon3, lat3, z3)
    ask14 = AbrahamsonEtAl2014()
    # event information doesn't matter...
    event = {
        'lat': 0,
        'lon': 0,
        'depth': 0,
        'mag': 7,
        'id': '',
        'locstring': '',
        'type': 'U',
        'time': ShakeDateTime.utcfromtimestamp(int(time.time())),
        'timezone': 'UTC'
    }
    source = Source(event, flt)

    # Get NGA distances
    distfile = '../data/NGAW2_distances.csv'
    df = pd.read_csv(distfile)
    df2 = df.loc[df['EQID'] == 137]
    slat = df2['Station Latitude'].as_matrix()
    slon = df2['Station Longitude'].as_matrix()
    sdep = np.zeros(slat.shape)
    nga_repi = df2['EpiD (km)'].as_matrix()
    nga_rhypo = df2['HypD (km)'].as_matrix()
    nga_rrup = df2['ClstD (km)'].as_matrix()
    nga_rjb = df2['Joyner-Boore Dist. (km)'].as_matrix()
    nga_rx = df2['T'].as_matrix()

    dist = Distance(ask14, source, slat, slon, sdep)
    dctx = dist.getDistanceContext()
    fig = plt.figure(figsize=(8, 8))
    plt.scatter(nga_rjb, dctx.rjb, alpha=0.5, facecolors='none')
    plt.plot([0, nga_rjb.max()], [0, dctx.rjb.max()], 'b')
    plt.savefig('Chi-Chi_Rjb.png')
    fig = plt.figure(figsize=(8, 8))
    plt.scatter(nga_rrup, dctx.rrup, alpha=0.5, facecolors='none')
    plt.plot([0, nga_rrup.max()], [0, dctx.rrup.max()], 'b')
    plt.savefig('Chi-Chi_Rrup.png')
    fig = plt.figure(figsize=(8, 8))
    plt.scatter(nga_rx, dctx.rx, alpha=0.5, facecolors='none')
    plt.plot([nga_rx.min(), nga_rx.max()],
             [dctx.rx.min(), dctx.rx.max()], 'b')
    plt.savefig('Chi-Chi_Rx.png')
def test_ss3():
    magnitude = 7.2
    dip = np.array([90])
    rake = 180.0
    width = np.array([15])
    fltx = np.array([0, 0])
    flty = np.array([0, 80])
    zp = np.array([0])
    epix = np.array([0])
    epiy = np.array([0.2 * flty[1]])

    # Convert to lat/lon
    proj = geo.utils.get_orthographic_projection(-122, -120, 39, 37)
    tlon, tlat = proj(fltx, flty, reverse=True)
    epilon, epilat = proj(epix, epiy, reverse=True)

    flt = fault.Fault.fromTrace(np.array([tlon[0]]),
                                np.array([tlat[0]]),
                                np.array([tlon[1]]),
                                np.array([tlat[1]]),
                                zp,
                                width,
                                dip,
                                reference='ss3')

    event = {
        'lat': epilat[0],
        'lon': epilon[0],
        'depth': 10,
        'mag': magnitude,
        'id': 'ss3',
        'locstring': 'test',
        'type': 'SS',
        'timezone': 'UTC'
    }
    event['time'] = ShakeDateTime.utcfromtimestamp(int(time.time()))
    event['created'] = ShakeDateTime.utcfromtimestamp(int(time.time()))

    x = np.linspace(-60, 60, 21)
    y = np.linspace(-60, 138, 34)
    site_x, site_y = np.meshgrid(x, y)
    slon, slat = proj(site_x, site_y, reverse=True)
    deps = np.zeros_like(slon)
    source = Source(event, flt)
    source.setEventParam('rake', rake)

    test1 = Bayless2013(source, slat, slon, deps, T=1.0)

    # Test fd
    fd = test1.getFd()
    fd_test = np.array([
        [
            0.00000000e+00, 0.00000000e+00, 2.14620746e-03, 6.47899336e-03,
            1.23119791e-02, 1.91676140e-02, 2.64009788e-02, 3.32427846e-02,
            3.88863288e-02, 4.26104002e-02, 4.39120296e-02, 4.26104002e-02,
            3.88863288e-02, 3.32427846e-02, 2.64009788e-02, 1.91676140e-02,
            1.23119791e-02, 6.47899336e-03, 2.14620746e-03, 0.00000000e+00,
            0.00000000e+00
        ],
        [
            0.00000000e+00, 8.57780996e-04, 3.99405791e-03, 9.31948105e-03,
            1.65406113e-02, 2.51316805e-02, 3.43205435e-02, 4.31274592e-02,
            5.04747209e-02, 5.53634169e-02, 5.70796092e-02, 5.53634169e-02,
            5.04747209e-02, 4.31274592e-02, 3.43205435e-02, 2.51316805e-02,
            1.65406113e-02, 9.31948105e-03, 3.99405791e-03, 8.57780996e-04,
            0.00000000e+00
        ],
        [
            -7.32594549e-04, 1.80425497e-04, 3.76908220e-03, 1.00175179e-02,
            1.86854835e-02, 2.92291145e-02, 4.07487277e-02, 5.20057177e-02,
            6.15509770e-02, 6.79776087e-02, 7.02477931e-02, 6.79776087e-02,
            6.15509770e-02, 5.20057177e-02, 4.07487277e-02, 2.92291145e-02,
            1.86854835e-02, 1.00175179e-02, 3.76908220e-03, 1.80425497e-04,
            -7.32594549e-04
        ],
        [
            -3.29238561e-03, -2.60643191e-03, 1.16635260e-03, 8.15185259e-03,
            1.82290773e-02, 3.08983182e-02, 4.51608038e-02, 5.94769126e-02,
            7.18919113e-02, 8.03888307e-02, 8.34165399e-02, 8.03888307e-02,
            7.18919113e-02, 5.94769126e-02, 4.51608038e-02, 3.08983182e-02,
            1.82290773e-02, 8.15185259e-03, 1.16635260e-03, -2.60643191e-03,
            -3.29238561e-03
        ],
        [
            -7.68543266e-03, -7.63179286e-03, -4.08866637e-03, 3.27605236e-03,
            1.45558215e-02, 2.94068040e-02, 4.68176355e-02, 6.49397159e-02,
            7.72066272e-02, 8.50445368e-02, 8.77974692e-02, 8.50445368e-02,
            7.72066272e-02, 6.49397159e-02, 4.68176355e-02, 2.94068040e-02,
            1.45558215e-02, 3.27605236e-03, -4.08866637e-03, -7.63179286e-03,
            -7.68543266e-03
        ],
        [
            -1.38078234e-02, -1.49011067e-02, -1.21731364e-02, -5.02168047e-03,
            6.98177526e-03, 2.38268531e-02, 4.30419205e-02, 6.00041964e-02,
            7.44541603e-02, 8.42939552e-02, 8.77989590e-02, 8.42939552e-02,
            7.44541603e-02, 6.00041964e-02, 4.30419205e-02, 2.38268531e-02,
            6.98177526e-03, -5.02168047e-03, -1.21731364e-02, -1.49011067e-02,
            -1.38078234e-02
        ],
        [
            -2.13780396e-02, -2.42165379e-02, -2.30613142e-02, -1.70011475e-02,
            -5.15036128e-03, 1.25885635e-02, 3.24536739e-02, 5.25619351e-02,
            7.05100243e-02, 8.31900906e-02, 8.78003567e-02, 8.31900906e-02,
            7.05100243e-02, 5.25619351e-02, 3.24536739e-02, 1.25885635e-02,
            -5.15036128e-03, -1.70011475e-02, -2.30613142e-02, -2.42165379e-02,
            -2.13780396e-02
        ],
        [
            -2.98882710e-02, -3.50862342e-02, -3.63793490e-02, -3.25716319e-02,
            -2.22546618e-02, -3.59274163e-03, 1.83064517e-02, 4.20112440e-02,
            6.46115966e-02, 8.14746164e-02, 8.78016623e-02, 8.14746164e-02,
            6.46115966e-02, 4.20112440e-02, 1.83064517e-02, -3.59274163e-03,
            -2.22546618e-02, -3.25716319e-02, -3.63793490e-02, -3.50862342e-02,
            -2.98882710e-02
        ],
        [
            -3.85810679e-02, -4.66488633e-02, -5.12430987e-02, -5.10089462e-02,
            -4.20856023e-02, -2.36905234e-02, -6.33876287e-04, 2.66765430e-02,
            5.53289928e-02, 7.86066125e-02, 8.78028757e-02, 7.86066125e-02,
            5.53289928e-02, 2.66765430e-02, -6.33876287e-04, -2.36905234e-02,
            -4.20856023e-02, -5.10089462e-02, -5.12430987e-02, -4.66488633e-02,
            -3.85810679e-02
        ],
        [
            -4.64803335e-02, -5.76615888e-02, -6.61458422e-02, -7.06512643e-02,
            -6.38427394e-02, -4.77258398e-02, -2.55483969e-02, 4.05840724e-03,
            3.98470070e-02, 7.33053399e-02, 8.78039969e-02, 7.33053399e-02,
            3.98470070e-02, 4.05840724e-03, -2.55483969e-02, -4.77258398e-02,
            -6.38427394e-02, -7.06512643e-02, -6.61458422e-02, -5.76615888e-02,
            -4.64803335e-02
        ],
        [
            -5.25038299e-02, -6.66129442e-02, -7.90147081e-02, -8.87629178e-02,
            -8.59653118e-02, -7.42828398e-02, -5.64316505e-02, -2.87083225e-02,
            1.25945312e-02, 6.19971667e-02, 8.78050260e-02, 6.19971667e-02,
            1.25945312e-02, -2.87083225e-02, -5.64316505e-02, -7.42828398e-02,
            -8.59653118e-02, -8.87629178e-02, -7.90147081e-02, -6.66129442e-02,
            -5.25038299e-02
        ],
        [
            -5.69779111e-02, -7.36791817e-02, -8.97495345e-02, -1.04799583e-01,
            -1.07737239e-01, -1.02875880e-01, -9.46568471e-02, -7.95630162e-02,
            -4.96285112e-02, 6.59954795e-03, 5.25569882e-02, 6.59954795e-03,
            -4.96285112e-02, -7.95630162e-02, -9.46568471e-02, -1.02875880e-01,
            -1.07737239e-01, -1.04799583e-01, -8.97495345e-02, -7.36791817e-02,
            -5.69779111e-02
        ],
        [
            -5.90357675e-02, -7.69727119e-02, -9.48442826e-02, -1.12607620e-01,
            -1.18744885e-01, -1.18201834e-01, -1.17217017e-01, -1.15152899e-01,
            -1.09694433e-01, -8.82341332e-02, -1.61624035e-02, -8.82341332e-02,
            -1.09694433e-01, -1.15152899e-01, -1.17217017e-01, -1.18201834e-01,
            -1.18744885e-01, -1.12607620e-01, -9.48442826e-02, -7.69727119e-02,
            -5.90357675e-02
        ],
        [
            -5.92189452e-02, -7.72680305e-02, -9.53051857e-02, -1.13322519e-01,
            -1.19770917e-01, -1.19670660e-01, -1.19486798e-01, -1.19092639e-01,
            -1.17989113e-01, -1.12555820e-01, -4.50009776e-02, -1.12555820e-01,
            -1.17989113e-01, -1.19092639e-01, -1.19486798e-01, -1.19670660e-01,
            -1.19770917e-01, -1.13322519e-01, -9.53051857e-02, -7.72680305e-02,
            -5.92189452e-02
        ],
        [
            -5.79249958e-02, -7.51927112e-02, -9.20842554e-02, -1.08361430e-01,
            -1.12722790e-01, -1.09732675e-01, -1.04531672e-01, -9.44729544e-02,
            -7.23277773e-02, -2.05699911e-02, 3.58249631e-02, -2.05699911e-02,
            -7.23277773e-02, -9.44729544e-02, -1.04531672e-01, -1.09732675e-01,
            -1.12722790e-01, -1.08361430e-01, -9.20842554e-02, -7.51927112e-02,
            -5.79249958e-02
        ],
        [
            -5.42527703e-02, -6.93641123e-02, -8.31684773e-02, -9.49114165e-02,
            -9.41989454e-02, -8.48645354e-02, -7.00894708e-02, -4.58286259e-02,
            -6.37563061e-03, 4.68887998e-02, 7.77968419e-02, 4.68887998e-02,
            -6.37563061e-03, -4.58286259e-02, -7.00894708e-02, -8.48645354e-02,
            -9.41989454e-02, -9.49114165e-02, -8.31684773e-02, -6.93641123e-02,
            -5.42527703e-02
        ],
        [
            -4.82490057e-02, -5.99997941e-02, -6.91786120e-02, -7.44891242e-02,
            -6.73705808e-02, -5.13001284e-02, -2.84188057e-02, 3.60143816e-03,
            4.47470123e-02, 8.58663851e-02, 1.04548354e-01, 8.58663851e-02,
            4.47470123e-02, 3.60143816e-03, -2.84188057e-02, -5.13001284e-02,
            -6.73705808e-02, -7.44891242e-02, -6.91786120e-02, -5.99997941e-02,
            -4.82490057e-02
        ],
        [
            -4.03203010e-02, -4.79063206e-02, -5.16352259e-02, -4.98707253e-02,
            -3.67295509e-02, -1.57342058e-02, 1.13668830e-02, 4.46551184e-02,
            8.10450840e-02, 1.11780747e-01, 1.24226598e-01, 1.11780747e-01,
            8.10450840e-02, 4.46551184e-02, 1.13668830e-02, -1.57342058e-02,
            -3.67295509e-02, -4.98707253e-02, -5.16352259e-02, -4.79063206e-02,
            -4.03203010e-02
        ],
        [
            -3.10250239e-02, -3.40796094e-02, -3.22089254e-02, -2.37094100e-02,
            -5.85463114e-03, 1.77402761e-02, 4.57786845e-02, 7.69637052e-02,
            1.07537652e-01, 1.30906328e-01, 1.39800436e-01, 1.30906328e-01,
            1.07537652e-01, 7.69637052e-02, 4.57786845e-02, 1.77402761e-02,
            -5.85463114e-03, -2.37094100e-02, -3.22089254e-02, -3.40796094e-02,
            -3.10250239e-02
        ],
        [
            -2.09301700e-02, -1.94475962e-02, -1.22970199e-02, 2.07296407e-03,
            2.31516868e-02, 4.74574033e-02, 7.44743481e-02, 1.02380049e-01,
            1.27776301e-01, 1.46003379e-01, 1.52690015e-01, 1.46003379e-01,
            1.27776301e-01, 1.02380049e-01, 7.44743481e-02, 4.74574033e-02,
            2.31516868e-02, 2.07296407e-03, -1.22970199e-02, -1.94475962e-02,
            -2.09301700e-02
        ],
        [
            -1.05257992e-02, -4.74329696e-03, 7.12107274e-03, 2.63431361e-02,
            4.93709790e-02, 7.31527220e-02, 9.82233938e-02, 1.22728059e-01,
            1.43894925e-01, 1.58465026e-01, 1.63685984e-01, 1.58465026e-01,
            1.43894925e-01, 1.22728059e-01, 9.82233938e-02, 7.31527220e-02,
            4.93709790e-02, 2.63431361e-02, 7.12107274e-03, -4.74329696e-03,
            -1.05257992e-02
        ],
        [
            -1.89098657e-04, 9.52392382e-03, 2.54577716e-02, 4.85730869e-02,
            7.26048516e-02, 9.51726659e-02, 1.17988523e-01, 1.39380421e-01,
            1.57176612e-01, 1.69076915e-01, 1.73274075e-01, 1.69076915e-01,
            1.57176612e-01, 1.39380421e-01, 1.17988523e-01, 9.51726659e-02,
            7.26048516e-02, 4.85730869e-02, 2.54577716e-02, 9.52392382e-03,
            -1.89098657e-04
        ],
        [
            9.81732797e-03, 2.30419581e-02, 4.24234701e-02, 6.86213308e-02,
            9.30164618e-02, 1.14050063e-01, 1.34620894e-01, 1.53304069e-01,
            1.68420867e-01, 1.78321253e-01, 1.81774183e-01, 1.78321253e-01,
            1.68420867e-01, 1.53304069e-01, 1.34620894e-01, 1.14050063e-01,
            9.30164618e-02, 6.86213308e-02, 4.24234701e-02, 2.30419581e-02,
            9.81732797e-03
        ],
        [
            1.93290725e-02, 3.56493099e-02, 5.79271157e-02, 8.65611122e-02,
            1.10914315e-01, 1.30317702e-01, 1.48798006e-01, 1.65173224e-01,
            1.78147031e-01, 1.86513895e-01, 1.89408199e-01, 1.86513895e-01,
            1.78147031e-01, 1.65173224e-01, 1.48798006e-01, 1.30317702e-01,
            1.10914315e-01, 8.65611122e-02, 5.79271157e-02, 3.56493099e-02,
            1.93290725e-02
        ],
        [
            2.68168937e-02, 4.52356810e-02, 6.92261217e-02, 9.89630241e-02,
            1.23093435e-01, 1.40640067e-01, 1.56998943e-01, 1.71215219e-01,
            1.82297185e-01, 1.89360704e-01, 1.91789146e-01, 1.89360704e-01,
            1.82297185e-01, 1.71215219e-01, 1.56998943e-01, 1.40640067e-01,
            1.23093435e-01, 9.89630241e-02, 6.92261217e-02, 4.52356810e-02,
            2.68168937e-02
        ],
        [
            3.19403269e-02, 5.15051953e-02, 7.61032066e-02, 1.05705197e-01,
            1.31722206e-01, 1.47466588e-01, 1.61892450e-01, 1.74235616e-01,
            1.83735386e-01, 1.89735533e-01, 1.91788616e-01, 1.89735533e-01,
            1.83735386e-01, 1.74235616e-01, 1.61892450e-01, 1.47466588e-01,
            1.31722206e-01, 1.05705197e-01, 7.61032066e-02, 5.15051953e-02,
            3.19403269e-02
        ],
        [
            3.48604070e-02, 5.49292382e-02, 7.94274234e-02, 1.08149011e-01,
            1.38923419e-01, 1.53070440e-01, 1.65849067e-01, 1.76646162e-01,
            1.84871647e-01, 1.90029617e-01, 1.91787948e-01, 1.90029617e-01,
            1.84871647e-01, 1.76646162e-01, 1.65849067e-01, 1.53070440e-01,
            1.38923419e-01, 1.08149011e-01, 7.94274234e-02, 5.49292382e-02,
            3.48604070e-02
        ],
        [
            3.53402022e-02, 5.53653759e-02, 7.91965502e-02, 1.06486934e-01,
            1.36563003e-01, 1.57713955e-01, 1.69087164e-01, 1.78598269e-01,
            1.85784340e-01, 1.90264452e-01, 1.91787141e-01, 1.90264452e-01,
            1.85784340e-01, 1.78598269e-01, 1.69087164e-01, 1.57713955e-01,
            1.36563003e-01, 1.06486934e-01, 7.91965502e-02, 5.53653759e-02,
            3.53402022e-02
        ],
        [
            3.32889822e-02, 5.28319225e-02, 7.55769079e-02, 1.01077605e-01,
            1.28592068e-01, 1.57023616e-01, 1.71766715e-01, 1.80199729e-01,
            1.86528091e-01, 1.90454829e-01, 1.91786196e-01, 1.90454829e-01,
            1.86528091e-01, 1.80199729e-01, 1.71766715e-01, 1.57023616e-01,
            1.28592068e-01, 1.01077605e-01, 7.55769079e-02, 5.28319225e-02,
            3.32889822e-02
        ],
        [
            2.87295370e-02, 4.74613283e-02, 6.88388861e-02, 9.23568989e-02,
            1.17254645e-01, 1.42483223e-01, 1.66695764e-01, 1.81528776e-01,
            1.87141877e-01, 1.90611190e-01, 1.91785112e-01, 1.90611190e-01,
            1.87141877e-01, 1.81528776e-01, 1.66695764e-01, 1.42483223e-01,
            1.17254645e-01, 9.23568989e-02, 6.88388861e-02, 4.74613283e-02,
            2.87295370e-02
        ],
        [
            2.17650266e-02, 3.94568191e-02, 5.93023344e-02, 8.07720575e-02,
            1.03124482e-01, 1.25394282e-01, 1.46405870e-01, 1.64828303e-01,
            1.79288925e-01, 1.88553222e-01, 1.91747252e-01, 1.88553222e-01,
            1.79288925e-01, 1.64828303e-01, 1.46405870e-01, 1.25394282e-01,
            1.03124482e-01, 8.07720575e-02, 5.93023344e-02, 3.94568191e-02,
            2.17650266e-02
        ],
        [
            1.25495284e-02, 2.90572166e-02, 4.72972116e-02, 6.67423656e-02,
            8.66951873e-02, 1.06290296e-01, 1.24520131e-01, 1.40293247e-01,
            1.52531693e-01, 1.60303860e-01, 1.62970689e-01, 1.60303860e-01,
            1.52531693e-01, 1.40293247e-01, 1.24520131e-01, 1.06290296e-01,
            8.66951873e-02, 6.67423656e-02, 4.72972116e-02, 2.90572166e-02,
            1.25495284e-02
        ],
        [
            1.26441934e-03, 1.65114811e-02, 3.31390978e-02, 5.06407706e-02,
            6.83765492e-02, 8.55839448e-02, 1.01408074e-01, 1.14955639e-01,
            1.25373662e-01, 1.31946425e-01, 1.34193829e-01, 1.31946425e-01,
            1.25373662e-01, 1.14955639e-01, 1.01408074e-01, 8.55839448e-02,
            6.83765492e-02, 5.06407706e-02, 3.31390978e-02, 1.65114811e-02,
            1.26441934e-03
        ],
        [
            0.00000000e+00, 2.06213867e-03, 1.71162845e-02, 3.27888240e-02,
            4.85026462e-02, 6.35932476e-02, 7.73387997e-02, 8.90069217e-02,
            9.79166934e-02, 1.03509489e-01, 1.05416736e-01, 1.03509489e-01,
            9.79166934e-02, 8.90069217e-02, 7.73387997e-02, 6.35932476e-02,
            4.85026462e-02, 3.27888240e-02, 1.71162845e-02, 2.06213867e-03,
            0.00000000e+00
        ]
    ])
    np.testing.assert_allclose(fd, fd_test, rtol=1e-4)
def test_station(tmpdir):

    homedir = os.path.dirname(os.path.abspath(__file__))
    datadir = os.path.abspath(os.path.join(homedir, '..', 'data', 
            'eventdata', 'Calexico', 'input'))

    #
    # Read the event, source, and rupture files and produce a Source object
    #
    inputfile = os.path.join(datadir, 'stationlist_dat.xml')
    dyfifile = os.path.join(datadir, 'ciim3_dat.xml')
    eventfile = os.path.join(datadir, 'event.xml')
    rupturefile = os.path.join(datadir, 'wei_fault.txt')

    source_obj = Source.fromFile(eventfile, rupturefile=rupturefile)

    #
    # Set up the GMPE, IPE, and GMICE
    #
    gmpe_cy14 = ChiouYoungs2014()

    gmpe = MultiGMPE.from_list([gmpe_cy14], [1.0])

    gmice = WGRW12()

    ipe = AllenEtAl2012()

    #
    # 
    #
    rupture_ctx = source_obj.getRuptureContext([gmpe])

    smdx = 0.0083333333
    smdy = 0.0083333333
    lonspan = 6.0
    latspan = 4.0
    vs30filename = os.path.join(datadir, '..', 'vs30', 'vs30.grd')

    sites_obj_grid = Sites.fromCenter(
            rupture_ctx.hypo_lon, rupture_ctx.hypo_lat, lonspan, latspan, 
            smdx, smdy, defaultVs30=760.0, vs30File=vs30filename, 
            vs30measured_grid=None, padding=False, resample=False
        )

    xmlfiles = [inputfile, dyfifile]
#    dbfile = str(tmpdir.join('stations.db'))
    dbfile = os.path.join(str(tmpdir), 'stations.db')

    stations = StationList.fromXML(xmlfiles, dbfile, source_obj, 
            sites_obj_grid, gmpe, ipe, gmice)

    df1 = stations.getStationDataframe(1, sort=True)
    df2 = stations.getStationDataframe(0, sort=True)


    #
    # In case the test starts failing because of some minor change
    # in one of the prediction or conversion equations (or roundoff
    # or whatever), but the code is running correctly, uncomment 
    # these lines and re-run the test. Then, copy the new stations.db
    # file into tests/data/eventdata/Calexico/database/. Then
    # recomment these lines and rerun the test. It should succeed.
    #
    #shutil.copy(dbfile,'./stations.db')
    #print(os.getcwd())

    #
    # We should probably check these dataframes against some established
    # set, and also check the database against a known database. 
    #

    ref_dbfile = os.path.join(datadir, '..', 'database', 'stations.db')

    stations2 = StationList(ref_dbfile)

    ref_df1 = stations2.getStationDataframe(1, sort=True)
    ref_df2 = stations2.getStationDataframe(0, sort=True)

#    assert ref_df1.equals(df1)
#    assert ref_df2.equals(df2)

    pdt.assert_frame_equal(df1, ref_df1)
    pdt.assert_frame_equal(df2, ref_df2)
def test_ss3_move_hypo1():
    magnitude = 7.2
    dip = np.array([90])
    rake = 180.0
    width = np.array([15])
    fltx = np.array([0, 0])
    flty = np.array([0, 80])
    zp = np.array([0.0])
    epix = np.array([1.0])
    epiy = np.array([-1.0])

    # Convert to lat/lon
    proj = geo.utils.get_orthographic_projection(-122, -120, 39, 37)
    tlon, tlat = proj(fltx, flty, reverse=True)
    epilon, epilat = proj(epix, epiy, reverse=True)

    flt = rupture.QuadRupture.fromTrace(
        np.array([tlon[0]]), np.array([tlat[0]]),
        np.array([tlon[1]]), np.array([tlat[1]]),
        zp, width, dip, reference='ss3')

    event = {'lat': epilat[0],
             'lon': epilon[0],
             'depth': -1.0,
             'mag': magnitude,
             'id': 'ss3',
             'locstring': 'test',
             'type': 'SS',
             'timezone': 'UTC'}
    event['time'] = ShakeDateTime.utcfromtimestamp(int(time.time()))
    event['created'] = ShakeDateTime.utcfromtimestamp(int(time.time()))

    x = np.linspace(0, 20, 6)
    y = np.linspace(0, 90, 11)
    site_x, site_y = np.meshgrid(x, y)
    slon, slat = proj(site_x, site_y, reverse=True)
    deps = np.zeros_like(slon)
    source = Source(event, flt)
    source.setEventParam('rake', rake)

    test1 = Bayless2013(source, slat, slon, deps, T=1.0)
    phyp = copy.deepcopy(test1.phyp[0])
    plat, plon, pdep = ecef2latlon(phyp.x, phyp.y, phyp.z)

    px, py = proj(plon, plat, reverse=False)

    np.testing.assert_allclose(plat, 38.004233219183604, rtol=1e-4)
    np.testing.assert_allclose(plon, -120.98636122402166, rtol=1e-4)
    np.testing.assert_allclose(pdep, 7.4999999989205968, rtol=1e-4)

    #---------------------------------------------------------------------------
    # Also for multiple segments
    #---------------------------------------------------------------------------
    dip = np.array([90., 90., 90.])
    rake = 180.0
    width = np.array([15., 15., 10.])
    fltx = np.array([0., 0., 10., 20.])
    flty = np.array([0., 20., 60., 80.])
    zp = np.array([0., 0., 0.])
    epix = np.array([0.])
    epiy = np.array([0.])

    # Convert to lat/lon
    proj = geo.utils.get_orthographic_projection(-122, -120, 39, 37)
    tlon, tlat = proj(fltx, flty, reverse=True)
    epilon, epilat = proj(epix, epiy, reverse=True)

    flt = rupture.QuadRupture.fromTrace(
        np.array(tlon[0:3]), np.array(tlat[0:3]),
        np.array(tlon[1:4]), np.array(tlat[1:4]),
        zp, width, dip, reference='')

    event = {'lat': epilat[0],
             'lon': epilon[0],
             'depth': 1.0,
             'mag': magnitude,
             'id': '',
             'locstring': 'test',
             'type': 'SS',
             'timezone': 'UTC'}
    event['time'] = ShakeDateTime.utcfromtimestamp(int(time.time()))
    event['created'] = ShakeDateTime.utcfromtimestamp(int(time.time()))
    x = np.linspace(0, 20, 6)
    y = np.linspace(0, 90, 11)
    site_x, site_y = np.meshgrid(x, y)
    slon, slat = proj(site_x, site_y, reverse=True)
    deps = np.zeros_like(slon)
    source = Source(event, flt)
    source.setEventParam('rake', rake)
    test1 = Bayless2013(source, slat, slon, deps, T=1.0)

    # 1st pseudo-hyp
    phyp = copy.deepcopy(test1.phyp[0])
    plat, plon, pdep = ecef2latlon(phyp.x, phyp.y, phyp.z)
    px, py = proj(plon, plat, reverse=False)
    np.testing.assert_allclose(plat, 38.004233219183604, rtol=1e-4)
    np.testing.assert_allclose(plon, -120.98636122402166, rtol=1e-4)
    np.testing.assert_allclose(pdep, 7.4999999989205968, rtol=1e-4)

    # 2nd pseudo-hyp
    phyp = copy.deepcopy(test1.phyp[1])
    plat, plon, pdep = ecef2latlon(phyp.x, phyp.y, phyp.z)
    px, py = proj(plon, plat, reverse=False)
    np.testing.assert_allclose(plat, 38.184097835787796, rtol=1e-4)
    np.testing.assert_allclose(plon, -120.98636122402166, rtol=1e-4)
    np.testing.assert_allclose(pdep, 7.4999999989103525, rtol=1e-4)

    # 3rd pseudo-hyp
    phyp = copy.deepcopy(test1.phyp[2])
    plat, plon, pdep = ecef2latlon(phyp.x, phyp.y, phyp.z)
    px, py = proj(plon, plat, reverse=False)
    np.testing.assert_allclose(plat, 38.543778594535752, rtol=1e-4)
    np.testing.assert_allclose(plon, -120.87137783362499, rtol=1e-4)
    np.testing.assert_allclose(pdep, 4.9999999995063993, rtol=1e-4)
def test_ss3_m6():
    magnitude = 6.0
    dip = np.array([90])
    rake = 180.0
    width = np.array([15])
    fltx = np.array([0, 0])
    flty = np.array([0, 80])
    zp = np.array([0])
    epix = np.array([0])
    epiy = np.array([0.2 * flty[1]])

    # Convert to lat/lon
    proj = geo.utils.get_orthographic_projection(-122, -120, 39, 37)
    tlon, tlat = proj(fltx, flty, reverse=True)
    epilon, epilat = proj(epix, epiy, reverse=True)

    flt = rupture.QuadRupture.fromTrace(
        np.array([tlon[0]]), np.array([tlat[0]]),
        np.array([tlon[1]]), np.array([tlat[1]]),
        zp, width, dip, reference='ss3')

    event = {'lat': epilat[0],
             'lon': epilon[0],
             'depth': 10,
             'mag': magnitude,
             'id': 'ss3',
             'locstring': 'test',
             'type': 'SS',
             'timezone': 'UTC'}
    event['time'] = ShakeDateTime.utcfromtimestamp(int(time.time()))
    event['created'] = ShakeDateTime.utcfromtimestamp(int(time.time()))

    x = np.linspace(0, 20, 6)
    y = np.linspace(0, 90, 11)
    site_x, site_y = np.meshgrid(x, y)
    slon, slat = proj(site_x, site_y, reverse=True)
    deps = np.zeros_like(slon)
    source = Source(event, flt)
    source.setEventParam('rake', rake)

    test1 = Bayless2013(source, slat, slon, deps, T=1.0)

    # Test fd
    fd = test1.getFd()
    fd_test = np.array(
      [[ 0.05853668,  0.05032323,  0.0306438 ,  0.00839635, -0.01102162,
        -0.02621319],
       [ 0.01720501, -0.00687296, -0.03804823, -0.05547473, -0.0644932 ,
        -0.06947135],
       [-0.03000065, -0.07006634, -0.07708165, -0.07865941, -0.0792369 ,
        -0.07950887],
       [ 0.0398062 ,  0.02571145, -0.0018651 , -0.0255418 , -0.04176278,
        -0.05235095],
       [ 0.0696989 ,  0.06389524,  0.04890304,  0.02983134,  0.01098535,
        -0.00545921],
       [ 0.088278  ,  0.08511069,  0.07628596,  0.06350294,  0.04875897,
         0.03373495],
       [ 0.10179334,  0.09978475,  0.09401676,  0.0851842 ,  0.07422509,
         0.06210369],
       [ 0.11242209,  0.11102701,  0.10696056,  0.10055471,  0.09229027,
         0.08271454],
       [ 0.12118279,  0.12015315,  0.11712653,  0.11228058,  0.10588323,
         0.09825795],
       [ 0.12785957,  0.12706892,  0.12473264,  0.12095384,  0.11589197,
         0.10974684],
       [ 0.12785908,  0.12724852,  0.12543819,  0.12249026,  0.11850249,
         0.11360047]])
    np.testing.assert_allclose(
        fd, fd_test, rtol=1e-4)
Exemple #19
0
def _test_intensity():

    datadir = os.path.abspath(
        os.path.join(homedir, '..', 'data', 'eventdata', 'northridge'))
    shakefile = os.path.join(datadir, 'northridge_grid.xml')
    topofile = os.path.join(datadir, 'northridge_topo.grd')
    faultfile = os.path.join(datadir, 'northridge_fault.txt')
    cityfile = os.path.join(datadir, 'northridge_cities.txt')
    coastfile = os.path.join(datadir, 'northridge_coastline.json')
    countryfile = os.path.join(datadir, 'northridge_countries.json')
    statefile = os.path.join(datadir, 'northridge_states.json')
    lakefile = os.path.join(datadir, 'northridge_lakes.json')
    oceanfile = os.path.join(datadir, 'northridge_ocean.json')
    stationfile = os.path.join(datadir, 'northridge_stations.db')
    roadfile = os.path.join(datadir, 'northridge_roads.json')
    tancptfile = os.path.join(shakedir, 'shakemap', 'mapping', 'tan.cpt')
    shakecptfile = os.path.join(shakedir, 'shakemap', 'mapping',
                                'shakecpt.cpt')

    layerdict = {
        'coast': coastfile,
        'ocean': oceanfile,
        'lake': lakefile,
        'country': countryfile,
        'roads': roadfile,
        'state': statefile
    }

    tancolormap = GMTColorMap.loadFromCPT(tancptfile)
    shakecolormap = GMTColorMap.loadFromCPT(shakecptfile)
    cities = BasemapCities.loadFromCSV(cityfile)
    shakemap = ShakeGrid.load(shakefile, adjust='res')
    stations = StationList(stationfile)
    fault = Fault.readFaultFile(faultfile)
    edict = shakemap.getEventDict()
    eventdict = {
        'lat': edict['lat'],
        'lon': edict['lon'],
        'depth': edict['depth'],
        'mag': edict['magnitude'],
        'time': edict['event_timestamp']
    }
    source = Source(eventdict, fault)
    maker = MapMaker(shakemap, topofile, stations, fault, layerdict, source,
                     cities)

    # draw intensity map
    outfolder = os.path.expanduser('~')
    maker.setIntensityLayer('mmi')
    maker.setIntensityGMTColorMap(shakecolormap)
    intensity_map = maker.drawIntensityMap(outfolder)
    print('Intensity map saved as: %s' % intensity_map)

    # draw contour maps
    maker.setContourGMTColorMap(tancolormap)

    # Draw pgv contours
    maker.setContourLayer('pgv')
    contour_pgv_map = maker.drawContourMap(outfolder)
    print('PGV contour map saved as: %s' % contour_pgv_map)

    # Draw pga contours
    maker.setContourLayer('pga')
    contour_pga_map = maker.drawContourMap(outfolder)
    print('PGA contour map saved as: %s' % contour_pga_map)

    # Draw psa0.3 contours
    maker.setContourLayer('psa03')
    contour_psa03_map = maker.drawContourMap(outfolder)
    print('PSA0.3 contour map saved as: %s' % contour_psa03_map)

    # Draw psa1.0 contours
    maker.setContourLayer('psa10')
    contour_psa10_map = maker.drawContourMap(outfolder)
    print('PSA1.0 contour map saved as: %s' % contour_psa10_map)

    # Draw psa3.0 contours
    maker.setContourLayer('psa30')
    contour_psa30_map = maker.drawContourMap(outfolder)
    print('PSA3.0 contour map saved as: %s' % contour_psa30_map)
def test_multigmpe():
    # Define gmpes and their weights
    gmpes = [AbrahamsonEtAl2014(), BooreEtAl2014(),
             CampbellBozorgnia2014(), ChiouYoungs2014()]
    wts = [0.25, 0.25, 0.25, 0.25]

    # Make sites instance
    vs30file = os.path.join(shakedir, 'tests/data/Vs30_test.grd')
    cx = -118.2
    cy = 34.1
    dx = 0.0083
    dy = 0.0083
    xspan = 0.0083 * 5
    yspan = 0.0083 * 5
    site = Sites.createFromCenter(cx, cy, xspan, yspan, dx, dy,
                                  vs30File=vs30file,
                                  padding=True, resample=False)
    sctx = site.getSitesContext()
    sctx.vs30 = np.reshape(sctx.vs30, (-1,))
    sctx.vs30measured = np.reshape(sctx.vs30measured, (-1,))
    sctx.z1pt0 = np.reshape(sctx.z1pt0, (-1,))

    # Need separate z1pt0 arrays
    sctx.z1pt0cy14 = mg._z1_from_vs30_cy14_cal(sctx.vs30)
    sctx.z1pt0ask14 = mg._z1_from_vs30_ask14_cal(sctx.vs30)
    sctx.z2pt5 = mg._z2p5_from_vs30_cb14_cal(sctx.vs30) / 1000.0

    # Make souce instance
    lat0 = np.array([34.1])
    lon0 = np.array([-118.2])
    lat1 = np.array([34.2])
    lon1 = np.array([-118.15])
    z = np.array([1.0])
    W = np.array([3.0])
    dip = np.array([30.])

    flt = Fault.fromTrace(lon0, lat0, lon1, lat1, z, W, dip)
    event = {'lat': 34.1, 'lon': -118.2, 'depth': 1, 'mag': 6,
             'id': '', 'locstring': '', 'rake': 30.3,
             'time': ShakeDateTime.utcfromtimestamp(int(time.time())),
             'timezone': 'UTC'}
    source = Source(event, flt)

    # Make a rupture context
    rupt = source.getRuptureContext(gmpes)

    # Make a distance context
    dctx = Distance.fromSites(gmpes, source, site).getDistanceContext()
    dctx.rhypo = np.reshape(dctx.rhypo, (-1,))
    dctx.rx = np.reshape(dctx.rx, (-1,))
    dctx.rjb = np.reshape(dctx.rjb, (-1,))
    dctx.ry0 = np.reshape(dctx.ry0, (-1,))
    dctx.rrup = np.reshape(dctx.rrup, (-1,))

    # Compute weighted GMPE
    iimt = imt.PGV()
    stddev_types = [const.StdDev.TOTAL]
    mgmpe = mg.MultiGMPE.from_list(gmpes, wts)
    lnmu, lnsd = mgmpe.get_mean_and_stddevs(
        sctx, rupt, dctx, iimt, stddev_types)

    lnmud = np.array(
        [3.44828531,  3.49829605,  3.61749432,  3.64343805,  3.7001028,
         3.7348924,  3.76927164,  3.78659955,  3.82600784,  3.46635007,
         3.53816879,  3.6486898,  3.67058155,  3.72223342,  3.75403094,
         3.79315031,  3.79871491,  3.82093027,  3.54889613,  3.57531437,
         3.64441687,  3.69915981,  3.74491289,  3.78931599,  3.80957828,
         3.80870754,  3.8731021,  3.5927326,  3.60764647,  3.66894024,
         3.72148551,  3.75742965,  3.82164661,  3.86341308,  3.87171115,
         3.79092594,  3.64153758,  3.61835381,  3.68166249,  3.7338161,
         3.82454214,  3.81543928,  3.81507658,  3.80006803,  3.77165695,
         3.65178742,  3.71324776,  3.70389969,  3.77034752,  3.78259432,
         3.78677497,  3.79838465,  3.79050287,  3.75066018,  3.52883328,
         3.67813977,  3.71754876,  3.65520574,  3.69463436,  3.72516445,
         3.7457098,  3.74672185,  3.72615784,  3.44535551,  3.61907294,
         3.58790363,  3.58068716,  3.61177983,  3.64349327,  3.66698468,
         3.67129902,  3.65483002]
    )

    lnsdd = np.array(
       [ 0.63560302,  0.63648101,  0.63610581,  0.6390135 ,  0.64203528,
         0.64624098,  0.64851812,  0.64640406,  0.64384305,  0.6361429 ,
         0.63677975,  0.63715381,  0.64040366,  0.64404005,  0.64782624,
         0.6476325 ,  0.64509458,  0.64297808,  0.63477576,  0.63727968,
         0.63899462,  0.64205578,  0.64604037,  0.64815296,  0.64609948,
         0.64402734,  0.63844724,  0.6343891 ,  0.63806041,  0.64043609,
         0.64406094,  0.64776777,  0.64717195,  0.64297191,  0.64011346,
         0.64110084,  0.63137566,  0.63864151,  0.64163093,  0.64588687,
         0.64714873,  0.64603694,  0.64397734,  0.64217431,  0.63958323,
         0.62883338,  0.63127469,  0.63961477,  0.64097303,  0.6442055 ,
         0.64376449,  0.64273526,  0.64112115,  0.63815862,  0.63575399,
         0.6291859 ,  0.63180644,  0.6394421 ,  0.63946545,  0.63947169,
         0.63935499,  0.63832598,  0.63664816,  0.63595663,  0.62755689,
         0.63523274,  0.63663489,  0.63631586,  0.63616589,  0.63597828,
         0.63542126,  0.63500847])

    np.testing.assert_allclose(lnmu, lnmud)
    np.testing.assert_allclose(lnsd[0], lnsdd)

    # Check for exception due to weights:
    with pytest.raises(Exception) as a:
        wts = [0.25, 0.25, 0.25, 0.25 + 1e-4]
        mgmpe = mg.MultiGMPE.from_list(gmpes, wts)

    # Check exception on GMPE check
    with pytest.raises(Exception) as a:
        wts = [1.0]
        mgmpe = mg.MultiGMPE.from_list(['BA08'], wts)

    # Check exception on tectonic region
    with pytest.raises(Exception) as a:
        gmpes = [BooreEtAl2014(), Campbell2003()]
        wts = [0.5, 0.5]
        mgmpe = mg.MultiGMPE.from_list(gmpes, wts)

    # Check exception on length of gmpe and weight lenghts
    with pytest.raises(Exception) as a:
        gmpes = [BooreEtAl2014(), Campbell2003()]
        wts = [1.0]
        mgmpe = mg.MultiGMPE.from_list(gmpes, wts)
    

    # Check PGV from a GMPE without PGV
    gmpes = [Campbell2003()]
    wts = [1.0]
    mgmpe = mg.MultiGMPE.from_list(gmpes, wts)
    lnmu, lnsd = mgmpe.get_mean_and_stddevs(
        sctx, rupt, dctx, iimt, stddev_types)

    lnmud = np.array(
      [ 3.09152212,  3.1524312 ,  3.20749883,  3.25431585,  3.29035521,
        3.31326677,  3.32116911,  3.31341321,  3.29819842,  3.12252648,
        3.18081138,  3.23208034,  3.27383205,  3.30358765,  3.319195  ,
        3.31916753,  3.30623521,  3.28938984,  3.15235911,  3.20745205,
        3.25429394,  3.29035582,  3.31328548,  3.32119931,  3.31344697,
        3.2982328 ,  3.27982759,  3.17945026,  3.23203088,  3.2738231 ,
        3.30360265,  3.31922869,  3.31921198,  3.30628471,  3.28944133,
        3.26955097,  3.18990634,  3.24351181,  3.28521502,  3.31195497,
        3.32124956,  3.3135073 ,  3.29830033,  3.27989827,  3.25860053,
        3.17942778,  3.23201703,  3.27282524,  3.29888607,  3.3078892 ,
        3.30156745,  3.2884687 ,  3.26964276,  3.24701758,  3.14910673,
        3.19888101,  3.23727522,  3.26163304,  3.2701699 ,  3.2690822 ,
        3.26201491,  3.24919602,  3.23101321,  3.10184816,  3.1475792 ,
        3.18259748,  3.20467529,  3.21444387,  3.21832088,  3.21671138,
        3.20966263,  3.19737325]
    )

    lnsdd = np.array(
       [ 0.83458518,  0.83458518,  0.83458518,  0.83458518,  0.83458518,
         0.83458518,  0.83458518,  0.83458518,  0.83458518,  0.83458518,
         0.83458518,  0.83458518,  0.83458518,  0.83458518,  0.83458518,
         0.83458518,  0.83458518,  0.83458518,  0.83458518,  0.83458518,
         0.83458518,  0.83458518,  0.83458518,  0.83458518,  0.83458518,
         0.83458518,  0.83458518,  0.83458518,  0.83458518,  0.83458518,
         0.83458518,  0.83458518,  0.83458518,  0.83458518,  0.83458518,
         0.83458518,  0.83458518,  0.83458518,  0.83458518,  0.83458518,
         0.83458518,  0.83458518,  0.83458518,  0.83458518,  0.83458518,
         0.83458518,  0.83458518,  0.83458518,  0.83458518,  0.83458518,
         0.83458518,  0.83458518,  0.83458518,  0.83458518,  0.83458518,
         0.83458518,  0.83458518,  0.83458518,  0.83458518,  0.83458518,
         0.83458518,  0.83458518,  0.83458518,  0.83458518,  0.83458518,
         0.83458518,  0.83458518,  0.83458518,  0.83458518,  0.83458518,
         0.83458518,  0.83458518]
    )

    np.testing.assert_allclose(lnmu, lnmud)
    np.testing.assert_allclose(lnsd[0], lnsdd)
def test_rv4():
    magnitude = 7.0
    rake = 90.0
    width = np.array([28])
    fltx = np.array([0, 0])
    flty = np.array([0, 32])
    zp = np.array([0])
    dip = np.array([30])

    # Convert to lat/lon
    proj = geo.utils.get_orthographic_projection(-122, -120, 39, 37)
    tlon, tlat = proj(fltx, flty, reverse=True)

    flt = fault.Fault.fromTrace(np.array([tlon[0]]),
                                np.array([tlat[0]]),
                                np.array([tlon[1]]),
                                np.array([tlat[1]]),
                                zp,
                                width,
                                dip,
                                reference='')
    L = flt.getFaultLength()

    # Try to figure out epicenter
    tmp = flt.getQuadrilaterals()[0]
    pp0 = Vector.fromPoint(
        point.Point(tmp[0].longitude, tmp[0].latitude, tmp[0].depth))
    pp1 = Vector.fromPoint(
        point.Point(tmp[1].longitude, tmp[1].latitude, tmp[1].depth))
    pp2 = Vector.fromPoint(
        point.Point(tmp[2].longitude, tmp[2].latitude, tmp[2].depth))
    pp3 = Vector.fromPoint(
        point.Point(tmp[3].longitude, tmp[3].latitude, tmp[3].depth))
    dxp = 6 / L
    dyp = (width - 8) / width
    mp0 = pp0 + (pp1 - pp0) * dxp
    mp1 = pp3 + (pp2 - pp3) * dxp
    rp = mp0 + (mp1 - mp0) * dyp
    epilat, epilon, epidepth = ecef2latlon(rp.x, rp.y, rp.z)

    event = {
        'lat': epilat,
        'lon': epilon,
        'depth': epidepth,
        'mag': magnitude,
        'id': 'test',
        'locstring': 'rv4',
        'type': 'DS',
        'timezone': 'UTC'
    }
    event['time'] = ShakeDateTime.utcfromtimestamp(int(time.time()))
    event['created'] = ShakeDateTime.utcfromtimestamp(int(time.time()))

    x = np.linspace(-50, 50, 11)
    y = np.linspace(-50, 50, 11)
    site_x, site_y = np.meshgrid(x, y)
    slon, slat = proj(site_x, site_y, reverse=True)
    deps = np.zeros_like(slon)
    source = Source(event, flt)
    source.setEventParam('rake', rake)

    test1 = Bayless2013(source, slat, slon, deps, T=2.0)

    # Test fd
    fd = test1.getFd()
    fd_test = np.array(
        [[
            0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 1.72143257e-03,
            1.34977260e-03, 4.33616224e-15, 1.24446253e-03, 1.16142357e-03,
            2.25464716e-03, 7.05281751e-04, 0.00000000e+00
        ],
         [
             0.00000000e+00, 0.00000000e+00, 7.62610242e-03, 1.25133844e-02,
             5.61896104e-03, 7.63126014e-15, 4.52266194e-03, 4.67970900e-03,
             1.02820316e-02, 5.13160096e-03, -6.13926251e-03
         ],
         [
             0.00000000e+00, 4.00495234e-03, 2.37608386e-02, 2.37139333e-02,
             9.55224050e-03, 5.66364910e-15, 7.70344813e-03, 7.36466362e-03,
             1.48239704e-02, 8.40388145e-03, -1.58592485e-02
         ],
         [
             8.08385547e-19, 9.38150101e-03, 3.38610620e-02, 3.85351492e-02,
             1.91044918e-02, 3.98697802e-15, 1.54321666e-02, 1.21913760e-02,
             2.04435166e-02, 1.04931859e-02, -1.85935894e-02
         ],
         [
             2.12025421e-18, 1.37316085e-02, 4.40193799e-02, 6.16562477e-02,
             4.77612496e-02, 2.60257085e-15, 3.86322888e-02, 1.97965887e-02,
             2.64882038e-02, 1.23335908e-02, -2.07389932e-02
         ],
         [
             2.64338576e-18, 1.45898292e-02, 4.89104213e-02, 7.70703166e-02,
             9.55225258e-02, 1.01875104e-01, 7.73459329e-02, 2.50275508e-02,
             2.93537540e-02, 1.30949577e-02, -2.15685454e-02
         ],
         [
             2.64330042e-18, 1.45898262e-02, 4.89104186e-02, 7.70703146e-02,
             9.55225248e-02, 1.01910945e-01, 7.74050835e-02, 2.52307946e-02,
             2.92970736e-02, 1.30880504e-02, -2.15685424e-02
         ],
         [
             2.64318867e-18, 1.45898259e-02, 4.89104184e-02, 7.70703144e-02,
             9.55225247e-02, 1.01933432e-01, 7.74421258e-02, 2.53572923e-02,
             2.92615130e-02, 1.30837284e-02, -2.15685422e-02
         ],
         [
             2.64305117e-18, 1.45898284e-02, 4.89104206e-02, 7.70703161e-02,
             9.55225256e-02, 1.01942593e-01, 7.74571359e-02, 2.54081640e-02,
             2.92472117e-02, 1.30819985e-02, -2.15685446e-02
         ],
         [
             2.30141673e-18, 1.40210825e-02, 4.56205547e-02, 6.63109661e-02,
             5.79266964e-02, 2.33044622e-15, 4.69672564e-02, 2.18401553e-02,
             2.72864925e-02, 1.25728575e-02, -2.10227772e-02
         ],
         [
             1.10672535e-18, 1.04777076e-02, 3.59041065e-02, 4.24614318e-02,
             2.24217216e-02, 3.66914762e-15, 1.81728517e-02, 1.39301504e-02,
             2.14956836e-02, 1.08711460e-02, -1.90802849e-02
         ]])
    np.testing.assert_allclose(fd, fd_test, rtol=2e-4)
def test_distance_no_fault():
    # Make sites instance
    vs30file = os.path.join(shakedir, 'tests/data/Vs30_test.grd')
    cx = -118.2
    cy = 34.1
    dx = 0.0083
    dy = 0.0083
    xspan = 0.0083 * 5
    yspan = 0.0083 * 5
    site = Sites.createFromCenter(cx, cy, xspan, yspan, dx, dy,
                                  vs30File=vs30file,
                                  padding=True, resample=False)
    # Make souce instance
    #  - Unknown/no tectonic region
    #  - Mech is ALL
    event = {'lat': 34.1, 'lon': -118.2, 'depth': 1, 'mag': 6,
             'id': '', 'locstring': '',
             'time': ShakeDateTime.utcfromtimestamp(int(time.time())),
             'timezone': 'UTC'}
    source = Source(event)
    source.setMechanism('ALL')
    gmpe = AbrahamsonEtAl2014()
    dists = Distance.fromSites(gmpe, source, site)
    dctx = dists.getDistanceContext()

    rjb = np.array(
        [[1.69885399, 1.43717125, 1.2401229, 1.12306082, 1.08344161,
          1.12306082, 1.2401229, 1.43717125, 1.69885399],
         [1.31170897, 1.03475736, 0.82507289, 0.69891123, 0.6553167,
            0.69891123, 0.82507289, 1.03475736, 1.31170897],
            [1.02288894, 0.72918893, 0.50725874, 0.36675284, 0.31957002,
             0.36675284, 0.50725874, 0.72918893, 1.02288894],
            [0.83950888, 0.53487557, 0.30135487, 0.14776055, 0.09290799,
             0.14776055, 0.30135487, 0.53487557, 0.83950888],
            [0.77835253, 0.46963915, 0.2280053, 0.06643829, 0.,
             0.06643829, 0.2280053, 0.46963915, 0.77835253],
            [0.83964536, 0.53495323, 0.30139344, 0.14777086, 0.09290799,
             0.14777086, 0.30139344, 0.53495323, 0.83964536],
            [1.02314515, 0.72933979, 0.50732902, 0.36677229, 0.31957002,
             0.36677229, 0.50732902, 0.72933979, 1.02314515],
            [1.31208408, 1.03497272, 0.82517579, 0.69893672, 0.6553167,
             0.69893672, 0.82517579, 1.03497272, 1.31208408]]
    )

    np.testing.assert_allclose(
        rjb, dctx.rjb, rtol=0, atol=0.01)

    rrup = np.array(
        [[8.55244227, 8.44386675, 8.30721612, 8.28522381, 8.27615555,
          8.28522381, 8.30721612, 8.44386675, 8.55244227],
         [8.36298714, 8.26004043, 8.11921658, 8.11376473, 8.09579991,
          8.11376473, 8.11921658, 8.26004043, 8.36298714],
         [8.25524589, 8.12023764, 8.01816823, 7.93517669, 7.9600631,
          7.93517669, 8.01816823, 8.12023764, 8.25524589],
         [8.13306689, 8.00702434, 7.95888951, 7.8881565, 7.88352525,
          7.8881565, 7.95888951, 8.00702434, 8.13306689],
         [8.12102175, 8.02022568, 7.90722113, 7.85639147, 0.,
          7.85639147, 7.90722113, 8.02022568, 8.12102175],
         [8.13319461, 8.00698213, 7.95890141, 7.88813785, 7.88352525,
          7.88813785, 7.95890141, 8.00698213, 8.13319461],
         [8.25535307, 8.12025813, 8.01814988, 7.93520725, 7.9600631,
          7.93520725, 8.01814988, 8.12025813, 8.25535307],
         [8.36326021, 8.26012423, 8.11931772, 8.1137722, 8.09579991,
          8.1137722, 8.11931772, 8.26012423, 8.36326021]]
    )

    np.testing.assert_allclose(
        rrup, dctx.rrup, rtol=0, atol=0.01)

    # Souce instance
    #  - Tectonic region: active
    #  - Mech is ALL

    source.setMechanism('ALL')
    source._tectonic_region = 'Active Shallow Crust'
    dists = Distance.fromSites(gmpe, source, site)
    dctx = dists.getDistanceContext()

    rjb = np.array(
        [[1.7826962, 1.5154877, 1.31394288, 1.19278311, 1.15168205,
          1.19278311, 1.31394288, 1.5154877, 1.7826962],
         [1.38719993, 1.10115976, 0.88331216, 0.75077594, 0.70491937,
            0.75077594, 0.88331216, 1.10115976, 1.38719993],
            [1.08884025, 0.78261273, 0.54865189, 0.39914713, 0.34852149,
             0.39914713, 0.54865189, 0.78261273, 1.08884025],
            [0.8983245, 0.57800579, 0.32895654, 0.16294131, 0.10290794,
             0.16294131, 0.32895654, 0.57800579, 0.8983245],
            [0.83428851, 0.50865092, 0.25000954, 0.07371315, 0.,
             0.07371315, 0.25000954, 0.50865092, 0.83428851],
            [0.89846642, 0.57808833, 0.32899797, 0.16295256, 0.10290794,
             0.16295256, 0.32899797, 0.57808833, 0.89846642],
            [1.08910622, 0.78277133, 0.5487266, 0.39916785, 0.34852149,
             0.39916785, 0.5487266, 0.78277133, 1.08910622],
            [1.38758368, 1.10138329, 0.88341918, 0.75080275, 0.70491937,
             0.75080275, 0.88341918, 1.10138329, 1.38758368]]
    )

    np.testing.assert_allclose(
        rjb, dctx.rjb, rtol=0, atol=0.01)

    rrup = np.array(
        [[9.0098969, 8.90622877, 8.76985073, 8.7508322, 8.74262729,
          8.7508322, 8.76985073, 8.90622877, 9.0098969],
         [8.82590475, 8.72726953, 8.58534811, 8.58323541, 8.5654409,
            8.58323541, 8.58534811, 8.72726953, 8.82590475],
            [8.72260676, 8.58922774, 8.4871601, 8.40244887, 8.4297141,
             8.40244887, 8.4871601, 8.58922774, 8.72260676],
            [8.59952763, 8.47470803, 8.42880892, 8.35631312, 8.35201539,
             8.35631312, 8.42880892, 8.47470803, 8.59952763],
            [8.58864463, 8.49021276, 8.37536496, 8.32350188, 0.,
             8.32350188, 8.37536496, 8.49021276, 8.58864463],
            [8.59965826, 8.47466149, 8.42882081, 8.35629324, 8.35201539,
             8.35629324, 8.42882081, 8.47466149, 8.59965826],
            [8.72271133, 8.58924512, 8.48713903, 8.4024808, 8.4297141,
             8.4024808, 8.48713903, 8.58924512, 8.72271133],
            [8.8261781, 8.72735075, 8.58545174, 8.5832426, 8.5654409,
             8.5832426, 8.58545174, 8.72735075, 8.8261781]]
    )

    np.testing.assert_allclose(
        rrup, dctx.rrup, rtol=0, atol=0.01)

    # Souce instance
    #  - Tectonic region: active
    #  - Mech is RS

    source.setMechanism('RS')
    source._tectonic_region = 'Active Shallow Crust'
    dists = Distance.fromSites(gmpe, source, site)
    dctx = dists.getDistanceContext()

    rjb = np.array(
        [[1.62698377, 1.35570722, 1.15237308, 1.03338675, 0.99327828,
          1.03338675, 1.15237308, 1.35570722, 1.62698377],
         [1.22613799, 0.94405696, 0.73298036, 0.61025288, 0.56806407,
            0.61025288, 0.73298036, 0.94405696, 1.22613799],
            [0.93206877, 0.63960501, 0.42680989, 0.29738908, 0.25562246,
             0.29738908, 0.42680989, 0.63960501, 0.93206877],
            [0.74745861, 0.45239173, 0.23958848, 0.10874248, 0.06508855,
             0.10874248, 0.23958848, 0.45239173, 0.74745861],
            [0.68734787, 0.39203111, 0.17571632, 0.04480241, 0.,
             0.04480241, 0.17571632, 0.39203111, 0.68734787],
            [0.74759552, 0.45246373, 0.2396224, 0.1087508, 0.06508855,
             0.1087508, 0.2396224, 0.45246373, 0.74759552],
            [0.93232753, 0.63975135, 0.42687494, 0.29740691, 0.25562246,
             0.29740691, 0.42687494, 0.63975135, 0.93232753],
            [1.22652484, 0.94427453, 0.73308353, 0.61027758, 0.56806407,
             0.61027758, 0.73308353, 0.94427453, 1.22652484]]
    )

    np.testing.assert_allclose(
        rjb, dctx.rjb, rtol=0, atol=0.01)

    rrup = np.array(
        [[9.10924586, 9.00129817, 8.86119702, 8.8412603, 8.83276567,
          8.8412603, 8.86119702, 9.00129817, 9.10924586],
         [8.91865949, 8.81700098, 8.67259556, 8.6707467, 8.65288076,
            8.6707467, 8.67259556, 8.81700098, 8.91865949],
            [8.81223015, 8.67672238, 8.57444747, 8.49014736, 8.5183922,
             8.49014736, 8.57444747, 8.67672238, 8.81223015],
            [8.68698864, 8.56159881, 8.51774523, 8.44708824, 8.44391005,
             8.44708824, 8.51774523, 8.56159881, 8.68698864],
            [8.67600489, 8.57788943, 8.46484058, 8.41567031, 0.,
             8.41567031, 8.46484058, 8.57788943, 8.67600489],
            [8.68712127, 8.56155103, 8.51775668, 8.44706792, 8.44391005,
             8.44706792, 8.51775668, 8.56155103, 8.68712127],
            [8.81233708, 8.67673955, 8.5744255, 8.49017946, 8.5183922,
             8.49017946, 8.5744255, 8.67673955, 8.81233708],
            [8.91894005, 8.81708413, 8.67270074, 8.67075389, 8.65288076,
             8.67075389, 8.67270074, 8.81708413, 8.91894005]]
    )

    np.testing.assert_allclose(
        rrup, dctx.rrup, rtol=0, atol=0.01)

    # Souce instance
    #  - Tectonic region: active
    #  - Mech is NM

    source.setMechanism('NM')
    source._tectonic_region = 'Active Shallow Crust'
    dists = Distance.fromSites(gmpe, source, site)
    dctx = dists.getDistanceContext()

    rjb = np.array(
        [[1.37668217, 1.14221705, 0.96668127, 0.86561026, 0.8316382,
          0.86561026, 0.96668127, 1.14221705, 1.37668217],
         [1.03033842, 0.7899535, 0.61128249, 0.50843465, 0.47310349,
            0.50843465, 0.61128249, 0.7899535, 1.03033842],
            [0.77980196, 0.53301769, 0.35495992, 0.24706995, 0.21231668,
             0.24706995, 0.35495992, 0.53301769, 0.77980196],
            [0.62353293, 0.37628889, 0.19897762, 0.09028387, 0.05409271,
             0.09028387, 0.19897762, 0.37628889, 0.62353293],
            [0.57300663, 0.32596438, 0.14586493, 0.03728933, 0.,
             0.03728933, 0.14586493, 0.32596438, 0.57300663],
            [0.62364878, 0.37634892, 0.19900584, 0.09029076, 0.05409271,
             0.09029076, 0.19900584, 0.37634892, 0.62364878],
            [0.78002107, 0.53314026, 0.35501415, 0.24708481, 0.21231668,
             0.24708481, 0.35501415, 0.53314026, 0.78002107],
            [1.03067234, 0.79013775, 0.61136979, 0.50845534, 0.47310349,
             0.50845534, 0.61136979, 0.79013775, 1.03067234]]
    )

    np.testing.assert_allclose(
        rjb, dctx.rjb, rtol=0, atol=0.01)

    rrup = np.array(
        [[8.24181179, 8.13246903, 7.99744833, 7.97427973, 7.96488994,
          7.97427973, 7.99744833, 8.13246903, 8.24181179],
         [8.0523808, 7.94857792, 7.8097532, 7.80275055, 7.78489184,
            7.80275055, 7.8097532, 7.94857792, 8.0523808],
            [7.94376991, 7.80938707, 7.70839546, 7.62713392, 7.65061937,
             7.62713392, 7.70839546, 7.80938707, 7.94376991],
            [7.82330022, 7.69799123, 7.64933048, 7.58013644, 7.57530397,
             7.58013644, 7.64933048, 7.69799123, 7.82330022],
            [7.81082127, 7.7099614, 7.59905656, 7.54904995, 0.,
             7.54904995, 7.59905656, 7.7099614, 7.81082127],
            [7.8234252, 7.69795153, 7.64934225, 7.58011859, 7.57530397,
             7.58011859, 7.64934225, 7.69795153, 7.8234252],
            [7.94387723, 7.80940883, 7.70837861, 7.62716348, 7.65061937,
             7.62716348, 7.70837861, 7.80940883, 7.94387723],
            [8.05265024, 7.94866209, 7.80985208, 7.80275807, 7.78489184,
             7.80275807, 7.80985208, 7.94866209, 8.05265024]]
    )

    np.testing.assert_allclose(
        rrup, dctx.rrup, rtol=0, atol=0.01)

    # Souce instance
    #  - Tectonic region: active
    #  - Mech is SS

    source.setMechanism('SS')
    source._tectonic_region = 'Active Shallow Crust'
    dists = Distance.fromSites(gmpe, source, site)
    dctx = dists.getDistanceContext()

    rjb = np.array(
        [[2.87579088, 2.5456044, 2.29185071, 2.12884849, 2.07268393,
          2.12884849, 2.29185071, 2.5456044, 2.87579088],
         [2.38460904, 2.00333625, 1.69987873, 1.49778447, 1.42682201,
            1.49778447, 1.69987873, 2.00333625, 2.38460904],
            [1.98637252, 1.54679373, 1.17582687, 0.914992, 0.81812345,
             0.914992, 1.17582687, 1.54679373, 1.98637252],
            [1.72104847, 1.22626178, 0.78021345, 0.43426687, 0.29180829,
             0.43426687, 0.78021345, 1.22626178, 1.72104847],
            [1.62592524, 1.10672645, 0.62338097, 0.21787328, 0.,
             0.21787328, 0.62338097, 1.10672645, 1.62592524],
            [1.72124839, 1.22640328, 0.7802939, 0.43429295, 0.29180829,
             0.43429295, 0.7802939, 1.22640328, 1.72124839],
            [1.98673898, 1.54703738, 1.17595553, 0.9150287, 0.81812345,
             0.9150287, 1.17595553, 1.54703738, 1.98673898],
            [2.3850933, 2.00364385, 1.7000298, 1.49782584, 1.42682201,
             1.49782584, 1.7000298, 2.00364385, 2.3850933]]
    )

    np.testing.assert_allclose(
        rjb, dctx.rjb, rtol=0, atol=0.01)

    rrup = np.array(
        [[8.37718818, 8.22723595, 8.0625333, 8.01740393, 8.00044835,
          8.01740393, 8.0625333, 8.22723595, 8.37718818],
         [8.12814884, 7.97494557, 7.79806013, 7.76281765, 7.735536,
            7.76281765, 7.79806013, 7.97494557, 8.12814884],
            [7.96791548, 7.77614524, 7.62644695, 7.5120851, 7.52225031,
             7.5120851, 7.62644695, 7.77614524, 7.96791548],
            [7.81413881, 7.62326525, 7.51608628, 7.40624352, 7.38577101,
             7.40624352, 7.51608628, 7.62326525, 7.81413881],
            [7.78868149, 7.61851685, 7.44728324, 7.35263272, 0.,
             7.35263272, 7.44728324, 7.61851685, 7.78868149],
            [7.81428779, 7.62324614, 7.51610811, 7.40622914, 7.38577101,
             7.40622914, 7.51610811, 7.62324614, 7.81428779],
            [7.96807069, 7.77620063, 7.62644821, 7.51211876, 7.52225031,
             7.51211876, 7.62644821, 7.77620063, 7.96807069],
            [8.12847458, 7.97507013, 7.798177, 7.76283076, 7.735536,
             7.76283076, 7.798177, 7.97507013, 8.12847458]]
    )

    np.testing.assert_allclose(
        rrup, dctx.rrup, rtol=0, atol=0.01)

    # Souce instance
    #  - Tectonic region: stable
    #  - Mech is all

    source.setMechanism('ALL')
    source._tectonic_region = 'Stable Shallow Crust'
    dists = Distance.fromSites(gmpe, source, site)
    dctx = dists.getDistanceContext()

    rjb = np.array(
        [[2.03925124, 1.73155737, 1.4995708, 1.36023359, 1.3129796,
          1.36023359, 1.4995708, 1.73155737, 2.03925124],
         [1.58388219, 1.25490086, 1.00456938, 0.85266727, 0.80013341,
            0.85266727, 1.00456938, 1.25490086, 1.58388219],
            [1.24073997, 0.8891456, 0.621322, 0.4507253, 0.39310934,
             0.4507253, 0.621322, 0.8891456, 1.24073997],
            [1.02181451, 0.65483395, 0.37085259, 0.18246645, 0.11472522,
             0.18246645, 0.37085259, 0.65483395, 1.02181451],
            [0.94836452, 0.57566287, 0.28111922, 0.08186392, 0.,
             0.08186392, 0.28111922, 0.57566287, 0.94836452],
            [1.02197754, 0.65492818, 0.37089972, 0.18247916, 0.11472522,
             0.18247916, 0.37089972, 0.65492818, 1.02197754],
            [1.24104568, 0.88932733, 0.62140729, 0.45074894, 0.39310934,
             0.45074894, 0.62140729, 0.88932733, 1.24104568],
            [1.58432388, 1.2551578, 1.00469232, 0.85269799, 0.80013341,
             0.85269799, 1.00469232, 1.2551578, 1.58432388]]
    )

    np.testing.assert_allclose(
        rjb, dctx.rjb, rtol=0, atol=0.01)

    rrup = np.array(
        [[6.77027261, 6.6175234, 6.4620818, 6.41487409, 6.39777405,
          6.41487409, 6.4620818, 6.6175234, 6.77027261],
         [6.52306411, 6.37308879, 6.21401632, 6.17868645, 6.15460391,
            6.17868645, 6.21401632, 6.37308879, 6.52306411],
            [6.36643129, 6.19096821, 6.06108712, 5.96682577, 5.97487576,
             5.96682577, 6.06108712, 6.19096821, 6.36643129],
            [6.22803711, 6.05926472, 5.97000316, 5.88510619, 5.87109182,
             5.88510619, 5.97000316, 6.05926472, 6.22803711],
            [6.2037114, 6.05384337, 5.91573235, 5.84608701, 0.,
             5.84608701, 5.91573235, 6.05384337, 6.2037114],
            [6.22816728, 6.05925153, 5.97002045, 5.88509436, 5.87109182,
             5.88509436, 5.97002045, 6.05925153, 6.22816728],
            [6.36657773, 6.1910207, 6.0610899, 5.96685295, 5.97487576,
             5.96685295, 6.0610899, 6.1910207, 6.36657773],
            [6.52336953, 6.37320723, 6.21411804, 6.17869829, 6.15460391,
             6.17869829, 6.21411804, 6.37320723, 6.52336953]]
    )

    np.testing.assert_allclose(
        rrup, dctx.rrup, rtol=0, atol=0.01)

    # Souce instance
    #  - Tectonic region: stable
    #  - Mech is RS

    source.setMechanism('RS')
    source._tectonic_region = 'Stable Shallow Crust'
    dists = Distance.fromSites(gmpe, source, site)
    dctx = dists.getDistanceContext()

    rjb = np.array(
        [[1.82771374, 1.52643611, 1.30046931, 1.16760601, 1.12277703,
          1.16760601, 1.30046931, 1.52643611, 1.82771374],
         [1.38246113, 1.06775489, 0.83169032, 0.69344486, 0.645892,
            0.69344486, 0.83169032, 1.06775489, 1.38246113],
            [1.05435251, 0.72652464, 0.48639585, 0.33959401, 0.2920419,
             0.33959401, 0.48639585, 0.72652464, 1.05435251],
            [0.84788911, 0.51540522, 0.27378007, 0.12450057, 0.07451822,
             0.12450057, 0.27378007, 0.51540522, 0.84788911],
            [0.78032378, 0.44695345, 0.20099496, 0.05123817, 0.,
             0.05123817, 0.20099496, 0.44695345, 0.78032378],
            [0.84804229, 0.51548687, 0.2738187, 0.12451008, 0.07451822,
             0.12451008, 0.2738187, 0.51548687, 0.84804229],
            [1.0546418, 0.72668956, 0.48646962, 0.33961424, 0.2920419,
             0.33961424, 0.48646962, 0.72668956, 1.0546418],
            [1.38289108, 1.06799812, 0.83180576, 0.6934727, 0.645892,
             0.6934727, 0.83180576, 1.06799812, 1.38289108]]
    )

    np.testing.assert_allclose(
        rjb, dctx.rjb, rtol=0, atol=0.01)

    rrup = np.array(
        [[6.73175745, 6.58096329, 6.42717448, 6.38198779, 6.36563579,
          6.38198779, 6.42717448, 6.58096329, 6.73175745],
         [6.48753618, 6.34187696, 6.18689857, 6.15537676, 6.13263668,
            6.15537676, 6.18689857, 6.34187696, 6.48753618],
            [6.33544657, 6.1667275, 6.04409157, 5.9555343, 5.96586226,
             5.9555343, 6.04409157, 6.1667275, 6.33544657],
            [6.20063145, 6.04113522, 5.96186983, 5.88482917, 5.87387645,
             5.88482917, 5.96186983, 6.04113522, 6.20063145],
            [6.17796118, 6.03838637, 5.91117189, 5.85034216, 0.,
             5.85034216, 5.91117189, 6.03838637, 6.17796118],
            [6.20075891, 6.04111884, 5.96188527, 5.88481675, 5.87387645,
             5.88481675, 5.96188527, 6.04111884, 6.20075891],
            [6.3355881, 6.16677535, 6.04409147, 5.9555607, 5.96586226,
             5.9555607, 6.04409147, 6.16677535, 6.3355881],
            [6.48783842, 6.34199129, 6.18699823, 6.15538781, 6.13263668,
             6.15538781, 6.18699823, 6.34199129, 6.48783842]]
    )

    np.testing.assert_allclose(
        rrup, dctx.rrup, rtol=0, atol=0.01)

    # Souce instance
    #  - Tectonic region: stable
    #  - Mech is NM

    source.setMechanism('NM')
    source._tectonic_region = 'Stable Shallow Crust'
    dists = Distance.fromSites(gmpe, source, site)
    dctx = dists.getDistanceContext()

    rjb = np.array(
        [[1.90163976, 1.59090426, 1.35772802, 1.21975997, 1.17315714,
          1.21975997, 1.35772802, 1.59090426, 1.90163976],
         [1.44234875, 1.11595412, 0.87047978, 0.72608143, 0.6763976,
            0.72608143, 0.87047978, 1.11595412, 1.44234875],
            [1.10201987, 0.7606424, 0.50965507, 0.35594865, 0.30612448,
             0.35594865, 0.50965507, 0.7606424, 1.10201987],
            [0.88732736, 0.5400275, 0.28698884, 0.13052973, 0.0781228,
             0.13052973, 0.28698884, 0.5400275, 0.88732736],
            [0.81684854, 0.46835876, 0.21071547, 0.05370847, 0.,
             0.05370847, 0.21071547, 0.46835876, 0.81684854],
            [0.88748668, 0.54011298, 0.28702933, 0.1305397, 0.0781228,
             0.1305397, 0.28702933, 0.54011298, 0.88748668],
            [1.10232064, 0.76081471, 0.50973231, 0.35596982, 0.30612448,
             0.35596982, 0.50973231, 0.76081471, 1.10232064],
            [1.44279245, 1.116207, 0.87059985, 0.72611052, 0.6763976,
             0.72611052, 0.87059985, 1.116207, 1.44279245]]
    )

    np.testing.assert_allclose(
        rjb, dctx.rjb, rtol=0, atol=0.01)

    rrup = np.array(
        [[6.51552789, 6.35105562, 6.18910077, 6.13678714, 6.11808742,
          6.13678714, 6.18910077, 6.35105562, 6.51552789],
         [6.25219098, 6.09163676, 5.92783781, 5.88815939, 5.8632258,
            5.88815939, 5.92783781, 6.09163676, 6.25219098],
            [6.08458377, 5.9012656, 5.76798442, 5.67370059, 5.68021406,
             5.67370059, 5.76798442, 5.9012656, 6.08458377],
            [5.9420166, 5.76720934, 5.67516593, 5.59187321, 5.57813972,
             5.59187321, 5.67516593, 5.76720934, 5.9420166],
            [5.91572554, 5.75981247, 5.62205686, 5.55427369, 0.,
             5.55427369, 5.62205686, 5.75981247, 5.91572554],
            [5.9421484, 5.76719951, 5.67518325, 5.59186206, 5.57813972,
             5.59186206, 5.67518325, 5.76719951, 5.9421484],
            [6.0847386, 5.90132265, 5.76798949, 5.67372705, 5.68021406,
             5.67372705, 5.76798949, 5.90132265, 6.0847386],
            [6.25250826, 6.09176251, 5.92794056, 5.88817184, 5.8632258,
             5.88817184, 5.92794056, 6.09176251, 6.25250826]]
    )

    np.testing.assert_allclose(
        rrup, dctx.rrup, rtol=0, atol=0.01)

    # Souce instance
    #  - Tectonic region: stable
    #  - Mech is SS

    source.setMechanism('SS')
    source._tectonic_region = 'Stable Shallow Crust'
    dists = Distance.fromSites(gmpe, source, site)
    dctx = dists.getDistanceContext()

    rjb = np.array(
        [[2.72873417, 2.37144886, 2.09954885, 1.92851977, 1.86995227,
          1.92851977, 2.09954885, 2.37144886, 2.72873417],
         [2.19864742, 1.7978181, 1.48474703, 1.28392745, 1.21396452,
            1.28392745, 1.48474703, 1.7978181, 2.19864742],
            [1.78020402, 1.3323943, 0.97123526, 0.72919945, 0.64364445,
             0.72919945, 0.97123526, 1.3323943, 1.78020402],
            [1.5064397, 1.01846151, 0.61039475, 0.3191157, 0.20751973,
             0.3191157, 0.61039475, 1.01846151, 1.5064397],
            [1.41089093, 0.90673813, 0.47477623, 0.15160737, 0.,
             0.15160737, 0.47477623, 0.90673813, 1.41089093],
            [1.50664467, 1.01859418, 0.61046522, 0.3191364, 0.20751973,
             0.3191364, 0.61046522, 1.01859418, 1.50664467],
            [1.7805844, 1.33263554, 0.97135557, 0.72923321, 0.64364445,
             0.72923321, 0.97135557, 1.33263554, 1.7805844],
            [2.1991657, 1.79813761, 1.48490175, 1.28396831, 1.21396452,
             1.28396831, 1.48490175, 1.79813761, 2.1991657]]
    )

    np.testing.assert_allclose(
        rjb, dctx.rjb, rtol=0, atol=0.01)

    rrup = np.array(
        [[5.84874652, 5.62706324, 5.42929028, 5.34392626, 5.31406828,
          5.34392626, 5.42929028, 5.62706324, 5.84874652],
         [5.50458234, 5.27459324, 5.06456452, 4.9821571, 4.94467172,
            4.9821571, 5.06456452, 5.27459324, 5.50458234],
            [5.26448475, 5.00483041, 4.80820132, 4.67381918, 4.65867853,
             4.67381918, 4.80820132, 5.00483041, 5.26448475],
            [5.08135199, 4.81923672, 4.64680257, 4.514164, 4.47986499,
             4.514164, 4.64680257, 4.81923672, 5.08135199],
            [5.03620112, 4.78581905, 4.57286829, 4.44979468, 0.,
             4.44979468, 4.57286829, 4.78581905, 5.03620112],
            [5.0815089, 4.81926166, 4.64683299, 4.51415954, 4.47986499,
             4.51415954, 4.64683299, 4.81926166, 5.0815089],
            [5.26470503, 5.00493679, 4.80823501, 4.67384847, 4.65867853,
             4.67384847, 4.80823501, 5.00493679, 5.26470503],
            [5.5049661, 5.27477487, 5.06468553, 4.98217732, 4.94467172,
             4.98217732, 5.06468553, 5.27477487, 5.5049661]]
    )

    np.testing.assert_allclose(
        rrup, dctx.rrup, rtol=0, atol=0.01)
Exemple #23
0
def _test():

    tmp, dbfile = tempfile.mkstemp()
    os.close(tmp)
    os.remove(dbfile)

    homedir = os.path.dirname(os.path.abspath(__file__))
    xmlfile = os.path.abspath(
        os.path.join(homedir, '..', 'data', 'eventdata', 'northridge',
                     'northridge_stations.xml'))
    stationfile = os.path.abspath(
        os.path.join(homedir, '..', 'data', 'eventdata', 'northridge',
                     'northridge_stations.db'))
    eventdict = {
        'lat': 34.213,
        'lon': -118.537,
        'depth': 18.2,
        'mag': 6.7,
        'time': datetime(1994, 1, 17, 12, 30, 55),
        'mech': 'ALL',
        'dip': 45,
        'rake': 90
    }

    try:
        print('Testing load from XML format...')
        t1 = time.time()
        stations1 = StationList.loadFromXML([xmlfile], dbfile)
        t2 = time.time()
        print('Passed load from XML format %i stations in %.2f seconds.' %
              (len(stations1), t2 - t1))

        print('Testing filling in distance and derived MMI/PGM values...')
        source = Source(eventdict)
        stations1.fillTables(source)
        print('Passed filling in distance and derived MMI/PGM values...')

        print('Testing retrieval of MMI data from StationList object...')
        t1 = time.time()
        mmidf1 = stations1.getMMIStations()
        t2 = time.time()
        print(
            'Passed retrieval of %i MMI data in %.2f seconds from StationList object.'
            % (len(mmidf1), t2 - t1))

        print(
            'Testing retrieval of instrumented data from StationList object...'
        )
        t1 = time.time()
        imtdf1 = stations1.getInstrumentedStations()
        t2 = time.time()
        print(
            'Passed retrieval of %i instrumented data in %.2f seconds from StationList object.'
            % (len(imtdf1), t2 - t1))

        print('Testing load from sqlite format...')
        t1 = time.time()
        stations2 = StationList(stationfile)
        t2 = time.time()
        print('Passed load from sqlite format %i stations in %.2f seconds.' %
              (len(stations1), t2 - t1))

        print('Testing retrieval of MMI data from StationList object...')
        t1 = time.time()
        mmidf2 = stations2.getMMIStations()
        t2 = time.time()
        print(
            'Passed retrieval of %i MMI data in %.2f seconds from StationList object.'
            % (len(mmidf2), t2 - t1))

        print(
            'Testing retrieval of instrumented data from StationList object...'
        )
        t1 = time.time()
        imtdf2 = stations2.getInstrumentedStations()
        t2 = time.time()
        print(
            'Passed retrieval of %i instrumented data in %.2f seconds from StationList object.'
            % (len(imtdf1), t2 - t1))

        assert (len(stations1) == len(stations2))

    except Exception as msg:
        print('Error caught: %s' % str(msg))
    if os.path.isfile(dbfile):
        os.remove(dbfile)