Index: trunk/SDToolBox/output_data.py =================================================================== diff -u -r71 -r73 --- trunk/SDToolBox/output_data.py (.../output_data.py) (revision 71) +++ trunk/SDToolBox/output_data.py (.../output_data.py) (revision 73) @@ -33,6 +33,8 @@ dim_station_key = 'station' dim_time_key = 'time' + var_non_gridded = 'non-gridded' + var_gridded = 'gridded' var_station_key = 'station' var_time_key = 'time' var_lat_key = 'lat' @@ -281,7 +283,9 @@ datetime.utcnow(), format='%Y-%m-%dT%H:%MZ') - netcdf.cdm_data_type = 'gridded' + netcdf.cdm_data_type = self.var_non_gridded + if self.is_gridded: + netcdf.cdm_data_type = self.var_gridded def __set_time_and_stations(self, netcdf: Dataset): """Creates the needed header defined by the dataset Index: trunk/tests/test_output_data.py =================================================================== diff -u -r70 -r73 --- trunk/tests/test_output_data.py (.../test_output_data.py) (revision 70) +++ trunk/tests/test_output_data.py (.../test_output_data.py) (revision 73) @@ -155,13 +155,6 @@ os.remove(output_netcdf_filepath) - @pytest.mark.unittest - def test_dummy(self): - x = [4.2, 42, 42, 4.2] - y = [2.4, 2.4, 24, 24] - res = np.meshgrid(x, y, sparse=False, indexing='xy') - assert res is not None - @pytest.mark.systemtest def test_when_given_all_arguments_does_not_raise(self): # 1. Given @@ -176,9 +169,9 @@ input_data.input_years = [1981, 1982] input_data.input_coordinates = [ (4.2, 2.6), - # (2.4, 2.4), - # (42, 24), - # (4.2, 2.4) + (14, 4.2), + (42, 24), + (4.2, 2.4) ] input_data.is_gridded = False Index: trunk/SDToolBox/output_messages.py =================================================================== diff -u -r65 -r73 --- trunk/SDToolBox/output_messages.py (.../output_messages.py) (revision 65) +++ trunk/SDToolBox/output_messages.py (.../output_messages.py) (revision 73) @@ -26,3 +26,4 @@ error_no_gradient_was_calculated = 'No gradient was calculated, ' + \ ' please calculate the gradient on the xarray before proceeding' error_function_not_implemented = 'Functionality not implemented.' +error_only_for_gridded_dataset = 'Functionality only available for gridded datasets.' \ No newline at end of file Index: trunk/SDToolBox/data_processing.py =================================================================== diff -u -r72 -r73 --- trunk/SDToolBox/data_processing.py (.../data_processing.py) (revision 72) +++ trunk/SDToolBox/data_processing.py (.../data_processing.py) (revision 73) @@ -121,28 +121,47 @@ resolution is None: raise Exception(om.error_all_arguments_required) + if not DataProcessing.is_gridded(dataset): + raise Exception(om.error_only_for_gridded_dataset) + DataProcessing.set_original_scale(dataset) # Interpolated data new_lon = np.linspace( dataset.lon[0], dataset.lon[-1], - dataset.dims[OutputData.var_lon_key] * dataset.lon.original_scale * resolution) + dataset.lon.original_scale * resolution) new_lat = np.linspace( dataset.lat[0], dataset.lat[-1], - dataset.dims[OutputData.var_lat_key] * dataset.lat.original_scale * resolution) + dataset.lat.original_scale * resolution) return dataset.interp(lat=new_lat, lon=new_lon) @staticmethod def set_original_scale(dataset: xr.Dataset): if 'original_scale' not in dataset.lon.attrs: - dataset.lon.original_scale = np.diff(dataset.lon[:]).mean() + dataset.lon.attrs['original_scale'] = \ + np.diff(dataset.lon[:]).mean() if 'original_scale' not in dataset.lat.attrs: - dataset.lat.original_scale = np.diff(dataset.lat[:]).mean() + dataset.lat.attrs['original_scale'] = \ + np.diff(dataset.lat[:]).mean() @staticmethod + def is_gridded(dataset: xr.Dataset) -> bool: + """Verifies if the given dataset is of type gridded or not. + + Arguments: + dataset {xr.Dataset} -- xArray with netCDF information. + + Returns: + bool -- Whether the dataset is marked as gridded. + """ + if dataset is None: + return False + return dataset.cdm_data_type == OutputData.var_gridded + + @staticmethod def compute_spatial_gradients(data_array: xr.DataArray): meshed_latitudes, meshed_longitudes = \ Index: trunk/tests/test_data_processing.py =================================================================== diff -u -r72 -r73 --- trunk/tests/test_data_processing.py (.../test_data_processing.py) (revision 72) +++ trunk/tests/test_data_processing.py (.../test_data_processing.py) (revision 73) @@ -211,15 +211,34 @@ class Test_SpatialResampling: @pytest.mark.unittest - def test_when_arguments_given_mean_resample_done(self): + def test_given_non_gridded_dataset_then_resample_raises(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') - assert os.path.exists(test_nc_file), '' + \ - 'Test file not found at {}'.format(test_nc_file) - dataset = xr.open_dataset(test_nc_file) + dataset = get_default_xarray_dataset() + dataset.attrs['cdm_data_type'] = OutputData.var_non_gridded resampled_data = None + expected_message = om.error_only_for_gridded_dataset # 2. When + with pytest.raises(Exception) as e_info: + resampled_data = DataProcessing.spatial_resampling( + dataset=dataset, + resolution=2) + + # 3. Then + assert resampled_data is None + assert expected_message == str(e_info.value) + + @pytest.mark.unittest + def test_given_gridded_dataset_then_resamples_does_not_raise(self): + # 1. Given + dataset = xr.DataArray( + np.sin(0.3 * np.arange(20).reshape(5, 4)), + [ + (OutputData.var_lon_key, np.arange(5)), + (OutputData.var_lat_key, np.arange(4))]) + dataset.attrs['cdm_data_type'] = OutputData.var_gridded + resampled_data = None + + # 2. When try: resampled_data = DataProcessing.spatial_resampling( dataset=dataset, @@ -233,7 +252,6 @@ # 3. Then assert resampled_data is not None - assert len(resampled_data['time'][:]) > len(resampled_data['time'][:]) class TestSpatialGradientsCalculation: