get_init.py 6.08 KB
Newer Older
1 2 3
import numpy as np
import sys

sbiri's avatar
sbiri committed
4
def get_init(spd, T, SST, lat, hum, P, Rl, Rs, cskin, skin, wl, gust, L, tol,
sbiri's avatar
sbiri committed
5
             n, meth, qmeth):
6 7 8 9 10 11 12 13 14 15 16 17 18 19
    """
    Checks initial input values and sets defaults if needed

    Parameters
    ----------
    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
sbiri's avatar
sbiri committed
20
    hum : float
21
        relative humidity, if None is set to 80%
22 23 24 25 26 27 28
    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
29 30 31 32 33
        cool skin correction (0: off, 1: on)
    skin : str
        cool skin adjustment method "C35" (default), "ecmwf" or "Beljaars"
    wl : int
        warm layer correction (0: off default, 1: on)
34 35 36
    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
37
        zi PBL height (m) 600 for COARE, 1000 for UA and ecmwf, 800 default
38
        default for COARE [1, 1.2, 600]
39
        default for UA, ecmwf [1, 1, 1000]
40 41 42
        default else [1, 1.2, 800]
    L : int
        Monin-Obukhov length definition options
sbiri's avatar
sbiri committed
43
    tol : float
44 45 46 47
        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
48
                 adjustment lim1-6 ['all', 0.01, 0.01, 5e-05, 1e-3, 0.1, 0.1]
sbiri's avatar
sbiri committed
49 50
    n : int
        number of iterations
51
    meth : str
sbiri's avatar
sbiri committed
52
        "S80","S88","LP82","YT96","UA","LY04","C30","C35","ecmwf",
53
        "Beljaars"
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
    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

    Returns
    -------
    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
73 74 75 76
    skin : str
        cool skin adjustment method
    wl : int
        warm layer correction switch
77 78 79 80 81 82
    gust : int
        gustiness switch
    tol : float
        tolerance limits
    L : int
        MO length switch
sbiri's avatar
sbiri committed
83 84
    n : int
        number of iterations
85
    """
86
    # check if input is correct (type, size, value) and set defaults
87 88 89
    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")
90 91 92 93
    elif ((spd.dtype not in ['float64', 'float32']) or
          (T.dtype not in ['float64', 'float32']) or
          (SST.dtype not in ['float64', 'float32'])):
        sys.exit("input dtype of spd, T and SST should be float")
94 95
    # if input values are nan break
    if meth not in ["S80", "S88", "LP82", "YT96", "UA", "LY04", "C30", "C35",
sbiri's avatar
sbiri committed
96
                    "ecmwf", "Beljaars"]:
97
        sys.exit("unknown method")
98 99 100
    if qmeth not in ["HylandWexler", "Hardy", "Preining", "Wexler",
                     "GoffGratch", "WMO", "MagnusTetens", "Buck", "Buck2",
                     "WMO2018", "Sonntag", "Bolton", "IAPWS", "MurphyKoop"]:
101 102 103 104 105 106 107
        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)
108 109 110 111 112
    if (hum == None):
        RH = np.ones(SST.shape)*80
        hum = ['rh', RH]
    else:
        hum = hum
113 114 115 116 117 118 119 120 121
    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"
122 123
                             or meth == "YT96" or meth == "UA" or
                             meth == "LY04")):
124
        cskin = 0
sbiri's avatar
sbiri committed
125
    elif ((cskin == None) and (meth == "C30" or meth == "C35"
126
                               or meth == "ecmwf" or meth == "Beljaars")):
127
        cskin = 1
sbiri's avatar
sbiri committed
128
        if ((skin == None) and (meth == "C30" or meth == "C35")):
129 130 131 132 133 134 135
            skin = "C35"
        elif ((skin == None) and (meth == "ecmwf")):
            skin = "ecmwf"
        elif ((skin == None) and (meth == "Beljaars")):
            skin = "Beljaars"
    if (wl == None):
        wl = 0
sbiri's avatar
sbiri committed
136
    if (np.all(gust == None) and (meth == "C30" or meth == "C35")):
137
        gust = [1, 1.2, 600]
138
    elif (np.all(gust == None) and (meth == "UA" or meth == "ecmwf" or
139
                                    meth == "Beljaars")):
140
        gust = [1, 1, 1000]
sbiri's avatar
sbiri committed
141
    elif np.all(gust == None):
142
        gust = [1, 1.2, 800]
143 144
    elif (np.all(gust == None) and (meth == "YT96")):
        gust = [0, 0, 0]
145 146
    elif ((np.size(gust) < 3) and (gust == 0)):
        gust = [0, 0, 0]
147 148
    elif (np.size(gust) < 3):
        sys.exit("gust input must be a 3x1 array")
149
    if (L not in [None, "S80", "ecmwf"]):
150
        sys.exit("L input must be either None, S80 or ecmwf")
sbiri's avatar
sbiri committed
151
    if (L == None):
152
        L = "ecmwf"
153
    if (tol == None):
sbiri's avatar
sbiri committed
154
        tol = ['all', 0.01, 0.01, 1e-05, 1e-3, 0.1, 0.1]
155 156
    elif (tol[0] not in ['flux', 'ref', 'all']):
        sys.exit("unknown tolerance input")
sbiri's avatar
sbiri committed
157 158 159 160
    if (n < 5):
        n = 5
        print("Number of iterations should not be less than 5; n is set to 5")
    return lat, hum, P, Rl, Rs, cskin, skin, wl, gust, tol, L, n