|
| 1 | +"""Tests conversion to GSSHA format.""" |
| 2 | +import xarray_data_accessor as xda |
| 3 | +import xarray as xr |
| 4 | +import pytest |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | + |
| 8 | +@pytest.fixture |
| 9 | +def test_dir() -> Path: |
| 10 | + """Gets the test data directory.""" |
| 11 | + TEST_DIR = Path.cwd() / 'testing/test_data' |
| 12 | + if not TEST_DIR.exists(): |
| 13 | + TEST_DIR = Path.cwd() / 'test_data' |
| 14 | + return TEST_DIR |
| 15 | + |
| 16 | + |
| 17 | +@pytest.fixture |
| 18 | +def test_dataset(test_dir) -> xr.Dataset: |
| 19 | + """Gets the test dataset.""" |
| 20 | + # get test netcdf file into a xr.Dataset |
| 21 | + test_netcdf = test_dir / 'cds_era5_dataset.nc' |
| 22 | + ds = xr.open_dataset(test_netcdf) |
| 23 | + ds = ds.rio.write_crs(ds.attrs['EPSG']) |
| 24 | + return ds |
| 25 | + |
| 26 | + |
| 27 | +def test_factory() -> None: |
| 28 | + """Make sure the function was correctly registered.""" |
| 29 | + assert 'ConvertToGSSHA' in xda.DataConversionFactory.get_converter_classes().keys() |
| 30 | + gssha_obj = xda.DataConversionFactory.get_converter_classes()[ |
| 31 | + 'ConvertToGSSHA'] |
| 32 | + assert isinstance(gssha_obj, object) |
| 33 | + assert issubclass( |
| 34 | + gssha_obj, |
| 35 | + xda.data_converters.base.DataConverterBase, |
| 36 | + ) |
| 37 | + for func_name in gssha_obj.get_conversion_functions().keys(): |
| 38 | + assert func_name in dir(xda.DataConversionFunctions) |
| 39 | + |
| 40 | + |
| 41 | +def test_precipitation_input(test_dataset) -> None: |
| 42 | + out_path = xda.DataConversionFunctions.make_gssha_precipitation_input( |
| 43 | + test_dataset, |
| 44 | + precipitation_variable='2m_temperature', |
| 45 | + ) |
| 46 | + |
| 47 | + assert out_path.exists() |
| 48 | + assert out_path.suffix == '.gag' |
| 49 | + out_path.unlink() |
| 50 | + |
| 51 | + |
| 52 | +def test_to_grass_ascii(test_dataset) -> None: |
| 53 | + |
| 54 | + # test with correct HMET variable |
| 55 | + out_list = xda.DataConversionFunctions.make_gssha_grass_ascii( |
| 56 | + test_dataset, |
| 57 | + variable='2m_temperature', |
| 58 | + hmet_variable='Dry Bulb Temperature', |
| 59 | + start_time=test_dataset.time.values[0], |
| 60 | + end_time=test_dataset.time.values[1], |
| 61 | + ) |
| 62 | + assert isinstance(out_list, list) |
| 63 | + assert len(out_list) == 2 |
| 64 | + for file in out_list: |
| 65 | + assert file.exists() |
| 66 | + assert file.suffix == '.asc' |
| 67 | + file.unlink() |
| 68 | + |
| 69 | + # test with incorrect HMET variable |
| 70 | + |
| 71 | + |
| 72 | +def test_hmet_wes_ascii(test_dataset) -> None: |
| 73 | + |
| 74 | + out_path = xda.DataConversionFunctions.make_gssha_hmet_wes( |
| 75 | + test_dataset, |
| 76 | + variable_to_hmet={'2m_temperature': 'Dry Bulb Temperature'}, |
| 77 | + start_time=test_dataset.time.values[0], |
| 78 | + end_time=test_dataset.time.values[1], |
| 79 | + file_suffix='.test', |
| 80 | + ) |
| 81 | + assert out_path.exists() |
| 82 | + assert out_path.suffix == '.test' |
| 83 | + assert out_path.name.replace('.test', '') == 'hmet_wes' |
| 84 | + out_path.unlink() |
0 commit comments