Commit d5ec7a38 authored by Marius Kriegerowski's avatar Marius Kriegerowski
Browse files

use obspy for origin calculation

parent bffcef4a
......@@ -4,11 +4,9 @@ LABEL maintainer="marius.kriegerowski@gfz-potsdam.de"
WORKDIR /usr/src/loss-calculator
RUN apt-get clean && apt-get -y update && apt-get install -y libgdal-dev
RUN apt-get clean && apt-get -y update && apt-get install -y libgdal-dev sqlite3 libsqlite3-mod-spatialite
# Install dependencies first to improve layer cacheing
RUN pip install --no-cache-dir numpy~=1.21
COPY setup.py .
RUN pip install --no-cache-dir .
......
......@@ -12,7 +12,8 @@ region of interest and fragility functions that model the probability of exceedi
* `scipy`
* `pyproj`
* `shapely`
* `GDAL` header files. (can be installed with `apt-get install libgdal-dev`)
* `sqlite3` and the `libsqlite3-mod-spatialite` extension
* `GDAL` header files. (`apt-get install libgdal-dev`)
## Usage
......
import argparse
import asyncio
import copy
import logging
import os
import shutil
......@@ -12,14 +13,17 @@ from fastapi import FastAPI, Body, HTTPException
from fastapi.responses import FileResponse
from geopy.adapters import AioHTTPAdapter
from geopy.geocoders import Nominatim
from pyrocko.io import quakeml
from starlette.background import BackgroundTask
from losscalculator.damage_calculator import damage_calculator
from losscalculator.ground_motion import GMField
from obspy.io.quakeml.core import Unpickler
logger = logging.getLogger(__name__)
QuakeMLUnpickler = Unpickler()
app = FastAPI()
DATA_PATH = os.environ.get("LOSSCALCULATOR_DATA", None)
......@@ -82,13 +86,14 @@ class ShakemapiService:
async def region_from_event(event_quakeml: str) -> str:
"""Queries OSM API for country information of a given QuakeML event."""
event = await quakeml_to_event(event_quakeml)
event_quakeml = copy.deepcopy(event_quakeml)
origin = await quakeml_to_event(event_quakeml.encode())
async with Nominatim(
user_agent="losscalculator",
adapter_factory=AioHTTPAdapter,
) as geolocator:
location = await geolocator.reverse(f"{event.lat},{event.lon}")
location = await geolocator.reverse(f"{origin.latitude},{origin.longitude}")
country = location.raw["address"]["country"].lower()
......@@ -98,11 +103,17 @@ async def region_from_event(event_quakeml: str) -> str:
async def quakeml_to_event(event_quakeml):
"""Loads a QuakeML event and extracts the info as pyrocko event."""
catalog = quakeml.QuakeML.load_xml(string=event_quakeml)
events = catalog.get_pyrocko_events()
assert len(events) == 1, "QuakeML Example needs to have exactly one event in it"
event = events[0]
return event
catalog = QuakeMLUnpickler.loads(string=event_quakeml)
assert len(catalog.events) == 1, "QuakeML Example needs to have exactly one event in it"
event = catalog.events[0]
origin = event.preferred_origin()
if origin is None:
if len(event.origins) > 1:
logger.warning("event quakeml contains more than one origin. Taking the first.")
origin = event.origins[0]
return origin
@app.post("/loss")
......
......@@ -22,7 +22,7 @@ setup(
"fastapi~=0.70",
"requests~=2.26",
"uvicorn~=0.15",
"pyrocko~=2021.9.14",
"obspy~=1.2.2",
"geopy~=2.2.0",
"aiohttp~=3.8.1",
],
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment