-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
241 lines (185 loc) · 6.88 KB
/
util.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#utilitary lib
import os, sys
import math
import csv
language = 'es-US'
def intToRoman(intVal):
numbers = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
letters = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"]
if intVal < 1 or intVal > 3999 :
return str(intVal)
roman = ""
for i in range(0,len(numbers),1):#(int i = 0; i < numbers.length; i++) {
while intVal >= numbers[i]:
roman += letters[i];
intVal -= numbers[i];
return roman
def intToColorDescription(intVal):
if intVal >12 or intVal<0:
intVal = -1
colorDict = {
-1: "--;#FFFFFF",
0: "--;#D3D3D3",
1: "I. No Sentido;#FFFFFF",
2: "II. Muy Débil;#BFCCFF",
3: "III. Leve;#9999FF",
4: "IV. Moderado;#80FFFF",
5: "V. Poco Fuerte;#7DF894",
6: "VI. Fuerte;#FFFF00",
7: "VII. Muy Fuerte;#FFC800",
8: "VIII. Destructivo;#FF9100",
9: "IX. Muy Destructivo;#FF0000",
10: "X. Desastroso;#C80000",
11: "XI. Muy Desastroso;#800000",
12: "XII. Catastrófico;#000000"
}
return colorDict[intVal]
def ipe_allen2012_hyp(epiDistance, magnitude, depth):#hypoDistance km
a = 2.085;
b = 1.428;
c = -1.402;
d = 0.078;
s = 1.0;
m1=-0.209;
m2=2.042;
if depth<0 :
return -1
#Obtaining the hypocentral distance
#The form is a triangle - Can be improved(?)
hypoDistance = math.sqrt(math.pow(epiDistance,2)+math.pow(depth,2))
rm = m1+m2 * math.exp(magnitude-5)
if hypoDistance <= 50 :
I = a + b*magnitude + c* math.log( math.sqrt( math.pow(hypoDistance,2) + math.pow(rm,2)))+s
else:
I = a + b*magnitude + c * math.log( math.sqrt( math.pow(hypoDistance,2) + math.pow(rm,2)))+d* math.log(hypoDistance/50)+s
if hypoDistance <= 50 :
I2 = a + b*magnitude + c * math.log( math.sqrt( math.pow(hypoDistance,2) + math.pow(rm,2)))+d* math.log(hypoDistance/50)+s
else:
I2 = a + b*magnitude + c* math.log( math.sqrt( math.pow(hypoDistance,2) + math.pow(rm,2)))+s
intensity = round(I) # intensity (MMI, float)
intensity2 = round(I2) # intensity (MMI, float)
#print("Values: ",I, I2,intensity, intensity2, depth, magnitude,hypoDistance, epiDistance)
if intensity>12:
return 12
elif intensity<0:
return 0
else:
return I
def ipe_allen2012_hyp_sigma(epiDistance, depth):
#Constants
s1 = 0.82
s2 = 0.37
s3 = 22.9
hypoDistance = math.sqrt(math.pow(epiDistance,2)+math.pow(depth,2))
sigma = s1 + s2/(1 + math.pow( hypoDistance / s3,2))
return sigma
def distanceEpiToPoint(epiLat, epiLon, lat, lon):
rEpiLat = math.radians(epiLat)
rEpiLon = math.radians(epiLon)
rLat = math.radians(lat)
rLon = math.radians(lon)
distance = math.acos( math.sin(rEpiLat) * math.sin(rLat) + math.cos(rEpiLat) * math.cos(rLat) * math.cos(rEpiLon-rLon))*6371;
return int(distance)
def distanceHypoToPoint(epiLat, epiLon, depth, lat, lon):
rEpiLat = math.radians(epiLat)
rEpiLon = math.radians(epiLon)
rLat = math.radians(lat)
rLon = math.radians(lon)
distance = math.acos( math.sin(rEpiLat) * math.sin(rLat) + math.cos(rEpiLat) * math.cos(rLat) * math.cos(rEpiLon-rLon))*6371;
hypoDist = math.sqrt(distance*distance + depth*depth)
return int(hypoDist)
def findNearestPlace( data, epiLat, epiLon ):
try:
return min( data, key=lambda p: distance( [epiLat, float(p['lat']), epiLon, float(p['lon'])] ) )
except Exception as e:
print(repr(e))
return None
def azimuth( points ):
#points is a list [refLat, epiLat, refLon, epiLon]
refLat, epiLat, refLon, epiLon = map(math.radians, points)
dist_lats = epiLat - refLat
dist_longs = epiLon - refLon
azRad = math.atan2( math.sin(dist_longs)*math.cos(epiLat) , math.cos( refLat)*math.sin( epiLat ) - math.sin( refLat ) * math.cos( epiLat ) * math.cos ( dist_longs) )
azDeg = azRad * 180 / math.pi
if azDeg < 0:
azimuth = 360 + azDeg
else:
azimuth = azDeg
return int(round(azimuth))
def direction(azVal, language ):
if azVal >= 0 and azVal<10:
return "N"
elif azVal >= 10 and azVal < 40:
return "NNE"
elif azVal >= 40 and azVal < 50:
return "NE"
elif azVal >= 50 and azVal < 80:
return "ENE"
elif azVal >= 80 and azVal < 100:
return "E"
elif azVal >= 100 and azVal < 130:
return "ESE"
elif azVal >= 130 and azVal < 140:
return "SE"
elif azVal >= 140 and azVal < 170:
return "SSE"
elif azVal >= 170 and azVal < 190:
return "S"
elif azVal >= 190 and azVal < 220:
if language == 'es-US':
return "SSO"
else:
return "SSW"
elif azVal >= 220 and azVal < 230:
if language == 'es-US':
return "SO"
else:
return "SW"
elif azVal >= 230 and azVal < 260:
if language == 'es-US':
return "OSO"
else:
return "WSW"
elif azVal >= 260 and azVal < 280:
if language == 'es-US':
return "O"
else:
return "W"
elif azVal >= 280 and azVal < 310:
if language == 'es-US':
return "ONO"
else:
return "WNW"
elif azVal >= 310 and azVal < 320:
if language == 'es-US':
return "NO"
else:
return "NW"
elif azVal >= 320 and azVal < 350:
if language == 'es-US':
return "NNO"
else:
return "NNW"
elif azVal >= 350 and azVal <= 360:
return "N"
def location( distKm, azText, city, country, language ):
if language == 'es-US':
return str(distKm)+' km al '+ azText + ' de '+ city + ', '+ country
else:
return str(distKm)+' km '+ azText + ' of '+ city + ', '+ country
def distance( points ):
#points is a list [refLat, epiLat, refLon, epiLon]
refLat, epiLat, refLon, epiLon = map(math.radians, points)
dist_lats = epiLat - refLat
dist_longs = epiLon - refLon
a = math.sin(dist_lats/2)**2 + math.cos(refLat) * math.cos(epiLat) * math.sin(dist_longs/2)**2
c = 2 * math.atan2 ( math.sqrt(a), math.sqrt(1 - a) )
radius_earth = 6371 # the Earth's radius
distance = c * radius_earth # distance in km
#print(int(round(distance)))
return int(round(distance))
def csvFile2dic( filename ):
with open(filename, encoding = 'utf8') as f:
file_data=csv.reader(f)
headers=next(file_data)
return [dict(zip(headers,i)) for i in file_data]