8
8
9
9
import jinja2
10
10
11
- R_0 = 6371. # Radius of the Earth (km).
11
+ R_0 = 6371.0 # Radius of the Earth (km).
12
12
13
13
14
14
def frange (start , stop , step ):
@@ -59,11 +59,12 @@ def field_to_csv(field, x_range, y_range, filename):
59
59
"""
60
60
with open (filename , 'w+' ) as csv_file :
61
61
for itt_y in y_range :
62
- csv_file .write (', ' .join ('%.2f' % field (x , itt_y ) for
63
- x in x_range ) + '\n ' )
62
+ csv_file .write (
63
+ ', ' .join ('%.2f' % field (x , itt_y ) for x in x_range ) + '\n '
64
+ )
64
65
65
66
66
- def generate_matrix (dim_x , dim_y , value = 0. ):
67
+ def generate_matrix (dim_x , dim_y , value = 0.0 ):
67
68
"""Generates a 2D list with the desired dimensions.
68
69
69
70
Args:
@@ -102,12 +103,15 @@ def great_arc_distance(coordinate_1, coordinate_2):
102
103
lng_2 = math .radians (lng_2 )
103
104
lat_2 = math .radians (lat_2 )
104
105
return (
105
- 2 * R_0 * math .asin (
106
+ 2
107
+ * R_0
108
+ * math .asin (
106
109
math .sqrt (
107
- (math .sin ((lat_2 - lat_1 ) / 2. ) ** 2 ) + (
108
- math .cos (lat_1 ) *
109
- math .cos (lat_2 ) *
110
- (math .sin ((lng_2 - lng_1 ) / 2. ) ** 2 )
110
+ (math .sin ((lat_2 - lat_1 ) / 2.0 ) ** 2 )
111
+ + (
112
+ math .cos (lat_1 )
113
+ * math .cos (lat_2 )
114
+ * (math .sin ((lng_2 - lng_1 ) / 2.0 ) ** 2 )
111
115
)
112
116
)
113
117
)
@@ -132,6 +136,7 @@ def interpolate_grid(points, dim_x, dim_y, d_x, d_y, spline_order=0):
132
136
data.
133
137
134
138
"""
139
+
135
140
def spline_0 (pos_x , pos_y , z_val ):
136
141
"""Zeroth order beta spline (i.e. nearest point)."""
137
142
return [(int (round (pos_x )), int (round (pos_y )), z_val )] # [(x, y, z)]
@@ -147,18 +152,19 @@ def spline_1(pos_x, pos_y, z_val):
147
152
(x_0 , y_0 , (x_0 + d_x - pos_x ) * (y_0 + d_y - pos_y ) * z_val ),
148
153
(x_1 , y_0 , (pos_x - x_0 ) * (y_0 + d_y - pos_y ) * z_val ),
149
154
(x_0 , y_1 , (x_0 + d_x - pos_x ) * (pos_y - y_0 ) * z_val ),
150
- (x_1 , y_1 , (pos_x - x_0 ) * (pos_y - y_0 ) * z_val )
155
+ (x_1 , y_1 , (pos_x - x_0 ) * (pos_y - y_0 ) * z_val ),
151
156
]
152
157
153
158
if spline_order == 0 :
154
159
spline = spline_0
155
160
elif spline_order == 1 :
156
161
spline = spline_1
157
162
else :
158
- raise ValueError ('Invalid spline order "%d" must be in (0, 1).' %
159
- spline_order )
163
+ raise ValueError (
164
+ 'Invalid spline order "%d" must be in (0, 1).' % spline_order
165
+ )
160
166
161
- grid = generate_matrix (dim_x , dim_y , 0. )
167
+ grid = generate_matrix (dim_x , dim_y , 0.0 )
162
168
163
169
for x_val , y_val , z_val in points :
164
170
x_coord = x_val / d_x
@@ -176,6 +182,7 @@ def spline_1(pos_x, pos_y, z_val):
176
182
def plot_vector_grid (filename , x_grid , y_grid ):
177
183
try :
178
184
import matplotlib
185
+
179
186
matplotlib .use ('Agg' )
180
187
import matplotlib .pyplot as plt
181
188
except ImportError :
@@ -192,13 +199,15 @@ def plot_vector_grid(filename, x_grid, y_grid):
192
199
y_coords .append (itt_y )
193
200
z_coords .append ((
194
201
x_grid [itt_y ][itt_x ],
195
- y_grid [itt_y ][itt_x ]
202
+ y_grid [itt_y ][itt_x ],
196
203
))
197
204
198
- plt .quiver (x_coords ,
199
- y_coords ,
200
- [x [0 ] for x in z_coords ],
201
- [y [1 ] for y in z_coords ])
205
+ plt .quiver (
206
+ x_coords ,
207
+ y_coords ,
208
+ [x [0 ] for x in z_coords ],
209
+ [y [1 ] for y in z_coords ],
210
+ )
202
211
fig .savefig (filename )
203
212
204
213
@@ -208,7 +217,8 @@ def get_grid_coordinates(lng, lat, domain, resolution):
208
217
length_y = int (abs (domain ['lat2' ] - domain ['lat1' ]) // resolution )
209
218
return (
210
219
int ((abs (lng - domain ['lng1' ])) // resolution ),
211
- length_y - int ((abs (lat - domain ['lat1' ])) // resolution ))
220
+ length_y - int ((abs (lat - domain ['lat1' ])) // resolution ),
221
+ )
212
222
213
223
214
224
class SurfaceFitter :
@@ -233,11 +243,11 @@ def __init__(self, x_points, y_points, z_points, kind='linear'):
233
243
self .points = list (zip (x_points , y_points , z_points ))
234
244
235
245
if kind == 'linear' :
236
- self .power = 1.
246
+ self .power = 1.0
237
247
elif kind == 'quadratic' :
238
- self .power = 2.
248
+ self .power = 2.0
239
249
elif kind == 'cubic' :
240
- self .power = 3.
250
+ self .power = 3.0
241
251
else :
242
252
raise ValueError ('"%s" is not a valid interpolation method' % kind )
243
253
@@ -254,7 +264,7 @@ def __call__(self, grid_x, grid_y):
254
264
z_val = z_point
255
265
break
256
266
else :
257
- weight = 1. / ((math .sqrt (d_x ** 2 + d_y ** 2 )) ** self .power )
267
+ weight = 1.0 / ((math .sqrt (d_x ** 2 + d_y ** 2 )) ** self .power )
258
268
sum_weight += weight
259
269
sum_value += weight * z_point
260
270
@@ -270,17 +280,20 @@ def parse_domain(domain):
270
280
'lng1' : bbox [0 ],
271
281
'lat1' : bbox [1 ],
272
282
'lng2' : bbox [2 ],
273
- 'lat2' : bbox [3 ]
283
+ 'lat2' : bbox [3 ],
274
284
}
275
285
276
286
277
287
def generate_html_map (filename , template_file , data , domain , resolution ):
278
288
with open (template_file , 'r' ) as template :
279
289
with open (filename , 'w+' ) as html_file :
280
- html_file .write (jinja2 .Template (template .read ()).render (
281
- resolution = resolution ,
282
- lng1 = domain ['lng1' ],
283
- lng2 = domain ['lng2' ],
284
- lat1 = domain ['lat1' ],
285
- lat2 = domain ['lat2' ],
286
- data = data ))
290
+ html_file .write (
291
+ jinja2 .Template (template .read ()).render (
292
+ resolution = resolution ,
293
+ lng1 = domain ['lng1' ],
294
+ lng2 = domain ['lng2' ],
295
+ lat1 = domain ['lat1' ],
296
+ lat2 = domain ['lat2' ],
297
+ data = data ,
298
+ )
299
+ )
0 commit comments