Skip to content

Commit 899a413

Browse files
committed
Adding StationXML test case.
1 parent e438deb commit 899a413

File tree

5 files changed

+548
-0
lines changed

5 files changed

+548
-0
lines changed

stationxml_test/README.md

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# StationXML Test Case
2+
3+
This folder contains the test case for the evalresp integration in ObsPy.
4+
5+
The following is a short description of the files.
6+
7+
* **download_all_stations.py**: Downloads the StationXML files at the response
8+
level from IRIS. It requires an `all_stations.xml` file, a station level
9+
StationXML file which has to be downloaded separately.
10+
* **convert_to_SEED.sh**: Bash script converting all StationXML files to SEED
11+
using the Java tool by IRIS.
12+
* **evresp_process.py**: Running evalresp in a separate process to test whether
13+
it segfaults or not. Otherwise it would crash the current Python process.
14+
15+
* **test_response_large_scale.py**: The actual test case. It loops over every
16+
StationXML file in the *StationXML* subfolder and calculates the response for
17+
each channel using the ObsPy to evalresp bridge. These responses are compared
18+
to responses calculate by converting the SEED files in the *SEED* subfolder
19+
to RESP files and directly using evalresp with them.

stationxml_test/convert_to_SEED.sh

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env bash
2+
# -*- coding: utf-8 -*-
3+
#
4+
# Converts all StationXML files to SEED files.
5+
6+
folder="SEED/"
7+
extension=".seed"
8+
converter="~/Downloads/stationxml-converter-1.0.1.jar"
9+
10+
for i in StationXML/*.xml
11+
do
12+
name=`echo $i | cut -d'/' -f2`
13+
14+
filename=$(basename "$name")
15+
filename="${filename%.*}"
16+
17+
name=$folder$filename$extension
18+
19+
if [ ! -f $name ]
20+
then
21+
echo "Converting " $i
22+
java -jar $converter -s $i > $name
23+
fi
24+
25+
done
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
"""
4+
Downloads the response level StationXML file for all stations in the file
5+
'all_stations.xml' which contains a station level StationXML file.
6+
7+
:copyright:
8+
Lion Krischer ([email protected]), 2014
9+
:license:
10+
GNU Lesser General Public License, Version 3
11+
(http://www.gnu.org/copyleft/lesser.html)
12+
"""
13+
import colorama
14+
from obspy.station import read_inventory
15+
from obspy.fdsn import Client
16+
import os
17+
18+
output_dir = "StationXML"
19+
20+
c = Client()
21+
22+
inv = read_inventory("./all_stations.xml")
23+
24+
25+
def print_error(msg):
26+
print colorama.Fore.RED + msg + colorama.Fore.RESET
27+
28+
29+
def print_ok(msg):
30+
print colorama.Fore.GREEN + msg + colorama.Fore.RESET
31+
32+
33+
for network in inv.networks:
34+
for station in network.stations:
35+
output_filename = os.path.join(output_dir, "%s.%s.xml" %
36+
(network.code, station.code))
37+
if os.path.exists(output_filename):
38+
continue
39+
40+
try:
41+
out = c.get_stations(network=network.code, station=station.code,
42+
level="response")
43+
except:
44+
print_error("Failed to download %s.%s." % (network.code,
45+
station.code))
46+
continue
47+
with open(output_filename, "w") as fh:
48+
fh.write(out)
49+
print_ok("Downloaded %s.%s." % (network.code, station.code))

stationxml_test/evresp_process.py

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
"""
4+
Separate process to test for evalresp segfaults which otherwise interrupt the
5+
current Python process.
6+
7+
:copyright:
8+
Lion Krischer ([email protected]), 2014
9+
:license:
10+
GNU Lesser General Public License, Version 3
11+
(http://www.gnu.org/copyleft/lesser.html)
12+
"""
13+
from multiprocessing import Process
14+
from obspy.signal.invsim import evalresp
15+
16+
from obspy.core.util.misc import CatchOutput
17+
18+
import sys
19+
20+
21+
def test_evalresp_segfault(t_samp, filename, date, stat_id, chan_id, net_id,
22+
loc_id, units):
23+
out = None
24+
try:
25+
with CatchOutput() as out:
26+
evalresp(
27+
t_samp, 5, filename, date=date, station=stat_id,
28+
channel=chan_id, network=net_id, locid=loc_id,
29+
units=units, freq=True)
30+
except:
31+
if out and out.stderr and "are not supported" in out.stderr:
32+
sys.exit(99)
33+
else:
34+
sys.exit(1)
35+
36+
37+
def test_for_segfault(t_samp, filename, date, stat_id, chan_id, net_id, loc_id,
38+
units):
39+
40+
p = Process(target=test_evalresp_segfault,
41+
args=(t_samp, filename, date, stat_id, chan_id, net_id,
42+
loc_id, units))
43+
p.start()
44+
p.join()
45+
46+
p.terminate()
47+
p.terminate()
48+
p.terminate()
49+
50+
exitcode = p.exitcode
51+
filename.seek(0, 0)
52+
53+
if exitcode and exitcode not in [1, 99]:
54+
return "segfault"
55+
elif exitcode == 1:
56+
return "Failed to calculate response"
57+
elif exitcode == 99:
58+
return "uniterror"
59+
else:
60+
return None

0 commit comments

Comments
 (0)