-
Notifications
You must be signed in to change notification settings - Fork 1
/
calc_advection.ncl
356 lines (275 loc) · 15.4 KB
/
calc_advection.ncl
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
; calc_advection.ncl
;===================================================================================================
; calculate horizontal advective forcing required for SCM
;
; from email from Filip Vana:
;
; => forcing is instantaneous
; => advection forcing is limited to advection along model levels
;
;===================================================================================================
function calc_advection(in_data:float,u_data:float,v_data:float)
local earth_radius,pi,deg_to_rad,tmp_array,lat_array,lon_array,x_position,y_position,x_grad_horx_m,x_grad_hory_m,hor_tend_data
begin
; in_data -> quantity to advect
; u_data -> u winds on correct pressure levels
; v_data -> v winds on correct pressure levels
; calculate horizontal advection from instantaneous fields
; print("calculate advection")
; a) calculate horizontal advection
earth_radius = 6371.22*10^3 ; radius in m
pi = 4*atan(1.0)
deg_to_rad = 2.0*pi/360.0
tmp_array = new(dimsizes(in_data),typeof(in_data))
lat_array = conform(tmp_array,in_data&lat,2)
lon_array = conform(tmp_array,in_data&lon,3)
x_position = earth_radius*cos(deg_to_rad*lat_array)*deg_to_rad*lon_array
y_position = earth_radius*deg_to_rad*lat_array
x_grad_horx_m = center_finite_diff_n(in_data,x_position,False,0,3)
x_grad_hory_m = center_finite_diff_n(in_data,y_position,False,0,2)
hor_tend_data = - (u_data*x_grad_horx_m+v_data*x_grad_hory_m)
copy_VarCoords(in_data,hor_tend_data)
delete([/earth_radius,pi,deg_to_rad,tmp_array,lat_array,lon_array/])
delete([/x_position,y_position,x_grad_horx_m,x_grad_hory_m/])
return(hor_tend_data)
end
function center_finite_diff_o6(in_data:float,x_data:float,mydim:integer,smoothme:logical)
local in_data,x_data,mydim,smoothme,dims_in,i_hor,in_data_tmp,x_data_tmp,out_data_tmp,in_data_rearr,out_data_rearr,x_data_rearr,mean_dx,in_data_sm
begin
; in_data -> quantity to calc derivative of
; x_data -> x distance
; smoothme -> logical to smooth before calculating deriv
in_data_tmp = in_data
in_data_tmp!0 = "zero"
in_data_tmp!1 = "one"
in_data_tmp!2 = "two"
in_data_tmp!3 = "three"
x_data_tmp = x_data
out_data_tmp = new(dimsizes(in_data),typeof(in_data))
copy_VarCoords(in_data_tmp,x_data_tmp)
copy_VarCoords(in_data_tmp,out_data_tmp)
; assume 4D data
if (mydim.eq.0) then
in_data_rearr = in_data_tmp
out_data_rearr = out_data_tmp
x_data_rearr = x_data
end if
if (mydim.eq.1) then
in_data_rearr = in_data_tmp(one|:,zero|:,two|:,three|:)
out_data_rearr = out_data_tmp(one|:,zero|:,two|:,three|:)
x_data_rearr = x_data_tmp(one|:,zero|:,two|:,three|:)
end if
if (mydim.eq.2) then
in_data_rearr = in_data_tmp(two|:,zero|:,one|:,three|:)
out_data_rearr = out_data_tmp(two|:,zero|:,one|:,three|:)
x_data_rearr = x_data_tmp(two|:,zero|:,one|:,three|:)
end if
if (mydim.eq.3) then
in_data_rearr = in_data_tmp(three|:,zero|:,one|:,two|:)
out_data_rearr = out_data_tmp(three|:,zero|:,one|:,two|:)
x_data_rearr = x_data_tmp(three|:,zero|:,one|:,two|:)
end if
dims_in = dimsizes(in_data_rearr)
mean_dx = new(dims_in,typeof(in_data_rearr))
; smooth dim 0 if necessary
if (smoothme) then
in_data_sm = in_data_rearr
do i_hor = 3,dims_in(0)-4 ;==============================================
in_data_sm(i_hor,:,:,:) = (0.05*in_data_rearr(i_hor-3,:,:,:) + \
0.30*in_data_rearr(i_hor-2,:,:,:) + \
0.75*in_data_rearr(i_hor-1,:,:,:) + \
1.00*in_data_rearr(i_hor ,:,:,:) + \
0.75*in_data_rearr(i_hor+1,:,:,:) + \
0.30*in_data_rearr(i_hor+2,:,:,:) + \
0.05*in_data_rearr(i_hor+3,:,:,:))/3.2
end do
i_hor = 2 ;==============================================
in_data_sm(i_hor,:,:,:) = (0.5*in_data_rearr(i_hor-2,:,:,:) + \
2.0*in_data_rearr(i_hor-1,:,:,:) + \
3.0*in_data_rearr(i_hor ,:,:,:) + \
2.0*in_data_rearr(i_hor+1,:,:,:) + \
0.5*in_data_rearr(i_hor+2,:,:,:))/8.0
i_hor = dims_in(0)-3 ;==============================================
in_data_sm(i_hor,:,:,:) = (0.5*in_data_rearr(i_hor-2,:,:,:) + \
2.0*in_data_rearr(i_hor-1,:,:,:) + \
3.0*in_data_rearr(i_hor ,:,:,:) + \
2.0*in_data_rearr(i_hor+1,:,:,:) + \
0.5*in_data_rearr(i_hor+2,:,:,:))/8.0
i_hor = 1 ;==============================================
in_data_sm(i_hor,:,:,:) = (0.25*in_data_rearr(i_hor-1,:,:,:) + \
0.50*in_data_rearr(i_hor ,:,:,:) + \
0.25*in_data_rearr(i_hor+1,:,:,:))
i_hor = dims_in(0)-2 ;==============================================
in_data_sm(i_hor,:,:,:) = (0.25*in_data_rearr(i_hor-1,:,:,:) + \
0.50*in_data_rearr(i_hor ,:,:,:) + \
0.25*in_data_rearr(i_hor+1,:,:,:))
else
in_data_sm = in_data_rearr
end if
;; calculate the derivative on smoothed (or not) data
do i_hor = 3,dims_in(0)-4 ;==============================================
out_data_rearr(i_hor,:,:,:) = (-(1.0/60.0)*in_data_sm(i_hor-3,:,:,:) + \
(3.0/20.0)*in_data_sm(i_hor-2,:,:,:) - \
( 3.0/4.0)*in_data_sm(i_hor-1,:,:,:) + \
(1.0/60.0)*in_data_sm(i_hor+3,:,:,:) - \
(3.0/20.0)*in_data_sm(i_hor+2,:,:,:) +\
( 3.0/4.0)*in_data_sm(i_hor+1,:,:,:))
mean_dx(i_hor,:,:,:) = (x_data_rearr(i_hor+1,:,:,:)-x_data_rearr(i_hor-1,:,:,:))/2
out_data_rearr(i_hor,:,:,:) = out_data_rearr(i_hor,:,:,:)/mean_dx(i_hor,:,:,:)
end do
i_hor = 2 ;=============================================================
out_data_rearr(i_hor,:,:,:) = (1.0/12.0)*in_data_sm(i_hor-2,:,:,:) \
-( 2.0/3.0)*in_data_sm(i_hor-1,:,:,:) \
+( 2.0/3.0)*in_data_sm(i_hor+1,:,:,:) \
-(1.0/12.0)*in_data_sm(i_hor+2,:,:,:)
mean_dx(i_hor,:,:,:) = (x_data_rearr(i_hor+1,:,:,:)-x_data_rearr(i_hor-1,:,:,:))/2
out_data_rearr(i_hor,:,:,:) = out_data_rearr(i_hor,:,:,:)/mean_dx(i_hor,:,:,:)
i_hor = dims_in(0)-3 ;===================================================
out_data_rearr(i_hor,:,:,:) = (1.0/12.0)*in_data_sm(i_hor-2,:,:,:) \
-( 2.0/3.0)*in_data_sm(i_hor-1,:,:,:) \
+( 2.0/3.0)*in_data_sm(i_hor+1,:,:,:) \
-(1.0/12.0)*in_data_sm(i_hor+2,:,:,:)
mean_dx(i_hor,:,:,:) = (x_data_rearr(i_hor+1,:,:,:)-x_data_rearr(i_hor-1,:,:,:))/2
out_data_rearr(i_hor,:,:,:) = out_data_rearr(i_hor,:,:,:)/mean_dx(i_hor,:,:,:)
i_hor = 1 ;============================================================
out_data_rearr(i_hor,:,:,:) = -(1.0/2.0)*in_data_sm(i_hor-1,:,:,:) \
+(1.0/2.0)*in_data_sm(i_hor+1,:,:,:)
mean_dx(i_hor,:,:,:) = (x_data_rearr(i_hor+1,:,:,:)-x_data_rearr(i_hor-1,:,:,:))/2
out_data_rearr(i_hor,:,:,:) = out_data_rearr(i_hor,:,:,:)/mean_dx(i_hor,:,:,:)
i_hor = dims_in(0)-2 ;=================================================
out_data_rearr(i_hor,:,:,:) = -(1.0/2.0)*in_data_sm(i_hor-1,:,:,:) \
+(1.0/2.0)*in_data_sm(i_hor+1,:,:,:)
mean_dx(i_hor,:,:,:) = (x_data_rearr(i_hor+1,:,:,:)-x_data_rearr(i_hor-1,:,:,:))/2
out_data_rearr(i_hor,:,:,:) = out_data_rearr(i_hor,:,:,:)/mean_dx(i_hor,:,:,:)
i_hor = 0 ;=============================================================
out_data_rearr(i_hor,:,:,:) = -in_data_sm(i_hor,:,:,:) \
+in_data_sm(i_hor+1,:,:,:)
mean_dx(i_hor,:,:,:) = (x_data_rearr(i_hor+1,:,:,:)-x_data_rearr(i_hor,:,:,:))
out_data_rearr(i_hor,:,:,:) = out_data_rearr(i_hor,:,:,:)/mean_dx(i_hor,:,:,:)
i_hor = dims_in(0)-1 ;==================================================
out_data_rearr(i_hor,:,:,:) = in_data_sm(i_hor,:,:,:) \
-in_data_sm(i_hor-1,:,:,:)
mean_dx(i_hor,:,:,:) = (x_data_rearr(i_hor,:,:,:)-x_data_rearr(i_hor-1,:,:,:))
out_data_rearr(i_hor,:,:,:) = out_data_rearr(i_hor,:,:,:)/mean_dx(i_hor,:,:,:)
out_data = out_data_rearr(zero|:,one|:,two|:,three|:)
copy_VarCoords(in_data,out_data)
return(out_data)
end
function center_finite_diff_o4(in_data:float,x_data:float,mydim:integer,smoothme:logical)
local in_data,x_data,mydim,smoothme,dims_in,i_hor,in_data_tmp,x_data_tmp,out_data_tmp,in_data_rearr,out_data_rearr,x_data_rearr,mean_dx,in_data_sm
begin
; in_data -> quantity to calc derivative of
; x_data -> x distance
; smoothme -> logical to smooth before calculating deriv
in_data_tmp = in_data
in_data_tmp!0 = "zero"
in_data_tmp!1 = "one"
in_data_tmp!2 = "two"
in_data_tmp!3 = "three"
x_data_tmp = x_data
out_data_tmp = new(dimsizes(in_data),typeof(in_data))
copy_VarCoords(in_data_tmp,x_data_tmp)
copy_VarCoords(in_data_tmp,out_data_tmp)
; assume 4D data
if (mydim.eq.0) then
in_data_rearr = in_data_tmp
out_data_rearr = out_data_tmp
x_data_rearr = x_data
end if
if (mydim.eq.1) then
in_data_rearr = in_data_tmp(one|:,zero|:,two|:,three|:)
out_data_rearr = out_data_tmp(one|:,zero|:,two|:,three|:)
x_data_rearr = x_data_tmp(one|:,zero|:,two|:,three|:)
end if
if (mydim.eq.2) then
in_data_rearr = in_data_tmp(two|:,zero|:,one|:,three|:)
out_data_rearr = out_data_tmp(two|:,zero|:,one|:,three|:)
x_data_rearr = x_data_tmp(two|:,zero|:,one|:,three|:)
end if
if (mydim.eq.3) then
in_data_rearr = in_data_tmp(three|:,zero|:,one|:,two|:)
out_data_rearr = out_data_tmp(three|:,zero|:,one|:,two|:)
x_data_rearr = x_data_tmp(three|:,zero|:,one|:,two|:)
end if
dims_in = dimsizes(in_data_rearr)
mean_dx = new(dims_in,typeof(in_data_rearr))
; smooth dim 0 if necessary
if (smoothme) then
in_data_sm = in_data_rearr
do i_hor = 2,dims_in(0)-3 ;==============================================
in_data_sm(i_hor,:,:,:) = (0.5*in_data_rearr(i_hor-2,:,:,:) + \
2.0*in_data_rearr(i_hor-1,:,:,:) + \
3.0*in_data_rearr(i_hor ,:,:,:) + \
2.0*in_data_rearr(i_hor+1,:,:,:) + \
0.5*in_data_rearr(i_hor+2,:,:,:))/8.0
end do
i_hor = 1 ;==============================================
in_data_sm(i_hor,:,:,:) = (0.25*in_data_rearr(i_hor-1,:,:,:) + \
0.50*in_data_rearr(i_hor ,:,:,:) + \
0.25*in_data_rearr(i_hor+1,:,:,:))
i_hor = dims_in(0)-2 ;==============================================
in_data_sm(i_hor,:,:,:) = (0.25*in_data_rearr(i_hor-1,:,:,:) + \
0.50*in_data_rearr(i_hor ,:,:,:) + \
0.25*in_data_rearr(i_hor+1,:,:,:))
else
in_data_sm = in_data_rearr
end if
;; calculate the derivative on smoothed (or not) data
do i_hor = 2,dims_in(0)-3 ;=========================================
out_data_rearr(i_hor,:,:,:) = (1.0/12.0)*in_data_sm(i_hor-2,:,:,:) \
-( 2.0/3.0)*in_data_sm(i_hor-1,:,:,:) \
+( 2.0/3.0)*in_data_sm(i_hor+1,:,:,:) \
-(1.0/12.0)*in_data_sm(i_hor+2,:,:,:)
mean_dx(i_hor,:,:,:) = (x_data_rearr(i_hor+1,:,:,:)-x_data_rearr(i_hor-1,:,:,:))/2
out_data_rearr(i_hor,:,:,:) = out_data_rearr(i_hor,:,:,:)/mean_dx(i_hor,:,:,:)
end do
i_hor = 1 ;============================================================
out_data_rearr(i_hor,:,:,:) = -(1.0/2.0)*in_data_sm(i_hor-1,:,:,:) \
+(1.0/2.0)*in_data_sm(i_hor+1,:,:,:)
mean_dx(i_hor,:,:,:) = (x_data_rearr(i_hor+1,:,:,:)-x_data_rearr(i_hor-1,:,:,:))/2
out_data_rearr(i_hor,:,:,:) = out_data_rearr(i_hor,:,:,:)/mean_dx(i_hor,:,:,:)
i_hor = dims_in(0)-2 ;=================================================
out_data_rearr(i_hor,:,:,:) = -(1.0/2.0)*in_data_sm(i_hor-1,:,:,:) \
+(1.0/2.0)*in_data_sm(i_hor+1,:,:,:)
mean_dx(i_hor,:,:,:) = (x_data_rearr(i_hor+1,:,:,:)-x_data_rearr(i_hor-1,:,:,:))/2
out_data_rearr(i_hor,:,:,:) = out_data_rearr(i_hor,:,:,:)/mean_dx(i_hor,:,:,:)
i_hor = 0 ;=============================================================
out_data_rearr(i_hor,:,:,:) = -in_data_sm(i_hor,:,:,:) \
+in_data_sm(i_hor+1,:,:,:)
mean_dx(i_hor,:,:,:) = (x_data_rearr(i_hor+1,:,:,:)-x_data_rearr(i_hor,:,:,:))
out_data_rearr(i_hor,:,:,:) = out_data_rearr(i_hor,:,:,:)/mean_dx(i_hor,:,:,:)
i_hor = dims_in(0)-1 ;==================================================
out_data_rearr(i_hor,:,:,:) = in_data_sm(i_hor,:,:,:) \
-in_data_sm(i_hor-1,:,:,:)
mean_dx(i_hor,:,:,:) = (x_data_rearr(i_hor,:,:,:)-x_data_rearr(i_hor-1,:,:,:))
out_data_rearr(i_hor,:,:,:) = out_data_rearr(i_hor,:,:,:)/mean_dx(i_hor,:,:,:)
out_data = out_data_rearr(zero|:,one|:,two|:,three|:)
copy_VarCoords(in_data,out_data)
return(out_data)
end
function hor_derivative(in_data:float,smoothme:logical)
local smoothme,earth_radius,pi,deg_to_rad,tmp_array,lat_array,lon_array,x_position,y_position,x_grad_horx_m,x_grad_hory_m
begin
; in_data -> quantity to calc derivatives for
earth_radius = 6371.22*10^3 ; radius in m
pi = 4*atan(1.0)
deg_to_rad = 2.0*pi/360.0
tmp_array = new(dimsizes(in_data),typeof(in_data))
lat_array = conform(tmp_array,in_data&lat,2)
lon_array = conform(tmp_array,in_data&lon,3)
x_position = earth_radius*cos(deg_to_rad*lat_array)*deg_to_rad*lon_array
y_position = earth_radius*deg_to_rad*lat_array
; x_grad_horx_m = center_finite_diff_n(in_data,x_position,False,0,3)
; x_grad_hory_m = center_finite_diff_n(in_data,y_position,False,0,2)
; ; if smoothme = tru, smooth each dimension in turn
; x_grad_horx_m = center_finite_diff_o6(in_data,x_position,3,smoothme)
; x_grad_hory_m = center_finite_diff_o6(in_data,y_position,2,smoothme)
; if smoothme = tru, smooth each dimension in turn
x_grad_horx_m = center_finite_diff_o4(in_data,x_position,3,smoothme)
x_grad_hory_m = center_finite_diff_o4(in_data,y_position,2,smoothme)
copy_VarCoords(in_data,x_grad_horx_m)
copy_VarCoords(in_data,x_grad_hory_m)
delete([/earth_radius,pi,deg_to_rad,tmp_array,lat_array,lon_array/])
delete([/x_position,y_position/])
return([/x_grad_horx_m,x_grad_hory_m/])
end