-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanalysis.py
348 lines (313 loc) · 10.2 KB
/
analysis.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
### Functions to help with ADCP data analysis
__author__ = 'Antonin Affholder'
## Imports
from mpl_toolkits.basemap import Basemap
import numpy as np
import netCDF4 as nc
import datetime as dt
def ComputeATD(lon,lat,m = Basemap(projection='merc')):
"""
Computes the along track distance between the given points
"""
x,y = m(lon,lat)
atd = np.cumsum(np.sqrt( (x[1:] - x[:-1])**2 + (y[1:] - y[:-1])**2 ))
atd = np.append(np.array([0]),atd)
return(atd)
def RegularGrid(matrix,x,dx,y):
"""
regularaizes the grid
"""
xreg = np.arange(x[0] + dx/2,x[-1] - dx/2,dx)
matrix_reg = np.full((len(xreg),len(y)),np.nan)
for j in range(len(y)):
for i in range(len(xreg)):
val = xreg[i] #value to interpolate to
valsup = val + dx/2
valinf = val - dx/2
#find the points laying in the interval
indexes = np.where((x >= valinf) * (x <= valsup))
if len(indexes) == 0:
fill = np.nan
elif np.sum(np.isfinite(matrix[indexes,j])) == 0:
fill = np.nan
else:
fill = np.nanmean(matrix[indexes,j])
matrix_reg[i,j] = fill
return(matrix_reg,xreg)
def RegularLine(x,y,dx):
xreg = np.arange(min(x) + dx/2,max(x) - dx/2,dx)
matrix_reg = np.full(len(xreg),np.nan)
for i in range(len(xreg)):
val = xreg[i]
valsup = val + dx/2
valinf = val - dx/2
#find the points laying in the interval
indexes = np.where((x >= valinf) * (x <= valsup))
if len(indexes) == 0:
fill = np.nan
elif np.sum(np.isfinite(y[indexes])) == 0:
fill = np.nan
else:
fill = np.nanmean(y[indexes])
matrix_reg[i] = fill
return(matrix_reg,xreg)
def RegularGrid2D(matrix,x,y,dx,scale=1e3):
"""
regularaizes the grid
x and y have same size
dx is in both directions
Problems with empty squares
"""
# x axis
minxreg = np.floor(min(x)/scale)*scale
maxxreg = np.ceil(max(x)/scale)*scale
xbins = np.ceil((maxxreg-minxreg)/dx)
xsreg = np.arange(minxreg + dx/2,minxreg + xbins*dx - dx/2,dx)
# y axis
minyreg = np.floor(min(y)/scale)*scale
maxyreg = np.ceil(max(y)/scale)*scale
ybins = np.ceil((maxyreg-minyreg)/dx)
ysreg = np.arange(minyreg + dx/2,minyreg + ybins*dx - dx/2,dx)
# New coordinates
xreg = []
yreg = []
regmatrix = []
for i in range(len(xsreg)):
for j in range(len(ysreg)):
# make the square in which we retrieve the data
xregval = xsreg[i]
yregval = ysreg[j]
data_index = np.where(((x >= xregval - dx/2) * (x <= xregval + dx/2)) *
((y >= yregval - dx/2) * (y <= yregval + dx/2)))[0]
if len(data_index)>0:
wcolumn = np.nanmean(matrix[data_index,:],axis=0)
xreg.append(xregval)
yreg.append(yregval)
regmatrix.append(wcolumn)
xreg = np.array(xreg)
yreg = np.array(yreg)
regmatrix = np.array(regmatrix)
return(regmatrix,xreg,yreg)
def RegularGrid3D(matrix,x,y,dx,zbins,scale=1e3):
"""
regularaizes the grid
x and y have same size
dx is in both directions
"""
# x axis
minxreg = np.floor(min(x)/scale)*scale
maxxreg = np.ceil(max(x)/scale)*scale
xbins = np.ceil((maxxreg-minxreg)/dx)
xsreg = np.arange(minxreg + dx/2,minxreg + xbins*dx - dx/2,dx)
# y axis
minyreg = np.floor(min(y)/scale)*scale
maxyreg = np.ceil(max(y)/scale)*scale
ybins = np.ceil((maxyreg-minyreg)/dx)
ysreg = np.arange(minyreg + dx/2,minyreg + ybins*dx - dx/2,dx)
# New coordinates
xreg = []
yreg = []
regmatrix = np.full((len(xsreg),len(ysreg),zbins),np.nan)
for i in range(len(xsreg)):
for j in range(len(ysreg)):
# make the square in which we retrieve the data
xregval = xsreg[i]
yregval = ysreg[j]
data_index = np.where(((x >= xregval - dx/2) * (x <= xregval + dx/2)) *
((y >= yregval - dx/2) * (y <= yregval + dx/2)))[0]
if len(data_index)>0:
wcolumn = np.nanmean(matrix[data_index,:],axis=0)
regmatrix[i,j,:] = wcolumn
return(regmatrix,xsreg,ysreg)
def BoxCarFilter(M,bins_x,bins_y):
"""
Boxcar filter, or blur
Every point is the average of the box x_len,ylen
M is the field matrix of size = (len(X),len(Y))
bins_x is the HALF of the horizontal size of the box in bins
bins_y is the HALF of the vertical size of the box in bins
"""
xl,yl = M.shape
boxdim = 2*bins_x*2*bins_y
Mp = np.zeros((xl,yl))
for i in range(xl):
for j in range(yl):
xmin = i - bins_x
ymin = j - bins_y
xmax = i + bins_x
ymax = j + bins_y
if xmin < 0:
xmin = 0
if ymin < 0:
ymin = 0
if xmax > xl:
xmax = -1
if ymax > yl:
ymax = -1
box = M[xmin : xmax, ymin : ymax]
if np.sum(np.isfinite(box)) < boxdim/2:
avg = np.nan
else:
avg = np.nanmean(box)
Mp[i,j] = avg
return(Mp)
def BoxCarFilter3D(M,x,y,z,xdist,ydist,zdist):
"""
This is not supposed to work
"""
xl,yl,zl = M.shape
Mp = np.zeros((xl,yl,zl)) # should be nans
dx = xdist/2
dy = ydist/2
dz = zdist/2
xbins = np.ceil(xdist/np.nanmean(x[1:] - x[:-1]))
ybins = np.ceil(ydist/np.nanmean(y[1:] - y[:-1]))
zbins = np.ceil(zdist/np.nanmean(z[1:] - z[:-1]))
boxsize = xbins*ybins*zbins
for i in range(xl):
for j in range(yl):
for k in range(zl):
xval = x[i]
yval = y[j]
zval = z[k]
indexes = np.where( ( x >= xval - dx)*( x <= xval + dx)*
( y >= yval - dy)*( y <= yval + dy)*
( z >= zval - dz)*( z <= zval + dz))
if np.sum(np.isfinite(M[indexes])) < boxsize/6:
val = np.nan
else:
val = np.nanmean(M[indexes])
Mp[i,j,k] = avg
return(Mp)
def BoxCarFilter2(M,x,y,z,xdist,ydist,zdist):
"""
Boxcar filter, or blur
Taking into account that the data is distributed in a 3D space
means the spheroid
x y same size but not M
M is 2D
"""
X = []
Y = []
Z = []
# Vl = []
for i in range(len(x)):
for j in range(len(z)):
X.append(x[i])
Y.append(y[i])
Z.append(z[j])
# Vl.append(V[i,j])
X = np.array(X)
Y = np.array(Y)
Z = np.array(Z)
# Vl = np.array(Vl)
Mf = M.flatten()
xl,zl = M.shape
dx = xdist/2
dy = ydist/2
dz = zdist/2
xbins = np.ceil(xdist/np.nanmean(x[1:] - x[:-1]))
ybins = np.ceil(ydist/np.nanmean(y[1:] - y[:-1]))
zbins = np.ceil(zdist/np.nanmean(z[1:] - z[:-1]))
boxsize = xbins*zbins
Mp = np.full(len(X),np.nan)
for i in range(len(X)):
xval = X[i]
yval = Y[i]
zval = Z[i]
indexes = np.where( ( X >= xval - dx)*( X <= xval + dx)*
( Y >= yval - dy)*( Y <= yval + dy)*
( Z >= zval - dz)*( Z <= zval + dz))[0]
if np.sum(np.isfinite(Mf[indexes])) < boxsize/8:
val = np.nan
else:
val = np.nanmean(Mf[indexes])
Mp[i] = val
Mpuf = np.reshape(Mp,(xl,zl))
return(Mpuf)
def FindMaxMin(V,atd,depths,depthlim = [0,3000]):
"""
"""
atd_min = []
atd_max = []
dpt = []
depthmin = depthlim[0]
depthmax = depthlim[1]
subdepths = depths[(depths > depthmin) * (depths < depthmax)]
for i in range(len(subdepths)):
index = np.where(depths == subdepths[i])
vs = V[:,index]
if np.sum(np.isfinite(vs)) > 0:
min_index = np.where(vs == np.nanmin(vs))[0][0]
max_index = np.where(vs == np.nanmax(vs))[0][0]
dpt.append(subdepths[i])
atd_min.append(atd[min_index])
atd_max.append(atd[max_index])
atd_min = np.array(atd_min)
atd_max = np.array(atd_max)
dpt = np.array(dpt)
return(atd_min,atd_max,dpt)
def TrackSSH(lon,lat,SSH,date,mid=True):
"""
Gives the SLA measured on the ship's track at date
SSH is a netcdf file
date is datetime date
"""
tunits = SSH['time'].units
time = nc.num2date(SSH['time'][:],tunits)
if mid: #12hrs offset between date and dates in time
time_index = np.where(time == date + dt.timedelta(hours=12))[0][0]
else:
time_index = np.where(time == date)[0][0]
ssh = []
xstep = SSH['longitude'].step
ystep = SSH['latitude'].step
hxstep = np.round(xstep/2,3)
hystep = np.round(ystep/2,3)
ssh_xaxis = SSH['longitude'][:]
ssh_yaxis = SSH['latitude'][:]
zos = SSH['zos'][time_index,:,:]
for i in range(len(lon)):
longitude = lon[i]
latitude = lat[i]
lg_idx = np.where((ssh_xaxis >= longitude - hxstep)*(ssh_xaxis <= longitude + hxstep))[0][0]
lt_idx = np.where((ssh_yaxis >= latitude - hystep)*(ssh_yaxis <= latitude + hystep))[0][0]
ssh.append(zos[lt_idx,lg_idx])
ssh = np.array(ssh)
return(ssh)
def VirtualCenter(v,orientation='EW',deltat=10):
"""
Finds the hodograph virtual center index
"""
#U_int = np.nancumsum(u)*deltat
V_int = np.nancumsum(v)*deltat
if orientation == 'EW':
# Anticylonic case and EW section
y_max = np.nanmax(np.abs(V_int))
index = np.where(V_int == y_max)[0]
if len(index)>0:
index = index[0]
else:
index = False
elif orientation == 'SN':
x_max = np.nanmax(np.abs(U_int))
index = np.where(U_int == x_max)[0]
if len(index)>0:
index = index[0]
else:
index = False
return(index)
def RadialVorticity(v,x):
'''
Poor man's vorticity
'''
zeta = []
for j in range(len(x)):
Vs = v[j,:]
Bs = Vs/x[j]
zetas = 2*np.arctan(Bs)
zeta.append(zetas)
zeta = np.array(zeta)
return(zeta)