3
3
from scipy import interpolate
4
4
from scipy import spatial
5
5
from pyproj import Proj
6
- import numpy as np
6
+ from numpy import unique , array , unravel_index , r_ , floor , interp , arange , \
7
+ sin , cos , deg2rad , arctan2 , sqrt , pi , zeros , reciprocal , ma , empty , \
8
+ concatenate
7
9
import logging
8
10
from ..tracking_objects import nearest
9
11
@@ -106,8 +108,8 @@ def read_nc_att(self, varname, att):
106
108
107
109
@property
108
110
def is_regular (self ):
109
- steps_lon = np . unique (self ._lon [0 , 1 :] - self ._lon [0 ,:- 1 ])
110
- steps_lat = np . unique (self ._lat [1 :, 0 ] - self ._lat [:- 1 , 0 ])
111
+ steps_lon = unique (self ._lon [0 , 1 :] - self ._lon [0 ,:- 1 ])
112
+ steps_lat = unique (self ._lat [1 :, 0 ] - self ._lat [:- 1 , 0 ])
111
113
return len (steps_lon ) == 1 and len (steps_lat ) == 1 and \
112
114
steps_lon [0 ] != 0. and steps_lat [0 ] != 0.
113
115
@@ -135,14 +137,14 @@ def kdt(lon, lat, limits, k=4):
135
137
136
138
Don't use cKDTree for regular grid
137
139
"""
138
- ppoints = np . array ([lon .ravel (), lat .ravel ()]).T
140
+ ppoints = array ([lon .ravel (), lat .ravel ()]).T
139
141
ptree = spatial .cKDTree (ppoints )
140
142
pindices = ptree .query (limits , k = k )[1 ]
141
- iind , jind = np . array ([], dtype = int ), np . array ([], dtype = int )
143
+ iind , jind = array ([], dtype = int ), array ([], dtype = int )
142
144
for pind in pindices .ravel ():
143
- j , i = np . unravel_index (pind , lon .shape )
144
- iind = np . r_ [iind , i ]
145
- jind = np . r_ [jind , j ]
145
+ j , i = unravel_index (pind , lon .shape )
146
+ iind = r_ [iind , i ]
147
+ jind = r_ [jind , j ]
146
148
return iind , jind
147
149
148
150
if 'AvisoGrid' in self .__class__ .__name__ :
@@ -152,16 +154,16 @@ def kdt(lon, lat, limits, k=4):
152
154
Used for a zero crossing, e.g., across Agulhas region
153
155
"""
154
156
if self .is_regular :
155
- i_1 = int (np . floor (np . interp ((lonmin - 0.5 ) % 360 ,
157
+ i_1 = int (floor (interp ((lonmin - 0.5 ) % 360 ,
156
158
self ._lon [0 ],
157
- np . arange (len (self ._lon [0 ])))))
158
- i_0 = int (np . floor (np . interp ((lonmax + 0.5 ) % 360 ,
159
+ arange (len (self ._lon [0 ])))))
160
+ i_0 = int (floor (interp ((lonmax + 0.5 ) % 360 ,
159
161
self ._lon [0 ],
160
- np . arange (len (self ._lon [0 ])))
162
+ arange (len (self ._lon [0 ])))
161
163
) + 1 )
162
164
else :
163
165
def half_limits (lon , lat ):
164
- return np . array ([[lon .min (), lon .max (),
166
+ return array ([[lon .min (), lon .max (),
165
167
lon .max (), lon .min ()],
166
168
[lat .min (), lat .min (),
167
169
lat .max (), lat .max ()]]).T
@@ -230,12 +232,12 @@ def haversine_dist(self, lon1, lat1, lon2, lat2):
230
232
Return:
231
233
distance (m)
232
234
"""
233
- sin_dlat = np . sin (np . deg2rad (lat2 - lat1 ) * 0.5 )
234
- sin_dlon = np . sin (np . deg2rad (lon2 - lon1 ) * 0.5 )
235
- cos_lat1 = np . cos (np . deg2rad (lat1 ))
236
- cos_lat2 = np . cos (np . deg2rad (lat2 ))
235
+ sin_dlat = sin (deg2rad (lat2 - lat1 ) * 0.5 )
236
+ sin_dlon = sin (deg2rad (lon2 - lon1 ) * 0.5 )
237
+ cos_lat1 = cos (deg2rad (lat1 ))
238
+ cos_lat2 = cos (deg2rad (lat2 ))
237
239
a_val = sin_dlon ** 2 * cos_lat1 * cos_lat2 + sin_dlat ** 2
238
- c_val = 2 * np . arctan2 (np . sqrt (a_val ), np . sqrt (1 - a_val ))
240
+ c_val = 2 * arctan2 (sqrt (a_val ), sqrt (1 - a_val ))
239
241
return 6371315.0 * c_val # Return the distance
240
242
241
243
def nearest_point (self , lon , lat ):
@@ -260,7 +262,7 @@ def get_aviso_f_pm_pn(self):
260
262
logging .info ('--- Computing Coriolis (f), d_x(p_m),'
261
263
'd_y (p_n) for padded grid' )
262
264
# Get GRAVITY / Coriolis
263
- self ._gof = np . sin (np . deg2rad (self .latpad )) * 4. * np . pi / 86400.
265
+ self ._gof = sin (deg2rad (self .latpad )) * 4. * pi / 86400.
264
266
self ._f_val = self ._gof .copy ()
265
267
self ._gof = self .GRAVITY / self ._gof
266
268
@@ -270,29 +272,29 @@ def get_aviso_f_pm_pn(self):
270
272
latv = self .half_interp (self .latpad [:- 1 ], self .latpad [1 :])
271
273
272
274
# Get p_m and p_n
273
- p_m = np . zeros_like (self .lonpad )
275
+ p_m = zeros (self .lonpad . shape )
274
276
p_m [:, 1 :- 1 ] = self .haversine_dist (lonu [:, :- 1 ], latu [:, :- 1 ],
275
277
lonu [:, 1 :], latu [:, 1 :])
276
278
p_m [:, 0 ] = p_m [:, 1 ]
277
279
p_m [:, - 1 ] = p_m [:, - 2 ]
278
280
self ._dx = p_m
279
- self ._pm = np . reciprocal (p_m )
281
+ self ._pm = reciprocal (p_m )
280
282
281
- p_n = np . zeros_like (self .lonpad )
283
+ p_n = zeros (self .lonpad . shape )
282
284
p_n [1 :- 1 ] = self .haversine_dist (lonv [:- 1 ], latv [:- 1 ],
283
285
lonv [1 :], latv [1 :])
284
286
p_n [0 ] = p_n [1 ]
285
287
p_n [- 1 ] = p_n [- 2 ]
286
288
self ._dy = p_n
287
- self ._pn = np . reciprocal (p_n )
289
+ self ._pn = reciprocal (p_n )
288
290
return self
289
291
290
292
def u2rho_2d (self , uu_in ):
291
293
"""
292
294
Convert a 2D field at u_val points to a field at rho points
293
295
"""
294
296
def uu2ur (uu_in , m_p , l_p ):
295
- u_out = np . zeros ((m_p , l_p ))
297
+ u_out = zeros ((m_p , l_p ))
296
298
u_out [:, 1 :- 1 ] = self .half_interp (uu_in [:, :- 1 ], uu_in [:, 1 :])
297
299
u_out [:, 0 ] = u_out [:, 1 ]
298
300
u_out [:, - 1 ] = u_out [:, - 2 ]
@@ -303,7 +305,7 @@ def uu2ur(uu_in, m_p, l_p):
303
305
def v2rho_2d (self , vv_in ):
304
306
# Convert a 2D field at v_val points to a field at rho points
305
307
def vv2vr (vv_in , m_p , l_p ):
306
- v_out = np . zeros ((m_p , l_p ))
308
+ v_out = zeros ((m_p , l_p ))
307
309
v_out [1 :- 1 ] = self .half_interp (vv_in [:- 1 ], vv_in [1 :])
308
310
v_out [0 ] = v_out [1 ]
309
311
v_out [- 1 ] = v_out [- 2 ]
@@ -372,13 +374,13 @@ def set_geostrophic_velocity(self, zeta):
372
374
zeta1 , zeta2 = zeta .data [1 :].view (), zeta .data [:- 1 ].view ()
373
375
pn1 , pn2 = self .p_n [1 :].view (), self .p_n [:- 1 ].view ()
374
376
self .upad [:] = self .v2rho_2d (
375
- np . ma .array ((zeta1 - zeta2 ) * 0.5 * (pn1 + pn2 ), mask = self .vmask ))
377
+ ma .array ((zeta1 - zeta2 ) * 0.5 * (pn1 + pn2 ), mask = self .vmask ))
376
378
self .upad *= - self .gof
377
379
378
380
zeta1 , zeta2 = zeta .data [:, 1 :].view (), zeta .data [:, :- 1 ].view ()
379
381
pm1 , pm2 = self .p_m [:, 1 :].view (), self .p_m [:, :- 1 ].view ()
380
382
self .vpad [:] = self .u2rho_2d (
381
- np . ma .array ((zeta1 - zeta2 ) * 0.5 * (pm1 + pm2 ), mask = self .umask ))
383
+ ma .array ((zeta1 - zeta2 ) * 0.5 * (pm1 + pm2 ), mask = self .umask ))
382
384
self .vpad *= self .gof
383
385
return self
384
386
@@ -388,13 +390,13 @@ def set_u_v_eke(self, pad=2):
388
390
"""
389
391
j_size = self .slice_j_pad .stop - self .slice_j_pad .start
390
392
if self .zero_crossing :
391
- u_1 = np . empty ((j_size , self .slice_i_pad .start ))
392
- u_0 = np . empty ((j_size , self ._lon .shape [1 ] - self .slice_i_pad .stop ))
393
- self .upad = np . ma .concatenate ((u_0 , u_1 ), axis = 1 )
393
+ u_1 = empty ((j_size , self .slice_i_pad .start ))
394
+ u_0 = empty ((j_size , self ._lon .shape [1 ] - self .slice_i_pad .stop ))
395
+ self .upad = ma .concatenate ((u_0 , u_1 ), axis = 1 )
394
396
else :
395
- self .upad = np . empty ((j_size ,
397
+ self .upad = empty ((j_size ,
396
398
self .slice_i_pad .stop - self .slice_i_pad .start ))
397
- self .vpad = np . empty_like (self .upad )
399
+ self .vpad = empty (self .upad . shape )
398
400
399
401
def get_eke (self ):
400
402
"""
@@ -412,7 +414,7 @@ def uspd(self):
412
414
if hasattr (uspd , 'mask' ):
413
415
uspd .mask += self .mask [self .view_unpad ]
414
416
else :
415
- uspd = np . ma .array (uspd , mask = self .mask [self .view_unpad ])
417
+ uspd = ma .array (uspd , mask = self .mask [self .view_unpad ])
416
418
return uspd
417
419
418
420
def set_interp_coeffs (self , sla , uspd ):
@@ -429,8 +431,8 @@ def set_interp_coeffs(self, sla, uspd):
429
431
def create_index_inverse (slice_to_inverse , size ):
430
432
"""Return an array of index
431
433
"""
432
- index = np . concatenate ((np . arange (slice_to_inverse .stop , size ),
433
- np . arange (slice_to_inverse .start )))
434
+ index = concatenate ((arange (slice_to_inverse .stop , size ),
435
+ arange (slice_to_inverse .start )))
434
436
return index
435
437
436
438
def gaussian_resolution (self , zwl , mwl ):
0 commit comments