flux_subs.py 28.1 KB
Newer Older
sbiri's avatar
sbiri committed
1
import numpy as np
2
from util_subs import (CtoK, kappa, gc, visc_air)
sbiri's avatar
sbiri committed
3

sbiri's avatar
sbiri committed
4
# ---------------------------------------------------------------------
sbiri's avatar
sbiri committed
5

sbiri's avatar
sbiri committed
6

sbiri's avatar
sbiri committed
7
def cdn_calc(u10n, Ta, Tp, lat, meth="S80"):
sbiri's avatar
sbiri committed
8
    """ Calculates neutral drag coefficient
sbiri's avatar
sbiri committed
9

sbiri's avatar
sbiri committed
10 11 12 13 14 15 16 17
    Parameters
    ----------
    u10n : float
        neutral 10m wind speed (m/s)
    Ta   : float
        air temperature (K)
    Tp   : float
        wave period
18 19
    lat : float
        latitude
sbiri's avatar
sbiri committed
20 21
    meth : str

sbiri's avatar
sbiri committed
22 23 24 25
    Returns
    -------
    cdn : float
    """
26
    cdn = np.zeros(Ta.shape)*np.nan
sbiri's avatar
sbiri committed
27
    if (meth == "S80"):
sbiri's avatar
sbiri committed
28 29
        cdn = np.where(u10n <= 3, (0.61+0.567/u10n)*0.001,
                       (0.61+0.063*u10n)*0.001)
sbiri's avatar
sbiri committed
30
    elif (meth == "LP82"):
sbiri's avatar
sbiri committed
31 32 33
        cdn = np.where((u10n < 11) & (u10n >= 4), 1.2*0.001,
                       np.where((u10n <= 25) & (u10n >= 11),
                       (0.49+0.065*u10n)*0.001, 1.14*0.001))
sbiri's avatar
sbiri committed
34
    elif (meth == "S88" or meth == "UA" or meth == "ERA5" or meth == "C30" or
35
          meth == "C35" or meth == "C40" or meth == "Beljaars"):
sbiri's avatar
sbiri committed
36 37
        cdn = cdn_from_roughness(u10n, Ta, None, lat, meth)
    elif (meth == "YT96"):
sbiri's avatar
sbiri committed
38
        # for u<3 same as S80
sbiri's avatar
sbiri committed
39 40 41 42
        cdn = np.where((u10n < 6) & (u10n >= 3),
                       (0.29+3.1/u10n+7.7/u10n**2)*0.001,
                       np.where((u10n <= 26) & (u10n >= 6),
                       (0.60 + 0.070*u10n)*0.001, (0.61+0.567/u10n)*0.001))
sbiri's avatar
sbiri committed
43
    elif (meth == "LY04"):
sbiri's avatar
sbiri committed
44
        cdn = np.where(u10n >= 0.5,
45 46
                       (0.142+(2.7/u10n)+(u10n/13.09))*0.001,
                       (0.142+(2.7/0.5)+(0.5/13.09))*0.001)
sbiri's avatar
sbiri committed
47
    else:
sbiri's avatar
sbiri committed
48
        print("unknown method cdn: "+meth)
sbiri's avatar
sbiri committed
49
    return cdn
sbiri's avatar
sbiri committed
50 51 52
# ---------------------------------------------------------------------


sbiri's avatar
sbiri committed
53
def cdn_from_roughness(u10n, Ta, Tp, lat, meth="S88"):
sbiri's avatar
sbiri committed
54
    """ Calculates neutral drag coefficient from roughness length
sbiri's avatar
sbiri committed
55

sbiri's avatar
sbiri committed
56 57 58 59 60 61 62 63
    Parameters
    ----------
    u10n : float
        neutral 10m wind speed (m/s)
    Ta   : float
        air temperature (K)
    Tp   : float
        wave period
64 65
    lat : float
        latitude
sbiri's avatar
sbiri committed
66 67
    meth : str

sbiri's avatar
sbiri committed
68 69 70 71
    Returns
    -------
    cdn : float
    """
sbiri's avatar
sbiri committed
72
    g, tol = gc(lat, None), 0.000001
sbiri's avatar
sbiri committed
73 74 75
    cdn, usr = np.zeros(Ta.shape), np.zeros(Ta.shape)
    cdnn = (0.61+0.063*u10n)*0.001
    zo, zc, zs = np.zeros(Ta.shape), np.zeros(Ta.shape), np.zeros(Ta.shape)
sbiri's avatar
sbiri committed
76 77
    for it in range(5):
        cdn = np.copy(cdnn)
sbiri's avatar
sbiri committed
78
        usr = np.sqrt(cdn*u10n**2)
sbiri's avatar
sbiri committed
79
        if (meth == "S88"):
80
            # Charnock roughness length (eq. 4 in Smith 88)
sbiri's avatar
sbiri committed
81
            zc = 0.011*np.power(usr, 2)/g
82
            #  smooth surface roughness length (eq. 6 in Smith 88)
sbiri's avatar
sbiri committed
83
            zs = 0.11*visc_air(Ta)/usr
84
            zo = zc + zs  #  eq. 7 & 8 in Smith 88
sbiri's avatar
sbiri committed
85
        elif (meth == "UA"):
sbiri's avatar
sbiri committed
86 87
            # valid for 0<u<18m/s # Zeng et al. 1998 (24)
            zo = 0.013*np.power(usr, 2)/g+0.11*visc_air(Ta)/usr
sbiri's avatar
sbiri committed
88 89
        elif (meth == "C30"):
            a = 0.011*np.ones(Ta.shape)
90
            a = np.where(u10n > 10, 0.011+(u10n-10)*(0.018-0.011)/(18-10),
sbiri's avatar
sbiri committed
91 92 93 94
                         np.where(u10n > 18, 0.018, a))
            zo = a*np.power(usr, 2)/g+0.11*visc_air(Ta)/usr
        elif (meth == "C35"):
            a = 0.011*np.ones(Ta.shape)
95 96 97
            # a = np.where(u10n > 19, 0.0017*19-0.0050,
            #             np.where((u10n > 7) & (u10n <= 18),
            #                       0.0017*u10n-0.0050, a))
98
            a = np.where(u10n > 19, 0.0017*19-0.0050, 0.0017*u10n-0.0050)
sbiri's avatar
sbiri committed
99 100 101 102 103
            zo = 0.11*visc_air(Ta)/usr+a*np.power(usr, 2)/g
        elif (meth == "C40"):
            a = 0.011*np.ones(Ta.shape)
            a = np.where(u10n > 22, 0.0016*22-0.0035, 0.0016*u10n-0.0035)
            zo = a*np.power(usr, 2)/g+0.11*visc_air(Ta)/usr # surface roughness
104
        elif ((meth == "ERA5" or meth == "Beljaars")):
105
            # eq. (3.26) p.38 over sea IFS Documentation cy46r1
106
            zo = 0.018*np.power(usr, 2)/g+0.11*visc_air(Ta)/usr
sbiri's avatar
sbiri committed
107
        else:
sbiri's avatar
sbiri committed
108
            print("unknown method for cdn_from_roughness "+meth)
sbiri's avatar
sbiri committed
109
        cdnn = (kappa/np.log(10/zo))**2
sbiri's avatar
sbiri committed
110
    cdn = np.where(np.abs(cdnn-cdn) < tol, cdnn, np.nan)
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    return cdn
# ---------------------------------------------------------------------


def cd_calc(cdn, height, ref_ht, psim):
    """ Calculates drag coefficient at reference height

    Parameters
    ----------
    cdn : float
        neutral drag coefficient
    height : float
        original sensor height (m)
    ref_ht : float
        reference height (m)
    psim : float
        momentum stability function

    Returns
    -------
    cd : float
    """
    cd = (cdn/np.power(1+(np.sqrt(cdn)*(np.log(height/ref_ht)-psim))/kappa, 2))
    return cd
sbiri's avatar
sbiri committed
135
# ---------------------------------------------------------------------
sbiri's avatar
sbiri committed
136

sbiri's avatar
sbiri committed
137

sbiri's avatar
sbiri committed
138
def ctcqn_calc(zol, cdn, u10n, zo, Ta, meth="S80"):
sbiri's avatar
sbiri committed
139
    """ Calculates neutral heat and moisture exchange coefficients
sbiri's avatar
sbiri committed
140

sbiri's avatar
sbiri committed
141 142 143 144 145
    Parameters
    ----------
    zol  : float
        height over MO length
    cdn  : float
146
        neutral drag coefficient
sbiri's avatar
sbiri committed
147 148 149 150 151 152
    u10n : float
        neutral 10m wind speed (m/s)
    zo   : float
        surface roughness (m)
    Ta   : float
        air temperature (K)
sbiri's avatar
sbiri committed
153 154
    meth : str

sbiri's avatar
sbiri committed
155 156 157 158 159 160 161
    Returns
    -------
    ctn : float
        neutral heat exchange coefficient
    cqn : float
        neutral moisture exchange coefficient
    """
sbiri's avatar
sbiri committed
162
    if (meth == "S80" or meth == "S88" or meth == "YT96"):
sbiri's avatar
sbiri committed
163
        cqn = np.ones(Ta.shape)*1.20*0.001  # from S88
sbiri's avatar
sbiri committed
164
        ctn = np.ones(Ta.shape)*1.00*0.001
sbiri's avatar
sbiri committed
165
    elif (meth == "LP82"):
sbiri's avatar
sbiri committed
166
        cqn = np.where((zol <= 0) & (u10n > 4) & (u10n < 14), 1.15*0.001,
167
                       1*0.001)
sbiri's avatar
sbiri committed
168 169
        ctn = np.where((zol <= 0) & (u10n > 4) & (u10n < 25), 1.13*0.001,
                       0.66*0.001)
sbiri's avatar
sbiri committed
170 171 172 173 174
    elif (meth == "LY04"):
        cqn = 34.6*0.001*np.sqrt(cdn)
        ctn = np.where(zol <= 0, 32.7*0.001*np.sqrt(cdn), 18*0.001*np.sqrt(cdn))
    elif (meth == "UA"):
        usr = np.sqrt(cdn*np.power(u10n, 2))
sbiri's avatar
sbiri committed
175
        # Zeng et al. 1998 (25)
sbiri's avatar
sbiri committed
176 177
        re=usr*zo/visc_air(Ta)
        zoq = zo/np.exp(2.67*np.power(re, 1/4)-2.57)
sbiri's avatar
sbiri committed
178 179 180 181 182
        zot = zoq
        cqn = np.where((u10n > 0.5) & (u10n < 18), np.power(kappa, 2) /
                       (np.log(10/zo)*np.log(10/zoq)), np.nan)
        ctn = np.where((u10n > 0.5) & (u10n < 18), np.power(kappa, 2) /
                       (np.log(10/zo)*np.log(10/zoq)), np.nan)
sbiri's avatar
sbiri committed
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
    elif (meth == "C30"):
        usr = np.sqrt(cdn*np.power(u10n, 2))
        rr = zo*usr/visc_air(Ta)
        zoq = np.where(5e-5/np.power(rr, 0.6) > 1.15e-4, 1.15e-4,
                       5e-5/np.power(rr, 0.6))  # moisture roughness
        zot=zoq  # temperature roughness
        cqn = kappa**2/np.log(10/zo)/np.log(10/zoq)
        ctn = kappa**2/np.log(10/zo)/np.log(10/zot)
    elif (meth == "C35"):
        usr = np.sqrt(cdn*np.power(u10n, 2))
        rr = zo*usr/visc_air(Ta)
        zoq = np.where(5.8e-5/np.power(rr, 0.72) > 1.6e-4, 1.6e-4,
                       5.8e-5/np.power(rr, 0.72))  # moisture roughness
        zot=zoq  # temperature roughness
        cqn = kappa**2/np.log(10/zo)/np.log(10/zoq)
        ctn = kappa**2/np.log(10/zo)/np.log(10/zot)
    elif (meth == "C40"):
        usr = np.sqrt(cdn*np.power(u10n, 2))
        rr = zo*usr/visc_air(Ta)
        zot = np.where(1.0e-4/np.power(rr, 0.55) > 2.4e-4/np.power(rr, 1.2),
                       2.4e-4/np.power(rr, 1.2),
                       1.0e-4/np.power(rr, 0.55)) # temperature roughness
        zoq = np.where(2.0e-5/np.power(rr,0.22) > 1.1e-4/np.power(rr,0.9),
                       1.1e-4/np.power(rr,0.9), 2.0e-5/np.power(rr,0.22))
        # moisture roughness determined by the CLIMODE, GASEX and CBLAST data
#        zoq = np.where(5e-5/np.power(rr, 0.6) > 1.15e-4, 1.15e-4,
#                       5e-5/np.power(rr, 0.6))  # moisture roughness as in C30
        cqn = kappa**2/np.log(10/zo)/np.log(10/zoq)
        ctn = kappa**2/np.log(10/zo)/np.log(10/zot)
212
    elif (meth == "ERA5" or meth == "Beljaars"):
213
        # eq. (3.26) p.38 over sea IFS Documentation cy46r1
sbiri's avatar
sbiri committed
214 215 216 217 218
        usr = np.sqrt(cdn*np.power(u10n, 2))
        zot = 0.40*visc_air(Ta)/usr
        zoq = 0.62*visc_air(Ta)/usr
        cqn = kappa**2/np.log(10/zo)/np.log(10/zoq)
        ctn = kappa**2/np.log(10/zo)/np.log(10/zot)
sbiri's avatar
sbiri committed
219
    else:
sbiri's avatar
sbiri committed
220
        print("unknown method ctcqn: "+meth)
sbiri's avatar
sbiri committed
221
    return ctn, cqn
sbiri's avatar
sbiri committed
222 223 224
# ---------------------------------------------------------------------


225
def ctcq_calc(cdn, cd, ctn, cqn, ht, hq, ref_ht, psit, psiq):
sbiri's avatar
sbiri committed
226
    """ Calculates heat and moisture exchange coefficients at reference height
sbiri's avatar
sbiri committed
227

sbiri's avatar
sbiri committed
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
    Parameters
    ----------
    cdn : float
        neutral drag coefficient
    cd  : float
        drag coefficient at reference height
    ctn : float
        neutral heat exchange coefficient
    cqn : float
        neutral moisture exchange coefficient
    h_t : float
        original temperature sensor height (m)
    h_q : float
        original moisture sensor height (m)
    ref_ht : float
        reference height (m)
    psit : float
        heat stability function
    psiq : float
        moisture stability function
sbiri's avatar
sbiri committed
248

sbiri's avatar
sbiri committed
249 250 251
    Returns
    -------
    ct : float
252
       heat exchange coefficient
sbiri's avatar
sbiri committed
253
    cq : float
254
       moisture exchange coefficient
sbiri's avatar
sbiri committed
255
    """
256
    ct = (ctn*np.sqrt(cd/cdn) /
257
          (1+ctn*((np.log(ht/ref_ht)-psit)/(kappa*np.sqrt(cdn)))))
258
    cq = (cqn*np.sqrt(cd/cdn) /
259
          (1+cqn*((np.log(hq/ref_ht)-psiq)/(kappa*np.sqrt(cdn)))))
sbiri's avatar
sbiri committed
260
    return ct, cq
sbiri's avatar
sbiri committed
261 262 263
# ---------------------------------------------------------------------


sbiri's avatar
sbiri committed
264 265 266 267 268 269 270 271 272 273 274 275 276
def get_stabco(meth="S80"):
    """ Gives the coefficients \\alpha, \\beta, \\gamma for stability functions

    Parameters
    ----------
    meth : str

    Returns
    -------
    coeffs : float
    """
    alpha, beta, gamma = 0, 0, 0
    if (meth == "S80" or meth == "S88" or meth == "LY04" or
277 278
        meth == "UA" or meth == "ERA5" or meth == "C30" or
        meth == "C35" or meth == "C40" or meth == "Beljaars"):
sbiri's avatar
sbiri committed
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
        alpha, beta, gamma = 16, 0.25, 5  # Smith 1980, from Dyer (1974)
    elif (meth == "LP82"):
        alpha, beta, gamma = 16, 0.25, 7
    elif (meth == "YT96"):
        alpha, beta, gamma = 20, 0.25, 5
    else:
        print("unknown method stabco: "+meth)
    coeffs = np.zeros(3)
    coeffs[0] = alpha
    coeffs[1] = beta
    coeffs[2] = gamma
    return coeffs
# ---------------------------------------------------------------------


sbiri's avatar
sbiri committed
294
def psim_calc(zol, meth="S80"):
sbiri's avatar
sbiri committed
295
    """ Calculates momentum stability function
sbiri's avatar
sbiri committed
296

sbiri's avatar
sbiri committed
297 298 299 300
    Parameters
    ----------
    zol : float
        height over MO length
sbiri's avatar
sbiri committed
301 302
    meth : str

sbiri's avatar
sbiri committed
303 304 305 306
    Returns
    -------
    psim : float
    """
sbiri's avatar
sbiri committed
307
    if (meth == "ERA5"):
308
        psim = psim_era5(zol)
sbiri's avatar
sbiri committed
309 310
    elif (meth == "C30" or meth == "C35" or meth == "C40"):
        psim = psiu_26(zol, meth)
311 312
    elif (meth == "Beljaars"): # Beljaars (1997) eq. 16, 17
        psim = np.where(zol < 0, psim_conv(zol, meth), psi_Bel(zol))
sbiri's avatar
sbiri committed
313
    else:
314 315
        psim = np.where(zol < 0, psim_conv(zol, meth),
                        psim_stab(zol, meth))
sbiri's avatar
sbiri committed
316
    return psim
sbiri's avatar
sbiri committed
317 318 319
# ---------------------------------------------------------------------


sbiri's avatar
sbiri committed
320
def psit_calc(zol, meth="S80"):
sbiri's avatar
sbiri committed
321
    """ Calculates heat stability function
sbiri's avatar
sbiri committed
322

sbiri's avatar
sbiri committed
323 324 325 326
    Parameters
    ----------
    zol : float
        height over MO length
sbiri's avatar
sbiri committed
327
    meth : str
328
        parameterisation method
sbiri's avatar
sbiri committed
329

sbiri's avatar
sbiri committed
330 331 332 333
    Returns
    -------
    psit : float
    """
sbiri's avatar
sbiri committed
334
    if (meth == "ERA5"):
335 336
        psit = np.where(zol < 0, psi_conv(zol, meth),
                        psi_era5(zol))
sbiri's avatar
sbiri committed
337 338
    elif (meth == "C30" or meth == "C35" or meth == "C40"):
        psit = psit_26(zol)
339 340
    elif (meth == "Beljaars"): # Beljaars (1997) eq. 16, 17
        psit = np.where(zol < 0, psi_conv(zol, meth), psi_Bel(zol))
sbiri's avatar
sbiri committed
341
    else:
342 343
        psit = np.where(zol < 0, psi_conv(zol, meth),
                        psi_stab(zol, meth))
sbiri's avatar
sbiri committed
344
    return psit
sbiri's avatar
sbiri committed
345 346 347
# ---------------------------------------------------------------------


348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
def psi_Bel(zol):
    """ Calculates momentum/heat stability function

    Parameters
    ----------
    zol : float
        height over MO length
    meth : str
        parameterisation method

    Returns
    -------
    psit : float
    """
    a, b, c, d = 0.7, 0.75, 5, 0.35
    psi = -(a*zol+b*(zol-c/d)*np.exp(-d*zol)+b*c/d)
    return psi
# ---------------------------------------------------------------------


368
def psi_era5(zol):
sbiri's avatar
sbiri committed
369
    """ Calculates heat stability function for stable conditions
sbiri's avatar
sbiri committed
370
        for method ERA5
sbiri's avatar
sbiri committed
371

sbiri's avatar
sbiri committed
372 373 374 375
    Parameters
    ----------
    zol : float
        height over MO length
sbiri's avatar
sbiri committed
376

sbiri's avatar
sbiri committed
377 378 379 380
    Returns
    -------
    psit : float
    """
381
    # eq (3.22) p. 37 IFS Documentation cy46r1
sbiri's avatar
sbiri committed
382 383
    a, b, c, d = 1, 2/3, 5, 0.35
    psit = -b*(zol-c/d)*np.exp(-d*zol)-np.power(1+(2/3)*a*zol, 1.5)-(b*c)/d+1
sbiri's avatar
sbiri committed
384
    return psit
sbiri's avatar
sbiri committed
385 386
# ---------------------------------------------------------------------

sbiri's avatar
sbiri committed
387 388 389 390 391 392 393 394 395 396 397 398 399 400

def psit_26(zol):
    """ Computes temperature structure function as in C35

    Parameters
    ----------
    zol : float
        height over MO length

    Returns
    -------
    psi : float
    """
    b, d = 2/3, 0.35
401 402 403 404 405 406 407 408
    dzol = np.where(d*zol > 50, 50, d*zol)
    psi = np.where(zol > 0,-(np.power(1+b*zol, 1.5)+b*(zol-14.28) *
                             np.exp(-dzol)+8.525), np.nan)
    psik = np.where(zol < 0, 2*np.log((1+np.sqrt(1-15*zol))/2), np.nan)
    psic = np.where(zol < 0, 1.5*np.log((1+np.power(1-34.15*zol, 1/3) +
                    np.power(1-34.15*zol, 2/3))/3)-np.sqrt(3) *
                    np.arctan(1+2*np.power(1-34.15*zol, 1/3))/np.sqrt(3) +
                    4*np.arctan(1)/np.sqrt(3), np.nan)
409 410
    f = np.power(zol, 2)/(1+np.power(zol, 2))
    psi = np.where(zol < 0, (1-f)*psik+f*psic, psi)
sbiri's avatar
sbiri committed
411 412 413 414
    return psi
# ---------------------------------------------------------------------


415
def psi_conv(zol, meth):
sbiri's avatar
sbiri committed
416
    """ Calculates heat stability function for unstable conditions
sbiri's avatar
sbiri committed
417

sbiri's avatar
sbiri committed
418 419 420 421
    Parameters
    ----------
    zol : float
        height over MO length
422 423
    meth : str
        parameterisation method
sbiri's avatar
sbiri committed
424

sbiri's avatar
sbiri committed
425 426 427 428
    Returns
    -------
    psit : float
    """
429 430
    coeffs = get_stabco(meth)
    alpha, beta = coeffs[0], coeffs[1]
431 432
    xtmp = np.power(1-alpha*zol, beta)
    psit = 2*np.log((1+np.power(xtmp, 2))*0.5)
sbiri's avatar
sbiri committed
433
    return psit
sbiri's avatar
sbiri committed
434 435 436
# ---------------------------------------------------------------------


437
def psi_stab(zol, meth):
sbiri's avatar
sbiri committed
438
    """ Calculates heat stability function for stable conditions
sbiri's avatar
sbiri committed
439

sbiri's avatar
sbiri committed
440 441 442 443
    Parameters
    ----------
    zol : float
        height over MO length
444 445
    meth : str
        parameterisation method
sbiri's avatar
sbiri committed
446

sbiri's avatar
sbiri committed
447 448 449 450
    Returns
    -------
    psit : float
    """
451 452
    coeffs = get_stabco(meth)
    gamma = coeffs[2]
sbiri's avatar
sbiri committed
453
    psit = -gamma*zol
sbiri's avatar
sbiri committed
454
    return psit
sbiri's avatar
sbiri committed
455 456 457
# ---------------------------------------------------------------------


458 459
def psim_era5(zol):
    """ Calculates momentum stability function for method ERA5
sbiri's avatar
sbiri committed
460

sbiri's avatar
sbiri committed
461 462 463 464
    Parameters
    ----------
    zol : float
        height over MO length
sbiri's avatar
sbiri committed
465

sbiri's avatar
sbiri committed
466 467 468 469
    Returns
    -------
    psim : float
    """
470 471 472 473
    # eq (3.20, 3.22) p. 37 IFS Documentation cy46r1
    coeffs = get_stabco("ERA5")
    alpha, beta = coeffs[0], coeffs[1]
    xtmp = np.power(1-alpha*zol, beta)
sbiri's avatar
sbiri committed
474
    a, b, c, d = 1, 2/3, 5, 0.35
475 476 477
    psim = np.where(zol < 0, np.pi/2-2*np.arctan(xtmp) +
                    np.log((np.power(1+xtmp, 2)*(1+np.power(xtmp, 2)))/8),
                    -b*(zol-c/d)*np.exp(-d*zol)-a*zol-(b*c)/d)
sbiri's avatar
sbiri committed
478 479 480 481
    return psim
# ---------------------------------------------------------------------


sbiri's avatar
sbiri committed
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
def psiu_26(zol, meth):
    """ Computes velocity structure function C35

    Parameters
    ----------
    zol : float
        height over MO length

    Returns
    -------
    psi : float
    """
    if (meth == "C30"):
        dzol = np.where(0.35*zol > 50, 50, 0.35*zol) # stable
        psi = np.where(zol > 0, -((1+zol)+0.6667*(zol-14.28)*np.exp(-dzol) +
                                  8.525), np.nan)
        x = np.where(zol < 0, np.power(1-15*zol, 0.25), np.nan)
        psik = np.where(zol < 0, 2*np.log((1+x)/2)+np.log((1+np.power(x, 2)) /
                        2)-2*np.arctan(x)+2*np.arctan(1), np.nan)
        x = np.where(zol < 0, np.power(1-10.15*zol, 0.3333), np.nan)
        psic = np.where(zol < 0, 1.5*np.log((1+x+np.power(x, 2))/3) -
                        np.sqrt(3)*np.arctan((1+2*x)/np.sqrt(3)) +
                        4*np.arctan(1)/np.sqrt(3), np.nan)
        f = np.power(zol, 2)/(1+np.power(zol, 2))
        psi = np.where(zol < 0, (1-f)*psik+f*psic, psi)
    elif (meth == "C35" or meth == "C40"):
        dzol = np.where(0.35*zol > 50, 50, 0.35*zol)  # stable
        a, b, c, d = 0.7, 3/4, 5, 0.35
        psi = np.where(zol > 0, -(a*zol+b*(zol-c/d)*np.exp(-dzol)+b*c/d),
                       np.nan)
        x = np.where(zol < 0, np.power(1-15*zol, 0.25), np.nan)
        psik = np.where(zol < 0, 2*np.log((1+x)/2)+np.log((1+x**2)/2) -
                        2*np.arctan(x)+2*np.arctan(1), np.nan)
        x = np.where(zol < 0, np.power(1-10.15*zol, 0.3333), np.nan)
        psic = np.where(zol < 0, 1.5*np.log((1+x+np.power(x, 2))/3) -
                        np.sqrt(3)*np.arctan((1+2*x)/np.sqrt(3)) +
                        4*np.arctan(1)/np.sqrt(3), np.nan)
        f = np.power(zol, 2)/(1+np.power(zol, 2))
        psi = np.where(zol < 0, (1-f)*psik+f*psic, psi)
    return psi
522 523
#------------------------------------------------------------------------------

sbiri's avatar
sbiri committed
524 525


526
def psim_conv(zol, meth):
sbiri's avatar
sbiri committed
527
    """ Calculates momentum stability function for unstable conditions
sbiri's avatar
sbiri committed
528

sbiri's avatar
sbiri committed
529 530 531 532
    Parameters
    ----------
    zol : float
        height over MO length
533 534
    meth : str
        parameterisation method
sbiri's avatar
sbiri committed
535

sbiri's avatar
sbiri committed
536 537 538 539
    Returns
    -------
    psim : float
    """
540 541
    coeffs = get_stabco(meth)
    alpha, beta = coeffs[0], coeffs[1]
542 543
    xtmp = np.power(1-alpha*zol, beta)
    psim = (2*np.log((1+xtmp)*0.5)+np.log((1+np.power(xtmp, 2))*0.5) -
sbiri's avatar
sbiri committed
544
            2*np.arctan(xtmp)+np.pi/2)
sbiri's avatar
sbiri committed
545
    return psim
sbiri's avatar
sbiri committed
546 547 548
# ---------------------------------------------------------------------


549
def psim_stab(zol, meth):
sbiri's avatar
sbiri committed
550
    """ Calculates momentum stability function for stable conditions
sbiri's avatar
sbiri committed
551

sbiri's avatar
sbiri committed
552 553 554 555
    Parameters
    ----------
    zol : float
        height over MO length
556 557
    meth : str
        parameterisation method
sbiri's avatar
sbiri committed
558

sbiri's avatar
sbiri committed
559 560 561 562
    Returns
    -------
    psim : float
    """
563 564
    coeffs = get_stabco(meth)
    gamma = coeffs[2]
sbiri's avatar
sbiri committed
565 566 567 568 569
    psim = -gamma*zol
    return psim
# ---------------------------------------------------------------------


sbiri's avatar
sbiri committed
570
def get_skin(sst, qsea, rho, Rl, Rs, Rnl, cp, lv, tkt, usr, tsr, qsr, lat):
sbiri's avatar
sbiri committed
571
    """ Computes cool skin
sbiri's avatar
sbiri committed
572

sbiri's avatar
sbiri committed
573 574 575 576 577 578 579 580 581 582 583 584
    Parameters
    ----------
    sst : float
        sea surface temperature ($^\circ$\,C)
    qsea : float
        specific humidity over sea (g/kg)
    rho : float
        density of air (kg/m^3)
    Rl : float
        downward longwave radiation (W/m^2)
    Rs : float
        downward shortwave radiation (W/m^2)
sbiri's avatar
sbiri committed
585 586
    Rnl : float
        upwelling IR radiation (W/m^2)
sbiri's avatar
sbiri committed
587 588 589 590
    cp : float
       specific heat of air at constant pressure
    lv : float
       latent heat of vaporization
sbiri's avatar
sbiri committed
591 592
    tkt : float
       cool skin thickness
sbiri's avatar
sbiri committed
593 594 595 596 597 598 599 600
    usr : float
       friction velocity
    tsr : float
       star temperature
    qsr : float
       star humidity
    lat : float
       latitude
sbiri's avatar
sbiri committed
601

sbiri's avatar
sbiri committed
602 603 604
    Returns
    -------
    dter : float
605
       cool-skin temperature depression
sbiri's avatar
sbiri committed
606
    dqer : float
607 608 609
       cool-skin humidity depression
    tkt  : float
       cool skin thickness
sbiri's avatar
sbiri committed
610
    """
sbiri's avatar
sbiri committed
611 612
    # coded following Saunders (1967) with lambda = 6
    g = gc(lat, None)
sbiri's avatar
sbiri committed
613 614
    if (np.nanmin(sst) > 200):  # if sst in Kelvin convert to Celsius
        sst = sst-CtoK
sbiri's avatar
sbiri committed
615 616 617 618
    # ************  cool skin constants  *******
    # density of water, specific heat capacity of water, water viscosity,
    # thermal conductivity of water
    rhow, cpw, visw, tcw = 1022, 4000, 1e-6, 0.6
sbiri's avatar
sbiri committed
619
    Al = 2.1e-5*np.power(sst+3.2, 0.79)
sbiri's avatar
sbiri committed
620
    be = 0.026
sbiri's avatar
sbiri committed
621 622
    bigc = 16*g*cpw*np.power(rhow*visw, 3)/(np.power(tcw, 2)*np.power(rho, 2))
    wetc = 0.622*lv*qsea/(287.1*np.power(sst+273.16, 2))
sbiri's avatar
sbiri committed
623 624 625 626 627 628 629
    Rns = 0.945*Rs  # albedo correction
    hsb = -rho*cp*usr*tsr
    hlb = -rho*lv*usr*qsr
    qout = Rnl+hsb+hlb
    dels = Rns*(0.065+11*tkt-6.6e-5/tkt*(1-np.exp(-tkt/8.0e-4)))
    qcol = qout-dels
    alq = Al*qcol+be*hlb*cpw/lv
sbiri's avatar
sbiri committed
630
    xlamx = 6*np.ones(sst.shape)
sbiri's avatar
sbiri committed
631
    xlamx = np.where(alq > 0, 6/(1+(bigc*alq/usr**4)**0.75)**0.333, 6)
sbiri's avatar
sbiri committed
632 633 634
    tkt = np.where(alq > 0, xlamx*visw/(np.sqrt(rho/rhow)*usr),
                   np.where(xlamx*visw/(np.sqrt(rho/rhow)*usr) > 0.01, 0.01,
                   xlamx*visw/(np.sqrt(rho/rhow)*usr)))
sbiri's avatar
sbiri committed
635 636
    dter = qcol*tkt/tcw
    dqer = wetc*dter
sbiri's avatar
sbiri committed
637
    return dter, dqer, tkt
sbiri's avatar
sbiri committed
638 639 640 641
# ---------------------------------------------------------------------


def get_gust(beta, Ta, usr, tsrv, zi, lat):
sbiri's avatar
sbiri committed
642
    """ Computes gustiness
sbiri's avatar
sbiri committed
643

sbiri's avatar
sbiri committed
644 645 646 647 648 649 650 651 652 653 654 655 656 657
    Parameters
    ----------
    beta : float
        constant
    Ta : float
        air temperature (K)
    usr : float
        friction velocity (m/s)
    tsrv : float
        star virtual temperature of air (K)
    zi : int
        scale height of the boundary layer depth (m)
    lat : float
        latitude
sbiri's avatar
sbiri committed
658

sbiri's avatar
sbiri committed
659 660 661 662
    Returns
    -------
    ug : float
    """
663
    if (np.nanmax(Ta) < 200):  # convert to K if in Celsius
sbiri's avatar
sbiri committed
664 665
        Ta = Ta+273.16
    g = gc(lat, None)
sbiri's avatar
sbiri committed
666
    Bf = (-g/Ta)*usr*tsrv
sbiri's avatar
sbiri committed
667 668 669 670 671 672
    ug = np.ones(np.shape(Ta))*0.2
    ug = np.where(Bf > 0, beta*np.power(Bf*zi, 1/3), 0.2)
    return ug
# ---------------------------------------------------------------------


sbiri's avatar
sbiri committed
673
def get_L(L, lat, usr, tsr, qsr, t10n, tv10n, qair, h_in, T, Ta, th, tv, sst,
674
          dt, dtv, dq, zo, wind, monob, meth):
sbiri's avatar
sbiri committed
675 676 677 678 679 680 681
    """
    calculates Monin-Obukhov length and virtual star temperature

    Parameters
    ----------
    L : int
        Monin-Obukhov length definition options
682 683
           "S80"  : default for S80, S88, LP82, YT96 and LY04
           "ERA5" : following ERA5 (IFS Documentation cy46r1), default for ERA5
sbiri's avatar
sbiri committed
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
    lat : float
        latitude
    usr : float
        friction wind speed (m/s)
    tsr : float
        star temperature (K)
    qsr : float
        star specific humidity (g/kg)
    t10n : float
        neutral temperature at 10m (K)
    tv10n : float
        neutral virtual temperature at 10m (K)
    qair : float
        air specific humidity (g/kg)
    h_in : float
        sensor heights (m)
    T : float
        air temperature (K)
    Ta : float
        air temperature (K)
    th : float
        potential temperature (K)
    tv : float
        virtual temperature (K)
    sst : float
        sea surface temperature (K)
    dt : float
        temperature difference (K)
    dq : float
        specific humidity difference (g/kg)
    wind : float
        wind speed (m/s)
    monob : float
        Monin-Obukhov length from previous iteration step (m)
    meth : str
719 720
        bulk parameterisation method option: "S80", "S88", "LP82", "YT96",
        "UA", "LY04", "C30", "C35", "C40", "ERA5"
sbiri's avatar
sbiri committed
721 722 723

    Returns
    -------
724 725 726 727
    tsrv : float
        virtual star temperature (K)
    monob : float
        M-O length (m)
sbiri's avatar
sbiri committed
728 729

    """
sbiri's avatar
sbiri committed
730
    g = gc(lat)
731
    if (L == "S80"):
sbiri's avatar
sbiri committed
732 733 734
        tsrv = tsr+0.61*t10n*qsr
        monob = ((tv10n*np.power(usr, 2))/(g*kappa*tsrv))
        monob = np.where(np.fabs(monob) < 1, np.where(monob < 0, -1, 1), monob)
735
    elif (L == "ERA5"):
sbiri's avatar
sbiri committed
736 737 738 739 740 741 742 743 744 745 746
        tsrv = tsr+0.61*t10n*qsr
        Rb = ((g*h_in[0]*((2*dt)/(Ta+sst-g*h_in[0])+0.61*dq)) /
              np.power(wind, 2))
        zo = (0.11*visc_air(Ta)/usr+0.018*np.power(usr, 2)/g)
        zot = 0.40*visc_air(Ta)/usr
        zol = (Rb*(np.power(np.log((h_in[0]+zo)/zo)-psim_calc((h_in[0]+zo) /
                                                              monob, meth) +
                            psim_calc(zo/monob, meth), 2) /
                   (np.log((h_in[0]+zo)/zot) -
                    psit_calc((h_in[0]+zo)/monob, meth) +
                    psit_calc(zot/monob, meth))))
747
        monob = h_in[0]/zol        
sbiri's avatar
sbiri committed
748
    return tsrv, monob
sbiri's avatar
sbiri committed
749 750 751
#------------------------------------------------------------------------------


752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
def get_strs(h_in, monob, wind, zo, zot, zoq, dt, dq, dter, dqer, ct, cq,
             cskin, meth):
    """
    calculates star wind speed, temperature and specific humidity

    Parameters
    ----------
    h_in : float
        sensor heights (m)
    monob : float
        M-O length (m)
    wind : float
        wind speed (m/s)
    zo : float
        momentum roughness length (m)
    zot : float
        temperature roughness length (m)
    zoq : float
        moisture roughness length (m)
    dt : float
        temperature difference (K)
    dq : float
        specific humidity difference (g/kg)
    dter : float
        cskin temperature adjustment (K)
    dqer : float
778
        cskin q adjustment (g/kg)
779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
    ct : float
        temperature exchange coefficient
    cq : float
        moisture exchange coefficient
    cskin : int
        cool skin adjustment switch
    meth : str
        bulk parameterisation method option: "S80", "S88", "LP82", "YT96", "UA",
        "LY04", "C30", "C35", "C40", "ERA5"

    Returns
    -------
    usr : float
        friction wind speed (m/s)
    tsr : float
        star temperature (K)
    qsr : float
        star specific humidity (g/kg)

    """
    if (meth == "UA"):
800
        usr = np.where(h_in[0]/monob <= -1.574, kappa*wind /
801 802 803 804
                       (np.log(-1.574*monob/zo)-psim_calc(-1.574, meth) +
                        psim_calc(zo/monob, meth) +
                        1.14*(np.power(-h_in[0]/monob, 1/3) -
                        np.power(1.574, 1/3))),
805 806 807 808 809 810 811 812 813 814 815
                       np.where(h_in[0]/monob < 0, kappa*wind /
                                (np.log(h_in[0]/zo) -
                                 psim_calc(h_in[0]/monob, meth) +
                                 psim_calc(zo/monob, meth)),
                                np.where(h_in[0]/monob <= 1, kappa*wind /
                                         (np.log(h_in[0]/zo) +
                                          5*h_in[0]/monob-5*zo/monob),
                                         kappa*wind/(np.log(monob/zo)+5 -
                                                     5*zo/monob +
                                                     5*np.log(h_in[0]/monob) +
                                                     h_in[0]/monob-1))))
816 817 818 819 820
                                # Zeng et al. 1998 (7-10)
        tsr = np.where(h_in[1]/monob < -0.465, kappa*(dt+dter*cskin) /
                       (np.log((-0.465*monob)/zot) -
                        psit_calc(-0.465, meth)+0.8*(np.power(0.465, -1/3) -
                        np.power(-h_in[1]/monob, -1/3))),
821 822 823 824 825 826 827 828 829 830 831 832
                       np.where(h_in[1]/monob < 0, kappa*(dt+dter*cskin) /
                                (np.log(h_in[1]/zot) -
                                 psit_calc(h_in[1]/monob, meth) +
                                 psit_calc(zot/monob, meth)),
                                np.where(h_in[1]/monob <= 1,
                                         kappa*(dt+dter*cskin) /
                                         (np.log(h_in[1]/zot) +
                                          5*h_in[1]/monob-5*zot/monob),
                                         kappa*(dt+dter*cskin) /
                                         (np.log(monob/zot)+5 -
                                          5*zot/monob+5*np.log(h_in[1]/monob) +
                                          h_in[1]/monob-1))))
833 834 835 836 837 838
                                # Zeng et al. 1998 (11-14)
        qsr = np.where(h_in[2]/monob < -0.465, kappa*(dq+dqer*cskin) /
                       (np.log((-0.465*monob)/zoq) -
                        psit_calc(-0.465, meth)+psit_calc(zoq/monob, meth) +
                        0.8*(np.power(0.465, -1/3) -
                             np.power(-h_in[2]/monob, -1/3))),
839
                       np.where(h_in[2]/monob < 0,
840 841 842
                                kappa*(dq+dqer*cskin)/(np.log(h_in[1]/zot) -
                                psit_calc(h_in[2]/monob, meth) +
                                psit_calc(zoq/monob, meth)),
843
                                np.where(h_in[2]/monob <= 1,
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
                                         kappa*(dq+dqer*cskin) /
                                         (np.log(h_in[1]/zoq)+5*h_in[2]/monob -
                                          5*zoq/monob),
                                         kappa*(dq+dqer*cskin)/
                                         (np.log(monob/zoq)+5-5*zoq/monob +
                                          5*np.log(h_in[2]/monob) +
                                          h_in[2]/monob-1))))
    elif (meth == "C30" or meth == "C35" or meth == "C40"):
        usr = (wind*kappa/(np.log(h_in[0]/zo)-psiu_26(h_in[0]/monob, meth)))
        tsr = ((dt+dter*cskin)*(kappa/(np.log(h_in[1]/zot) -
                                       psit_26(h_in[1]/monob))))
        qsr = ((dq+dqer*cskin)*(kappa/(np.log(h_in[2]/zoq) -
                                       psit_26(h_in[2]/monob))))
    else:
        usr = (wind*kappa/(np.log(h_in[0]/zo)-psim_calc(h_in[0]/monob, meth)))
        tsr = ct*wind*(dt+dter*cskin)/usr
        qsr = cq*wind*(dq+dqer*cskin)/usr
    return usr, tsr, qsr
sbiri's avatar
sbiri committed
862
# ---------------------------------------------------------------------