-
Notifications
You must be signed in to change notification settings - Fork 0
/
cl_varidx.py
1186 lines (948 loc) · 40.2 KB
/
cl_varidx.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
# ----------------------------------------------------------------------------------------------------------------------
# Climate information dashboard.
#
# Class definition: RCP and RCPs.
#
# Contributors:
# 1. [email protected]
# (C) 2021-2022 Ouranos Inc., Canada
# ----------------------------------------------------------------------------------------------------------------------
# External libraries.
import glob
import os
from typing import List, Union, Optional
# Dashboard libraries.
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.
Cell 1: Long description
Cell 2: Short description (for y-label).
Cell 3: Units.
Cell 4: Precision (number of decimal places to show).
--------------------------------------------------------------------------------------------------------------------
"""
return {
# Variables (CORDEX):
c.V_TAS: ["Température moyenne", "Température", "°C", 1],
c.V_TASMIN: ["Température minimale journalière", "Température", "°C", 1],
c.V_TASMAX: ["Température maximale journalière", "Température", "°C", 1],
c.V_UAS: ["Vitesse du vent (dir. est)", "Vitesse", "km/h", 0],
c.V_VAS: ["Vitesse du vent (dir. nord)", "Vitesse", "km/h", 0],
c.V_SFCWINDMAX: ["Vitesse du vent", "Vitesse", "km/h", 0],
c.V_PS: ["Pression barométrique", "Pression barométrique", "Pa", 0],
c.V_RSDS: ["Radiation solaire", "Radiation solaire", "Pa", 0],
c.V_PR: ["Cumul de précipitation", "Cumul", "mm", 0],
c.V_EVSPSBL: ["Évapotranspiration", "Cumul", "mm", 0],
c.V_EVSPSBLPOT: ["Évapotranspiration potentielle", "Cumul", "mm", 0],
c.V_HUSS: ["Humidité spécifique", "Humidité spécifique", "", 2],
c.V_CLT: ["Couvert nuageux", "Couvert nuageux", "%", 1],
c.V_SNW: ["Neige au sol", "Hauteur (en eau)", "mm", 0],
# Variables (ECMWF: ERA5 and ERA5-Land):
c.V_ECMWF_D2M: ["Point de rosée", "Point de rosée", "", 1],
c.V_ECMWF_T2M: ["Température moyenne", "Température", "°C", 1],
c.V_ECMWF_T2MMIN: ["Température minimale journalière", "Température", "°C", 1],
c.V_ECMWF_T2MMAX: ["Température maximale journalière", "Température", "°C", 1],
c.V_ECMWF_SP: ["Pression barométrique", "Pression barométrique", "Pa", 0],
c.V_ECMWF_TP: ["Précipitation", "Cumul", "mm", 0],
c.V_ECMWF_U10: ["Vitesse du vent (dir. est)", "Vitesse", "km/h", 0],
c.V_ECMWF_U10MIN: ["Vitesse du vent min. (dir. est)", "Vitesse", "km/h", 0],
c.V_ECMWF_U10MAX: ["Vitesse du vent max. (dir. est)", "Vitesse", "km/h", 0],
c.V_ECMWF_V10: ["Vitesse du vent (dir. nord)", "Vitesse", "km/h", 0],
c.V_ECMWF_V10MIN: ["Vitesse du vent min. (dir. nord)", "Vitesse", "km/h", 0],
c.V_ECMWF_V10MAX: ["Vitesse du vent max. (dir. nord)", "Vitesse", "km/h", 0],
c.V_ECMWF_UV10: ["Vitesse du vent", "Vitesse", "km/h", 0],
c.V_ECMWF_UV10MIN: ["Vitesse du vent min.", "Vitesse", "km/h", 0],
c.V_ECMWF_UV10MAX: ["Vitesse du vent max.", "Vitesse", "km/h", 0],
c.V_ECMWF_SSRD: ["Radiation solaire", "Radiation solaire", "Pa", 0],
c.V_ECMWF_E: ["Évaporation", "Cumul", "mm", 0],
c.V_ECMWF_PEV: ["Évapotranspiration potentielle", "Cumul", "mm", 0],
c.V_ECMWF_SH: ["Humidité spécifique", "Humidité spécifique", "", 2],
c.V_ECMWF_SNW: ["Neige au sol", "Hauteur (en eau)", "mm", 0],
# Variables (ENACTS):
c.V_ENACTS_TMIN: ["Température minimale journalière", "Température", "°C", 1],
c.V_ENACTS_TMAX: ["Température maximale journalière", "Température", "°C", 1],
c.V_ENACTS_RR: ["Précipitation", "Cumul", "mm", 0],
c.V_ENACTS_PET: ["Évapotranspiration potentielle", "Cumul", "mm", 0],
# Variables (CHIRPS):
c.V_CHIRPS_PRECIP: ["Précipitation", "Cumul", "mm", 0],
# Indices:
c.I_ETR: ["Ecart extrême de température", "Température", "°C", 1],
c.I_TX90P: ["Nombre de jours chauds (Tmax > 90e)", "Nbr. jours", "jours", 0],
c.I_HEAT_WAVE_MAX_LENGTH: ["Durée maximale des vagues de chaleur (Tmax ≥ {1}; Tmin ≥ {2}; " +
"{3} jours consécutifs)", "Durée", "jours", 0],
c.I_HEAT_WAVE_TOTAL_LENGTH: ["Durée totale des vagues de chaleur (Tmax ≥ {1}; Tmin ≥ {2}; " +
"{3} jours consécutifs)", "Durée", "jours", 0],
c.I_HOT_SPELL_FREQUENCY: ["Nombre de périodes chaudes (Tmax ≥ {1}; {2} jours consécutifs)",
"Nbr. périodes", "périodes", 0],
c.I_HOT_SPELL_MAX_LENGTH: ["Durée maximale des périodes chaudes (Tmax ≥ {1}; {2} jours consécutifs)",
"Durée", "jours", 0],
c.I_HOT_SPELL_TOTAL_LENGTH: ["Durée totale des périodes chaudes (Tmax ≥ {1}; {2} jours consécutifs)",
"Durée", "jours", 0],
c.I_TGG: ["Valeur moyenne (à partir de Tmin et Tmax)", "Température", "°C", 1],
c.I_TNG: ["Température minimale", "Nbr. mois", "", 1],
c.I_TNX: ["Valeur maximale de Tmin", "Température", "°C", 1],
c.I_TXG: ["Valeur moyenne de Tmax", "Température", "°C", 1],
c.I_TXX: ["Température maximale", "Température", "°C", 1],
c.I_TNG_MONTHS_BELOW: ["Nbr mois frais (moyenne(Tmin) < {1}°C)", "Nbr. mois", "mois", 1],
c.I_TX_DAYS_ABOVE: ["Nombre de jours chauds (Tmax > {1})", "Nbr. jours", "jours", 0],
c.I_TN_DAYS_BELOW: ["Nombre de jours frais (Tmin < {1})", "Nbr. jours", "jours", 0],
c.I_TROPICAL_NIGHTS: ["Nombre de nuits chaudes (Tmin > {1})", "Nbr. jours", "jours", 0],
c.I_WSDI: ["Indice de durée des périodes chaudes (Tmax ≥ {1}; {2} jours consécutifs)",
"Indice", "", 0],
c.I_RX1DAY: ["Cumul de précipitation (1 jour)", "Cumul", "mm", 0],
c.I_RX5DAY: ["Cumul de précipitation (5 jours)", "Cumul", "mm", 0],
c.I_CDD: ["Nombre de jours secs consécutifs (P < {1} mm)", "Nbr. jours", "jours", 0],
c.I_CWD: ["Nombre de jours pluvieux consécutifs (P ≥ {1} mm)", "Nbr. jours", "jours", 0],
c.I_DRY_DAYS: ["Nombre de jours secs (P < {1} mm)", "Nbr. jours", "jours", 0],
c.I_WET_DAYS: ["Nombre de jours pluvieux (P ≥ {1} mm)", "Nbr. jours", "jours", 0],
c.I_PRCPTOT: ["Cumul de précipitation", "Cumul", "mm", 0],
c.I_R10MM: ["Nombre de jours avec P ≥ 10 mm", "Nbr. jours", "jours", 0],
c.I_R20MM: ["Nombre de jours avec P ≥ 20 mm", "Nbr. jours", "jours", 0],
c.I_SDII: ["Intensité moyenne des précipitations", "Intensité", "mm/day", 0],
c.I_RAIN_SEASON_START: ["Début de la {1}saison de pluie", "Jour", "", 0],
c.I_RAIN_SEASON_END: ["Fin de la {1}saison de pluie", "Jour", "", 0],
c.I_RAIN_SEASON_LENGTH: ["Durée de la {1}saison de pluie", "Nbr. Jours", "jours", 0],
c.I_RAIN_SEASON_PRCPTOT: ["Cumul de précipitation pendant la {1}saison de pluie", "Cumul", "mm", 0],
c.I_DRY_SPELL_TOTAL_LENGTH: ["Durée totale des périodes sèches (P < {1} mm/jour; {2} jours consécutifs)",
"Nbr. jours", "jours", 0],
c.I_WG_DAYS_ABOVE: ["Nombre de jours avec vent fort directionnel (Vmoy ≥ {1}; de {3}±{4}°)",
"Nbr. jours", "jours", 0],
c.I_WX_DAYS_ABOVE: ["Nombre de jours avec vent fort (Vmax ≥ {1})",
"Nbr. jours", "jours", 0],
c.I_DROUGHT_CODE: ["Code de sécheresse", "Code", "", 0]
}
class VarIdx(cl_object.Obj):
"""
--------------------------------------------------------------------------------------------------------------------
Class defining the object VarIdx.
--------------------------------------------------------------------------------------------------------------------
"""
# Parameters.
_params = []
# Ensemble name.
_ens = ""
def __init__(
self,
code: str,
ens: Optional[str] = ""
):
"""
----------------------------------------
Constructor.
# Parameters.
code: str
Variable or index code.
ens: Optional[str]
Ensemble name.
----------------------------------------
"""
# Variable or index name.
vi_name = extract_vi_name(code)
# Variable or index description.
if (code == "") or (vi_name not in dict(code_props()).keys()):
desc = ""
else:
desc = dict(code_props())[vi_name][0]
# Ensemnble name.
if ens != "":
self._ens = ens
else:
if code in c.V_CORDEX:
self.ens = c.ENS_CORDEX
elif code in c.V_ECMWF:
self.ens = c.ENS_ERA5
elif code in c.V_ENACTS:
self.ens = c.ENS_ENACTS
elif code in c.V_CHIRPS:
self.ens = c.ENS_CHIRPS
super(VarIdx, self).__init__(code=code, desc=desc)
@property
def name(
self
) -> str:
"""
----------------------------------------
Extract name.
Returns
-------
str
Name.
----------------------------------------
"""
pos = self.code.rfind("_")
if pos >= 0:
tokens = self.code.split("_")
if tokens[len(tokens) - 1].isdigit():
return self.code[0:pos]
return self.code
@property
def identifier(
self
) -> str:
"""
----------------------------------------
Extract identifier.
The identifier is the number at the end of the code if there are multiple indices with the same name
but with different parameters.
Returns
-------
str
Identifier.
----------------------------------------
"""
return self.code.replace(self.name, "").replace("_", "")
@property
def desc(
self,
) -> str:
"""
----------------------------------------
Get description.
Returns
-------
str
Description.
----------------------------------------
"""
desc = dict(code_props())[self.name][0]
# Number of parameters.
n_param = len(self.params)
def format_centile(
_param: str,
_unit: str
) -> str:
is_centile = "p" in _param
return _param.replace("p", "") + ("e centile" if is_centile else _unit)
for i in range(1, len(self.params) + 1):
param = str(self.params[i - 1])
val = ""
key = "{" + str(i) + "}"
# Assign 1st parameter.
if (i == 1) and (key in desc):
if self.name in [c.I_RAIN_SEASON_START, c.I_RAIN_SEASON_END, c.I_RAIN_SEASON_LENGTH,
c.I_RAIN_SEASON_PRCPTOT]:
val = self.identifier
if val != "":
val += "ère " if val == "1" else "e "
elif self.name in [c.I_HEAT_WAVE_MAX_LENGTH, c.I_HEAT_WAVE_TOTAL_LENGTH, c.I_HOT_SPELL_MAX_LENGTH,
c.I_HOT_SPELL_FREQUENCY, c.I_HOT_SPELL_TOTAL_LENGTH, c.I_TX_DAYS_ABOVE,
c.I_TN_DAYS_BELOW, c.I_TROPICAL_NIGHTS, c.I_WG_DAYS_ABOVE, c.I_WX_DAYS_ABOVE]:
unit = ""
if self.name in [c.I_HEAT_WAVE_MAX_LENGTH, c.I_HEAT_WAVE_TOTAL_LENGTH, c.I_HOT_SPELL_MAX_LENGTH,
c.I_HOT_SPELL_FREQUENCY, c.I_HOT_SPELL_TOTAL_LENGTH, c.I_TX_DAYS_ABOVE,
c.I_TN_DAYS_BELOW, c.I_TROPICAL_NIGHTS]:
unit = VarIdx(c.V_TASMAX).unit
elif self.name in [c.I_WG_DAYS_ABOVE, c.I_WX_DAYS_ABOVE]:
unit = VarIdx(c.V_SFCWINDMAX).unit
val = format_centile(param, unit)
elif self.name in [c.I_WSDI]:
unit = VarIdx(c.V_TASMAX).unit
val = format_centile("90p" if param == "nan" else param, unit)
elif param != "nan":
val = param
desc = desc.replace(key, val)
# Assign 2nd parameter.
elif (i == 2) and (key in desc):
if self.name == c.I_PRCPTOT:
val = param if n_param >= 2 else "1"
elif self.name in [c.I_HEAT_WAVE_MAX_LENGTH, c.I_HEAT_WAVE_TOTAL_LENGTH]:
unit = VarIdx(c.V_TASMAX).unit
val = format_centile(param, unit)
elif param != "nan":
val = param
desc = desc.replace(key, val)
# Assign 3rd parameter.
elif (i == 3) and (key in desc):
if self.name == c.I_PRCPTOT:
val = param if n_param >= 3 else "365"
elif param != "nan":
val = param
desc = desc.replace(key, val)
# Assign 4th parameter.
if (i == 4) and (key in desc):
if param != "nan":
val = param
desc = desc.replace(key, val)
# Special cases.
if (self.name == c.I_PRCPTOT) and (n_param >= 2):
desc += "(" + str(self.params[0]) + " à " + str(self.params[1]) + ")"
if (self.name == c.I_DRY_SPELL_TOTAL_LENGTH) and (n_param >= 5):
desc = desc.replace(")", "; " + str(self.params[3]) + " à " + str(self.params[4]) + ")")
return desc
@property
def label(
self
) -> str:
"""
----------------------------------------
Combine description and unit.
Returns
-------
str
Combine description and unit.
----------------------------------------
"""
label = dict(code_props())[extract_vi_name(self.code)][1]
unit = str(self.unit)
if (unit not in ["", "1"]) and (unit not in label):
label += " (" + unit + ")"
return label
@property
def unit(
self
) -> str:
"""
----------------------------------------
Get unit.
Returns
-------
str
Unit.
----------------------------------------
"""
return dict(code_props())[extract_vi_name(self.code)][2]
@property
def precision(
self
) -> int:
"""
----------------------------------------
Get precision (number of decimals).
Returns
-------
int
Precision (number of decimals).
----------------------------------------
"""
return dict(code_props())[extract_vi_name(self.code)][3]
@property
def is_var(
self
) -> bool:
"""
----------------------------------------
Determine if the instance is a recognized climate variable.
Returns
-------
bool
True if the current instance is a recognized climate variable.
----------------------------------------
"""
return self.name in (c.V_CORDEX + c.V_ECMWF + c.V_ENACTS + c.V_CHIRPS)
@property
def is_idx(
self
) -> bool:
"""
----------------------------------------
Determine if the instance is a recognized climate index.
Returns
-------
bool
True if the current instance is a recognized climate index.
----------------------------------------
"""
return self.name in c.indices
@property
def is_var_or_idx(
self
) -> bool:
"""
----------------------------------------
Determine if the instance is a recognized climate variable or climate index.
Returns
-------
bool
True if the current instance is a recognized climate variable or climate index.
----------------------------------------
"""
return self.is_var or self.is_idx
@property
def is_summable(
self
) -> bool:
"""
----------------------------------------
Determine if the variable is summable.
An summable variable can have its values summed up for any given frequency. For instance, we can calculate the
the sum of precipitation values in one month. This is not the case for temperature, for which a mean
value can be calculated instead.
----------------------------------------
"""
summable_variables = [c.V_PR, c.V_EVSPSBL, c.V_EVSPSBLPOT, c.V_ECMWF_TP, c.V_ECMWF_E, c.V_ECMWF_PEV,
c.V_ENACTS_RR, c.V_ENACTS_PET, c.V_CHIRPS_PRECIP]
return self.name in summable_variables
@property
def is_volumetric(
self
) -> bool:
"""
----------------------------------------
Determine if the variable is a volumetric.
An volumetric variable has a volume. For instance, an amount of precipitation has a corresponding height, area
or volume. This is not the case for temperature.
----------------------------------------
"""
volumetric_variables = [c.V_PR, c.V_EVSPSBL, c.V_EVSPSBLPOT, c.V_SNW, c.V_ECMWF_TP, c.V_ECMWF_E, c.V_ECMWF_PEV,
c.V_ECMWF_SNW, c.V_ENACTS_RR, c.V_ENACTS_PET, c.V_CHIRPS_PRECIP]
return self.name in volumetric_variables
@property
def is_group(
self
) -> bool:
"""
----------------------------------------
Determine if the instance is a group of indices.
----------------------------------------
"""
return group(self.code) == self.code
@property
def ens(
self
) -> str:
"""
----------------------------------------
Get ensemble name.
Returns
-------
str
Ensemble name.
----------------------------------------
"""
return self._ens
@ens.setter
def ens(
self,
ens: str
):
"""
----------------------------------------
Set ensemble name.
Parameters
----------
ens: str
Ensemble name.
----------------------------------------
"""
self._ens = ens
@property
def params(
self
) -> List[any]:
"""
----------------------------------------
Get parameters.
Returns
-------
List[any]
Parameters.
----------------------------------------
"""
# By default, use the available parameters.
params = self._params
# If no parameters are set, use those in the configuration file.
if len(params) == 0:
params = cntx.idx_params_from_code(self.code)
return params
@params.setter
def params(
self,
params: List[any]
):
"""
----------------------------------------
Set parameters.
Parameters
----------
params: List[any]
Parameters.
----------------------------------------
"""
self._params = params
def convert_name(
self,
ens: str
) -> Union[any, str]:
"""
----------------------------------------
Convert from CORDEX variable name to the equivalent variable name in another set (ERA5*, ENACTS, CHIRPS)
(or the opposite).
Parameters
----------
ens: str
Ensemble.
----------------------------------------
"""
# Equivalences.
# CORDEX ERA5* ENACTS CHIRPS
# ---------------------------------------------------------------------
equi = [[c.V_SFTLF, c.V_ECMWF_LSM, "", "" ],
[c.V_TAS, c.V_ECMWF_T2M, "", "" ],
[c.V_TASMIN, c.V_ECMWF_T2MMIN, c.V_ENACTS_TMIN, "" ],
[c.V_TASMAX, c.V_ECMWF_T2MMAX, c.V_ENACTS_TMAX, "" ],
[c.V_PR, c.V_ECMWF_TP, c.V_ENACTS_RR, c.V_CHIRPS_PRECIP],
[c.V_UAS, c.V_ECMWF_U10, "", "" ],
[c.V_VAS, c.V_ECMWF_V10, "", "" ],
[c.V_SFCWINDMAX, c.V_ECMWF_UV10MAX, "", "" ],
[c.V_PS, c.V_ECMWF_SP, "", "" ],
[c.V_RSDS, c.V_ECMWF_SSRD, "", "" ],
[c.V_EVSPSBL, c.V_ECMWF_E, "", "" ],
[c.V_EVSPSBLPOT, c.V_ECMWF_PEV, c.V_ENACTS_PET, "" ],
[c.V_HUSS, c.V_ECMWF_SH, "", "" ],
[c.V_SNW, c.V_ECMWF_SNW, "", "" ]]
# Loop through equivalences.
for i in range(len(equi)):
# Verify if there is a match.
if self.name in equi[i]:
if self.ens == c.ENS_CORDEX:
if ens in [c.ENS_ERA5, c.ENS_ERA5_LAND]:
return equi[i][1]
elif ens == c.ENS_ENACTS:
return equi[i][2]
elif ens == c.ENS_CHIRPS:
return equi[i][3]
else:
return equi[i][0]
return None
def equi_path(
self,
p: str,
vi_code_b: str,
rcp: str
) -> str:
"""
----------------------------------------
Determine the equivalent path for another variable or index.
Parameters
----------
p: str
Path associated with the current instance.
vi_code_b: str
Name of climate variable or index to replace with.
rcp: str
Emission scenario.
----------------------------------------
"""
# Determine if we have variables or indices.
vi_code_a = self.code
varidx_b = VarIdx(vi_code_b)
a_is_var = self.name in cntx.vars.code_l
b_is_var = varidx_b.name in cntx.vars.code_l
fn = os.path.basename(p)
# No conversion required.
if vi_code_a != vi_code_b:
# Variable->Variable or Index->Index.
if (a_is_var and b_is_var) or (not a_is_var and not b_is_var):
p = p.replace(self.name, varidx_b.name)
# Variable -> Index (or the opposite)
else:
# Variable -> Index.
if a_is_var and not b_is_var:
p = cntx.d_idx(vi_code_b)
# Index -> Variable.
else:
if rcp == c.REF:
p = cntx.d_ref(vi_code_b)
else:
p = cntx.d_scen(c.CAT_QQMAP, vi_code_b)
# Both.
if rcp == c.REF:
p += varidx_b.name + "_" + c.REF + "." + cntx.f_data_out
else:
p += fn.replace(self.name + "_", varidx_b.name + "_")
return p
@property
def requirements(
self
) -> List[str]:
"""
----------------------------------------
List the climate variables and indices that are required to calculate the current climate index.
Parameters
----------
List[str]
List of climate variable and index codes.
----------------------------------------
"""
vi_code_l = []
# Temperature.
if self.name in [c.I_TNX, c.I_TNG, c.I_TROPICAL_NIGHTS, c.I_TNG_MONTHS_BELOW, c.I_HEAT_WAVE_MAX_LENGTH,
c.I_HEAT_WAVE_TOTAL_LENGTH, c.I_TGG, c.I_ETR, c.I_TN_DAYS_BELOW]:
vi_code_l.append(c.V_TASMIN)
if self.name in [c.I_TX90P, c.I_TX_DAYS_ABOVE, c.I_HOT_SPELL_FREQUENCY, c.I_HOT_SPELL_MAX_LENGTH,
c.I_HOT_SPELL_TOTAL_LENGTH, c.I_TXG, c.I_TXX, c.I_WSDI, c.I_HEAT_WAVE_MAX_LENGTH,
c.I_HEAT_WAVE_TOTAL_LENGTH, c.I_TGG, c.I_ETR]:
vi_code_l.append(c.V_TASMAX)
# Precipitation.
if self.name in [c.I_RX1DAY, c.I_RX5DAY, c.I_CWD, c.I_CDD, c.I_SDII, c.I_PRCPTOT, c.I_R10MM, c.I_R20MM,
c.I_WET_DAYS, c.I_DRY_DAYS, c.I_RAIN_SEASON_START, c.I_RAIN_SEASON_END,
c.I_RAIN_SEASON_PRCPTOT, c.I_RAIN_SEASON_LENGTH, c.I_DRY_SPELL_TOTAL_LENGTH, c.I_RAIN_SEASON]:
if self.name != c.I_RAIN_SEASON_LENGTH:
vi_code_l.append(c.V_PR)
if self.name in [c.I_RAIN_SEASON_END, c.I_RAIN_SEASON]:
if c.V_EVSPSBLPOT in cntx.vars.code_l:
vi_code_l.append(c.V_EVSPSBLPOT)
elif c.V_EVSPSBL in cntx.vars.code_l:
vi_code_l.append(c.V_EVSPSBL)
else:
vi_code_l.append("nan")
if self.name in [c.I_RAIN_SEASON_END, c.I_RAIN_SEASON_LENGTH, c.I_RAIN_SEASON_PRCPTOT]:
vi_code_l.append(self.code.replace(self.name, c.I_RAIN_SEASON_START))
if self.name == c.I_RAIN_SEASON:
vi_code_l.append("nan")
if (self.name in [c.I_RAIN_SEASON_END, c.I_RAIN_SEASON]) and (len(self.params) > 7):
vi_code_l.append(str(self.params[len(self.params) - 1]))
if self.name in [c.I_RAIN_SEASON_LENGTH, c.I_RAIN_SEASON_PRCPTOT]:
vi_code_l.append(self.code.replace(self.name, c.I_RAIN_SEASON_END))
# Temperature-precipitation.
if self.name == c.I_DROUGHT_CODE:
vi_code_l.append(c.V_TAS)
vi_code_l.append(c.V_PR)
# Wind.
if self.name == c.I_WG_DAYS_ABOVE:
vi_code_l.append(c.V_UAS)
vi_code_l.append(c.V_VAS)
elif self.name == c.I_WX_DAYS_ABOVE:
vi_code_l.append(c.V_SFCWINDMAX)
return vi_code_l
def list_simulations(
self,
rcp_code: str
) -> List[str]:
"""
----------------------------------------
List simulations to consider.
A climate index can be excluded, based on:
- exception lists
- the availability of dependencies (climate scenarios and indices).
Parameters
----------
rcp_code: str
Emission scenario.
Returns
-------
List[str]
List of simulations.
----------------------------------------
"""
if self.is_var:
sim_code_l = self.list_simulations_for_scen(rcp_code)
else:
sim_code_l = self.list_simulations_for_idx(rcp_code)
return sim_code_l
def list_simulations_for_scen(
self,
rcp_code: Optional[str]
) -> List[str]:
"""
----------------------------------------
List available simulations.
Parameters
----------
rcp_code: Optional[str]
Emission scenario code.
Returns
-------
List[str]
List of simulation codes.
----------------------------------------
"""
# List all simulations.
p_sim_ref_l, p_sim_fut_l = cntx.list_cordex(self.name, rcp_code=rcp_code)
# Filter simulations.
sim_code_l = []
for p_sim_fut in p_sim_fut_l:
sim_code = os.path.basename(os.path.dirname(p_sim_fut)) + "_" + os.path.basename(p_sim_fut)
if not cntx.exclude_simulation(self.name, sim_code, c.CAT_SCEN):
sim_code_l.append(sim_code)
sim_code_l.sort()
return sim_code_l
def list_simulations_for_idx(
self,
rcp_code: Optional[str]
) -> List[str]:
"""
----------------------------------------
List simulations to consider.
A climate index can be excluded, based on:
- exception lists
- the availability of dependencies (climate scenarios and indices).
Parameters
----------
rcp_code: Optional[str]
Emission scenario.
Returns
-------
List[str]
List of simulations.
----------------------------------------
"""
# Function that extracts simulation code from a path.
def extract_sim_code(
p: str
) -> str:
if (c.CAT_SCEN + cntx.sep + c.CAT_OBS in p) or ("_" + c.REF in p):
return c.REF
else:
return os.path.basename(p).replace(vi_name_0 + "_", "").replace("." + cntx.f_data_out, "")
# List required climate variables and indices.
vi_code_l = self.requirements
# Verify if variable or index is available for the current station.
for vi_code in vi_code_l:
if ((vi_code != "nan") and
((not os.path.isdir(cntx.d_scen(c.CAT_QQMAP, vi_code))) and
(not os.path.isdir(cntx.d_idx(vi_code))))):
return []
# List simulation files for the first variable. As soon as there is no file for one variable, the
# analysis for the current RCP needs to abort.
varidx_0 = VarIdx(vi_code_l[0])
vi_code_0 = varidx_0.code
vi_name_0 = varidx_0.name
vi_code_grp_0 = group(vi_code_0) if varidx_0.is_group else vi_code_0
sim_code = "*_" + rcp_code
if varidx_0.is_var:
p_sim_l = [cntx.p_scen(c.CAT_OBS, vi_name_0, c.REF)] +\
list(glob.glob(cntx.p_scen(c.CAT_QQMAP, vi_name_0, sim_code)))
else:
p_sim_l = [cntx.p_idx(vi_code_grp_0, vi_name_0, c.REF)] +\
list(glob.glob(cntx.p_idx(vi_code_grp_0, vi_name_0, sim_code)))
if not p_sim_l:
return []
sim_code_l = []
# Select the simulations that are not present in an exception list.
p_sim_filter_l = []
for p_sim in p_sim_l:
sim_code = extract_sim_code(p_sim)
if (not cntx.exclude_simulation(self.name, sim_code)) and os.path.exists(p_sim):
p_sim_filter_l.append(p_sim)
sim_code_l.append(sim_code)
p_sim_l = p_sim_filter_l
# Ensure that simulations are available for other variables than the first one.
if len(vi_code_l) > 1:
# Loop through simulations.
for p_sim_i in p_sim_l:
# Determine if there is at least one dataset missing.
missing = False
for vi_code_j in vi_code_l[1:]:
if vi_code_j != "nan":
p_sim_j = varidx_0.equi_path(p_sim_i, vi_code_j, rcp_code)
if not os.path.exists(p_sim_j):
missing = True
break
# Remove simulation code.
if missing:
sim_code = extract_sim_code(p_sim_i)
if sim_code in sim_code_l:
sim_code_l.remove(sim_code)
sim_code_l.sort()
return sim_code_l
class VarIdxs(cl_object.Objs):
"""
--------------------------------------------------------------------------------------------------------------------
Class defining the object VarIdxs.
--------------------------------------------------------------------------------------------------------------------
"""
def __init__(
self,
*args
):
"""
----------------------------------------
Constructor.
----------------------------------------
"""
super().__init__()
if len(args) > 0:
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
# The items are extracted from file names.
# ~/<project_code>/<view_code>/*/*.csv
if view_code == c.VIEW_TBL:
pattern = project_code + "/<view_code>/*/*.csv"
pattern = pattern.replace("<view_code>/", view_code + "/")
p_l = list(cntx.files(pattern)[cl_gd.PROP_PATH])
for p_i in p_l:
code = os.path.basename(os.path.dirname(p_i))
if code not in code_l:
code_l.append(code)
# The items are extracted from directory names.
# ~/<project_code>/<view_code>/<varidx_code>/*
elif view_code in [c.VIEW_TS, c.VIEW_MAP, c.VIEW_CYCLE, c.VIEW_TS_BIAS, c.VIEW_CLUSTER, c.VIEW_TAYLOR]:
pattern = project_code + "/<view_code>/*"
if view_code == c.VIEW_CYCLE:
pattern = pattern.replace("<view_code>/", view_code + "*/")
else:
pattern = pattern.replace("<view_code>/", view_code + "/")
p_l = list(cntx.files(pattern)[cl_gd.PROP_PATH])
p_l = list(set([p.replace(project_code + "/", "") for p in p_l]))
code_l = list(set([p.split("/")[1] for p in p_l if len(p.split("/")) > 0]))
# Remove any item that is not a climate variable or climate index.
if view_code == c.VIEW_MAP:
code_l = [code for code in code_l if VarIdx(code).is_var_or_idx]