Exemple #1
0
def vgd_read(fileId, ip1=-1, ip2=-1, kind=-1, version=-1):
    """
    Construct a vgrid descriptor from the vgrid record in a RPN standard file.

    Args:
        fileId   (int)  : Opened RPN Std file unit number
        ip1      (int)  : Ip1 of the vgrid record to find, use -1 for any (I)
        ip2      (int)  : Ip2 of the vgrid record to find, use -1 for any (I)
        kind     (int)  : Kind of vertical coor
        version  (int)  : Version of vertical coor
    Returns:
        VGridDescriptor ref
    Raises:
        TypeError
        VGDError

    Examples:
    >>> import os, os.path, sys
    >>> import rpnpy.vgd.all as vgd
    >>> import rpnpy.librmn.all as rmn
    >>> ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip()
    >>> fileName = os.path.join(ATM_MODEL_DFILES,'bcmk_toctoc','2009042700_000')
    >>> fileId = rmn.fstopenall(fileName, rmn.FST_RO)
    >>> try:
    >>>     myvgd = vgd.vgd_read(fileId)
    >>> except:
    >>>     sys.stderr.write("There was a problem reading the VGridDescriptor")
    >>> finally:
    >>>     rmn.fstcloseall(fileId)

    See Also:
        rpnpy.librmn.fstd98.fstopenall
        rpnpy.librmn.fstd98.fstcloseall
        rpnpy.librmn.const.FST_RO
        rpnpy.vgd.const.VGD_KIND_VER
        vgd_new
        vgd_write
        vgd_levels
        vgd_free
    """
    vgd_ptr = _vp.c_vgd_construct()
    ok = _vp.c_vgd_new_read(vgd_ptr, fileId, ip1, ip2, kind, version)
    if ok != _vc.VGD_OK:
        raise VGDError(
            "Problem getting vgd from file (id={0}, ip1={1}, ip2={2}, kind={3}, version={4})".format(
                fileId, ip1, ip2, kind, version
            )
        )
    return vgd_ptr
Exemple #2
0
def vgd_fromlist(vgd_table):
    """
    Build a VGridDescriptor from previously encoded table values.

    Args:
        vgd_table (list): Encoded VGridDescriptor values in a list
                          This is obtained with vgd_tolist
    Returns:
        VGridDescriptor ref
    Raises:
        TypeError
        VGDError

    Examples:
    >>> import sys
    >>> import rpnpy.vgd.all as vgd
    >>> lvls  = (500.,850.,1000.)
    >>> try:
    >>>     myvgd    = vgd.vgd_new_pres(lvls)
    >>>     vgdtable = vgd.vgd_tolist(myvgd)
    >>> except:
    >>>     sys.stderr.write("There was a problem creating/encoding the VGridDescriptor")
    >>>     sys.exit(1)
    >>> ## ...
    >>> try:
    >>>     myvgd2 = vgd.vgd_fromlist(vgdtable)
    >>> except:
    >>>     sys.stderr.write("There was a problem creating the VGridDescriptor in a list")

    See Also:
        vgd_tolist
        vgd_new
        vgd_read
        vgd_levels
        vgd_free
    """
    vgd_ptr = _vp.c_vgd_construct()
    vtbl = vgd_table.ctypes.data_as(_ct.POINTER(_ct.c_double))
    (ni, nj, nk) = vgd_table.shape
    ok = _vp.c_vgd_new_from_table(vgd_ptr, vtbl, ni, nj, nk)
    if ok != _vc.VGD_OK:
        raise VGDError("Problem Creating a new VGD from provided table)")
    return vgd_ptr
Exemple #3
0
def vgd_new(kind, version, hyb, rcoef1=None, rcoef2=None, ptop=None, pref=None, dhm=None, dht=None, ip1=-1, ip2=-1):
    """
    General function to Build an VGridDescriptor initialized with provided info.

    Args:
        kind     (int)  : Kind of vertical coor
        version  (int)  : Version of vertical coor
        hyb      (list) : list of level values
        rcoef1   (float): 1st Coordinate recification R-coefficient
        rcoef1   (float): 2nd Coordinate recification R-coefficient
        ptop     (float): Top level pressure [Pa]
        pref     (float): Reference level pressure [Pa]
        dhm      (float): Height of the Diagnostic Momentum level [m AGL]
        dht      (float): Height of the Diagnostic Thermodynamic level [m AGL]
        ip1      (int)  : Ip1 of the vgrid record
        ip2      (int)  : Ip2 of the vgrid record
    Returns:
        VGridDescriptor ref
    Raises:
        TypeError
        VGDError

    Examples:
    >>> import sys
    >>> import rpnpy.vgd.all as vgd
    >>> (kind, version) = vgd.VGD_KIND_VER['hybmd']
    >>> lvls  = (0.013,   0.027,    0.051,    0.075, \
                 0.101,   0.127,    0.155,    0.185,    0.219, \
                 0.258,   0.302,    0.351,    0.405,    0.460, \
                 0.516,   0.574,    0.631,    0.688,    0.744, \
                 0.796,   0.842,    0.884,    0.922,    0.955, \
                 0.980,   0.995)
    >>> rcoef1 = 0.
    >>> rcoef2 = 1.
    >>> pref   = 100000.
    >>> dhm    = 10.
    >>> dht    = 2.
    >>> try:
    >>>     myvgd = vgd.vgd_new(kind, version, lvls, rcoef1, rcoef2, pref=pref, dhm=dhm, dht=dht)
    >>> except vgd.VGDError:
    >>>     sys.stderr.write("There was a problem creating the VGridDescriptor")
    
    See Also:
        rpnpy.vgd.const.VGD_KIND_VER
        vgd_new_sigm
        vgd_new_pres
        vgd_new_eta
        vgd_new_hyb
        vgd_new_hybs
        vgd_new_hybt
        vgd_new_hybm
        vgd_new_hybmd
        vgd_write
        vgd_levels
        vgd_free
    """
    if isinstance(hyb, (list, tuple)):
        hyb = _np.array(hyb, copy=True, dtype=_np.float32, order="FORTRAN")
    elif isinstance(hyb, _np.ndarray):
        hyb = _np.array(hyb.flatten(), copy=True, dtype=_np.float32, order="FORTRAN")
    else:
        raise TypeError("hyb should be list or ndarray: {0}".format(str(type(ip1list))))
    if not rcoef1 is None:
        rcoef1 = _ct.POINTER(_ct.c_float)(_ct.c_float(rcoef1))
    if not rcoef2 is None:
        rcoef2 = _ct.POINTER(_ct.c_float)(_ct.c_float(rcoef2))
    if not ptop is None:
        ptop = _ct.POINTER(_ct.c_double)(_ct.c_double(ptop))
    if not pref is None:
        pref = _ct.POINTER(_ct.c_double)(_ct.c_double(pref))
    if not dhm is None:
        dhm = _ct.POINTER(_ct.c_float)(_ct.c_float(dhm))
    if not dht is None:
        dht = _ct.POINTER(_ct.c_float)(_ct.c_float(dht))
    vgd_ptr = _vp.c_vgd_construct()
    p_ptop_out = None
    if kind == 5 and version in (4, 5):
        if not ptop is None:
            p_ptop_out = ptop
        else:
            ptop_out = 100.0
            p_ptop_out = _ct.POINTER(_ct.c_double)(_ct.c_double(ptop_out))
    ok = _vp.c_vgd_new_gen(
        vgd_ptr, kind, version, hyb, hyb.size, rcoef1, rcoef2, ptop, pref, p_ptop_out, ip1, ip2, dhm, dht
    )
    if ok != _vc.VGD_OK:
        raise VGDError("Problem building VGD (kind={0}, version={1}): Error={2})".format(kind, version, ok))
    return vgd_ptr