diff --git a/climada/engine/impact.py b/climada/engine/impact.py index 26043290d..c7d956883 100644 --- a/climada/engine/impact.py +++ b/climada/engine/impact.py @@ -46,7 +46,6 @@ from scipy import sparse from tqdm import tqdm -import climada.util.checker as u_check import climada.util.coordinates as u_coord import climada.util.dates_times as u_dt import climada.util.interpolation as u_interp @@ -549,7 +548,7 @@ def local_exceedance_impact( ) # check frequency unit - return_period_unit = u_check.convert_frequency_unit_to_time_unit( + return_period_unit = u_dt.convert_frequency_unit_to_time_unit( self.frequency_unit ) diff --git a/climada/hazard/base.py b/climada/hazard/base.py index bdb76c354..b97396083 100644 --- a/climada/hazard/base.py +++ b/climada/hazard/base.py @@ -538,7 +538,7 @@ def local_exceedance_intensity( if not min_intensity and min_intensity != 0: min_intensity = self.intensity_thres # check frequency unit - return_period_unit = u_check.convert_frequency_unit_to_time_unit( + return_period_unit = u_dt.convert_frequency_unit_to_time_unit( self.frequency_unit ) @@ -659,7 +659,7 @@ def local_return_period( if not min_intensity and min_intensity != 0: min_intensity = self.intensity_thres # check frequency unit - return_period_unit = u_check.convert_frequency_unit_to_time_unit( + return_period_unit = u_dt.convert_frequency_unit_to_time_unit( self.frequency_unit ) diff --git a/climada/util/checker.py b/climada/util/checker.py index 8fa977ed7..0d17b6036 100644 --- a/climada/util/checker.py +++ b/climada/util/checker.py @@ -228,32 +228,3 @@ def prune_csr_matrix(matrix: sparse.csr_matrix): matrix.check_format() matrix.eliminate_zeros() matrix.sum_duplicates() - - -def convert_frequency_unit_to_time_unit(frequency_unit): - """Converts common frequency units to corresponding time units. Unknown frequency - units are converted to "years". - - Parameters - ---------- - frequency_unit : str - unit of frequency - - Returns - ------- - str - corresponding time unit. - """ - if frequency_unit in ["1/year", "annual", "1/y", "1/a"]: - time_unit = "years" - elif frequency_unit in ["1/month", "monthly", "1/m"]: - time_unit = "months" - elif frequency_unit in ["1/week", "weekly", "1/w"]: - time_unit = "weeks" - else: - LOGGER.warning( - f"Frequency unit {frequency_unit} is not known, " - "years will be used as time unit." - ) - time_unit = "years" - return time_unit diff --git a/climada/util/dates_times.py b/climada/util/dates_times.py index 07882bf11..87453b0ae 100644 --- a/climada/util/dates_times.py +++ b/climada/util/dates_times.py @@ -114,3 +114,32 @@ def first_year(ordinal_vector): int """ return dt.date.fromordinal(np.min(ordinal_vector)).year + + +def convert_frequency_unit_to_time_unit(frequency_unit): + """Converts common frequency units to corresponding time units. Unknown frequency + units are converted to "years". + + Parameters + ---------- + frequency_unit : str + unit of frequency + + Returns + ------- + str + corresponding time unit. + """ + if frequency_unit in ["1/year", "annual", "1/y", "1/a"]: + time_unit = "years" + elif frequency_unit in ["1/month", "monthly", "1/m"]: + time_unit = "months" + elif frequency_unit in ["1/week", "weekly", "1/w"]: + time_unit = "weeks" + else: + LOGGER.warning( + f"Frequency unit {frequency_unit} is not known, " + "years will be used as time unit." + ) + time_unit = "years" + return time_unit diff --git a/climada/util/test/test_checker.py b/climada/util/test/test_checker.py index ab6a0882c..493f394e4 100644 --- a/climada/util/test/test_checker.py +++ b/climada/util/test/test_checker.py @@ -140,18 +140,6 @@ def test_prune_csr_matrix(self): np.testing.assert_array_equal(matrix.data, [3]) self.assertEqual(matrix.nnz, 1) - def test_convert_frequency_unit_to_time_unit(self): - # test frequency unit to time unit conversion - frequency_units = ["1/year", "1/y", "1/month", "1/week", "", "unknown"] - time_units = ["years", "years", "months", "weeks", "years", "years"] - np.testing.assert_equal( - time_units, - [ - u_check.convert_frequency_unit_to_time_unit(frequency_unit) - for frequency_unit in frequency_units - ], - ) - # Execute Tests if __name__ == "__main__": diff --git a/climada/util/test/test_dates_times.py b/climada/util/test/test_dates_times.py index 3cc9d25bc..b4b1dfc84 100644 --- a/climada/util/test/test_dates_times.py +++ b/climada/util/test/test_dates_times.py @@ -88,8 +88,25 @@ def test_first_year_pass(self): self.assertEqual(u_dt.first_year(np.array(ordinal_date)), 1918) +class TestUnitConversion(unittest.TestCase): + """Test time and frequency conversion functions""" + + def test_convert_frequency_unit_to_time_unit(self): + # test frequency unit to time unit conversion + frequency_units = ["1/year", "1/y", "1/month", "1/week", "", "unknown"] + time_units = ["years", "years", "months", "weeks", "years", "years"] + np.testing.assert_equal( + time_units, + [ + u_dt.convert_frequency_unit_to_time_unit(frequency_unit) + for frequency_unit in frequency_units + ], + ) + + # Execute Tests if __name__ == "__main__": TESTS = unittest.TestLoader().loadTestsFromTestCase(TestDateString) TESTS.addTests(unittest.TestLoader().loadTestsFromTestCase(TestDateNumpy)) + TESTS.addTests(unittest.TestLoader().loadTestsFromTestCase(TestUnitConversion)) unittest.TextTestRunner(verbosity=2).run(TESTS)