Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,18 @@
|
|
| 1 |
-
from fastapi import FastAPI, HTTPException
|
| 2 |
from starlette.responses import JSONResponse
|
| 3 |
from obspy.clients.fdsn import Client
|
| 4 |
from obspy.core.utcdatetime import UTCDateTime
|
| 5 |
import logging
|
|
|
|
| 6 |
|
| 7 |
# Configure logging
|
| 8 |
logging.basicConfig(level=logging.INFO)
|
| 9 |
|
| 10 |
# Initialize the FastAPI app
|
| 11 |
app = FastAPI(
|
| 12 |
-
title="Earthquake Catalog API",
|
| 13 |
-
description="
|
| 14 |
-
version="1.0
|
| 15 |
)
|
| 16 |
|
| 17 |
# Initialize the IRIS FDSN client
|
|
@@ -27,24 +28,26 @@ def greet_json():
|
|
| 27 |
"""
|
| 28 |
A simple 'Hello World' endpoint.
|
| 29 |
"""
|
| 30 |
-
return {"Hello": "World!", "message": "Go to /
|
| 31 |
|
| 32 |
@app.get("/earthquakes")
|
| 33 |
-
async def get_earthquake_catalog(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
"""
|
| 35 |
-
Fetches earthquake data
|
| 36 |
-
from 2015-01-05 to 2016-03-05.
|
| 37 |
"""
|
| 38 |
if not client:
|
| 39 |
-
raise HTTPException(
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
)
|
| 43 |
|
| 44 |
try:
|
| 45 |
-
starttime = UTCDateTime(
|
| 46 |
-
endtime = UTCDateTime(
|
| 47 |
-
min_magnitude = 5.0
|
| 48 |
|
| 49 |
logging.info(f"Fetching events from {starttime} to {endtime} with min magnitude {min_magnitude}.")
|
| 50 |
catalog = client.get_events(
|
|
@@ -55,21 +58,22 @@ async def get_earthquake_catalog():
|
|
| 55 |
logging.info(f"Found {len(catalog)} events. Now processing.")
|
| 56 |
|
| 57 |
earthquake_list = []
|
| 58 |
-
# --- Key Change Start ---
|
| 59 |
-
# Loop through each event and process it individually to prevent one bad event from crashing the app.
|
| 60 |
for event in catalog:
|
| 61 |
try:
|
| 62 |
-
# Ensure the event has a preferred origin and magnitude
|
| 63 |
-
if not event.preferred_origin() or not event.preferred_magnitude():
|
| 64 |
-
continue # Skip to the next event if essential data is missing
|
| 65 |
-
|
| 66 |
origin = event.preferred_origin()
|
| 67 |
magnitude = event.preferred_magnitude()
|
| 68 |
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
place = "N/A"
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
| 73 |
|
| 74 |
earthquake_list.append({
|
| 75 |
"time": origin.time.isoformat(),
|
|
@@ -81,17 +85,12 @@ async def get_earthquake_catalog():
|
|
| 81 |
"place": place,
|
| 82 |
})
|
| 83 |
except Exception as e:
|
| 84 |
-
|
| 85 |
-
logging.warning(f"Skipping one event due to a processing error: {e}")
|
| 86 |
continue
|
| 87 |
-
|
| 88 |
-
|
| 89 |
logging.info(f"Successfully processed {len(earthquake_list)} events.")
|
| 90 |
return JSONResponse(content={"earthquakes": earthquake_list})
|
| 91 |
|
| 92 |
except Exception as e:
|
| 93 |
logging.error(f"A major error occurred during the request: {e}")
|
| 94 |
-
raise HTTPException(
|
| 95 |
-
status_code=500, # Internal Server Error
|
| 96 |
-
detail=f"An internal error occurred: {e}",
|
| 97 |
-
)
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException, Query
|
| 2 |
from starlette.responses import JSONResponse
|
| 3 |
from obspy.clients.fdsn import Client
|
| 4 |
from obspy.core.utcdatetime import UTCDateTime
|
| 5 |
import logging
|
| 6 |
+
from datetime import date
|
| 7 |
|
| 8 |
# Configure logging
|
| 9 |
logging.basicConfig(level=logging.INFO)
|
| 10 |
|
| 11 |
# Initialize the FastAPI app
|
| 12 |
app = FastAPI(
|
| 13 |
+
title="Dynamic Earthquake Catalog API",
|
| 14 |
+
description="A robust API to fetch earthquake data from the IRIS FDSN web service using URL parameters.",
|
| 15 |
+
version="2.1.0",
|
| 16 |
)
|
| 17 |
|
| 18 |
# Initialize the IRIS FDSN client
|
|
|
|
| 28 |
"""
|
| 29 |
A simple 'Hello World' endpoint.
|
| 30 |
"""
|
| 31 |
+
return {"Hello": "World!", "message": "Go to /docs to try the dynamic earthquake endpoint. cwadayi"}
|
| 32 |
|
| 33 |
@app.get("/earthquakes")
|
| 34 |
+
async def get_earthquake_catalog(
|
| 35 |
+
start_date: date = Query(default="2024-01-01", description="Start date in YYYY-MM-DD format"),
|
| 36 |
+
end_date: date = Query(default="2024-01-07", description="End date in YYYY-MM-DD format"),
|
| 37 |
+
min_magnitude: float = Query(default=4.5, description="Minimum earthquake magnitude (e.g., 5.5)", gt=0, le=10)
|
| 38 |
+
):
|
| 39 |
"""
|
| 40 |
+
Fetches earthquake data based on date and magnitude parameters provided in the URL.
|
|
|
|
| 41 |
"""
|
| 42 |
if not client:
|
| 43 |
+
raise HTTPException(status_code=503, detail="The IRIS FDSN client is not available.")
|
| 44 |
+
|
| 45 |
+
if start_date > end_date:
|
| 46 |
+
raise HTTPException(status_code=400, detail="The start_date cannot be after the end_date.")
|
| 47 |
|
| 48 |
try:
|
| 49 |
+
starttime = UTCDateTime(start_date)
|
| 50 |
+
endtime = UTCDateTime(end_date)
|
|
|
|
| 51 |
|
| 52 |
logging.info(f"Fetching events from {starttime} to {endtime} with min magnitude {min_magnitude}.")
|
| 53 |
catalog = client.get_events(
|
|
|
|
| 58 |
logging.info(f"Found {len(catalog)} events. Now processing.")
|
| 59 |
|
| 60 |
earthquake_list = []
|
|
|
|
|
|
|
| 61 |
for event in catalog:
|
| 62 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
origin = event.preferred_origin()
|
| 64 |
magnitude = event.preferred_magnitude()
|
| 65 |
|
| 66 |
+
if not origin or not magnitude:
|
| 67 |
+
continue
|
| 68 |
+
|
| 69 |
+
# --- Key Change Start ---
|
| 70 |
+
# Safely get the description using getattr. This avoids errors if the 'descriptions'
|
| 71 |
+
# attribute doesn't exist, by providing a default empty list [].
|
| 72 |
place = "N/A"
|
| 73 |
+
descriptions = getattr(event, 'descriptions', [])
|
| 74 |
+
if descriptions:
|
| 75 |
+
place = descriptions[0].text
|
| 76 |
+
# --- Key Change End ---
|
| 77 |
|
| 78 |
earthquake_list.append({
|
| 79 |
"time": origin.time.isoformat(),
|
|
|
|
| 85 |
"place": place,
|
| 86 |
})
|
| 87 |
except Exception as e:
|
| 88 |
+
logging.warning(f"Skipping event due to unexpected processing error: {e}")
|
|
|
|
| 89 |
continue
|
| 90 |
+
|
|
|
|
| 91 |
logging.info(f"Successfully processed {len(earthquake_list)} events.")
|
| 92 |
return JSONResponse(content={"earthquakes": earthquake_list})
|
| 93 |
|
| 94 |
except Exception as e:
|
| 95 |
logging.error(f"A major error occurred during the request: {e}")
|
| 96 |
+
raise HTTPException(status_code=500, detail=f"An internal error occurred: {e}")
|
|
|
|
|
|
|
|
|