Index: trunk/SDToolBox/data_processing.py =================================================================== diff -u -r79 -r82 --- trunk/SDToolBox/data_processing.py (.../data_processing.py) (revision 79) +++ trunk/SDToolBox/data_processing.py (.../data_processing.py) (revision 82) @@ -139,49 +139,38 @@ @staticmethod def spatial_resampling_grid( dataset: xr.Dataset, - resolution: int) -> xr.Dataset: + resample_latitude: xr.DataArray, + resample_longitude: xr.DataArray) -> xr.Dataset: """Resamples spatially a given dataset. Arguments: dataset {xr.Dataset} -- Dataset to resample. - resolution {int} -- Number of times to apply resolution. + resample_latitude {xr.Dataset} -- Resample latitude to apply. + resample_longitude {xr.Dataset} -- Resample longitude to apply. Returns: xr.Dataset -- Resampled dataset. """ # Resample gridded data into predefined grid. # Resample into a number of times the original resolution. if dataset is None or \ - resolution is None: + resample_latitude is None or \ + resample_longitude is None: raise Exception(om.error_all_arguments_required) if not DataProcessing.is_gridded(dataset): raise Exception(om.error_only_for_gridded_dataset) + # keep_attrs seems not to be working, we'll set the attrs manually. # dataset.set_options(keep_attrs=True) DataProcessing.set_original_scale(dataset) # Interpolated data - new_lat_res = resolution * abs(dataset.lat.original_scale) - new_lon_res = resolution * abs(dataset.lon.original_scale) - print( - 'Spatial rescaling for lat (resolution: {}),'.format(new_lat_res) + - ' and lon (resolution{})'.format(new_lon_res)) + print('Interpolating with new latitude and longitude.') result_dataset = dataset.interp( - lat=np.linspace( - dataset.lat[0], - dataset.lat[-1], - num=(dataset.lat.size * abs(dataset.lat.original_scale)) / - new_lat_res), - lon=np.linspace( - dataset.lon[0], - dataset.lon[-1], - num=(dataset.lon.size * abs(dataset.lon.original_scale)) / - new_lon_res)) - result_dataset.lat.attrs[OutputData.var_original_scale] = \ - new_lat_res - result_dataset.lon.attrs[OutputData.var_original_scale] = \ - new_lon_res + lat=resample_latitude, + lon=resample_longitude) + DataProcessing.set_original_scale(result_dataset) return result_dataset @staticmethod Index: trunk/tests/test_data_processing.py =================================================================== diff -u -r79 -r82 --- trunk/tests/test_data_processing.py (.../test_data_processing.py) (revision 79) +++ trunk/tests/test_data_processing.py (.../test_data_processing.py) (revision 82) @@ -45,7 +45,7 @@ dir_test_data = TestUtils.get_local_test_data_dir('system_tests_nc') test_nc_file = os.path.join(dir_test_data, 'dummy_hs_non_gridded.nc') assert os.path.exists(test_nc_file), '' + \ - 'Test file not found at {}'.format(test_nc_file) + 'Test file not found at {}'.format(test_nc_file) return xr.open_dataset(test_nc_file) @@ -58,7 +58,7 @@ dir_test_data = TestUtils.get_local_test_data_dir('system_tests_nc') test_nc_file = os.path.join(dir_test_data, 'dummy_hs_gridded.nc') assert os.path.exists(test_nc_file), '' + \ - 'Test file not found at {}'.format(test_nc_file) + 'Test file not found at {}'.format(test_nc_file) return xr.open_dataset(test_nc_file) @@ -97,7 +97,7 @@ # 2. When with pytest.raises(Exception) as e_info: - resampled_data = DataProcessing.time_resampling( + resampled_data = DataProcessing._time_resampling( dataset=dataset, scale=scale, frequency_string=frequency_string @@ -116,7 +116,7 @@ # 2. When try: - resampled_data = DataProcessing.time_resampling( + resampled_data = DataProcessing._time_resampling( dataset=dataset, scale=scale, frequency_string=frequency_string @@ -141,7 +141,7 @@ # 2. When try: - resampled_data = DataProcessing.mean_resampling( + resampled_data = DataProcessing.mean_time_resampling( dataset=dataset, scale=scale, frequency_string=frequency_string @@ -165,7 +165,7 @@ # 2. When try: - resampled_data = DataProcessing.max_resampling( + resampled_data = DataProcessing.max_time_resampling( dataset=dataset, scale=scale, frequency_string=frequency_string @@ -184,7 +184,9 @@ def test_when_mean_resampling_given_file_then_returns_new_xarray(self): # 1. Given dir_test_data = TestUtils.get_local_test_data_dir('system_tests_nc') - test_nc_file = os.path.join(dir_test_data, 'dummy_wave_multiple_file.nc') + test_nc_file = os.path.join( + dir_test_data, + 'dummy_wave_multiple_file.nc') assert os.path.exists(test_nc_file), '' + \ 'Test file not found at {}'.format(test_nc_file) dataset = xr.open_dataset(test_nc_file) @@ -326,6 +328,111 @@ assert resampled_data.lat.size < dataset.lat.size +class Test_SpatialResamplingGrid: + + @pytest.mark.unittest + def test_given_no_dataset_then_raises(self): + # 1. Given + expected_message = om.error_all_arguments_required + resampled_data = None + resample_latitude = xr.DataArray( + [0, 2, 4], + dims=OutputData.var_lat_key) + resample_longitude = xr.DataArray( + [0, 2], + dims=OutputData.var_lon_key) + + # 2. When + with pytest.raises(Exception) as e_info: + resampled_data = DataProcessing.spatial_resampling_grid( + dataset=None, + resample_latitude=resample_latitude, + resample_longitude=resample_longitude) + + # 3. Then + assert not resampled_data + assert str(e_info.value) == expected_message + + @pytest.mark.unittest + def test_given_no_resample_latitude_then_raises(self): + # 1. Given + expected_message = om.error_all_arguments_required + resampled_data = None + dataset = xr.DataArray( + np.sin(0.3 * np.arange(20).reshape(5, 4)), + [ + (OutputData.var_lon_key, np.arange(5)), + (OutputData.var_lat_key, [0.1, 0.2, 0.3, 0.4])]) + resample_longitude = xr.DataArray( + [0, 2], + dims=OutputData.var_lon_key) + # 2. When + with pytest.raises(Exception) as e_info: + resampled_data = DataProcessing.spatial_resampling_grid( + dataset=dataset, + resample_latitude=None, + resample_longitude=resample_longitude) + + # 3. Then + assert not resampled_data + assert str(e_info.value) == expected_message + + @pytest.mark.unittest + def test_given_no_resample_longitude_then_raises(self): + expected_message = om.error_all_arguments_required + resampled_data = None + dataset = xr.DataArray( + np.sin(0.3 * np.arange(20).reshape(5, 4)), + [ + (OutputData.var_lon_key, np.arange(5)), + (OutputData.var_lat_key, [0.1, 0.2, 0.3, 0.4])]) + resample_latitude = xr.DataArray( + [0, 2, 4], + dims=OutputData.var_lat_key) + # 2. When + with pytest.raises(Exception) as e_info: + resampled_data = DataProcessing.spatial_resampling_grid( + dataset=dataset, + resample_latitude=resample_latitude, + resample_longitude=None) + + # 3. Then + assert not resampled_data + assert str(e_info.value) == expected_message + + @pytest.mark.unittest + def test_given_all_args_does_not_raise(self): + # 1. Given + expected_message = om.error_all_arguments_required + resampled_data = None + dataset = xr.DataArray( + np.sin(0.3 * np.arange(20).reshape(5, 4)), + [ + (OutputData.var_lon_key, np.arange(5)), + (OutputData.var_lat_key, [0.1, 0.2, 0.3, 0.4])]) + resample_latitude = xr.DataArray( + [0, 2, 4], + dims=OutputData.var_lat_key) + resample_longitude = xr.DataArray( + [0, 2], + dims=OutputData.var_lon_key) + # 2. When + try: + resampled_data = DataProcessing.spatial_resampling_grid( + dataset=dataset, + resample_latitude=resample_latitude, + resample_longitude=resample_longitude) + except Exception as e_info: + pytest.fail( + 'Exception thrown, but not expected: ' + + '{}'.format(str(e_info)) + ) + + # 3. Then + assert not resampled_data + assert str(e_info.value) == expected_message + + class Test_SpatialGradientsCalculation: @pytest.mark.unittest