Skip to content

Commit eda57ca

Browse files
adapt returned Impact object
1 parent 0e30806 commit eda57ca

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

climada/util/yearsets.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
Define functions to handle impact_yearsets
1414
"""
1515

16-
import copy
1716
import logging
1817

1918
import numpy as np
2019
from numpy.random import default_rng
2120

2221
import climada.util.dates_times as u_dt
22+
from climada.engine import Impact
2323

2424
LOGGER = logging.getLogger(__name__)
2525

@@ -127,19 +127,23 @@ def impact_yearset_from_sampling_vect(
127127
# compute impact per sampled_year
128128
imp_per_year = compute_imp_per_year(imp, sampling_vect)
129129

130-
# copy imp object as basis for the yimp object
131-
yimp = copy.deepcopy(imp)
132-
133130
if correction_fac: # adjust for sampling error
134-
imp_per_year = imp_per_year.astype(float) / calculate_correction_fac(
135-
imp_per_year, imp
136-
)
131+
correction_factor = calculate_correction_fac(imp_per_year, imp)
132+
imp_per_year = imp_per_year.astype(float) * correction_factor
133+
LOGGER.info("The correction factor is %s.", correction_factor)
134+
135+
# create yearly impact object
136+
yimp = Impact(
137+
at_event=imp_per_year,
138+
date=u_dt.str_to_date([str(date) + "-01-01" for date in sampled_years]),
139+
event_name=np.arange(1, len(sampled_years) + 1),
140+
event_id=np.arange(1, len(sampled_years) + 1),
141+
unit=imp.unit,
142+
haz_type=imp.haz_type,
143+
frequency=np.ones(len(sampled_years)) / len(sampled_years),
144+
)
137145

138-
# save calculations in yimp
139-
yimp.at_event = imp_per_year
140-
yimp.event_id = np.arange(1, len(sampled_years) + 1)
141-
yimp.date = u_dt.str_to_date([str(date) + "-01-01" for date in sampled_years])
142-
yimp.frequency = np.full_like(yimp.at_event, 1.0 / len(sampled_years), dtype=float)
146+
yimp.aai_agg = np.sum(yimp.frequency * yimp.at_event)
143147

144148
return yimp
145149

@@ -301,10 +305,9 @@ def calculate_correction_fac(imp_per_year, imp):
301305
The correction factor is calculated as imp_eai/yimp_eai
302306
"""
303307

304-
yimp_eai = np.sum(imp_per_year) / len(imp_per_year)
308+
yimp_eai = np.mean(imp_per_year)
305309
imp_eai = np.sum(imp.frequency * imp.at_event)
306310
correction_factor = imp_eai / yimp_eai
307-
LOGGER.info("The correction factor amounts to %s", (correction_factor - 1) * 100)
308311

309312
# if correction_factor > 0.1:
310313
# tex = raw_input("Do you want to exclude small events?")

0 commit comments

Comments
 (0)