@@ -65,29 +65,40 @@ def test_save(filename, source, extension, muddle, tmp_path):
65
65
# Caused an issue because they got sorted before
66
66
if muddle :
67
67
data = data .sortby ("track_id" , ascending = False )
68
+
68
69
# Copy the data because save modifies the dataset at the moment
69
- huracanpy .save (data .copy (), str (tmp_path / f"tmp_file.{ extension } " ))
70
+ data_orig = data .copy ()
71
+ huracanpy .save (data , str (tmp_path / f"tmp_file.{ extension } " ))
72
+
73
+ # Check that the original data is not modified by the save function
74
+ _assert_dataset_identical (data_orig , data )
70
75
71
76
# Reload the data and check it is still the same
72
- data_ = huracanpy .load (str (tmp_path / f"tmp_file.{ extension } " ))
77
+ # Saving as netcdf does force sorting by track_id so apply this
78
+ if extension == "nc" :
79
+ data = data .sortby ("track_id" )
80
+ data_reload = huracanpy .load (str (tmp_path / f"tmp_file.{ extension } " ))
81
+ _assert_dataset_identical (data , data_reload )
82
+
73
83
74
- assert len (data .variables ) == len (data_ .variables )
75
- assert len (data .coords ) == len (data_ .coords )
76
- for var in list (data .variables ) + list (data .coords ):
84
+ def _assert_dataset_identical (ds1 , ds2 ):
85
+ assert len (ds1 .variables ) == len (ds2 .variables )
86
+ assert len (ds1 .coords ) == len (ds2 .coords )
87
+ for var in list (ds1 .variables ) + list (ds1 .coords ):
77
88
# Work around for xarray inconsistent loading the data as float or double
78
89
# depending on fill_value and scale_factor
79
90
# np.testing.assert_allclose doesn't work for datetime64, object, or string
80
- if np .issubdtype (data [var ].dtype , np .number ):
81
- if data [var ].dtype != data_ [var ].dtype :
91
+ if np .issubdtype (ds1 [var ].dtype , np .number ):
92
+ if ds1 [var ].dtype != ds2 [var ].dtype :
82
93
rtol = 1e-6
83
94
else :
84
95
rtol = 0
85
96
np .testing .assert_allclose (
86
- data [var ].data .astype (data_ [var ].dtype ), data_ [var ].data , rtol = rtol
97
+ ds1 [var ].data .astype (ds2 [var ].dtype ), ds2 [var ].data , rtol = rtol
87
98
)
88
99
else :
89
- assert (data [var ].data == data_ [var ].data ).all ()
100
+ assert (ds1 [var ].data == ds2 [var ].data ).all ()
90
101
91
- assert len (data .attrs ) == len (data_ .attrs )
92
- for attr in data .attrs :
93
- assert data .attrs [attr ] == data_ .attrs [attr ]
102
+ assert len (ds1 .attrs ) == len (ds2 .attrs )
103
+ for attr in ds1 .attrs :
104
+ assert ds1 .attrs [attr ] == ds2 .attrs [attr ]
0 commit comments