diff --git a/pysteps/postprocessing/diagnostics.py b/pysteps/postprocessing/diagnostics.py index 426a25f59..a6f9f7c85 100644 --- a/pysteps/postprocessing/diagnostics.py +++ b/pysteps/postprocessing/diagnostics.py @@ -2,13 +2,13 @@ pysteps.postprocessing.diagnostics ==================== -Methods for applying postprocessing. +Methods for diagnostic postprocessing. The methods in this module implement the following interface:: - postprocess_xxx(optional arguments) + diagnostic_xyz(optional arguments) -where **xxx** is the name of the postprocess to be applied. +where **xyz** is the name of the diagnostic postprocessing to be applied. Postprocessor standardizations can be specified here if there is a desired input and output format that all should adhere to. @@ -21,51 +21,10 @@ """ -try: - from osgeo import gdal, gdalconst, osr - GDAL_IMPORTED = True -except ImportError: - GDAL_IMPORTED = False +def diagnostics_example1(filename, **kwargs): + return "Hello, I am an example postprocessor." -try: - import h5py - H5PY_IMPORTED = True -except ImportError: - H5PY_IMPORTED = False - -try: - import metranet - - METRANET_IMPORTED = True -except ImportError: - METRANET_IMPORTED = False - -try: - import netCDF4 - - NETCDF4_IMPORTED = True -except ImportError: - NETCDF4_IMPORTED = False - -try: - from PIL import Image - - PIL_IMPORTED = True -except ImportError: - PIL_IMPORTED = False - -try: - import pyproj - - PYPROJ_IMPORTED = True -except ImportError: - PYPROJ_IMPORTED = False - -try: - import pygrib - - PYGRIB_IMPORTED = True -except ImportError: - PYGRIB_IMPORTED = False +def diagnostics_example2(filename, **kwargs): + return 42 diff --git a/pysteps/postprocessing/interface.py b/pysteps/postprocessing/interface.py index ccd4193ed..422ac78e1 100644 --- a/pysteps/postprocessing/interface.py +++ b/pysteps/postprocessing/interface.py @@ -20,7 +20,9 @@ from pysteps.postprocessing import diagnostics from pprint import pprint -_diagnostics_methods = dict() +_diagnostics_methods = dict( + example1=diagnostics.diagnostics_example1, example3=lambda x: [x, x] +) def discover_diagnostics(): @@ -97,6 +99,8 @@ def diagnostics_info(): available_diagnostics = set(available_diagnostics) diagnostics_in_the_interface = set(diagnostics_in_the_interface) + available_diagnostics = {s.split("_")[1] for s in available_diagnostics} + difference = available_diagnostics ^ diagnostics_in_the_interface if len(difference) > 0: print("\nIMPORTANT:") @@ -145,7 +149,7 @@ def get_method(name, method_type): | Name | Description | +=============+=======================================================+ - method_type: {'diagnostics', diagnostic_diagnostics} + method_type: {'diagnostics', diagnostics_name} Type of the method (see tables above). """ diff --git a/pysteps/tests/test_interfaces.py b/pysteps/tests/test_interfaces.py index c490b2539..bec4338f6 100644 --- a/pysteps/tests/test_interfaces.py +++ b/pysteps/tests/test_interfaces.py @@ -360,3 +360,51 @@ def test_tracking_interface(): invalid_names = ["lucas-kanade", "dating"] _generic_interface_test(method_getter, valid_names_func_pair, invalid_names) + + +def test_postprocessing_interface(): + """Test the postprocessing module interface.""" + + import pysteps.postprocessing as postprocessing + from pysteps.postprocessing import diagnostics_example1 + from pysteps.postprocessing import diagnostics_example2 + + valid_names_func_pair = [("example1", diagnostics_example1)] + + def method_getter(name): + return pysteps.postprocessing.interface.get_method(name, "diagnostics") + + invalid_names = ["bom", "fmi", "knmi", "mch", "mrms", "opera", "saf"] + _generic_interface_test(method_getter, valid_names_func_pair, invalid_names) + + # Test for invalid argument type + with pytest.raises(TypeError): + pysteps.postprocessing.interface.get_method("example1", None) + pysteps.postprocessing.interface.get_method(None, "diagnostics") + + # Test for invalid method types + with pytest.raises(ValueError): + pysteps.postprocessing.interface.get_method("example1", "io") + + with pytest.raises(TypeError): + pysteps.postprocessing.interface.get_method(24, "diagnostics") + + assert isinstance( + pysteps.postprocessing.interface.diagnostics_info()[0], + set, + ) + + assert isinstance( + pysteps.postprocessing.interface.diagnostics_info()[1], + set, + ) + + assert isinstance( + diagnostics_example1(filename="example_filename"), + str, + ) + + assert isinstance( + diagnostics_example2(filename="example_filename"), + int, + )