-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_shakemap.py
executable file
·193 lines (156 loc) · 5.19 KB
/
test_shakemap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/env python3
# Copyright © 2021-2022 Helmholtz Centre Potsdam GFZ German Research Centre for
# Geosciences, Potsdam, Germany
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
import unittest
import shakemap
class TestReadShakemapGridData(unittest.TestCase):
"""
This is the test class for reading
the content from the grid_data text.
"""
def test_one_row_ints(self):
"""
Test with one row of integers.
"""
raw_data = "15 16 17"
result = list(shakemap.read_shakemap_data_from_str(raw_data))
self.assertEqual([15, 16, 17], result)
def test_two_rows_ints(self):
"""
Test with two rows of integers.
Here we use a real newline.
"""
raw_data = "15 16 17\n18 19 20"
result = list(shakemap.read_shakemap_data_from_str(raw_data))
self.assertEqual([15, 16, 17, 18, 19, 20], result)
def test_two_rows_without_newline_ints(self):
"""
Simulates the usage of two rows (2x3 values)
but the newline was replaced by a simple space.
"""
raw_data = "15 16 17 18 19 20"
result = list(shakemap.read_shakemap_data_from_str(raw_data))
self.assertEqual([15, 16, 17, 18, 19, 20], result)
def test_negative(self):
"""
Test with some negative numbers.
"""
raw_data = "15 -16 17 18 19 -20"
result = list(shakemap.read_shakemap_data_from_str(raw_data))
self.assertEqual([15, -16, 17, 18, 19, -20], result)
def test_quoted(self):
"""
Test with quoted values (double quotes).
"""
raw_data = (
'"0 1" 1 -71.4786 -33.0123 4 0 0.0 0.0 '
+ '"0 2" 2 -71.5396 -33.0543 48 0 0.0 0.0'
)
result = list(shakemap.read_shakemap_data_from_str(raw_data))
self.assertEqual(
[
"0 1",
1,
-71.4786,
-33.0123,
4,
0,
0.0,
0.0,
"0 2",
2,
-71.5396,
-33.0543,
48,
0,
0.0,
0.0,
],
result,
)
def test_quoted_single(self):
"""
Test with quoted values (single quotes).
"""
raw_data = (
"'0 1' 1 -71.4786 -33.0123 4 0 0.0 0.0 "
+ "'0 2' 2 -71.5396 -33.0543 48 0 0.0 0.0"
)
result = list(shakemap.read_shakemap_data_from_str(raw_data))
self.assertEqual(
[
"0 1",
1,
-71.4786,
-33.0123,
4,
0,
0.0,
0.0,
"0 2",
2,
-71.5396,
-33.0543,
48,
0,
0.0,
0.0,
],
result,
)
class TestShakemap(unittest.TestCase):
def test_read_ts_shakemap(self):
shake_map_ts = shakemap.Shakemaps.from_file(
"./testinputs/shakemap_tsunami.xml"
)
ts_provider = shake_map_ts.to_intensity_provider()
ts_intensity, ts_units = ts_provider.get_nearest(
lon=-71.547, lat=-32.9857
)
self.assertEqual("m", ts_units["INUN_MEAN_POLY"])
self.assertLess(1.598, ts_intensity["INUN_MEAN_POLY"])
self.assertLess(ts_intensity["INUN_MEAN_POLY"], 1.6)
def test_read_shakemap(self):
"""
Reads a normal shakemap (as it is the output of shakyground.
:return: None
"""
shake_map_eq = shakemap.Shakemaps.from_file(
"./testinputs/shakemap.xml"
)
eq_provider = shake_map_eq.to_intensity_provider()
self.assertIsNotNone(eq_provider)
eq_intensity, eq_units = eq_provider.get_nearest(
lon=-72.7, lat=-31.6416666667
)
self.assertEqual("g", eq_units["PGA"])
self.assertLess(0.028835543, eq_intensity["PGA"])
self.assertLess(eq_intensity["PGA"], 0.028835545)
def test_lahar_shakemap(self):
"""
Reads a lahar shakemap.
"""
shake_map_lahar = shakemap.Shakemaps.from_file(
"./testinputs/shakemap_lahar.xml"
)
lh_provider = shake_map_lahar.to_intensity_provider()
lh_intensity, lh_units = lh_provider.get_nearest(
lon=-78.64682219, lat=-0.864361486349
)
self.assertEqual("m/s", lh_units["MAXVELOCITY"])
self.assertLess(3.28, lh_intensity["MAXVELOCITY"])
self.assertLess(lh_intensity["MAXVELOCITY"], 3.30)
if __name__ == "__main__":
unittest.main()