-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathflux.py
386 lines (348 loc) · 11.9 KB
/
flux.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
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import functools
import aerobulk.aerobulk.mod_aerobulk_wrap_noskin as aeronoskin
import aerobulk.aerobulk.mod_aerobulk_wrap_skin as aeroskin
import xarray as xr
VALID_ALGOS = ["coare3p0", "coare3p6", "ecmwf", "ncar", "andreas"]
VALID_ALGOS_SKIN = ["coare3p0", "coare3p6", "ecmwf"]
def _check_algo(algo, valids):
if algo not in valids:
raise ValueError(f"Algorithm {algo} not valid. Choose from {valids}.")
def noskin_np(
sst,
t_zt,
hum_zt,
u_zu,
v_zu,
slp,
algo,
zt,
zu,
niter,
):
"""Python wrapper for aerobulk without skin correction.
!ATTENTION If input not provided in correct units, will crash.
Parameters
----------
sst : numpy.array
Bulk sea surface temperature [K]
t_zt : numpy.array
Absolute air temperature at height zt [K]
hum_zt : numpy.array
air humidity at zt, can be given as:
- specific humidity [kg/kg]
- dew-point temperature [K]
- relative humidity [%]
=> type should normally be recognized based on value range
u_zu : numpy.array
zonal wind speed at zu [m/s]
v_zu : numpy.array
meridional wind speed at zu [m/s]
slp : numpy.array, optional
mean sea-level pressure [Pa] ~101000 Pa,
by default 101000.0
algo : str
Algorithm, can be one of: "coare3p0", "coare3p6", "ecmwf", "ncar", "andreas",
zt : int
height for temperature and spec. hum. of air [m],
zu : int
height for wind (10m = traditional anemometric height [m],
niter : int
Number of iteration steps used in the algorithm,
Returns
-------
ql : numpy.array
Latent heat flux [W/m^2]
qh : numpy.array
Sensible heat flux [W/m^2]
taux : numpy.array
zonal wind stress [N/m^2]
tauy : numpy.array
meridional wind stress [N/m^2]
evap : numpy.array
evaporation [mm/s] aka [kg/m^2/s] (usually <0, as ocean loses water!)
"""
(
ql,
qh,
taux,
tauy,
evap,
) = aeronoskin.mod_aerobulk_wrapper_noskin.aerobulk_model_noskin(
algo, zt, zu, sst, t_zt, hum_zt, u_zu, v_zu, slp, niter
)
return ql, qh, taux, tauy, evap
def skin_np(
sst,
t_zt,
hum_zt,
u_zu,
v_zu,
rad_sw,
rad_lw,
slp,
algo,
zt,
zu,
niter,
):
"""Python wrapper for aerobulk with skin correction.
!ATTENTION If input not provided in correct units, will crash.
Parameters
----------
sst : numpy.array
Bulk sea surface temperature [K]
t_zt : numpy.array
Absolute air temperature at height zt [K]
hum_zt : numpy.array
air humidity at zt, can be given as:
- specific humidity [kg/kg]
- dew-point temperature [K]
- relative humidity [%]
=> type should normally be recognized based on value range
u_zu : numpy.array
zonal wind speed at zu [m/s]
v_zu : numpy.array
meridional wind speed at zu [m/s]
rad_sw : numpy.array
downwelling shortwave radiation at the surface (>0) [W/m^2]
rad_lw : numpy.array
rad_lw : downwelling longwave radiation at the surface (>0) [W/m^2]
slp : numpy.array, optional
mean sea-level pressure [Pa] ~101000 Pa,
by default 101000.0
algo : str
Algorithm, can be one of: "coare3p0", "coare3p6", "ecmwf",
zt : int
height for temperature and spec. hum. of air [m],
zu : int
height for wind (10m = traditional anemometric height [m],
niter : int
Number of iteration steps used in the algorithm,
Returns
-------
ql : numpy.array
Latent heat flux [W/m^2]
qh : numpy.array
Sensible heat flux [W/m^2]
taux : numpy.array
zonal wind stress [N/m^2]
tauy : numpy.array
meridional wind stress [N/m^2]
t_s : numpy.array
skin temperature [K] (only when l_use_skin_schemes=TRUE)
evap : numpy.array
evaporation [mm/s] aka [kg/m^2/s] (usually <0, as ocean loses water!)
"""
(
ql,
qh,
taux,
tauy,
t_s,
evap,
) = aeroskin.mod_aerobulk_wrapper_skin.aerobulk_model_skin(
algo, zt, zu, sst, t_zt, hum_zt, u_zu, v_zu, slp, rad_sw, rad_lw, niter
)
return ql, qh, taux, tauy, t_s, evap
def input_and_output_check(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
# Check the input shape
test_arg = args[
0
] # assuming that all the input shapes are the same size. TODO: More thorough check
if len(test_arg.dims) < 3:
# TODO promote using expand_dims?
raise NotImplementedError(
f"Aerobulk-Python expects all input fields as 3D arrays. Found {len(test_arg.dims)} dimensions on input."
)
if len(test_arg.dims) > 4:
# TODO iterate over extra dims? Or reshape?
raise NotImplementedError(
f"Aerobulk-Python expects all input fields as 3D arrays. Found {len(test_arg.dims)} dimensions on input."
)
out_vars = func(*args, **kwargs)
# TODO: Here we could 'un-reshape' or squeeze the output according to the logic above
if any(var.ndim != 3 for var in out_vars):
raise ValueError(
f"f2py returned result of unexpected shape. Got {[var.shape for var in out_vars]}"
)
return out_vars
return wrapper
@input_and_output_check
def noskin(
sst, t_zt, hum_zt, u_zu, v_zu, slp=101000.0, algo="coare3p0", zt=2, zu=10, niter=6
):
"""xarray wrapper for aerobulk without skin correction.
Warnings
--------
!ATTENTION If input not provided in the units shown in [] below the code will crash.
Parameters
----------
sst : xarray.DataArray
Bulk sea surface temperature [K]
t_zt : xarray.DataArray
Absolute air temperature at height zt [K]
hum_zt : xarray.DataArray
air humidity at zt, can be given as:
- specific humidity [kg/kg]
- dew-point temperature [K]
- relative humidity [%]
=> type should normally be recognized based on value range
u_zu : xarray.DataArray
zonal wind speed at zu [m/s]
v_zu : xarray.DataArray
meridional wind speed at zu [m/s]
slp : xarray.DataArray, optional
mean sea-level pressure [Pa] ~101000 Pa,
by default 101000.0
algo : str, optional
Algorithm, can be one of: "coare3p0", "coare3p6", "ecmwf", "ncar", "andreas",
by default "coare3p0"
zt : int, optional
height for temperature and spec. hum. of air [m],
by default 10
zu : int, optional
height for wind (10m = traditional anemometric height [m],
by default 2
niter : int, optional
Number of iteration steps used in the algorithm,
by default 6
Returns
-------
ql : xarray.DataArray
Latent heat flux [W/m^2]
qh : xarray.DataArray
Sensible heat flux [W/m^2]
taux : xarray.DataArray
zonal wind stress [N/m^2]
tauy : xarray.DataArray
meridional wind stress [N/m^2]
evap : xarray.DataArray
evaporation [mm/s] aka [kg/m^2/s] (usually <0, as ocean loses water!)
"""
_check_algo(algo, VALID_ALGOS)
sst, t_zt, hum_zt, u_zu, v_zu, slp = xr.broadcast(
sst, t_zt, hum_zt, u_zu, v_zu, slp
)
out_vars = xr.apply_ufunc(
noskin_np,
sst,
t_zt,
hum_zt,
u_zu,
v_zu,
slp,
input_core_dims=[()] * 6,
output_core_dims=[()] * 5,
dask="parallelized",
kwargs=dict(
algo=algo,
zt=zt,
zu=zu,
niter=niter,
),
output_dtypes=[sst.dtype]
* 5, # deactivates the 1 element check which aerobulk does not like
)
if not isinstance(out_vars, tuple) or len(out_vars) != 5:
raise TypeError("F2Py returned unexpected types")
return out_vars
@input_and_output_check
def skin(
sst,
t_zt,
hum_zt,
u_zu,
v_zu,
rad_sw,
rad_lw,
slp=101000.0,
algo="coare3p0",
zt=2,
zu=10,
niter=6,
):
"""xarray wrapper for aerobulk with skin correction.
Warnings
--------
!ATTENTION If input not provided in the units shown in [] below the code will crash.
Parameters
----------
sst : xr.DataArray
Bulk sea surface temperature [K]
t_zt : xr.DataArray
Absolute air temperature at height zt [K]
hum_zt : xr.DataArray
air humidity at zt, can be given as:
- specific humidity [kg/kg]
- dew-point temperature [K]
- relative humidity [%]
=> type should normally be recognized based on value range
u_zu : xr.DataArray
zonal wind speed at zu [m/s]
v_zu : xr.DataArray
meridional wind speed at zu [m/s]
rad_sw : xr.DataArray
downwelling shortwave radiation at the surface (>0) [W/m^2]
rad_lw : xr.DataArray
rad_lw : downwelling longwave radiation at the surface (>0) [W/m^2]
slp : xr.DataArray, optional
mean sea-level pressure [Pa] ~101000 Pa,
by default 101000.0
algo : str, optional
Algorithm, can be one of: "coare3p0", "coare3p6", "ecmwf",
by default "coare3p0"
zt : int, optional
height for temperature and spec. hum. of air [m],
by default 10
zu : int, optional
height for wind (10m = traditional anemometric height [m],
by default 2
niter : int, optional
Number of iteration steps used in the algorithm,
by default 6
Returns
-------
ql : xr.DataArray
Latent heat flux [W/m^2]
qh : xr.DataArray
Sensible heat flux [W/m^2]
taux : xr.DataArray
zonal wind stress [N/m^2]
tauy : xr.DataArray
meridional wind stress [N/m^2]
t_s : xr.DataArray
skin temperature [K] (only when l_use_skin_schemes=TRUE)
evap : xr.DataArray
evaporation [mm/s] aka [kg/m^2/s] (usually <0, as ocean loses water!)
"""
_check_algo(algo, VALID_ALGOS_SKIN)
sst, t_zt, hum_zt, u_zu, v_zu, rad_sw, rad_lw, slp = xr.broadcast(
sst, t_zt, hum_zt, u_zu, v_zu, rad_sw, rad_lw, slp
)
out_vars = xr.apply_ufunc(
skin_np,
sst,
t_zt,
hum_zt,
u_zu,
v_zu,
rad_sw,
rad_lw,
slp,
input_core_dims=[()] * 8,
output_core_dims=[()] * 6,
dask="parallelized",
kwargs=dict(
algo=algo,
zt=zt,
zu=zu,
niter=niter,
),
output_dtypes=[sst.dtype]
* 6, # deactivates the 1 element check which aerobulk does not like
)
if not isinstance(out_vars, tuple) or len(out_vars) != 6:
raise TypeError("F2Py returned unexpected types")
return out_vars