Exemple #1
0
def grib_index_get_double(iid,key):
    """
    @brief Get the distinct values of the key in argument contained in the index. The key must belong to the index. 
    
    This function is used when the type of the key was explicitly defined as double or when the native type of the key is double.
    
    \b Examples: \ref index.py "index.py"
    
    @param indexid   id of an index created from a file. The index must have been created with the key in argument.
    @param key       key for wich the values are returned
    @return tuple with values of key in index
    @exception GribInternalError 
    """
    nval = grib_index_get_size(iid,key)

    a = _internal.new_doubleArray(nval)
    s = _internal.intp()
    s.assign(nval)

    GRIB_CHECK(_internal.grib_c_index_get_double(iid,key,a,s))

    result = []
    for i in range(nval):
        result.append(_internal.doubleArray_getitem(a,i))

    _internal.delete_doubleArray(a)
    _internal.delete_intp(s)

    return tuple(result)
Exemple #2
0
def grib_get_double_array(gribid,key):
    """
    @brief Get the value of the key as a double array. 
    
    If NumPy is enabled, the double array will be stored in a NumPy ndarray.
    Otherwise, Python's native array type will be used.

    @param gribid   id of the grib loaded in memory
    @param key      key name
    @return numpy.ndarray or array
    @exception GribInternalError 
    """
    if with_numpy():
        nval = grib_get_size(gribid,key)
        err,result = _internal.grib_get_double_ndarray(gribid,key,nval)
        GRIB_CHECK(err)
        return result
    else:
        nval = grib_get_size(gribid,key)
        a = _internal.new_doubleArray(nval)
        s = _internal.intp()
        s.assign(nval)

        GRIB_CHECK(_internal.grib_c_get_real8_array(gribid,key,a,s))

        result = array("d")
        for i in range(nval):
            result.append(_internal.doubleArray_getitem(a,i))

        _internal.delete_doubleArray(a)
        _internal.delete_intp(s)

        return result
Exemple #3
0
def grib_set_double_array(gribid,key,inarray):
    """
    @brief Set the value of the key to a double array.

    The input array can be a numpy.ndarray or a python sequence like tuple, list, array, ...

    If NumPy is enabled, the wrapper will internally try to convert the input to a NumPy array
    before extracting its data and length. This is possible as NumPy
    allows the construction of arrays from arbitrary python sequences.

    The elements of the input sequence need to be convertible to a double.

    @param gribid   id of the grib loaded in memory
    @param key      key name
    @param inarray  tuple,list,array,numpy.ndarray 
    @exception GribInternalError 
    """
    if with_numpy():
        GRIB_CHECK(_internal.grib_set_double_ndarray(gribid,key,inarray))
    else:
        nval = len(inarray)
        a = _internal.new_doubleArray(nval)
        s = _internal.intp()
        s.assign(nval)

        for i in range(nval):
            _internal.doubleArray_setitem(a,i,inarray[i])

        GRIB_CHECK(_internal.grib_c_set_real8_array(gribid,key,a,s))

        _internal.delete_doubleArray(a)
        _internal.delete_intp(s)
Exemple #4
0
def grib_get_double_elements(gribid,key,indexes):
    """
    @brief Get as double array the elements of the "key" array whose indexes are listed in the input array.

    @param gribid      id of the grib loaded in memory
    @param key         the key to be searched
    @param indexes     list or tuple of indexes
    @return numpy.ndarray or array
    @exception GribInternalError 

    """
    if with_numpy():
        nidx = len(indexes)
        err,result = _internal.grib_get_double_ndelements(gribid,key,indexes,nidx)
        GRIB_CHECK(err)
        return result
    else:
        nidx = len(indexes)

        pidx = _internal.new_intArray(nidx)
        pval = _internal.new_doubleArray(nidx)
        psize = _internal.intp()
        psize.assign(nidx)

        for i in range(len(indexes)):
            _internal.intArray_setitem(pidx,i,indexes[i])

        err = _internal.grib_c_get_real8_elements(gribid,key,pidx,pval,psize)
        GRIB_CHECK(err)

        result = array("d")
        for i in range(psize.value()):
            result.append(_internal.doubleArray_getitem(pval,i))

        _internal.delete_intArray(pidx)
        _internal.delete_doubleArray(pval)
        _internal.delete_intp(psize)

        return result
Exemple #5
0
def grib_find_nearest(gribid,inlat,inlon,is_lsm = False,npoints = 1):
    """
    @brief Find the nearest grid point or the nearest four grid points to a given latitude/longitude.

    The number of nearest points returned can be controled through the npoints function argument.

    \b Examples: \ref nearest.py "nearest.py"
    
    @param gribid     id of the grib loaded in memory
    @param inlat      latitude of the point
    @param inlon      longitude of the point
    @param is_lsm     True if the nearest land point is required otherwise False.
    @param npoints    1 or 4 nearest grid points
    @return (npoints*(outlat,outlon,value,dist,index))
    @exception GribInternalError 
    """
    if npoints == 1:
        err,outlat,outlon,value,dist,idx = _internal.grib_c_find_nearest_single(gribid,is_lsm,inlat,inlon)
        GRIB_CHECK(err)
        return (Bunch(lat = outlat,lon = outlon,value = value,distance = dist,index = idx),)
    elif npoints == 4:
        poutlat = _internal.new_doubleArray(4)
        poutlon = _internal.new_doubleArray(4)
        pvalues = _internal.new_doubleArray(4)
        pdist = _internal.new_doubleArray(4)
        pidx = _internal.new_intArray(4)

        GRIB_CHECK(_internal.grib_c_find_nearest_four_single(gribid,is_lsm,inlat,inlon,poutlat,poutlon,pvalues,pdist,pidx))

        result = []
        for i in range(4):
            result.append(Bunch( \
                lat = _internal.doubleArray_getitem(poutlat,i), \
                lon = _internal.doubleArray_getitem(poutlon,i), \
                value = _internal.doubleArray_getitem(pvalues,i), \
                distance = _internal.doubleArray_getitem(pdist,i), \
                index = _internal.intArray_getitem(pidx,i), \
            ))

        _internal.delete_doubleArray(poutlat)
        _internal.delete_doubleArray(poutlon)
        _internal.delete_doubleArray(pvalues)
        _internal.delete_doubleArray(pdist)
        _internal.delete_intArray(pidx)

        return tuple(result)
    else:
        raise ValueError("Invalid value for npoints. Expecting 1 or 4.")