|
24 | 24 | import numpy as np |
25 | 25 |
|
26 | 26 | from ..util import log_level |
| 27 | +from ..util.checker import size |
27 | 28 | from ..util.forecast import Forecast |
28 | 29 | from .impact import Impact |
29 | 30 |
|
@@ -51,8 +52,8 @@ def __init__( |
51 | 52 | impact_kwargs |
52 | 53 | Keyword-arguments passed to ~:py:class`climada.engine.impact.Impact`. |
53 | 54 | """ |
54 | | - # TODO: Maybe assert array lengths? |
55 | 55 | super().__init__(lead_time=lead_time, member=member, **impact_kwargs) |
| 56 | + self._check_sizes() |
56 | 57 |
|
57 | 58 | @classmethod |
58 | 59 | def from_impact( |
@@ -88,3 +89,154 @@ def from_impact( |
88 | 89 | imp_mat=impact.imp_mat, |
89 | 90 | haz_type=impact.haz_type, |
90 | 91 | ) |
| 92 | + |
| 93 | + @property |
| 94 | + def at_event(self): |
| 95 | + """Get the total impact for each member/lead_time combination.""" |
| 96 | + LOGGER.warning( |
| 97 | + "at_event gives the total impact for one specific combination of member and " |
| 98 | + "lead_time." |
| 99 | + ) |
| 100 | + return self._at_event |
| 101 | + |
| 102 | + @at_event.setter |
| 103 | + def at_event(self, value): |
| 104 | + """Set the total impact for each member/lead_time combination.""" |
| 105 | + self._at_event = value |
| 106 | + |
| 107 | + def local_exceedance_impact( |
| 108 | + self, |
| 109 | + return_periods=(25, 50, 100, 250), |
| 110 | + method="interpolate", |
| 111 | + min_impact=0, |
| 112 | + log_frequency=True, |
| 113 | + log_impact=True, |
| 114 | + bin_decimals=None, |
| 115 | + ): |
| 116 | + """Compution of local exceedance impact for given return periods is not |
| 117 | + implemented for ImpactForecast. |
| 118 | +
|
| 119 | + See Also |
| 120 | + -------- |
| 121 | + See :py:meth:`~climada.engine.impact.Impact.local_exceedance_impact` |
| 122 | +
|
| 123 | + Raises |
| 124 | + ------ |
| 125 | + NotImplementedError |
| 126 | + """ |
| 127 | + |
| 128 | + LOGGER.error("local_exceedance_impact is not defined for ImpactForecast") |
| 129 | + raise NotImplementedError( |
| 130 | + "local_exceedance_impact is not defined for ImpactForecast" |
| 131 | + ) |
| 132 | + |
| 133 | + def local_return_period( |
| 134 | + self, |
| 135 | + threshold_impact=(1000.0, 10000.0), |
| 136 | + method="interpolate", |
| 137 | + min_impact=0, |
| 138 | + log_frequency=True, |
| 139 | + log_impact=True, |
| 140 | + bin_decimals=None, |
| 141 | + ): |
| 142 | + """Compution of local return period for given impact thresholds is not |
| 143 | + implemented for ImpactForecast. |
| 144 | +
|
| 145 | + See Also |
| 146 | + -------- |
| 147 | + See :py:meth:`~climada.engine.impact.Impact.local_return_period` |
| 148 | +
|
| 149 | + Raises |
| 150 | + ------- |
| 151 | + NotImplementedError |
| 152 | + """ |
| 153 | + |
| 154 | + LOGGER.error("local_return_period is not defined for ImpactForecast") |
| 155 | + raise NotImplementedError( |
| 156 | + "local_return_period is not defined for ImpactForecast" |
| 157 | + ) |
| 158 | + |
| 159 | + def calc_freq_curve(self, return_per=None): |
| 160 | + """Computation of the impact exceedance frequency curve is not |
| 161 | + implemented for ImpactForecast. |
| 162 | +
|
| 163 | + See Also |
| 164 | + -------- |
| 165 | + See :py:meth:`~climada.engine.impact.Impact.calc_freq_curve` |
| 166 | +
|
| 167 | + Raises |
| 168 | + ------ |
| 169 | + NotImplementedError |
| 170 | + """ |
| 171 | + |
| 172 | + LOGGER.error("calc_freq_curve is not defined for ImpactForecast") |
| 173 | + raise NotImplementedError("calc_freq_curve is not defined for ImpactForecast") |
| 174 | + |
| 175 | + def _check_sizes(self): |
| 176 | + """Check sizes of forecast data vs. impact data. |
| 177 | +
|
| 178 | + Raises |
| 179 | + ------ |
| 180 | + ValueError |
| 181 | + If the sizes of the forecast data do not match the |
| 182 | + :py:attr:`~climada.engine.impact.Impact.event_id` |
| 183 | + """ |
| 184 | + num_entries = len(self.event_id) |
| 185 | + size(exp_len=num_entries, var=self.member, var_name="Forecast.member") |
| 186 | + size(exp_len=num_entries, var=self.lead_time, var_name="Forecast.lead_time") |
| 187 | + |
| 188 | + def select( |
| 189 | + self, |
| 190 | + event_ids=None, |
| 191 | + event_names=None, |
| 192 | + dates=None, |
| 193 | + coord_exp=None, |
| 194 | + reset_frequency=False, |
| 195 | + member=None, |
| 196 | + lead_time=None, |
| 197 | + ): |
| 198 | + """Select entries based on the parameters and return a new instance. |
| 199 | + The selection will contain the intersection of all given parameters. |
| 200 | +
|
| 201 | + Parameters |
| 202 | + ---------- |
| 203 | + member : Sequence of ints |
| 204 | + Ensemble members to select |
| 205 | + lead_time : Sequence of numpy.timedelta64 |
| 206 | + Lead times to select |
| 207 | +
|
| 208 | + Returns |
| 209 | + ------- |
| 210 | + ImpactForecast |
| 211 | +
|
| 212 | + See Also |
| 213 | + -------- |
| 214 | + :py:meth:`~climada.engine.impact.Impact.select` |
| 215 | + """ |
| 216 | + if member is not None or lead_time is not None: |
| 217 | + mask_member = ( |
| 218 | + self.idx_member(member) |
| 219 | + if member is not None |
| 220 | + else np.full_like(self.member, True, dtype=bool) |
| 221 | + ) |
| 222 | + mask_lead_time = ( |
| 223 | + self.idx_lead_time(lead_time) |
| 224 | + if lead_time is not None |
| 225 | + else np.full_like(self.lead_time, True, dtype=bool) |
| 226 | + ) |
| 227 | + event_id_from_forecast_mask = np.asarray(self.event_id)[ |
| 228 | + (mask_member & mask_lead_time) |
| 229 | + ] |
| 230 | + event_ids = ( |
| 231 | + np.intersect1d(event_ids, event_id_from_forecast_mask) |
| 232 | + if event_ids is not None |
| 233 | + else event_id_from_forecast_mask |
| 234 | + ) |
| 235 | + |
| 236 | + return super().select( |
| 237 | + event_ids=event_ids, |
| 238 | + event_names=event_names, |
| 239 | + dates=dates, |
| 240 | + coord_exp=coord_exp, |
| 241 | + reset_frequency=reset_frequency, |
| 242 | + ) |
0 commit comments