-
Notifications
You must be signed in to change notification settings - Fork 0
/
block_fns.py
304 lines (199 loc) · 8.81 KB
/
block_fns.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
import numpy as np
import collections
from thermolib.constants import constants
c = constants()
def blockave1D(field, db):
"""
Takes a 1D field with nx points and divides into blocks.
The new field has length nx/db.
"""
nx = field.shape[0]
nxblock = nx // db
blockfield = np.average(np.split(field, nxblock, axis=0), axis=1)
return blockfield
def blockave2D(field, db):
"""
Takes a nx x ny field and divides the field into blocks - the new field has
dimensions nx' x ny' where nx' = nx/db, ny' = ny/db
db is the block width in number of grid cells.
the field is averaged over the block area (db points)
in the case of 3D field, averaging is only performed in horizontal direction.
assumes nx = ny = even integer.
db must be a multiple of nx
"""
nx = field.shape[0]
ny = field.shape[1]
nxblock = nx // db
nyblock = ny // db
#split up field column-wise, take average row-wise. then split up resulting field row-wise, and take average column-wise.
blockfield = np.average(np.split(np.average(np.split(field, nxblock, axis=1), axis=-1), nyblock, axis=1), axis=-1)
return blockfield
def blockave3D(field, db):
nz = field.shape[0]
nx = field.shape[1]
ny = field.shape[2]
nxblock = nx // db
nyblock = ny // db
blockfield = np.zeros((nz, nxblock, nyblock))
for i in range(nz):
field_z = field[i,:,:]
blockfield[i,:,:] = np.average(np.split(np.average(np.split(field_z, nxblock, axis=1), axis=-1), nyblock, axis=1), axis=-1)
return blockfield
def blocksum3D(field, db):
nz = field.shape[0]
nx = field.shape[1]
ny = field.shape[2]
nxblock = nx // db
nyblock = ny // db
blockfield = np.zeros((nz, nxblock, nyblock))
for i in range(nz):
field_z = field[i,:,:]
blockfield[i,:,:] = np.sum(np.split(np.sum(np.split(field_z, nxblock, axis=1), axis=-1), nyblock, axis=1), axis=-1)
return blockfield
def blocksort1D(sfield, ofield, db):
"""
Takes two 1D fields of length nx, and sorts according to sfield.
"""
nx = sfield.shape[0]
nxblock = nx // db
blocksfield = np.average(np.split(sfield, nxblock, axis=0), axis=1)
blockofield = np.average(np.split(ofield, nxblock, axis=0), axis=1)
d = dict(zip(blocksfield, blockofield))
od = collections.OrderedDict(sorted(d.items()))
return od
def blocksort2D_1Din(sfield, ofield, db):
"""
Takes a 1D field of length nx, sfield, and 2D (x-z) of length nz by nx and sorts according to sfield (preserves z shape).
"""
nx = sfield.shape[0]
nz = ofield.shape[0]
nxblock = nx // db
blocksfield = np.average(np.split(sfield, nxblock, axis=0), axis=1)
blockofield = np.zeros((nz, nxblock))
for i in range(nz):
field_z = ofield[i,:]
blockofield[i,:] = np.average(np.split(field_z, nxblock, axis=0), axis=1)
sorter = blocksfield.argsort()
blocksfield = np.sort(blocksfield)
blockofield = blockofield[:,sorter]
return (blocksfield, blockofield)
def blocksort2D(sfield, ofield, db):
"""
Takes two nx x ny fields and divides them into blocks - the new fields have
dimensions nx' x ny' where nx' = nx/db, ny' = ny/db
db is half the block width in number of grid cells.
the fields are averaged over the block area (db points) and then
ofield is sorted according to sfield (spatial structure is lost)
the returned value is a dictionary with sfield as the key and ofield as the value
assumes nx = ny = even integer.
db must be a multiple of nx
"""
nx = sfield.shape[0]
ny = sfield.shape[1]
nxblock = nx / db
nyblock = ny / db
#tave_field = np.mean(field[ti-ntave:ti,:,:])
#tave_field = np.squeeze(tave_field)
#split up field column-wise, take average row-wise. then split up resulting field row-wise, and take average column-wise.
blocksfield = np.average(np.split(np.average(np.split(sfield, nxblock, axis=1), axis=-1), nyblock, axis=1), axis=-1)
blockofield = np.average(np.split(np.average(np.split(ofield, nxblock, axis=1), axis=-1), nyblock, axis=1), axis=-1)
blocksfield = blocksfield.flatten()
blockofield = blockofield.flatten()
d = dict(zip(blocksfield, blockofield))
od = collections.OrderedDict(sorted(d.items()))
return od
def blocksort3D(sfield, ofield, db):
"""
Takes a nx x ny field and a nz x nx x ny field and divides them into horizontal blocks - the new fields have
dimensions nx' x ny' where nx' = nx/db, ny' = ny/db
db is half the block width in number of grid cells.
the fields are averaged over the horizontal block area (db points) and then
ofield is sorted according to sfield
(horizontal spatial structure is lost, only retain vertical structure.. so the resulting ofield is sorted profiles)
the returned value is a tuple with the sorted fields (sfield, ofield)
assumes nx = ny = even integer.
db must be a multiple of nx
"""
nz = ofield.shape[0]
nx = ofield.shape[1]
ny = ofield.shape[2]
nxblock = nx // db
nyblock = ny // db
blocksfield = np.average(np.split(np.average(np.split(sfield, nxblock, axis=1), axis=-1), nyblock, axis=1), axis=-1)
blockofield = np.zeros((nz, nxblock, nyblock))
for i in range(nz):
field_z = ofield[i,:,:]
blockofield[i,:,:] = np.average(np.split(np.average(np.split(field_z, nxblock, axis=1), axis=-1), nyblock, axis=1), axis=-1)
blocksfield = blocksfield.flatten()
blockofield = blockofield.reshape((nz, nxblock*nyblock))
sorter = blocksfield.argsort()
blocksfield = np.sort(blocksfield)
blockofield = blockofield[:,sorter]
return (blocksfield, blockofield)
def blockxysort2D(sfield, xx, yy, db):
"""
Takes a nx x ny field and divides it into blocks - the new field has
dimensions nx' x ny' where nx' = nx/db, ny' = ny/db
db is half the block width in number of grid cells.
the field is averaged over the block area (db points) and then
ofield is sorted according to sfield along with the (x,y) coords of the block
the returned value is a dictionary with sfield as the key and (x,y) as value
assumes nx = ny = even integer.
db must be a multiple of nx
"""
nx = sfield.shape[0]
ny = sfield.shape[1]
nxblock = nx // db
nyblock = ny // db
#tave_field = np.mean(field[ti-ntave:ti,:,:])
#tave_field = np.squeeze(tave_field)
#split up field column-wise, take average row-wise. then split up resulting field row-wise, and take average column-wise.
blocksfield = np.average(np.split(np.average(np.split(sfield, nxblock, axis=1), axis=-1), nyblock, axis=1), axis=-1)
xblock = np.average(np.split(np.average(np.split(xx, nxblock, axis=1), axis=-1), nyblock, axis=1), axis=-1)
yblock = np.average(np.split(np.average(np.split(yy, nxblock, axis=1), axis=-1), nyblock, axis=1), axis=-1)
xblock = xblock.flatten()
yblock = yblock.flatten()
xyblock = zip(xblock, yblock)
blocksfield = blocksfield.flatten()
d = dict(zip(blocksfield, xyblock))
od = collections.OrderedDict(sorted(d.items()))
return od
def xysort(sfield, x, y):
x = x.flatten()
y = y.flatten()
sfield = sfield.flatten()
xy = zip(x, y)
d = dict(zip(sfield, xy))
od = collections.OrderedDict(sorted(d.items()))
return od
def vertint(field, p):
"""
vertically integrates a 3D field over pressure.
if hydrostatic atmoshpere, this is equivalent to density weighted vertical integral.
ignore the last level of field when integrating to have consistent shape with diff(p)
"""
nz = field.shape[0]
nx = field.shape[1]
ny = field.shape[2]
dp3D= np.ones((nz-1, nx, ny))
dp3D= np.transpose(dp3D)
dp = -np.diff(p)
dp3D[:,:,:]=dp
dp3D = np.transpose(dp3D)
fieldhat = (1/c.g)*np.sum(np.multiply(field[:-1,:,:], dp3D), axis=0)
return fieldhat
def vertint2D(field, p):
"""
vertically integrates a 2D (x-z) field over pressure.
if hydrostatic atmoshpere, this is equivalent to density weighted vertical integral.
ignore the last level of field when integrating to have consistent shape with diff(p)
"""
nz = field.shape[0]
nx = field.shape[1]
dp2D= np.ones((nz-1, nx))
dp2D= np.transpose(dp2D)
dp = -np.diff(p)
dp2D[:,:]=dp
dp2D = np.transpose(dp2D)
fieldhat = (1/c.g)*np.sum(np.multiply(field[:-1,:], dp2D), axis=0)
return fieldhat