|
18 | 18 |
|
19 | 19 | """ |
20 | 20 |
|
21 | | -import copy |
22 | | -from datetime import datetime |
23 | | - |
24 | | -import numpy as np |
25 | | - |
26 | | -from climada.hazard.base import Hazard |
27 | | - |
28 | | - |
29 | | -def get_dates(haz: Hazard): |
30 | | - """ |
31 | | - Convert ordinal dates from a Hazard object to datetime objects. |
32 | | -
|
33 | | - Parameters |
34 | | - ---------- |
35 | | - haz : Hazard |
36 | | - A Hazard instance with ordinal date values. |
37 | | -
|
38 | | - Returns |
39 | | - ------- |
40 | | - list of datetime |
41 | | - List of datetime objects corresponding to the ordinal dates in `haz`. |
42 | | -
|
43 | | - Example |
44 | | - ------- |
45 | | - >>> haz = Hazard(...) |
46 | | - >>> get_dates(haz) |
47 | | - [datetime(2020, 1, 1), datetime(2020, 1, 2), ...] |
48 | | - """ |
49 | | - return [datetime.fromordinal(date) for date in haz.date] |
50 | | - |
51 | | - |
52 | | -def get_years(haz: Hazard): |
53 | | - """ |
54 | | - Extract unique years from ordinal dates in a Hazard object. |
55 | | -
|
56 | | - Parameters |
57 | | - ---------- |
58 | | - haz : Hazard |
59 | | - A Hazard instance containing ordinal date values. |
60 | | -
|
61 | | - Returns |
62 | | - ------- |
63 | | - np.ndarray |
64 | | - Array of unique years as integers, derived from the ordinal dates in `haz`. |
65 | | -
|
66 | | - Example |
67 | | - ------- |
68 | | - >>> haz = Hazard(...) |
69 | | - >>> get_years(haz) |
70 | | - array([2020, 2021, ...]) |
71 | | - """ |
72 | | - return np.unique(np.array([datetime.fromordinal(date).year for date in haz.date])) |
73 | | - |
74 | | - |
75 | | -def grow_exp(exp, exp_growth_rate, elapsed): |
76 | | - """ |
77 | | - Apply exponential growth to the exposure values over a specified period. |
78 | | -
|
79 | | - Parameters |
80 | | - ---------- |
81 | | - exp : Exposures |
82 | | - The initial Exposures object with values to be grown. |
83 | | - exp_growth_rate : float |
84 | | - The annual growth rate to apply (in decimal form, e.g., 0.01 for 1%). |
85 | | - elapsed : int |
86 | | - Number of years over which to apply the growth. |
87 | | -
|
88 | | - Returns |
89 | | - ------- |
90 | | - Exposures |
91 | | - A deep copy of the original Exposures object with grown exposure values. |
92 | | -
|
93 | | - Example |
94 | | - ------- |
95 | | - >>> exp = Exposures(...) |
96 | | - >>> grow_exp(exp, 0.01, 5) |
97 | | - Exposures object with values grown by 5%. |
98 | | - """ |
99 | | - exp_grown = copy.deepcopy(exp) |
100 | | - # Exponential growth |
101 | | - exp_growth_rate = 0.01 |
102 | | - exp_grown.gdf.value = exp_grown.gdf.value * (1 + exp_growth_rate) ** elapsed |
103 | | - return exp_grown |
104 | | - |
105 | | - |
106 | | -class TBRTrajectories: |
107 | | - |
108 | | - # Compute impacts for trajectories with present exposure and future exposure and interpolate in between |
109 | | - # |
110 | | - |
111 | | - @classmethod |
112 | | - def create_hazard_yearly_set(cls, haz: Hazard): |
113 | | - haz_set = {} |
114 | | - years = get_years(haz) |
115 | | - for year in range(years.min(), years.max(), 1): |
116 | | - haz_set[year] = haz.select( |
117 | | - date=[f"{str(year)}-01-01", f"{str(year+1)}-01-01"] |
118 | | - ) |
119 | | - |
120 | | - return haz_set |
121 | | - |
122 | | - @classmethod |
123 | | - def create_exposure_set(cls, snapshot_years, exp1, exp2=None, growth=None): |
124 | | - exp_set = {} |
125 | | - year_0 = snapshot_years.min() |
126 | | - if exp2 is None: |
127 | | - if growth is None: |
128 | | - raise ValueError("Need to specify either final exposure or growth.") |
129 | | - else: |
130 | | - exp_set = { |
131 | | - year: grow_exp(exp1, growth, year - year_0) |
132 | | - for year in snapshot_years |
133 | | - } |
134 | | - else: |
135 | | - exp_set = { |
136 | | - year: np.interp(exp1, exp2, year - year_0) for year in snapshot_years |
137 | | - } |
138 | | - return exp_set |
| 21 | +# import copy |
| 22 | +# from datetime import datetime |
| 23 | + |
| 24 | +# import numpy as np |
| 25 | + |
| 26 | +# from climada.hazard.base import Hazard |
| 27 | + |
| 28 | + |
| 29 | +# def get_dates(haz: Hazard): |
| 30 | +# """ |
| 31 | +# Convert ordinal dates from a Hazard object to datetime objects. |
| 32 | + |
| 33 | +# Parameters |
| 34 | +# ---------- |
| 35 | +# haz : Hazard |
| 36 | +# A Hazard instance with ordinal date values. |
| 37 | + |
| 38 | +# Returns |
| 39 | +# ------- |
| 40 | +# list of datetime |
| 41 | +# List of datetime objects corresponding to the ordinal dates in `haz`. |
| 42 | + |
| 43 | +# Example |
| 44 | +# ------- |
| 45 | +# >>> haz = Hazard(...) |
| 46 | +# >>> get_dates(haz) |
| 47 | +# [datetime(2020, 1, 1), datetime(2020, 1, 2), ...] |
| 48 | +# """ |
| 49 | +# return [datetime.fromordinal(date) for date in haz.date] |
| 50 | + |
| 51 | + |
| 52 | +# def get_years(haz: Hazard): |
| 53 | +# """ |
| 54 | +# Extract unique years from ordinal dates in a Hazard object. |
| 55 | + |
| 56 | +# Parameters |
| 57 | +# ---------- |
| 58 | +# haz : Hazard |
| 59 | +# A Hazard instance containing ordinal date values. |
| 60 | + |
| 61 | +# Returns |
| 62 | +# ------- |
| 63 | +# np.ndarray |
| 64 | +# Array of unique years as integers, derived from the ordinal dates in `haz`. |
| 65 | + |
| 66 | +# Example |
| 67 | +# ------- |
| 68 | +# >>> haz = Hazard(...) |
| 69 | +# >>> get_years(haz) |
| 70 | +# array([2020, 2021, ...]) |
| 71 | +# """ |
| 72 | +# return np.unique(np.array([datetime.fromordinal(date).year for date in haz.date])) |
| 73 | + |
| 74 | + |
| 75 | +# def grow_exp(exp, exp_growth_rate, elapsed): |
| 76 | +# """ |
| 77 | +# Apply exponential growth to the exposure values over a specified period. |
| 78 | + |
| 79 | +# Parameters |
| 80 | +# ---------- |
| 81 | +# exp : Exposures |
| 82 | +# The initial Exposures object with values to be grown. |
| 83 | +# exp_growth_rate : float |
| 84 | +# The annual growth rate to apply (in decimal form, e.g., 0.01 for 1%). |
| 85 | +# elapsed : int |
| 86 | +# Number of years over which to apply the growth. |
| 87 | + |
| 88 | +# Returns |
| 89 | +# ------- |
| 90 | +# Exposures |
| 91 | +# A deep copy of the original Exposures object with grown exposure values. |
| 92 | + |
| 93 | +# Example |
| 94 | +# ------- |
| 95 | +# >>> exp = Exposures(...) |
| 96 | +# >>> grow_exp(exp, 0.01, 5) |
| 97 | +# Exposures object with values grown by 5%. |
| 98 | +# """ |
| 99 | +# exp_grown = copy.deepcopy(exp) |
| 100 | +# # Exponential growth |
| 101 | +# exp_growth_rate = 0.01 |
| 102 | +# exp_grown.gdf.value = exp_grown.gdf.value * (1 + exp_growth_rate) ** elapsed |
| 103 | +# return exp_grown |
| 104 | + |
| 105 | + |
| 106 | +# class TBRTrajectories: |
| 107 | + |
| 108 | +# # Compute impacts for trajectories with present exposure and future exposure and interpolate in between |
| 109 | +# # |
| 110 | + |
| 111 | +# @classmethod |
| 112 | +# def create_hazard_yearly_set(cls, haz: Hazard): |
| 113 | +# haz_set = {} |
| 114 | +# years = get_years(haz) |
| 115 | +# for year in range(years.min(), years.max(), 1): |
| 116 | +# haz_set[year] = haz.select( |
| 117 | +# date=[f"{str(year)}-01-01", f"{str(year+1)}-01-01"] |
| 118 | +# ) |
| 119 | + |
| 120 | +# return haz_set |
| 121 | + |
| 122 | +# @classmethod |
| 123 | +# def create_exposure_set(cls, snapshot_years, exp1, exp2=None, growth=None): |
| 124 | +# exp_set = {} |
| 125 | +# year_0 = snapshot_years.min() |
| 126 | +# if exp2 is None: |
| 127 | +# if growth is None: |
| 128 | +# raise ValueError("Need to specify either final exposure or growth.") |
| 129 | +# else: |
| 130 | +# exp_set = { |
| 131 | +# year: grow_exp(exp1, growth, year - year_0) |
| 132 | +# for year in snapshot_years |
| 133 | +# } |
| 134 | +# else: |
| 135 | +# exp_set = { |
| 136 | +# year: np.interp(exp1, exp2, year - year_0) for year in snapshot_years |
| 137 | +# } |
| 138 | +# return exp_set |
0 commit comments