flux_subs.py 38.4 KB
Newer Older
sbiri's avatar
sbiri committed
1
import numpy as np
sbiri's avatar
sbiri committed
2
import sys
3
from VaporPressure import VaporPressure
sbiri's avatar
sbiri committed
4 5

CtoK = 273.16  # 273.15
6
""" Conversion factor for $^\circ\,$C to K """
sbiri's avatar
sbiri committed
7

sbiri's avatar
sbiri committed
8
kappa = 0.4  # NOTE: 0.41
sbiri's avatar
sbiri committed
9
""" von Karman's constant """
sbiri's avatar
sbiri committed
10
# ---------------------------------------------------------------------
sbiri's avatar
sbiri committed
11

sbiri's avatar
sbiri committed
12

sbiri's avatar
sbiri committed
13
def cdn_calc(u10n, Ta, Tp, lat, meth="S80"):
sbiri's avatar
sbiri committed
14
    """ Calculates neutral drag coefficient
sbiri's avatar
sbiri committed
15

sbiri's avatar
sbiri committed
16 17 18 19 20 21 22 23
    Parameters
    ----------
    u10n : float
        neutral 10m wind speed (m/s)
    Ta   : float
        air temperature (K)
    Tp   : float
        wave period
24 25
    lat : float
        latitude
sbiri's avatar
sbiri committed
26 27
    meth : str

sbiri's avatar
sbiri committed
28 29 30 31
    Returns
    -------
    cdn : float
    """
32
    cdn = np.zeros(Ta.shape)*np.nan
sbiri's avatar
sbiri committed
33
    if (meth == "S80"):
sbiri's avatar
sbiri committed
34 35
        cdn = np.where(u10n <= 3, (0.61+0.567/u10n)*0.001,
                       (0.61+0.063*u10n)*0.001)
sbiri's avatar
sbiri committed
36
    elif (meth == "LP82"):
sbiri's avatar
sbiri committed
37 38 39
        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
40 41 42 43
    elif (meth == "S88" or meth == "UA" or meth == "ERA5" or meth == "C30" or
          meth == "C35" or meth == "C40"):
        cdn = cdn_from_roughness(u10n, Ta, None, lat, meth)
    elif (meth == "YT96"):
sbiri's avatar
sbiri committed
44
        # for u<3 same as S80
sbiri's avatar
sbiri committed
45 46 47 48
        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
49
    elif (meth == "LY04"):
sbiri's avatar
sbiri committed
50 51
        cdn = np.where(u10n >= 0.5,
                       (0.142+(2.7/u10n)+(u10n/13.09))*0.001, np.nan)
sbiri's avatar
sbiri committed
52
    else:
sbiri's avatar
sbiri committed
53
        print("unknown method cdn: "+meth)
sbiri's avatar
sbiri committed
54
    return cdn
sbiri's avatar
sbiri committed
55 56 57
# ---------------------------------------------------------------------


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

sbiri's avatar
sbiri committed
61 62 63 64 65 66 67 68
    Parameters
    ----------
    u10n : float
        neutral 10m wind speed (m/s)
    Ta   : float
        air temperature (K)
    Tp   : float
        wave period
69 70
    lat : float
        latitude
sbiri's avatar
sbiri committed
71 72
    meth : str

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

sbiri's avatar
sbiri committed
142

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

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

sbiri's avatar
sbiri committed
160 161 162 163 164 165 166
    Returns
    -------
    ctn : float
        neutral heat exchange coefficient
    cqn : float
        neutral moisture exchange coefficient
    """
sbiri's avatar
sbiri committed
167
    if (meth == "S80" or meth == "S88" or meth == "YT96"):
sbiri's avatar
sbiri committed
168
        cqn = np.ones(Ta.shape)*1.20*0.001  # from S88
sbiri's avatar
sbiri committed
169
        ctn = np.ones(Ta.shape)*1.00*0.001
sbiri's avatar
sbiri committed
170
    elif (meth == "LP82"):
sbiri's avatar
sbiri committed
171
        cqn = np.where((zol <= 0) & (u10n > 4) & (u10n < 14), 1.15*0.001,
172
                       1*0.001)
sbiri's avatar
sbiri committed
173 174
        ctn = np.where((zol <= 0) & (u10n > 4) & (u10n < 25), 1.13*0.001,
                       0.66*0.001)
sbiri's avatar
sbiri committed
175 176 177 178 179
    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
180
        # Zeng et al. 1998 (25)
sbiri's avatar
sbiri committed
181 182
        re=usr*zo/visc_air(Ta)
        zoq = zo/np.exp(2.67*np.power(re, 1/4)-2.57)
sbiri's avatar
sbiri committed
183 184 185 186 187
        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
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
    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)
    elif (meth == "ERA5"):
218
        # eq. (3.26) p.38 over sea IFS Documentation cy46r1
sbiri's avatar
sbiri committed
219 220 221 222 223
        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
224
    else:
sbiri's avatar
sbiri committed
225
        print("unknown method ctcqn: "+meth)
sbiri's avatar
sbiri committed
226
    return ctn, cqn
sbiri's avatar
sbiri committed
227 228 229
# ---------------------------------------------------------------------


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

sbiri's avatar
sbiri committed
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
    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
253

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


sbiri's avatar
sbiri committed
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
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
        meth == "UA" or meth == "ERA5" or meth == "C30" or meth == "C35" or
        meth == "C40"):
        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
299
def psim_calc(zol, meth="S80"):
sbiri's avatar
sbiri committed
300
    """ Calculates momentum stability function
sbiri's avatar
sbiri committed
301

sbiri's avatar
sbiri committed
302 303 304 305
    Parameters
    ----------
    zol : float
        height over MO length
sbiri's avatar
sbiri committed
306 307
    meth : str

sbiri's avatar
sbiri committed
308 309 310 311
    Returns
    -------
    psim : float
    """
sbiri's avatar
sbiri committed
312
    if (meth == "ERA5"):
313
        psim = psim_era5(zol)
sbiri's avatar
sbiri committed
314 315
    elif (meth == "C30" or meth == "C35" or meth == "C40"):
        psim = psiu_26(zol, meth)
sbiri's avatar
sbiri committed
316
    else:
317 318
        psim = np.where(zol < 0, psim_conv(zol, meth),
                        psim_stab(zol, meth))
sbiri's avatar
sbiri committed
319
    return psim
sbiri's avatar
sbiri committed
320 321 322
# ---------------------------------------------------------------------


sbiri's avatar
sbiri committed
323
def psit_calc(zol, meth="S80"):
sbiri's avatar
sbiri committed
324
    """ Calculates heat stability function
sbiri's avatar
sbiri committed
325

sbiri's avatar
sbiri committed
326 327 328 329
    Parameters
    ----------
    zol : float
        height over MO length
sbiri's avatar
sbiri committed
330
    meth : str
331
        parameterisation method
sbiri's avatar
sbiri committed
332

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


349
def psi_era5(zol):
sbiri's avatar
sbiri committed
350
    """ Calculates heat stability function for stable conditions
sbiri's avatar
sbiri committed
351
        for method ERA5
sbiri's avatar
sbiri committed
352

sbiri's avatar
sbiri committed
353 354 355 356
    Parameters
    ----------
    zol : float
        height over MO length
sbiri's avatar
sbiri committed
357

sbiri's avatar
sbiri committed
358 359 360 361
    Returns
    -------
    psit : float
    """
362
    # eq (3.22) p. 37 IFS Documentation cy46r1
sbiri's avatar
sbiri committed
363 364
    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
365
    return psit
sbiri's avatar
sbiri committed
366 367
# ---------------------------------------------------------------------

sbiri's avatar
sbiri committed
368 369 370 371 372 373 374 375 376 377 378 379 380 381

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
382 383 384 385 386 387 388 389
    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)
390 391
    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
392 393 394 395
    return psi
# ---------------------------------------------------------------------


396
def psi_conv(zol, meth):
sbiri's avatar
sbiri committed
397
    """ Calculates heat stability function for unstable conditions
sbiri's avatar
sbiri committed
398

sbiri's avatar
sbiri committed
399 400 401 402
    Parameters
    ----------
    zol : float
        height over MO length
403 404
    meth : str
        parameterisation method
sbiri's avatar
sbiri committed
405

sbiri's avatar
sbiri committed
406 407 408 409
    Returns
    -------
    psit : float
    """
410 411
    coeffs = get_stabco(meth)
    alpha, beta = coeffs[0], coeffs[1]
412 413
    xtmp = np.power(1-alpha*zol, beta)
    psit = 2*np.log((1+np.power(xtmp, 2))*0.5)
sbiri's avatar
sbiri committed
414
    return psit
sbiri's avatar
sbiri committed
415 416 417
# ---------------------------------------------------------------------


418
def psi_stab(zol, meth):
sbiri's avatar
sbiri committed
419
    """ Calculates heat stability function for stable conditions
sbiri's avatar
sbiri committed
420

sbiri's avatar
sbiri committed
421 422 423 424
    Parameters
    ----------
    zol : float
        height over MO length
425 426
    meth : str
        parameterisation method
sbiri's avatar
sbiri committed
427

sbiri's avatar
sbiri committed
428 429 430 431
    Returns
    -------
    psit : float
    """
432 433
    coeffs = get_stabco(meth)
    gamma = coeffs[2]
sbiri's avatar
sbiri committed
434
    psit = -gamma*zol
sbiri's avatar
sbiri committed
435
    return psit
sbiri's avatar
sbiri committed
436 437 438
# ---------------------------------------------------------------------


439 440
def psim_era5(zol):
    """ Calculates momentum stability function for method ERA5
sbiri's avatar
sbiri committed
441

sbiri's avatar
sbiri committed
442 443 444 445
    Parameters
    ----------
    zol : float
        height over MO length
sbiri's avatar
sbiri committed
446

sbiri's avatar
sbiri committed
447 448 449 450
    Returns
    -------
    psim : float
    """
451 452 453 454
    # 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
455
    a, b, c, d = 1, 2/3, 5, 0.35
456 457 458
    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
459 460 461 462
    return psim
# ---------------------------------------------------------------------


sbiri's avatar
sbiri committed
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
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
# ---------------------------------------------------------------------


506
def psim_conv(zol, meth):
sbiri's avatar
sbiri committed
507
    """ Calculates momentum stability function for unstable conditions
sbiri's avatar
sbiri committed
508

sbiri's avatar
sbiri committed
509 510 511 512
    Parameters
    ----------
    zol : float
        height over MO length
513 514
    meth : str
        parameterisation method
sbiri's avatar
sbiri committed
515

sbiri's avatar
sbiri committed
516 517 518 519
    Returns
    -------
    psim : float
    """
520 521
    coeffs = get_stabco(meth)
    alpha, beta = coeffs[0], coeffs[1]
522 523
    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
524
            2*np.arctan(xtmp)+np.pi/2)
sbiri's avatar
sbiri committed
525
    return psim
sbiri's avatar
sbiri committed
526 527 528
# ---------------------------------------------------------------------


529
def psim_stab(zol, meth):
sbiri's avatar
sbiri committed
530
    """ Calculates momentum stability function for stable conditions
sbiri's avatar
sbiri committed
531

sbiri's avatar
sbiri committed
532 533 534 535
    Parameters
    ----------
    zol : float
        height over MO length
536 537
    meth : str
        parameterisation method
sbiri's avatar
sbiri committed
538

sbiri's avatar
sbiri committed
539 540 541 542
    Returns
    -------
    psim : float
    """
543 544
    coeffs = get_stabco(meth)
    gamma = coeffs[2]
sbiri's avatar
sbiri committed
545 546 547 548 549
    psim = -gamma*zol
    return psim
# ---------------------------------------------------------------------


sbiri's avatar
sbiri committed
550 551 552
def get_init(spd, T, SST, lat, P, Rl, Rs, cskin, gust, L, tol, meth, qmeth):
    """
    Checks initial input values and sets defaults if needed
sbiri's avatar
sbiri committed
553

sbiri's avatar
sbiri committed
554 555
    Parameters
    ----------
sbiri's avatar
sbiri committed
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
    spd : float
        relative wind speed in m/s (is assumed as magnitude difference
        between wind and surface current vectors)
    T : float
        air temperature in K
    SST : float
        sea surface temperature in K
    lat : float
        latitude (deg), default 45deg
    P : float
        air pressure (hPa), default 1013hPa
    Rl : float
        downward longwave radiation (W/m^2)
    Rs : float
        downward shortwave radiation (W/m^2)
    cskin : int
        0 switch cool skin adjustment off, else 1
        default is 1
    gust : int
        3x1 [x, beta, zi] x=1 to include the effect of gustiness, else 0
        beta gustiness parameter, beta=1 for UA, beta=1.2 for COARE
        zi PBL height (m) 600 for COARE, 1000 for UA and ERA5, 800 default
        default for COARE [1, 1.2, 600]
        default for UA, ERA5 [1, 1, 1000]
        default else [1, 1.2, 800]
    L : int
        Monin-Obukhov length definition options
        0 : default for S80, S88, LP82, YT96 and LY04
        1 : following UA (Zeng et al., 1998), default for UA
        2 : following ERA5 (IFS Documentation cy46r1), default for ERA5
        3 : COARE3.5 (Edson et al., 2013), default for C30, C35 and C40
    tol : float
        4x1 or 7x1 [option, lim1-3 or lim1-6]
        option : 'flux' to set tolerance limits for fluxes only lim1-3
        option : 'ref' to set tolerance limits for height adjustment lim-1-3
        option : 'all' to set tolerance limits for both fluxes and height
                 adjustment lim1-6 ['all', 0.01, 0.01, 5e-05, 0.01, 1, 1]
    meth : str
        "S80","S88","LP82","YT96","UA","LY04","C30","C35","C40","ERA5"
    qmeth : str
        is the saturation evaporation method to use amongst
        "HylandWexler","Hardy","Preining","Wexler","GoffGratch","CIMO",
        "MagnusTetens","Buck","Buck2","WMO","WMO2000","Sonntag","Bolton",
        "IAPWS","MurphyKoop"]
        default is Buck2
sbiri's avatar
sbiri committed
601

sbiri's avatar
sbiri committed
602 603
    Returns
    -------
sbiri's avatar
sbiri committed
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
    lat : float
        latitude
    P : float
        air pressure (hPa)
    Rl : float
        downward longwave radiation (W/m^2)
    Rs : float
        downward shortwave radiation (W/m^2)
    cskin : int
        cool skin adjustment switch
    gust : int
        gustiness switch
    tol : float
        tolerance limits
    L : int
        MO length switch

sbiri's avatar
sbiri committed
621
    """
sbiri's avatar
sbiri committed
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
    if ((type(spd) != np.ndarray) or (type(T) != np.ndarray) or
         (type(SST) != np.ndarray)):
        sys.exit("input type of spd, T and SST should be numpy.ndarray")
    # if input values are nan break
    if meth not in ["S80", "S88", "LP82", "YT96", "UA", "LY04", "C30", "C35",
                    "C40","ERA5"]:
        sys.exit("unknown method")
    if qmeth not in ["HylandWexler", "Hardy", "Preining", "Wexler", "CIMO",
                      "GoffGratch", "MagnusTetens", "Buck", "Buck2", "WMO",
                      "WMO2000", "Sonntag", "Bolton", "IAPWS", "MurphyKoop"]:
        sys.exit("unknown q-method")
    if (np.all(np.isnan(spd)) or np.all(np.isnan(T)) or np.all(np.isnan(SST))):
        sys.exit("input wind, T or SST is empty")
    if (np.all(lat == None)):  # set latitude to 45deg if empty
        lat = 45*np.ones(spd.shape)
    elif ((np.all(lat != None)) and (np.size(lat) == 1)):
        lat = np.ones(spd.shape)*np.copy(lat)
    if ((np.all(P == None)) or np.all(np.isnan(P))):
        P = np.ones(spd.shape)*1013
    elif (((np.all(P != None)) or np.all(~np.isnan(P))) and np.size(P) == 1):
        P = np.ones(spd.shape)*np.copy(P)
    if (np.all(Rl == None) or np.all(np.isnan(Rl))):
        Rl = np.ones(spd.shape)*370    # set to default for COARE3.5
    if (np.all(Rs == None) or np.all(np.isnan(Rs))):
        Rs = np.ones(spd.shape)*150  # set to default for COARE3.5
    if ((cskin == None) and (meth == "S80" or meth == "S88" or meth == "LP82"
                              or meth == "YT96")):
        cskin = 0
    elif ((cskin == None) and (meth == "UA" or meth == "LY04" or meth == "C30"
                                or meth == "C35" or meth == "C40"
                                or meth == "ERA5")):
        cskin = 1
    if ((gust == None) and (meth == "C30" or meth == "C35" or meth == "C40")):
        gust = [1, 1.2, 600]
    elif ((gust == None) and (meth == "UA" or meth == "ERA5")):
        gust = [1, 1, 1000]
    elif (gust == None):
        gust = [1, 1.2, 800]
    elif (np.size(gust) < 3):
        sys.exit("gust input must be a 3x1 array")
    if (L not in [None, 0, 1, 2, 3]):
        sys.exit("L input must be either None, 0, 1, 2 or 3")
    if ((L == None) and (meth == "S80" or meth == "S88" or meth == "LP82"
                              or meth == "YT96" or meth == "LY04")):
        L = 0
    elif ((L == None) and (meth == "UA")):
        L = 1
    elif ((L == None) and (meth == "ERA5")):
        L = 2
    elif ((L == None) and (meth == "C30" or meth == "C35" or meth == "C40")):
        L = 3
    if (tol == None):
        tol = ['flux', 0.01, 1, 1]
    elif (tol[0] not in ['flux', 'ref', 'all']):
        sys.exit("unknown tolerance input")
    return lat, P, Rl, Rs, cskin, gust, tol, L
sbiri's avatar
sbiri committed
678 679 680
# ---------------------------------------------------------------------


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

sbiri's avatar
sbiri committed
684 685 686 687 688 689 690 691 692 693 694 695
    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
696 697
    Rnl : float
        upwelling IR radiation (W/m^2)
sbiri's avatar
sbiri committed
698 699 700 701
    cp : float
       specific heat of air at constant pressure
    lv : float
       latent heat of vaporization
sbiri's avatar
sbiri committed
702 703
    tkt : float
       cool skin thickness
sbiri's avatar
sbiri committed
704 705 706 707 708 709 710 711
    usr : float
       friction velocity
    tsr : float
       star temperature
    qsr : float
       star humidity
    lat : float
       latitude
sbiri's avatar
sbiri committed
712

sbiri's avatar
sbiri committed
713 714 715
    Returns
    -------
    dter : float
sbiri's avatar
sbiri committed
716 717
    dqer : float

sbiri's avatar
sbiri committed
718
    """
sbiri's avatar
sbiri committed
719 720
    # coded following Saunders (1967) with lambda = 6
    g = gc(lat, None)
sbiri's avatar
sbiri committed
721 722
    if (np.nanmin(sst) > 200):  # if sst in Kelvin convert to Celsius
        sst = sst-CtoK
sbiri's avatar
sbiri committed
723 724 725 726
    # ************  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
727
    Al = 2.1e-5*np.power(sst+3.2, 0.79)
sbiri's avatar
sbiri committed
728
    be = 0.026
sbiri's avatar
sbiri committed
729 730
    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
731 732 733 734 735 736 737
    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
738
    xlamx = 6*np.ones(sst.shape)
sbiri's avatar
sbiri committed
739
    xlamx = np.where(alq > 0, 6/(1+(bigc*alq/usr**4)**0.75)**0.333, 6)
sbiri's avatar
sbiri committed
740 741 742
    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
743 744
    dter = qcol*tkt/tcw
    dqer = wetc*dter
sbiri's avatar
sbiri committed
745
    return dter, dqer, tkt
sbiri's avatar
sbiri committed
746 747 748 749
# ---------------------------------------------------------------------


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

sbiri's avatar
sbiri committed
752 753 754 755 756 757 758 759 760 761 762 763 764 765
    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
766

sbiri's avatar
sbiri committed
767 768 769 770
    Returns
    -------
    ug : float
    """
771
    if (np.nanmax(Ta) < 200):  # convert to K if in Celsius
sbiri's avatar
sbiri committed
772 773
        Ta = Ta+273.16
    g = gc(lat, None)
sbiri's avatar
sbiri committed
774
    Bf = (-g/Ta)*usr*tsrv
sbiri's avatar
sbiri committed
775 776 777 778 779 780
    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
781 782
def get_L(L, lat, usr, tsr, qsr, t10n, tv10n, qair, h_in, T, Ta, th, tv, sst,
          dt, dq, wind, monob, meth):
sbiri's avatar
sbiri committed
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833
    """
    calculates Monin-Obukhov length and virtual star temperature

    Parameters
    ----------
    L : int
        Monin-Obukhov length definition options
           0 : default for S80, S88, LP82, YT96 and LY04
           1 : following UA (Zeng et al., 1998), default for UA
           2 : following ERA5 (IFS Documentation cy46r1), default for ERA5
           3 : COARE3.5 (Edson et al., 2013), default for C30, C35 and C40
    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
        bulk parameterisation method option: "S80", "S88", "LP82", "YT96", "UA",
        "LY04", "C30", "C35", "C40", "ERA5"

    Returns
    -------
834 835 836 837
    tsrv : float
        virtual star temperature (K)
    monob : float
        M-O length (m)
sbiri's avatar
sbiri committed
838 839

    """
sbiri's avatar
sbiri committed
840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866
    g = gc(lat)
    if (L == 0):
        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)
    elif (L == 1):
        tsrv = tsr*(1.+0.61*qair)+0.61*th*qsr
        monob = ((tv*np.power(usr, 2))/(kappa*g*tsrv))
    elif (L == 2):
        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))))
        monob = h_in[0]/zol
    elif (L == 3):
        tsrv = tsr+0.61*(T+CtoK)*qsr
        zol = (kappa*g*h_in[0]/(T+CtoK)*(tsr+0.61*(T+CtoK)*qsr) /
               np.power(usr, 2))
        monob = h_in[0]/zol
    return tsrv, monob
sbiri's avatar
sbiri committed
867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
#------------------------------------------------------------------------------


def get_hum(hum, T, sst, P, qmeth):
    """
    Get specific humidity output

    Parameters
    ----------
    hum : array
        humidity input switch 2x1 [x, values] default is relative humidity
            x='rh' : relative humidity in %
            x='q' : specific humidity (g/kg)
            x='Td' : dew point temperature (K)
    T : float
        air temperature in K
    sst : float
        sea surface temperature in K
    P : float
        air pressure at sea level in hPa
    qmeth : str
        method to calculate specific humidity from vapor pressure

    Returns
    -------
    qair : float
        specific humidity of air
    qsea : float
        specific humidity over sea surface

    """
    if (hum == None):
        RH = np.ones(sst.shape)*80
        qsea = qsat_sea(sst, P, qmeth)/1000     # surface water q (g/kg)
        qair = qsat_air(T, P, RH, qmeth)/1000   # q of air (g/kg)
    elif (hum[0] not in ['rh', 'q', 'Td']):
sbiri's avatar
sbiri committed
903
        sys.exit("unknown humidity input")
sbiri's avatar
sbiri committed
904 905 906 907
        qair, qsea = np.nan, np.nan
    elif (hum[0] == 'rh'):
        RH = hum[1]
        if (np.all(RH < 1)):
sbiri's avatar
sbiri committed
908
            sys.exit("input relative humidity units should be \%")
sbiri's avatar
sbiri committed
909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925
            qair, qsea = np.nan, np.nan
        qsea = qsat_sea(sst, P, qmeth)/1000    # surface water q (g/kg)
        qair = qsat_air(T, P, RH, qmeth)/1000  # q of air (g/kg)
    elif (hum[0] == 'q'):
        qair = hum[1]
        qsea = qsat_sea(sst, P, qmeth)/1000  # surface water q (g/kg)
    elif (hum[0] == 'Td'):
        Td = hum[1] # dew point temperature (K)
        Td = np.where(Td < 200, np.copy(Td)+CtoK, np.copy(Td))
        T = np.where(T < 200, np.copy(T)+CtoK, np.copy(T))
        esd = 611.21*np.exp(17.502*((Td-273.16)/(Td-32.19)))
        es = 611.21*np.exp(17.502*((T-273.16)/(T-32.19)))
        RH = 100*esd/es
        qair = qsat_air(T, P, RH, qmeth)/1000  # q of air (g/kg)
        qsea = qsat_sea(sst, P, qmeth)/1000    # surface water q (g/kg)
    return qair, qsea
#-------------------------------------------------------------------------
sbiri's avatar
sbiri committed
926 927


928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
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
        cskin q adjustment (q/kg)
    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"):
        usr = np.where(h_in[0]/monob < -1.574, kappa*wind /
                       (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))),
                       np.where((h_in[0]/monob > -1.574) & (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 > 0) &
                                (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))))
                                # 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))),
                       np.where((h_in[1]/monob > -0.465) & (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 > 0) & (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))))
                                # 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))),
                       np.where((h_in[2]/monob > -0.465) & (h_in[2]/monob < 0),
                                kappa*(dq+dqer*cskin)/(np.log(h_in[1]/zot) -
                                psit_calc(h_in[2]/monob, meth) +
                                psit_calc(zoq/monob, meth)),
                                np.where((h_in[2]/monob > 0) &
                                         (h_in[2]/monob<1),
                                         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
#------------------------------------------------------------------------------


1039
def get_heights(h, dim_len):
sbiri's avatar
sbiri committed
1040
    """ Reads input heights for velocity, temperature and humidity
sbiri's avatar
sbiri committed
1041

sbiri's avatar
sbiri committed
1042 1043 1044 1045
    Parameters
    ----------
    h : float
        input heights (m)
1046 1047
    dim_len : int
        length dimension
sbiri's avatar
sbiri committed
1048

sbiri's avatar
sbiri committed
1049 1050 1051 1052
    Returns
    -------
    hh : array
    """
1053
    hh = np.zeros((3, dim_len))
sbiri's avatar
sbiri committed
1054
    if (type(h) == float or type(h) == int):
1055
        hh[0, :], hh[1, :], hh[2, :] = h, h, h
1056
    elif (len(h) == 2 and np.ndim(h) == 1):
1057
        hh[0, :], hh[1, :], hh[2, :] = h[0], h[1], h[1]
1058
    elif (len(h) == 3 and np.ndim(h) == 1):
1059
        hh[0, :], hh[1, :], hh[2, :] = h[0], h[1], h[2]
1060
    elif (len(h) == 1 and np.ndim(h) == 2):
1061 1062
        hh = np.zeros((3, h.shape[1]))
        hh[0, :], hh[1, :], hh[2, :] = h[0, :], h[0, :], h[0, :]
1063
    elif (len(h) == 2 and np.ndim(h) == 2):
1064 1065
        hh = np.zeros((3, h.shape[1]))
        hh[0, :], hh[1, :], hh[2, :] = h[0, :], h[1, :], h[1, :]
1066
    elif (len(h) == 3 and np.ndim(h) == 2):
1067 1068
        hh = np.zeros((3, h.shape[1]))
        hh = np.copy(h)
sbiri's avatar
sbiri committed
1069
    return hh
sbiri's avatar
sbiri committed
1070 1071 1072
# ---------------------------------------------------------------------


1073 1074
def qsat_sea(T, P, qmeth):
    """ Computes surface saturation specific humidity (g/kg)
sbiri's avatar
sbiri committed
1075

sbiri's avatar
sbiri committed
1076 1077 1078 1079 1080 1081
    Parameters
    ----------
    T : float
        temperature ($^\\circ$\\,C)
    P : float
        pressure (mb)
1082 1083
    qmeth : str
        method to calculate vapor pressure
sbiri's avatar
sbiri committed
1084

sbiri's avatar
sbiri committed
1085 1086 1087
    Returns
    -------
    qs : float
sbiri's avatar
sbiri committed
1088 1089 1090 1091
    """
    T = np.asarray(T)
    if (np.nanmin(T) > 200):  # if Ta in Kelvin convert to Celsius
        T = T-CtoK
1092
    ex = VaporPressure(T, P, 'liquid', qmeth)
sbiri's avatar
sbiri committed
1093 1094 1095 1096 1097 1098
    es = 0.98*ex  # reduction at sea surface
    qs = 622*es/(P-0.378*es)
    return qs
# ------------------------------------------------------------------------------


1099
def qsat_air(T, P, rh, qmeth):
sbiri's avatar
sbiri committed
1100
    """ Computes saturation specific humidity (g/kg) as in C35
sbiri's avatar
sbiri committed
1101

sbiri's avatar
sbiri committed
1102 1103 1104 1105 1106 1107
    Parameters
    ----------
    T : float
        temperature ($^\circ$\,C)
    P : float
        pressure (mb)
1108 1109 1110 1111
    rh : float
       relative humidity (%)
    qmeth : str
        method to calculate vapor pressure
sbiri's avatar
sbiri committed
1112

sbiri's avatar
sbiri committed
1113 1114 1115 1116
    Returns
    -------
    q : float
    em : float
sbiri's avatar
sbiri committed
1117 1118 1119 1120
    """
    T = np.asarray(T)
    if (np.nanmin(T) > 200):  # if Ta in Kelvin convert to Celsius
        T = T-CtoK
1121
    es = VaporPressure(T, P, 'liquid', qmeth)
sbiri's avatar
sbiri committed
1122 1123
    em = 0.01*rh*es
    q = 622*em/(P-0.378*em)
1124
    return q
sbiri's avatar
sbiri committed
1125 1126 1127 1128
# ---------------------------------------------------------------------


def gc(lat, lon=None):
sbiri's avatar
sbiri committed
1129
    """ Computes gravity relative to latitude
sbiri's avatar
sbiri committed
1130

sbiri's avatar
sbiri committed
1131 1132 1133 1134 1135 1136
    Parameters
    ----------
    lat : float
        latitude ($^\circ$)
    lon : float
        longitude ($^\circ$, optional)
sbiri's avatar
sbiri committed
1137

sbiri's avatar
sbiri committed
1138 1139 1140 1141
    Returns
    -------
    gc : float
        gravity constant (m/s^2)
sbiri's avatar
sbiri committed
1142 1143 1144 1145 1146 1147 1148
    """
    gamma = 9.7803267715
    c1 = 0.0052790414
    c2 = 0.0000232718
    c3 = 0.0000001262
    c4 = 0.0000000007
    if lon is not None:
sbiri's avatar
sbiri committed
1149
        lon_m, lat_m = np.meshgrid(lon, lat)
sbiri's avatar
sbiri committed
1150 1151 1152 1153
    else:
        lat_m = lat
    phi = lat_m*np.pi/180.
    xx = np.sin(phi)
sbiri's avatar
sbiri committed
1154 1155
    gc = (gamma*(1+c1*np.power(xx, 2)+c2*np.power(xx, 4)+c3*np.power(xx, 6) +
          c4*np.power(xx, 8)))
sbiri's avatar
sbiri committed
1156
    return gc
sbiri's avatar
sbiri committed
1157 1158 1159
# ---------------------------------------------------------------------


sbiri's avatar
sbiri committed
1160
def visc_air(T):
sbiri's avatar
sbiri committed
1161
    """ Computes the kinematic viscosity of dry air as a function of air temp.
sbiri's avatar
sbiri committed
1162
    following Andreas (1989), CRREL Report 89-11.
sbiri's avatar
sbiri committed
1163

sbiri's avatar
sbiri committed
1164 1165 1166 1167
    Parameters
    ----------
    Ta : float
        air temperature ($^\circ$\,C)
sbiri's avatar
sbiri committed
1168

sbiri's avatar
sbiri committed
1169 1170 1171 1172
    Returns
    -------
    visa : float
        kinematic viscosity (m^2/s)
sbiri's avatar
sbiri committed
1173
    """
sbiri's avatar
sbiri committed
1174 1175 1176 1177 1178
    T = np.asarray(T)
    if (np.nanmin(T) > 200):  # if Ta in Kelvin convert to Celsius
        T = T-273.16
    visa = 1.326e-5*(1+6.542e-3*T+8.301e-6*np.power(T, 2) -
                     4.84e-9*np.power(T, 3))
sbiri's avatar
sbiri committed
1179
    return visa