Skip to content

Instantly share code, notes, and snippets.

@krischer
Created April 5, 2017 21:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krischer/f85692cbe9063dc2bdd514d2b7c04c84 to your computer and use it in GitHub Desktop.
Save krischer/f85692cbe9063dc2bdd514d2b7c04c84 to your computer and use it in GitHub Desktop.
Asynchronously download information about all stations worldwise and write them to a JSON file.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Requires Python >= 3.5, aiohttp, and pandas.
$ pip install aiohttp pandas
Upon running it will create a file 'networks_stations.json' which contains all
stations it could find across FDSN web service capable data centers. For each
station it contains the epochs and coordinates.
"""
import asyncio
from aiohttp import ClientSession
import io
import json
import pandas as pd
# All known nodes.
NODES = ["http://eida.bgr.de",
"http://eida.ethz.ch",
"http://service.geonet.org.nz",
"https://geofon.gfz-potsdam.de",
"http://webservices.rm.ingv.it",
"http://eida.ipgp.fr",
"http://eida.koeri.boun.edu.tr",
"http://erde.geophysik.uni-muenchen.de",
"http://service.ncedc.org",
"http://eida-sc3.infp.ro",
"http://eida.gein.noa.gr",
"http://ws.resif.fr",
"https://service.scedc.caltech.edu",
"http://sismo.iag.usp.br",
"http://www.orfeus-eu.org",
"http://service.iris.edu"]
async def fetch(url, session):
async with session.get(url) as response:
if response.status != 200:
print("'%s' return code %i - will be ignored." %
(url, response.status))
return None
r = await response.read()
return (url, r)
async def run():
tasks = []
async with ClientSession() as session:
for url in NODES:
_url = url + "/fdsnws/station/1/query?level=station&format=text"
task = asyncio.ensure_future(fetch(_url, session))
tasks.append(task)
responses = await asyncio.gather(*tasks)
return responses
loop = asyncio.get_event_loop()
future = asyncio.ensure_future(run())
inventories = [_i for _i in loop.run_until_complete(future) if _i]
print("Downloaded %i inventories." % len(inventories))
_inv = []
for _i in inventories:
with io.BytesIO(_i[1]) as buf:
buf.seek(0, 0)
try:
csv = pd.read_csv(buf, sep="|")
csv.columns = ["network", "station", "latitude", "longitude",
"elevation", "site_name", "starttime", "endtime"]
_inv.append(csv)
except:
print("Failed to parse result from '%s'. Invalid CSV file?" %
_i[0])
inv = pd.concat(_inv)
result = {}
# Get unique stations and get all epochs.
for key, value in inv.sort_values(by=["network", "station"]).groupby(
["network", "station"]):
# Safe-guard.
assert key not in result
del value["network"]
del value["station"]
del value["site_name"]
value.drop_duplicates()
result["%s.%s" % key] = list(value.T.to_dict().values())
with open("networks_stations.json", "wt") as fh:
json.dump(result, fh, indent=4)
print("Written to 'networks_stations.json'.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment