-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathopenCreateData.py
270 lines (228 loc) · 9.59 KB
/
openCreateData.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
"""
$Id: openCreateData.py 2285 2012-06-24 18:01:49Z dkindig $
Plotting routine for tests in regrid2.ESMF using ginned up data
"""
# Allow imports from cdat_tests
import sys
sys.path.append('../cdat_tests')
import cdms2,cdat_info
import regrid2
import os
import numpy as np
import ESMF
home = os.getenv("HOME") + "/"
install_prefix = cdat_info.get_prefix() + '/'
sample_data_dir = cdat_info.get_sampledata_path() + '/'
class dataNoPeri:
def __init__(self, nx, ny, xCrdLimits, yCrdLimits):
"""
Contruct a coordinate set for a non periodic grid using global
coordinates
@param nx Number of x coordinate cell centers
@param ny Number of y coordinate cell centers
@param xBnds x nodal Bounds for the coordinates. Sum of xBnds must be 360
@param yBnds y nodal Bounds for the coordinates
"""
dims = [ny,nx]
nx1, ny1 = nx+1, ny+1
xLc, xUc = xCrdLimits[0], xCrdLimits[1]
yLc, yUc = yCrdLimits[0], yCrdLimits[1]
# Cell centered coordinates
x = np.linspace(xLc, xUc, nx)
y = np.linspace(yLc, yUc, ny)
xx = np.outer(np.ones(ny,), x)
yy = np.outer(y, np.ones(nx,))
xInterval = abs(x[0]-x[1])/2.
yInterval = abs(y[0]-y[1])/2.
# Nodal coordinates
xb = np.linspace(xLc-xInterval, xUc+xInterval, nx1)
yb = np.linspace(yLc-yInterval, yUc+yInterval, ny1)
xxb = np.outer(np.ones(ny1,), xb)
yyb = np.outer(yb, np.ones(nx1,))
# cell centered data
d = 0 * xx; d[0, 0] = 1.0
self.data = np.array(d, dtype = np.float32)
self.dims = dims
# Nodal data
d = 0 * xxb; d[0,0] = 1.0
self.dataN = np.array(d, dtype = np.float32)
# Convert to a cdms2 variable
flat = cdms2.axis.createAxis(y, id = 'lat')
flat.units='degrees_north'
flon = cdms2.axis.createAxis(x, id = 'lon')
flon.units='degrees_east'
self.cdmsFromCell = cdms2.grid.createRectGrid(flat, flon)
self.cdmsFromCell.genBounds()
self.cdmsFromData = cdms2.createVariable(self.data, mask = None,
grid = self.cdmsFromCell,
axes = [flat, flon],
id = 'fromData')
if yy.shape != xx.shape:
print 'yy.shape', yy.shape, '!= xx.shape', xx.shape
raise 'Coordinate shape mismatch'
if yyb.shape != xxb.shape:
print 'yyb.shape', yyb.shape, '!= xxb.shape', xxb.shape
raise 'Bounds shape mismatch'
self.coords = [xx, yy]
self.bounds = [xxb, yyb]
class dataMaskedNoPeri:
def __init__(self, nx, ny, xCrdLimits, yCrdLimits):
"""
Contruct a coordinate set for a non periodic grid using global
coordinates
@param nx Number of x coordinate cell centers
@param ny Number of y coordinate cell centers
@param xCrdLimits x nodal Bounds for the coordinates. Sum of xCrdLimits must be 360
@param yCrdLimits y nodal Bounds for the coordinates
"""
dims = [ny,nx]
nx1, ny1 = nx+1, ny+1
xLc, xUc = xCrdLimits[0], xCrdLimits[1]
yLc, yUc = yCrdLimits[0], yCrdLimits[1]
# Cell centered coordinates
x = np.linspace(xLc, xUc, nx)
y = np.linspace(yLc, yUc, ny)
xx = np.outer(np.ones(ny,), x)
yy = np.outer(y, np.ones(nx,))
xInterval = abs(x[0]-x[1])/2.
yInterval = abs(y[0]-y[1])/2.
# Nodal coordinates
xb = np.linspace(xLc-xInterval, xUc+xInterval, nx1)
yb = np.linspace(yLc-yInterval, yUc+yInterval, ny1)
xxb = np.outer(np.ones(ny1,), xb)
yyb = np.outer(yb, np.ones(nx1,))
# cell centered data
d = 0 * xx; d[0, 0] = 1.0; d[1, 1] = 1.0
mask = np.zeros(xx.shape, dtype = np.bool)
self.data = np.ma.array(d, mask = mask, dtype = np.float32)
self.data.mask[1, 1] = True
self.dims = dims
# Nodal data
d = 0 * xxb; d[0, 0] = 1.0; d[1, 1] = 1.0
mask = np.zeros(xxb.shape, dtype = np.bool)
self.dataN = np.ma.array(d, mask = mask, dtype = np.float32)
self.dataN.mask[1, 1] = True
self.dims = dims
# Convert to a cdms2 variable
flat = cdms2.axis.createAxis(y, id = 'lat')
flat.units='degrees_north'
flon = cdms2.axis.createAxis(x, id = 'lon')
flon.units='degrees_east'
self.cdmsFromGrid = cdms2.grid.createRectGrid(flat, flon)
self.cdmsFromGrid.genBounds()
self.cdmsFromData = cdms2.createVariable(self.data, mask = None,
grid = self.cdmsFromGrid,
axes = [flat, flon],
id = 'fromData')
if yy.shape != xx.shape:
print 'yy.shape', yy.shape, '!= xx.shape', xx.shape
raise 'Coordinate shape mismatch'
if yyb.shape != xxb.shape:
print 'yyb.shape', yyb.shape, '!= xxb.shape', xxb.shape
raise 'Bounds shape mismatch'
self.coords = [yy, xx]
self.bounds = [yyb, xxb]
class fakeData:
def __init__(self, nx, ny, xBnds, yBnds, tnx, tny):
"""
Contruct a coordinate set for a non periodic grid using global
coordinates
@param nx Number of x coordinate cell centers
@param ny Number of y coordinate cell centers
@param xBnds x nodal Bounds for the coordinates. Sum of xBnds must be 360
@param yBnds y nodal Bounds for the coordinates
@param tnx Destination grid size. Use xBnds for limits
@param tny Destination grid size. Use yBnds for limits
"""
def getCrdAndBnd(start,stop,n):
crd = np.linspace(start,stop,n)
itv = abs(crd[0]-crd[1])/2
bnd = np.linspace(start-itv,stop+itv,n+1)
return crd, bnd
fdims = np.array([nY, nX])
nVals = fdims.prod()
fLat,fLatB = getCrdAndBnd(lbY, ubY, nY)
fLon,fLonB = getCrdAndBnd(lbX, ubX, nX)
self.fCrd,dims = gsRegrid.makeCurvilinear([fLat, fLon])
self.fBnd,dims = gsRegrid.makeCurvilinear([fLatB, fLonB])
self.fakeFromGrid = [fLat, fLon]
flat = cdms2.axis.createAxis(fLat, id = 'lat')
flat.units='degrees_north'
flon = cdms2.axis.createAxis(fLon, id = 'lon')
flon.units='degrees_east'
self.cdmsFromGrid = cdms2.grid.createRectGrid(flat, flon)
nlat, nlon = len(fLat), len(fLon)
tLat,tLatB = getCrdAndBnd(lbY, ubY, tnY)
tLon,tLonB = getCrdAndBnd(lbX, ubX, tnX)
self.tCrd,dims = gsRegrid.makeCurvilinear([tLat, tLon])
self.tBnd,dims = gsRegrid.makeCurvilinear([tLatB, tLonB])
self.fakeToGrid = self.tCrd
tlat = cdms2.axis.createAxis(tLat, bounds=tLatB, id = 'lat')
tlat.units='degrees_north'
tlon = cdms2.axis.createAxis(tLon, bounds=tLonB, id = 'lon')
tlon.units='degrees_east'
self.cdmsToGrid = cdms2.grid.createRectGrid(tlat,tlon)
fData = np.ma.zeros(fdims, np.float32) * 0.5
fData[0,0] = 1
mData = fData.copy()
fData.mask = False
self.fData = fData.copy()
self.fData.id = 'fakeData'
self.cdmsFromData = cdms2.createVariable(fData, mask = None,
grid = self.cdmsFromGrid,
axes = [flat, flon],
id = 'fromData')
self.tData = np.ma.zeros(self.tCrd[0].shape, self.fData.dtype)
self.tData.mask = False
nlat2,nlon2 = nlat/2,nlon/2
pos1 = [nlat2-1,nlat2,nlat2+1]
pos2 = [nlon2-1,nlon2,nlon2+1]
pos1 = [1]
pos2 = [2]
# Initialize and populate the mask
mData.mask = True
mData.mask = False
mData.mask[1,1] = True
# for i in range(len(pos1)):
# mData[pos1[i], :] = 1e20
# mData[pos1[i], :].mask = True
# mData[:, pos2[i]] = 1e20
# mData[:, pos2[i]].mask = True
self.mData = mData.copy()
self.mData.id = 'maskData'
self.fromMaskData = cdms2.createVariable(mData,
grid = self.cdmsFromGrid,
axes = [flat, flon],
mask = mData.mask,
id = 'maskData')
self.eps = 1e-7 # Epsilon
# Point values to test against
self.gclt = 180.0
class salinity:
def __init__(self):
filename = "so_Omon_ACCESS1-0_historical_r1i1p1_185001-185412_2timesteps.nc"
# filename = "so_Omon_HadGEM2-CC_historical_r1i1p1_185912-186911_2timesteps.nc"
# h=cdms2.open(sample_data_dir + '/' + filename)
h=cdms2.open('./' + filename)
data = h('so')[0, 0, ...]
self.grid = [data.getLatitude(), data.getLongitude()]
self.grid2D = self.grid
self.data = data
class clt:
def __init__(self):
filename = "clt.nc"
g=cdms2.open(sample_data_dir + '/' + filename)
data = g('clt')
self.grid = [data.getLatitude(), data.getLongitude()]
g2D = data.getGrid().toCurveGrid()
self.grid2D = [g2D.getLatitude()[:], g2D.getLongitude()[:]]
self.data = data
class tas:
def __init__(self):
filename = "era40_tas_sample.nc"
g=cdms2.open(sample_data_dir + '/' + filename)
data = g('tas')
self.grid = [data.getLatitude(), data.getLongitude()]
g2D = data.getGrid().toCurveGrid()
self.grid2D = [g2D.getLatitude()[:], g2D.getLongitude()[:]]
self.data = data