2121from eccodes import *
2222
2323SAMPLE_DATA_FOLDER = os .path .join (os .path .dirname (__file__ ), "sample-data" )
24- TEST_DATA = os .path .join (SAMPLE_DATA_FOLDER , "era5-levels-members.grib " )
24+ TEST_DATA = os .path .join (SAMPLE_DATA_FOLDER , "multi_field.grib2 " )
2525
2626
2727def get_sample_fullpath (sample ):
@@ -148,6 +148,17 @@ def test_gts_header():
148148 codes_gts_header (False )
149149
150150
151+ def test_extract_offsets ():
152+ fpath = get_sample_fullpath ("BUFR4.tmpl" )
153+ if fpath is None :
154+ return
155+ is_strict = True
156+ offsets = codes_extract_offsets (fpath , CODES_PRODUCT_ANY , is_strict )
157+ offsets_list = list (offsets )
158+ assert len (offsets_list ) == 1
159+ assert offsets_list [0 ] == 0
160+
161+
151162# ---------------------------------------------
152163# GRIB
153164# ---------------------------------------------
@@ -320,6 +331,7 @@ def test_grib_get_double_element():
320331 gid = codes_grib_new_from_samples ("gg_sfc_grib2" )
321332 elem = codes_get_double_element (gid , "values" , 1 )
322333 assert math .isclose (elem , 259.9865 , abs_tol = 0.001 )
334+ codes_release (gid )
323335
324336
325337def test_grib_get_double_elements ():
@@ -407,7 +419,7 @@ def test_grib_ecc_1042():
407419 2 ,
408420 3 ,
409421 ],
410- dtype = np . float ,
422+ dtype = float ,
411423 )
412424 codes_set_values (gid , write_vals )
413425 read_vals = codes_get_values (gid )
@@ -492,14 +504,35 @@ def test_grib_index_new_from_file(tmpdir):
492504
493505
494506def test_grib_multi_support_reset_file ():
495- # TODO: read an actual multi-field GRIB here
496- fpath = get_sample_fullpath ("GRIB2.tmpl" )
497- if fpath is None :
498- return
499- codes_grib_multi_support_on ()
500- with open (fpath , "rb" ) as f :
501- codes_grib_multi_support_reset_file (f )
502- codes_grib_multi_support_off ()
507+ try :
508+ # TODO: read an actual multi-field GRIB here
509+ fpath = get_sample_fullpath ("GRIB2.tmpl" )
510+ if fpath is None :
511+ return
512+ codes_grib_multi_support_on ()
513+ with open (fpath , "rb" ) as f :
514+ codes_grib_multi_support_reset_file (f )
515+ finally :
516+ codes_grib_multi_support_off ()
517+
518+
519+ def test_grib_multi_field_write (tmpdir ):
520+ # Note: codes_grib_multi_new() calls codes_grib_multi_support_on()
521+ # hence the 'finally' block
522+ try :
523+ gid = codes_grib_new_from_samples ("GRIB2" )
524+ mgid = codes_grib_multi_new ()
525+ section_num = 4
526+ for step in range (12 , 132 , 12 ):
527+ codes_set (gid , "step" , step )
528+ codes_grib_multi_append (gid , section_num , mgid )
529+ output = tmpdir .join ("test_grib_multi_field_write.grib2" )
530+ with open (str (output ), "wb" ) as fout :
531+ codes_grib_multi_write (mgid , fout )
532+ codes_grib_multi_release (mgid )
533+ codes_release (gid )
534+ finally :
535+ codes_grib_multi_support_off ()
503536
504537
505538def test_grib_uuid_get_set ():
@@ -654,7 +687,6 @@ def test_bufr_get_message_offset():
654687 assert codes_get_message_offset (gid ) == 0
655688
656689
657- # Experimental features
658690def test_codes_bufr_key_is_header ():
659691 bid = codes_bufr_new_from_samples ("BUFR4_local_satellite" )
660692 assert codes_bufr_key_is_header (bid , "edition" )
@@ -670,17 +702,6 @@ def test_codes_bufr_key_is_header():
670702 assert not codes_bufr_key_is_header (bid , "#6#brightnessTemperature" )
671703
672704
673- def test_extract_offsets ():
674- fpath = get_sample_fullpath ("BUFR4.tmpl" )
675- if fpath is None :
676- return
677- is_strict = True
678- offsets = codes_extract_offsets (fpath , CODES_PRODUCT_ANY , is_strict )
679- offsets_list = list (offsets )
680- assert len (offsets_list ) == 1
681- assert offsets_list [0 ] == 0
682-
683-
684705def test_bufr_extract_headers ():
685706 fpath = get_sample_fullpath ("BUFR4_local.tmpl" )
686707 if fpath is None :
@@ -695,3 +716,30 @@ def test_bufr_extract_headers():
695716 assert header ["ident" ].strip () == "91334"
696717 assert header ["rdbtimeSecond" ] == 19
697718 assert math .isclose (header ["localLongitude" ], 151.83 )
719+
720+
721+ # ---------------------------------------------
722+ # Experimental features
723+ # ---------------------------------------------
724+ def test_grib_nearest2 ():
725+ if "codes_grib_nearest_new" not in dir (eccodes ):
726+ return
727+ gid = codes_grib_new_from_samples ("gg_sfc_grib2" )
728+ lat , lon = 40 , 20
729+ flags = CODES_GRIB_NEAREST_SAME_GRID | CODES_GRIB_NEAREST_SAME_POINT
730+ nid = codes_grib_nearest_new (gid )
731+ assert nid > 0
732+ nearest = codes_grib_nearest_find (nid , gid , lat , lon , flags )
733+ assert len (nearest ) == 4
734+ expected_indexes = (2679 , 2678 , 2517 , 2516 )
735+ returned_indexes = (
736+ nearest [0 ].index ,
737+ nearest [1 ].index ,
738+ nearest [2 ].index ,
739+ nearest [3 ].index ,
740+ )
741+ assert sorted (expected_indexes ) == sorted (returned_indexes )
742+ assert math .isclose (nearest [0 ].value , 295.22085 , abs_tol = 0.0001 )
743+ assert math .isclose (nearest [2 ].distance , 24.16520 , abs_tol = 0.0001 )
744+ codes_release (gid )
745+ codes_grib_nearest_delete (nid )
0 commit comments