1919SAMPLE_DATA_FOLDER = os .path .join (os .path .dirname (__file__ ), "sample-data" )
2020TEST_DATA = os .path .join (SAMPLE_DATA_FOLDER , "era5-levels-members.grib" )
2121
22+
23+ def get_sample_fullpath (sample ):
24+ samples_path = codes_samples_path ()
25+ if not os .path .isdir (samples_path ):
26+ return None
27+ return os .path .join (samples_path , sample )
28+
29+
2230# ---------------------------------------------
2331# ANY PRODUCT
2432# ---------------------------------------------
@@ -61,18 +69,17 @@ def test_codes_get_native_type():
6169
6270
6371def test_new_from_file ():
64- samples_path = codes_samples_path ( )
65- if not os . path . isdir ( samples_path ) :
72+ fpath = get_sample_fullpath ( "GRIB2.tmpl" )
73+ if fpath is None :
6674 return
67- fpath = os .path .join (samples_path , "GRIB2.tmpl" )
6875 with open (fpath , "rb" ) as f :
6976 msgid = codes_new_from_file (f , CODES_PRODUCT_GRIB )
7077 assert msgid
71- fpath = os . path . join ( samples_path , "BUFR4.tmpl" )
78+ fpath = get_sample_fullpath ( "BUFR4.tmpl" )
7279 with open (fpath , "rb" ) as f :
7380 msgid = codes_new_from_file (f , CODES_PRODUCT_BUFR )
7481 assert msgid
75- fpath = os . path . join ( samples_path , "clusters_grib1.tmpl" )
82+ fpath = get_sample_fullpath ( "clusters_grib1.tmpl" )
7683 with open (fpath , "rb" ) as f :
7784 msgid = codes_new_from_file (f , CODES_PRODUCT_ANY )
7885 assert msgid
@@ -88,18 +95,17 @@ def test_new_from_file():
8895
8996
9097def test_any_read ():
91- samples_path = codes_samples_path ( )
92- if not os . path . isdir ( samples_path ) :
98+ fpath = get_sample_fullpath ( "GRIB1.tmpl" )
99+ if fpath is None :
93100 return
94- fpath = os .path .join (samples_path , "GRIB1.tmpl" )
95101 with open (fpath , "rb" ) as f :
96102 msgid = codes_any_new_from_file (f )
97103 assert codes_get (msgid , "edition" ) == 1
98104 assert codes_get (msgid , "identifier" ) == "GRIB"
99105 assert codes_get (msgid , "identifier" , str ) == "GRIB"
100106 codes_release (msgid )
101107
102- fpath = os . path . join ( samples_path , "BUFR3.tmpl" )
108+ fpath = get_sample_fullpath ( "BUFR3.tmpl" )
103109 with open (fpath , "rb" ) as f :
104110 msgid = codes_any_new_from_file (f )
105111 assert codes_get (msgid , "edition" ) == 3
@@ -108,10 +114,9 @@ def test_any_read():
108114
109115
110116def test_count_in_file ():
111- samples_path = codes_samples_path ( )
112- if not os . path . isdir ( samples_path ) :
117+ fpath = get_sample_fullpath ( "GRIB1.tmpl" )
118+ if fpath is None :
113119 return
114- fpath = os .path .join (samples_path , "GRIB1.tmpl" )
115120 with open (fpath , "rb" ) as f :
116121 assert codes_count_in_file (f ) == 1
117122
@@ -449,10 +454,9 @@ def test_grib_new_from_file_error(tmp_path):
449454
450455
451456def test_grib_index_new_from_file (tmpdir ):
452- samples_path = codes_samples_path ( )
453- if not os . path . isdir ( samples_path ) :
457+ fpath = get_sample_fullpath ( "GRIB1.tmpl" )
458+ if fpath is None :
454459 return
455- fpath = os .path .join (samples_path , "GRIB1.tmpl" )
456460 index_keys = ["shortName" , "level" , "number" , "step" ]
457461 iid = codes_index_new_from_file (fpath , index_keys )
458462 index_file = str (tmpdir .join ("temp.grib.index" ))
@@ -480,10 +484,7 @@ def test_grib_index_new_from_file(tmpdir):
480484
481485def test_grib_multi_support_reset_file ():
482486 # TODO: read an actual multi-field GRIB here
483- samples_path = codes_samples_path ()
484- if not os .path .isdir (samples_path ):
485- return
486- fpath = os .path .join (samples_path , "GRIB2.tmpl" )
487+ fpath = get_sample_fullpath ("GRIB2.tmpl" )
487488 codes_grib_multi_support_on ()
488489 with open (fpath , "rb" ) as f :
489490 codes_grib_multi_support_reset_file (f )
@@ -625,13 +626,22 @@ def test_bufr_get_message_offset():
625626 assert codes_get_message_offset (gid ) == 0
626627
627628
628- # Experimental feature
629+ # Experimental features
630+ def _test_extract_offsets ():
631+ fpath = get_sample_fullpath ("BUFR4.tmpl" )
632+ if fpath is None :
633+ return
634+ is_strict = True
635+ offsets = codes_extract_offsets (fpath , CODES_PRODUCT_ANY , is_strict )
636+ offsets_list = list (offsets )
637+ assert len (offsets_list ) == 1
638+ assert offsets_list [0 ] == 0
639+
640+
629641def test_bufr_extract_headers ():
630- samples_path = codes_samples_path ( )
631- if not os . path . isdir ( samples_path ) :
642+ fpath = get_sample_fullpath ( "BUFR4_local.tmpl" )
643+ if fpath is None :
632644 return
633- print ("Samples path = " , samples_path )
634- fpath = os .path .join (samples_path , "BUFR4_local.tmpl" )
635645 headers = list (codes_bufr_extract_headers (fpath ))
636646 # Sample file contains just one message
637647 assert len (headers ) == 1
0 commit comments