GRIB-790 PEP8ify Python interface

This commit is contained in:
Shahram Najm 2015-09-24 14:38:42 +01:00
parent 11c8bcb13d
commit 77e108821e
1 changed files with 454 additions and 372 deletions

View File

@ -57,6 +57,7 @@ CODES_PRODUCT_GTS=4
# environment variable is defined # environment variable is defined
no_type_checks = os.environ.get('GRIB_API_PYTHON_NO_TYPE_CHECKS') is not None no_type_checks = os.environ.get('GRIB_API_PYTHON_NO_TYPE_CHECKS') is not None
# Function-arguments type-checking decorator # Function-arguments type-checking decorator
# inspired from http://code.activestate.com/recipes/454322-type-checking-decorator/ # inspired from http://code.activestate.com/recipes/454322-type-checking-decorator/
# modified to support multiple allowed types and all types in the same decorator call # modified to support multiple allowed types and all types in the same decorator call
@ -66,6 +67,7 @@ def require(**_params_):
def check_types(_func_, _params_=_params_): def check_types(_func_, _params_=_params_):
if no_type_checks: if no_type_checks:
return _func_ return _func_
@wraps(_func_) @wraps(_func_)
# The wrapper function. Replaces the target function and receives its args # The wrapper function. Replaces the target function and receives its args
def modified(*args, **kw): def modified(*args, **kw):
@ -74,7 +76,7 @@ def require(**_params_):
kw.update(zip(arg_names, args)) kw.update(zip(arg_names, args))
for name, allowed_types in _params_.iteritems(): for name, allowed_types in _params_.iteritems():
param = kw[name] param = kw[name]
if type(allowed_types) == types.TypeType: if isinstance(allowed_types, types.TypeType):
allowed_types = (allowed_types,) allowed_types = (allowed_types,)
assert any([isinstance(param, type1) for type1 in allowed_types]), \ assert any([isinstance(param, type1) for type1 in allowed_types]), \
"Parameter '%s' should be of type %s" % (name, " or ".join([t.__name__ for t in allowed_types])) "Parameter '%s' should be of type %s" % (name, " or ".join([t.__name__ for t in allowed_types]))
@ -82,6 +84,7 @@ def require(**_params_):
return modified return modified
return check_types return check_types
class GribInternalError(Exception): class GribInternalError(Exception):
""" """
@brief Wrap errors coming from the C API in a Python exception object. @brief Wrap errors coming from the C API in a Python exception object.
@ -94,9 +97,11 @@ class GribInternalError(Exception):
assert(err == 0) assert(err == 0)
else: else:
self.msg = value self.msg = value
def __str__(self): def __str__(self):
return self.msg return self.msg
class Bunch(dict): class Bunch(dict):
""" """
The collector of a bunch of named stuff :). The collector of a bunch of named stuff :).
@ -127,6 +132,7 @@ class Bunch(dict):
in self.__dict__.items()] in self.__dict__.items()]
return '\n'.join(state) return '\n'.join(state)
def with_numpy(): def with_numpy():
""" """
@brief Is numpy enabled? @brief Is numpy enabled?
@ -141,6 +147,7 @@ def with_numpy():
return numpy return numpy
@require(errid=int) @require(errid=int)
def GRIB_CHECK(errid): def GRIB_CHECK(errid):
""" """
@ -153,6 +160,7 @@ def GRIB_CHECK(errid):
if errid: if errid:
raise GribInternalError(errid) raise GribInternalError(errid)
@require(fileobj=file) @require(fileobj=file)
def gts_new_from_file(fileobj, headers_only=False): def gts_new_from_file(fileobj, headers_only=False):
""" """
@ -284,6 +292,7 @@ def bufr_new_from_file(fileobj, headers_only = False):
else: else:
return gribid return gribid
@require(fileobj=file) @require(fileobj=file)
def grib_new_from_file(fileobj, headers_only=False): def grib_new_from_file(fileobj, headers_only=False):
""" """
@ -315,6 +324,7 @@ def grib_new_from_file(fileobj, headers_only = False):
else: else:
return gribid return gribid
@require(fileobj=file) @require(fileobj=file)
def grib_count_in_file(fileobj): def grib_count_in_file(fileobj):
""" """
@ -330,6 +340,7 @@ def grib_count_in_file(fileobj):
GRIB_CHECK(err) GRIB_CHECK(err)
return num return num
def grib_multi_support_on(): def grib_multi_support_on():
""" """
@brief Turn on the support for multiple fields in a single message. @brief Turn on the support for multiple fields in a single message.
@ -338,6 +349,7 @@ def grib_multi_support_on():
""" """
_internal.grib_c_multi_support_on() _internal.grib_c_multi_support_on()
def grib_multi_support_off(): def grib_multi_support_off():
""" """
@brief Turn off the support for multiple fields in a single message. @brief Turn off the support for multiple fields in a single message.
@ -346,6 +358,7 @@ def grib_multi_support_off():
""" """
_internal.grib_c_multi_support_off() _internal.grib_c_multi_support_off()
@require(gribid=int) @require(gribid=int)
def grib_release(gribid): def grib_release(gribid):
""" """
@ -358,6 +371,7 @@ def grib_release(gribid):
""" """
GRIB_CHECK(_internal.grib_c_release(gribid)) GRIB_CHECK(_internal.grib_c_release(gribid))
@require(gribid=int, key=str) @require(gribid=int, key=str)
def grib_get_string(gribid, key): def grib_get_string(gribid, key):
""" """
@ -378,6 +392,7 @@ def grib_get_string(gribid, key):
return value return value
@require(gribid=int, key=str, value=str) @require(gribid=int, key=str, value=str)
def grib_set_string(gribid, key, value): def grib_set_string(gribid, key, value):
""" """
@ -390,6 +405,7 @@ def grib_set_string(gribid, key, value):
""" """
GRIB_CHECK(_internal.grib_c_set_string(gribid, key, value, len(value))) GRIB_CHECK(_internal.grib_c_set_string(gribid, key, value, len(value)))
def grib_gribex_mode_on(): def grib_gribex_mode_on():
""" """
@brief Turn on the compatibility mode with gribex. @brief Turn on the compatibility mode with gribex.
@ -398,6 +414,7 @@ def grib_gribex_mode_on():
""" """
_internal.grib_c_gribex_mode_on() _internal.grib_c_gribex_mode_on()
def grib_gribex_mode_off(): def grib_gribex_mode_off():
""" """
@brief Turn off the compatibility mode with gribex. @brief Turn off the compatibility mode with gribex.
@ -406,6 +423,7 @@ def grib_gribex_mode_off():
""" """
_internal.grib_c_gribex_mode_off() _internal.grib_c_gribex_mode_off()
@require(gribid=int, fileobj=file) @require(gribid=int, fileobj=file)
def grib_write(gribid, fileobj): def grib_write(gribid, fileobj):
""" """
@ -419,6 +437,7 @@ def grib_write(gribid, fileobj):
""" """
GRIB_CHECK(_internal.grib_c_write(gribid, fileobj)) GRIB_CHECK(_internal.grib_c_write(gribid, fileobj))
@require(multigribid=int, fileobj=file) @require(multigribid=int, fileobj=file)
def grib_multi_write(multigribid, fileobj): def grib_multi_write(multigribid, fileobj):
""" """
@ -432,6 +451,7 @@ def grib_multi_write(multigribid, fileobj):
""" """
GRIB_CHECK(_internal.grib_c_multi_write(multigribid, fileobj)) GRIB_CHECK(_internal.grib_c_multi_write(multigribid, fileobj))
@require(ingribid=int, startsection=int, multigribid=int) @require(ingribid=int, startsection=int, multigribid=int)
def grib_multi_append(ingribid, startsection, multigribid): def grib_multi_append(ingribid, startsection, multigribid):
""" """
@ -450,6 +470,7 @@ def grib_multi_append(ingribid, startsection, multigribid):
""" """
GRIB_CHECK(_internal.grib_c_multi_append(ingribid, startsection, multigribid)) GRIB_CHECK(_internal.grib_c_multi_append(ingribid, startsection, multigribid))
@require(gribid=int, key=str) @require(gribid=int, key=str)
def grib_get_size(gribid, key): def grib_get_size(gribid, key):
""" """
@ -465,6 +486,7 @@ def grib_get_size(gribid,key):
GRIB_CHECK(err) GRIB_CHECK(err)
return result return result
@require(gribid=int, key=str) @require(gribid=int, key=str)
def grib_get_string_length(gribid, key): def grib_get_string_length(gribid, key):
""" """
@ -478,6 +500,7 @@ def grib_get_string_length(gribid,key):
GRIB_CHECK(err) GRIB_CHECK(err)
return result return result
@require(iterid=int) @require(iterid=int)
def grib_skip_computed(iterid): def grib_skip_computed(iterid):
""" """
@ -493,6 +516,7 @@ def grib_skip_computed(iterid):
""" """
GRIB_CHECK(_internal.grib_c_skip_computed(iterid)) GRIB_CHECK(_internal.grib_c_skip_computed(iterid))
@require(iterid=int) @require(iterid=int)
def grib_skip_coded(iterid): def grib_skip_coded(iterid):
""" """
@ -507,6 +531,7 @@ def grib_skip_coded(iterid):
""" """
GRIB_CHECK(_internal.grib_c_skip_coded(iterid)) GRIB_CHECK(_internal.grib_c_skip_coded(iterid))
@require(iterid=int) @require(iterid=int)
def grib_skip_edition_specific(iterid): def grib_skip_edition_specific(iterid):
""" """
@ -519,6 +544,7 @@ def grib_skip_edition_specific(iterid):
""" """
GRIB_CHECK(_internal.grib_c_skip_edition_specific(iterid)) GRIB_CHECK(_internal.grib_c_skip_edition_specific(iterid))
@require(iterid=int) @require(iterid=int)
def grib_skip_duplicates(iterid): def grib_skip_duplicates(iterid):
""" """
@ -531,6 +557,7 @@ def grib_skip_duplicates(iterid):
""" """
GRIB_CHECK(_internal.grib_c_skip_duplicates(iterid)) GRIB_CHECK(_internal.grib_c_skip_duplicates(iterid))
@require(iterid=int) @require(iterid=int)
def grib_skip_read_only(iterid): def grib_skip_read_only(iterid):
""" """
@ -545,6 +572,7 @@ def grib_skip_read_only(iterid):
""" """
GRIB_CHECK(_internal.grib_c_skip_read_only(iterid)) GRIB_CHECK(_internal.grib_c_skip_read_only(iterid))
@require(iterid=int) @require(iterid=int)
def grib_skip_function(iterid): def grib_skip_function(iterid):
""" """
@ -557,6 +585,7 @@ def grib_skip_function(iterid):
""" """
GRIB_CHECK(_internal.grib_c_skip_function(iterid)) GRIB_CHECK(_internal.grib_c_skip_function(iterid))
@require(gribid=int, mode=int) @require(gribid=int, mode=int)
def grib_iterator_new(gribid, mode): def grib_iterator_new(gribid, mode):
""" """
@ -575,6 +604,7 @@ def grib_iterator_new(gribid,mode):
GRIB_CHECK(err) GRIB_CHECK(err)
return iterid return iterid
@require(iterid=int) @require(iterid=int)
def grib_iterator_delete(iterid): def grib_iterator_delete(iterid):
""" """
@ -587,6 +617,7 @@ def grib_iterator_delete(iterid):
""" """
GRIB_CHECK(_internal.grib_c_iterator_delete(iterid)) GRIB_CHECK(_internal.grib_c_iterator_delete(iterid))
@require(iterid=int) @require(iterid=int)
def grib_iterator_next(iterid): def grib_iterator_next(iterid):
""" """
@ -606,6 +637,7 @@ def grib_iterator_next(iterid):
else: else:
return (lat, lon, value) return (lat, lon, value)
@require(gribid=int) @require(gribid=int)
def grib_keys_iterator_new(gribid, namespace=None): def grib_keys_iterator_new(gribid, namespace=None):
""" """
@ -633,6 +665,7 @@ def grib_keys_iterator_new(gribid,namespace=None):
GRIB_CHECK(err) GRIB_CHECK(err)
return iterid return iterid
@require(iterid=int) @require(iterid=int)
def grib_keys_iterator_next(iterid): def grib_keys_iterator_next(iterid):
""" """
@ -648,6 +681,7 @@ def grib_keys_iterator_next(iterid):
GRIB_CHECK(res) GRIB_CHECK(res)
return res return res
@require(iterid=int) @require(iterid=int)
def grib_keys_iterator_delete(iterid): def grib_keys_iterator_delete(iterid):
""" """
@ -660,6 +694,7 @@ def grib_keys_iterator_delete(iterid):
""" """
GRIB_CHECK(_internal.grib_c_keys_iterator_delete(iterid)) GRIB_CHECK(_internal.grib_c_keys_iterator_delete(iterid))
@require(iterid=int) @require(iterid=int)
def grib_keys_iterator_get_name(iterid): def grib_keys_iterator_get_name(iterid):
""" """
@ -675,6 +710,7 @@ def grib_keys_iterator_get_name(iterid):
GRIB_CHECK(err) GRIB_CHECK(err)
return name return name
@require(iterid=int) @require(iterid=int)
def grib_keys_iterator_rewind(iterid): def grib_keys_iterator_rewind(iterid):
""" """
@ -685,6 +721,7 @@ def grib_keys_iterator_rewind(iterid):
""" """
GRIB_CHECK(_internal.grib_c_keys_iterator_rewind(iterid)) GRIB_CHECK(_internal.grib_c_keys_iterator_rewind(iterid))
@require(gribid=int, key=str) @require(gribid=int, key=str)
def grib_get_long(gribid, key): def grib_get_long(gribid, key):
""" """
@ -699,6 +736,7 @@ def grib_get_long(gribid,key):
GRIB_CHECK(err) GRIB_CHECK(err)
return value return value
@require(gribid=int, key=str) @require(gribid=int, key=str)
def grib_get_double(gribid, key): def grib_get_double(gribid, key):
""" """
@ -713,6 +751,7 @@ def grib_get_double(gribid,key):
GRIB_CHECK(err) GRIB_CHECK(err)
return value return value
@require(gribid=int, key=str, value=(int, long, float, str)) @require(gribid=int, key=str, value=(int, long, float, str))
def grib_set_long(gribid, key, value): def grib_set_long(gribid, key, value):
""" """
@ -736,6 +775,7 @@ def grib_set_long(gribid,key,value):
GRIB_CHECK(_internal.grib_c_set_long(gribid, key, value)) GRIB_CHECK(_internal.grib_c_set_long(gribid, key, value))
@require(gribid=int, key=str, value=(int, long, float, str)) @require(gribid=int, key=str, value=(int, long, float, str))
def grib_set_double(gribid, key, value): def grib_set_double(gribid, key, value):
""" """
@ -756,6 +796,7 @@ def grib_set_double(gribid,key,value):
GRIB_CHECK(_internal.grib_c_set_double(gribid, key, value)) GRIB_CHECK(_internal.grib_c_set_double(gribid, key, value))
@require(samplename=str) @require(samplename=str)
def grib_new_from_samples(samplename): def grib_new_from_samples(samplename):
""" """
@ -775,6 +816,7 @@ def grib_new_from_samples(samplename):
GRIB_CHECK(err) GRIB_CHECK(err)
return gribid return gribid
@require(gribid_src=int) @require(gribid_src=int)
def grib_clone(gribid_src): def grib_clone(gribid_src):
""" """
@ -793,6 +835,7 @@ def grib_clone(gribid_src):
GRIB_CHECK(err) GRIB_CHECK(err)
return newgribid_src return newgribid_src
@require(gribid=int, key=str) @require(gribid=int, key=str)
def grib_set_double_array(gribid, key, inarray): def grib_set_double_array(gribid, key, inarray):
""" """
@ -826,6 +869,7 @@ def grib_set_double_array(gribid, key, inarray):
_internal.delete_doubleArray(a) _internal.delete_doubleArray(a)
@require(gribid=int, key=str) @require(gribid=int, key=str)
def grib_get_double_array(gribid, key): def grib_get_double_array(gribid, key):
""" """
@ -916,6 +960,7 @@ def grib_set_long_array(gribid, key, inarray):
_internal.delete_longArray(a) _internal.delete_longArray(a)
@require(gribid=int, key=str) @require(gribid=int, key=str)
def grib_get_long_array(gribid, key): def grib_get_long_array(gribid, key):
""" """
@ -950,6 +995,7 @@ def grib_get_long_array(gribid,key):
return result return result
def grib_multi_new(): def grib_multi_new():
""" """
@brief Create a new multi field and return its id. @brief Create a new multi field and return its id.
@ -963,6 +1009,7 @@ def grib_multi_new():
GRIB_CHECK(err) GRIB_CHECK(err)
return mgid return mgid
@require(gribid=int) @require(gribid=int)
def grib_multi_release(gribid): def grib_multi_release(gribid):
""" """
@ -975,6 +1022,7 @@ def grib_multi_release(gribid):
""" """
GRIB_CHECK(_internal.grib_c_multi_release(gribid)) GRIB_CHECK(_internal.grib_c_multi_release(gribid))
@require(gribid_src=int, namespace=str, gribid_dest=int) @require(gribid_src=int, namespace=str, gribid_dest=int)
def grib_copy_namespace(gribid_src, namespace, gribid_dest): def grib_copy_namespace(gribid_src, namespace, gribid_dest):
""" """
@ -988,6 +1036,7 @@ def grib_copy_namespace(gribid_src, namespace, gribid_dest):
""" """
GRIB_CHECK(_internal.grib_c_copy_namespace(gribid_src, namespace, gribid_dest)) GRIB_CHECK(_internal.grib_c_copy_namespace(gribid_src, namespace, gribid_dest))
@require(filename=str, keys=(tuple, list)) @require(filename=str, keys=(tuple, list))
def grib_index_new_from_file(filename, keys): def grib_index_new_from_file(filename, keys):
""" """
@ -1005,6 +1054,7 @@ def grib_index_new_from_file(filename, keys):
GRIB_CHECK(err) GRIB_CHECK(err)
return iid return iid
@require(indexid=int, filename=str) @require(indexid=int, filename=str)
def grib_index_add_file(indexid, filename): def grib_index_add_file(indexid, filename):
""" """
@ -1019,6 +1069,7 @@ def grib_index_add_file(indexid, filename):
err = _internal.grib_c_index_add_file(indexid, filename) err = _internal.grib_c_index_add_file(indexid, filename)
GRIB_CHECK(err) GRIB_CHECK(err)
@require(indexid=int) @require(indexid=int)
def grib_index_release(indexid): def grib_index_release(indexid):
""" """
@ -1031,6 +1082,7 @@ def grib_index_release(indexid):
""" """
GRIB_CHECK(_internal.grib_c_index_release(indexid)) GRIB_CHECK(_internal.grib_c_index_release(indexid))
@require(indexid=int, key=str) @require(indexid=int, key=str)
def grib_index_get_size(indexid, key): def grib_index_get_size(indexid, key):
""" """
@ -1047,6 +1099,7 @@ def grib_index_get_size(indexid,key):
GRIB_CHECK(err) GRIB_CHECK(err)
return value return value
@require(indexid=int, key=str) @require(indexid=int, key=str)
def grib_index_get_long(indexid, key): def grib_index_get_long(indexid, key):
""" """
@ -1077,6 +1130,7 @@ def grib_index_get_long(indexid,key):
return tuple(result) return tuple(result)
def grib_index_get_string(indexid, key): def grib_index_get_string(indexid, key):
""" """
@brief Get the distinct values of the key in argument contained in the index. The key must belong to the index. @brief Get the distinct values of the key in argument contained in the index. The key must belong to the index.
@ -1107,6 +1161,7 @@ def grib_index_get_string(indexid,key):
return tuple(result) return tuple(result)
@require(iid=int, key=str) @require(iid=int, key=str)
def grib_index_get_double(iid, key): def grib_index_get_double(iid, key):
""" """
@ -1137,6 +1192,7 @@ def grib_index_get_double(iid,key):
return tuple(result) return tuple(result)
@require(indexid=int, key=str, val=int) @require(indexid=int, key=str, val=int)
def grib_index_select_long(indexid, key, val): def grib_index_select_long(indexid, key, val):
""" """
@ -1153,6 +1209,7 @@ def grib_index_select_long(indexid,key,val):
""" """
GRIB_CHECK(_internal.grib_c_index_select_long(indexid, key, val)) GRIB_CHECK(_internal.grib_c_index_select_long(indexid, key, val))
@require(iid=int, key=str, val=float) @require(iid=int, key=str, val=float)
def grib_index_select_double(iid, key, val): def grib_index_select_double(iid, key, val):
""" """
@ -1169,6 +1226,7 @@ def grib_index_select_double(iid,key,val):
""" """
GRIB_CHECK(_internal.grib_c_index_select_real8(iid, key, val)) GRIB_CHECK(_internal.grib_c_index_select_real8(iid, key, val))
@require(indexid=int, key=str, val=str) @require(indexid=int, key=str, val=str)
def grib_index_select_string(indexid, key, val): def grib_index_select_string(indexid, key, val):
""" """
@ -1189,6 +1247,7 @@ def grib_index_select_string(indexid,key,val):
""" """
GRIB_CHECK(_internal.grib_c_index_select_string(indexid, key, val)) GRIB_CHECK(_internal.grib_c_index_select_string(indexid, key, val))
def grib_new_from_index(indexid): def grib_new_from_index(indexid):
""" """
@brief Create a new handle from an index after having selected the key values. @brief Create a new handle from an index after having selected the key values.
@ -1214,6 +1273,7 @@ def grib_new_from_index(indexid):
else: else:
return gribid return gribid
@require(gribid=int) @require(gribid=int)
def grib_get_message_size(gribid): def grib_get_message_size(gribid):
""" """
@ -1227,6 +1287,7 @@ def grib_get_message_size(gribid):
GRIB_CHECK(err) GRIB_CHECK(err)
return value return value
@require(gribid=int) @require(gribid=int)
def grib_get_message_offset(gribid): def grib_get_message_offset(gribid):
""" """
@ -1240,6 +1301,7 @@ def grib_get_message_offset(gribid):
GRIB_CHECK(err) GRIB_CHECK(err)
return value return value
@require(gribid=int, key=str, index=int) @require(gribid=int, key=str, index=int)
def grib_get_double_element(gribid, key, index): def grib_get_double_element(gribid, key, index):
""" """
@ -1256,6 +1318,7 @@ def grib_get_double_element(gribid,key,index):
GRIB_CHECK(err) GRIB_CHECK(err)
return value return value
@require(gribid=int, key=str, indexes=(list, tuple)) @require(gribid=int, key=str, indexes=(list, tuple))
def grib_get_double_elements(gribid, key, indexes): def grib_get_double_elements(gribid, key, indexes):
""" """
@ -1296,6 +1359,7 @@ def grib_get_double_elements(gribid,key,indexes):
return result return result
def grib_get_elements(gribid, key, indexes): def grib_get_elements(gribid, key, indexes):
""" """
@brief Retrieve the elements of the key array for the indexes specified in the input. @brief Retrieve the elements of the key array for the indexes specified in the input.
@ -1316,6 +1380,7 @@ def grib_get_elements(gribid,key,indexes):
GRIB_CHECK(err) GRIB_CHECK(err)
return result return result
@require(gribid=int, key=str) @require(gribid=int, key=str)
def grib_set_missing(gribid, key): def grib_set_missing(gribid, key):
""" """
@ -1332,6 +1397,7 @@ def grib_set_missing(gribid,key):
""" """
GRIB_CHECK(_internal.grib_c_set_missing(gribid, key)) GRIB_CHECK(_internal.grib_c_set_missing(gribid, key))
@require(gribid=int) @require(gribid=int)
def grib_set_key_vals(gribid, key_vals): def grib_set_key_vals(gribid, key_vals):
""" """
@ -1354,7 +1420,7 @@ def grib_set_key_vals(gribid, key_vals):
for kv in key_vals: for kv in key_vals:
if not isinstance(kv, str): if not isinstance(kv, str):
raise TypeError("Invalid list/tuple element type '%s'" % kv) raise TypeError("Invalid list/tuple element type '%s'" % kv)
if not '=' in str(kv): if '=' not in str(kv):
raise GribInternalError("Invalid list/tuple element format '%s'" % kv) raise GribInternalError("Invalid list/tuple element format '%s'" % kv)
if len(key_vals_str) > 0: if len(key_vals_str) > 0:
key_vals_str += ',' key_vals_str += ','
@ -1370,6 +1436,7 @@ def grib_set_key_vals(gribid, key_vals):
GRIB_CHECK(_internal.grib_c_set_key_vals(gribid, key_vals_str)) GRIB_CHECK(_internal.grib_c_set_key_vals(gribid, key_vals_str))
@require(gribid=int, key=str) @require(gribid=int, key=str)
def grib_is_missing(gribid, key): def grib_is_missing(gribid, key):
""" """
@ -1389,6 +1456,7 @@ def grib_is_missing(gribid,key):
GRIB_CHECK(err) GRIB_CHECK(err)
return value return value
@require(gribid=int, key=str) @require(gribid=int, key=str)
def grib_is_defined(gribid, key): def grib_is_defined(gribid, key):
""" """
@ -1402,6 +1470,7 @@ def grib_is_defined(gribid,key):
GRIB_CHECK(err) GRIB_CHECK(err)
return value return value
@require(gribid=int, inlat=(int, float), inlon=(int, float)) @require(gribid=int, inlat=(int, float), inlon=(int, float))
def grib_find_nearest(gribid, inlat, inlon, is_lsm=False, npoints=1): def grib_find_nearest(gribid, inlat, inlon, is_lsm=False, npoints=1):
""" """
@ -1434,13 +1503,11 @@ def grib_find_nearest(gribid,inlat,inlon,is_lsm = False,npoints = 1):
result = [] result = []
for i in range(4): for i in range(4):
result.append(Bunch( \ result.append(Bunch(lat=_internal.doubleArray_getitem(poutlat, i),
lat = _internal.doubleArray_getitem(poutlat,i), \ lon=_internal.doubleArray_getitem(poutlon, i),
lon = _internal.doubleArray_getitem(poutlon,i), \ value=_internal.doubleArray_getitem(pvalues, i),
value = _internal.doubleArray_getitem(pvalues,i), \ distance=_internal.doubleArray_getitem(pdist, i),
distance = _internal.doubleArray_getitem(pdist,i), \ index=_internal.intArray_getitem(pidx, i)))
index = _internal.intArray_getitem(pidx,i), \
))
_internal.delete_doubleArray(poutlat) _internal.delete_doubleArray(poutlat)
_internal.delete_doubleArray(poutlon) _internal.delete_doubleArray(poutlon)
@ -1452,6 +1519,7 @@ def grib_find_nearest(gribid,inlat,inlon,is_lsm = False,npoints = 1):
else: else:
raise ValueError("Invalid value for npoints. Expecting 1 or 4.") raise ValueError("Invalid value for npoints. Expecting 1 or 4.")
@require(gribid=int, key=str) @require(gribid=int, key=str)
def grib_get_native_type(gribid, key): def grib_get_native_type(gribid, key):
""" """
@ -1471,6 +1539,7 @@ def grib_get_native_type(gribid, key):
else: else:
return None return None
@require(gribid=int, key=str) @require(gribid=int, key=str)
def grib_get(gribid, key, ktype=None): def grib_get(gribid, key, ktype=None):
""" """
@ -1505,6 +1574,7 @@ def grib_get(gribid,key, ktype=None):
return result return result
@require(gribid=int, key=str) @require(gribid=int, key=str)
def grib_get_array(gribid, key, ktype=None): def grib_get_array(gribid, key, ktype=None):
""" """
@ -1534,6 +1604,7 @@ def grib_get_array(gribid,key, ktype=None):
return result return result
@require(gribid=int) @require(gribid=int)
def grib_get_values(gribid): def grib_get_values(gribid):
""" """
@ -1549,6 +1620,7 @@ def grib_get_values(gribid):
""" """
return grib_get_double_array(gribid, "values") return grib_get_double_array(gribid, "values")
@require(gribid=int) @require(gribid=int)
def grib_set_values(gribid, values): def grib_set_values(gribid, values):
""" """
@ -1569,6 +1641,7 @@ def grib_set_values(gribid,values):
""" """
grib_set_double_array(gribid, "values", values) grib_set_double_array(gribid, "values", values)
@require(gribid=int, key=str) @require(gribid=int, key=str)
def grib_set(gribid, key, value): def grib_set(gribid, key, value):
""" """
@ -1594,6 +1667,7 @@ def grib_set(gribid,key,value):
else: else:
raise GribInternalError("Invalid type of value when setting key '%s'." % key) raise GribInternalError("Invalid type of value when setting key '%s'." % key)
@require(gribid=int, key=str) @require(gribid=int, key=str)
def grib_set_array(gribid, key, value): def grib_set_array(gribid, key, value):
""" """
@ -1628,6 +1702,7 @@ def grib_set_array(gribid,key,value):
else: else:
raise GribInternalError("Invalid type of value when setting key '%s'." % key) raise GribInternalError("Invalid type of value when setting key '%s'." % key)
@require(indexid=int, key=str) @require(indexid=int, key=str)
def grib_index_get(indexid, key, ktype=str): def grib_index_get(indexid, key, ktype=str):
""" """
@ -1657,6 +1732,7 @@ def grib_index_get(indexid,key, ktype=str):
return result return result
@require(indexid=int, key=str) @require(indexid=int, key=str)
def grib_index_select(indexid, key, value): def grib_index_select(indexid, key, value):
""" """
@ -1678,6 +1754,7 @@ def grib_index_select(indexid,key,value):
else: else:
raise GribInternalError("Invalid type of value when setting key '%s'." % key) raise GribInternalError("Invalid type of value when setting key '%s'." % key)
@require(indexid=int, filename=str) @require(indexid=int, filename=str)
def grib_index_write(indexid, filename): def grib_index_write(indexid, filename):
""" """
@ -1693,6 +1770,7 @@ def grib_index_write(indexid, filename):
""" """
GRIB_CHECK(_internal.grib_c_index_write(indexid, filename)) GRIB_CHECK(_internal.grib_c_index_write(indexid, filename))
@require(filename=str) @require(filename=str)
def grib_index_read(filename): def grib_index_read(filename):
""" """
@ -1708,6 +1786,7 @@ def grib_index_read(filename):
GRIB_CHECK(err) GRIB_CHECK(err)
return indexid return indexid
@require(flag=bool) @require(flag=bool)
def grib_no_fail_on_wrong_length(flag): def grib_no_fail_on_wrong_length(flag):
""" """
@ -1720,6 +1799,7 @@ def grib_no_fail_on_wrong_length(flag):
else: else:
_internal.no_fail_on_wrong_length(0) _internal.no_fail_on_wrong_length(0)
@require(flag=bool) @require(flag=bool)
def grib_gts_header(flag): def grib_gts_header(flag):
""" """
@ -1732,6 +1812,7 @@ def grib_gts_header(flag):
else: else:
_internal.grib_c_gts_header_off() _internal.grib_c_gts_header_off()
def grib_get_api_version(): def grib_get_api_version():
""" """
@brief Get the api version. @brief Get the api version.
@ -1748,6 +1829,7 @@ def grib_get_api_version():
__version__ = grib_get_api_version() __version__ = grib_get_api_version()
@require(gribid=int) @require(gribid=int)
def grib_get_message(gribid): def grib_get_message(gribid):
""" """
@ -1765,6 +1847,7 @@ def grib_get_message(gribid):
GRIB_CHECK(error) GRIB_CHECK(error)
return message return message
@require(message=str) @require(message=str)
def grib_new_from_message(message): def grib_new_from_message(message):
""" """
@ -1781,4 +1864,3 @@ def grib_new_from_message(message):
error, gribid = _internal.grib_c_new_from_message(0, message, len(message)) error, gribid = _internal.grib_c_new_from_message(0, message, len(message))
GRIB_CHECK(error) GRIB_CHECK(error)
return gribid return gribid