forked from trondkr/model2roms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interp2D.py
150 lines (125 loc) · 6.66 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
from __future__ import print_function
import numpy as np
import datetime
import extrapolate as ex
import os
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(2019, 3, 12)
__version__ = "1.5"
__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(confM2R, mydata, myvar):
if confM2R.showprogress is True:
try:
import progressbar
widgets=['\rHorizontal interpolation:', progressbar.Percentage(), progressbar.Bar()]
progress = progressbar.ProgressBar(confM2R.grdMODEL.nlevels, widgets=widgets).start()
except ImportError:
print("Could not find module progressbar")
confM2R.showprogress=False
pass
indexROMS_Z_ST, toxi, toeta, mymask = setupIndexes(confM2R, myvar)
array1 = np.zeros((indexROMS_Z_ST), dtype=np.float)
# 2D or 3D interpolation
depthlevels=confM2R.grdMODEL.nlevels
if myvar in ['ssh', 'ageice', 'uice', 'vice', 'aice', 'hice', 'snow_thick','hs']:
depthlevels=1
for k in range(depthlevels):
if confM2R.useesmf:
if depthlevels==1:
indata=np.squeeze(mydata[:, :])
else:
indata=np.squeeze(mydata[k, :, :])
# We interpolate to RHO fields for all variables and then we later interpolate RHO points to U and V points
# But input data are read on U and V and RHO grids if they differ (as NorESM and GLORYS does).
if myvar in ['uice','uvel']:
confM2R.grdMODEL.fieldSrc_u.data[:, :] = np.flipud(np.rot90(indata))
field = confM2R.grdMODEL.regridSrc2Dst_u(confM2R.grdMODEL.fieldSrc_u, confM2R.grdMODEL.fieldDst_rho)
elif myvar in ['vice','vvel']:
confM2R.grdMODEL.fieldSrc_v.data[:, :] = np.flipud(np.rot90(indata))
field = confM2R.grdMODEL.regridSrc2Dst_v(confM2R.grdMODEL.fieldSrc_v, confM2R.grdMODEL.fieldDst_rho)
else:
confM2R.grdMODEL.fieldSrc_rho.data[:, :] = np.flipud(np.rot90(indata))
field = confM2R.grdMODEL.regridSrc2Dst_rho(confM2R.grdMODEL.fieldSrc_rho, confM2R.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))
if confM2R.usefilter:
field = laplacefilter(field, 1000, toxi, toeta)
field = field * mymask
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 myvar in ["hice","aice"]:
# import plotData
# plotData.contourMap(grdROMS,grdROMS.lon_rho,grdROMS.lat_rho, field, "surface", myvar)
if confM2R.showprogress is True:
progress.update(k)
if confM2R.showprogress is True:
progress.finish()
return array1
def setupIndexes(confM2R, myvar):
if myvar in ["uice"]:
indexROMS_Z_ST = (confM2R.grdMODEL.nlevels, confM2R.grdROMS.eta_u, confM2R.grdROMS.xi_u)
toxi = confM2R.grdROMS.xi_u
toeta = confM2R.grdROMS.eta_u
mymask = confM2R.grdROMS.mask_u
elif myvar in ["vice"]:
indexROMS_Z_ST = (confM2R.grdMODEL.nlevels, confM2R.grdROMS.eta_v, confM2R.grdROMS.xi_v)
toxi = confM2R.grdROMS.xi_v
toeta = confM2R.grdROMS.eta_v
mymask = confM2R.grdROMS.mask_v
else:
indexROMS_Z_ST = (confM2R.grdMODEL.nlevels, confM2R.grdROMS.eta_rho, confM2R.grdROMS.xi_rho)
toxi = confM2R.grdROMS.xi_rho
toeta = confM2R.grdROMS.eta_rho
mymask = confM2R.grdROMS.mask_rho
return indexROMS_Z_ST, toxi, toeta, mymask
def setupESMFInterpolationWeights(confM2R):
if confM2R.useesmf:
print("=>Creating the interpolation weights and indexes using ESMF (this may take some time....):")
print(" -> regridSrc2Dst at RHO points")
confM2R.grdMODEL.fieldSrc_rho = ESMF.Field(confM2R.grdMODEL.esmfgrid, "fieldSrc",
staggerloc=ESMF.StaggerLoc.CENTER)
confM2R.grdMODEL.fieldDst_rho = ESMF.Field(confM2R.grdROMS.esmfgrid, "fieldDst",
staggerloc=ESMF.StaggerLoc.CENTER)
confM2R.grdMODEL.regridSrc2Dst_rho = ESMF.Regrid(confM2R.grdMODEL.fieldSrc_rho,
confM2R.grdMODEL.fieldDst_rho,
regrid_method=ESMF.RegridMethod.BILINEAR,
unmapped_action=ESMF.UnmappedAction.IGNORE)
print(" -> regridSrc2Dst at U points to RHO")
confM2R.grdMODEL.fieldSrc_u = ESMF.Field(confM2R.grdMODEL.esmfgrid_u, "fieldSrc",
staggerloc=ESMF.StaggerLoc.CENTER)
confM2R.grdMODEL.regridSrc2Dst_u = ESMF.Regrid(confM2R.grdMODEL.fieldSrc_u,
confM2R.grdMODEL.fieldDst_rho,
regrid_method=ESMF.RegridMethod.BILINEAR,
unmapped_action=ESMF.UnmappedAction.IGNORE)
print(" -> regridSrc2Dst at V points to RHO")
confM2R.grdMODEL.fieldSrc_v = ESMF.Field(confM2R.grdMODEL.esmfgrid_v, "fieldSrc",
staggerloc=ESMF.StaggerLoc.CENTER)
confM2R.grdMODEL.regridSrc2Dst_v = ESMF.Regrid(confM2R.grdMODEL.fieldSrc_v,
confM2R.grdMODEL.fieldDst_rho,
regrid_method=ESMF.RegridMethod.BILINEAR,
unmapped_action=ESMF.UnmappedAction.IGNORE)