|
| 1 | + |
| 2 | +import pygmm |
| 3 | +import numpy as np |
| 4 | +import math |
| 5 | + |
| 6 | +from pyincore.models.hazard.earthquake import Earthquake |
| 7 | +from pyincore.utils.hazardutil import HazardUtil |
| 8 | +from pyincore.models.units import Units |
| 9 | + |
| 10 | + |
| 11 | +class LocalEarthquake(Earthquake): |
| 12 | + |
| 13 | + def __init__(self, metadata): |
| 14 | + super().__init__(metadata) |
| 15 | + |
| 16 | + attenuations = metadata["attenuations"] |
| 17 | + |
| 18 | + # Currently, we only support one attenuation |
| 19 | + self.attenuation = list(attenuations.keys())[0] |
| 20 | + |
| 21 | + self.eq_parameters = self.get_pygmm_parameters(metadata) |
| 22 | + self.default_units = {"pga": "g", "pgv": "cm/s", "pgd": "cm", "sa": "g"} |
| 23 | + |
| 24 | + def read_hazard_values(self, payload: list, hazard_service=None, **kwargs): |
| 25 | + """Retrieve bulk earthquake hazard values either from the Hazard service or read it from local Dataset |
| 26 | +
|
| 27 | + Args: |
| 28 | + payload (list): |
| 29 | + hazard_service (obj): Hazard service. |
| 30 | + kwargs (dict): Keyword arguments. |
| 31 | + Returns: |
| 32 | + obj: Hazard values. |
| 33 | +
|
| 34 | + """ |
| 35 | + |
| 36 | + print("read value from model: "+self.attenuation) |
| 37 | + model_class = getattr(pygmm, self.attenuation) |
| 38 | + # magnitude = self.magnitude |
| 39 | + eq_parameters = self.eq_parameters |
| 40 | + |
| 41 | + response = [] |
| 42 | + |
| 43 | + # TODO shouldn't need this |
| 44 | + magnitude = eq_parameters["mag"] |
| 45 | + for req in payload: |
| 46 | + hazard_values = [] |
| 47 | + for index, req_demand_type in enumerate(req["demands"]): |
| 48 | + req_demand = req_demand_type.lower() |
| 49 | + req_demand_period = 0.0 |
| 50 | + |
| 51 | + if "sa" in req_demand: |
| 52 | + req_demand_parts = req_demand.split(" ") |
| 53 | + req_demand_period = req_demand_parts[0] |
| 54 | + req_demand = req_demand_parts[1] |
| 55 | + |
| 56 | + latitude = float(req["loc"].split(",")[0]) |
| 57 | + longitude = float(req["loc"].split(",")[1]) |
| 58 | + # longitude, latitude |
| 59 | + |
| 60 | + local_site = [latitude, longitude] |
| 61 | + |
| 62 | + rupture_distance_ergo = HazardUtil.compute_rupture_distance(magnitude, self.depth, self.mechanism, |
| 63 | + self.azimuth_angle, self.dip_angle, |
| 64 | + local_site, self.source_site,) |
| 65 | + joyner_boore_distance = HazardUtil.compute_joyner_boore_distance(magnitude, self.mechanism, self.depth, |
| 66 | + self.azimuth_angle, self.dip_angle, |
| 67 | + local_site, self.source_site) |
| 68 | + |
| 69 | + z_tor = self.coseismic_rupture_depth |
| 70 | + down_dip_width = self.get_downdip_rupture_width(magnitude, self.mechanism) |
| 71 | + |
| 72 | + horizontal_distance = HazardUtil.compute_horizontal_distance(joyner_boore_distance, z_tor, |
| 73 | + down_dip_width, self.dip_angle, |
| 74 | + self.azimuth_angle, rupture_distance_ergo) |
| 75 | + |
| 76 | + # TODO - this should come from a shapefile of site conditions maybe? |
| 77 | + site_condition = self.default_site_condition |
| 78 | + site_vs30 = 760 |
| 79 | + |
| 80 | + eq_parameters["dist_rup"] = rupture_distance_ergo |
| 81 | + eq_parameters["dist_jb"] = joyner_boore_distance |
| 82 | + eq_parameters["dist_x"] = horizontal_distance |
| 83 | + |
| 84 | + scenario = pygmm.Scenario(**eq_parameters) |
| 85 | + model = model_class(scenario) |
| 86 | + if req_demand == "pga": |
| 87 | + raw_hazard_value = model.pga |
| 88 | + elif req_demand == "pgv": |
| 89 | + raw_hazard_value = model.pgv |
| 90 | + elif req_demand == "pgd": |
| 91 | + raw_hazard_value = model.pgd |
| 92 | + elif req_demand == "sa": |
| 93 | + sa_values = model.spec_accels |
| 94 | + sa_index_array = np.where(model.periods == float(req_demand_period)) |
| 95 | + if len(sa_index_array[0]) != 0: |
| 96 | + sa_index = sa_index_array[0][0] |
| 97 | + else: |
| 98 | + sa_index = self.find_nearest(model.periods, float(req_demand_period)) |
| 99 | + |
| 100 | + sa_values = model.spec_accels |
| 101 | + raw_hazard_value = sa_values[sa_index] |
| 102 | + |
| 103 | + if raw_hazard_value is None: |
| 104 | + converted_hazard_value = raw_hazard_value |
| 105 | + else: |
| 106 | + converted_hazard_value = Units.convert_hazard(raw_hazard_value, |
| 107 | + original_demand_units=self.default_units[req_demand], |
| 108 | + requested_demand_units=req["units"][index]) |
| 109 | + |
| 110 | + hazard_values.append(converted_hazard_value) |
| 111 | + |
| 112 | + req.update({"hazardValues": hazard_values}) |
| 113 | + response.append(req) |
| 114 | + |
| 115 | + return response |
| 116 | + |
| 117 | + def find_nearest(self, period_array, period): |
| 118 | + array = np.asarray(period_array) |
| 119 | + idx = (np.abs(period_array - period)).argmin() |
| 120 | + return idx |
| 121 | + |
| 122 | + def get_pygmm_parameters(self, metadata): |
| 123 | + |
| 124 | + eq_parameters = metadata["eqParameters"] |
| 125 | + magnitude = float(eq_parameters["magnitude"]) |
| 126 | + event_type = eq_parameters["eventType"] |
| 127 | + |
| 128 | + # TODO - need to handle the multiple attenuation models case |
| 129 | + fault_type_map = eq_parameters["faultTypeMap"] |
| 130 | + mechanism = fault_type_map[self.attenuation] |
| 131 | + |
| 132 | + if mechanism == "Strike-Slip": |
| 133 | + mechanism_pygmm = "SS" |
| 134 | + else: |
| 135 | + # TODO handle other cases |
| 136 | + mechanism_pygmm = mechanism |
| 137 | + |
| 138 | + dip_angle = float(eq_parameters["dipAngle"]) |
| 139 | + # self.azimuth_angle = eq_parameters["azimuthAngle"] |
| 140 | + |
| 141 | + # TODO remove these later |
| 142 | + self.mechanism = mechanism_pygmm |
| 143 | + self.dip_angle = dip_angle |
| 144 | + |
| 145 | + self.azimuth_angle = 130.0 |
| 146 | + self.default_site_condition = "soil" |
| 147 | + self.default_site_vs30 = 760 |
| 148 | + self.source_site = [float(eq_parameters["srcLatitude"]), float(eq_parameters["srcLongitude"])] |
| 149 | + self.depth = float(eq_parameters["depth"]) |
| 150 | + self.coseismic_rupture_depth = float(eq_parameters["coseismicRuptureDepth"]) |
| 151 | + |
| 152 | + # TODO all the eq parameters should be mapped into a dictionary that can be passed to the pygmm Scenario so |
| 153 | + # we can map IN-CORE parameters to pygmm parameters (e.g. Ztor is depth_tor in pygmm) |
| 154 | + |
| 155 | + eq_parameters = {"mag": magnitude, "site_cond": self.default_site_condition, "v_s30": self.default_site_vs30, |
| 156 | + "dip": dip_angle, "mechanism": mechanism_pygmm, "event_type": event_type, |
| 157 | + "depth_tor": 3.0, "depth_1_0": 0.0} |
| 158 | + |
| 159 | + return eq_parameters |
| 160 | + |
| 161 | + def get_downdip_rupture_width(self, magnitude, mechanism): |
| 162 | + # TODO this should come from a dict of coefficients for different mechanisms |
| 163 | + a = -0.76 |
| 164 | + b = 0.27 |
| 165 | + |
| 166 | + return math.pow(10, a + b * magnitude) |
0 commit comments