-
Notifications
You must be signed in to change notification settings - Fork 0
/
cl_rcp.py
357 lines (282 loc) · 9.96 KB
/
cl_rcp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# -*- coding: utf-8 -*-
# ----------------------------------------------------------------------------------------------------------------------
# Climate information dashboard.
#
# Class definition: RCP and RCPs.
#
# Contributors:
# 1. [email protected]
# (C) 2021-2022 Ouranos Inc., Canada
# ----------------------------------------------------------------------------------------------------------------------
# External libraries.
import pandas as pd
from typing import List, Union
# Dashboard libraries.
import cl_auth
import cl_gd
import cl_object
from cl_constant import const as c
from cl_context import cntx
def code_props(
) -> dict:
"""
--------------------------------------------------------------------------------------------------------------------
Get a dictionary of codes and properties.
Returns
-------
dict
Dictionary of codes and properties.
--------------------------------------------------------------------------------------------------------------------
"""
return {
c.REF: ["Référence", "black"],
c.RCP26: ["RCP 2.6", "blue"],
c.RCP45: ["RCP 4.5", "green"],
c.RCP85: ["RCP 8.5", "red"],
c.RCPXX: ["Tous", "pink"]
}
class RCP(cl_object.Obj):
"""
--------------------------------------------------------------------------------------------------------------------
Class defining the object RCP.
--------------------------------------------------------------------------------------------------------------------
"""
def __init__(
self,
code: str
):
"""
----------------------------------------
Constructor.
----------------------------------------
"""
if code in list(dict(code_props()).keys()):
super(RCP, self).__init__(code=code, desc=dict(code_props())[code][0])
@property
def color(
self
) -> str:
"""
----------------------------------------
Get color.
Returns
-------
str
Color.
----------------------------------------
"""
return "" if self.code == "" else dict(code_props())[self.code][1]
@property
def is_ref(
self
) -> bool:
"""
----------------------------------------
Determine if the instance is a reference RCP.
Returns
-------
bool
True if the instance is a reference RCP.
----------------------------------------
"""
return self.code == c.REF
class RCPs(cl_object.Objs):
"""
--------------------------------------------------------------------------------------------------------------------
Class defining the object RCPs.
--------------------------------------------------------------------------------------------------------------------
"""
def __init__(
self,
*args
):
"""
----------------------------------------
Constructor.
----------------------------------------
"""
super(RCPs, self).__init__()
if len(args) == 1:
if args[0] == "*":
self.load()
else:
self.add(args[0])
def load(
self
):
"""
----------------------------------------
Load items.
----------------------------------------
"""
# Codes.
project_code = cntx.project.code if cntx.project is not None else ""
view_code = cntx.view.code if cntx.view is not None else ""
vi_code = cntx.varidx.code if cntx.varidx is not None else ""
vi_name = cntx.varidx.name if cntx.varidx is not None else ""
if view_code == c.VIEW_CLUSTER:
view_code = c.VIEW_TS
vi_code = cntx.varidxs.items[0].code
vi_name = cntx.varidxs.items[0].name
hor_code = cntx.hor.code if cntx.hor is not None else ""
delta_code = cntx.delta.code if cntx.delta is not None else "False"
# Base directory.
p_base = str(cl_auth.path(project_code))
# The items are extracted from the 'rcp' column of data files ('tbl' view).
# ~/<project_code>/tbl/<vi_code>*.csv
if view_code == c.VIEW_TBL:
p = "<view_code>/<vi_code>/<vi_name>.csv"
p = p.replace("<view_code>", view_code)
p = p.replace("<vi_code>", vi_code)
p = p.replace("<vi_name>", vi_name)
if cntx.project.drive is not None:
df = pd.DataFrame(cntx.project.drive.load_csv(path=p))
else:
df = pd.read_csv(p_base + "/" + project_code + "/" + p)
item_l = list(df.columns) if view_code == c.VIEW_TS else df["rcp"]
if (delta_code == "True") and (c.REF in item_l):
item_l.remove(c.REF)
# The items are extracted from column names ('ts' or 'ts_bias' view).
# ~/<project_code>/<view_code>/<vi_code>/*.csv
elif view_code in [c.VIEW_TS, c.VIEW_TS_BIAS]:
p = "<view_code>/<vi_code>/<vi_name>_<mode>_<delta>.csv"
p = p.replace("<view_code>", view_code)
p = p.replace("<vi_code>", vi_code)
p = p.replace("<vi_name>", vi_name)
p = p.replace("<mode>", "rcp")
p = p.replace("_<delta>", "_delta" if delta_code == "True" else "")
if cntx.project.drive is not None:
df = pd.DataFrame(cntx.project.drive.load_csv(path=p))
else:
df = pd.read_csv(p_base + "/" + project_code + "/" + p)
item_l = list(df.columns)
if c.REF in item_l:
item_l.remove(c.REF)
# The items are extracted from file names.
# ~/<project_code>/map/<vi_code>/<hor_code>/*
elif view_code == c.VIEW_MAP:
pattern = project_code + "/<view_code>/<vi_code>/<hor_code>/*.csv"
pattern = pattern.replace("<view_code>", view_code)
pattern = pattern.replace("<vi_code>", vi_code)
pattern = pattern.replace("<hor_code>", hor_code)
item_l = list(cntx.files(pattern)[cl_gd.PROP_PATH])
# Only keep the reference dataset if the reference period was selected.
if hor_code == cntx.per_ref_str:
item_l_tmp = []
for item in item_l:
if c.REF in item:
item_l_tmp.append(item)
break
item_l = item_l_tmp
# The items are extracted from file names.
# ~/<project_code>/cycle*/<vi_code>/<hor_code>/*.csv
elif view_code == c.VIEW_CYCLE:
pattern = project_code + "/<view_code>*/<vi_code>/<hor_code>/*.csv"
pattern = pattern.replace("<view_code>", view_code)
pattern = pattern.replace("<vi_code>", vi_code)
pattern = pattern.replace("<hor_code>", hor_code)
item_l = list(cntx.files(pattern)[cl_gd.PROP_PATH])
else:
item_l = []
# Extract RCPs.
# Sort items, but let the reference emission scenario be first.
code_l = []
ref_found = False
for item in item_l:
code = ""
if c.REF in item:
ref_found = True
elif c.RCP26 in item:
code = c.RCP26
elif c.RCP45 in item:
code = c.RCP45
elif c.RCP85 in item:
code = c.RCP85
if (code != "") and (code not in code_l):
code_l.append(code)
code_l.sort()
if ref_found and (delta_code == "False"):
code_l = [c.REF] + code_l
self.add(code_l)
def add(
self,
item: Union[str, List[str], RCP],
inplace: bool = True
):
"""
----------------------------------------
Add one or several items.
Parameters
----------
item: Union[str, List[str], RCP]
Item (code, list of codes or instance of RCP).
inplace: bool
If True, modifies the current instance.
----------------------------------------
"""
items = []
if isinstance(item, RCP):
items = [item]
else:
code_l = item
if isinstance(item, str):
code_l = [item]
for i in range(len(code_l)):
items.append(RCP(code_l[i]))
return super(RCPs, self).add(items, inplace)
@property
def code_l(
self
) -> List[str]:
"""
----------------------------------------
Get a list of codes.
Returns
-------
List[str]
Codes.
----------------------------------------
"""
code_l = super(RCPs, self).code_l
if c.REF in code_l:
code_l.remove(c.REF)
code_l = [c.REF] + code_l
return code_l
@property
def desc_l(
self
) -> List[str]:
"""
----------------------------------------
Get a list of descriptions.
Returns
-------
List[str]
Descriptions.
----------------------------------------
"""
desc_l = super(RCPs, self).desc_l
if code_props()[c.REF][0] in desc_l:
desc_l.remove(code_props()[c.REF][0])
desc_l = [code_props()[c.REF][0]] + desc_l
return desc_l
@property
def color_l(
self
) -> List[str]:
"""
----------------------------------------
Get colors.
Returns
-------
List[str]
Colors.
----------------------------------------
"""
color_l = []
for item in self.items:
color_l.append(item.color)
if code_props()[c.REF][1] in color_l:
color_l.remove(code_props()[c.REF][1])
color_l = [code_props()[c.REF][1]] + color_l
return color_l