-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHotSpotLoader.py
138 lines (93 loc) · 4.13 KB
/
HotSpotLoader.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
import json
# The optimization workflow doesn't actually call the functions below that read from Shapefile
# so we won't require user to install 'shapefile' module.
# If that changes then we'll get an AttributeError when the function is called,
# in which case the try/except part should be removed.
try:
import shapefile as shp
except ImportError:
pass
def GetHotSpotTrails(FNAME):
shapeobj = shp.Reader(FNAME)
for i in range(len(shapeobj.fields)):
if 'Age' in shapeobj.fields[i]:
AgeInd = i-1
elif 'AgeError' in shapeobj.fields[i]:
AgeErrorInd = i-1
elif 'Chain' in shapeobj.fields[i]:
NameInd = i-1
elif 'PLATEID1' in shapeobj.fields[i]:
PlateIDInd = i-1
chains = {}
for shape, record in zip(shapeobj.shapes(), shapeobj.records()):
name = record[NameInd]
# Make the volcanic chain if necessary
if name not in chains:
chains[name] = {'lon': [], 'lat': [], 'age': [], 'age_error': [], 'plateid': []}
chains[name]['lon'].append(shape.points[0][0])
chains[name]['lat'].append(shape.points[0][1])
chains[name]['age'].append(record[AgeInd])
chains[name]['age_error'].append(record[AgeErrorInd])
chains[name]['plateid'].append(record[PlateIDInd])
# Sort the data in terms of age -------
import itertools as it
for name in chains:
if len(chains[name]['age']) < 2:
continue
sorted_lists = sorted(zip(chains[name]['lon'], chains[name]['lat'],
chains[name]['age'], chains[name]['age_error'],
chains[name]['plateid']),
key=lambda x: x[2])
chains[name]['lon'], chains[name]['lat'], chains[name]['age'], \
chains[name]['age_error'],chains[name]['plateid'] = zip(*sorted_lists)
return chains
def GetHotSpotLocations(FNAME):
shapeobj = shp.Reader(FNAME)
for i in range(len(shapeobj.fields)):
if 'NAME' in shapeobj.fields[i]:
NameInd = i-1
# Create the dictionary
hotspots = {}
for shape, record in zip(shapeobj.shapes(), shapeobj.records()):
name = record[NameInd]
hotspots[name] = {}
hotspots[name]['lon'] = shape.points[0][0]
hotspots[name]['lat'] = shape.points[0][1]
return hotspots
def GetHotSpotTrailsFromGeoJSON(FNAME):
with open(FNAME) as f:
data = json.load(f)
chains = {}
for feature in data['features']:
name = feature['properties']['Chain']
# Make the volcanic chain if necessary
if name not in chains:
chains[name] = {'lon': [], 'lat': [], 'age': [], 'age_error': [], 'plateid': []}
chains[name]['lon'].append(feature['geometry']['coordinates'][0])
chains[name]['lat'].append(feature['geometry']['coordinates'][1])
chains[name]['age'].append(feature['properties']['Age'])
chains[name]['age_error'].append(feature['properties']['AgeError'])
chains[name]['plateid'].append(feature['properties']['PLATEID1'])
# Sort the data in terms of age -------
import itertools as it
for name in chains:
if len(chains[name]['age']) < 2:
continue
sorted_lists = sorted(zip(chains[name]['lon'], chains[name]['lat'],
chains[name]['age'], chains[name]['age_error'],
chains[name]['plateid']),
key=lambda x: x[2])
chains[name]['lon'], chains[name]['lat'], chains[name]['age'], \
chains[name]['age_error'],chains[name]['plateid'] = zip(*sorted_lists)
return chains
def GetHotSpotLocationsFromGeoJSON(FNAME):
with open(FNAME) as f:
data = json.load(f)
# Create the dictionary
hotspots = {}
for feature in data['features']:
name = feature['properties']['NAME']
hotspots[name] = {}
hotspots[name]['lon'] = feature['geometry']['coordinates'][0]
hotspots[name]['lat'] = feature['geometry']['coordinates'][1]
return hotspots