Index: trunk/SDToolBox/output_messages.py =================================================================== diff -u -r85 -r87 --- trunk/SDToolBox/output_messages.py (.../output_messages.py) (revision 85) +++ trunk/SDToolBox/output_messages.py (.../output_messages.py) (revision 87) @@ -28,4 +28,7 @@ error_function_not_implemented = 'Functionality not implemented.' error_only_for_gridded_dataset = 'Functionality only available for gridded datasets.' error_attribute_not_in_dataset = 'Attribute not in dataset' +error_wrong_format_input_coordinates_LON_missing = 'Input coordinates ' + 'should have a LON key' +error_wrong_format_input_coordinates_LAT_missing = 'Input coordinates should have a LAT key' +error_wrong_format_input_coordinates_not_a_dictionary = 'Input coordinates should be a dictionary' error_pd_input_list_not_valid = 'Input list not valid, available variables: {}' Index: trunk/tests/test_data_processing.py =================================================================== diff -u -r85 -r87 --- trunk/tests/test_data_processing.py (.../test_data_processing.py) (revision 85) +++ trunk/tests/test_data_processing.py (.../test_data_processing.py) (revision 87) @@ -1,6 +1,6 @@ import pytest import os - +from typing import List, Set, Dict, Tuple, Optional from datetime import datetime, timedelta import numpy as np Index: trunk/tests/TestUtils.py =================================================================== diff -u -r2 -r87 --- trunk/tests/TestUtils.py (.../TestUtils.py) (revision 2) +++ trunk/tests/TestUtils.py (.../TestUtils.py) (revision 87) @@ -2,7 +2,7 @@ import os try: from pip import main as pipmain -except: +except Exception: from pip._internal import main as pipmain @@ -51,7 +51,7 @@ try: dir_path = '{}\\{}\\'.format(test_data_name, dir_name) test_dir = os.path.join(test_dir, dir_path) - except: + except Exception: print("An error occurred trying to find {}".format(dir_name)) return test_dir Index: trunk/tests/test_input_data.py =================================================================== diff -u -r86 -r87 --- trunk/tests/test_input_data.py (.../test_input_data.py) (revision 86) +++ trunk/tests/test_input_data.py (.../test_input_data.py) (revision 87) @@ -17,7 +17,17 @@ @pytest.mark.unittest def test_when_create_empty_dataacquistion_no_exception_risen(self): try: - InputData() + # 1. Given + input_variables = ['du', 'du', 'mmy'] + input_coordinates = {'LON': [4.2, 42], 'LAT': [4.5, 3.3]} + input_years = [1989, 1990, 1988, 1989] + + # 2. When + input_data = InputData( + input_coordinates=input_coordinates, + input_variables=input_variables, + input_years=input_years + ) except Exception as e_info: err_mssg = 'Error while creating Input data object.' + \ '{}'.format(str(e_info)) @@ -27,7 +37,7 @@ def test_when_given_args_sets_them(self): # 1. Given input_variables = ['du', 'du', 'mmy'] - input_coordinates = [(42, 4.2)] + input_coordinates = {'LON': [4.2, 42], 'LAT': [4.5, 3.3]} input_years = [1989, 1990, 1988, 1989] # 2. When @@ -52,10 +62,17 @@ [(None), ([])]) def test_when_no_valid_input_years_raises(self, input_years: list): # 1. Given - input_data = InputData() - input_data.input_coordinates = [(42, 4.2)] - input_data.input_variables = ['swh'] - input_data.input_years = input_years + input_coordinates = {'LON': [4.2, 42], 'LAT': [4.5, 3.3]} + input_variables = ['swh'] + + is_gridded = False + input_scenarios = [] + input_data = InputData(input_coordinates, + input_variables, + input_scenarios, + input_years, + is_gridded) + validation_result = None expected_message = om.error_no_valid_list_of_years @@ -74,10 +91,16 @@ def test_given_no_input_variables_exception_risen( self, input_variables: list): # 1. Given - input_data = InputData() - input_data.input_variables = input_variables - input_data.input_coordinates = [(42, 4.2)] - input_data.input_years = [1942, 1924] + input_coordinates = {'LON': [4.2, 42], 'LAT': [4.5, 3.3]} + input_years = [2011] + is_gridded = False + input_scenarios = ["sc1"] + input_data = InputData(input_coordinates, + input_variables, + input_scenarios, + input_years, + is_gridded) + validation_result = None expected_message = om.error_no_valid_list_of_vars @@ -96,16 +119,21 @@ def test_given_no_input_coordinates_exception_risen( self, input_coordinates: list): # 1. Given - input_data = InputData() - input_data.input_coordinates = input_coordinates - input_data.input_years = [2014] - input_data.input_variables = ['swh'] + input_years = [2014] + input_variables = ['swh'] + is_gridded = False + input_scenarios = ["sc1", "sc2"] + validation_result = None expected_message = om.error_not_enough_coordinates # 2. When - with pytest.raises(IOError) as e_info: - validation_result = input_data.validate() + with pytest.raises(Exception) as e_info: + input_data = InputData(input_coordinates, + input_variables, + input_scenarios, + input_years, + is_gridded) # 3. Then assert validation_result is None @@ -114,10 +142,18 @@ @pytest.mark.unittest def test_when_valid_args_then_returns_true(self): # 1. Given - input_data = InputData() - input_data.input_coordinates = [(42, 4.2)] - input_data.input_variables = ['swh'] - input_data.input_years = [1942] + + input_coordinates = {'LON': [42, 4.2], 'LAT': [24, 2.4]} + input_variables = ['swh'] + input_scenarios = ["sc1", "sc2"] + input_years = [1942] + is_gridded = False + input_data = InputData(input_coordinates, + input_variables, + input_scenarios, + input_years, + is_gridded) + validation_result = None # 2. When @@ -134,10 +170,17 @@ @pytest.mark.integrationtest def test_given_valid_args_then_removes_duplicates(self): # 1. Given - input_data = InputData() - input_data.input_variables = ['du', 'du', 'mmy'] - input_data.input_coordinates = [(42, 4.2)] - input_data.input_years = [1989, 1990, 1988, 1989] + input_coordinates = {'LON': [42, 4.2], 'LAT': [24, 2.4]} + input_variables = ['du', 'mmy'] + input_scenarios = ["sc1", "sc2"] + input_years = [1989, 1990, 1988, 1989] + is_gridded = False + input_data = InputData(input_coordinates, + input_variables, + input_scenarios, + input_years, + is_gridded) + expected_input_variables = ['du', 'mmy'] expected_input_years = [1988, 1989, 1990] validation_result = None @@ -164,8 +207,16 @@ [(None), ([])]) def test_when_no_valid_input_years_raises(self, input_years: list): # 1. Given - input_data = InputData() - input_data.input_years = input_years + input_coordinates = {'LON': [42, 4.2], 'LAT': [24, 2.4]} + input_variables = ['du', 'mmy'] + input_scenarios = ["sc1", "sc2"] + + is_gridded = False + input_data = InputData(input_coordinates, + input_variables, + input_scenarios, + input_years, + is_gridded) validation_result = None expected_message = om.error_no_valid_list_of_years @@ -180,8 +231,16 @@ @pytest.mark.unittest def test_when_unordered_input_years_then_orders_it(self): # 1. Given - input_data = InputData() - input_data.input_years = [1989, 1990, 1980] + input_coordinates = {'LON': [42, 4.2], 'LAT': [24, 2.4]} + input_variables = ['dummy'] + input_scenarios = ["sc1", "sc2"] + input_years = [1989, 1990, 1980] + is_gridded = False + input_data = InputData(input_coordinates, + input_variables, + input_scenarios, + input_years, + is_gridded) expected_input_years = [1980, 1989, 1990] validation_result = None @@ -200,8 +259,17 @@ @pytest.mark.unittest def test_when_input_years_with_repeated_years_then_removes_them(self): # 1. Given - input_data = InputData() - input_data.input_years = [1990, 1989, 1989] + input_coordinates = {'LON': [42, 4.2], 'LAT': [24, 2.4]} + input_variables = ['dummy'] + input_scenarios = ["sc1", "sc2"] + input_years = [1990, 1989, 1989] + is_gridded = False + input_data = InputData(input_coordinates, + input_variables, + input_scenarios, + input_years, + is_gridded) + expected_input_years = [1989, 1990] validation_result = None @@ -220,8 +288,17 @@ @pytest.mark.unittest def test_when_valid_input_years_returns_true(self): # 1. Given - input_data = InputData() - input_data.input_years = [1942] + input_coordinates = {'LON': [42, 4.2], 'LAT': [24, 2.4]} + input_variables = ['dummy'] + input_scenarios = ["sc1", "sc2"] + input_years = [1992, 1290] + is_gridded = False + input_data = InputData(input_coordinates, + input_variables, + input_scenarios, + input_years, + is_gridded) + validation_result = None # 2. When @@ -239,7 +316,7 @@ def test_input_years_as_np_array_converts_to_years(self): # 1. Given input_variables = ['du', 'du', 'mmy'] - input_coordinates = [(42, 4.2)] + input_coordinates = {'LON': [4.2, 42], 'LAT': [4.5, 3.3]} input_years = np.array([1989, 1990, 1988, 1989]) # 2. When @@ -263,7 +340,16 @@ def test_given_no_input_variables_exception_risen( self, input_variables: list): # 1. Given - input_data = InputData() + input_coordinates = {'LON': [42, 4.2], 'LAT': [24, 2.4]} + + input_scenarios = ["sc1", "sc2"] + input_years = [1989, 1990, 1988, 1989] + is_gridded = False + input_data = InputData(input_coordinates, + input_variables, + input_scenarios, + input_years, + is_gridded) input_data.input_variables = input_variables validation_result = None expected_message = om.error_no_valid_list_of_vars @@ -279,8 +365,18 @@ @pytest.mark.unittest def test_given_input_variables_returns_true(self): # 1. Given - input_data = InputData() - input_data.input_variables = ['dummy'] + + input_coordinates = {'LON': [42, 4.2], 'LAT': [24, 2.4]} + input_variables = ['dummy'] + input_scenarios = ["sc1", "sc2"] + input_years = [1992, 1290] + is_gridded = False + input_data = InputData(input_coordinates, + input_variables, + input_scenarios, + input_years, + is_gridded) + validation_result = None # 2. When @@ -297,8 +393,16 @@ @pytest.mark.unittest def test_given_input_variables_then_removes_duplicates(self): # 1. Given - input_data = InputData() - input_data.input_variables = ['du', 'du', 'mmy'] + input_coordinates = {'LON': [42, 4.2], 'LAT': [24, 2.4]} + input_variables = ['DU', 'MMY'] + input_scenarios = ["sc1", "sc2"] + input_years = [1992, 1290] + is_gridded = False + input_data = InputData(input_coordinates, + input_variables, + input_scenarios, + input_years, + is_gridded) expected_input_variables = ['du', 'mmy'] validation_result = None @@ -318,8 +422,16 @@ @pytest.mark.unittest def test_given_lowercase_vars_then_makes_them_upper(self): # 1. Given - input_data = InputData() - input_data.input_variables = ['DU', 'MMY'] + input_coordinates = {'LON': [42, 4.2], 'LAT': [24, 2.4]} + input_variables = ['DU', 'MMY'] + input_scenarios = ["sc1", "sc2"] + input_years = [1992, 1290] + is_gridded = False + input_data = InputData(input_coordinates, + input_variables, + input_scenarios, + input_years, + is_gridded) expected_input_variables = ['du', 'mmy'] validation_result = None @@ -345,24 +457,39 @@ def test_given_no_input_coordinates_exception_risen( self, input_coordinates: list): # 1. Given - input_data = InputData() - input_data.input_coordinates = input_coordinates + input_variables = ['du', 'mmy'] + input_scenarios = ["sc1", "sc2"] + input_years = [1989, 1990, 1988, 1989] + is_gridded = False + validation_result = None expected_message = om.error_not_enough_coordinates # 2. When - with pytest.raises(IOError) as e_info: - validation_result = input_data.validate_input_coordinates() + with pytest.raises(Exception) as e_info: + input_data = InputData(input_coordinates, + input_variables, + input_scenarios, + input_years, + is_gridded) # 3. Then - assert validation_result is None assert expected_message == str(e_info.value) @pytest.mark.unittest def test_given_input_coordinates_returns_true(self): # 1. Given - input_data = InputData() - input_data.input_coordinates = [(4.2, 42)] + input_coordinates = {'LON': [42, 4.2], 'LAT': [24, 2.4]} + input_variables = ["var1", "var2"] + input_scenarios = ["sc1", "sc2"] + input_years = [1992, 1290] + is_gridded = False + input_data = InputData(input_coordinates, + input_variables, + input_scenarios, + input_years, + is_gridded) + validation_result = None # 2. When @@ -379,11 +506,10 @@ @pytest.mark.unittest def test_given_wrong_format_input_coords_raise_exception(self): # 1. Given - input_coordinates = [(4.2, 42)] + input_coordinates = [3, 4, 5] input_variables = ["var1", "var2"] input_scenarios = ["sc1", "sc2"] input_years = [1992, 1290] - input_coordinates = [(4.2, 42)] is_gridded = False expected_message = \ om.error_wrong_format_input_coordinates_not_a_dictionary @@ -401,11 +527,10 @@ @pytest.mark.unittest def test_given_wrong_format_input_coords_no_LON_key_raise_exception(self): # 1. Given - input_coordinates = {'LAN': [4.2, 42], 'LAT': [4.5, 3.3]} + input_coordinates = {'LAN': [4.2, 42], 'LAT': [4.5, 3.3]} # wrong LON key input_variables = ["var1", "var2"] input_scenarios = ["sc1", "sc2"] input_years = [1992, 1290] - input_coordinates = [(4.2, 42)] is_gridded = False expected_message = \ om.error_wrong_format_input_coordinates_LON_missing @@ -420,15 +545,43 @@ assert expected_message == str(e_info.value) + @pytest.mark.unittest + def test_given_wrong_format_input_coords_no_LAT_key_raise_exception(self): + # 1. Given + input_coordinates = {'LON': [4.2, 42], 'LAR': [4.5, 3.3]} # wrong LAT key + input_variables = ["var1", "var2"] + input_scenarios = ["sc1", "sc2"] + input_years = [1992, 1290] + # input_coordinates = [(4.2, 42)] + is_gridded = False + expected_message = \ + om.error_wrong_format_input_coordinates_LAT_missing + # 2. When + with pytest.raises(Exception) as e_info: + input_data = InputData(input_coordinates, + input_variables, + input_scenarios, + input_years, + is_gridded) + assert expected_message == str(e_info.value) + @pytest.mark.integrationtest def test_given_valid_args_extracts_lon_lat(self): # 1. Given - input_data = InputData() - input_data.input_coordinates = [(42, 4.2), (24, 2.4)] - expected_lon = [42, 24] - expected_lat = [4.2, 2.4] + input_coordinates = {'LON': [42, 4.2], 'LAT': [24, 2.4]} + input_variables = ["var1", "var2"] + input_scenarios = ["sc1", "sc2"] + input_years = [1992, 1290] + is_gridded = False + input_data = InputData(input_coordinates, + input_variables, + input_scenarios, + input_years, + is_gridded) + expected_lon = [42, 4.2] + expected_lat = [24, 2.4] validation_result = None # 2. When @@ -451,8 +604,16 @@ @pytest.mark.integrationtest def test_given_no_max_min_lon_when_validate_then_raises(self): # 1. Given - input_data = InputData() - input_data.input_coordinates = [(42, 4.2), (24, 2.4)] + input_coordinates = {'LON': [42, 4.2], 'LAT': [24, 2.4]} + input_variables = ["var1", "var2"] + input_scenarios = ["sc1", "sc2"] + input_years = [1992, 1290] + is_gridded = False + input_data = InputData(input_coordinates, + input_variables, + input_scenarios, + input_years, + is_gridded) input_data.max_longitude = None validation_result = None expected_message = om.error_max_lon_not_set Index: trunk/SDToolBox/input_data.py =================================================================== diff -u -r86 -r87 --- trunk/SDToolBox/input_data.py (.../input_data.py) (revision 86) +++ trunk/SDToolBox/input_data.py (.../input_data.py) (revision 87) @@ -60,6 +60,12 @@ is_gridded {bool} -- Coordinates represent a grid. (default: {False}) """ + if input_coordinates is None: + raise Exception( + om.error_not_enough_coordinates) + if not input_coordinates: + raise Exception( + om.error_not_enough_coordinates) if not isinstance(input_coordinates, dict): raise Exception( om.error_wrong_format_input_coordinates_not_a_dictionary) @@ -168,7 +174,8 @@ input_coordinates and corrects the longitude if needed. In case of a gridded input, we only get """ - self._input_lon, self._input_lat = zip(*self.input_coordinates) + self._input_lon = self.input_coordinates['LON'] + self._input_lat = self.input_coordinates['LAT'] self._input_lon = [ self.__get_corrected_longitude(lon) for lon in self._input_lon] Index: trunk/tests/test_output_data.py =================================================================== diff -u -r79 -r87 --- trunk/tests/test_output_data.py (.../test_output_data.py) (revision 79) +++ trunk/tests/test_output_data.py (.../test_output_data.py) (revision 87) @@ -207,17 +207,18 @@ input_data = InputData() input_data.input_variables = ['swh'] - input_data.input_coordinates = [ - (4.2, 2.4), - (2.5, 42), - (4.2, 3.6)] + input_data.input_years = [1981, 1982] - input_data.input_coordinates = [ + input_data.input_coordinates = {'LON': [ (4.2, 2.6), (14, 4.2), (42, 24), (4.2, 2.4) - ] + ], 'LAT': [(4.2, 2.6), + (14, 4.2), + (42, 24), + (4.2, 2.4)]} + input_data.is_gridded = False # 2. When