| Total Complexity | 4 |
| Total Lines | 38 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | import obspy |
||
| 2 | import numpy as np |
||
| 3 | from rsudp.client import Client |
||
| 4 | |||
| 5 | class PGVAlert: |
||
| 6 | def __init__(self, threshold): |
||
| 7 | self.threshold = threshold # Richter magnitude threshold |
||
| 8 | |||
| 9 | def process(self, data): |
||
| 10 | # Create a Stream object from the incoming data |
||
| 11 | st = obspy.Stream(obspy.Trace(data)) |
||
| 12 | # Remove mean and linear trends |
||
| 13 | st.detrend("demean") |
||
| 14 | st.detrend("linear") |
||
| 15 | # Apply bandpass filter to isolate frequencies of interest |
||
| 16 | st.filter("bandpass", freqmin=0.1, freqmax=20.0) |
||
| 17 | # Integrate to convert displacement to velocity |
||
| 18 | st.integrate() |
||
| 19 | # Find Peak Ground Velocity (PGV) |
||
| 20 | pgv = max(abs(st[0].data)) |
||
| 21 | # Convert PGV from m/s to cm/s |
||
| 22 | pgv_cm_s = pgv * 100 |
||
| 23 | # Estimate Richter magnitude using the empirical relationship |
||
| 24 | magnitude = np.log10(pgv_cm_s) + 2.4 |
||
| 25 | |||
| 26 | # Trigger alert if magnitude exceeds threshold |
||
| 27 | if magnitude >= self.threshold: |
||
| 28 | self.trigger_alert(magnitude) |
||
| 29 | |||
| 30 | def trigger_alert(self, magnitude): |
||
| 31 | print(f"Earthquake detected! Estimated Richter Magnitude: {magnitude}") |
||
| 32 | |||
| 33 | # Initialize and start the client with the custom alert |
||
| 34 | client = Client() |
||
| 35 | alert = PGVAlert(threshold=4.0) |
||
| 36 | client.add_listener(alert.process) |
||
| 37 | client.run() |
||
| 38 |