-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathvariables.py
166 lines (129 loc) · 4.33 KB
/
variables.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Autopep8: https://pypi.org/project/autopep8/
# Check with http://pep8online.com/
import proplot as plot
# =============================================================================
# Select variable on CICLAD
# =============================================================================
def get_table(var):
if var in ['tas', 'pr', 'prsn', 'ta']:
table = 'Amon'
elif var in ['snc']:
table = 'LImon'
else:
raise NameError('The variable ' + name + ' is not defined')
return table
def get_var_infos(var):
"""
Get default informations about variable as: label, units, levels, cmap,
extend for regular plot, differences and bias.
Parameters
----------
var : str
Variable name. Options are:
- 'snc', 'frac_snow' (Snow Cover Extent)
- 'tas', 't2m', 'tmp (Near-Surface Air Temperature)
- 'pr' (Total Precipitation)
- 'ta' (Air Temperature)
Returns
-------
label : str
Name of the variable.
units : str
Usual units of the variable.
levels, levels_diff, levels_bias : ndarray
Levels.
cmap, cmap_diff, cmap_bias : str
Usual colormap for the varibale.
extend, extend, extend : {{'neither', 'min', 'max', 'both'}}
Where to assign unique colors to out-of-bounds data and draw
"extensions" (triangles, by default) on the colorbar.
Example
-------
>>> import sys
>>> sys.path.insert(1, '/home/mlalande/notebooks/utils')
>>> import utils as u
>>>
>>> label, units, \
levels, cmap, extend, \
levels_diff, cmap_diff, extend_diff, \
levels_bias, cmap_bias, extend_bias = u.get_var_infos('snc')
"""
# Snow Cover Extent
if var in ['snc', 'frac_snow']:
label = 'Snow Cover Extent'
units = '%'
levels = plot.arange(0, 100, 10)
cmap = 'viridis'
extend = 'neither'
levels_diff = plot.arange(-50, 50, 10)
cmap_diff = 'BuRd_r'
extend_diff = 'both'
levels_bias = plot.arange(-50, 50, 10)
cmap_bias = 'BuRd_r'
extend_bias = 'both'
# Near-Surface Air Temperature
elif var in ['tas', 't2m', 'tmp']:
label = 'Near-Surface Air Temperature'
units = '°C'
levels = plot.arange(-30, 30, 5)
cmap = 'CoolWarm'
extend = 'neither'
levels_diff = plot.arange(-5, 5, 1)
cmap_diff = 'BuRd'
extend_diff = 'both'
levels_bias = plot.arange(-14, 14, 2)
cmap_bias = 'BuRd'
extend_bias = 'both'
# Total Precipitation
elif var in ['pr', 'tp', 'precip']:
label = 'Total Precipitation'
units = 'mm/day'
levels = plot.arange(0, 5, 0.5)
cmap = 'DryWet'
extend = 'neither'
levels_diff = plot.arange(-1, 1, 0.2)
cmap_diff = 'BuRd_r'
extend_diff = 'both'
levels_bias = plot.arange(-5, 5, 1)
cmap_bias = 'BuRd_r'
extend_bias = 'both'
# Snowfall
elif var == 'prsn':
label = 'Snowfall'
units = 'mm/day'
cmap = 'DryWet'
levels = plot.arange(0, 5, 0.5)
# Eastward Wind
elif var == 'ua':
label = 'Eastward Wind'
units = 'm/s'
cmap = 'CoolWarm'
levels = plot.arange(-7, 7, 1)
# Air Temperature
elif var == 'ta':
label = 'Air Temperature'
units = '°C'
levels = plot.arange(-30, 30, 2)
cmap = 'Spectral'
extend = 'both'
levels_diff = plot.arange(-1, 1, 0.2)
cmap_diff = 'BuRd'
extend_diff = 'both'
levels_bias = plot.arange(-7, 7, 1)
cmap_bias = 'BuRd'
extend_bias = 'neither'
else:
raise ValueError(
f"""Invalid variable argument: '{var}'. Valid names are:
- 'snc', 'frac_snow' (Snow Cover Extent)
- 'tas', 't2m', 'tmp (Near-Surface Air Temperature)
- 'pr' (Total Precipitation)
- 'ta' (Air Temperature)
"""
)
return label, units, \
levels, cmap, extend, \
levels_diff, cmap_diff, extend_diff, \
levels_bias, cmap_bias, extend_bias