@@ -352,7 +352,13 @@ def test_write_netcdf_mode(self):
352352 if ex_field_n in (8 , 9 , 10 ):
353353 continue
354354
355- cf .write (ex_field , tmpfile , fmt = fmt , mode = "a" )
355+ cf .write (
356+ ex_field ,
357+ tmpfile ,
358+ fmt = fmt ,
359+ mode = "a" ,
360+ netcdf_backend = "netCDF4" ,
361+ )
356362 f = cf .read (tmpfile )
357363
358364 if ex_field_n == 5 : # another special case
@@ -433,15 +439,25 @@ def test_write_netcdf_mode(self):
433439 # field n=5 which aggregates to one with n=2] => + 1 - 1 = + 0:
434440 overall_length = len (append_ex_fields )
435441 cf .write (
436- append_ex_fields , tmpfile , fmt = fmt , mode = "a"
442+ append_ex_fields ,
443+ tmpfile ,
444+ fmt = fmt ,
445+ mode = "a" ,
446+ netcdf_backend = "netCDF4" ,
437447 ) # 2. now append
438448 f = cf .read (tmpfile )
439449 self .assertEqual (len (f ), overall_length )
440450
441451 # Also test the mode="r+" alias for mode="a".
442- cf .write (g , tmpfile , fmt = fmt , mode = "w" ) # 1. overwrite to wipe
443452 cf .write (
444- append_ex_fields , tmpfile , fmt = fmt , mode = "r+"
453+ g , tmpfile , fmt = fmt , mode = "w" , netcdf_backend = "netCDF4"
454+ ) # 1. overwrite to wipe
455+ cf .write (
456+ append_ex_fields ,
457+ tmpfile ,
458+ fmt = fmt ,
459+ mode = "r+" ,
460+ netcdf_backend = "netCDF4" ,
445461 ) # 2. now append
446462 f = cf .read (tmpfile )
447463 self .assertEqual (len (f ), overall_length )
@@ -543,8 +559,12 @@ def test_write_netcdf_mode(self):
543559 # )
544560
545561 # Check behaviour when append identical fields, as an edge case:
546- cf .write (g , tmpfile , fmt = fmt , mode = "w" ) # 1. overwrite to wipe
547- cf .write (g_copy , tmpfile , fmt = fmt , mode = "a" ) # 2. now append
562+ cf .write (
563+ g , tmpfile , fmt = fmt , mode = "w" , netcdf_backend = "netCDF4"
564+ ) # 1. overwrite to wipe
565+ cf .write (
566+ g_copy , tmpfile , fmt = fmt , mode = "a" , netcdf_backend = "netCDF4"
567+ ) # 2. now append
548568 f = cf .read (tmpfile )
549569 self .assertEqual (len (f ), 2 )
550570 self .assertTrue (
@@ -849,7 +869,7 @@ def test_write_omit_data(self):
849869 f = self .f1
850870 cf .write (f , tmpfile )
851871
852- cf .write (f , tmpfile , omit_data = "all" )
872+ cf .write (f , tmpfile , omit_data = "all" , netcdf_backend = "netCDF4" )
853873 g = cf .read (tmpfile )
854874 self .assertEqual (len (g ), 1 )
855875 g = g [0 ]
@@ -861,7 +881,12 @@ def test_write_omit_data(self):
861881 # Check that a dump works
862882 g .dump (display = False )
863883
864- cf .write (f , tmpfile , omit_data = ("field" , "dimension_coordinate" ))
884+ cf .write (
885+ f ,
886+ tmpfile ,
887+ omit_data = ("field" , "dimension_coordinate" ),
888+ netcdf_backend = "netCDF4" ,
889+ )
865890 g = cf .read (tmpfile )[0 ]
866891
867892 # Check that only the field and dimension coordinate data are
@@ -870,7 +895,7 @@ def test_write_omit_data(self):
870895 self .assertFalse (np .ma .count (g .construct ("grid_latitude" ).array ))
871896 self .assertTrue (np .ma .count (g .construct ("latitude" ).array ))
872897
873- cf .write (f , tmpfile , omit_data = "field" )
898+ cf .write (f , tmpfile , omit_data = "field" , netcdf_backend = "netCDF4" )
874899 g = cf .read (tmpfile )[0 ]
875900
876901 # Check that only the field data are missing
@@ -956,6 +981,73 @@ def test_read_zarr(self):
956981 z = cf .read (zarr_dataset , dataset_type = "Zarr" )
957982 self .assertEqual (len (z ), 1 )
958983
984+ def test_write_netcdf_backend (self ):
985+ """Test cf.write with different netCDF backends."""
986+ f = self .f0
987+
988+ cf .write (f , tmpfile0 , netcdf_backend = "h5netcdf-h5py" )
989+ cf .write (f , tmpfile1 , netcdf_backend = "netCDF4" )
990+ f0 = cf .read (tmpfile0 )[0 ]
991+ f1 = cf .read (tmpfile1 )[0 ]
992+ self .assertTrue (f1 .equals (f0 ))
993+
994+ f = cf .read (filename )
995+ cf .write (f , tmpfile0 , netcdf_backend = "h5netcdf-h5py" )
996+ cf .write (f , tmpfile1 , netcdf_backend = "netCDF4" )
997+ f0 = cf .read (tmpfile0 )[0 ]
998+ f1 = cf .read (tmpfile1 )[0 ]
999+ self .assertTrue (f1 .equals (f0 ))
1000+
1001+ # Bad fmt/backend combinations
1002+ for backend in ("netCDF4" , "h5netcdf-h5py" ):
1003+ with self .assertRaises (ValueError ):
1004+ cf .write (f , tmpfile , fmt = "ZARR3" , netcdf_backend = backend )
1005+
1006+ for backend in ("zarr" , "h5netcdf-h5py" ):
1007+ with self .assertRaises (ValueError ):
1008+ cf .write (
1009+ f , tmpfile , fmt = "NETCDF3_CLASSIC" , netcdf_backend = backend
1010+ )
1011+
1012+ for backend in ("zarr" ,):
1013+ with self .assertRaises (ValueError ):
1014+ cf .write (f , tmpfile , fmt = "NETCDF4" , netcdf_backend = backend )
1015+
1016+ def test_write_h5py_options (self ):
1017+ """Test cf.write with h5py_options."""
1018+ f = self .f0
1019+ h5py_options = dict (
1020+ fs_strategy = "page" , fs_page_size = 2 ** 20 , meta_block_size = 500000
1021+ )
1022+
1023+ cf .write (f , tmpfile0 , h5py_options = None )
1024+ size = os .path .getsize (tmpfile0 )
1025+
1026+ cf .write (
1027+ f ,
1028+ tmpfile1 ,
1029+ netcdf_backend = "h5netcdf-h5py" ,
1030+ h5py_options = h5py_options ,
1031+ )
1032+ self .assertTrue (os .path .getsize (tmpfile1 ) > size )
1033+
1034+ f0 = cf .read (tmpfile0 )[0 ]
1035+ f1 = cf .read (tmpfile1 )[0 ]
1036+ self .assertTrue (f1 .equals (f0 ))
1037+
1038+ with self .assertRaises (ValueError ):
1039+ cf .write (
1040+ f ,
1041+ tmpfile0 ,
1042+ netcdf_backend = "netCDF4" ,
1043+ h5py_options = h5py_options ,
1044+ )
1045+
1046+ with self .assertRaises (ValueError ):
1047+ cf .write (
1048+ f , tmpfile0 , fmt = "NETCDF3_CLASSIC" , h5py_options = h5py_options
1049+ )
1050+
9591051 def test_read_netcdf_file (self ):
9601052 """Test cf.read for differing the netcdf_file backend."""
9611053 f = self .f0
0 commit comments