-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEarthquakeDataReader.py
More file actions
110 lines (84 loc) · 3.62 KB
/
EarthquakeDataReader.py
File metadata and controls
110 lines (84 loc) · 3.62 KB
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
import vtk
import string
import time
class EarthquakeDataReader(object):
# Filter Latitude (x)
LatMax = 45.5 # from south(deg) 44.3
LatMin = 44.3 # to north(deg) 45.5
# Filter Longitude (y)
LonMax = 12.0 # from west(deg) 10.0
LonMin = 10.0 # to east(deg) 12
# Filter depth (z)
zMin = 0.0
zMax = 1.0
# Filter strength of earthquake
StrengthMin = 0
StrengthMax = 10
def set_strength_filter(self, min_strength, max_strength):
self.StrengthMin = min_strength
self.StrengthMax = max_strength
def get_strength_range(self):
return self.StrengthMin, self.StrengthMax
def get_bounds(self):
return self.LatMin, self.LatMax, self.LonMin, self.LonMax, self.zMin, self.zMax
# Read data points
def read_points(self, inputfile):
# all data
all_data = {}
# Open the file
infile = open(inputfile)
# Read one line
line = infile.readline()
# Loop through lines
while line:
# Split the line into data
data = line.split(';')
# Skip the commented lines
if data and data[0][0] != '#':
# Convert data into float
# print data[0], data[1], data[2], data[3], data[4].split('--')[0]
date, x, y, z, r = data[0].rstrip(';'), float(data[1].rstrip(';')), float(data[2].rstrip(';')), float(data[3].rstrip(';')), float(data[4].split('--')[0])
z /= 100
# Filter location range
# @see: http://www.zhang-liu.com/misl/map.html
# Latitude (x): from south(deg) 44.3 - north(deg) 45.5
# Longitude (y): from west(deg) 10.0 - east(deg) 12
if x < self.LatMin or x > self.LatMax or y < self.LonMin or y > self.LonMax:
# read next line
line = infile.readline()
continue
# Filter strength of earthquake
if r < self.StrengthMin or r > self.StrengthMax:
# read next line
line = infile.readline()
continue
# create one dataset for each month
# date string example: '2014-09-23 18:31:02.300'
year = date[:4]
month = date[5:7]
# create a containers for the given year and for the given month
if not all_data.has_key(year):
all_data[year] = {}
if not all_data.get(year).has_key(month):
all_data.get(year)[month] = {
'points': vtk.vtkPoints(),
'scalar': vtk.vtkFloatArray(),
'tid': vtk.vtkFloatArray()
}
row = string.split(date)
adate = row[0].split('-')
atime = row[1].split(':')
temp = atime[2].split('.')
atime[2] = temp[0]
if atime[2] == '':
atime[2] = '00'
t = time.mktime([int(adate[0]), int(adate[1]), int(adate[2]), int(atime[0]), int(atime[1]), int(atime[2]), 0, 0, 0])
if z > self.zMax:
self.zMax = z
# Insert floats into the point array
all_data.get(year)[month]['points'].InsertNextPoint(x, y, z)
all_data.get(year)[month]['scalar'].InsertNextValue(r)
all_data.get(year)[month]['tid'].InsertNextValue(t)
# read next line
line = infile.readline()
return all_data