-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools.py
312 lines (238 loc) · 8.99 KB
/
tools.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
#!/usr/bin/env python
###############################################################################
# Copyright (C) 2017-2019 Potsdam-Institute for Climate Impact Reasearch (PIK),
# Author: Torsten Albrecht ([email protected])
# License: GNU AFFERO GENERAL PUBLIC LICENSE version 3
#
# This script provides tools for the ensemble analysis
###############################################################################
from netCDF4 import Dataset as NC
import numpy as np
import config as cf; reload(cf)
#import matplotlib.pyplot as plt
#from matplotlib import cm, colors
#import matplotlib as mpl
################################################################
def ncload(filename):
try:
data = NC(filename, 'r')
except Exception:
print """Specify NetCDF input file."""
print filename
exit(-1)
return data
def get_data(fn,variables):
vardata = []
fndata = ncload(fn)
for varname in variables:
var = np.squeeze(fndata.variables[varname][:])
vardata.append(var)
return vardata
######### load observational data ###############################
def load_observations(of,ovf):
### Bedmap2
bedmap = ncload(of)
#global xobs,yobs,Mx,My,Hobs,hobs,Bobs,mobs,cellarea,latobs,lonobs
xobs = np.squeeze(bedmap.variables["x"][:])
yobs = np.squeeze(bedmap.variables["y"][:])
latobs = np.squeeze(bedmap.variables["lat"][:])
lonobs = np.squeeze(bedmap.variables["lon"][:])
Hobs = np.squeeze(bedmap.variables["thk"][:])
Bobs = np.squeeze(bedmap.variables["topg"][:])
hobs = np.ma.array(np.squeeze(bedmap.variables["usurf"][:]))
mobs = np.squeeze(bedmap.variables["mask"][:])
cellarea = np.squeeze(bedmap.variables["cell_area"][:])
#obsres = np.diff(xobs)[0]/1000.0 #(x[1]-x[0])/1000.0
Mx=np.shape(mobs)[0]
My=np.shape(mobs)[1]
bedmap.close()
### Rignot velocities
veldata = ncload(ovf)
#global velobs
velobs = np.ma.array(np.squeeze(veldata.variables["v_magnitude"][:]))
velstnd = np.ma.array(np.squeeze(veldata.variables["v_std"][:]))
velstnd[velstnd<1.5]=1.5 #lower bound
veldata.close()
return [xobs,yobs,Mx,My,mobs,Bobs,Hobs,hobs,lonobs,latobs,cellarea,velobs,velstnd]
################################################################
def get_xyz_coordinates(lonval,latval):
xc = np.cos (latval*np.pi/180.)*np.cos (lonval*np.pi/180.)
yc = np.cos (latval*np.pi/180.)*np.sin (lonval*np.pi/180.)
zc = np.sin (latval*np.pi/180.)
return (xc,yc,zc)
################################################################
def get_closest_point(lonval,latval,lonobs,latobs):
zdistmin = 1.e20
iupl=0
jupl=0
zxs,zys,zzs = get_xyz_coordinates(lonval,latval)
Mx,My = np.shape(lonobs)
#find the closest point on grid (not interpolated!)
for i in range(Mx):
for j in range(My):
zx,zy,zz = get_xyz_coordinates(lonobs[i,j],latobs[i,j])
zdist = np.sqrt ((zxs-zx)**2 + (zys-zy)**2 + (zzs-zz)**2)
if (zdist<zdistmin):
iupl = i
jupl = j
zdistmin = zdist
return (iupl,jupl)
#### calculate interpolated value
def get_interpolated_value(i,j,di,dj,val):
i2 = np.int(i+np.sign(di))
j2 = np.int(j+np.sign(dj))
di=np.abs(di)
dj=np.abs(dj)
#print np.shape(val),np.ndim(val)
if np.ndim(val)==2:
return (1.0-dj)*((1.0-di)*val[i,j] + di*val[i,j2]) + dj*((1.0-di)*val[i2,j] + di*val[i2,j2])
elif np.ndim(val)==3:
return (1.0-dj)*((1.0-di)*val[:,i,j] + di*val[:,i,j2]) + dj*((1.0-di)*val[:,i2,j] + di*val[:,i2,j2])
else:
print "No interpolation possible for dimensions of order "+str(np.dim(val))
return 0
################################################################
def find_interpolation(icl,jcl,lon0,lat0,lonobs,latobs):
lonij = lonobs[icl,jcl]
latij = latobs[icl,jcl]
xi,yi,zi = get_xyz_coordinates(lonij,latij)
x0,y0,z0 = get_xyz_coordinates(lon0,lat0)
#direct grid neighbors
xn,yn,zn = get_xyz_coordinates(lonobs[icl+1,jcl],latobs[icl+1,jcl])
xs,ys,zs = get_xyz_coordinates(lonobs[icl-1,jcl],latobs[icl-1,jcl])
xe,ye,ze = get_xyz_coordinates(lonobs[icl,jcl+1],latobs[icl,jcl+1])
xw,yw,zw = get_xyz_coordinates(lonobs[icl,jcl-1],latobs[icl,jcl-1])
dist0=np.zeros([2,2])
dist0[0,0] = np.sqrt ((xn-x0)**2 + (yn-y0)**2 + (zn-z0)**2)
dist0[1,0] = np.sqrt ((xs-x0)**2 + (ys-y0)**2 + (zs-z0)**2)
dist0[0,1] = np.sqrt ((xe-x0)**2 + (ye-y0)**2 + (ze-z0)**2)
dist0[1,1] = np.sqrt ((xw-x0)**2 + (yw-y0)**2 + (zw-z0)**2)
distb=np.zeros([2,2])
distb[0,0] = np.sqrt ((xn-xi)**2 + (yn-yi)**2 + (zn-zi)**2)
distb[1,0] = np.sqrt ((xs-xi)**2 + (ys-yi)**2 + (zs-zi)**2)
distb[0,1] = np.sqrt ((xe-xi)**2 + (ye-yi)**2 + (ze-zi)**2)
distb[1,1] = np.sqrt ((xw-xi)**2 + (yw-yi)**2 + (zw-zi)**2)
nni = np.int(np.sign(dist0[1,0]-dist0[0,0]))
nnj = np.int(np.sign(dist0[1,1]-dist0[0,1]))
#mapping of 1-> 0 and -1-> 1
indx = np.int(0.5*(1-nni))
indy = np.int(0.5*(1-nnj))
#print nni,nnj,indx,indy
dista = np.sqrt ((x0-xi)**2 + (y0-yi)**2 + (z0-zi)**2)
distb1 = distb[indx,0]
distb2 = distb[indy,1]
distc1 = dist0[indx,0]
distc2 = dist0[indy,1]
#formula of heron
s1 = 0.5* (dista + distb1 + distc1)
s2 = 0.5* (dista + distb2 + distc2)
di = 2.0 * np.sqrt(s1*(s1-dista)*(s1-distb1)*(s1-distc1)) / distb1 / distb2
dj = 2.0 * np.sqrt(s2*(s2-dista)*(s2-distb2)*(s2-distc2)) / distb2 / distb1
"""
fig14 = plt.figure(14,figsize=(8, 4)),plt.clf()
ax3=plt.subplot(111)
ax3.plot(lon0,lat0,"ro",markersize=15)
ax3.plot(lonij,latij,"go")
ax3.plot(lonobs[icl+nni,jcl+nnj],latobs[icl+nni,jcl+nnj],"bo")
ax3.plot(lonobs[icl+nni,jcl],latobs[icl+nni,jcl],"bo")
ax3.plot(lonobs[icl,jcl+nnj],latobs[icl,jcl+nnj],"bo")
lonint=get_interpolated_value(icl,jcl,nni*di,nnj*dj,lonobs)
latint=get_interpolated_value(icl,jcl,nni*di,nnj*dj,latobs)
ax3.plot(lonint,latint,"yo")
plt.show()
"""
return (nni*di,nnj*dj)
#############################################################
def get_mask_value(H,B,sl):
intmask=0.0
rhor = cf.rhoi/cf.rhosw
if (H > 0.0 and H*rhor > (sl-B)):
intmask = cf.mgr #2.0 # grounded
elif H > 0.0 and H*rhor <= (sl-B):
intmask = cf.mfl #3.0 # floating
elif (H == 0.0 and 0.0 <= (sl-B)):
intmask = cf.moc #4.0 #icefree ocean
return intmask
############################################################
def mask_without_icerises(data, start_coords, fill_value):
"""
Flood fill algorithm from https://gist.github.com/JDWarner/1158a9515c7f1b1c21f1
Parameters
----------
data : (M, N) ndarray of uint8 type
Image with flood to be filled. Modified inplace.
start_coords : tuple
Length-2 tuple of ints defining (row, col) start coordinates.
fill_value : int
Value the flooded area will take after the fill.
"""
xsize, ysize = data.shape
orig_value = data[start_coords[0], start_coords[1]]
stack = set(((start_coords[0], start_coords[1]),))
if fill_value == orig_value:
raise ValueError("Filling region with same value "
"already present is unsupported. "
"Did you already fill this region?")
while stack:
x, y = stack.pop()
if data[x, y] == orig_value:
data[x, y] = fill_value
if x > 0:
stack.add((x - 1, y))
if x < (xsize - 1):
stack.add((x + 1, y))
if y > 0:
stack.add((x, y - 1))
if y < (ysize - 1):
stack.add((x, y + 1))
return data
#####################################################################
def get_points_along_transect(xobs,yobs,dkm,transects):
trans_points=[]
for l,points in enumerate(transects):
pkm=[]
for pp in points:
pkm.append([xobs[np.int(pp[0])],yobs[np.int(pp[1])]])
dp=np.float(dkm)/np.float(cf.resolution)
dpr=dp
po=[]
po.append(points[0])
pox=po[0][0]
poy=po[0][1]
count=0
d=0
while count<len(points)-1:
xdir=np.sign(points[count+1][0]-points[count][0])
ydir=np.sign(points[count+1][1]-points[count][1])
my=np.float(points[count][1]-points[count+1][1])
mx=np.float(points[count][0]-points[count+1][0])
if mx==0:
dpx=0.0
dpy=ydir*dpr
mnew=np.nan
elif my==0:
dpy=0.0
dpx=xdir*dpr
mnew=0.0
else:
mnew=my/mx
dpy=ydir*np.sqrt((mnew*dpr)**2/(1.0+mnew**2))
dpx=dpy/mnew
dx=dpx*(xobs[np.int(points[count][0]+xdir)]-xobs[np.int(points[count][0])])
dy=dpy*(yobs[np.int(points[count][1]+ydir)]-yobs[np.int(points[count][1])])
px=dpx+pox
py=dpy+poy
if (xdir*(px-points[count+1][0])<=0.0 and ydir*(py-points[count+1][1])<=0.0):
po.append([px,py])
dpr=dp
pox=po[d+1][0]
poy=po[d+1][1]
else: #overshoot, next point, take rest distance
dpr=np.sqrt((px-points[count+1][0])**2+(py-points[count+1][1])**2)
pox=points[count+1][0]
poy=points[count+1][1]
count+=1
d-=1
d+=1
trans_points.append(po)
return trans_points