@@ -46,13 +46,13 @@ def setUpClass(cls):
4646 cls .time = np .array ([dt .datetime (1999 , 1 , 1 ), dt .datetime (2000 , 1 , 1 )])
4747 cls .latitude = np .array ([0 , 1 ])
4848 cls .longitude = np .array ([0 , 1 , 2 ])
49- dset = xr .Dataset (
49+ cls . dset = xr .Dataset (
5050 {
5151 "intensity" : (["time" , "latitude" , "longitude" ], cls .intensity ),
5252 },
5353 dict (time = cls .time , latitude = cls .latitude , longitude = cls .longitude ),
5454 )
55- dset .to_netcdf (cls .netcdf_path )
55+ cls . dset .to_netcdf (cls .netcdf_path )
5656
5757 @classmethod
5858 def tearDownClass (cls ):
@@ -108,49 +108,49 @@ def _assert_default_types(self, hazard):
108108
109109 def test_load_path (self ):
110110 """Load the data with path as argument"""
111+ hazard = Hazard .from_xarray_raster (self .netcdf_path , "" , "" )
112+ self ._assert_default (hazard )
113+
114+ # Check deprecated method
111115 hazard = Hazard .from_xarray_raster_file (self .netcdf_path , "" , "" )
112116 self ._assert_default (hazard )
113117
114118 # Check wrong paths
115- with self .assertRaises (FileNotFoundError ) as cm :
116- Hazard .from_xarray_raster_file ("file-does-not-exist.nc" , "" , "" )
117- self .assertIn ("file-does-not-exist.nc" , str (cm .exception ))
118- with self .assertRaises (KeyError ) as cm :
119- Hazard .from_xarray_raster_file (
119+ with self .assertRaisesRegex (FileNotFoundError , "file-does-not-exist.nc" ):
120+ Hazard .from_xarray_raster ("file-does-not-exist.nc" , "" , "" )
121+ with self .assertRaisesRegex (KeyError , "wrong-intensity-path" ):
122+ Hazard .from_xarray_raster (
120123 self .netcdf_path , "" , "" , intensity = "wrong-intensity-path"
121124 )
122- self .assertIn ("wrong-intensity-path" , str (cm .exception ))
123125
124- def test_load_dataset (self ):
126+ @patch ("climada.hazard.xarray.xr.open_dataset" )
127+ def test_load_dataset (self , open_dataset_mock ):
125128 """Load the data from an opened dataset as argument"""
129+ open_dataset_mock .return_value .__enter__ .return_value = self .dset
126130
127- def _load_and_assert (chunks ):
128- with xr .open_dataset (self .netcdf_path , chunks = chunks ) as dataset :
129- hazard = Hazard .from_xarray_raster (dataset , "" , "" )
130- self ._assert_default (hazard )
131+ def _load_and_assert (** kwargs ):
132+ hazard = Hazard .from_xarray_raster (
133+ self .netcdf_path , "" , "" , open_dataset_kws = kwargs
134+ )
135+ self ._assert_default (hazard )
131136
132- _load_and_assert (chunks = None )
133- _load_and_assert (chunks = dict (latitude = 1 , longitude = 1 , time = 1 ))
137+ _load_and_assert ()
138+ open_dataset_mock .assert_called_once_with (self .netcdf_path , chunks = "auto" )
139+ open_dataset_mock .reset_mock ()
140+ _load_and_assert (chunks = dict (latitude = 1 , longitude = 1 , time = 1 ), engine = "netcdf4" )
141+ open_dataset_mock .assert_called_once_with (
142+ self .netcdf_path ,
143+ chunks = dict (latitude = 1 , longitude = 1 , time = 1 ),
144+ engine = "netcdf4" ,
145+ )
134146
135147 def test_type_error (self ):
136148 """Calling 'from_xarray_raster' with wrong data type should throw"""
137- # Passing a path
138- with self .assertRaises (TypeError ) as cm :
139- Hazard .from_xarray_raster (self .netcdf_path , "" , "" )
140- self .assertIn (
141- "Use Hazard.from_xarray_raster_file instead" ,
142- str (cm .exception ),
143- )
144-
145149 # Passing a DataArray
146- with xr .open_dataset (self .netcdf_path ) as dset , self .assertRaises (
147- TypeError
148- ) as cm :
150+ with xr .open_dataset (self .netcdf_path ) as dset , self .assertRaisesRegex (
151+ TypeError , "This method only supports passing xr.Dataset"
152+ ):
149153 Hazard .from_xarray_raster (dset ["intensity" ], "" , "" )
150- self .assertIn (
151- "This method only supports xarray.Dataset as input data" ,
152- str (cm .exception ),
153- )
154154
155155 def test_type_and_unit (self ):
156156 """Test passing a custom type and unit"""
@@ -352,9 +352,7 @@ def test_crs(self):
352352
353353 def test_crs_from_input (crs_input ):
354354 crs = CRS .from_user_input (crs_input )
355- hazard = Hazard .from_xarray_raster_file (
356- self .netcdf_path , "" , "" , crs = crs_input
357- )
355+ hazard = Hazard .from_xarray_raster (self .netcdf_path , "" , "" , crs = crs_input )
358356 self .assertEqual (hazard .centroids .geometry .crs , crs )
359357
360358 test_crs_from_input ("EPSG:3857" )
@@ -366,6 +364,7 @@ def test_missing_dims(self):
366364 # Drop time as dimension, but not as coordinate!
367365 with xr .open_dataset (self .netcdf_path ) as ds :
368366 ds = ds .isel (time = 0 ).squeeze ()
367+ print (ds )
369368 hazard = Hazard .from_xarray_raster (ds , "" , "" )
370369 self ._assert_default_types (hazard )
371370 np .testing .assert_array_equal (
@@ -447,7 +446,7 @@ def _assert_intensity_fraction(self, hazard):
447446
448447 def test_dimension_naming (self ):
449448 """Test if dimensions with different names can be read"""
450- hazard = Hazard .from_xarray_raster_file (
449+ hazard = Hazard .from_xarray_raster (
451450 self .netcdf_path ,
452451 "" ,
453452 "" ,
@@ -462,7 +461,7 @@ def test_dimension_naming(self):
462461
463462 def test_coordinate_naming (self ):
464463 """Test if coordinates with different names than dimensions can be read"""
465- hazard = Hazard .from_xarray_raster_file (
464+ hazard = Hazard .from_xarray_raster (
466465 self .netcdf_path ,
467466 "" ,
468467 "" ,
@@ -477,7 +476,7 @@ def test_coordinate_naming(self):
477476
478477 def test_2D_coordinates (self ):
479478 """Test if read method correctly handles 2D coordinates"""
480- hazard = Hazard .from_xarray_raster_file (
479+ hazard = Hazard .from_xarray_raster (
481480 self .netcdf_path ,
482481 "" ,
483482 "" ,
@@ -579,18 +578,17 @@ def test_2D_time(self):
579578 def test_errors (self ):
580579 """Check if expected errors are thrown"""
581580 # Wrong coordinate key
582- with self .assertRaises (ValueError ) as cm :
583- Hazard .from_xarray_raster_file (
581+ with self .assertRaisesRegex (ValueError , "Unknown coordinates passed" ) :
582+ Hazard .from_xarray_raster (
584583 self .netcdf_path ,
585584 "" ,
586585 "" ,
587586 coordinate_vars = dict (bar = "latitude" , longitude = "baz" ),
588587 )
589- self .assertIn ("Unknown coordinates passed: '['bar']'." , str (cm .exception ))
590588
591589 # Correctly specified, but the custom dimension does not exist
592590 with self .assertRaisesRegex (RuntimeError , "lalalatitude" ):
593- Hazard .from_xarray_raster_file (
591+ Hazard .from_xarray_raster (
594592 self .netcdf_path ,
595593 "" ,
596594 "" ,
0 commit comments