def test_slip(): # Rupture requires an origin even when not used: origin = Origin({ 'id': 'test', 'lon': 0, 'lat': 0, 'depth': 5.0, 'mag': 7.0, 'netid': 'us', 'network': '', 'locstring': '', 'time': HistoricTime.utcfromtimestamp(time.time()) }) # Make a rupture 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.]) rup = QuadRupture.fromTrace(lon0, lat0, lon1, lat1, z, W, dip, origin) slp = get_quad_slip(rup.getQuadrilaterals()[0], 30).getArray() slpd = np.array([0.80816457, 0.25350787, 0.53160491]) np.testing.assert_allclose(slp, slpd) slp = get_quad_strike_vector(rup.getQuadrilaterals()[0]).getArray() slpd = np.array([0.58311969, 0.27569625, 0.76417472]) np.testing.assert_allclose(slp, slpd) slp = get_quad_down_dip_vector(rup.getQuadrilaterals()[0]).getArray() slpd = np.array([0.81219873, -0.17763484, -0.55567895]) np.testing.assert_allclose(slp, slpd) slp = get_local_unit_slip_vector(22, 30, 86).getArray() slpd = np.array([0.82714003, 0.38830563, 0.49878203]) np.testing.assert_allclose(slp, slpd) slp = get_local_unit_slip_vector_DS(22, 30, -86).getArray() slpd = np.array([-0.80100879, -0.32362856, -0.49878203]) np.testing.assert_allclose(slp, slpd) slp = get_local_unit_slip_vector_SS(22, 80, 5).getArray() slpd = np.array([0.3731811, 0.92365564, 0.]) np.testing.assert_allclose(slp, slpd) mech = rake_to_mech(-160) assert mech == 'SS' mech = rake_to_mech(0) assert mech == 'SS' mech = rake_to_mech(160) assert mech == 'SS' mech = rake_to_mech(-80) assert mech == 'NM' mech = rake_to_mech(80) assert mech == 'RS'
def test_EdgeRupture_vs_QuadRupture(): # Sites stuff cx = -122.15 cy = 37.15 dx = 0.01 dy = 0.01 xspan = 1.5 yspan = 1.5 west = cx - xspan / 2.0 east = cx + xspan / 2.0 south = cy - yspan / 2.0 north = cy + yspan / 2.0 nx = np.ceil(((east - west - EPS) / dx) + 1) ny = np.ceil(((north - south - EPS) / dy) + 1) lats = np.linspace(north, south, ny) lons = np.linspace(west, east, nx) lon, lat = np.meshgrid(lons, lats) dep = np.zeros_like(lon) # Construct QuadRupture xp0 = np.array([-122.0, -122.5]) yp0 = np.array([37.1, 37.4]) xp1 = np.array([-121.7, -122.3]) yp1 = np.array([37.2, 37.2]) zp = np.array([0, 6]) widths = np.array([30, 20]) dips = np.array([30, 40]) origin = Origin({ 'lat': 33.15, 'lon': -122.15, 'depth': 0, 'mag': 7.2, 'id': '', 'netid': '', 'network': '', 'locstring': '', 'time': HistoricTime.utcfromtimestamp(time.time()) }) qrup = QuadRupture.fromTrace(xp0, yp0, xp1, yp1, zp, widths, dips, origin) rrup_q, _ = qrup.computeRrup(lon, lat, dep) rjb_q, _ = qrup.computeRjb(lon, lat, dep) # Construct equivalent EdgeRupture toplons = np.array([-122.0, -121.7, -122.5, -122.3]) toplats = np.array([37.1, 37.2, 37.4, 37.2]) topdeps = np.array([0, 0, 6, 6]) botlons = np.array([-121.886864, -121.587568, -122.635467, -122.435338]) botlats = np.array([36.884527, 36.984246, 37.314035, 37.114261]) botdeps = np.array([15.0000, 14.9998, 18.8558, 18.8559]) group_index = [0, 0, 1, 1] erup = EdgeRupture.fromArrays(toplons, toplats, topdeps, botlons, botlats, botdeps, origin, group_index) rrup_e, _ = erup.computeRrup(lon, lat, dep) rjb_e, _ = erup.computeRjb(lon, lat, dep) # Check that QuadRupture and EdgeRupture give the same result # (we check the absolute values of QuadRupture elsewhere) np.testing.assert_allclose(rrup_e, rrup_q, atol=0.35) np.testing.assert_allclose(rjb_e, rjb_q, atol=0.35)
def test_multisegment_discordant(): # The one thing that isn't check above is discordancy for segments # with multiple quads. For this, we need a synthetic example. x0 = np.array([0, 1, -1, 10, 9, 7]) y0 = np.array([0, 10, 20, 40, 35, 30]) z0 = np.array([0, 0, 0, 0, 0, 0]) x1 = np.array([1, -1, 0, 9, 7, 6]) y1 = np.array([10, 20, 30, 35, 30, 25]) z1 = np.array([0, 0, 0, 0, 0, 0]) x2 = np.array([3, 1, 2, 7, 5, 4]) y2 = np.array([10, 20, 30, 35, 30, 25]) z2 = np.array([10, 10, 10, 10, 10, 10]) x3 = np.array([2, 3, 1, 8, 7, 5]) y3 = np.array([0, 10, 20, 40, 35, 30]) z3 = np.array([10, 10, 10, 10, 10, 10]) epilat = 32.15270 epilon = -115.30500 proj = OrthographicProjection(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) # Make an Origin object; most of the 'event' values don't matter for # this example origin = Origin({ 'lat': 0, 'lon': 0, 'depth': 0, 'mag': 7.2, 'id': '', 'netid': '', 'network': '', 'locstring': '', 'time': HistoricTime.utcfromtimestamp(time.time()) }) rup = QuadRupture.fromVertices(lon0, lat0, z0, lon1, lat1, z1, lon2, lat2, z2, lon3, lat3, z3, origin, group_index=[0, 0, 0, 1, 1, 1]) # Sites buf = 0.25 lat = np.linspace(np.nanmin(rup.lats) - buf, np.nanmax(rup.lats) + buf, 20) lon = np.linspace(np.nanmin(rup.lons) - buf, np.nanmax(rup.lons) + buf, 20) lons, lats = np.meshgrid(lon, lat) dep = np.zeros_like(lons) x, y = proj(lon, lat) rupx, rupy = proj(rup.lons, rup.lats) # Calculate U and T dtypes = ['U', 'T'] dists = get_distance(dtypes, lats, lons, dep, rup) targetU = np.array( [[ -28.53228275, -28.36479713, -28.20139732, -28.0407734, -27.88135558, -27.72144153, -27.55935946, -27.39362017, -27.22300147, -27.04653062, -26.86338215, -26.67275638, -26.47381287, -26.26569449, -26.04762427, -25.81902477, -25.57961136, -25.32943282, -25.06885791, -24.79852214 ], [ -23.53750292, -23.3748086, -23.21793537, -23.06521934, -22.91449689, -22.76331684, -22.60928211, -22.45042208, -22.28542121, -22.11355532, -21.93435402, -21.74720475, -21.55115107, -21.34497916, -21.12749377, -20.89781118, -20.6555466, -20.40086149, -20.13439948, -19.85716145 ], [ -18.53499939, -18.37689929, -18.22732841, -18.08427516, -17.94468687, -17.80472632, -17.66045115, -17.50880802, -17.3484421, -17.17963435, -17.0032098, -16.81921732, -16.62638972, -16.42258419, -16.20564846, -15.9741218, -15.72753538, -15.4663671, -15.19180844, -14.9054813 ], [ -13.52283359, -13.36797542, -13.22589288, -13.09466537, -12.97028551, -12.84653536, -12.71591089, -12.57212088, -12.41335561, -12.24319318, -12.06681006, -11.88598424, -11.69798166, -11.49796348, -11.28169605, -11.04691388, -10.79343174, -10.52262594, -10.23677602, -9.93851158 ], [ -8.49936685, -8.34357094, -8.20650964, -8.08786858, -7.98403171, -7.88628837, -7.78005273, -7.64833307, -7.48359988, -7.29992491, -7.11862682, -6.94410189, -6.76618701, -6.5727842, -6.35634881, -6.11465447, -5.84925708, -5.56369035, -5.26212482, -4.94857454 ], [ -3.46638168, -3.30047216, -3.15914418, -3.04618465, -2.96252939, -2.90194067, -2.84436315, -2.75029014, -2.56983592, -2.33744275, -2.1512136, -1.99833104, -1.84066354, -1.6541107, -1.43071517, -1.17252753, -0.88592286, -0.57817222, -0.25582315, 0.07585567 ], [ 1.56416954, 1.75393848, 1.9183586, 2.04909316, 2.13723278, 2.17776584, 2.18272501, 2.20967639, 2.37405656, 2.65073289, 2.80205222, 2.90973407, 3.05124404, 3.2505182, 3.50336116, 3.7967575, 4.11742779, 4.45465822, 4.80070204, 5.15033407 ], [ 6.5633489, 6.78740885, 6.99419348, 7.17551069, 7.31963558, 7.4113505, 7.43666779, 7.40177458, 7.40517136, 7.58520044, 7.62013169, 7.71596777, 7.90558457, 8.17213015, 8.49008681, 8.83763176, 9.19937294, 9.56556659, 9.9305469, 10.29132309 ], [ 11.48996073, 11.74301446, 11.99016964, 12.22782156, 12.44984059, 12.6446727, 12.78798484, 12.82584849, 12.61992833, 12.26579742, 12.32166685, 12.54665462, 12.86628045, 13.23578462, 13.62571822, 14.01882924, 14.40617707, 14.78388296, 15.15089889, 15.5076165 ], [ 16.31383216, 16.57376544, 16.83189511, 17.08626411, 17.33309437, 17.56429108, 17.76005623, 17.85853532, 17.57101025, 17.32637346, 17.45075419, 17.77199513, 18.16933168, 18.58284635, 18.9891851, 19.37985879, 19.75324557, 20.11079653, 20.4549905, 20.78837053 ], [ 21.03975749, 21.28450315, 21.5243142, 21.75603974, 21.97469496, 22.17298057, 22.34310053, 22.49668569, 22.73940191, 22.70030633, 22.95351405, 23.35967832, 23.75891016, 24.14867803, 24.51536915, 24.85878249, 25.18398203, 25.49615514, 25.79932964, 26.09638269 ], [ 25.70484089, 25.92709225, 26.14280395, 26.35119497, 26.55363501, 26.75827099, 26.9915523, 27.31779086, 27.77993211, 27.71070831, 28.13624949, 28.723482, 29.25285078, 29.66404032, 30.00169474, 30.30044315, 30.57916576, 30.84804427, 31.1126134, 31.37586841 ], [ 30.35406633, 30.5585145, 30.75843356, 30.95627127, 31.15811912, 31.3763124, 31.63114968, 31.94156189, 32.23691802, 32.38759301, 32.86915665, 33.83467935, 34.46125278, 34.89905345, 35.25111257, 35.55095664, 35.82150686, 36.07720619, 36.32643896, 36.57385362 ], [ 35.0222379, 35.21734711, 35.41081942, 35.60589495, 35.80774808, 36.02313791, 36.25826988, 36.51619168, 36.81025966, 37.21777129, 37.86674108, 38.66578072, 39.25203723, 39.78060643, 40.20815617, 40.5606039, 40.86634527, 41.14457482, 41.40732554, 41.66197722 ], [ 39.73046099, 39.92514041, 40.12152415, 40.32316112, 40.5350467, 40.76393316, 41.01937758, 41.3172128, 41.68596492, 42.16604148, 42.77622755, 43.447503, 44.03771478, 44.55012468, 45.00551259, 45.40376857, 45.75505135, 46.07204699, 46.36554362, 46.64361367 ], [ 44.4876174, 44.68959464, 44.89710008, 45.11420443, 45.34646809, 45.60143197, 45.88932906, 46.22363997, 46.61975585, 47.0884227, 47.62307543, 48.1913408, 48.74937117, 49.26945799, 49.74327902, 50.17123158, 50.55810895, 50.91098842, 51.23731582, 51.54375617 ], [ 49.29279265, 49.50696882, 49.73006999, 49.96625305, 50.22080319, 50.50022572, 50.81209441, 51.1642666, 51.56290694, 52.00913021, 52.49553006, 53.00565389, 53.51861282, 54.01614414, 54.48672101, 54.9254339, 55.33212663, 55.70951516, 56.06170563, 56.39317058 ], [ 54.13906629, 54.3671694, 54.60643024, 54.86053563, 55.13377911, 55.43088558, 55.75658576, 56.1148189, 56.50752978, 56.93329478, 57.38640012, 57.85715119, 58.33367994, 58.80451404, 59.26065475, 59.69644542, 60.10938419, 60.49940252, 60.86803179, 61.21767916 ], [ 59.01741908, 59.25887491, 59.51248349, 59.78119592, 60.06816694, 60.37651862, 60.70895927, 61.0672529, 61.45160192, 61.86010542, 62.28853397, 62.73062937, 63.17894547, 63.62598375, 64.06523791, 64.49185106, 64.90281064, 65.2967858, 65.67377362, 66.03469546 ], [ 63.9193099, 64.17236414, 64.4376317, 64.71732366, 65.01362255, 65.32847988, 65.66334836, 66.0188704, 66.39457546, 66.7886684, 67.19800022, 67.61828012, 68.04451487, 68.47157851, 68.89476917, 69.31022713, 69.71515194, 70.10782673, 70.4875021, 70.85420436 ]]) np.testing.assert_allclose(targetU, dists['U'], atol=0.01) targetT = np.array([ [ -2.27427469e+01, -1.97498544e+01, -1.67512900e+01, -1.37464632e+01, -1.07350712e+01, -7.71715083e+00, -4.69305811e+00, -1.66336318e+00, 1.37131605e+00, 4.41047613e+00, 7.45381136e+00, 1.05011799e+01, 1.35524779e+01, 1.66074913e+01, 1.96657949e+01, 2.27267294e+01, 2.57894503e+01, 2.88530154e+01, 3.19164798e+01, 3.49789747e+01 ], [ -2.30778766e+01, -2.00896906e+01, -1.70950973e+01, -1.40931667e+01, -1.10834219e+01, -8.06600712e+00, -5.04171582e+00, -2.01179123e+00, 1.02248614e+00, 4.06025218e+00, 7.10129626e+00, 1.01459367e+01, 1.31946312e+01, 1.62475702e+01, 1.93044511e+01, 2.23644788e+01, 2.54265185e+01, 2.84892997e+01, 3.15515954e+01, 3.46123426e+01 ], [ -2.33971472e+01, -2.04144525e+01, -1.74245193e+01, -1.44256870e+01, -1.14169177e+01, -8.39830615e+00, -5.37141115e+00, -2.33902937e+00, 6.95823925e-01, 3.73133431e+00, 6.76769593e+00, 9.80663091e+00, 1.28500821e+01, 1.58991008e+01, 1.89534737e+01, 2.20119662e+01, 2.50728111e+01, 2.81341606e+01, 3.11943854e+01, 3.42522163e+01 ], [ -2.36965870e+01, -2.07206976e+01, -1.77370901e+01, -1.47426715e+01, -1.17347885e+01, -8.71247709e+00, -5.67801094e+00, -2.63761285e+00, 4.00625914e-01, 3.43182302e+00, 6.45782532e+00, 9.48491128e+00, 1.25187545e+01, 1.55616657e+01, 1.86127822e+01, 2.16694756e+01, 2.47286680e+01, 2.77876297e+01, 3.08443066e+01, 3.38973527e+01 ], [ -2.39698399e+01, -2.10022612e+01, -1.80281475e+01, -1.50423801e+01, -1.20388157e+01, -9.01204040e+00, -5.96160398e+00, -2.89867328e+00, 1.52194374e-01, 3.17268218e+00, 6.17334725e+00, 9.17699572e+00, 1.21964990e+01, 1.52330975e+01, 1.82821226e+01, 2.13375815e+01, 2.43943933e+01, 2.74490375e+01, 3.04994435e+01, 3.35446330e+01 ], [ -2.42070742e+01, -2.12471979e+01, -1.82855675e+01, -1.53163304e+01, -1.23296744e+01, -9.31127857e+00, -6.24535210e+00, -3.12882361e+00, -2.24460581e-02, 2.95354485e+00, 5.89215412e+00, 8.86387424e+00, 1.18748249e+01, 1.49128245e+01, 1.79640055e+01, 2.10182501e+01, 2.40696313e+01, 2.71153177e+01, 3.01543919e+01, 3.31869788e+01 ], [ -2.43971375e+01, -2.14368866e+01, -1.84826148e+01, -1.55321207e+01, -1.25786621e+01, -9.60654678e+00, -6.58612151e+00, -3.48118311e+00, -3.16555025e-01, 2.61618307e+00, 5.53740540e+00, 8.52666510e+00, 1.15623361e+01, 1.46149780e+01, 1.76674294e+01, 2.07125025e+01, 2.37483764e+01, 2.67756033e+01, 2.97955606e+01, 3.28097430e+01 ], [ -2.45384925e+01, -2.15583842e+01, -1.85874288e+01, -1.56290738e+01, -1.26867853e+01, -9.76140655e+00, -6.84407754e+00, -3.90089971e+00, -8.41806596e-01, 2.14754495e+00, 5.18583472e+00, 8.26271822e+00, 1.13266091e+01, 1.43684333e+01, 1.73916223e+01, 2.04017469e+01, 2.34034936e+01, 2.64002111e+01, 2.93941282e+01, 3.23866586e+01 ], [ -2.46576775e+01, -2.16355610e+01, -1.86129545e+01, -1.55919156e+01, -1.25763765e+01, -9.57306672e+00, -6.59044329e+00, -3.62352541e+00, -5.92041388e-01, 2.33255341e+00, 5.29498494e+00, 8.24834463e+00, 1.11833819e+01, 1.41167617e+01, 1.70571082e+01, 2.00065102e+01, 2.29645946e+01, 2.59302937e+01, 2.89023967e+01, 3.18797332e+01 ], [ -2.48161623e+01, -2.17489533e+01, -1.86651328e+01, -1.55589864e+01, -1.24224388e+01, -9.24466730e+00, -6.01521475e+00, -2.75148770e+00, 3.89519039e-01, 2.99589525e+00, 5.45696689e+00, 8.01247078e+00, 1.07291540e+01, 1.35565782e+01, 1.64461360e+01, 1.93723515e+01, 2.23222250e+01, 2.52881249e+01, 2.82650171e+01, 3.12494172e+01 ], [ -2.50857405e+01, -2.20002811e+01, -1.88926336e+01, -1.57550887e+01, -1.25770789e+01, -9.34497451e+00, -6.04430316e+00, -2.67290100e+00, 5.40854953e-01, 2.30509492e+00, 3.58183843e+00, 6.23701436e+00, 9.28727128e+00, 1.23205706e+01, 1.53428945e+01, 1.83666035e+01, 2.13934954e+01, 2.44218171e+01, 2.74496472e+01, 3.04757209e+01 ], [ -2.55082697e+01, -2.24454912e+01, -1.93710045e+01, -1.62824768e+01, -1.31767102e+01, -1.00469827e+01, -6.86985653e+00, -3.54681638e+00, 1.07062999e-01, 3.34891657e-01, -1.70694750e-01, 3.57896940e+00, 7.17013928e+00, 1.05232789e+01, 1.37976070e+01, 1.70230221e+01, 2.02076136e+01, 2.33576919e+01, 2.64794914e+01, 2.95785985e+01 ], [ -2.60778515e+01, -2.30695744e+01, -2.00684150e+01, -1.70790651e+01, -1.41074315e+01, -1.11587507e+01, -8.23273307e+00, -5.33306966e+00, -2.80144302e+00, -1.84760416e+00, -1.05368779e+00, 1.26163211e+00, 4.90086292e+00, 8.53883059e+00, 1.20996577e+01, 1.55589098e+01, 1.89237978e+01, 2.22114952e+01, 2.54390313e+01, 2.86203790e+01 ], [ -2.67537229e+01, -2.38123298e+01, -2.08964272e+01, -1.80168638e+01, -1.51896230e+01, -1.24401995e+01, -9.81536176e+00, -7.41008520e+00, -5.38073414e+00, -3.78262975e+00, -2.29669890e+00, -3.53057240e-01, 3.13642477e+00, 6.97021789e+00, 1.07026969e+01, 1.42945488e+01, 1.77655640e+01, 2.11406146e+01, 2.44409375e+01, 2.76832489e+01 ], [ -2.74832153e+01, -2.46028623e+01, -2.17593854e+01, -1.89653378e+01, -1.62381218e+01, -1.36022404e+01, -1.10914555e+01, -8.74572618e+00, -6.58963863e+00, -4.58336507e+00, -2.57607747e+00, -2.67233150e-01, 2.82788692e+00, 6.36737407e+00, 9.93334021e+00, 1.34440609e+01, 1.68846862e+01, 2.02577163e+01, 2.35710668e+01, 2.68337230e+01 ], [ -2.82199728e+01, -2.53838486e+01, -2.25869234e+01, -1.98388981e+01, -1.71512612e+01, -1.45365893e+01, -1.20059297e+01, -9.56220853e+00, -7.18799023e+00, -4.83006994e+00, -2.39120744e+00, 2.51308627e-01, 3.17331949e+00, 6.33022626e+00, 9.61428455e+00, 1.29426788e+01, 1.62705993e+01, 1.95770961e+01, 2.28539081e+01, 2.60992146e+01 ], [ -2.89332200e+01, -2.61222251e+01, -2.33461951e+01, -2.06105222e+01, -1.79200485e+01, -1.52776479e+01, -1.26817390e+01, -1.01225741e+01, -7.57801870e+00, -5.01122873e+00, -2.37434916e+00, 3.78303328e-01, 3.27093827e+00, 6.29527723e+00, 9.41912399e+00, 1.26046423e+01, 1.58204753e+01, 1.90448689e+01, 2.22643265e+01, 2.54712657e+01 ], [ -2.96082809e+01, -2.68067500e+01, -2.40331802e+01, -2.12894659e+01, -1.85760763e+01, -1.58910124e+01, -1.32284735e+01, -1.05774862e+01, -7.92111801e+00, -5.23724922e+00, -2.50179068e+00, 3.05790568e-01, 3.19666197e+00, 6.16975035e+00, 9.21353475e+00, 1.23109519e+01, 1.54443017e+01, 1.85982825e+01, 2.17611016e+01, 2.49243957e+01 ], [ -3.02420504e+01, -2.74395034e+01, -2.46578094e+01, -2.18967353e+01, -1.91546206e+01, -1.64278219e+01, -1.37101816e+01, -1.09927140e+01, -8.26378719e+00, -5.51007519e+00, -2.71836365e+00, 1.22111758e-01, 3.01754598e+00, 5.96851903e+00, 8.97043180e+00, 1.20150997e+01, 1.50927299e+01, 1.81935916e+01, 2.13090724e+01, 2.44321477e+01 ], [ -3.08377073e+01, -2.80281994e+01, -2.52335334e+01, -2.24524366e+01, -1.96825082e+01, -1.69199811e+01, -1.41595768e+01, -1.13945492e+01, -8.61701306e+00, -5.81861191e+00, -2.99148017e+00, -1.29317230e-01, 2.77170749e+00, 5.71249079e+00, 8.69110042e+00, 1.17033684e+01, 1.47437429e+01, 1.78061436e+01, 2.08846464e+01, 2.39739290e+01 ] ]) np.testing.assert_allclose(targetT, dists['T'], atol=0.01)
def test_origin(): 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" time="2008-05-12T06:28:01Z" depth="19.0" netid="us" network="" locstring="EASTERN SICHUAN, CHINA" mech="" />""" source_text = "mech=RS" ffile = io.StringIO(fault_text) # noqa efile = io.StringIO(event_text) sfile = io.StringIO(source_text) origin = Origin.fromFile(efile, sourcefile=sfile) testdict = { 'mag': 7.9, 'id': '2008ryan', 'locstring': 'EASTERN SICHUAN, CHINA', 'mech': 'RS', 'lon': 103.3639, 'lat': 30.9858, 'depth': 19.0 } for key in testdict.keys(): value = eval(f'origin.{key}') if type(value) is str: assert testdict[key] == value if type(value) is float: np.testing.assert_almost_equal(testdict[key], value) assert origin.mech == "RS" origin.setMechanism("SS") assert origin.mech == "SS" origin.setMechanism("SS", rake=10) assert origin.mech == "SS" assert origin.rake == 10.0 assert origin.dip == 90.0 origin.setMechanism("SS", rake=-350) assert origin.rake == 10.0 origin.setMechanism("SS", rake=370) assert origin.rake == 10.0 with pytest.raises(Exception) as a: origin.setMechanism("SS", dip=100) with pytest.raises(Exception) as a: origin.setMechanism("Strike slip") # Rake too large with pytest.raises(Exception) as a: origin.setMechanism("SS", rake=1111) # Lat is greater than 90 with pytest.raises(Exception) as a: event_text = """<?xml version="1.0" encoding="US-ASCII" standalone="yes"?> <earthquake id="2008" lat="91.9858" lon="103.3639" mag="7.9" time="2008-05-12T06:28:01Z" timezone="GMT" depth="19.0" locstring="EASTERN SICHUAN, CHINA" created="1211173621" otime="1210573681" mech="" netid="us" network=""/>""" efile = io.StringIO(event_text) origin = Origin.fromFile(efile) # Lon is greater than 180 with pytest.raises(Exception) as a: event_text = """<?xml version="1.0" encoding="US-ASCII" standalone="yes"?> <earthquake id="2008" lat="31.9858" lon="183.3639" mag="7.9" time="2008-05-12T06:28:01Z" timezone="GMT" depth="19.0" locstring="EASTERN SICHUAN, CHINA" created="1211173621" otime="1210573681" type="" netid="us" network=""/>""" efile = io.StringIO(event_text) origin = Origin.fromFile(efile) # No event id with pytest.raises(Exception) as a: event_text = """<?xml version="1.0" encoding="US-ASCII" standalone="yes"?> <earthquake lat="30.9858" lon="103.3639" mag="7.9" time="2008-05-12T06:28:01Z" timezone="GMT" depth="19.0" locstring="EASTERN SICHUAN, CHINA" created="1211173621" otime="1210573681" type="" netid="us" network=""/>""" efile = io.StringIO(event_text) origin = Origin.fromFile(efile) # Put mech in event keys event_text = """<?xml version="1.0" encoding="US-ASCII" standalone="yes"?> <earthquake id="2008" lat="30.9858" lon="103.3639" mag="7.9" time="2008-05-12T06:28:01Z" depth="19.0" locstring="EASTERN SICHUAN, CHINA" created="1211173621" otime="1210573681" type="" mech="SS" netid="us" network=""/>""" efile = io.StringIO(event_text) origin = Origin.fromFile(efile) assert origin.mech == 'SS' # Empty mech event_text = """<?xml version="1.0" encoding="US-ASCII" standalone="yes"?> <earthquake id="2008" lat="30.9858" lon="103.3639" mag="7.9" time="2008-05-12T06:28:01Z" depth="19.0" locstring="EASTERN SICHUAN, CHINA" created="1211173621" otime="1210573681" type="" mech="" netid="us" network=""/>""" efile = io.StringIO(event_text) origin = Origin.fromFile(efile) assert origin.mech == 'ALL' # Mech not acceptable value with pytest.raises(Exception) as a: # noqa event_text = """<?xml version="1.0" encoding="US-ASCII" standalone="yes"?> <earthquake id="2008" lat="31.9858" lon="103.3639" mag="7.9" time="2008-05-12T06:28:01Z" depth="19.0" locstring="EASTERN SICHUAN, CHINA" created="1211173621" otime="1210573681" type="" mech="Strike slip" netid="us" network=""/>""" efile = io.StringIO(event_text) origin = Origin.fromFile(efile) # Missing keys with pytest.raises(KeyError): event_text = """<?xml version="1.0" encoding="US-ASCII" standalone="yes"?> <earthquake id="2008" mag="7.9" time="2008-05-12T06:28:01Z" depth="19.0" locstring="EASTERN SICHUAN, CHINA" created="1211173621" otime="1210573681" type="" network=""/>""" efile = io.StringIO(event_text) origin = Origin.fromFile(efile) # Use "type" instead of "mech" event_text = """<?xml version="1.0" encoding="US-ASCII" standalone="yes"?> <earthquake id="2008ryan" lat="30.9858" lon="103.3639" mag="7.9" time="2008-05-12T06:28:01Z" depth="19.0" netid="us" network="" locstring="EASTERN SICHUAN, CHINA" type="RS" />""" efile = io.StringIO(event_text) origin = Origin.fromFile(efile) assert origin.mech == 'RS' # No rake or mech event_text = """<?xml version="1.0" encoding="US-ASCII" standalone="yes"?> <earthquake id="2008ryan" lat="30.9858" lon="103.3639" mag="7.9" time="2008-05-12T06:28:01Z" depth="19.0" netid="us" network="" locstring="EASTERN SICHUAN, CHINA" reference="Smith, et al. (2019)" />""" efile = io.StringIO(event_text) origin = Origin.fromFile(efile) assert origin.rake == 0.0 hypo = origin.getHypo() assert hypo.x == origin.lon assert hypo.y == origin.lat assert hypo.z == origin.depth # Write the origin to a file event = {} event['id'] = 'us2000ryan' event['netid'] = 'us' event['network'] = 'USGS Network' event['lat'] = 30.9858 event['lon'] = 103.3639 event['depth'] = 19.0 event['mag'] = 7.9 event['time'] = HistoricTime.strptime('2008-05-12T06:28:01.0Z', constants.TIMEFMT) event['locstring'] = "EASTERN SICHUAN, CHINA" event['mech'] = 'RS' event['reference'] = "Smith, et al. (2019)" event['productcode'] = "us2000ryan" tfile = tempfile.NamedTemporaryFile() xmlfile = tfile.name tfile.close() res = write_event_file(event, xmlfile) with open(xmlfile, 'r') as f: fstr = f.read() target_str = ( '<earthquake id="us2000ryan" netid="us" network="USGS Network" lat="30.9858" lon="103.3639" depth="19.0" mag="7.9" time="2008-05-12T06:28:01Z" locstring="EASTERN SICHUAN, CHINA" mech="RS" reference="Smith, et al. (2019)" productcode="us2000ryan" event_type="ACTUAL"/>' ) assert target_str in fstr if res is not None: print(res) assert False origin = Origin.fromFile(xmlfile) os.remove(xmlfile) assert origin.id == event['id'] assert origin.netid == event['netid'] assert origin.network == event['network'] assert origin.time == event['time']
def _event_station_metrics(self, event): self.eventid = event.id logging.info('Computing station metrics for event %s...' % self.eventid) event_dir = os.path.join(self.gmrecords.data_path, self.eventid) workname = os.path.join(event_dir, WORKSPACE_NAME) if not os.path.isfile(workname): logging.info( 'No workspace file found for event %s. Please run ' 'subcommand \'assemble\' to generate workspace file.' % self.eventid) logging.info('Continuing to next event.') return event.id self.workspace = StreamWorkspace.open(workname) self._get_pstreams() rupture_file = get_rupture_file(event_dir) origin = Origin({ 'id': self.eventid, 'netid': '', 'network': '', 'lat': event.latitude, 'lon': event.longitude, 'depth': event.depth_km, 'locstring': '', 'mag': event.magnitude, 'time': event.time }) rupture = get_rupture(origin, rupture_file) if not hasattr(self, 'pstreams'): logging.info('No processed waveforms available. No station ' 'metrics computed.') self.workspace.close() return for stream in self.pstreams: logging.info('Calculating station metrics for %s...' % stream.get_id()) summary = StationSummary.from_config(stream, event=event, config=self.gmrecords.conf, calc_waveform_metrics=False, calc_station_metrics=True, rupture=rupture, vs30_grids=self.vs30_grids) xmlstr = summary.get_station_xml() metricpath = '/'.join([ format_netsta(stream[0].stats), format_nslit(stream[0].stats, stream.get_inst(), self.eventid) ]) self.workspace.insert_aux(xmlstr, 'StationMetrics', metricpath, overwrite=self.gmrecords.args.overwrite) logging.info('Added station metrics to workspace files ' 'with tag \'%s\'.' % self.gmrecords.args.label) self.workspace.close() return event.id
def test_incorrect(): # Number of points in polyon is even rupture_text = """# Source: Ji, C., D. V. Helmberger, D. J. Wald, and \ K.-F. Ma (2003). Slip history and dynamic implications of the 1999 Chi-Chi, \ Taiwan, earthquake, J. Geophys. Res. 108, 2412, doi:10.1029/2002JB001764. 120.72300 24.27980 0 121.00000 24.05000 17 121.09300 24.07190 17 121.04300 24.33120 17 121.04300 24.33120 17 120.72300 24.27980 0 > 120.72300 24.27980 0 120.68000 23.70000 0 120.97200 23.60400 17 121.00000 24.05000 17 120.72300 24.27980 0 > 120.97200 23.60400 17 120.68000 23.70000 0 120.58600 23.58850 0 120.78900 23.40240 17 120.97200 23.60400 17""" # noqa # Rupture requires an origin even when not used: origin = Origin({ 'id': 'test', 'lon': 0, 'lat': 0, 'depth': 5.0, 'mag': 7.0, 'netid': 'us', 'network': '', 'locstring': '', 'time': HistoricTime.utcfromtimestamp(time.time()) }) cbuf = io.StringIO(rupture_text) with pytest.raises(Exception): get_rupture(origin, cbuf) # Top points must be first rupture_text = """# Test 120.72300 24.27980 0 121.00000 24.05000 17 121.09300 24.07190 17 121.04300 24.33120 17 120.72300 24.27980 0""" # noqa cbuf = io.StringIO(rupture_text) with pytest.raises(Exception): get_rupture(origin, cbuf) # Wrong order of lat/lon rupture_text = """# Test -118.421 34.315 5.000 -118.587 34.401 5.000 -118.693 34.261 20.427 -118.527 34.175 20.427 -118.421 34.315 5.000 """ # noqa cbuf = io.StringIO(rupture_text) with pytest.raises(Exception): get_rupture(origin, cbuf, new_format=False) # Wrong order of lat/lon rupture_text = """# Test 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 """ # noqa cbuf = io.StringIO(rupture_text) with pytest.raises(Exception): get_rupture(origin, cbuf, new_format=True) # Unclosed segments rupture_text = """# Test 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.6 5.000 """ # noqa cbuf = io.StringIO(rupture_text) with pytest.raises(Exception): get_rupture(origin, cbuf, new_format=False) # incorrect delimiter rupture_text = """#Test 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 """ # noqa cbuf = io.StringIO(rupture_text) with pytest.raises(Exception): get_rupture(origin, cbuf, new_format=False) # incorrect delimiter, new format rupture_text = """#Test 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 """ # noqa cbuf = io.StringIO(rupture_text) with pytest.raises(Exception): get_rupture(origin, cbuf, new_format=True) # Not 3 columns rupture_text = """#Test 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 """ # noqa cbuf = io.StringIO(rupture_text) with pytest.raises(Exception): get_rupture(origin, cbuf, new_format=False) # Json incorrect test = { "metadata": { "id": "test", "mag": 7.0, "lon": 0, "mech": "ALL", "depth": 5.0, "time": "2018-07-02T22:50:03Z", "netid": "us", "rake": 0.0, "lat": 0, "network": "", "locstring": "", "reference": "Test" }, "features": [{ "type": "Feature", "geometry": { "coordinates": [[[[-118.421, 34.315, 5.0], [-118.587, 34.401, 5.0], [-118.693, 34.261, 20.427], [-118.527, 34.175, 20.427], [-118.421, 34.315, 5.0]]]], "type": "MultiPolygon" }, "properties": { "rupture type": "rupture extent" } }], "type": "FeatureCollection" } # incorrect type test_incorrect = copy.deepcopy(test) test_incorrect['type'] = 'Feature' with pytest.raises(Exception) as e: validate_json(test_incorrect) print(str(e)) # Incorrect number of features test_incorrect = copy.deepcopy(test) test_incorrect['features'].append(['wrong']) with pytest.raises(Exception) as e: validate_json(test_incorrect) print(str(e)) # no reference test_incorrect = copy.deepcopy(test) test_incorrect['metadata'].pop('reference', None) with pytest.raises(Exception) as e: validate_json(test_incorrect) print(str(e)) # incorrect feature type test_incorrect = copy.deepcopy(test) test_incorrect['features'][0]['type'] = 'fred' with pytest.raises(Exception) as e: validate_json(test_incorrect) print(str(e)) # incorrect feature geometry type test_incorrect = copy.deepcopy(test) test_incorrect['features'][0]['geometry']['type'] = 'fred' with pytest.raises(Exception) as e: validate_json(test_incorrect) print(str(e)) # no coordinates test_incorrect = copy.deepcopy(test) test_incorrect['features'][0]['geometry'].pop('coordinates', None) with pytest.raises(Exception) as e: validate_json(test_incorrect) print(str(e))
def test_parse_complicated_rupture(): rupture_text = """# SOURCE: Barka, A., H. S. Akyz, E. Altunel, G. Sunal, \ Z. Akir, A. Dikbas, B. Yerli, R. Armijo, B. Meyer, J. B. d. Chabalier, \ T. Rockwell, J. R. Dolan, R. Hartleb, T. Dawson, S. Christofferson, \ A. Tucker, T. Fumal, R. Langridge, H. Stenner, W. Lettis, J. Bachhuber, \ and W. Page (2002). The Surface Rupture and Slip Distribution of the \ 17 August 1999 Izmit Earthquake (M 7.4), North Anatolian Fault, Bull. \ Seism. Soc. Am. 92, 43-60. 29.33760 40.70985 0 29.51528 40.72733 0 29.51528 40.72933 20 29.33760 40.71185 20 29.33760 40.70985 0 > 29.61152 40.70513 0 29.87519 40.74903 0 29.87519 40.75103 20 29.61152 40.70713 20 29.61152 40.70513 0 > 29.88662 40.72582 0 30.11126 40.72336 0 30.19265 40.73432 0 30.19265 40.73632 20 30.11126 40.72536 20 29.88662 40.72782 20 29.88662 40.72582 0 > 30.30494 40.71210 0 30.46540 40.71081 0 30.56511 40.70739 0 30.56511 40.70939 20 30.46540 40.71281 20 30.30494 40.71410 20 30.30494 40.71210 0 > 30.57658 40.71621 0 30.63731 40.70068 0 30.63731 40.70268 20 30.57658 40.71821 20 30.57658 40.71621 0 > 30.72900 40.69947 0 30.93655 40.79654 0 30.93655 40.79854 20 30.72900 40.70147 20 30.72900 40.69947 0 > 30.94688 40.80199 0 31.01799 40.84501 0 31.01799 40.84701 20 30.94688 40.80399 20 30.94688 40.80199 0""" # noqa # Rupture requires an origin even when not used: origin = Origin({ 'id': 'test', 'lon': 0, 'lat': 0, 'depth': 5.0, 'mag': 7.0, 'netid': 'us', 'network': '', 'locstring': '', 'time': HistoricTime.utcfromtimestamp(time.time()) }) cbuf = io.StringIO(rupture_text) rupture = get_rupture(origin, cbuf) strike = rupture.getStrike() np.testing.assert_allclose(strike, -100.46, atol=0.01) dip = rupture.getDip() np.testing.assert_allclose(dip, 89.40, atol=0.01) L = rupture.getLength() np.testing.assert_allclose(L, 119.56, atol=0.01) W = rupture.getWidth() np.testing.assert_allclose(W, 20.0, atol=0.01) nq = rupture.getNumQuads() np.testing.assert_allclose(nq, 9) ng = rupture.getNumGroups() np.testing.assert_allclose(ng, 7) sind = rupture._getGroupIndex() np.testing.assert_allclose(sind, [0, 1, 2, 2, 3, 3, 4, 5, 6]) ztor = rupture.getDepthToTop() np.testing.assert_allclose(ztor, 0, atol=0.01) itl = rupture.getIndividualTopLengths() itl_d = np.array([ 15.13750778, 22.80237887, 18.98053425, 6.98263853, 13.55978731, 8.43444811, 5.41399812, 20.57788056, 7.66869463 ]) np.testing.assert_allclose(itl, itl_d, atol=0.01) iw = rupture.getIndividualWidths() iw_d = np.array([ 20.00122876, 20.00122608, 20.00120173, 20.00121028, 20.00121513, 20.00121568, 20.00107293, 20.00105498, 20.00083348 ]) np.testing.assert_allclose(iw, iw_d, atol=0.01) lats = rupture.lats lats_d = np.array([ 40.72733, 40.70985, 40.71185, 40.72932969, 40.72733, np.nan, 40.74903, 40.70513, 40.70713, 40.75102924, 40.74903, np.nan, 40.72336, 40.72582, 40.72336, 40.72536, 40.72782, 40.72536004, 40.72336, np.nan, 40.71081, 40.7121, 40.71081, 40.71281, 40.7141, 40.71281002, 40.71081, np.nan, 40.70068, 40.71621, 40.71821, 40.70268025, 40.70068, np.nan, 40.79654, 40.69947, 40.70147, 40.79853872, 40.79654, np.nan, 40.84501, 40.80199, 40.80399, 40.84700952, 40.84501, np.nan ]) np.testing.assert_allclose(lats, lats_d, atol=0.001) lons = rupture.lons lons_d = np.array([ 29.51528, 29.3376, 29.3376, 29.51528005, 29.51528, np.nan, 29.87519, 29.61152, 29.61152, 29.87519021, 29.87519, np.nan, 30.11126, 29.88662, 30.11126, 30.11126, 29.88662, 30.11126, 30.11126, np.nan, 30.4654, 30.30494, 30.4654, 30.4654, 30.30494, 30.4654, 30.4654, np.nan, 30.63731, 30.57658, 30.57658, 30.63731011, 30.63731, np.nan, 30.93655, 30.729, 30.729, 30.93655103, 30.93655, np.nan, 31.01799, 30.94688, 30.94688, 31.0179905, 31.01799, np.nan ]) np.testing.assert_allclose(lons, lons_d, atol=0.001)
def test_fromTrace(): xp0 = [0.0] xp1 = [0.0] yp0 = [0.0] yp1 = [0.05] zp = [0.0] widths = [10.0] dips = [45.0] # Rupture requires an origin even when not used: origin = Origin({ 'id': 'test', 'lon': -121.81529, 'lat': 37.73707, 'depth': 5.0, 'mag': 7.0, 'netid': 'us', 'network': '', 'locstring': '', 'time': HistoricTime.utcfromtimestamp(time.time()) }) # Error: unequal array lengths with pytest.raises(Exception) as e: rupture = QuadRupture.fromTrace( xp0, yp0, xp1, yp1, zp[:-1], widths, dips, origin, reference='From J Smith, (personal communication)') print(str(e)) # Error: invalid strike with pytest.raises(Exception) as e: rupture = QuadRupture.fromTrace( xp0, yp0, xp1, yp1, zp, widths, dips, origin, strike=[236.0, 250.0], reference='From J Smith, (personal communication)') print(str(e)) # TODO: These write tests exercise code, but we don't check the results rupture = QuadRupture.fromTrace( xp0, yp0, xp1, yp1, zp, widths, dips, origin, reference='From J Smith, (personal communication)') fstr = io.StringIO() rupture.writeTextFile(fstr) tfile = tempfile.NamedTemporaryFile() tname = tfile.name tfile.close() rupture.writeTextFile(tname) os.remove(tname) tfile = tempfile.NamedTemporaryFile() tname = tfile.name tfile.close() rupture.writeGeoJson(tname) os.remove(tname) xp0 = [-121.81529, -121.82298] xp1 = [-121.82298, -121.83068] yp0 = [37.73707, 37.74233] yp1 = [37.74233, 37.74758] zp = [10, 15] widths = [15.0, 20.0] dips = [30.0, 45.0] rupture = QuadRupture.fromTrace( xp0, yp0, xp1, yp1, zp, widths, dips, origin, reference='From J Smith, (personal communication)') assert rupture.getReference() == 'From J Smith, (personal communication)' rorigin = rupture.getOrigin() assert rorigin.id == origin.id assert rorigin.mag == origin.mag assert rorigin.depth == origin.depth rx = rupture.getRuptureContext([]) np.testing.assert_allclose([rx.strike, rx.dip, rx.ztor, rx.width], [ -49.183708644954905, 37.638322472702534, 9.999999999371358, 17.47024205615428 ]) rhyp = rupture.computeRhyp(np.array([-121.5]), np.array([37.0]), np.array([0])) repi = rupture.computeRepi(np.array([-121.5]), np.array([37.0]), np.array([0])) np.testing.assert_allclose([rhyp[0], repi[0]], [86.709236, 86.564956])
def test_QuadRupture(): # Rupture requires an origin even when not used: origin = Origin({ 'id': 'test', 'lon': 0, 'lat': 0, 'depth': 5.0, 'mag': 7.0, 'netid': 'us', 'network': '', 'locstring': '', 'time': HistoricTime.utcfromtimestamp(time.time()) }) # First with json file file = os.path.join(homedir, 'rupture_data/izmit.json') rupj = get_rupture(origin, file) # Then with text file: file = os.path.join(homedir, 'rupture_data/Barkaetal02_fault.txt') rupt = get_rupture(origin, file) np.testing.assert_allclose(rupj.lats, rupt.lats, atol=1e-5) np.testing.assert_allclose(rupj.lons, rupt.lons, atol=1e-5) np.testing.assert_allclose(rupj._depth, rupt._depth, atol=1e-5) np.testing.assert_allclose(rupt.getArea(), 2391.2822653900268, atol=1e-5) target = np.array([ 29.51528, 29.3376, 29.3376, 29.51528005, 29.51528, np.nan, 29.87519, 29.61152, 29.61152, 29.87519021, 29.87519, np.nan, 30.11126, 29.88662, 30.11126, 30.11126, 29.88662, 30.11126, 30.11126, np.nan, 30.4654, 30.30494, 30.4654, 30.4654, 30.30494, 30.4654, 30.4654, np.nan, 30.63731, 30.57658, 30.57658, 30.63731011, 30.63731, np.nan, 30.93655, 30.729, 30.729, 30.93655103, 30.93655, np.nan, 31.01799, 30.94688, 30.94688, 31.0179905, 31.01799, np.nan ]) np.testing.assert_allclose(rupj.lons, target, atol=1e-5) target = np.array([ 40.72733, 40.70985, 40.71185, 40.72932969, 40.72733, np.nan, 40.74903, 40.70513, 40.70713, 40.75102924, 40.74903, np.nan, 40.72336, 40.72582, 40.72336, 40.72536, 40.72782, 40.72536004, 40.72336, np.nan, 40.71081, 40.7121, 40.71081, 40.71281, 40.7141, 40.71281002, 40.71081, np.nan, 40.70068, 40.71621, 40.71821, 40.70268025, 40.70068, np.nan, 40.79654, 40.69947, 40.70147, 40.79853872, 40.79654, np.nan, 40.84501, 40.80199, 40.80399, 40.84700952, 40.84501, np.nan ]) np.testing.assert_allclose(rupj.lats, target, atol=1e-5) target = np.array([ -0.00000000e+00, -0.00000000e+00, 2.00000000e+01, 1.99999325e+01, -0.00000000e+00, np.nan, -9.31322575e-13, -0.00000000e+00, 2.00000000e+01, 1.99998304e+01, -9.31322575e-13, np.nan, 9.31322575e-13, -0.00000000e+00, 9.31322575e-13, 2.00000000e+01, 2.00000000e+01, 2.00000095e+01, 9.31322575e-13, np.nan, -0.00000000e+00, -0.00000000e+00, -0.00000000e+00, 2.00000000e+01, 2.00000000e+01, 2.00000050e+01, -0.00000000e+00, np.nan, -0.00000000e+00, -0.00000000e+00, 2.00000000e+01, 2.00000600e+01, -0.00000000e+00, np.nan, -0.00000000e+00, -0.00000000e+00, 2.00000000e+01, 1.99996249e+01, -0.00000000e+00, np.nan, -0.00000000e+00, -0.00000000e+00, 2.00000000e+01, 1.99998338e+01, -0.00000000e+00, np.nan ]) np.testing.assert_allclose(rupj.depths, target, atol=1e-5)
def test_rupture_depth(interactive=False): DIP = 17.0 WIDTH = 20.0 GRIDRES = 0.1 names = [ 'single', 'double', 'triple', 'concave', 'concave_simple', 'ANrvSA' ] means = [ 3.1554422780092461, 2.9224454569459781, 3.0381968625073563, 2.0522694624400271, 2.4805390352818755, 2.8740121776209673 ] stds = [ 2.1895293825074575, 2.0506459673526174, 2.0244588429154402, 2.0112565876976416, 2.1599789955270019, 1.6156220309120068 ] xp0list = [ np.array([118.3]), np.array([10.1, 10.1]), np.array([10.1, 10.1, 10.3]), np.array([10.9, 10.5, 10.9]), np.array([10.9, 10.6]), np.array([ -76.483, -76.626, -76.757, -76.99, -77.024, -76.925, -76.65, -76.321, -75.997, -75.958 ]) ] xp1list = [ np.array([118.3]), np.array([10.1, 10.3]), np.array([10.1, 10.3, 10.1]), np.array([10.5, 10.9, 11.3]), np.array([10.6, 10.9]), np.array([ -76.626, -76.757, -76.99, -77.024, -76.925, -76.65, -76.321, -75.997, -75.958, -76.006 ]) ] yp0list = [ np.array([34.2]), np.array([34.2, 34.5]), np.array([34.2, 34.5, 34.8]), np.array([34.2, 34.5, 34.8]), np.array([35.1, 35.2]), np.array([ -52.068, -51.377, -50.729, -49.845, -49.192, -48.507, -47.875, -47.478, -47.08, -46.422 ]) ] yp1list = [ np.array([34.5]), np.array([34.5, 34.8]), np.array([34.5, 34.8, 35.1]), np.array([34.5, 34.8, 34.6]), np.array([35.2, 35.4]), np.array([ -51.377, -50.729, -49.845, -49.192, -48.507, -47.875, -47.478, -47.08, -46.422, -45.659 ]) ] for i in range(0, len(xp0list)): xp0 = xp0list[i] xp1 = xp1list[i] yp0 = yp0list[i] yp1 = yp1list[i] name = names[i] mean_value = means[i] std_value = stds[i] zp = np.zeros(xp0.shape) strike = azimuth(xp0[0], yp0[0], xp1[-1], yp1[-1]) widths = np.ones(xp0.shape) * WIDTH dips = np.ones(xp0.shape) * DIP strike = [strike] origin = Origin({ 'id': 'test', 'lon': 0, 'lat': 0, 'depth': 5.0, 'mag': 7.0, 'netid': 'us', 'network': '', 'locstring': '', 'time': HistoricTime.utcfromtimestamp(time.time()) }) rupture = QuadRupture.fromTrace(xp0, yp0, xp1, yp1, zp, widths, dips, origin, strike=strike) # make a grid of points over both quads, ask for depths ymin = np.nanmin(rupture.lats) ymax = np.nanmax(rupture.lats) xmin = np.nanmin(rupture.lons) xmax = np.nanmax(rupture.lons) xmin = np.floor(xmin * (1 / GRIDRES)) / (1 / GRIDRES) xmax = np.ceil(xmax * (1 / GRIDRES)) / (1 / GRIDRES) ymin = np.floor(ymin * (1 / GRIDRES)) / (1 / GRIDRES) ymax = np.ceil(ymax * (1 / GRIDRES)) / (1 / GRIDRES) nx = int(np.ceil(((xmax - xmin - EPS) / GRIDRES) + 1)) ny = int(np.ceil(((ymax - ymin - EPS) / GRIDRES) + 1)) depths = np.zeros((ny, nx)) for row in range(0, ny): for col in range(0, nx): lat, lon = get_lat_lon(row, col, xmin, ymax, GRIDRES) depth = rupture.getDepthAtPoint(lat, lon) depths[row, col] = depth np.testing.assert_almost_equal(np.nanmean(depths), mean_value) np.testing.assert_almost_equal(np.nanstd(depths), std_value) if interactive: fig, axes = plt.subplots(nrows=2, ncols=1) ax1, ax2 = axes xdata = np.append(xp0, xp1[-1]) ydata = np.append(yp0, yp1[-1]) plt.sca(ax1) plt.plot(xdata, ydata, 'b') plt.sca(ax2) im = plt.imshow(depths, cmap='viridis_r') # noqa ch = plt.colorbar() # noqa fname = os.path.join(os.path.expanduser('~'), 'quad_%s_test.png' % name) print('Saving image for %s quad test... %s' % (name, fname)) plt.savefig(fname) plt.close()
def test_EdgeRupture(): # Rupture requires an origin even when not used: origin = Origin({ 'id': 'test', 'lon': 0, 'lat': 0, 'depth': 5.0, 'mag': 7.0, 'netid': 'us', 'network': '', 'locstring': '', 'time': HistoricTime.utcfromtimestamp(time.time()) }) file = os.path.join(homedir, 'rupture_data/cascadia.json') rup = get_rupture(origin, file) np.testing.assert_allclose(rup.getArea(), 105635.92827547337) # Force read Northridge as EdgeRupture file = os.path.join(homedir, 'rupture_data/northridge_fault.txt') d = text_to_json(file, new_format=True) rupt = EdgeRupture(d, origin) strike = rupt.getStrike() np.testing.assert_allclose(strike, 121.97, atol=0.01) dip = rupt.getDip() np.testing.assert_allclose(dip, 40.12, atol=0.01) L = rupt.getLength() np.testing.assert_allclose(L, 17.99, atol=0.01) W = rupt.getWidth() np.testing.assert_allclose(W, 23.92, atol=0.01) ztor = rupt.getDepthToTop() np.testing.assert_allclose(ztor, 5, atol=0.01) # And again for the same vertices but reversed order file = os.path.join(homedir, 'rupture_data/northridge_fixed_fault.txt') d = text_to_json(file, new_format=True) rupt = EdgeRupture(d, origin) strike = rupt.getStrike() np.testing.assert_allclose(strike, 121.97, atol=0.01) dip = rupt.getDip() np.testing.assert_allclose(dip, 40.12, atol=0.01) L = rupt.getLength() np.testing.assert_allclose(L, 17.99, atol=0.01) W = rupt.getWidth() np.testing.assert_allclose(W, 23.92, atol=0.01) ztor = rupt.getDepthToTop() np.testing.assert_allclose(ztor, 5, atol=0.01) # Test for fromArrays method toplats = np.array([37.0, 38.0]) toplons = np.array([-120.0, -120.0]) topdeps = np.array([0.0, 0.0]) botlats = copy.copy(toplats) botlons = copy.copy(toplons) botdeps = np.array([10.0, 10.0]) erup = EdgeRupture.fromArrays(toplons, toplats, topdeps, botlons, botlats, botdeps, origin) # Error: array lengths differ with pytest.raises(Exception) as e: qrup = QuadRupture.fromVertices( [toplons[0]], [toplats[0]], [topdeps[0]], [toplons[1]], [toplats[1]], [topdeps[1]], [botlons[1]], [botlats[1]], [botdeps[1]], [botlons[0]], [botlats[0]], [botdeps[0]][:-1], origin) print(str(e)) # Error: group index too long with pytest.raises(Exception) as e: qrup = QuadRupture.fromVertices([toplons[0]], [toplats[0]], [topdeps[0]], [toplons[1]], [toplats[1]], [topdeps[1]], [botlons[1]], [botlats[1]], [botdeps[1]], [botlons[0]], [botlats[0]], [botdeps[0]], origin, group_index=[0, 0, 0, 0, 0, 0]) print(str(e)) qrup = QuadRupture.fromVertices([toplons[0]], [toplats[0]], [topdeps[0]], [toplons[1]], [toplats[1]], [topdeps[1]], [botlons[1]], [botlats[1]], [botdeps[1]], [botlons[0]], [botlats[0]], [botdeps[0]], origin) np.testing.assert_allclose(erup.getArea(), 1108.9414759967776) np.testing.assert_allclose(erup.getDepthToTop(), 0) np.testing.assert_allclose(erup.getLength(), 111.19492664455889) np.testing.assert_allclose(erup.lats, np.array([37., 38., 38., 37., 37., np.nan])) np.testing.assert_allclose( erup.lons, np.array([-120., -120., -120., -120., -120., np.nan])) np.testing.assert_allclose(erup.depths, np.array([0., 0., 10., 10., 0., np.nan])) np.testing.assert_allclose(erup._getGroupIndex(), np.array([0., 0.])) quads = erup.getQuadrilaterals() np.testing.assert_allclose(quads[0][0].x, -120.0) # Need to also test the distances with EdgeRupture lons = np.linspace(-120.1, -121.0, 10) lats = np.linspace(37.0, 38, 10) deps = np.zeros_like(lons) rrup1, _ = qrup.computeRrup(lons, lats, deps) rrup2, _ = erup.computeRrup(lons, lats, deps) np.testing.assert_allclose(rrup1, rrup2, atol=2e-2) rjb1, _ = qrup.computeRjb(lons, lats, deps) rjb2, _ = erup.computeRjb(lons, lats, deps) np.testing.assert_allclose(rjb1, rjb2, atol=2e-2) gc2 = erup.computeGC2(lons, lats, deps) targetRy0 = np.array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.67335931]) targetRx = np.array([ -8.88024949, -17.73390996, -26.56167797, -35.3634266, -44.13902929, -52.88835984, -61.61129242, -70.30770154, -78.97746209, -87.6204493 ]) np.testing.assert_allclose(gc2['ry0'], targetRy0) np.testing.assert_allclose(gc2['rx'], targetRx)
def test_rupture_from_dict(): # Grab an EdgeRupture origin = Origin({ 'id': 'test', 'lat': 0, 'lon': 0, 'depth': 5.0, 'mag': 7.0, 'netid': 'us', 'network': '', 'locstring': '', 'time': HistoricTime.utcfromtimestamp(time.time()) }) file = os.path.join(homedir, 'rupture_data/cascadia.json') rup_original = get_rupture(origin, file) d = rup_original._geojson rup_from_dict = rupture_from_dict(d) assert rup_from_dict._mesh_dx == 0.5 # Specify mesh_dx rup_original = get_rupture(origin, file, mesh_dx=1.0) d = rup_original._geojson rup_from_dict = rupture_from_dict(d) assert rup_from_dict._mesh_dx == 1.0 # Quad rupture file = os.path.join(homedir, 'rupture_data/izmit.json') rup_original = get_rupture(origin, file) d = rup_original._geojson rup_from_dict = rupture_from_dict(d) assert rup_from_dict.getArea() == rup_original.getArea() # Note, there's a bit of an inconsistency highlighted here because # magnitude has key 'magnitude' in the izmit file, but 'mag' in # the origin and both get retained. # Point rupture from json file = os.path.join(homedir, 'rupture_data/point.json') rup = get_rupture(origin, file) assert rup.lats == 0 assert rup.lons == 0 # Point rupture origin = Origin({ 'id': 'test', 'lon': -122.5, 'lat': 37.3, 'depth': 5.0, 'mag': 7.0, 'netid': 'us', 'network': '', 'locstring': '', 'time': HistoricTime.utcfromtimestamp(time.time()) }) rup_original = get_rupture(origin) d = rup_original._geojson rup_from_dict = rupture_from_dict(d) assert rup_from_dict.lats == 37.3 assert rup_from_dict.lons == -122.5 assert rup_original.getLength() is None assert rup_original.getWidth() == constants.DEFAULT_WIDTH assert rup_original.getArea() is None assert rup_original.getStrike() == constants.DEFAULT_STRIKE assert rup_original.getDip() == constants.DEFAULT_DIP assert rup_original.getDepthToTop() == constants.DEFAULT_ZTOR assert rup_original.getQuadrilaterals() is None assert rup_original.depths == 5.0 # No mech, no tectonic region rjb, _ = rup_original.computeRjb(np.array([-122.0]), np.array([37.0]), np.array([0.0])) rrup, _ = rup_original.computeRrup(np.array([-122.0]), np.array([37.0]), np.array([0.0])) if do_tests is True: np.testing.assert_allclose([rjb[0], rrup[0]], [42.757296, 46.614723]) else: print(rjb[0], rrup[0]) # Various combinations of mech and tectonic region... rup_original._origin._tectonic_region = 'Active Shallow Crust' rup_original._origin.mech = 'ALL' rjb, _ = rup_original.computeRjb(np.array([-122.0]), np.array([37.0]), np.array([0.0])) rrup, _ = rup_original.computeRrup(np.array([-122.0]), np.array([37.0]), np.array([0.0])) if do_tests is True: np.testing.assert_allclose([rjb[0], rrup[0]], [42.757296, 46.614723]) else: print(rjb[0], rrup[0]) rup_original._origin.mech = 'RS' rjb, _ = rup_original.computeRjb(np.array([-122.0]), np.array([37.0]), np.array([0.0])) rrup, _ = rup_original.computeRrup(np.array([-122.0]), np.array([37.0]), np.array([0.0])) if do_tests is True: np.testing.assert_allclose([rjb[0], rrup[0]], [39.779893, 44.033556]) else: print(rjb[0], rrup[0]) rup_original._origin.mech = 'NM' rjb, _ = rup_original.computeRjb(np.array([-122.0]), np.array([37.0]), np.array([0.0])) rrup, _ = rup_original.computeRrup(np.array([-122.0]), np.array([37.0]), np.array([0.0])) if do_tests is True: np.testing.assert_allclose([rjb[0], rrup[0]], [40.937772, 45.254891]) else: print(rjb[0], rrup[0]) rup_original._origin.mech = 'SS' rjb, _ = rup_original.computeRjb(np.array([-122.0]), np.array([37.0]), np.array([0.0])) rrup, _ = rup_original.computeRrup(np.array([-122.0]), np.array([37.0]), np.array([0.0])) if do_tests is True: np.testing.assert_allclose([rjb[0], rrup[0]], [46.750567, 48.108934]) else: print(rjb[0], rrup[0]) rup_original._origin._tectonic_region = 'Stable Shallow Crust' rup_original._origin.mech = 'ALL' rjb, _ = rup_original.computeRjb(np.array([-122.0]), np.array([37.0]), np.array([0.0])) rrup, _ = rup_original.computeRrup(np.array([-122.0]), np.array([37.0]), np.array([0.0])) if do_tests is True: np.testing.assert_allclose([rjb[0], rrup[0]], [43.676648, 48.008276]) else: print(rjb[0], rrup[0]) rup_original._origin.mech = 'RS' rjb, _ = rup_original.computeRjb(np.array([-122.0]), np.array([37.0]), np.array([0.0])) rrup, _ = rup_original.computeRrup(np.array([-122.0]), np.array([37.0]), np.array([0.0])) if do_tests is True: np.testing.assert_allclose([rjb[0], rrup[0]], [42.445057, 46.865434]) else: print(rjb[0], rrup[0]) rup_original._origin.mech = 'NM' rjb, _ = rup_original.computeRjb(np.array([-122.0]), np.array([37.0]), np.array([0.0])) rrup, _ = rup_original.computeRrup(np.array([-122.0]), np.array([37.0]), np.array([0.0])) if do_tests is True: np.testing.assert_allclose([rjb[0], rrup[0]], [43.233314, 47.563079]) else: print(rjb[0], rrup[0]) rup_original._origin.mech = 'SS' rjb, _ = rup_original.computeRjb(np.array([-122.0]), np.array([37.0]), np.array([0.0])) rrup, _ = rup_original.computeRrup(np.array([-122.0]), np.array([37.0]), np.array([0.0])) if do_tests is True: np.testing.assert_allclose([rjb[0], rrup[0]], [47.829729, 50.087485]) else: print(rjb[0], rrup[0]) rup_original._origin._tectonic_region = 'Somewhere Else' rup_original._origin.mech = 'ALL' rjb, var = rup_original.computeRjb(np.array([-122.0]), np.array([37.0]), np.array([0.0])) rrup, var = rup_original.computeRrup(np.array([-122.0]), np.array([37.0]), np.array([0.0])) if do_tests is True: np.testing.assert_allclose([rjb[0], rrup[0]], [42.757296, 46.614723]) else: print(rjb[0], rrup[0]) # This is just zeroes now, so there's not much to check gc2 = rup_original.computeGC2(np.array([-122.0]), np.array([37.0]), np.array([0.0])) assert gc2['rx'][0] == 0
def test_fromOrientation(): py = [0, 0.5] px = [0, 0.5] pz = [10, 20] dx = [5, 7] dy = [8, 5] width = [10, 40] length = [20, 50] strike = [0, 90] dip = [30, 20] # Rupture requires an origin even when not used: origin = Origin({ 'id': 'test', 'lon': 0, 'lat': 0, 'depth': 5.0, 'mag': 7.0, 'netid': 'us', 'network': '', 'locstring': '', 'time': HistoricTime.utcfromtimestamp(time.time()) }) rupture = QuadRupture.fromOrientation(px, py, pz, dx, dy, length, width, strike, dip, origin) p1 = rupture._geojson['features'][0]['geometry']['coordinates'][0][0][0] p2 = rupture._geojson['features'][0]['geometry']['coordinates'][0][0][1] p3 = rupture._geojson['features'][0]['geometry']['coordinates'][0][0][2] p4 = rupture._geojson['features'][0]['geometry']['coordinates'][0][0][3] p5 = rupture._geojson['features'][0]['geometry']['coordinates'][0][1][0] p6 = rupture._geojson['features'][0]['geometry']['coordinates'][0][1][1] p7 = rupture._geojson['features'][0]['geometry']['coordinates'][0][1][2] p8 = rupture._geojson['features'][0]['geometry']['coordinates'][0][1][3] # Check depths np.testing.assert_allclose(p1[2], 6) np.testing.assert_allclose(p2[2], 6) np.testing.assert_allclose(p3[2], 11) np.testing.assert_allclose(p4[2], 11) np.testing.assert_allclose(p5[2], 18.2898992834) np.testing.assert_allclose(p6[2], 18.2898992834) np.testing.assert_allclose(p7[2], 31.9707050164) np.testing.assert_allclose(p8[2], 31.9707050164) # Exception raised if no origin with pytest.raises(Exception) as a: rupture = QuadRupture.fromOrientation(px, py, pz, dx, dy, length, width, strike, dip, None) print(str(a)) # Exception raised if different lengths of arrays with pytest.raises(Exception) as a: py = [0, 2] px = [0] pz = [10] dx = [5] dy = [8] width = [10] length = [20] strike = [0] dip = [30] origin = Origin({ 'id': 'test', 'lon': 0, 'lat': 0, 'depth': 5.0, 'mag': 7.0, 'netid': 'us', 'network': '', 'locstring': '', 'time': HistoricTime.utcfromtimestamp(time.time()) }) rupture = QuadRupture.fromOrientation(px, py, pz, dx, dy, length, width, strike, dip, origin) print(str(a))
def calcMetrics(self, eventid, stations=None, labels=None, config=None, streams=None, stream_label=None, rupture_file=None, calc_station_metrics=True, calc_waveform_metrics=True): """ Calculate waveform and/or station metrics for a set of waveforms. Args: eventid (str): ID of event to search for in ASDF file. stations (list): List of stations to create metrics for. labels (list): List of processing labels to create metrics for. config (dict): Configuration dictionary. streams (StreamCollection): Optional StreamCollection object to create metrics for. stream_label (str): Label to be used in the metrics path when providing a StreamCollection. rupture_file (str): Path pointing to the rupture file. calc_station_metrics (bool): Whether to calculate station metrics. Default is True. calc_waveform_metrics (bool): Whether to calculate waveform metrics. Default is True. """ if not self.hasEvent(eventid): fmt = 'No event matching %s found in workspace.' raise KeyError(fmt % eventid) if streams is None: streams = self.getStreams( eventid, stations=stations, labels=labels) event = self.getEvent(eventid) # Load the rupture file origin = Origin({ 'id': event.id, 'netid': '', 'network': '', 'lat': event.latitude, 'lon': event.longitude, 'depth': event.depth_km, 'locstring': '', 'mag': event.magnitude, 'time': event.time }) rupture = get_rupture(origin, rupture_file) vs30_grids = None if config is not None: if 'vs30' in config['metrics']: vs30_grids = config['metrics']['vs30'] for vs30_name in vs30_grids: vs30_grids[vs30_name]['grid_object'] = GMTGrid.load( vs30_grids[vs30_name]['file']) for stream in streams: instrument = stream.get_id() logging.info('Calculating stream metrics for %s...' % instrument) try: summary = StationSummary.from_config( stream, event=event, config=config, calc_waveform_metrics=calc_waveform_metrics, calc_station_metrics=calc_station_metrics, rupture=rupture, vs30_grids=vs30_grids) except BaseException as pgme: fmt = ('Could not create stream metrics for event %s,' 'instrument %s: "%s"') logging.warning(fmt % (eventid, instrument, str(pgme))) continue if calc_waveform_metrics and stream.passed: xmlstr = summary.get_metric_xml() if stream_label is not None: tag = '%s_%s' % (eventid, stream_label) else: tag = stream.tag metricpath = '/'.join([ format_netsta(stream[0].stats), format_nslit(stream[0].stats, stream.get_inst(), tag), ]) self.insert_aux(xmlstr, 'WaveFormMetrics', metricpath) if calc_station_metrics: xmlstr = summary.get_station_xml() metricpath = '/'.join([ format_netsta(stream[0].stats), format_nslit(stream[0].stats, stream.get_inst(), eventid) ]) self.insert_aux(xmlstr, 'StationMetrics', metricpath)
def test_northridge(): rupture_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. -118.421 34.315 5.000 -118.587 34.401 5.000 -118.693 34.261 20.427 -118.527 34.175 20.427 -118.421 34.315 5.000 """ # noqa # Rupture requires an origin even when not used: origin = Origin({ 'id': 'test', 'lon': 0, 'lat': 0, 'depth': 5.0, 'mag': 7.0, 'netid': 'us', 'network': '', 'locstring': '', 'time': HistoricTime.utcfromtimestamp(time.time()) }) cbuf = io.StringIO(rupture_text) rupture = get_rupture(origin, cbuf) strike = rupture.getStrike() np.testing.assert_allclose(strike, 122.06, atol=0.01) dip = rupture.getDip() np.testing.assert_allclose(dip, 40.21, atol=0.01) L = rupture.getLength() np.testing.assert_allclose(L, 17.99, atol=0.01) W = rupture.getWidth() np.testing.assert_allclose(W, 23.94, atol=0.01) nq = rupture.getNumQuads() np.testing.assert_allclose(nq, 1) ng = rupture.getNumGroups() np.testing.assert_allclose(ng, 1) nd = rupture.getDeps() np.testing.assert_allclose(nd, [5.0, 5.0, 20.427, 20.427, np.nan]) sind = rupture._getGroupIndex() np.testing.assert_allclose(sind, [0]) ztor = rupture.getDepthToTop() np.testing.assert_allclose(ztor, 5, atol=0.01) itl = rupture.getIndividualTopLengths() np.testing.assert_allclose(itl, 17.99, atol=0.01) iw = rupture.getIndividualWidths() np.testing.assert_allclose(iw, 23.94, atol=0.01) lats = rupture.lats lats_d = np.array([34.401, 34.315, 34.175, 34.261, 34.401, np.nan]) np.testing.assert_allclose(lats, lats_d, atol=0.01) lons = rupture.lons lons_d = np.array( [-118.587, -118.421, -118.527, -118.693, -118.587, np.nan]) np.testing.assert_allclose(lons, lons_d, atol=0.01) ln, lt, de = rupture.getRuptureAsArrays() np.testing.assert_allclose( ln, np.array([-118.421, -118.587, -118.693, -118.527, np.nan]), atol=0.01) np.testing.assert_allclose(lt, np.array( [34.315, 34.401, 34.261, 34.175, np.nan]), atol=0.01) np.testing.assert_allclose(de, [5.0, 5.0, 20.427, 20.427, np.nan]) mesh = rupture.getRuptureAsMesh() np.testing.assert_allclose( mesh.lons, [-118.421, -118.587, -118.693, -118.527, np.nan]) np.testing.assert_allclose(mesh.lats, [34.315, 34.401, 34.261, 34.175, np.nan]) np.testing.assert_allclose(mesh.depths, [5., 5., 20.427, 20.427, np.nan])
def _event_station_metrics(self, event): self.eventid = event.id logging.info('Computing station metrics for event %s...' % self.eventid) event_dir = os.path.join(self.gmrecords.data_path, self.eventid) workname = os.path.join(event_dir, WORKSPACE_NAME) if not os.path.isfile(workname): logging.info( 'No workspace file found for event %s. Please run ' 'subcommand \'assemble\' to generate workspace file.' % self.eventid) logging.info('Continuing to next event.') return event.id self.workspace = StreamWorkspace.open(workname) self._get_pstreams() if not (hasattr(self, 'pstreams') and len(self.pstreams) > 0): logging.info('No streams found. Nothing to do. Goodbye.') self.workspace.close() return event.id rupture_file = get_rupture_file(event_dir) origin = Origin({ 'id': self.eventid, 'netid': '', 'network': '', 'lat': event.latitude, 'lon': event.longitude, 'depth': event.depth_km, 'locstring': '', 'mag': event.magnitude, 'time': event.time }) self.origin = origin rupture = get_rupture(origin, rupture_file) sta_lats = [] sta_lons = [] sta_elev = [] self.sta_repi = [] self.sta_rhyp = [] self.sta_baz = [] for st in self.pstreams: sta_lats.append(st[0].stats.coordinates.latitude) sta_lons.append(st[0].stats.coordinates.longitude) sta_elev.append(st[0].stats.coordinates.elevation) geo_tuple = gps2dist_azimuth(st[0].stats.coordinates.latitude, st[0].stats.coordinates.longitude, origin.lat, origin.lon) self.sta_repi.append(geo_tuple[0] / M_PER_KM) self.sta_baz.append(geo_tuple[1]) self.sta_rhyp.append( distance(st[0].stats.coordinates.longitude, st[0].stats.coordinates.latitude, -st[0].stats.coordinates.elevation / M_PER_KM, origin.lon, origin.lat, origin.depth)) if isinstance(rupture, PointRupture): self._get_ps2ff_splines() rjb_hat = self.rjb_spline(self.sta_repi) rjb_mean = rjb_hat[0] rjb_var = rjb_hat[1] rrup_hat = self.rrup_spline(self.sta_repi) rrup_mean = rrup_hat[0] rrup_var = rrup_hat[1] gc2_rx = np.full_like(rjb_mean, np.nan) gc2_ry = np.full_like(rjb_mean, np.nan) gc2_ry0 = np.full_like(rjb_mean, np.nan) gc2_U = np.full_like(rjb_mean, np.nan) gc2_T = np.full_like(rjb_mean, np.nan) else: logging.info('******************************') logging.info('* Found rupture *') logging.info('******************************') sta_lons = np.array(sta_lons) sta_lats = np.array(sta_lats) elev = np.full_like(sta_lons, ELEVATION_FOR_DISTANCE_CALCS) rrup_mean, rrup_var = rupture.computeRrup(sta_lons, sta_lats, elev) rjb_mean, rjb_var = rupture.computeRjb(sta_lons, sta_lats, elev) rrup_var = np.full_like(rrup_mean, np.nan) rjb_var = np.full_like(rjb_mean, np.nan) gc2_dict = rupture.computeGC2(sta_lons, sta_lats, elev) gc2_rx = gc2_dict['rx'] gc2_ry = gc2_dict['ry'] gc2_ry0 = gc2_dict['ry0'] gc2_U = gc2_dict['U'] gc2_T = gc2_dict['T'] # If we don't have a point rupture, then back azimuth needs # to be calculated to the closest point on the rupture self.sta_baz = [] for i in range(len(self.pstreams)): dists = [] bazs = [] for quad in rupture._quadrilaterals: P0, P1, P2, P3 = quad for point in [P0, P1]: dist, az, baz = gps2dist_azimuth( point.y, point.x, sta_lats[i], sta_lons[i]) dists.append(dist) bazs.append(baz) self.sta_baz.append(bazs[np.argmin(dists)]) for i, stream in enumerate(self.pstreams): logging.info('Calculating station metrics for %s...' % stream.get_id()) summary = StationSummary.from_config(stream, event=event, config=self.gmrecords.conf, calc_waveform_metrics=False, calc_station_metrics=False, rupture=rupture, vs30_grids=self.vs30_grids) summary._distances = { 'epicentral': self.sta_repi[i], 'hypocentral': self.sta_rhyp[i], 'rupture': rrup_mean[i], 'rupture_var': rrup_var[i], 'joyner_boore': rjb_mean[i], 'joyner_boore_var': rjb_var[i], 'gc2_rx': gc2_rx[i], 'gc2_ry': gc2_ry[i], 'gc2_ry0': gc2_ry0[i], 'gc2_U': gc2_U[i], 'gc2_T': gc2_T[i] } summary._back_azimuth = self.sta_baz[i] if self.vs30_grids is not None: for vs30_name in self.vs30_grids.keys(): tmpgrid = self.vs30_grids[vs30_name] summary._vs30[vs30_name] = { 'value': tmpgrid['grid_object'].getValue( float(sta_lats[i]), float(sta_lons[i])), 'column_header': tmpgrid['column_header'], 'readme_entry': tmpgrid['readme_entry'], 'units': tmpgrid['units'] } xmlstr = summary.get_station_xml() metricpath = '/'.join([ format_netsta(stream[0].stats), format_nslit(stream[0].stats, stream.get_inst(), self.eventid) ]) self.workspace.insert_aux(xmlstr, 'StationMetrics', metricpath, overwrite=self.gmrecords.args.overwrite) logging.info('Added station metrics to workspace files ' 'with tag \'%s\'.' % self.gmrecords.args.label) self.workspace.close() return event.id
def test_san_fernando(): # This is a challenging rupture due to overlapping and discordant # segments, as brought up by Graeme Weatherill. Our initial # implementation put the origin on the wrong side of the rupture. x0 = np.array([7.1845, 7.8693]) y0 = np.array([-10.3793, -16.2096]) z0 = np.array([3.0000, 0.0000]) x1 = np.array([-7.8506, -7.5856]) y1 = np.array([-4.9073, -12.0682]) z1 = np.array([3.0000, 0.0000]) x2 = np.array([-4.6129, -5.5149]) y2 = np.array([3.9887, -4.3408]) z2 = np.array([16.0300, 8.0000]) x3 = np.array([10.4222, 9.9400]) y3 = np.array([-1.4833, -8.4823]) z3 = np.array([16.0300, 8.0000]) epilat = 34.44000 epilon = -118.41000 proj = OrthographicProjection(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) # Rupture requires an origin even when not used: origin = Origin({ 'id': 'test', 'lat': 0, 'lon': 0, 'depth': 5.0, 'mag': 7.0, 'netid': '', 'network': '', 'locstring': '', 'time': HistoricTime.utcfromtimestamp(int(time.time())) }) rup = QuadRupture.fromVertices(lon0, lat0, z0, lon1, lat1, z1, lon2, lat2, z2, lon3, lat3, z3, origin) # Make a origin object; most of the 'event' values don't matter event = { 'lat': 0, 'lon': 0, 'depth': 0, 'mag': 6.61, 'id': '', 'locstring': '', 'type': 'ALL', 'netid': '', 'network': '', 'time': HistoricTime.utcfromtimestamp(int(time.time())) } origin = Origin(event) # Grid of sites buf = 0.25 lat = np.linspace(np.nanmin(rup._lat) - buf, np.nanmax(rup._lat) + buf, 10) lon = np.linspace(np.nanmin(rup._lon) - buf, np.nanmax(rup._lon) + buf, 10) lons, lats = np.meshgrid(lon, lat) dep = np.zeros_like(lons) x, y = proj(lon, lat) rupx, rupy = proj(rup._lon[~np.isnan(rup._lon)], rup._lat[~np.isnan(rup._lat)]) # Calculate U and T dtypes = ['U', 'T'] dists = get_distance(dtypes, lats, lons, dep, rup) targetU = np.array( [[ 29.37395812, 22.56039569, 15.74545461, 8.92543078, 2.09723735, -4.73938823, -11.58093887, -18.42177264, -25.25743913, -32.08635501 ], [ 31.84149137, 25.03129417, 18.22007124, 11.40292429, 4.57583886, -2.26009972, -9.09790123, -15.92911065, -22.75071243, -29.56450963 ], [ 34.30623138, 27.49382948, 20.67774678, 13.85111535, 7.0115472, 0.16942111, -6.65327488, -13.45181115, -20.24352643, -27.03530618 ], [ 36.78170249, 29.96380633, 23.1270492, 16.23906653, 9.32934682, 2.41729624, -4.2732657, -10.94940844, -17.703852, -24.4792072 ], [ 39.29233805, 32.49155866, 25.68380903, 18.73823089, 12.08780156, 5.99219619, -1.38387344, -8.28331275, -15.08759643, -21.87909368 ], [ 41.84662959, 35.09745097, 28.42432401, 21.98993679, 15.2994003, 8.38037254, 1.3900846, -5.5601922, -12.4250749, -19.24690137 ], [ 44.41552101, 37.69652131, 31.0257236, 24.38573309, 17.67059825, 10.84688716, 3.96604399, -2.920931, -9.78152208, -16.6132751 ], [ 46.97201328, 40.2558351, 33.55821495, 26.85923974, 20.12416451, 13.33640001, 6.50905851, -0.33349597, -7.17138975, -13.99568321 ], [ 49.51154107, 42.79053584, 36.07536907, 29.35382731, 22.61099757, 15.83894006, 9.04135415, 2.22928601, -4.58574545, -11.3959888 ], [ 52.03832734, 45.31289877, 38.58842009, 31.85764151, 25.11309728, 18.35066231, 11.57145669, 4.78070229, -2.01505508, -8.81029694 ]]) np.testing.assert_allclose(dists['U'], targetU, atol=0.01) targetT = np.array( [[ -40.32654805, -38.14066537, -35.95781299, -33.79265063, -31.65892948, -29.56075203, -27.48748112, -25.41823592, -23.33452174, -21.22822801 ], [ -32.28894353, -30.06603457, -27.83163648, -25.61482279, -23.45367121, -21.36959238, -19.34738882, -17.33510593, -15.28949735, -13.20224592 ], [ -24.30254163, -22.03532096, -19.70590091, -17.35907062, -15.10840929, -13.02682541, -11.13554925, -9.25705749, -7.26675455, -5.19396824 ], [ -16.41306482, -14.1418547, -11.68888578, -8.9318195, -6.39939727, -4.10984325, -2.85061088, -1.29211846, 0.68929792, 2.78115216 ], [ -8.63784529, -6.5089946, -4.32108309, -1.44275161, -0.05102145, -0.20890633, 3.92700516, 6.36977183, 8.55572399, 10.72128633 ], [ -0.88135778, 1.06766314, 2.77955566, 3.8241835, 5.99212478, 8.76823285, 11.54715599, 14.0961506, 16.4200502, 18.65346494 ], [ 6.98140207, 8.91888936, 10.77724993, 12.6499521, 14.79454638, 17.18482779, 19.63520498, 22.03525644, 24.35152986, 26.60592498 ], [ 14.95635952, 16.95134069, 18.94768299, 20.99811237, 23.15975573, 25.42700742, 27.74302905, 30.0547134, 32.33583361, 34.58421221 ], [ 22.9921068, 25.0353212, 27.09829391, 29.20364631, 31.3678744, 33.58684524, 35.8383652, 38.09736043, 40.34713771, 42.58152772 ], [ 31.05186177, 33.1252095, 35.21960344, 37.34488267, 39.50633206, 41.70076344, 43.91762786, 46.14415669, 48.37021739, 50.59029205 ]]) np.testing.assert_allclose(dists['T'], targetT, atol=0.01) # new method: ddict = _computeGC2(rup, lons, lats, dep) np.testing.assert_allclose(ddict['T'], targetT, atol=0.01) np.testing.assert_allclose(ddict['U'], targetU, atol=0.01)