# -*- coding: utf-8 -*-

import os

import pytest

import pysteps
from pysteps.tests.helpers import smart_assert

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


def test_io_import_opera_hdf5_shape():
    """Test the importer OPERA HDF5."""
    root_path = pysteps.rcparams.data_sources["opera"]["root_path"]
    filename = os.path.join(root_path, "20180824",
                            "T_PAAH21_C_EUOC_20180824180000.hdf")
    R, _, _ = pysteps.io.import_opera_hdf5(filename)
    assert R.shape == (2200, 1900)


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

expected_proj1 = ('+proj=laea +lat_0=55.0 +lon_0=10.0 '
                  '+x_0=1950000.0 '
                  '+y_0=-2100000.0 '
                  '+units=m +ellps=WGS84'
                  )

test_metadata = [
    ('projection', expected_proj1, None),
    ('ll_lon', -10.434576838640398, 1e-10),
    ('ll_lat', 31.746215319325056, 1e-10),
    ('ur_lon', 57.81196475014995, 1e-10),
    ('ur_lat', 67.62103710275053, 1e-10),
    ('x1', -0.0004161088727414608, 1e-6),
    ('y1', -4400000.001057557, 1e-10),
    ('x2', 3800000.0004256153, 1e-10),
    ('y2', -0.0004262728616595268, 1e-6),
    ('xpixelsize', 2000.0, 1e-10),
    ('xpixelsize', 2000.0, 1e-10),
    ('accutime', 15.0, 1e-10),
    ('yorigin', 'upper', None),
    ('unit', 'mm/h', None),
    ('institution', 'Odyssey datacentre', 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_opera_hdf5_metadata(variable, expected, tolerance):
    """Test the importer OPERA HDF5."""
    root_path = pysteps.rcparams.data_sources["opera"]["root_path"]
    filename = os.path.join(root_path, "20180824",
                            "T_PAAH21_C_EUOC_20180824180000.hdf")
    _, _, metadata = pysteps.io.import_opera_hdf5(filename)
    print(metadata)
    # assert metadata[variable] == expected
    smart_assert(metadata[variable], expected, tolerance)

@@ -28,17 +28,16 @@
 port os%0A
-%0A
 import p
@@ -110,142 +110,41 @@
 rt%0A%0A
-try:%0A    import h5py%0Aexcept ImportError:%0A    pytest.skip(%22test_io_opera_hdf5 tests. h5py library not present%22, allow_module_level=True
+h5py = pytest.importorskip(%22h5py%22
 )%0A%0A%0A
@@ -182,16 +182,16 @@
 hape():%0A
-
     %22%22%22T
@@ -214,32 +214,33 @@
  OPERA HDF5.%22%22%22%0A
+%0A
     root_path = 

Replace pytest.skip by pytest.importorskip