@@ -155,7 +155,6 @@ def wrapper(*args):
155155
156156
157157def get_handle (msgid ):
158- assert isinstance (msgid , int )
159158 h = ffi .cast ("grib_handle*" , msgid )
160159 if h == ffi .NULL :
161160 raise errors .InvalidGribError (f"get_handle: Bad message ID { msgid } " )
@@ -169,7 +168,6 @@ def put_handle(handle):
169168
170169
171170def get_multi_handle (msgid ):
172- assert isinstance (msgid , int )
173171 return ffi .cast ("grib_multi_handle*" , msgid )
174172
175173
@@ -178,7 +176,6 @@ def put_multi_handle(handle):
178176
179177
180178def get_index (indexid ):
181- assert isinstance (indexid , int )
182179 return ffi .cast ("grib_index*" , indexid )
183180
184181
@@ -187,7 +184,6 @@ def put_index(indexh):
187184
188185
189186def get_iterator (iterid ):
190- assert isinstance (iterid , int )
191187 return ffi .cast ("grib_iterator*" , iterid )
192188
193189
@@ -196,7 +192,6 @@ def put_iterator(iterh):
196192
197193
198194def get_grib_keys_iterator (iterid ):
199- assert isinstance (iterid , int )
200195 return ffi .cast ("grib_keys_iterator*" , iterid )
201196
202197
@@ -205,7 +200,6 @@ def put_grib_keys_iterator(iterh):
205200
206201
207202def get_bufr_keys_iterator (iterid ):
208- assert isinstance (iterid , int )
209203 return ffi .cast ("bufr_keys_iterator*" , iterid )
210204
211205
@@ -314,7 +308,7 @@ def codes_new_from_file(fileobj, product_kind, headers_only=False):
314308 return gts_new_from_file (fileobj , headers_only )
315309 if product_kind == CODES_PRODUCT_ANY :
316310 return any_new_from_file (fileobj , headers_only )
317- raise Exception ("Invalid product kind: " + product_kind )
311+ raise ValueError ("Invalid product kind %d" % product_kind )
318312
319313
320314@require (fileobj = file )
@@ -456,7 +450,7 @@ def grib_multi_support_reset_file(fileobj):
456450 @brief Reset file handle in multiple field support mode
457451 """
458452 context = lib .grib_context_get_default ()
459- GRIB_CHECK ( lib .grib_multi_support_reset_file (context , fileobj ) )
453+ lib .grib_multi_support_reset_file (context , fileobj )
460454
461455
462456@require (msgid = int )
@@ -971,7 +965,7 @@ def grib_get_double(msgid, key):
971965 return value_p [0 ]
972966
973967
974- @require (msgid = int , key = str , value = (int , float , str ))
968+ @require (msgid = int , key = str , value = (int , float , np . float16 , np . float32 , str ))
975969def grib_set_long (msgid , key , value ):
976970 """
977971 @brief Set the integer value for a key in a message.
@@ -990,13 +984,13 @@ def grib_set_long(msgid, key, value):
990984 raise TypeError ("Invalid type" )
991985
992986 if value > sys .maxsize :
993- raise TypeError ( "Invalid type " )
987+ raise ValueError ( "Value too large " )
994988
995989 h = get_handle (msgid )
996990 GRIB_CHECK (lib .grib_set_long (h , key .encode (ENC ), value ))
997991
998992
999- @require (msgid = int , key = str , value = (int , float , str ))
993+ @require (msgid = int , key = str , value = (int , float , np . float16 , np . float32 , str ))
1000994def grib_set_double (msgid , key , value ):
1001995 """
1002996 @brief Set the double value for a key in a message.
@@ -1037,7 +1031,7 @@ def codes_new_from_samples(samplename, product_kind):
10371031 return grib_new_from_samples (samplename )
10381032 if product_kind == CODES_PRODUCT_BUFR :
10391033 return codes_bufr_new_from_samples (samplename )
1040- raise Exception ("Invalid product kind: " + product_kind )
1034+ raise ValueError ("Invalid product kind %d" % product_kind )
10411035
10421036
10431037@require (samplename = str )
@@ -2039,7 +2033,7 @@ def grib_set(msgid, key, value):
20392033 """
20402034 if isinstance (value , int ):
20412035 grib_set_long (msgid , key , value )
2042- elif isinstance (value , float ):
2036+ elif isinstance (value , ( float , np . float16 , np . float32 , np . float64 ) ):
20432037 grib_set_double (msgid , key , value )
20442038 elif isinstance (value , str ):
20452039 grib_set_string (msgid , key , value )
@@ -2075,12 +2069,11 @@ def grib_set_array(msgid, key, value):
20752069 except TypeError :
20762070 pass
20772071
2078- if isinstance (val0 , float ):
2072+ if isinstance (val0 , ( float , np . float16 , np . float32 , np . float64 ) ):
20792073 grib_set_double_array (msgid , key , value )
20802074 elif isinstance (val0 , str ):
20812075 grib_set_string_array (msgid , key , value )
20822076 else :
2083- # Note: Cannot do isinstance(val0,int) for numpy.int64
20842077 try :
20852078 int (val0 )
20862079 except (ValueError , TypeError ):
@@ -2392,3 +2385,48 @@ def codes_bufr_extract_headers(filepath, is_strict=True):
23922385 while i < num_messages :
23932386 yield _convert_struct_to_dict (headers [i ])
23942387 i += 1
2388+
2389+
2390+ def codes_bufr_key_is_header (msgid , key ):
2391+ """
2392+ @brief Check if the BUFR key is in the header or in the data section.
2393+
2394+ If the data section has not been unpacked, then passing in a key from
2395+ the data section will throw KeyValueNotFoundError.
2396+
2397+ @param msgid id of the BUFR message loaded in memory
2398+ @param key key name
2399+ @return 1->header, 0->data section
2400+ @exception CodesInternalError
2401+ """
2402+ h = get_handle (msgid )
2403+ err , value = err_last (lib .codes_bufr_key_is_header )(h , key .encode (ENC ))
2404+ GRIB_CHECK (err )
2405+ return value
2406+
2407+
2408+ def codes_extract_offsets (filepath , product_kind , is_strict = True ):
2409+ """
2410+ @brief Message offset extraction (EXPERIMENTAL FEATURE)
2411+
2412+ @param filepath path of input file
2413+ @param is_strict fail as soon as any invalid message is encountered
2414+ @return a list of offsets
2415+ @exception CodesInternalError
2416+ """
2417+ context = lib .grib_context_get_default ()
2418+ offsets_p = ffi .new ("long int**" )
2419+ num_message_p = ffi .new ("int*" )
2420+
2421+ err = lib .codes_extract_offsets_malloc (
2422+ context , filepath .encode (ENC ), product_kind , offsets_p , num_message_p , is_strict
2423+ )
2424+ GRIB_CHECK (err )
2425+
2426+ num_messages = num_message_p [0 ]
2427+ offsets = offsets_p [0 ]
2428+
2429+ i = 0
2430+ while i < num_messages :
2431+ yield offsets [i ]
2432+ i += 1
0 commit comments