1- import netCDF4 as nc
1+ # Copyright Iris contributors
2+ #
3+ # This file is part of Iris and is released under the BSD license.
4+ # See LICENSE in the root of the repository for full licensing details.
5+ """Integration tests for string data handling."""
6+
7+ import subprocess
8+
29import numpy as np
310import pytest
411
512import iris
613from iris .coords import AuxCoord , DimCoord
714from iris .cube import Cube
15+ from iris .fileformats .netcdf import _thread_safe_nc
16+ from iris .tests import env_bin_path
817
918NX , N_STRLEN = 3 , 64
1019TEST_STRINGS = ["Münster" , "London" , "Amsterdam" ]
1625 TEST_COORD_VALS [- 1 ] = "Xsandwich" # makes the max coord strlen same as data one
1726
1827
28+ # Ensure all tests run with "split attrs" turned on.
1929@pytest .fixture (scope = "module" , autouse = True )
2030def enable_split_attrs ():
2131 with iris .FUTURE .context (save_split_attrs = True ):
@@ -59,7 +69,8 @@ def convert_bytesarray_to_strings(
5969
6070
6171def make_testfile (filepath , chararray , coordarray , encoding_str = None ):
62- with nc .Dataset (filepath , "w" ) as ds :
72+ ds = _thread_safe_nc .DatasetWrapper (filepath , "w" )
73+ try :
6374 ds .createDimension ("x" , NX )
6475 ds .createDimension ("nstr" , N_STRLEN )
6576 vx = ds .createVariable ("x" , int , dimensions = ("x" ))
@@ -100,6 +111,8 @@ def make_testfile(filepath, chararray, coordarray, encoding_str=None):
100111 if INCLUDE_NUMERIC_AUXCOORD :
101112 coords_str += " v_num"
102113 v .coordinates = coords_str
114+ finally :
115+ ds .close ()
103116
104117
105118def make_testcube (
@@ -119,12 +132,19 @@ def make_testcube(
119132 return cube
120133
121134
122- def show_result (filepath ):
123- from pp_utils import ncdump
135+ NCDUMP_PATHSTR = str (env_bin_path ("ncdump" ))
136+
137+
138+ def ncdump (nc_path : str , * args ):
139+ """Call ncdump to print a dump of a file."""
140+ call_args = [NCDUMP_PATHSTR , nc_path ] + list (* args )
141+ subprocess .run (call_args , check = True )
124142
143+
144+ def show_result (filepath ):
125145 print (f"File { filepath } " )
126146 print ("NCDUMP:" )
127- ncdump (filepath , "" )
147+ ncdump (filepath )
128148 # with nc.Dataset(filepath, "r") as ds:
129149 # v = ds.variables["v"]
130150 # print("\n----\nNetcdf data readback (basic)")
@@ -159,6 +179,13 @@ def show_result(filepath):
159179 print (repr (err ))
160180
161181
182+ @pytest .fixture (scope = "session" )
183+ def save_dir (tmp_path_factory ):
184+ return tmp_path_factory .mktemp ("save_files" )
185+
186+
187+ # TODO: the tests don't test things properly yet, they just exercise the code and print
188+ # things for manual debugging.
162189tsts = (
163190 None ,
164191 "ascii" ,
@@ -172,10 +199,10 @@ def show_result(filepath):
172199
173200
174201@pytest .mark .parametrize ("encoding" , tsts )
175- def test_load_encodings (encoding ):
202+ def test_load_encodings (encoding , save_dir ):
176203 # small change
177204 print (f"\n =========\n Testing encoding: { encoding } " )
178- filepath = f"tmp_ { str (encoding )} .nc"
205+ filepath = save_dir / f"tmp_load_ { str (encoding )} .nc"
179206 do_as = encoding
180207 if encoding != "utf-32" :
181208 do_as = "utf-8"
@@ -190,12 +217,12 @@ def test_load_encodings(encoding):
190217
191218
192219@pytest .mark .parametrize ("encoding" , tsts )
193- def test_save_encodings (encoding ):
220+ def test_save_encodings (encoding , save_dir ):
194221 cube = make_testcube (
195222 dataarray = TEST_STRINGS , coordarray = TEST_COORD_VALS , encoding_str = encoding
196223 )
197224 print (cube )
198- filepath = f"tmp_save_{ str (encoding )} .nc"
225+ filepath = save_dir / f"tmp_save_{ str (encoding )} .nc"
199226 if encoding == "ascii" :
200227 with pytest .raises (
201228 UnicodeEncodeError ,
@@ -205,19 +232,3 @@ def test_save_encodings(encoding):
205232 else :
206233 iris .save (cube , filepath )
207234 show_result (filepath )
208-
209-
210- # @pytest.mark.parametrize("ndim", [1, 2])
211- # def test_convert_bytes_to_strings(ndim: int):
212- # if ndim == 1:
213- # source = convert_strings_to_chararray(TEST_STRINGS, 16)
214- # elif ndim == 2:
215- # source = np.stack([
216- # convert_strings_to_chararray(TEST_STRINGS, 16),
217- # convert_strings_to_chararray(TEST_COORD_VALS, 16),
218- # ])
219- # else:
220- # raise ValueError(f"Unexpected param ndim={ndim}.")
221- # # convert the strings to bytes
222- # result = convert_bytesarray_to_strings(source)
223- # print(result)
0 commit comments