@@ -46,13 +46,13 @@ def setUpClass(cls):
46
46
cls .time = np .array ([dt .datetime (1999 , 1 , 1 ), dt .datetime (2000 , 1 , 1 )])
47
47
cls .latitude = np .array ([0 , 1 ])
48
48
cls .longitude = np .array ([0 , 1 , 2 ])
49
- dset = xr .Dataset (
49
+ cls . dset = xr .Dataset (
50
50
{
51
51
"intensity" : (["time" , "latitude" , "longitude" ], cls .intensity ),
52
52
},
53
53
dict (time = cls .time , latitude = cls .latitude , longitude = cls .longitude ),
54
54
)
55
- dset .to_netcdf (cls .netcdf_path )
55
+ cls . dset .to_netcdf (cls .netcdf_path )
56
56
57
57
@classmethod
58
58
def tearDownClass (cls ):
@@ -108,49 +108,49 @@ def _assert_default_types(self, hazard):
108
108
109
109
def test_load_path (self ):
110
110
"""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
111
115
hazard = Hazard .from_xarray_raster_file (self .netcdf_path , "" , "" )
112
116
self ._assert_default (hazard )
113
117
114
118
# 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 (
120
123
self .netcdf_path , "" , "" , intensity = "wrong-intensity-path"
121
124
)
122
- self .assertIn ("wrong-intensity-path" , str (cm .exception ))
123
125
124
- def test_load_dataset (self ):
126
+ @patch ("climada.hazard.xarray.xr.open_dataset" )
127
+ def test_load_dataset (self , open_dataset_mock ):
125
128
"""Load the data from an opened dataset as argument"""
129
+ open_dataset_mock .return_value .__enter__ .return_value = self .dset
126
130
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 )
131
136
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
+ )
134
146
135
147
def test_type_error (self ):
136
148
"""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
-
145
149
# 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
+ ):
149
153
Hazard .from_xarray_raster (dset ["intensity" ], "" , "" )
150
- self .assertIn (
151
- "This method only supports xarray.Dataset as input data" ,
152
- str (cm .exception ),
153
- )
154
154
155
155
def test_type_and_unit (self ):
156
156
"""Test passing a custom type and unit"""
@@ -352,9 +352,7 @@ def test_crs(self):
352
352
353
353
def test_crs_from_input (crs_input ):
354
354
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 )
358
356
self .assertEqual (hazard .centroids .geometry .crs , crs )
359
357
360
358
test_crs_from_input ("EPSG:3857" )
@@ -366,6 +364,7 @@ def test_missing_dims(self):
366
364
# Drop time as dimension, but not as coordinate!
367
365
with xr .open_dataset (self .netcdf_path ) as ds :
368
366
ds = ds .isel (time = 0 ).squeeze ()
367
+ print (ds )
369
368
hazard = Hazard .from_xarray_raster (ds , "" , "" )
370
369
self ._assert_default_types (hazard )
371
370
np .testing .assert_array_equal (
@@ -447,7 +446,7 @@ def _assert_intensity_fraction(self, hazard):
447
446
448
447
def test_dimension_naming (self ):
449
448
"""Test if dimensions with different names can be read"""
450
- hazard = Hazard .from_xarray_raster_file (
449
+ hazard = Hazard .from_xarray_raster (
451
450
self .netcdf_path ,
452
451
"" ,
453
452
"" ,
@@ -462,7 +461,7 @@ def test_dimension_naming(self):
462
461
463
462
def test_coordinate_naming (self ):
464
463
"""Test if coordinates with different names than dimensions can be read"""
465
- hazard = Hazard .from_xarray_raster_file (
464
+ hazard = Hazard .from_xarray_raster (
466
465
self .netcdf_path ,
467
466
"" ,
468
467
"" ,
@@ -477,7 +476,7 @@ def test_coordinate_naming(self):
477
476
478
477
def test_2D_coordinates (self ):
479
478
"""Test if read method correctly handles 2D coordinates"""
480
- hazard = Hazard .from_xarray_raster_file (
479
+ hazard = Hazard .from_xarray_raster (
481
480
self .netcdf_path ,
482
481
"" ,
483
482
"" ,
@@ -579,18 +578,17 @@ def test_2D_time(self):
579
578
def test_errors (self ):
580
579
"""Check if expected errors are thrown"""
581
580
# 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 (
584
583
self .netcdf_path ,
585
584
"" ,
586
585
"" ,
587
586
coordinate_vars = dict (bar = "latitude" , longitude = "baz" ),
588
587
)
589
- self .assertIn ("Unknown coordinates passed: '['bar']'." , str (cm .exception ))
590
588
591
589
# Correctly specified, but the custom dimension does not exist
592
590
with self .assertRaisesRegex (RuntimeError , "lalalatitude" ):
593
- Hazard .from_xarray_raster_file (
591
+ Hazard .from_xarray_raster (
594
592
self .netcdf_path ,
595
593
"" ,
596
594
"" ,
0 commit comments