<commit_before># -*- coding: utf-8 -*-

import os

import pytest

import pysteps
from pysteps.tests.helpers import smart_assert

try:
    import h5py

    h5py_imported = True
except ImportError:
    h5py_imported = False

if h5py_imported:

    def test_io_import_knmi_hdf5_shape():
        """Test the importer KNMI HDF5."""
        root_path = pysteps.rcparams.data_sources["knmi"]["root_path"]
        filename = os.path.join(root_path, "2010/08",
                                "RAD_NL25_RAP_5min_201008260000.h5")
        R, _, _ = pysteps.io.import_knmi_hdf5(filename)
        assert R.shape == (765, 700)


    # test_metadata: list of (variable,expected, tolerance) tuples

    expected_proj1 = ('+proj=stere +lat_0=90 +lon_0=0.0 '
                      '+lat_ts=60.0 +a=6378.137 +b=6356.752 +x_0=0 '
                      '+y_0=0')

    test_metadata = [
        ('projection', expected_proj1, None),
        ('x1', 0.0, 1e-10),
        ('y1', -3649950.360247753, 1e-10),
        ('x2', 699984.2646331593, 1e-10),
        ('y2', -4415038.179210632, 1e-10),
        ('xpixelsize', 1000.0, 1e-10),
        ('xpixelsize', 1000.0, 1e-10),
        ('accutime', 5.0, 1e-10),
        ('yorigin', 'upper', None),
        ('unit', 'mm', None),
        ('institution', 'KNMI - Royal Netherlands Meteorological Institute', None),
        ('transform', None, None),
        ('zerovalue', 0.0, 1e-10),
        ('threshold', 0.01, 1e-10),
    ]


    @pytest.mark.parametrize("variable,expected,tolerance", test_metadata)
    def test_io_import_knmi_hdf5_metadata(variable, expected, tolerance):
        """Test the importer KNMI HDF5."""
        root_path = pysteps.rcparams.data_sources["knmi"]["root_path"]
        filename = os.path.join(root_path, "2010/08",
                                "RAD_NL25_RAP_5min_201008260000.h5")
        _, _, metadata = pysteps.io.import_knmi_hdf5(filename)
        smart_assert(metadata[variable], expected, tolerance)<commit_msg>Skip test function using pytest.skip<commit_after># -*- coding: utf-8 -*-

import os
import pytest

import pysteps
from pysteps.tests.helpers import smart_assert

try:
    import h5py
except ImportError:
    pytest.skip("io_import_knmi_hdf5 tests. h5py library not present", allow_module_level=True)


def test_io_import_knmi_hdf5_shape():
    """Test the importer KNMI HDF5."""

    root_path = pysteps.rcparams.data_sources["knmi"]["root_path"]

    filename = os.path.join(root_path, "2010/08",
                            "RAD_NL25_RAP_5min_201008260000.h5")

    precip, __, __ = pysteps.io.import_knmi_hdf5(filename)

    assert precip.shape == (765, 700)


# test_metadata: list of (variable,expected, tolerance) tuples

expected_proj1 = ('+proj=stere +lat_0=90 +lon_0=0.0 '
                  '+lat_ts=60.0 +a=6378.137 +b=6356.752 +x_0=0 '
                  '+y_0=0')

test_metadata = [
    ('projection', expected_proj1, None),
    ('x1', 0.0, 1e-10),
    ('y1', -3649950.360247753, 1e-10),
    ('x2', 699984.2646331593, 1e-10),
    ('y2', -4415038.179210632, 1e-10),
    ('xpixelsize', 1000.0, 1e-10),
    ('xpixelsize', 1000.0, 1e-10),
    ('accutime', 5.0, 1e-10),
    ('yorigin', 'upper', None),
    ('unit', 'mm', None),
    ('institution', 'KNMI - Royal Netherlands Meteorological Institute', None),
    ('transform', None, None),
    ('zerovalue', 0.0, 1e-10),
    ('threshold', 0.01, 1e-10),
]


@pytest.mark.parametrize("variable,expected,tolerance", test_metadata)
def test_io_import_knmi_hdf5_metadata(variable, expected, tolerance):
    """Test the importer KNMI HDF5."""
    root_path = pysteps.rcparams.data_sources["knmi"]["root_path"]

    filename = os.path.join(root_path, "2010/08",
                            "RAD_NL25_RAP_5min_201008260000.h5")

    __, __, metadata = pysteps.io.import_knmi_hdf5(filename)

    smart_assert(metadata[variable], expected, tolerance)
{'fraction_non_alphanumeric': 0.15073815073815075, 'fraction_numerical': 0.09712509712509712, 'mean_word_length': 2.948875255623722, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2628789', 'n_tokens_mistral': 1658, 'n_tokens_neox': 1418, 'n_words': 258}