-
Notifications
You must be signed in to change notification settings - Fork 0
/
cl_sim.py
356 lines (278 loc) · 8.89 KB
/
cl_sim.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
# -*- coding: utf-8 -*-
# ----------------------------------------------------------------------------------------------------------------------
# Climate information dashboard.
#
# Class definition: Sim and Sims
#
# Contributors:
# 1. [email protected]
# (C) 2021-2022 Ouranos Inc., Canada
# ----------------------------------------------------------------------------------------------------------------------
# External libraries.
import os
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
from cl_rcp import RCP
def code_desc(
) -> dict:
"""
--------------------------------------------------------------------------------------------------------------------
Get a dictionary of codes and properties.
Returns
-------
dict
Dictionary of codes and properties.
--------------------------------------------------------------------------------------------------------------------
"""
return {
c.SIMXX: "Toutes"
}
class Sim(cl_object.Obj):
"""
--------------------------------------------------------------------------------------------------------------------
Class defining the object Sim.
--------------------------------------------------------------------------------------------------------------------
"""
def __init__(
self,
code: str
):
"""
----------------------------------------
Constructor.
----------------------------------------
"""
desc = dict(code_desc())[code] if code == c.SIMXX else code
super(Sim, self).__init__(code=code, desc=desc)
@property
def rcm(
self
) -> str:
"""
----------------------------------------
Get RCM.
Returns
-------
str
RCM.
----------------------------------------
"""
rcm = ""
tokens = self.code.split("_")
if len(tokens) >= 4:
rcm = tokens[0]
return rcm
@property
def domain(
self
) -> str:
"""
----------------------------------------
Get domain.
Returns
-------
str
Domain.
----------------------------------------
"""
domain = ""
tokens = self.code.split("_")
if len(tokens) >= 4:
domain = tokens[1]
return domain
@property
def gcm(
self
) -> str:
"""
----------------------------------------
Get GCM.
Returns
-------
str
GCM.
----------------------------------------
"""
gcm = ""
tokens = self.code.split("_")
if len(tokens) >= 4:
gcm = tokens[2]
return gcm
@property
def rcp(
self
) -> Union[RCP, None]:
"""
----------------------------------------
Get RCP.
Returns
-------
cl_rcp.RCP
RCP.
----------------------------------------
"""
rcp = None
if self.code == c.REF:
rcp = RCP(c.REF)
else:
tokens = self.code.split("_")
if len(tokens) >= 4:
rcp = RCP(tokens[3])
return rcp
@property
def desc(
self
) -> str:
"""
----------------------------------------
Get description.
Returns
-------
str
Description.
----------------------------------------
"""
desc = self.code
if self.code == c.REF:
desc = self.rcp.desc
else:
tokens = self.code.split("_")
if len(tokens) >= 4:
desc = self.rcm + "_" + self.gcm + " (" + self.rcp.desc + ")"
return desc
class Sims(cl_object.Objs):
"""
--------------------------------------------------------------------------------------------------------------------
Class defining the object Sims.
--------------------------------------------------------------------------------------------------------------------
"""
def __init__(
self,
*args
):
"""
----------------------------------------
Constructor.
----------------------------------------
"""
super(Sims, self).__init__()
if len(args) == 1:
if args[0] == "*":
self.load()
else:
self.add(args[0])
def load(
self
):
"""
----------------------------------------
Load items.
----------------------------------------
"""
code_l = []
# 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 ""
if view_code == c.VIEW_CLUSTER:
view_code = c.VIEW_TS
vi_code = cntx.varidx.code if cntx.varidx is not None else ""
vi_name = cntx.varidx.name if cntx.varidx is not None else ""
hor_code = cntx.hor.code if cntx.hor is not None else ""
rcp_code = cntx.rcp.code if cntx.rcp 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 file names.
# ~/<project_code>/<view_code>*/<vi_code>/<hor_code>/*.csv
if view_code == c.VIEW_CYCLE:
pattern = project_code + "/<view_code>*/<vi_code>/<hor_code>/*<rcp_code>*.csv"
pattern = pattern.replace("<view_code>", view_code)
pattern = pattern.replace("<vi_code>", vi_code)
pattern = pattern.replace("<hor_code>", hor_code)
pattern = pattern.replace("<rcp_code>", rcp_code)
p_l = list(cntx.files(pattern)[cl_gd.PROP_PATH])
# The code of each simulation is set to <RCM>_<domain>_<GCM>_<RCP>.
ref_found = False
for p_i in p_l:
if c.REF in p_i:
ref_found = True
else:
tokens = os.path.basename(p_i).split("_")
code = tokens[1] + "_" + tokens[2] + "_" + tokens[3] + "_" + tokens[4]
if code not in code_l:
code_l.append(code)
if ref_found:
code_l = [c.REF] + code_l
# The items are extracted from columns.
# ~/<project_code>/<view_code>/<vi_code>/<vi_code>_sim_*.csv
elif view_code in [c.VIEW_TS, c.VIEW_TS_BIAS]:
p = "<view_code>/<vi_code>/<vi_name>_sim_<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("_<delta>", "_delta" if delta_code == "True" else "")
# Load CSV file.
df = None
if "gd:" in p_base:
df = pd.DataFrame(cntx.project.drive.load_csv(path=p))
else:
p = p_base + "/" + project_code + "/" + p
if os.path.exists(p):
df = pd.read_csv(p)
if (df is not None) and (len(df) > 0):
df.drop(["year", c.REF], axis=1, inplace=True)
for column in list(df.columns):
if (rcp_code in ["", c.RCPXX]) or ((rcp_code not in ["", c.RCPXX]) and (rcp_code in column)):
code_l.append(column)
# Sort list and put reference first.
code_l.sort()
self.add(code_l)
def add(
self,
item: Union[str, List[str], Sim],
inplace: bool = True
):
"""
----------------------------------------
Add one or several items.
Parameters
----------
item: Union[str, List[str], Sim]
Item (code, list of codes or instance of Sim).
inplace: bool
If True, modifies the current instance.
----------------------------------------
"""
items = []
if isinstance(item, Sim):
items = [item]
else:
code_l = item
if isinstance(item, str):
code_l = [item]
for i in range(len(code_l)):
items.append(Sim(code_l[i]))
return super(Sims, self).add(items, inplace)
@property
def desc_l(
self
) -> List[str]:
"""
----------------------------------------
Get a list of descriptions.
Returns
-------
List[str]
Descriptions.
----------------------------------------
"""
desc_l = []
for item in self.items:
desc_l.append(item.desc)
return desc_l