Index: trunk/tests/test_output_data.py =================================================================== diff -u -r127 -r137 --- trunk/tests/test_output_data.py (.../test_output_data.py) (revision 127) +++ trunk/tests/test_output_data.py (.../test_output_data.py) (revision 137) @@ -17,68 +17,95 @@ class Test_jupyter_workflows: - @pytest.mark.systemtest def test_extract_non_gridded_then_gridded(self): self.test_extract_nongridded_data() self.test_extract_gridded_data() @pytest.mark.systemtest def test_extract_nongridded_data(self): - acceptance_folder = TestUtils.get_local_test_data_dir('acceptance_tests\\data') - DS = Dataset(os.path.join(acceptance_folder, 'NS_WAM_ERA5.nc')) - assert DS - lonWAM = DS.variables['lon'][:] - latWAM = DS.variables['lat'][:] + coordsWAM = { + "LAT": [ + 51.5, + 51.7, + 51.9, + 52.1, + 52.3, + 52.6, + 52.5, + 52.8, + 52.9, + 53.1, + 53.3, + 53.4, + 53.5, + 53.6, + 53.6, + 53.7, + 53.8, + ], + "LON": [ + 3.0, + 3.3, + 3.7, + 4.0, + 4.1, + 4.3, + 4.3, + 4.4, + 4.5, + 4.6, + 4.7, + 4.9, + 5.2, + 5.3, + 5.6, + 5.9, + 6.2, + 6.4, + 6.7, + ], + } + varWAM = ["swh", "pp1d", "mp1", "mwd", "wdw"] + timeWAMy = [1981] # 1987 to 2018 - coordsWAM = {'LON': [], 'LAT': []} - for i, j in zip(lonWAM, latWAM): - coordsWAM['LON'].append(i) - coordsWAM['LAT'].append(j) - - # set other data variables - variables = [] # saved names of the entries - for i in DS.variables: - print(i, DS.variables[i].shape) - variables.append(i) - varWAM = [v.lower() for v in variables[5:]] - timeWAMy = [1981] # 1987 to 2018 - # use the SDToolBox function to create input data Input_DataWAM = InputData( input_coordinates=coordsWAM, input_variables=varWAM, - input_scenarios=['era5'], - input_years=timeWAMy) # default is_gridded is false, referring to points + input_scenarios=["era5"], + input_years=timeWAMy, + ) # default is_gridded is false, referring to points # use the SDToolBox function to extract data # dir_test_data = 'P:\\metocean-data\\open\\ERA5\\data\\Global' - dir_test_data = TestUtils.get_local_test_data_dir('era5_test_data') + dir_test_data = TestUtils.get_local_test_data_dir("era5_test_data") extracted_data = ExtractData.get_era_5(dir_test_data, Input_DataWAM) assert extracted_data is not None @pytest.mark.systemtest def test_extract_gridded_data(self): - steplon = 1 # degrees - lonl = -5 # left lon value - lonr = 5 # right lon value + steplon = 1 # degrees + lonl = -5 # left lon value + lonr = 5 # right lon value steplat = 1 - latl = 50 # lower lat value - latu = 60 # upper lat value - xrang = np.arange(lonl, lonr+steplon, steplon).tolist() - yrang = np.arange(latl, latu+steplat, steplat).tolist() + latl = 50 # lower lat value + latu = 60 # upper lat value + xrang = np.arange(lonl, lonr + steplon, steplon).tolist() + yrang = np.arange(latl, latu + steplat, steplat).tolist() - coordsBOX = {'LON': xrang, 'LAT': yrang} - timeWAMy = [1981] # 1987 to 2018 + coordsBOX = {"LON": xrang, "LAT": yrang} + timeWAMy = [1981] # 1987 to 2018 # use the SDToolBox function to create input data Input_DataBOX = InputData( input_coordinates=coordsBOX, - input_variables=['msl'], - input_scenarios=['era5'], + input_variables=["msl"], + input_scenarios=["era5"], input_years=timeWAMy, - is_gridded=True) + is_gridded=True, + ) - dir_test_data = Path(TestUtils.get_local_test_data_dir('chunked_data')) + dir_test_data = Path(TestUtils.get_local_test_data_dir("chunked_data")) extracted_data = ExtractData.get_era_5(dir_test_data, Input_DataBOX) assert extracted_data is not None @@ -91,37 +118,35 @@ steplat = 1 latl = 50 latu = 60 - xrang = np.arange(lonl, lonr+steplon, steplon).tolist() - yrang = np.arange(latl, latu+steplat, steplat).tolist() + xrang = np.arange(lonl, lonr + steplon, steplon).tolist() + yrang = np.arange(latl, latu + steplat, steplat).tolist() - coordsBOX = {'LON': xrang, 'LAT': yrang} - timeWAMy = [1981] # 1987 to 2018 - variables = ['swh'] + coordsBOX = {"LON": xrang, "LAT": yrang} + timeWAMy = [1981] # 1987 to 2018 + variables = ["swh"] # use the SDToolBox function to create input data Input_DataBOX = InputData( input_coordinates=coordsBOX, input_variables=variables, - input_scenarios=['era5'], + input_scenarios=["era5"], input_years=timeWAMy, - is_gridded=True) + is_gridded=True, + ) - dir_test_data = Path(TestUtils.get_local_test_data_dir('chunked_data')) + dir_test_data = Path(TestUtils.get_local_test_data_dir("chunked_data")) # Extract Data extracted_data = ExtractData.get_era_5(dir_test_data, Input_DataBOX) assert extracted_data is not None # Generate Output - output_dir = TestUtils.get_local_test_data_dir('output_data') + output_dir = TestUtils.get_local_test_data_dir("output_data") era5_box = OutputData.generate_netcdf( - extracted_data, - dir_path=output_dir, - base_name='BOX', - dataset_code='ERA5') + extracted_data, dir_path=output_dir, base_name="BOX", dataset_code="ERA5" + ) assert era5_box is not None class Test_create: - @pytest.mark.unittest def test_when_no_list_of_variables_given_then_exception_not_risen(self): # 1. Given @@ -133,8 +158,7 @@ output_data = OutputData(None) data_result = output_data.data_dict except Exception as e_info: - pytest.fail( - 'Exception thrown but not expected. {}'.format(str(e_info))) + pytest.fail("Exception thrown but not expected. {}".format(str(e_info))) # 3. Then assert data_result is not None assert data_result[output_data.var_time_key] is not None @@ -145,15 +169,14 @@ @pytest.mark.unittest def test_when_list_of_variables_given_then_sets_data_dict(self): # 1. Given. - var_list = ['dum', 'my'] + var_list = ["dum", "my"] data_result = [] # 2. When try: output_data = OutputData(var_list) data_result = output_data.data_dict except Exception as e_info: - pytest.fail( - 'Exception thrown but not expected. {}'.format(str(e_info))) + pytest.fail("Exception thrown but not expected. {}".format(str(e_info))) # 3. Then assert data_result is not None @@ -167,36 +190,39 @@ class Test_generate_era5_netcdf: - @pytest.mark.systemtest def test_when_is_non_gridded_given_valid_input_generates_output(self): # 1. Given # When using local data you can just replace the comment in these lines - dir_test_data = Path(TestUtils.get_local_test_data_dir('chunked_data')) - output_test_data = TestUtils.get_local_test_data_dir('output_data') + dir_test_data = Path(TestUtils.get_local_test_data_dir("chunked_data")) + output_test_data = TestUtils.get_local_test_data_dir("output_data") 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_variables = ["swh"] + lonmin = -5 + lonmax = 11 + dlon = 0.5 + latmin = 50 + latmax = 62 + dlat = 0.5 + lon = np.arange(latmin, latmax + dlat, dlat) + lat = np.arange(lonmin, lonmax + dlon, dlon) + input_data.input_coordinates = {"LON": lon, "LAT": lat} input_data.is_gridded = False input_data.input_years = [1981, 1982] netcdf_filepath = None # 2. When try: output_data = ExtractData.get_era_5( - directory_path=dir_test_data, - input_data=input_data) + directory_path=dir_test_data, input_data=input_data + ) netcdf_filepath = output_data.generate_netcdf( dir_path=output_test_data, - base_name='test_non_gridded', - dataset_code=None + base_name="test_non_gridded", + dataset_code=None, ) except Exception as e_info: - pytest.fail( - 'Exception thrown but not expected. {}'.format(str(e_info))) + pytest.fail("Exception thrown but not expected. {}".format(str(e_info))) # 3. Then assert netcdf_filepath is not None @@ -207,31 +233,27 @@ def test_when_is_gridded_given_valid_input_generates_output(self): # 1. Given # When using local data you can just replace the comment in these lines - dir_test_data = Path(TestUtils.get_local_test_data_dir('chunked_data')) - output_test_data = TestUtils.get_local_test_data_dir('output_data') + dir_test_data = Path(TestUtils.get_local_test_data_dir("chunked_data")) + output_test_data = TestUtils.get_local_test_data_dir("output_data") 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_variables = ["swh"] + input_data.input_coordinates = [(4.2, 2.4), (2.5, 42), (4.2, 3.6)] input_data.is_gridded = True input_data.input_years = [1981, 1982] netcdf_filepath = None # 2. When try: output_data = ExtractData.get_era_5( - directory_path=dir_test_data, - input_data=input_data) + directory_path=dir_test_data, input_data=input_data + ) netcdf_filepath = output_data.generate_netcdf( dir_path=output_test_data, - base_name='test_non_gridded', - dataset_code=None + base_name="test_non_gridded", + dataset_code=None, ) except Exception as e_info: - pytest.fail( - 'Exception thrown but not expected. {}'.format(str(e_info))) + pytest.fail("Exception thrown but not expected. {}".format(str(e_info))) # 3. Then assert netcdf_filepath is not None @@ -240,13 +262,14 @@ class Test_generate_output: - @pytest.mark.unittest def test_when_no_arguments_given_no_valid_data_dict_then_raises(self): # 1. Given input_data = InputData() - input_data.input_variables = ['swh'] - input_data.input_coordinates = [(4.2, 2.4), ] + input_data.input_variables = ["swh"] + input_data.input_coordinates = [ + (4.2, 2.4), + ] input_data.input_years = [1981, 1982] output_data = None generated_output = None @@ -266,23 +289,24 @@ # 1. Given # When using local data you can just replace the comment in these lines # dir_test_data = \ - dir_test_data = Path(TestUtils.get_local_test_data_dir('chunked_data')) + dir_test_data = Path(TestUtils.get_local_test_data_dir("chunked_data")) input_data = InputData() - input_data.input_variables = ['swh'] - input_data.input_coordinates = [(4.2, 2.4), ] + input_data.input_variables = ["swh"] + input_data.input_coordinates = [ + (4.2, 2.4), + ] input_data.input_years = [1981, 1982] return_value = None # 2. When try: output_data = ExtractData.get_era_5( - directory_path=dir_test_data, - input_data=input_data) + directory_path=dir_test_data, input_data=input_data + ) return_value = output_data.generate_output() except Exception as e_info: - pytest.fail( - 'Exception thrown but not expected. {}'.format(str(e_info))) + pytest.fail("Exception thrown but not expected. {}".format(str(e_info))) # 3. Then output_xarray = output_data.get_xarray() @@ -299,38 +323,30 @@ def test_when_given_all_arguments_does_not_raise(self): # 1. Given # When using local data you can just replace the comment in these lines - dir_test_data = Path(TestUtils.get_local_test_data_dir('chunked_data')) - output_test_data = TestUtils.get_local_test_data_dir('output_data') + dir_test_data = Path(TestUtils.get_local_test_data_dir("chunked_data")) + output_test_data = TestUtils.get_local_test_data_dir("output_data") input_data = InputData() - input_data.input_variables = ['swh'] + input_data.input_variables = ["swh"] input_data.input_years = [1981, 1982] - 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.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 try: output_data = ExtractData.get_era_5( - directory_path=dir_test_data, - input_data=input_data) + directory_path=dir_test_data, input_data=input_data + ) output_data.generate_output( - dir_path=output_test_data, - base_name='test_wave', - dataset_code=None + dir_path=output_test_data, base_name="test_wave", dataset_code=None ) except Exception as e_info: - pytest.fail( - 'Exception thrown but not expected. {}'.format(str(e_info))) + pytest.fail("Exception thrown but not expected. {}".format(str(e_info))) # 3. Then output_xarray = output_data.get_xarray() @@ -344,21 +360,19 @@ class Test_generate_xarray_from_netcdf: - @pytest.mark.systemtest def test_when_given_netcdf_filepath_updates_output(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_file.nc') + dir_test_data = TestUtils.get_local_test_data_dir("system_tests_nc") + test_nc_file = os.path.join(dir_test_data, "dummy_wave_file.nc") assert os.path.exists(test_nc_file) # 2. When try: output_data = OutputData(None) output_data.generate_xarray_from_netcdf(test_nc_file) except Exception as e_info: - pytest.fail( - 'Exception thrown but not expected. {}'.format(str(e_info))) + pytest.fail("Exception thrown but not expected. {}".format(str(e_info))) # 3. Then output_xarray = output_data.get_xarray() Index: trunk/tests/test_extract_data.py =================================================================== diff -u -r136 -r137 --- trunk/tests/test_extract_data.py (.../test_extract_data.py) (revision 136) +++ trunk/tests/test_extract_data.py (.../test_extract_data.py) (revision 137) @@ -22,11 +22,20 @@ dir_test_data = Path(TestUtils.get_local_test_data_dir("chunked_data")) input_data = InputData() + lonmin = -5 + lonmax = 11 + dlon = 0.5 + latmin = 50 + latmax = 62 + dlat = 0.5 + lon = np.arange(latmin, latmax + dlat, dlat) + lat = np.arange(lonmin, lonmax + dlon, dlon) + input_data.input_variables = ["swh"] input_data.input_years = [1981, 1982] input_data.input_coordinates = { - input_data.longitude_key: [4.2, 42, 42, 4.2], - input_data.latitude_key: [2.4, 2.4, 24, 24], + input_data.longitude_key: lon, + input_data.latitude_key: lat, } # 2. When @@ -57,7 +66,15 @@ input_data = InputData() input_data.input_variables = input_variables - input_data.input_coordinates = {"LAT": [4.2], "LON": [2.4]} + lonmin = -5 + lonmax = 11 + dlon = 0.5 + latmin = 50 + latmax = 62 + dlat = 0.5 + lon = np.arange(latmin, latmax + dlat, dlat) + lat = np.arange(lonmin, lonmax + dlon, dlon) + input_data.input_coordinates = {"LAT": lat[1:2], "LON": lon[1:2]} input_data.input_years = [1981, 1982] # 2. When