|
| 1 | +""" |
| 2 | +Test StormEurope class |
| 3 | +""" |
| 4 | + |
| 5 | +import os |
| 6 | +import unittest |
| 7 | +import datetime as dt |
| 8 | +import numpy as np |
| 9 | +from scipy import sparse |
| 10 | + |
| 11 | +from climada import StormEurope, Centroids, GridPoints, SYSTEM_DIR |
| 12 | + |
| 13 | + |
| 14 | +class TestReader(unittest.TestCase): |
| 15 | + """ Test loading functions from the StormEurope class """ |
| 16 | + |
| 17 | + fn = [ |
| 18 | + 'fp_lothar_crop-test.nc', |
| 19 | + 'fp_xynthia_crop-test.nc', |
| 20 | + ] |
| 21 | + ncdfs = [os.path.join(SYSTEM_DIR, f) for f in fn] |
| 22 | + |
| 23 | + ct = Centroids(os.path.join(SYSTEM_DIR, 'fp_centroids-test.csv')) |
| 24 | + |
| 25 | + def test_centroids_from_nc(self): |
| 26 | + """ Test if centroids can be constructed correctly """ |
| 27 | + ct = StormEurope._centroids_from_nc(self.ncdfs[0]) |
| 28 | + |
| 29 | + self.assertTrue(isinstance(ct, Centroids)) |
| 30 | + self.assertTrue(isinstance(ct.coord, GridPoints)) |
| 31 | + self.assertEqual(ct.size, 10000) |
| 32 | + self.assertEqual(ct.coord.shape[0], ct.id.shape[0]) |
| 33 | + |
| 34 | + def test_read_footprints(self): |
| 35 | + """ Test read_footprints function, using two small test files""" |
| 36 | + se = StormEurope() |
| 37 | + se.read_footprints(self.ncdfs) |
| 38 | + |
| 39 | + self.assertEqual(se.tag.haz_type, 'WS') |
| 40 | + self.assertEqual(se.units, 'm/s') |
| 41 | + self.assertEqual(se.event_id.size, 2) |
| 42 | + self.assertEqual(se.date.size, 2) |
| 43 | + self.assertEqual(dt.datetime.fromordinal(se.date[0]).year, 1999) |
| 44 | + self.assertEqual(dt.datetime.fromordinal(se.date[0]).month, 12) |
| 45 | + self.assertEqual(dt.datetime.fromordinal(se.date[0]).day, 26) |
| 46 | + self.assertEqual(se.event_id[0], 1) |
| 47 | + self.assertEqual(se.event_name[0], 'Lothar') |
| 48 | + self.assertTrue(isinstance(se.intensity, sparse.csr.csr_matrix)) |
| 49 | + self.assertTrue(isinstance(se.fraction, sparse.csr.csr_matrix)) |
| 50 | + self.assertEqual(se.intensity.shape, (2, 10000)) |
| 51 | + self.assertEqual(se.fraction.shape, (2, 10000)) |
| 52 | + |
| 53 | + def test_read_with_ref(self): |
| 54 | + """ Test read_footprints while passing in a reference raster. """ |
| 55 | + se = StormEurope() |
| 56 | + se.read_footprints(self.ncdfs, ref_raster=self.ncdfs[1]) |
| 57 | + |
| 58 | + self.assertEqual(se.tag.haz_type, 'WS') |
| 59 | + self.assertEqual(se.units, 'm/s') |
| 60 | + self.assertEqual(se.event_id.size, 2) |
| 61 | + self.assertEqual(se.date.size, 2) |
| 62 | + self.assertEqual(dt.datetime.fromordinal(se.date[0]).year, 1999) |
| 63 | + self.assertEqual(dt.datetime.fromordinal(se.date[0]).month, 12) |
| 64 | + self.assertEqual(dt.datetime.fromordinal(se.date[0]).day, 26) |
| 65 | + self.assertEqual(se.event_id[0], 1) |
| 66 | + self.assertEqual(se.event_name[0], 'Lothar') |
| 67 | + self.assertTrue(isinstance(se.intensity, sparse.csr.csr_matrix)) |
| 68 | + self.assertTrue(isinstance(se.fraction, sparse.csr.csr_matrix)) |
| 69 | + self.assertEqual(se.intensity.shape, (2, 10000)) |
| 70 | + self.assertEqual(se.fraction.shape, (2, 10000)) |
| 71 | + |
| 72 | + def test_read_with_cent(self): |
| 73 | + """ Test read_footprints while passing in a Centroids object """ |
| 74 | + se = StormEurope() |
| 75 | + se.read_footprints(self.ncdfs, centroids=self.ct) |
| 76 | + |
| 77 | + self.assertEqual(se.tag.haz_type, 'WS') |
| 78 | + self.assertEqual(se.units, 'm/s') |
| 79 | + self.assertEqual(se.event_id.size, 2) |
| 80 | + self.assertEqual(se.date.size, 2) |
| 81 | + self.assertEqual(dt.datetime.fromordinal(se.date[0]).year, 1999) |
| 82 | + self.assertEqual(dt.datetime.fromordinal(se.date[0]).month, 12) |
| 83 | + self.assertEqual(dt.datetime.fromordinal(se.date[0]).day, 26) |
| 84 | + self.assertEqual(se.event_id[0], 1) |
| 85 | + self.assertEqual(se.event_name[0], 'Lothar') |
| 86 | + self.assertTrue(isinstance(se.intensity, sparse.csr.csr_matrix)) |
| 87 | + self.assertTrue(isinstance(se.fraction, sparse.csr.csr_matrix)) |
| 88 | + self.assertEqual(se.intensity.shape, (2, 10000)) |
| 89 | + self.assertEqual(se.fraction.shape, (2, 10000)) |
| 90 | + self.assertEqual( |
| 91 | + se.centroids.region_id[ |
| 92 | + np.isnan(se.centroids.region_id) |
| 93 | + ].size, |
| 94 | + 7515 |
| 95 | + ) |
| 96 | + |
| 97 | + |
| 98 | +# Execute Tests |
| 99 | +TESTS = unittest.TestLoader().loadTestsFromTestCase(TestReader) |
| 100 | +unittest.TextTestRunner(verbosity=2).run(TESTS) |
0 commit comments