-
Notifications
You must be signed in to change notification settings - Fork 0
/
interp2D.py
executable file
·156 lines (113 loc) · 5.59 KB
/
interp2D.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
import numpy as np
import datetime
#import mpl_toolkits.basemap as mp
import extrapolate as ex
try:
import ESMF
except ImportError:
print("Could not find module ESMF")
pass
__author__ = 'Trond Kristiansen'
__email__ = '[email protected]'
__created__ = datetime.datetime(2008, 12, 4)
__modified__ = datetime.datetime(2014, 8, 20)
__version__ = "1.3"
__status__ = "Development"
def laplaceFilter(field, threshold, toxi, toeta):
undef=2.0e+35
tx=0.9*undef
critx=0.01
cor=1.6
mxs=10
field=np.where(abs(field)>threshold,undef,field)
field=ex.extrapolate.fill(int(1),int(toxi),
int(1),int(toeta),
float(tx), float(critx), float(cor), float(mxs),
np.asarray(field, order='Fortran'),
int(toxi),
int(toeta))
return field
def doHorInterpolationRegularGrid(myvar, grdROMS, grdMODEL, mydata, show_progress, useFilter):
if show_progress is True:
import progressbar
#http://progressbar-2.readthedocs.org/en/latest/examples.html
progress = progressbar.ProgressBar(widgets=[progressbar.Percentage(), progressbar.Bar()], maxval=grdMODEL.Nlevels).start()
#progress = progressbar.ProgressBar(widgets=[progressbar.BouncingBar(marker=progressbar.RotatingMarker(), fill_left=True)], maxval=grdMODEL.Nlevels).start()
indexROMS_Z_ST = (grdMODEL.Nlevels, grdROMS.eta_rho, grdROMS.xi_rho)
array1=np.zeros((indexROMS_Z_ST), dtype=np.float64)
for k in range(grdMODEL.Nlevels):
if (grdMODEL.useESMF):
grdMODEL.fieldSrc.data[:,:]=np.flipud(np.rot90(np.squeeze(mydata[k,:,:])))
# Get the actual regridded array
field = grdMODEL.regridSrc2Dst_rho(grdMODEL.fieldSrc, grdMODEL.fieldDst_rho)
# Since ESMF uses coordinates (x,y) we need to rotate and flip to get back to (y,x) order.
field = np.fliplr(np.rot90(field.data,3))
else:
i0 = np.argmin(np.fabs(grdMODEL.lon[0,:]-180))
mydataout = np.ma.zeros(np.squeeze(mydata[k,:,:]).shape,mydata.dtype)
lonsout = np.ma.zeros((len(grdMODEL.lon[0,:])),grdMODEL.lon.dtype)
lonsout[0:len(grdMODEL.lon[0,:])-i0] = grdMODEL.lon[0,i0:]-360
lonsout[len(grdMODEL.lon[0,:])-i0:] = grdMODEL.lon[0,1:i0+1]
mydataout[:,0:len(grdMODEL.lon[0,:])-i0] = mydata[k,:,i0:]
mydataout[:,len(grdMODEL.lon[0,:])-i0:] = mydata[k,:,1:i0+1]
field = mp.interp(mydataout,lonsout,grdMODEL.lat[:,0],grdROMS.lon_rho,grdROMS.lat_rho,
checkbounds=False, masked=False, order=1)
if (useFilter):
field = laplaceFilter(field, 1000, grdROMS.xi_rho, grdROMS.eta_rho)
# field=field*grdROMS.mask_rho
array1[k,:,:]=field
# if k in [34,17,2]:
# import plotData
# plotData.contourMap(grdROMS, grdROMS.lon_rho, grdROMS.lat_rho, field, str(k)+'_withfilter', myvar)
# if __debug__:
# print "Data range after horisontal interpolation: ", field.min(), field.max()
if show_progress is True:
progress.update(k)
return array1
def doHorInterpolationSSHRegularGrid(myvar,grdROMS,grdMODEL,mydata,useFilter):
if myvar in ["uice"]:
indexROMS_Z_ST = (grdMODEL.Nlevels,grdROMS.eta_u, grdROMS.xi_u)
tolon = grdROMS.lon_u; tolat=grdROMS.lat_u
toxi = grdROMS.xi_u; toeta=grdROMS.eta_u;
mymask = grdROMS.mask_u
elif myvar in ["vice"]:
indexROMS_Z_ST = (grdMODEL.Nlevels,grdROMS.eta_v, grdROMS.xi_v)
tolon = grdROMS.lon_v; tolat=grdROMS.lat_v
toxi = grdROMS.xi_v; toeta=grdROMS.eta_v;
mymask = grdROMS.mask_v
else:
indexROMS_Z_ST = (grdMODEL.Nlevels,grdROMS.eta_rho, grdROMS.xi_rho)
tolon=grdROMS.lon_rho; tolat=grdROMS.lat_rho
toxi=grdROMS.xi_rho; toeta=grdROMS.eta_rho;
mymask = grdROMS.mask_rho
array1=np.zeros((indexROMS_Z_ST),dtype=np.float64)
if (grdMODEL.useESMF):
grdMODEL.fieldSrc.data[:,:]=np.flipud(np.rot90(np.squeeze(mydata[:,:])))
if myvar in ["uice"]:
field = grdMODEL.regridSrc2Dst_u(grdMODEL.fieldSrc, grdMODEL.fieldDst_u)
elif myvar in ["vice"]:
field = grdMODEL.regridSrc2Dst_v(grdMODEL.fieldSrc, grdMODEL.fieldDst_v)
else:
field = grdMODEL.regridSrc2Dst_rho(grdMODEL.fieldSrc, grdMODEL.fieldDst_rho)
field = np.fliplr(np.rot90(field.data,3))
# if myvar in ["hice","aice"]:
# import plotData
# plotData.contourMap(grdROMS,grdROMS.lon_rho,grdROMS.lat_rho, field, "surface", myvar)
else:
i0 = np.argmin(np.fabs(grdMODEL.lon[0,:]-180))
mydataout = np.zeros(np.squeeze(mydata[:,:]).shape,mydata.dtype)
lonsout = np.zeros((len(grdMODEL.lon[0,:])),grdMODEL.lon.dtype)
lonsout[0:len(grdMODEL.lon[0,:])-i0] = grdMODEL.lon[0,i0:]-360
lonsout[len(grdMODEL.lon[0,:])-i0:] = grdMODEL.lon[0,1:i0+1]
mydataout[:,0:len(grdMODEL.lon[0,:])-i0] = mydata[:,i0:]
mydataout[:,len(grdMODEL.lon[0,:])-i0:] = mydata[:,1:i0+1]
field = mp.interp(mydataout,lonsout,grdMODEL.lat[:,0], tolon, tolat,
checkbounds=False, masked=False, order=1)
# Smooth the output
if (useFilter):
field = laplaceFilter(field, 1000, toxi, toeta)
field=field*mymask
array1[0,:,:]=field
# import plotData
# plotData.contourMap(grdROMS, tolon, tolat, field, "34", myvar)
return array1