Exemple #1
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.")
Exemple #2
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