55(1) check equivalence of cubes : xarray.load(file) VS xarray.load(ncdata(file))
66(2) check equivalence of files : xarray -> file VS xarray->ncdata->file
77"""
8- from subprocess import check_output
9-
108import pytest
119import xarray
1210
1715 session_testdir ,
1816 standard_testcase ,
1917)
20- from tests .integration .equivalence_testing_utils import (
21- adjust_chunks ,
22- set_tiny_chunks ,
23- )
24-
25- # Avoid complaints that imported fixtures are "unused"
26- standard_testcase , session_testdir , adjust_chunks
2718
2819from ncdata .threadlock_sharing import lockshare_context
2920from ncdata .xarray import from_xarray , to_xarray
3021
22+ # Avoid complaints that imported fixtures are "unused"
23+ # TODO: declare fixtures in usual way in pytest config?
24+ standard_testcase , session_testdir
25+
26+
3127# _FIX_LOCKS = True
3228_FIX_LOCKS = False
3329
@@ -41,75 +37,31 @@ def use_xarraylock():
4137 yield
4238
4339
44- # _USE_TINY_CHUNKS = True
45- _USE_TINY_CHUNKS = False
46- set_tiny_chunks (_USE_TINY_CHUNKS )
47-
48-
4940def test_load_direct_vs_viancdata (
50- standard_testcase , use_xarraylock , adjust_chunks , tmp_path
41+ standard_testcase , use_xarraylock , tmp_path
5142):
5243 source_filepath = standard_testcase .filepath
5344 ncdata = from_nc4 (source_filepath )
5445
55- if standard_testcase .name in BAD_LOADSAVE_TESTCASES ["xarray" ]["load" ]:
46+ excluded_testcases = BAD_LOADSAVE_TESTCASES ["xarray" ]["load" ]
47+ if any (key in standard_testcase .name for key in excluded_testcases ):
5648 pytest .skip ("excluded testcase (xarray cannot load)" )
5749
58- # _Debug = True
59- _Debug = False
60- if _Debug :
61- print (f"\n testcase: { standard_testcase .name } " )
62- print ("spec =" )
63- print (standard_testcase .spec )
64- print ("\n ncdata =" )
65- print (ncdata )
66- print ("\n ncdump =" )
67- txt = check_output ([f"ncdump { source_filepath } " ], shell = True ).decode ()
68- print (txt )
69-
7050 # Load the testcase with Xarray.
7151 xr_ds = xarray .open_dataset (source_filepath , chunks = - 1 )
52+
7253 # Load same, via ncdata
7354 xr_ncdata_ds = to_xarray (ncdata )
7455
75- # Xarray dataset (variable) comparison is problematic
76- # result = xr_ncdata_ds.identical(xr_ds)
77-
78- # So for now, save Xarray datasets to disk + compare that way.
79- temp_xr_path = tmp_path / "tmp_out_xr.nc"
80- temp_xr_ncdata_path = tmp_path / "tmp_out_xr_ncdata.nc"
81- xr_ds .to_netcdf (temp_xr_path )
82- xr_ncdata_ds .to_netcdf (temp_xr_ncdata_path )
83-
84- if _Debug :
85- print ("\n \n -----\n Result ncdump : 'DIRECT' nc4 -> xr -> nc4 ... " )
86- txt = check_output ([f"ncdump { temp_xr_path } " ], shell = True ).decode ()
87- print (txt )
88- print (
89- "\n \n -----\n Result ncdump : 'INDIRECT'' nc4 -> ncdata-> xr -> nc4 ... "
90- )
91- txt = check_output (
92- [f"ncdump { temp_xr_ncdata_path } " ], shell = True
93- ).decode ()
94- print (txt )
95-
96- # FOR NOW: compare with experimental ncdata comparison.
97- # I know this is a bit circular, but it is useful for debugging, for now ...
98- result = compare_nc_datasets (
99- temp_xr_path ,
100- temp_xr_ncdata_path ,
101- check_dims_order = False ,
102- suppress_warnings = True ,
103- )
104- if result != []:
105- assert result == []
56+ # Treat as OK if it passes xarray comparison
57+ assert xr_ds .identical (xr_ncdata_ds )
10658
10759
10860def test_save_direct_vs_viancdata (standard_testcase , tmp_path ):
10961 source_filepath = standard_testcase .filepath
110- ncdata = from_nc4 (source_filepath )
11162
11263 excluded_testcases = BAD_LOADSAVE_TESTCASES ["xarray" ]["load" ]
64+ excluded_testcases .extend (BAD_LOADSAVE_TESTCASES ["xarray" ]["save" ])
11365 if any (key in standard_testcase .name for key in excluded_testcases ):
11466 pytest .skip ("excluded testcase" )
11567
@@ -124,31 +76,6 @@ def test_save_direct_vs_viancdata(standard_testcase, tmp_path):
12476 ncds_fromxr = from_xarray (xrds )
12577 to_nc4 (ncds_fromxr , temp_ncdata_savepath )
12678
127- # _Debug = True
128- _Debug = False
129- if _Debug :
130- ncdump_opts = "-h"
131- # ncdump_opts = ""
132- txt = f"""
133- testcase: { standard_testcase .name }
134- spec = { standard_testcase .spec }
135- ncdata = ...
136- { ncdata }
137- ncdump ORIGINAL TESTCASE SOURCEFILE =
138- """
139- txt += check_output (
140- [f"ncdump { ncdump_opts } { source_filepath } " ], shell = True
141- ).decode ()
142- txt += "\n ncdump DIRECT FROM XARRAY ="
143- txt += check_output (
144- [f"ncdump { ncdump_opts } { temp_direct_savepath } " ], shell = True
145- ).decode ()
146- txt += "\n ncdump VIA NCDATA ="
147- txt += check_output (
148- [f"ncdump { ncdump_opts } { temp_ncdata_savepath } " ], shell = True
149- ).decode ()
150- print (txt )
151-
15279 # Check equivalence
15380 results = compare_nc_datasets (
15481 temp_direct_savepath ,
0 commit comments