Skip to content

Commit

Permalink
Replace mat file with hdf5 test file
Browse files Browse the repository at this point in the history
  • Loading branch information
Chahan Kropf committed Nov 22, 2023
1 parent 73ee79e commit 0cabeac
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions climada/test/test_hazard.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,18 @@
from climada.hazard.base import Hazard
from climada.hazard.centroids import Centroids
from climada.hazard.storm_europe import StormEurope
from climada.util.constants import (HAZ_DEMO_FL, WS_DEMO_NC)
from climada.util.constants import (HAZ_DEMO_FL, WS_DEMO_NC, DEF_CRS)
from climada.util.api_client import Client
from climada.util import coordinates as u_coord
import climada.hazard.test as hazard_test
from climada.test import get_test_file

DATA_DIR = CONFIG.test_data.dir()

# Hazard test file from Git repository. Fraction is 1. Format: matlab.
HAZ_TEST_MAT :Path = Path(hazard_test.__file__).parent.joinpath('data', 'atl_prob_no_name.mat')
HAZ_TEST_TC :Path = get_test_file('test_tc_florida')
"""
Hazard test file from Data API: Hurricanes from 1851 to 2011 over Florida with 100 centroids.
Fraction is empty. Format: HDF5.
"""

class TestCentroids(unittest.TestCase):
"""Test centroids functionalities"""
Expand All @@ -47,7 +50,6 @@ def test_read_write_raster_pass(self):
"""Test write_raster: Hazard from raster data"""
haz_fl = Hazard.from_raster([HAZ_DEMO_FL])
haz_fl.haz_type = 'FL'
haz_fl.check()

self.assertEqual(haz_fl.intensity.shape, (1, 1032226))
self.assertEqual(haz_fl.intensity.min(), -9999.0)
Expand All @@ -59,6 +61,7 @@ def test_read_write_raster_pass(self):
haz_fl.haz_type = 'FL'
self.assertTrue(np.allclose(haz_fl.intensity.toarray(), haz_read.intensity.toarray()))
self.assertEqual(np.unique(np.array(haz_fl.fraction.toarray())).size, 2)
DATA_DIR.joinpath('test_write_hazard.tif').unlink()

def test_read_raster_pool_pass(self):
"""Test from_raster constructor with pool"""
Expand All @@ -83,8 +86,9 @@ def test_read_write_vector_pass(self):
event_name=['1'],
intensity=sparse.csr_matrix(np.array([0.5, 0.2, 0.1])),
fraction=sparse.csr_matrix(np.array([0.5, 0.2, 0.1]) / 2),
centroids=Centroids.from_lat_lon(
np.array([1, 2, 3]), np.array([1, 2, 3])),)
centroids=Centroids(
latitude=np.array([1, 2, 3]), longitude=np.array([1, 2, 3]), crs=DEF_CRS)
)
haz_fl.check()

haz_fl.write_raster(DATA_DIR.joinpath('test_write_hazard.tif'))
Expand All @@ -104,8 +108,9 @@ def test_write_fraction_pass(self):
event_name=['1'],
intensity=sparse.csr_matrix(np.array([0.5, 0.2, 0.1])),
fraction=sparse.csr_matrix(np.array([0.5, 0.2, 0.1]) / 2),
centroids=Centroids.from_lat_lon(
np.array([1, 2, 3]), np.array([1, 2, 3])),)
centroids=Centroids(
latitude=np.array([1, 2, 3]),longitude=np.array([1, 2, 3]), crs=DEF_CRS)
)
haz_fl.check()

haz_fl.write_raster(DATA_DIR.joinpath('test_write_hazard.tif'), intensity=False)
Expand Down Expand Up @@ -270,7 +275,7 @@ def test_write_read_pass(self):

file_name = str(DATA_DIR.joinpath("test_haz.h5"))
# Read demo matlab file
hazard = Hazard.from_mat(HAZ_TEST_MAT)
hazard = Hazard.from_hdf5(HAZ_TEST_TC)
hazard.event_name = list(map(str, hazard.event_name))
for todense_flag in [False, True]:
if todense_flag:
Expand Down

0 comments on commit 0cabeac

Please sign in to comment.